blob: f87878274e9fbb85f1dc3b95cf6d6f90ced1fa24 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Alexander Block31db9f72012-07-25 23:19:24 +02002/*
3 * Copyright (C) 2012 Alexander Block. All rights reserved.
Alexander Block31db9f72012-07-25 23:19:24 +02004 */
5
6#include <linux/bsearch.h>
7#include <linux/fs.h>
8#include <linux/file.h>
9#include <linux/sort.h>
10#include <linux/mount.h>
11#include <linux/xattr.h>
12#include <linux/posix_acl_xattr.h>
13#include <linux/radix-tree.h>
Stephen Rothwella1857eb2012-07-27 10:11:13 +100014#include <linux/vmalloc.h>
Andy Shevchenkoed848852013-08-21 10:32:13 +030015#include <linux/string.h>
Josef Bacik2351f432017-09-27 10:43:13 -040016#include <linux/compat.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020017#include <linux/crc32c.h>
Alexander Block31db9f72012-07-25 23:19:24 +020018
19#include "send.h"
20#include "backref.h"
21#include "locking.h"
22#include "disk-io.h"
23#include "btrfs_inode.h"
24#include "transaction.h"
Anand Jainebb87652016-03-10 17:26:59 +080025#include "compression.h"
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -030026#include "xattr.h"
Alexander Block31db9f72012-07-25 23:19:24 +020027
Alexander Block31db9f72012-07-25 23:19:24 +020028/*
Filipe Mananafd0ddbe2019-10-30 12:23:01 +000029 * Maximum number of references an extent can have in order for us to attempt to
30 * issue clone operations instead of write operations. This currently exists to
31 * avoid hitting limitations of the backreference walking code (taking a lot of
32 * time and using too much memory for extents with large number of references).
33 */
34#define SEND_MAX_EXTENT_REFS 64
35
36/*
Alexander Block31db9f72012-07-25 23:19:24 +020037 * A fs_path is a helper to dynamically build path names with unknown size.
38 * It reallocates the internal buffer on demand.
39 * It allows fast adding of path elements on the right side (normal path) and
40 * fast adding to the left side (reversed path). A reversed path can also be
41 * unreversed if needed.
42 */
43struct fs_path {
44 union {
45 struct {
46 char *start;
47 char *end;
Alexander Block31db9f72012-07-25 23:19:24 +020048
49 char *buf;
David Sterba1f5a7ff2014-02-03 19:23:47 +010050 unsigned short buf_len:15;
51 unsigned short reversed:1;
Alexander Block31db9f72012-07-25 23:19:24 +020052 char inline_buf[];
53 };
David Sterbaace01052014-02-05 16:17:34 +010054 /*
55 * Average path length does not exceed 200 bytes, we'll have
56 * better packing in the slab and higher chance to satisfy
57 * a allocation later during send.
58 */
59 char pad[256];
Alexander Block31db9f72012-07-25 23:19:24 +020060 };
61};
62#define FS_PATH_INLINE_SIZE \
63 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
64
65
66/* reused for each extent */
67struct clone_root {
68 struct btrfs_root *root;
69 u64 ino;
70 u64 offset;
71
72 u64 found_refs;
73};
74
75#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
76#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
77
78struct send_ctx {
79 struct file *send_filp;
80 loff_t send_off;
81 char *send_buf;
82 u32 send_size;
83 u32 send_max_size;
84 u64 total_send_size;
85 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000086 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020087
Alexander Block31db9f72012-07-25 23:19:24 +020088 struct btrfs_root *send_root;
89 struct btrfs_root *parent_root;
90 struct clone_root *clone_roots;
91 int clone_roots_cnt;
92
93 /* current state of the compare_tree call */
94 struct btrfs_path *left_path;
95 struct btrfs_path *right_path;
96 struct btrfs_key *cmp_key;
97
98 /*
99 * infos of the currently processed inode. In case of deleted inodes,
100 * these are the values from the deleted inode.
101 */
102 u64 cur_ino;
103 u64 cur_inode_gen;
104 int cur_inode_new;
105 int cur_inode_new_gen;
106 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200107 u64 cur_inode_size;
108 u64 cur_inode_mode;
Liu Bo644d1942014-02-27 17:29:01 +0800109 u64 cur_inode_rdev;
Josef Bacik16e75492013-10-22 12:18:51 -0400110 u64 cur_inode_last_extent;
Filipe Mananaffa7c422018-02-06 20:40:40 +0000111 u64 cur_inode_next_write_offset;
Filipe Manana46b2f452018-07-24 11:54:04 +0100112 bool ignore_cur_inode;
Alexander Block31db9f72012-07-25 23:19:24 +0200113
114 u64 send_progress;
115
116 struct list_head new_refs;
117 struct list_head deleted_refs;
118
119 struct radix_tree_root name_cache;
120 struct list_head name_cache_list;
121 int name_cache_size;
122
Liu Bo2131bcd2014-03-05 10:07:35 +0800123 struct file_ra_state ra;
124
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000125 /*
126 * We process inodes by their increasing order, so if before an
127 * incremental send we reverse the parent/child relationship of
128 * directories such that a directory with a lower inode number was
129 * the parent of a directory with a higher inode number, and the one
130 * becoming the new parent got renamed too, we can't rename/move the
131 * directory with lower inode number when we finish processing it - we
132 * must process the directory with higher inode number first, then
133 * rename/move it and then rename/move the directory with lower inode
134 * number. Example follows.
135 *
136 * Tree state when the first send was performed:
137 *
138 * .
139 * |-- a (ino 257)
140 * |-- b (ino 258)
141 * |
142 * |
143 * |-- c (ino 259)
144 * | |-- d (ino 260)
145 * |
146 * |-- c2 (ino 261)
147 *
148 * Tree state when the second (incremental) send is performed:
149 *
150 * .
151 * |-- a (ino 257)
152 * |-- b (ino 258)
153 * |-- c2 (ino 261)
154 * |-- d2 (ino 260)
155 * |-- cc (ino 259)
156 *
157 * The sequence of steps that lead to the second state was:
158 *
159 * mv /a/b/c/d /a/b/c2/d2
160 * mv /a/b/c /a/b/c2/d2/cc
161 *
162 * "c" has lower inode number, but we can't move it (2nd mv operation)
163 * before we move "d", which has higher inode number.
164 *
165 * So we just memorize which move/rename operations must be performed
166 * later when their respective parent is processed and moved/renamed.
167 */
168
169 /* Indexed by parent directory inode number. */
170 struct rb_root pending_dir_moves;
171
172 /*
173 * Reverse index, indexed by the inode number of a directory that
174 * is waiting for the move/rename of its immediate parent before its
175 * own move/rename can be performed.
176 */
177 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000178
179 /*
180 * A directory that is going to be rm'ed might have a child directory
181 * which is in the pending directory moves index above. In this case,
182 * the directory can only be removed after the move/rename of its child
183 * is performed. Example:
184 *
185 * Parent snapshot:
186 *
187 * . (ino 256)
188 * |-- a/ (ino 257)
189 * |-- b/ (ino 258)
190 * |-- c/ (ino 259)
191 * | |-- x/ (ino 260)
192 * |
193 * |-- y/ (ino 261)
194 *
195 * Send snapshot:
196 *
197 * . (ino 256)
198 * |-- a/ (ino 257)
199 * |-- b/ (ino 258)
200 * |-- YY/ (ino 261)
201 * |-- x/ (ino 260)
202 *
203 * Sequence of steps that lead to the send snapshot:
204 * rm -f /a/b/c/foo.txt
205 * mv /a/b/y /a/b/YY
206 * mv /a/b/c/x /a/b/YY
207 * rmdir /a/b/c
208 *
209 * When the child is processed, its move/rename is delayed until its
210 * parent is processed (as explained above), but all other operations
211 * like update utimes, chown, chgrp, etc, are performed and the paths
212 * that it uses for those operations must use the orphanized name of
213 * its parent (the directory we're going to rm later), so we need to
214 * memorize that name.
215 *
216 * Indexed by the inode number of the directory to be deleted.
217 */
218 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000219};
220
221struct pending_dir_move {
222 struct rb_node node;
223 struct list_head list;
224 u64 parent_ino;
225 u64 ino;
226 u64 gen;
227 struct list_head update_refs;
228};
229
230struct waiting_dir_move {
231 struct rb_node node;
232 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000233 /*
234 * There might be some directory that could not be removed because it
235 * was waiting for this directory inode to be moved first. Therefore
236 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
237 */
238 u64 rmdir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +0000239 u64 rmdir_gen;
Filipe Manana8b191a62015-04-09 14:09:14 +0100240 bool orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +0000241};
242
243struct orphan_dir_info {
244 struct rb_node node;
245 u64 ino;
246 u64 gen;
Robbie Ko0f96f512018-05-08 18:11:38 +0800247 u64 last_dir_index_offset;
Alexander Block31db9f72012-07-25 23:19:24 +0200248};
249
250struct name_cache_entry {
251 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200252 /*
253 * radix_tree has only 32bit entries but we need to handle 64bit inums.
254 * We use the lower 32bit of the 64bit inum to store it in the tree. If
255 * more then one inum would fall into the same entry, we use radix_list
256 * to store the additional entries. radix_list is also used to store
257 * entries where two entries have the same inum but different
258 * generations.
259 */
260 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200261 u64 ino;
262 u64 gen;
263 u64 parent_ino;
264 u64 parent_gen;
265 int ret;
266 int need_later_update;
267 int name_len;
268 char name[];
269};
270
David Sterba18d0f5c2019-08-21 19:12:59 +0200271#define ADVANCE 1
272#define ADVANCE_ONLY_NEXT -1
273
274enum btrfs_compare_tree_result {
275 BTRFS_COMPARE_TREE_NEW,
276 BTRFS_COMPARE_TREE_DELETED,
277 BTRFS_COMPARE_TREE_CHANGED,
278 BTRFS_COMPARE_TREE_SAME,
279};
David Sterba18d0f5c2019-08-21 19:12:59 +0200280
David Sterbae67c7182018-02-19 17:24:18 +0100281__cold
Filipe Manana95155582016-08-01 01:50:37 +0100282static void inconsistent_snapshot_error(struct send_ctx *sctx,
283 enum btrfs_compare_tree_result result,
284 const char *what)
285{
286 const char *result_string;
287
288 switch (result) {
289 case BTRFS_COMPARE_TREE_NEW:
290 result_string = "new";
291 break;
292 case BTRFS_COMPARE_TREE_DELETED:
293 result_string = "deleted";
294 break;
295 case BTRFS_COMPARE_TREE_CHANGED:
296 result_string = "updated";
297 break;
298 case BTRFS_COMPARE_TREE_SAME:
299 ASSERT(0);
300 result_string = "unchanged";
301 break;
302 default:
303 ASSERT(0);
304 result_string = "unexpected";
305 }
306
307 btrfs_err(sctx->send_root->fs_info,
308 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
309 result_string, what, sctx->cmp_key->objectid,
310 sctx->send_root->root_key.objectid,
311 (sctx->parent_root ?
312 sctx->parent_root->root_key.objectid : 0));
313}
314
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000315static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
316
Filipe Manana9dc44212014-02-19 14:31:44 +0000317static struct waiting_dir_move *
318get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
319
Filipe Manana0b3f4072020-12-10 12:09:02 +0000320static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
Filipe Manana9dc44212014-02-19 14:31:44 +0000321
Josef Bacik16e75492013-10-22 12:18:51 -0400322static int need_send_hole(struct send_ctx *sctx)
323{
324 return (sctx->parent_root && !sctx->cur_inode_new &&
325 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
326 S_ISREG(sctx->cur_inode_mode));
327}
328
Alexander Block31db9f72012-07-25 23:19:24 +0200329static void fs_path_reset(struct fs_path *p)
330{
331 if (p->reversed) {
332 p->start = p->buf + p->buf_len - 1;
333 p->end = p->start;
334 *p->start = 0;
335 } else {
336 p->start = p->buf;
337 p->end = p->start;
338 *p->start = 0;
339 }
340}
341
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000342static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200343{
344 struct fs_path *p;
345
David Sterbae780b0d2016-01-18 18:42:13 +0100346 p = kmalloc(sizeof(*p), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +0200347 if (!p)
348 return NULL;
349 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200350 p->buf = p->inline_buf;
351 p->buf_len = FS_PATH_INLINE_SIZE;
352 fs_path_reset(p);
353 return p;
354}
355
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000356static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200357{
358 struct fs_path *p;
359
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000360 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200361 if (!p)
362 return NULL;
363 p->reversed = 1;
364 fs_path_reset(p);
365 return p;
366}
367
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000368static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200369{
370 if (!p)
371 return;
David Sterbaace01052014-02-05 16:17:34 +0100372 if (p->buf != p->inline_buf)
373 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200374 kfree(p);
375}
376
377static int fs_path_len(struct fs_path *p)
378{
379 return p->end - p->start;
380}
381
382static int fs_path_ensure_buf(struct fs_path *p, int len)
383{
384 char *tmp_buf;
385 int path_len;
386 int old_buf_len;
387
388 len++;
389
390 if (p->buf_len >= len)
391 return 0;
392
Chris Masoncfd4a532014-04-26 05:02:03 -0700393 if (len > PATH_MAX) {
394 WARN_ON(1);
395 return -ENOMEM;
396 }
397
David Sterba1b2782c2014-02-25 19:32:59 +0100398 path_len = p->end - p->start;
399 old_buf_len = p->buf_len;
400
David Sterbaace01052014-02-05 16:17:34 +0100401 /*
402 * First time the inline_buf does not suffice
403 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100404 if (p->buf == p->inline_buf) {
David Sterbae780b0d2016-01-18 18:42:13 +0100405 tmp_buf = kmalloc(len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100406 if (tmp_buf)
407 memcpy(tmp_buf, p->buf, old_buf_len);
408 } else {
David Sterbae780b0d2016-01-18 18:42:13 +0100409 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100410 }
David Sterba9c9ca002014-02-25 19:33:08 +0100411 if (!tmp_buf)
412 return -ENOMEM;
413 p->buf = tmp_buf;
414 /*
415 * The real size of the buffer is bigger, this will let the fast path
416 * happen most of the time
417 */
418 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100419
Alexander Block31db9f72012-07-25 23:19:24 +0200420 if (p->reversed) {
421 tmp_buf = p->buf + old_buf_len - path_len - 1;
422 p->end = p->buf + p->buf_len - 1;
423 p->start = p->end - path_len;
424 memmove(p->start, tmp_buf, path_len + 1);
425 } else {
426 p->start = p->buf;
427 p->end = p->start + path_len;
428 }
429 return 0;
430}
431
David Sterbab23ab572014-02-03 19:23:19 +0100432static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
433 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200434{
435 int ret;
436 int new_len;
437
438 new_len = p->end - p->start + name_len;
439 if (p->start != p->end)
440 new_len++;
441 ret = fs_path_ensure_buf(p, new_len);
442 if (ret < 0)
443 goto out;
444
445 if (p->reversed) {
446 if (p->start != p->end)
447 *--p->start = '/';
448 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100449 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200450 } else {
451 if (p->start != p->end)
452 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100453 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200454 p->end += name_len;
455 *p->end = 0;
456 }
457
458out:
459 return ret;
460}
461
462static int fs_path_add(struct fs_path *p, const char *name, int name_len)
463{
464 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100465 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200466
David Sterbab23ab572014-02-03 19:23:19 +0100467 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200468 if (ret < 0)
469 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100470 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200471
472out:
473 return ret;
474}
475
476static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
477{
478 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100479 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200480
David Sterbab23ab572014-02-03 19:23:19 +0100481 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200482 if (ret < 0)
483 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100484 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200485
486out:
487 return ret;
488}
489
490static int fs_path_add_from_extent_buffer(struct fs_path *p,
491 struct extent_buffer *eb,
492 unsigned long off, int len)
493{
494 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100495 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200496
David Sterbab23ab572014-02-03 19:23:19 +0100497 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200498 if (ret < 0)
499 goto out;
500
David Sterbab23ab572014-02-03 19:23:19 +0100501 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200502
503out:
504 return ret;
505}
506
Alexander Block31db9f72012-07-25 23:19:24 +0200507static int fs_path_copy(struct fs_path *p, struct fs_path *from)
508{
509 int ret;
510
511 p->reversed = from->reversed;
512 fs_path_reset(p);
513
514 ret = fs_path_add_path(p, from);
515
516 return ret;
517}
518
519
520static void fs_path_unreverse(struct fs_path *p)
521{
522 char *tmp;
523 int len;
524
525 if (!p->reversed)
526 return;
527
528 tmp = p->start;
529 len = p->end - p->start;
530 p->start = p->buf;
531 p->end = p->start + len;
532 memmove(p->start, tmp, len + 1);
533 p->reversed = 0;
534}
535
536static struct btrfs_path *alloc_path_for_send(void)
537{
538 struct btrfs_path *path;
539
540 path = btrfs_alloc_path();
541 if (!path)
542 return NULL;
543 path->search_commit_root = 1;
544 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400545 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200546 return path;
547}
548
Eric Sandeen48a3b632013-04-25 20:41:01 +0000549static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200550{
551 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200552 u32 pos = 0;
553
Alexander Block31db9f72012-07-25 23:19:24 +0200554 while (pos < len) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200555 ret = kernel_write(filp, buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200556 /* TODO handle that correctly */
557 /*if (ret == -ERESTARTSYS) {
558 continue;
559 }*/
560 if (ret < 0)
Christoph Hellwig8e931572017-09-01 17:39:19 +0200561 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200562 if (ret == 0) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200563 return -EIO;
Alexander Block31db9f72012-07-25 23:19:24 +0200564 }
565 pos += ret;
566 }
567
Christoph Hellwig8e931572017-09-01 17:39:19 +0200568 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200569}
570
571static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
572{
573 struct btrfs_tlv_header *hdr;
574 int total_len = sizeof(*hdr) + len;
575 int left = sctx->send_max_size - sctx->send_size;
576
577 if (unlikely(left < total_len))
578 return -EOVERFLOW;
579
580 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
David Sterbae2f896b2020-09-15 10:54:23 +0200581 put_unaligned_le16(attr, &hdr->tlv_type);
582 put_unaligned_le16(len, &hdr->tlv_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200583 memcpy(hdr + 1, data, len);
584 sctx->send_size += total_len;
585
586 return 0;
587}
588
David Sterba95bc79d2013-12-16 17:34:10 +0100589#define TLV_PUT_DEFINE_INT(bits) \
590 static int tlv_put_u##bits(struct send_ctx *sctx, \
591 u##bits attr, u##bits value) \
592 { \
593 __le##bits __tmp = cpu_to_le##bits(value); \
594 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
595 }
Alexander Block31db9f72012-07-25 23:19:24 +0200596
David Sterba95bc79d2013-12-16 17:34:10 +0100597TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200598
599static int tlv_put_string(struct send_ctx *sctx, u16 attr,
600 const char *str, int len)
601{
602 if (len == -1)
603 len = strlen(str);
604 return tlv_put(sctx, attr, str, len);
605}
606
607static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
608 const u8 *uuid)
609{
610 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
611}
612
Alexander Block31db9f72012-07-25 23:19:24 +0200613static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
614 struct extent_buffer *eb,
615 struct btrfs_timespec *ts)
616{
617 struct btrfs_timespec bts;
618 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
619 return tlv_put(sctx, attr, &bts, sizeof(bts));
620}
621
622
Liu Bo895a72b2018-03-02 18:05:49 -0700623#define TLV_PUT(sctx, attrtype, data, attrlen) \
Alexander Block31db9f72012-07-25 23:19:24 +0200624 do { \
Liu Bo895a72b2018-03-02 18:05:49 -0700625 ret = tlv_put(sctx, attrtype, data, attrlen); \
Alexander Block31db9f72012-07-25 23:19:24 +0200626 if (ret < 0) \
627 goto tlv_put_failure; \
628 } while (0)
629
630#define TLV_PUT_INT(sctx, attrtype, bits, value) \
631 do { \
632 ret = tlv_put_u##bits(sctx, attrtype, value); \
633 if (ret < 0) \
634 goto tlv_put_failure; \
635 } while (0)
636
637#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
638#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
639#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
640#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
641#define TLV_PUT_STRING(sctx, attrtype, str, len) \
642 do { \
643 ret = tlv_put_string(sctx, attrtype, str, len); \
644 if (ret < 0) \
645 goto tlv_put_failure; \
646 } while (0)
647#define TLV_PUT_PATH(sctx, attrtype, p) \
648 do { \
649 ret = tlv_put_string(sctx, attrtype, p->start, \
650 p->end - p->start); \
651 if (ret < 0) \
652 goto tlv_put_failure; \
653 } while(0)
654#define TLV_PUT_UUID(sctx, attrtype, uuid) \
655 do { \
656 ret = tlv_put_uuid(sctx, attrtype, uuid); \
657 if (ret < 0) \
658 goto tlv_put_failure; \
659 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200660#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
661 do { \
662 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
663 if (ret < 0) \
664 goto tlv_put_failure; \
665 } while (0)
666
667static int send_header(struct send_ctx *sctx)
668{
669 struct btrfs_stream_header hdr;
670
671 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
672 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
673
Anand Jain1bcea352012-09-14 00:04:21 -0600674 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
675 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200676}
677
678/*
679 * For each command/item we want to send to userspace, we call this function.
680 */
681static int begin_cmd(struct send_ctx *sctx, int cmd)
682{
683 struct btrfs_cmd_header *hdr;
684
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530685 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200686 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200687
688 BUG_ON(sctx->send_size);
689
690 sctx->send_size += sizeof(*hdr);
691 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
David Sterbae2f896b2020-09-15 10:54:23 +0200692 put_unaligned_le16(cmd, &hdr->cmd);
Alexander Block31db9f72012-07-25 23:19:24 +0200693
694 return 0;
695}
696
697static int send_cmd(struct send_ctx *sctx)
698{
699 int ret;
700 struct btrfs_cmd_header *hdr;
701 u32 crc;
702
703 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
David Sterbae2f896b2020-09-15 10:54:23 +0200704 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
705 put_unaligned_le32(0, &hdr->crc);
Alexander Block31db9f72012-07-25 23:19:24 +0200706
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200707 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
David Sterbae2f896b2020-09-15 10:54:23 +0200708 put_unaligned_le32(crc, &hdr->crc);
Alexander Block31db9f72012-07-25 23:19:24 +0200709
Anand Jain1bcea352012-09-14 00:04:21 -0600710 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
711 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200712
713 sctx->total_send_size += sctx->send_size;
David Sterbae2f896b2020-09-15 10:54:23 +0200714 sctx->cmd_send_size[get_unaligned_le16(&hdr->cmd)] += sctx->send_size;
Alexander Block31db9f72012-07-25 23:19:24 +0200715 sctx->send_size = 0;
716
717 return ret;
718}
719
720/*
721 * Sends a move instruction to user space
722 */
723static int send_rename(struct send_ctx *sctx,
724 struct fs_path *from, struct fs_path *to)
725{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400726 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200727 int ret;
728
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400729 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200730
731 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
732 if (ret < 0)
733 goto out;
734
735 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
736 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
737
738 ret = send_cmd(sctx);
739
740tlv_put_failure:
741out:
742 return ret;
743}
744
745/*
746 * Sends a link instruction to user space
747 */
748static int send_link(struct send_ctx *sctx,
749 struct fs_path *path, struct fs_path *lnk)
750{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400751 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200752 int ret;
753
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400754 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200755
756 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
757 if (ret < 0)
758 goto out;
759
760 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
761 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
762
763 ret = send_cmd(sctx);
764
765tlv_put_failure:
766out:
767 return ret;
768}
769
770/*
771 * Sends an unlink instruction to user space
772 */
773static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
774{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400775 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200776 int ret;
777
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400778 btrfs_debug(fs_info, "send_unlink %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200779
780 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
781 if (ret < 0)
782 goto out;
783
784 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
785
786 ret = send_cmd(sctx);
787
788tlv_put_failure:
789out:
790 return ret;
791}
792
793/*
794 * Sends a rmdir instruction to user space
795 */
796static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
797{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400798 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200799 int ret;
800
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400801 btrfs_debug(fs_info, "send_rmdir %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200802
803 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
804 if (ret < 0)
805 goto out;
806
807 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
808
809 ret = send_cmd(sctx);
810
811tlv_put_failure:
812out:
813 return ret;
814}
815
816/*
817 * Helper function to retrieve some fields from an inode item.
818 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400819static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
820 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
821 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200822{
823 int ret;
824 struct btrfs_inode_item *ii;
825 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200826
827 key.objectid = ino;
828 key.type = BTRFS_INODE_ITEM_KEY;
829 key.offset = 0;
830 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200831 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400832 if (ret > 0)
833 ret = -ENOENT;
834 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200835 }
836
837 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
838 struct btrfs_inode_item);
839 if (size)
840 *size = btrfs_inode_size(path->nodes[0], ii);
841 if (gen)
842 *gen = btrfs_inode_generation(path->nodes[0], ii);
843 if (mode)
844 *mode = btrfs_inode_mode(path->nodes[0], ii);
845 if (uid)
846 *uid = btrfs_inode_uid(path->nodes[0], ii);
847 if (gid)
848 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200849 if (rdev)
850 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200851
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400852 return ret;
853}
854
855static int get_inode_info(struct btrfs_root *root,
856 u64 ino, u64 *size, u64 *gen,
857 u64 *mode, u64 *uid, u64 *gid,
858 u64 *rdev)
859{
860 struct btrfs_path *path;
861 int ret;
862
863 path = alloc_path_for_send();
864 if (!path)
865 return -ENOMEM;
866 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
867 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200868 btrfs_free_path(path);
869 return ret;
870}
871
872typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
873 struct fs_path *p,
874 void *ctx);
875
876/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000877 * Helper function to iterate the entries in ONE btrfs_inode_ref or
878 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200879 * The iterate callback may return a non zero value to stop iteration. This can
880 * be a negative value for error codes or 1 to simply stop it.
881 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000882 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200883 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000884static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200885 struct btrfs_key *found_key, int resolve,
886 iterate_inode_ref_t iterate, void *ctx)
887{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000888 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200889 struct btrfs_item *item;
890 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000891 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200892 struct btrfs_path *tmp_path;
893 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000894 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200895 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000896 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200897 u32 name_len;
898 char *start;
899 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000900 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200901 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000902 u64 dir;
903 unsigned long name_off;
904 unsigned long elem_size;
905 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200906
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000907 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200908 if (!p)
909 return -ENOMEM;
910
911 tmp_path = alloc_path_for_send();
912 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000913 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200914 return -ENOMEM;
915 }
916
Alexander Block31db9f72012-07-25 23:19:24 +0200917
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000918 if (found_key->type == BTRFS_INODE_REF_KEY) {
919 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
920 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100921 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000922 total = btrfs_item_size(eb, item);
923 elem_size = sizeof(*iref);
924 } else {
925 ptr = btrfs_item_ptr_offset(eb, slot);
926 total = btrfs_item_size_nr(eb, slot);
927 elem_size = sizeof(*extref);
928 }
929
Alexander Block31db9f72012-07-25 23:19:24 +0200930 while (cur < total) {
931 fs_path_reset(p);
932
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000933 if (found_key->type == BTRFS_INODE_REF_KEY) {
934 iref = (struct btrfs_inode_ref *)(ptr + cur);
935 name_len = btrfs_inode_ref_name_len(eb, iref);
936 name_off = (unsigned long)(iref + 1);
937 index = btrfs_inode_ref_index(eb, iref);
938 dir = found_key->offset;
939 } else {
940 extref = (struct btrfs_inode_extref *)(ptr + cur);
941 name_len = btrfs_inode_extref_name_len(eb, extref);
942 name_off = (unsigned long)&extref->name;
943 index = btrfs_inode_extref_index(eb, extref);
944 dir = btrfs_inode_extref_parent(eb, extref);
945 }
946
Alexander Block31db9f72012-07-25 23:19:24 +0200947 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000948 start = btrfs_ref_to_path(root, tmp_path, name_len,
949 name_off, eb, dir,
950 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200951 if (IS_ERR(start)) {
952 ret = PTR_ERR(start);
953 goto out;
954 }
955 if (start < p->buf) {
956 /* overflow , try again with larger buffer */
957 ret = fs_path_ensure_buf(p,
958 p->buf_len + p->buf - start);
959 if (ret < 0)
960 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000961 start = btrfs_ref_to_path(root, tmp_path,
962 name_len, name_off,
963 eb, dir,
964 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200965 if (IS_ERR(start)) {
966 ret = PTR_ERR(start);
967 goto out;
968 }
969 BUG_ON(start < p->buf);
970 }
971 p->start = start;
972 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000973 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
974 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200975 if (ret < 0)
976 goto out;
977 }
978
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000979 cur += elem_size + name_len;
980 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200981 if (ret)
982 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200983 num++;
984 }
985
986out:
987 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000988 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200989 return ret;
990}
991
992typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
993 const char *name, int name_len,
994 const char *data, int data_len,
995 u8 type, void *ctx);
996
997/*
998 * Helper function to iterate the entries in ONE btrfs_dir_item.
999 * The iterate callback may return a non zero value to stop iteration. This can
1000 * be a negative value for error codes or 1 to simply stop it.
1001 *
1002 * path must point to the dir item when called.
1003 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001004static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +02001005 iterate_dir_item_t iterate, void *ctx)
1006{
1007 int ret = 0;
1008 struct extent_buffer *eb;
1009 struct btrfs_item *item;
1010 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +02001011 struct btrfs_key di_key;
1012 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +01001013 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +02001014 u32 name_len;
1015 u32 data_len;
1016 u32 cur;
1017 u32 len;
1018 u32 total;
1019 int slot;
1020 int num;
1021 u8 type;
1022
Filipe Manana4395e0c2014-08-20 10:45:45 +01001023 /*
1024 * Start with a small buffer (1 page). If later we end up needing more
1025 * space, which can happen for xattrs on a fs with a leaf size greater
1026 * then the page size, attempt to increase the buffer. Typically xattr
1027 * values are small.
1028 */
1029 buf_len = PATH_MAX;
David Sterbae780b0d2016-01-18 18:42:13 +01001030 buf = kmalloc(buf_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02001031 if (!buf) {
1032 ret = -ENOMEM;
1033 goto out;
1034 }
1035
Alexander Block31db9f72012-07-25 23:19:24 +02001036 eb = path->nodes[0];
1037 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001038 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001039 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1040 cur = 0;
1041 len = 0;
1042 total = btrfs_item_size(eb, item);
1043
1044 num = 0;
1045 while (cur < total) {
1046 name_len = btrfs_dir_name_len(eb, di);
1047 data_len = btrfs_dir_data_len(eb, di);
1048 type = btrfs_dir_type(eb, di);
1049 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1050
Filipe Manana7e3ae332014-05-23 20:15:16 +01001051 if (type == BTRFS_FT_XATTR) {
1052 if (name_len > XATTR_NAME_MAX) {
1053 ret = -ENAMETOOLONG;
1054 goto out;
1055 }
Jeff Mahoneyda170662016-06-15 09:22:56 -04001056 if (name_len + data_len >
1057 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001058 ret = -E2BIG;
1059 goto out;
1060 }
1061 } else {
1062 /*
1063 * Path too long
1064 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001065 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001066 ret = -ENAMETOOLONG;
1067 goto out;
1068 }
Alexander Block31db9f72012-07-25 23:19:24 +02001069 }
1070
Filipe Manana4395e0c2014-08-20 10:45:45 +01001071 if (name_len + data_len > buf_len) {
1072 buf_len = name_len + data_len;
1073 if (is_vmalloc_addr(buf)) {
1074 vfree(buf);
1075 buf = NULL;
1076 } else {
1077 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001078 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001079
1080 if (!tmp)
1081 kfree(buf);
1082 buf = tmp;
1083 }
1084 if (!buf) {
David Sterbaf11f7442017-05-31 18:40:02 +02001085 buf = kvmalloc(buf_len, GFP_KERNEL);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001086 if (!buf) {
1087 ret = -ENOMEM;
1088 goto out;
1089 }
1090 }
1091 }
1092
Alexander Block31db9f72012-07-25 23:19:24 +02001093 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1094 name_len + data_len);
1095
1096 len = sizeof(*di) + name_len + data_len;
1097 di = (struct btrfs_dir_item *)((char *)di + len);
1098 cur += len;
1099
1100 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1101 data_len, type, ctx);
1102 if (ret < 0)
1103 goto out;
1104 if (ret) {
1105 ret = 0;
1106 goto out;
1107 }
1108
1109 num++;
1110 }
1111
1112out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001113 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001114 return ret;
1115}
1116
1117static int __copy_first_ref(int num, u64 dir, int index,
1118 struct fs_path *p, void *ctx)
1119{
1120 int ret;
1121 struct fs_path *pt = ctx;
1122
1123 ret = fs_path_copy(pt, p);
1124 if (ret < 0)
1125 return ret;
1126
1127 /* we want the first only */
1128 return 1;
1129}
1130
1131/*
1132 * Retrieve the first path of an inode. If an inode has more then one
1133 * ref/hardlink, this is ignored.
1134 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001135static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001136 u64 ino, struct fs_path *path)
1137{
1138 int ret;
1139 struct btrfs_key key, found_key;
1140 struct btrfs_path *p;
1141
1142 p = alloc_path_for_send();
1143 if (!p)
1144 return -ENOMEM;
1145
1146 fs_path_reset(path);
1147
1148 key.objectid = ino;
1149 key.type = BTRFS_INODE_REF_KEY;
1150 key.offset = 0;
1151
1152 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1153 if (ret < 0)
1154 goto out;
1155 if (ret) {
1156 ret = 1;
1157 goto out;
1158 }
1159 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1160 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001161 (found_key.type != BTRFS_INODE_REF_KEY &&
1162 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001163 ret = -ENOENT;
1164 goto out;
1165 }
1166
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001167 ret = iterate_inode_ref(root, p, &found_key, 1,
1168 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001169 if (ret < 0)
1170 goto out;
1171 ret = 0;
1172
1173out:
1174 btrfs_free_path(p);
1175 return ret;
1176}
1177
1178struct backref_ctx {
1179 struct send_ctx *sctx;
1180
1181 /* number of total found references */
1182 u64 found;
1183
1184 /*
1185 * used for clones found in send_root. clones found behind cur_objectid
1186 * and cur_offset are not considered as allowed clones.
1187 */
1188 u64 cur_objectid;
1189 u64 cur_offset;
1190
1191 /* may be truncated in case it's the last extent in a file */
1192 u64 extent_len;
1193
1194 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001195 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001196};
1197
1198static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1199{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001200 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001201 struct clone_root *cr = (struct clone_root *)elt;
1202
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001203 if (root < cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001204 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001205 if (root > cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001206 return 1;
1207 return 0;
1208}
1209
1210static int __clone_root_cmp_sort(const void *e1, const void *e2)
1211{
1212 struct clone_root *cr1 = (struct clone_root *)e1;
1213 struct clone_root *cr2 = (struct clone_root *)e2;
1214
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001215 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001216 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001217 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001218 return 1;
1219 return 0;
1220}
1221
1222/*
1223 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001224 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001225 */
1226static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1227{
1228 struct backref_ctx *bctx = ctx_;
1229 struct clone_root *found;
Alexander Block31db9f72012-07-25 23:19:24 +02001230
1231 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001232 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001233 bctx->sctx->clone_roots_cnt,
1234 sizeof(struct clone_root),
1235 __clone_root_cmp_bsearch);
1236 if (!found)
1237 return 0;
1238
1239 if (found->root == bctx->sctx->send_root &&
1240 ino == bctx->cur_objectid &&
1241 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001242 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001243 }
1244
1245 /*
Alexander Block31db9f72012-07-25 23:19:24 +02001246 * Make sure we don't consider clones from send_root that are
1247 * behind the current inode/offset.
1248 */
1249 if (found->root == bctx->sctx->send_root) {
1250 /*
Filipe Manana11f20692019-10-30 12:23:11 +00001251 * If the source inode was not yet processed we can't issue a
1252 * clone operation, as the source extent does not exist yet at
1253 * the destination of the stream.
Alexander Block31db9f72012-07-25 23:19:24 +02001254 */
Filipe Manana11f20692019-10-30 12:23:11 +00001255 if (ino > bctx->cur_objectid)
1256 return 0;
1257 /*
1258 * We clone from the inode currently being sent as long as the
1259 * source extent is already processed, otherwise we could try
1260 * to clone from an extent that does not exist yet at the
1261 * destination of the stream.
1262 */
1263 if (ino == bctx->cur_objectid &&
Filipe Manana9722b102020-01-29 17:09:53 +00001264 offset + bctx->extent_len >
1265 bctx->sctx->cur_inode_next_write_offset)
Alexander Block31db9f72012-07-25 23:19:24 +02001266 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001267 }
1268
1269 bctx->found++;
1270 found->found_refs++;
1271 if (ino < found->ino) {
1272 found->ino = ino;
1273 found->offset = offset;
1274 } else if (found->ino == ino) {
1275 /*
1276 * same extent found more then once in the same file.
1277 */
1278 if (found->offset > offset + bctx->extent_len)
1279 found->offset = offset;
1280 }
1281
1282 return 0;
1283}
1284
1285/*
Alexander Block766702e2012-07-28 14:11:31 +02001286 * Given an inode, offset and extent item, it finds a good clone for a clone
1287 * instruction. Returns -ENOENT when none could be found. The function makes
1288 * sure that the returned clone is usable at the point where sending is at the
1289 * moment. This means, that no clones are accepted which lie behind the current
1290 * inode+offset.
1291 *
Alexander Block31db9f72012-07-25 23:19:24 +02001292 * path must point to the extent item when called.
1293 */
1294static int find_extent_clone(struct send_ctx *sctx,
1295 struct btrfs_path *path,
1296 u64 ino, u64 data_offset,
1297 u64 ino_size,
1298 struct clone_root **found)
1299{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001300 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001301 int ret;
1302 int extent_type;
1303 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001304 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001305 u64 num_bytes;
1306 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001307 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001308 struct btrfs_file_extent_item *fi;
1309 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001310 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001311 struct clone_root *cur_clone_root;
1312 struct btrfs_key found_key;
1313 struct btrfs_path *tmp_path;
Filipe Mananafd0ddbe2019-10-30 12:23:01 +00001314 struct btrfs_extent_item *ei;
Chris Mason74dd17f2012-08-07 16:25:13 -04001315 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001316 u32 i;
1317
1318 tmp_path = alloc_path_for_send();
1319 if (!tmp_path)
1320 return -ENOMEM;
1321
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001322 /* We only use this path under the commit sem */
1323 tmp_path->need_commit_sem = 0;
1324
David Sterbae780b0d2016-01-18 18:42:13 +01001325 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001326 if (!backref_ctx) {
1327 ret = -ENOMEM;
1328 goto out;
1329 }
1330
Alexander Block31db9f72012-07-25 23:19:24 +02001331 if (data_offset >= ino_size) {
1332 /*
1333 * There may be extents that lie behind the file's size.
1334 * I at least had this in combination with snapshotting while
1335 * writing large files.
1336 */
1337 ret = 0;
1338 goto out;
1339 }
1340
1341 fi = btrfs_item_ptr(eb, path->slots[0],
1342 struct btrfs_file_extent_item);
1343 extent_type = btrfs_file_extent_type(eb, fi);
1344 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1345 ret = -ENOENT;
1346 goto out;
1347 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001348 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001349
1350 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001351 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1352 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001353 ret = -ENOENT;
1354 goto out;
1355 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001356 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001357
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001358 down_read(&fs_info->commit_root_sem);
1359 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001360 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001361 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001362
1363 if (ret < 0)
1364 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001365 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001366 ret = -EIO;
1367 goto out;
1368 }
1369
Filipe Mananafd0ddbe2019-10-30 12:23:01 +00001370 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1371 struct btrfs_extent_item);
1372 /*
1373 * Backreference walking (iterate_extent_inodes() below) is currently
1374 * too expensive when an extent has a large number of references, both
1375 * in time spent and used memory. So for now just fallback to write
1376 * operations instead of clone operations when an extent has more than
1377 * a certain amount of references.
1378 */
1379 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1380 ret = -ENOENT;
1381 goto out;
1382 }
1383 btrfs_release_path(tmp_path);
1384
Alexander Block31db9f72012-07-25 23:19:24 +02001385 /*
1386 * Setup the clone roots.
1387 */
1388 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1389 cur_clone_root = sctx->clone_roots + i;
1390 cur_clone_root->ino = (u64)-1;
1391 cur_clone_root->offset = 0;
1392 cur_clone_root->found_refs = 0;
1393 }
1394
Alexander Block35075bb2012-07-28 12:44:34 +02001395 backref_ctx->sctx = sctx;
1396 backref_ctx->found = 0;
1397 backref_ctx->cur_objectid = ino;
1398 backref_ctx->cur_offset = data_offset;
1399 backref_ctx->found_itself = 0;
1400 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001401
1402 /*
1403 * The last extent of a file may be too large due to page alignment.
1404 * We need to adjust extent_len in this case so that the checks in
1405 * __iterate_backrefs work.
1406 */
1407 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001408 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001409
1410 /*
1411 * Now collect all backrefs.
1412 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001413 if (compressed == BTRFS_COMPRESS_NONE)
1414 extent_item_pos = logical - found_key.objectid;
1415 else
1416 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001417 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1418 extent_item_pos, 1, __iterate_backrefs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001419 backref_ctx, false);
Chris Mason74dd17f2012-08-07 16:25:13 -04001420
Alexander Block31db9f72012-07-25 23:19:24 +02001421 if (ret < 0)
1422 goto out;
1423
Alexander Block35075bb2012-07-28 12:44:34 +02001424 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001425 /* found a bug in backref code? */
1426 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001427 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001428 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001429 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001430 goto out;
1431 }
1432
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001433 btrfs_debug(fs_info,
1434 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1435 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001436
Alexander Block35075bb2012-07-28 12:44:34 +02001437 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001438 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001439
1440 cur_clone_root = NULL;
1441 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1442 if (sctx->clone_roots[i].found_refs) {
1443 if (!cur_clone_root)
1444 cur_clone_root = sctx->clone_roots + i;
1445 else if (sctx->clone_roots[i].root == sctx->send_root)
1446 /* prefer clones from send_root over others */
1447 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001448 }
1449
1450 }
1451
1452 if (cur_clone_root) {
1453 *found = cur_clone_root;
1454 ret = 0;
1455 } else {
1456 ret = -ENOENT;
1457 }
1458
1459out:
1460 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001461 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001462 return ret;
1463}
1464
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001465static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001466 u64 ino,
1467 struct fs_path *dest)
1468{
1469 int ret;
1470 struct btrfs_path *path;
1471 struct btrfs_key key;
1472 struct btrfs_file_extent_item *ei;
1473 u8 type;
1474 u8 compression;
1475 unsigned long off;
1476 int len;
1477
1478 path = alloc_path_for_send();
1479 if (!path)
1480 return -ENOMEM;
1481
1482 key.objectid = ino;
1483 key.type = BTRFS_EXTENT_DATA_KEY;
1484 key.offset = 0;
1485 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1486 if (ret < 0)
1487 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001488 if (ret) {
1489 /*
1490 * An empty symlink inode. Can happen in rare error paths when
1491 * creating a symlink (transaction committed before the inode
1492 * eviction handler removed the symlink inode items and a crash
1493 * happened in between or the subvol was snapshoted in between).
1494 * Print an informative message to dmesg/syslog so that the user
1495 * can delete the symlink.
1496 */
1497 btrfs_err(root->fs_info,
1498 "Found empty symlink inode %llu at root %llu",
1499 ino, root->root_key.objectid);
1500 ret = -EIO;
1501 goto out;
1502 }
Alexander Block31db9f72012-07-25 23:19:24 +02001503
1504 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1505 struct btrfs_file_extent_item);
1506 type = btrfs_file_extent_type(path->nodes[0], ei);
1507 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1508 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1509 BUG_ON(compression);
1510
1511 off = btrfs_file_extent_inline_start(ei);
Qu Wenruoe41ca582018-06-06 15:41:49 +08001512 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001513
1514 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001515
1516out:
1517 btrfs_free_path(path);
1518 return ret;
1519}
1520
1521/*
1522 * Helper function to generate a file name that is unique in the root of
1523 * send_root and parent_root. This is used to generate names for orphan inodes.
1524 */
1525static int gen_unique_name(struct send_ctx *sctx,
1526 u64 ino, u64 gen,
1527 struct fs_path *dest)
1528{
1529 int ret = 0;
1530 struct btrfs_path *path;
1531 struct btrfs_dir_item *di;
1532 char tmp[64];
1533 int len;
1534 u64 idx = 0;
1535
1536 path = alloc_path_for_send();
1537 if (!path)
1538 return -ENOMEM;
1539
1540 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001541 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001542 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001543 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001544
1545 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1546 path, BTRFS_FIRST_FREE_OBJECTID,
1547 tmp, strlen(tmp), 0);
1548 btrfs_release_path(path);
1549 if (IS_ERR(di)) {
1550 ret = PTR_ERR(di);
1551 goto out;
1552 }
1553 if (di) {
1554 /* not unique, try again */
1555 idx++;
1556 continue;
1557 }
1558
1559 if (!sctx->parent_root) {
1560 /* unique */
1561 ret = 0;
1562 break;
1563 }
1564
1565 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1566 path, BTRFS_FIRST_FREE_OBJECTID,
1567 tmp, strlen(tmp), 0);
1568 btrfs_release_path(path);
1569 if (IS_ERR(di)) {
1570 ret = PTR_ERR(di);
1571 goto out;
1572 }
1573 if (di) {
1574 /* not unique, try again */
1575 idx++;
1576 continue;
1577 }
1578 /* unique */
1579 break;
1580 }
1581
1582 ret = fs_path_add(dest, tmp, strlen(tmp));
1583
1584out:
1585 btrfs_free_path(path);
1586 return ret;
1587}
1588
1589enum inode_state {
1590 inode_state_no_change,
1591 inode_state_will_create,
1592 inode_state_did_create,
1593 inode_state_will_delete,
1594 inode_state_did_delete,
1595};
1596
1597static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1598{
1599 int ret;
1600 int left_ret;
1601 int right_ret;
1602 u64 left_gen;
1603 u64 right_gen;
1604
1605 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001606 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001607 if (ret < 0 && ret != -ENOENT)
1608 goto out;
1609 left_ret = ret;
1610
1611 if (!sctx->parent_root) {
1612 right_ret = -ENOENT;
1613 } else {
1614 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001615 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001616 if (ret < 0 && ret != -ENOENT)
1617 goto out;
1618 right_ret = ret;
1619 }
1620
1621 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001622 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001623 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001624 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001625 if (ino < sctx->send_progress)
1626 ret = inode_state_did_create;
1627 else
1628 ret = inode_state_will_create;
1629 } else if (right_gen == gen) {
1630 if (ino < sctx->send_progress)
1631 ret = inode_state_did_delete;
1632 else
1633 ret = inode_state_will_delete;
1634 } else {
1635 ret = -ENOENT;
1636 }
1637 } else if (!left_ret) {
1638 if (left_gen == gen) {
1639 if (ino < sctx->send_progress)
1640 ret = inode_state_did_create;
1641 else
1642 ret = inode_state_will_create;
1643 } else {
1644 ret = -ENOENT;
1645 }
1646 } else if (!right_ret) {
1647 if (right_gen == gen) {
1648 if (ino < sctx->send_progress)
1649 ret = inode_state_did_delete;
1650 else
1651 ret = inode_state_will_delete;
1652 } else {
1653 ret = -ENOENT;
1654 }
1655 } else {
1656 ret = -ENOENT;
1657 }
1658
1659out:
1660 return ret;
1661}
1662
1663static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1664{
1665 int ret;
1666
Robbie Ko4dd99202017-01-05 16:24:55 +08001667 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1668 return 1;
1669
Alexander Block31db9f72012-07-25 23:19:24 +02001670 ret = get_cur_inode_state(sctx, ino, gen);
1671 if (ret < 0)
1672 goto out;
1673
1674 if (ret == inode_state_no_change ||
1675 ret == inode_state_did_create ||
1676 ret == inode_state_will_delete)
1677 ret = 1;
1678 else
1679 ret = 0;
1680
1681out:
1682 return ret;
1683}
1684
1685/*
1686 * Helper function to lookup a dir item in a dir.
1687 */
1688static int lookup_dir_item_inode(struct btrfs_root *root,
1689 u64 dir, const char *name, int name_len,
1690 u64 *found_inode,
1691 u8 *found_type)
1692{
1693 int ret = 0;
1694 struct btrfs_dir_item *di;
1695 struct btrfs_key key;
1696 struct btrfs_path *path;
1697
1698 path = alloc_path_for_send();
1699 if (!path)
1700 return -ENOMEM;
1701
1702 di = btrfs_lookup_dir_item(NULL, root, path,
1703 dir, name, name_len, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08001704 if (IS_ERR_OR_NULL(di)) {
1705 ret = di ? PTR_ERR(di) : -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02001706 goto out;
1707 }
1708 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001709 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1710 ret = -ENOENT;
1711 goto out;
1712 }
Alexander Block31db9f72012-07-25 23:19:24 +02001713 *found_inode = key.objectid;
1714 *found_type = btrfs_dir_type(path->nodes[0], di);
1715
1716out:
1717 btrfs_free_path(path);
1718 return ret;
1719}
1720
Alexander Block766702e2012-07-28 14:11:31 +02001721/*
1722 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1723 * generation of the parent dir and the name of the dir entry.
1724 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001725static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001726 u64 *dir, u64 *dir_gen, struct fs_path *name)
1727{
1728 int ret;
1729 struct btrfs_key key;
1730 struct btrfs_key found_key;
1731 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001732 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001733 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001734
1735 path = alloc_path_for_send();
1736 if (!path)
1737 return -ENOMEM;
1738
1739 key.objectid = ino;
1740 key.type = BTRFS_INODE_REF_KEY;
1741 key.offset = 0;
1742
1743 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1744 if (ret < 0)
1745 goto out;
1746 if (!ret)
1747 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1748 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001749 if (ret || found_key.objectid != ino ||
1750 (found_key.type != BTRFS_INODE_REF_KEY &&
1751 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001752 ret = -ENOENT;
1753 goto out;
1754 }
1755
Filipe Manana51a60252014-05-13 22:01:02 +01001756 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001757 struct btrfs_inode_ref *iref;
1758 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1759 struct btrfs_inode_ref);
1760 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1761 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1762 (unsigned long)(iref + 1),
1763 len);
1764 parent_dir = found_key.offset;
1765 } else {
1766 struct btrfs_inode_extref *extref;
1767 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1768 struct btrfs_inode_extref);
1769 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1770 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1771 (unsigned long)&extref->name, len);
1772 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1773 }
Alexander Block31db9f72012-07-25 23:19:24 +02001774 if (ret < 0)
1775 goto out;
1776 btrfs_release_path(path);
1777
Filipe Mananab46ab972014-03-21 12:46:54 +00001778 if (dir_gen) {
1779 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1780 NULL, NULL, NULL);
1781 if (ret < 0)
1782 goto out;
1783 }
Alexander Block31db9f72012-07-25 23:19:24 +02001784
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001785 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001786
1787out:
1788 btrfs_free_path(path);
1789 return ret;
1790}
1791
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001792static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001793 u64 ino, u64 dir,
1794 const char *name, int name_len)
1795{
1796 int ret;
1797 struct fs_path *tmp_name;
1798 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001799
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001800 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001801 if (!tmp_name)
1802 return -ENOMEM;
1803
Filipe Mananab46ab972014-03-21 12:46:54 +00001804 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001805 if (ret < 0)
1806 goto out;
1807
Alexander Blockb9291af2012-07-28 11:07:18 +02001808 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001809 ret = 0;
1810 goto out;
1811 }
1812
Alexander Blocke938c8a2012-07-28 16:33:49 +02001813 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001814
1815out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001816 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001817 return ret;
1818}
1819
Alexander Block766702e2012-07-28 14:11:31 +02001820/*
1821 * Used by process_recorded_refs to determine if a new ref would overwrite an
1822 * already existing ref. In case it detects an overwrite, it returns the
1823 * inode/gen in who_ino/who_gen.
1824 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1825 * to make sure later references to the overwritten inode are possible.
1826 * Orphanizing is however only required for the first ref of an inode.
1827 * process_recorded_refs does an additional is_first_ref check to see if
1828 * orphanizing is really required.
1829 */
Alexander Block31db9f72012-07-25 23:19:24 +02001830static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1831 const char *name, int name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01001832 u64 *who_ino, u64 *who_gen, u64 *who_mode)
Alexander Block31db9f72012-07-25 23:19:24 +02001833{
1834 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001835 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001836 u64 other_inode = 0;
1837 u8 other_type = 0;
1838
1839 if (!sctx->parent_root)
1840 goto out;
1841
1842 ret = is_inode_existent(sctx, dir, dir_gen);
1843 if (ret <= 0)
1844 goto out;
1845
Josef Bacikebdad912013-08-06 16:47:48 -04001846 /*
1847 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001848 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001849 * and we can just unlink this entry.
1850 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001851 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001852 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1853 NULL, NULL, NULL);
1854 if (ret < 0 && ret != -ENOENT)
1855 goto out;
1856 if (ret) {
1857 ret = 0;
1858 goto out;
1859 }
1860 if (gen != dir_gen)
1861 goto out;
1862 }
1863
Alexander Block31db9f72012-07-25 23:19:24 +02001864 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1865 &other_inode, &other_type);
1866 if (ret < 0 && ret != -ENOENT)
1867 goto out;
1868 if (ret) {
1869 ret = 0;
1870 goto out;
1871 }
1872
Alexander Block766702e2012-07-28 14:11:31 +02001873 /*
1874 * Check if the overwritten ref was already processed. If yes, the ref
1875 * was already unlinked/moved, so we can safely assume that we will not
1876 * overwrite anything at this point in time.
1877 */
Robbie Ko801bec32015-06-23 18:39:46 +08001878 if (other_inode > sctx->send_progress ||
1879 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001880 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Filipe Mananaf5962782017-06-22 20:03:51 +01001881 who_gen, who_mode, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001882 if (ret < 0)
1883 goto out;
1884
1885 ret = 1;
1886 *who_ino = other_inode;
1887 } else {
1888 ret = 0;
1889 }
1890
1891out:
1892 return ret;
1893}
1894
Alexander Block766702e2012-07-28 14:11:31 +02001895/*
1896 * Checks if the ref was overwritten by an already processed inode. This is
1897 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1898 * thus the orphan name needs be used.
1899 * process_recorded_refs also uses it to avoid unlinking of refs that were
1900 * overwritten.
1901 */
Alexander Block31db9f72012-07-25 23:19:24 +02001902static int did_overwrite_ref(struct send_ctx *sctx,
1903 u64 dir, u64 dir_gen,
1904 u64 ino, u64 ino_gen,
1905 const char *name, int name_len)
1906{
1907 int ret = 0;
1908 u64 gen;
1909 u64 ow_inode;
1910 u8 other_type;
1911
1912 if (!sctx->parent_root)
1913 goto out;
1914
1915 ret = is_inode_existent(sctx, dir, dir_gen);
1916 if (ret <= 0)
1917 goto out;
1918
Robbie Ko01914102017-01-05 16:24:58 +08001919 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1920 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1921 NULL, NULL, NULL);
1922 if (ret < 0 && ret != -ENOENT)
1923 goto out;
1924 if (ret) {
1925 ret = 0;
1926 goto out;
1927 }
1928 if (gen != dir_gen)
1929 goto out;
1930 }
1931
Alexander Block31db9f72012-07-25 23:19:24 +02001932 /* check if the ref was overwritten by another ref */
1933 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1934 &ow_inode, &other_type);
1935 if (ret < 0 && ret != -ENOENT)
1936 goto out;
1937 if (ret) {
1938 /* was never and will never be overwritten */
1939 ret = 0;
1940 goto out;
1941 }
1942
1943 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001944 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001945 if (ret < 0)
1946 goto out;
1947
1948 if (ow_inode == ino && gen == ino_gen) {
1949 ret = 0;
1950 goto out;
1951 }
1952
Filipe Manana8b191a62015-04-09 14:09:14 +01001953 /*
1954 * We know that it is or will be overwritten. Check this now.
1955 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001956 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1957 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001958 */
Filipe Mananab786f162015-09-26 15:30:23 +01001959 if ((ow_inode < sctx->send_progress) ||
1960 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1961 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001962 ret = 1;
1963 else
1964 ret = 0;
1965
1966out:
1967 return ret;
1968}
1969
Alexander Block766702e2012-07-28 14:11:31 +02001970/*
1971 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1972 * that got overwritten. This is used by process_recorded_refs to determine
1973 * if it has to use the path as returned by get_cur_path or the orphan name.
1974 */
Alexander Block31db9f72012-07-25 23:19:24 +02001975static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1976{
1977 int ret = 0;
1978 struct fs_path *name = NULL;
1979 u64 dir;
1980 u64 dir_gen;
1981
1982 if (!sctx->parent_root)
1983 goto out;
1984
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001985 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001986 if (!name)
1987 return -ENOMEM;
1988
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001989 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001990 if (ret < 0)
1991 goto out;
1992
1993 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1994 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001995
1996out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001997 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001998 return ret;
1999}
2000
Alexander Block766702e2012-07-28 14:11:31 +02002001/*
2002 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2003 * so we need to do some special handling in case we have clashes. This function
2004 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002005 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002006 */
Alexander Block31db9f72012-07-25 23:19:24 +02002007static int name_cache_insert(struct send_ctx *sctx,
2008 struct name_cache_entry *nce)
2009{
2010 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002011 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002012
Alexander Block7e0926f2012-07-28 14:20:58 +02002013 nce_head = radix_tree_lookup(&sctx->name_cache,
2014 (unsigned long)nce->ino);
2015 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002016 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002017 if (!nce_head) {
2018 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002019 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002020 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002021 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002022
Alexander Block7e0926f2012-07-28 14:20:58 +02002023 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002024 if (ret < 0) {
2025 kfree(nce_head);
2026 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002027 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002028 }
Alexander Block31db9f72012-07-25 23:19:24 +02002029 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002030 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002031 list_add_tail(&nce->list, &sctx->name_cache_list);
2032 sctx->name_cache_size++;
2033
2034 return ret;
2035}
2036
2037static void name_cache_delete(struct send_ctx *sctx,
2038 struct name_cache_entry *nce)
2039{
Alexander Block7e0926f2012-07-28 14:20:58 +02002040 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002041
Alexander Block7e0926f2012-07-28 14:20:58 +02002042 nce_head = radix_tree_lookup(&sctx->name_cache,
2043 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002044 if (!nce_head) {
2045 btrfs_err(sctx->send_root->fs_info,
2046 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2047 nce->ino, sctx->name_cache_size);
2048 }
Alexander Block31db9f72012-07-25 23:19:24 +02002049
Alexander Block7e0926f2012-07-28 14:20:58 +02002050 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002051 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002052 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002053
David Sterba57fb8912014-02-03 19:24:40 +01002054 /*
2055 * We may not get to the final release of nce_head if the lookup fails
2056 */
2057 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002058 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2059 kfree(nce_head);
2060 }
Alexander Block31db9f72012-07-25 23:19:24 +02002061}
2062
2063static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2064 u64 ino, u64 gen)
2065{
Alexander Block7e0926f2012-07-28 14:20:58 +02002066 struct list_head *nce_head;
2067 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002068
Alexander Block7e0926f2012-07-28 14:20:58 +02002069 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2070 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002071 return NULL;
2072
Alexander Block7e0926f2012-07-28 14:20:58 +02002073 list_for_each_entry(cur, nce_head, radix_list) {
2074 if (cur->ino == ino && cur->gen == gen)
2075 return cur;
2076 }
Alexander Block31db9f72012-07-25 23:19:24 +02002077 return NULL;
2078}
2079
Alexander Block766702e2012-07-28 14:11:31 +02002080/*
2081 * Removes the entry from the list and adds it back to the end. This marks the
2082 * entry as recently used so that name_cache_clean_unused does not remove it.
2083 */
Alexander Block31db9f72012-07-25 23:19:24 +02002084static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2085{
2086 list_del(&nce->list);
2087 list_add_tail(&nce->list, &sctx->name_cache_list);
2088}
2089
Alexander Block766702e2012-07-28 14:11:31 +02002090/*
2091 * Remove some entries from the beginning of name_cache_list.
2092 */
Alexander Block31db9f72012-07-25 23:19:24 +02002093static void name_cache_clean_unused(struct send_ctx *sctx)
2094{
2095 struct name_cache_entry *nce;
2096
2097 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2098 return;
2099
2100 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2101 nce = list_entry(sctx->name_cache_list.next,
2102 struct name_cache_entry, list);
2103 name_cache_delete(sctx, nce);
2104 kfree(nce);
2105 }
2106}
2107
2108static void name_cache_free(struct send_ctx *sctx)
2109{
2110 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002111
Alexander Blocke938c8a2012-07-28 16:33:49 +02002112 while (!list_empty(&sctx->name_cache_list)) {
2113 nce = list_entry(sctx->name_cache_list.next,
2114 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002115 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002116 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002117 }
2118}
2119
Alexander Block766702e2012-07-28 14:11:31 +02002120/*
2121 * Used by get_cur_path for each ref up to the root.
2122 * Returns 0 if it succeeded.
2123 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2124 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2125 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2126 * Returns <0 in case of error.
2127 */
Alexander Block31db9f72012-07-25 23:19:24 +02002128static int __get_cur_name_and_parent(struct send_ctx *sctx,
2129 u64 ino, u64 gen,
2130 u64 *parent_ino,
2131 u64 *parent_gen,
2132 struct fs_path *dest)
2133{
2134 int ret;
2135 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002136 struct name_cache_entry *nce = NULL;
2137
Alexander Block766702e2012-07-28 14:11:31 +02002138 /*
2139 * First check if we already did a call to this function with the same
2140 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2141 * return the cached result.
2142 */
Alexander Block31db9f72012-07-25 23:19:24 +02002143 nce = name_cache_search(sctx, ino, gen);
2144 if (nce) {
2145 if (ino < sctx->send_progress && nce->need_later_update) {
2146 name_cache_delete(sctx, nce);
2147 kfree(nce);
2148 nce = NULL;
2149 } else {
2150 name_cache_used(sctx, nce);
2151 *parent_ino = nce->parent_ino;
2152 *parent_gen = nce->parent_gen;
2153 ret = fs_path_add(dest, nce->name, nce->name_len);
2154 if (ret < 0)
2155 goto out;
2156 ret = nce->ret;
2157 goto out;
2158 }
2159 }
2160
Alexander Block766702e2012-07-28 14:11:31 +02002161 /*
2162 * If the inode is not existent yet, add the orphan name and return 1.
2163 * This should only happen for the parent dir that we determine in
2164 * __record_new_ref
2165 */
Alexander Block31db9f72012-07-25 23:19:24 +02002166 ret = is_inode_existent(sctx, ino, gen);
2167 if (ret < 0)
2168 goto out;
2169
2170 if (!ret) {
2171 ret = gen_unique_name(sctx, ino, gen, dest);
2172 if (ret < 0)
2173 goto out;
2174 ret = 1;
2175 goto out_cache;
2176 }
2177
Alexander Block766702e2012-07-28 14:11:31 +02002178 /*
2179 * Depending on whether the inode was already processed or not, use
2180 * send_root or parent_root for ref lookup.
2181 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002182 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002183 ret = get_first_ref(sctx->send_root, ino,
2184 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002185 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002186 ret = get_first_ref(sctx->parent_root, ino,
2187 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002188 if (ret < 0)
2189 goto out;
2190
Alexander Block766702e2012-07-28 14:11:31 +02002191 /*
2192 * Check if the ref was overwritten by an inode's ref that was processed
2193 * earlier. If yes, treat as orphan and return 1.
2194 */
Alexander Block31db9f72012-07-25 23:19:24 +02002195 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2196 dest->start, dest->end - dest->start);
2197 if (ret < 0)
2198 goto out;
2199 if (ret) {
2200 fs_path_reset(dest);
2201 ret = gen_unique_name(sctx, ino, gen, dest);
2202 if (ret < 0)
2203 goto out;
2204 ret = 1;
2205 }
2206
2207out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002208 /*
2209 * Store the result of the lookup in the name cache.
2210 */
David Sterbae780b0d2016-01-18 18:42:13 +01002211 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002212 if (!nce) {
2213 ret = -ENOMEM;
2214 goto out;
2215 }
2216
2217 nce->ino = ino;
2218 nce->gen = gen;
2219 nce->parent_ino = *parent_ino;
2220 nce->parent_gen = *parent_gen;
2221 nce->name_len = fs_path_len(dest);
2222 nce->ret = ret;
2223 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002224
2225 if (ino < sctx->send_progress)
2226 nce->need_later_update = 0;
2227 else
2228 nce->need_later_update = 1;
2229
2230 nce_ret = name_cache_insert(sctx, nce);
2231 if (nce_ret < 0)
2232 ret = nce_ret;
2233 name_cache_clean_unused(sctx);
2234
2235out:
Alexander Block31db9f72012-07-25 23:19:24 +02002236 return ret;
2237}
2238
2239/*
2240 * Magic happens here. This function returns the first ref to an inode as it
2241 * would look like while receiving the stream at this point in time.
2242 * We walk the path up to the root. For every inode in between, we check if it
2243 * was already processed/sent. If yes, we continue with the parent as found
2244 * in send_root. If not, we continue with the parent as found in parent_root.
2245 * If we encounter an inode that was deleted at this point in time, we use the
2246 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2247 * that were not created yet and overwritten inodes/refs.
2248 *
Andrea Gelmini52042d82018-11-28 12:05:13 +01002249 * When do we have orphan inodes:
Alexander Block31db9f72012-07-25 23:19:24 +02002250 * 1. When an inode is freshly created and thus no valid refs are available yet
2251 * 2. When a directory lost all it's refs (deleted) but still has dir items
2252 * inside which were not processed yet (pending for move/delete). If anyone
2253 * tried to get the path to the dir items, it would get a path inside that
2254 * orphan directory.
2255 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2256 * of an unprocessed inode. If in that case the first ref would be
2257 * overwritten, the overwritten inode gets "orphanized". Later when we
2258 * process this overwritten inode, it is restored at a new place by moving
2259 * the orphan inode.
2260 *
2261 * sctx->send_progress tells this function at which point in time receiving
2262 * would be.
2263 */
2264static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2265 struct fs_path *dest)
2266{
2267 int ret = 0;
2268 struct fs_path *name = NULL;
2269 u64 parent_inode = 0;
2270 u64 parent_gen = 0;
2271 int stop = 0;
2272
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002273 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002274 if (!name) {
2275 ret = -ENOMEM;
2276 goto out;
2277 }
2278
2279 dest->reversed = 1;
2280 fs_path_reset(dest);
2281
2282 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002283 struct waiting_dir_move *wdm;
2284
Alexander Block31db9f72012-07-25 23:19:24 +02002285 fs_path_reset(name);
2286
Filipe Manana0b3f4072020-12-10 12:09:02 +00002287 if (is_waiting_for_rm(sctx, ino, gen)) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002288 ret = gen_unique_name(sctx, ino, gen, name);
2289 if (ret < 0)
2290 goto out;
2291 ret = fs_path_add_path(dest, name);
2292 break;
2293 }
2294
Filipe Manana8b191a62015-04-09 14:09:14 +01002295 wdm = get_waiting_dir_move(sctx, ino);
2296 if (wdm && wdm->orphanized) {
2297 ret = gen_unique_name(sctx, ino, gen, name);
2298 stop = 1;
2299 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002300 ret = get_first_ref(sctx->parent_root, ino,
2301 &parent_inode, &parent_gen, name);
2302 } else {
2303 ret = __get_cur_name_and_parent(sctx, ino, gen,
2304 &parent_inode,
2305 &parent_gen, name);
2306 if (ret)
2307 stop = 1;
2308 }
2309
Alexander Block31db9f72012-07-25 23:19:24 +02002310 if (ret < 0)
2311 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002312
Alexander Block31db9f72012-07-25 23:19:24 +02002313 ret = fs_path_add_path(dest, name);
2314 if (ret < 0)
2315 goto out;
2316
2317 ino = parent_inode;
2318 gen = parent_gen;
2319 }
2320
2321out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002322 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002323 if (!ret)
2324 fs_path_unreverse(dest);
2325 return ret;
2326}
2327
2328/*
Alexander Block31db9f72012-07-25 23:19:24 +02002329 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2330 */
2331static int send_subvol_begin(struct send_ctx *sctx)
2332{
2333 int ret;
2334 struct btrfs_root *send_root = sctx->send_root;
2335 struct btrfs_root *parent_root = sctx->parent_root;
2336 struct btrfs_path *path;
2337 struct btrfs_key key;
2338 struct btrfs_root_ref *ref;
2339 struct extent_buffer *leaf;
2340 char *name = NULL;
2341 int namelen;
2342
Wang Shilongffcfaf82014-01-15 00:26:43 +08002343 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002344 if (!path)
2345 return -ENOMEM;
2346
David Sterbae780b0d2016-01-18 18:42:13 +01002347 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002348 if (!name) {
2349 btrfs_free_path(path);
2350 return -ENOMEM;
2351 }
2352
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002353 key.objectid = send_root->root_key.objectid;
Alexander Block31db9f72012-07-25 23:19:24 +02002354 key.type = BTRFS_ROOT_BACKREF_KEY;
2355 key.offset = 0;
2356
2357 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2358 &key, path, 1, 0);
2359 if (ret < 0)
2360 goto out;
2361 if (ret) {
2362 ret = -ENOENT;
2363 goto out;
2364 }
2365
2366 leaf = path->nodes[0];
2367 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2368 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002369 key.objectid != send_root->root_key.objectid) {
Alexander Block31db9f72012-07-25 23:19:24 +02002370 ret = -ENOENT;
2371 goto out;
2372 }
2373 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2374 namelen = btrfs_root_ref_name_len(leaf, ref);
2375 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2376 btrfs_release_path(path);
2377
Alexander Block31db9f72012-07-25 23:19:24 +02002378 if (parent_root) {
2379 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2380 if (ret < 0)
2381 goto out;
2382 } else {
2383 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2384 if (ret < 0)
2385 goto out;
2386 }
2387
2388 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002389
2390 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2391 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2392 sctx->send_root->root_item.received_uuid);
2393 else
2394 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2395 sctx->send_root->root_item.uuid);
2396
Alexander Block31db9f72012-07-25 23:19:24 +02002397 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
David Sterba09e3a282020-09-15 14:30:15 +02002398 btrfs_root_ctransid(&sctx->send_root->root_item));
Alexander Block31db9f72012-07-25 23:19:24 +02002399 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002400 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2401 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2402 parent_root->root_item.received_uuid);
2403 else
2404 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2405 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002406 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
David Sterba09e3a282020-09-15 14:30:15 +02002407 btrfs_root_ctransid(&sctx->parent_root->root_item));
Alexander Block31db9f72012-07-25 23:19:24 +02002408 }
2409
2410 ret = send_cmd(sctx);
2411
2412tlv_put_failure:
2413out:
2414 btrfs_free_path(path);
2415 kfree(name);
2416 return ret;
2417}
2418
2419static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2420{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002421 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002422 int ret = 0;
2423 struct fs_path *p;
2424
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002425 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002426
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002427 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002428 if (!p)
2429 return -ENOMEM;
2430
2431 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2432 if (ret < 0)
2433 goto out;
2434
2435 ret = get_cur_path(sctx, ino, gen, p);
2436 if (ret < 0)
2437 goto out;
2438 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2439 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2440
2441 ret = send_cmd(sctx);
2442
2443tlv_put_failure:
2444out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002445 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002446 return ret;
2447}
2448
2449static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2450{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002451 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002452 int ret = 0;
2453 struct fs_path *p;
2454
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002455 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002456
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002457 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002458 if (!p)
2459 return -ENOMEM;
2460
2461 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2462 if (ret < 0)
2463 goto out;
2464
2465 ret = get_cur_path(sctx, ino, gen, p);
2466 if (ret < 0)
2467 goto out;
2468 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2469 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2470
2471 ret = send_cmd(sctx);
2472
2473tlv_put_failure:
2474out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002475 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002476 return ret;
2477}
2478
2479static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2480{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002481 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002482 int ret = 0;
2483 struct fs_path *p;
2484
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002485 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2486 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002487
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002488 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002489 if (!p)
2490 return -ENOMEM;
2491
2492 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2493 if (ret < 0)
2494 goto out;
2495
2496 ret = get_cur_path(sctx, ino, gen, p);
2497 if (ret < 0)
2498 goto out;
2499 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2500 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2501 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2502
2503 ret = send_cmd(sctx);
2504
2505tlv_put_failure:
2506out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002507 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002508 return ret;
2509}
2510
2511static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2512{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002513 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002514 int ret = 0;
2515 struct fs_path *p = NULL;
2516 struct btrfs_inode_item *ii;
2517 struct btrfs_path *path = NULL;
2518 struct extent_buffer *eb;
2519 struct btrfs_key key;
2520 int slot;
2521
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002522 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002523
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002524 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002525 if (!p)
2526 return -ENOMEM;
2527
2528 path = alloc_path_for_send();
2529 if (!path) {
2530 ret = -ENOMEM;
2531 goto out;
2532 }
2533
2534 key.objectid = ino;
2535 key.type = BTRFS_INODE_ITEM_KEY;
2536 key.offset = 0;
2537 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002538 if (ret > 0)
2539 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002540 if (ret < 0)
2541 goto out;
2542
2543 eb = path->nodes[0];
2544 slot = path->slots[0];
2545 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2546
2547 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2548 if (ret < 0)
2549 goto out;
2550
2551 ret = get_cur_path(sctx, ino, gen, p);
2552 if (ret < 0)
2553 goto out;
2554 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002555 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2556 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2557 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002558 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002559
2560 ret = send_cmd(sctx);
2561
2562tlv_put_failure:
2563out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002564 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002565 btrfs_free_path(path);
2566 return ret;
2567}
2568
2569/*
2570 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2571 * a valid path yet because we did not process the refs yet. So, the inode
2572 * is created as orphan.
2573 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002574static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002575{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002576 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002577 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002578 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002579 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002580 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002581 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002582 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002583
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002584 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002585
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002586 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002587 if (!p)
2588 return -ENOMEM;
2589
Liu Bo644d1942014-02-27 17:29:01 +08002590 if (ino != sctx->cur_ino) {
2591 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2592 NULL, NULL, &rdev);
2593 if (ret < 0)
2594 goto out;
2595 } else {
2596 gen = sctx->cur_inode_gen;
2597 mode = sctx->cur_inode_mode;
2598 rdev = sctx->cur_inode_rdev;
2599 }
Alexander Block31db9f72012-07-25 23:19:24 +02002600
Alexander Blocke938c8a2012-07-28 16:33:49 +02002601 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002602 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002603 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002604 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002605 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002606 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002607 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002608 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002609 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002610 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002611 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002612 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002613 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002614 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002615 (int)(mode & S_IFMT));
Tsutomu Itohca6842b2016-01-22 09:13:25 +09002616 ret = -EOPNOTSUPP;
Alexander Block31db9f72012-07-25 23:19:24 +02002617 goto out;
2618 }
2619
2620 ret = begin_cmd(sctx, cmd);
2621 if (ret < 0)
2622 goto out;
2623
Alexander Block1f4692d2012-07-28 10:42:24 +02002624 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002625 if (ret < 0)
2626 goto out;
2627
2628 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002629 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002630
2631 if (S_ISLNK(mode)) {
2632 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002633 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002634 if (ret < 0)
2635 goto out;
2636 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2637 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2638 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002639 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2640 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002641 }
2642
2643 ret = send_cmd(sctx);
2644 if (ret < 0)
2645 goto out;
2646
2647
2648tlv_put_failure:
2649out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002650 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002651 return ret;
2652}
2653
Alexander Block1f4692d2012-07-28 10:42:24 +02002654/*
2655 * We need some special handling for inodes that get processed before the parent
2656 * directory got created. See process_recorded_refs for details.
2657 * This function does the check if we already created the dir out of order.
2658 */
2659static int did_create_dir(struct send_ctx *sctx, u64 dir)
2660{
2661 int ret = 0;
2662 struct btrfs_path *path = NULL;
2663 struct btrfs_key key;
2664 struct btrfs_key found_key;
2665 struct btrfs_key di_key;
2666 struct extent_buffer *eb;
2667 struct btrfs_dir_item *di;
2668 int slot;
2669
2670 path = alloc_path_for_send();
2671 if (!path) {
2672 ret = -ENOMEM;
2673 goto out;
2674 }
2675
2676 key.objectid = dir;
2677 key.type = BTRFS_DIR_INDEX_KEY;
2678 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002679 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2680 if (ret < 0)
2681 goto out;
2682
Alexander Block1f4692d2012-07-28 10:42:24 +02002683 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002684 eb = path->nodes[0];
2685 slot = path->slots[0];
2686 if (slot >= btrfs_header_nritems(eb)) {
2687 ret = btrfs_next_leaf(sctx->send_root, path);
2688 if (ret < 0) {
2689 goto out;
2690 } else if (ret > 0) {
2691 ret = 0;
2692 break;
2693 }
2694 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002695 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002696
2697 btrfs_item_key_to_cpu(eb, &found_key, slot);
2698 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002699 found_key.type != key.type) {
2700 ret = 0;
2701 goto out;
2702 }
2703
2704 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2705 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2706
Josef Bacika0525412013-08-12 10:56:14 -04002707 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2708 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002709 ret = 1;
2710 goto out;
2711 }
2712
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002713 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002714 }
2715
2716out:
2717 btrfs_free_path(path);
2718 return ret;
2719}
2720
2721/*
2722 * Only creates the inode if it is:
2723 * 1. Not a directory
2724 * 2. Or a directory which was not created already due to out of order
2725 * directories. See did_create_dir and process_recorded_refs for details.
2726 */
2727static int send_create_inode_if_needed(struct send_ctx *sctx)
2728{
2729 int ret;
2730
2731 if (S_ISDIR(sctx->cur_inode_mode)) {
2732 ret = did_create_dir(sctx, sctx->cur_ino);
2733 if (ret < 0)
2734 goto out;
2735 if (ret) {
2736 ret = 0;
2737 goto out;
2738 }
2739 }
2740
2741 ret = send_create_inode(sctx, sctx->cur_ino);
2742 if (ret < 0)
2743 goto out;
2744
2745out:
2746 return ret;
2747}
2748
Alexander Block31db9f72012-07-25 23:19:24 +02002749struct recorded_ref {
2750 struct list_head list;
Alexander Block31db9f72012-07-25 23:19:24 +02002751 char *name;
2752 struct fs_path *full_path;
2753 u64 dir;
2754 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002755 int name_len;
2756};
2757
Filipe Mananafdb13882017-06-13 14:13:11 +01002758static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2759{
2760 ref->full_path = path;
2761 ref->name = (char *)kbasename(ref->full_path->start);
2762 ref->name_len = ref->full_path->end - ref->name;
2763}
2764
Alexander Block31db9f72012-07-25 23:19:24 +02002765/*
2766 * We need to process new refs before deleted refs, but compare_tree gives us
2767 * everything mixed. So we first record all refs and later process them.
2768 * This function is a helper to record one ref.
2769 */
Liu Boa4d96d62014-03-03 21:31:03 +08002770static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002771 u64 dir_gen, struct fs_path *path)
2772{
2773 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002774
David Sterbae780b0d2016-01-18 18:42:13 +01002775 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002776 if (!ref)
2777 return -ENOMEM;
2778
2779 ref->dir = dir;
2780 ref->dir_gen = dir_gen;
Filipe Mananafdb13882017-06-13 14:13:11 +01002781 set_ref_path(ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02002782 list_add_tail(&ref->list, head);
2783 return 0;
2784}
2785
Josef Bacikba5e8f22013-08-16 16:52:55 -04002786static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2787{
2788 struct recorded_ref *new;
2789
David Sterbae780b0d2016-01-18 18:42:13 +01002790 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002791 if (!new)
2792 return -ENOMEM;
2793
2794 new->dir = ref->dir;
2795 new->dir_gen = ref->dir_gen;
2796 new->full_path = NULL;
2797 INIT_LIST_HEAD(&new->list);
2798 list_add_tail(&new->list, list);
2799 return 0;
2800}
2801
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002802static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002803{
2804 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002805
Alexander Blocke938c8a2012-07-28 16:33:49 +02002806 while (!list_empty(head)) {
2807 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002808 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002809 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002810 kfree(cur);
2811 }
Alexander Block31db9f72012-07-25 23:19:24 +02002812}
2813
2814static void free_recorded_refs(struct send_ctx *sctx)
2815{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002816 __free_recorded_refs(&sctx->new_refs);
2817 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002818}
2819
2820/*
Alexander Block766702e2012-07-28 14:11:31 +02002821 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002822 * ref of an unprocessed inode gets overwritten and for all non empty
2823 * directories.
2824 */
2825static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2826 struct fs_path *path)
2827{
2828 int ret;
2829 struct fs_path *orphan;
2830
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002831 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002832 if (!orphan)
2833 return -ENOMEM;
2834
2835 ret = gen_unique_name(sctx, ino, gen, orphan);
2836 if (ret < 0)
2837 goto out;
2838
2839 ret = send_rename(sctx, path, orphan);
2840
2841out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002842 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002843 return ret;
2844}
2845
Filipe Manana0b3f4072020-12-10 12:09:02 +00002846static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
2847 u64 dir_ino, u64 dir_gen)
Filipe Manana9dc44212014-02-19 14:31:44 +00002848{
2849 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2850 struct rb_node *parent = NULL;
2851 struct orphan_dir_info *entry, *odi;
2852
Filipe Manana9dc44212014-02-19 14:31:44 +00002853 while (*p) {
2854 parent = *p;
2855 entry = rb_entry(parent, struct orphan_dir_info, node);
Filipe Manana0b3f4072020-12-10 12:09:02 +00002856 if (dir_ino < entry->ino)
Filipe Manana9dc44212014-02-19 14:31:44 +00002857 p = &(*p)->rb_left;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002858 else if (dir_ino > entry->ino)
Filipe Manana9dc44212014-02-19 14:31:44 +00002859 p = &(*p)->rb_right;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002860 else if (dir_gen < entry->gen)
2861 p = &(*p)->rb_left;
2862 else if (dir_gen > entry->gen)
2863 p = &(*p)->rb_right;
2864 else
Filipe Manana9dc44212014-02-19 14:31:44 +00002865 return entry;
Filipe Manana9dc44212014-02-19 14:31:44 +00002866 }
2867
Robbie Ko35c8eda2018-05-08 18:11:37 +08002868 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2869 if (!odi)
2870 return ERR_PTR(-ENOMEM);
2871 odi->ino = dir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002872 odi->gen = dir_gen;
Robbie Ko0f96f512018-05-08 18:11:38 +08002873 odi->last_dir_index_offset = 0;
Robbie Ko35c8eda2018-05-08 18:11:37 +08002874
Filipe Manana9dc44212014-02-19 14:31:44 +00002875 rb_link_node(&odi->node, parent, p);
2876 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2877 return odi;
2878}
2879
Filipe Manana0b3f4072020-12-10 12:09:02 +00002880static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
2881 u64 dir_ino, u64 gen)
Filipe Manana9dc44212014-02-19 14:31:44 +00002882{
2883 struct rb_node *n = sctx->orphan_dirs.rb_node;
2884 struct orphan_dir_info *entry;
2885
2886 while (n) {
2887 entry = rb_entry(n, struct orphan_dir_info, node);
2888 if (dir_ino < entry->ino)
2889 n = n->rb_left;
2890 else if (dir_ino > entry->ino)
2891 n = n->rb_right;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002892 else if (gen < entry->gen)
2893 n = n->rb_left;
2894 else if (gen > entry->gen)
2895 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002896 else
2897 return entry;
2898 }
2899 return NULL;
2900}
2901
Filipe Manana0b3f4072020-12-10 12:09:02 +00002902static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
Filipe Manana9dc44212014-02-19 14:31:44 +00002903{
Filipe Manana0b3f4072020-12-10 12:09:02 +00002904 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
Filipe Manana9dc44212014-02-19 14:31:44 +00002905
2906 return odi != NULL;
2907}
2908
2909static void free_orphan_dir_info(struct send_ctx *sctx,
2910 struct orphan_dir_info *odi)
2911{
2912 if (!odi)
2913 return;
2914 rb_erase(&odi->node, &sctx->orphan_dirs);
2915 kfree(odi);
2916}
2917
Alexander Block31db9f72012-07-25 23:19:24 +02002918/*
2919 * Returns 1 if a directory can be removed at this point in time.
2920 * We check this by iterating all dir items and checking if the inode behind
2921 * the dir item was already processed.
2922 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002923static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2924 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002925{
2926 int ret = 0;
2927 struct btrfs_root *root = sctx->parent_root;
2928 struct btrfs_path *path;
2929 struct btrfs_key key;
2930 struct btrfs_key found_key;
2931 struct btrfs_key loc;
2932 struct btrfs_dir_item *di;
Robbie Ko0f96f512018-05-08 18:11:38 +08002933 struct orphan_dir_info *odi = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02002934
Alexander Block6d85ed052012-08-01 14:48:59 +02002935 /*
2936 * Don't try to rmdir the top/root subvolume dir.
2937 */
2938 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2939 return 0;
2940
Alexander Block31db9f72012-07-25 23:19:24 +02002941 path = alloc_path_for_send();
2942 if (!path)
2943 return -ENOMEM;
2944
2945 key.objectid = dir;
2946 key.type = BTRFS_DIR_INDEX_KEY;
2947 key.offset = 0;
Robbie Ko0f96f512018-05-08 18:11:38 +08002948
Filipe Manana0b3f4072020-12-10 12:09:02 +00002949 odi = get_orphan_dir_info(sctx, dir, dir_gen);
Robbie Ko0f96f512018-05-08 18:11:38 +08002950 if (odi)
2951 key.offset = odi->last_dir_index_offset;
2952
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002953 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2954 if (ret < 0)
2955 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002956
2957 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002958 struct waiting_dir_move *dm;
2959
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002960 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2961 ret = btrfs_next_leaf(root, path);
2962 if (ret < 0)
2963 goto out;
2964 else if (ret > 0)
2965 break;
2966 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002967 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002968 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2969 path->slots[0]);
2970 if (found_key.objectid != key.objectid ||
2971 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002972 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002973
2974 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2975 struct btrfs_dir_item);
2976 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2977
Filipe Manana9dc44212014-02-19 14:31:44 +00002978 dm = get_waiting_dir_move(sctx, loc.objectid);
2979 if (dm) {
Filipe Manana0b3f4072020-12-10 12:09:02 +00002980 odi = add_orphan_dir_info(sctx, dir, dir_gen);
Filipe Manana9dc44212014-02-19 14:31:44 +00002981 if (IS_ERR(odi)) {
2982 ret = PTR_ERR(odi);
2983 goto out;
2984 }
2985 odi->gen = dir_gen;
Robbie Ko0f96f512018-05-08 18:11:38 +08002986 odi->last_dir_index_offset = found_key.offset;
Filipe Manana9dc44212014-02-19 14:31:44 +00002987 dm->rmdir_ino = dir;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002988 dm->rmdir_gen = dir_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00002989 ret = 0;
2990 goto out;
2991 }
2992
Alexander Block31db9f72012-07-25 23:19:24 +02002993 if (loc.objectid > send_progress) {
Filipe Manana0b3f4072020-12-10 12:09:02 +00002994 odi = add_orphan_dir_info(sctx, dir, dir_gen);
Robbie Ko0f96f512018-05-08 18:11:38 +08002995 if (IS_ERR(odi)) {
2996 ret = PTR_ERR(odi);
2997 goto out;
2998 }
2999 odi->gen = dir_gen;
3000 odi->last_dir_index_offset = found_key.offset;
Alexander Block31db9f72012-07-25 23:19:24 +02003001 ret = 0;
3002 goto out;
3003 }
3004
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003005 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003006 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003007 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02003008
3009 ret = 1;
3010
3011out:
3012 btrfs_free_path(path);
3013 return ret;
3014}
3015
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003016static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3017{
Filipe Manana9dc44212014-02-19 14:31:44 +00003018 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003019
Filipe Manana9dc44212014-02-19 14:31:44 +00003020 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003021}
3022
Filipe Manana8b191a62015-04-09 14:09:14 +01003023static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003024{
3025 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3026 struct rb_node *parent = NULL;
3027 struct waiting_dir_move *entry, *dm;
3028
David Sterbae780b0d2016-01-18 18:42:13 +01003029 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003030 if (!dm)
3031 return -ENOMEM;
3032 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003033 dm->rmdir_ino = 0;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003034 dm->rmdir_gen = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003035 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003036
3037 while (*p) {
3038 parent = *p;
3039 entry = rb_entry(parent, struct waiting_dir_move, node);
3040 if (ino < entry->ino) {
3041 p = &(*p)->rb_left;
3042 } else if (ino > entry->ino) {
3043 p = &(*p)->rb_right;
3044 } else {
3045 kfree(dm);
3046 return -EEXIST;
3047 }
3048 }
3049
3050 rb_link_node(&dm->node, parent, p);
3051 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3052 return 0;
3053}
3054
Filipe Manana9dc44212014-02-19 14:31:44 +00003055static struct waiting_dir_move *
3056get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003057{
3058 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3059 struct waiting_dir_move *entry;
3060
3061 while (n) {
3062 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003063 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003064 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003065 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003066 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003067 else
3068 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003069 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003070 return NULL;
3071}
3072
3073static void free_waiting_dir_move(struct send_ctx *sctx,
3074 struct waiting_dir_move *dm)
3075{
3076 if (!dm)
3077 return;
3078 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3079 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003080}
3081
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003082static int add_pending_dir_move(struct send_ctx *sctx,
3083 u64 ino,
3084 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003085 u64 parent_ino,
3086 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003087 struct list_head *deleted_refs,
3088 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003089{
3090 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3091 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003092 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003093 struct recorded_ref *cur;
3094 int exists = 0;
3095 int ret;
3096
David Sterbae780b0d2016-01-18 18:42:13 +01003097 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003098 if (!pm)
3099 return -ENOMEM;
3100 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003101 pm->ino = ino;
3102 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003103 INIT_LIST_HEAD(&pm->list);
3104 INIT_LIST_HEAD(&pm->update_refs);
3105 RB_CLEAR_NODE(&pm->node);
3106
3107 while (*p) {
3108 parent = *p;
3109 entry = rb_entry(parent, struct pending_dir_move, node);
3110 if (parent_ino < entry->parent_ino) {
3111 p = &(*p)->rb_left;
3112 } else if (parent_ino > entry->parent_ino) {
3113 p = &(*p)->rb_right;
3114 } else {
3115 exists = 1;
3116 break;
3117 }
3118 }
3119
Filipe Mananaf9594922014-03-27 20:14:01 +00003120 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003121 ret = dup_ref(cur, &pm->update_refs);
3122 if (ret < 0)
3123 goto out;
3124 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003125 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003126 ret = dup_ref(cur, &pm->update_refs);
3127 if (ret < 0)
3128 goto out;
3129 }
3130
Filipe Manana8b191a62015-04-09 14:09:14 +01003131 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003132 if (ret)
3133 goto out;
3134
3135 if (exists) {
3136 list_add_tail(&pm->list, &entry->list);
3137 } else {
3138 rb_link_node(&pm->node, parent, p);
3139 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3140 }
3141 ret = 0;
3142out:
3143 if (ret) {
3144 __free_recorded_refs(&pm->update_refs);
3145 kfree(pm);
3146 }
3147 return ret;
3148}
3149
3150static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3151 u64 parent_ino)
3152{
3153 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3154 struct pending_dir_move *entry;
3155
3156 while (n) {
3157 entry = rb_entry(n, struct pending_dir_move, node);
3158 if (parent_ino < entry->parent_ino)
3159 n = n->rb_left;
3160 else if (parent_ino > entry->parent_ino)
3161 n = n->rb_right;
3162 else
3163 return entry;
3164 }
3165 return NULL;
3166}
3167
Robbie Ko801bec32015-06-23 18:39:46 +08003168static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3169 u64 ino, u64 gen, u64 *ancestor_ino)
3170{
3171 int ret = 0;
3172 u64 parent_inode = 0;
3173 u64 parent_gen = 0;
3174 u64 start_ino = ino;
3175
3176 *ancestor_ino = 0;
3177 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3178 fs_path_reset(name);
3179
Filipe Manana0b3f4072020-12-10 12:09:02 +00003180 if (is_waiting_for_rm(sctx, ino, gen))
Robbie Ko801bec32015-06-23 18:39:46 +08003181 break;
3182 if (is_waiting_for_move(sctx, ino)) {
3183 if (*ancestor_ino == 0)
3184 *ancestor_ino = ino;
3185 ret = get_first_ref(sctx->parent_root, ino,
3186 &parent_inode, &parent_gen, name);
3187 } else {
3188 ret = __get_cur_name_and_parent(sctx, ino, gen,
3189 &parent_inode,
3190 &parent_gen, name);
3191 if (ret > 0) {
3192 ret = 0;
3193 break;
3194 }
3195 }
3196 if (ret < 0)
3197 break;
3198 if (parent_inode == start_ino) {
3199 ret = 1;
3200 if (*ancestor_ino == 0)
3201 *ancestor_ino = ino;
3202 break;
3203 }
3204 ino = parent_inode;
3205 gen = parent_gen;
3206 }
3207 return ret;
3208}
3209
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003210static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3211{
3212 struct fs_path *from_path = NULL;
3213 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003214 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003215 u64 orig_progress = sctx->send_progress;
3216 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003217 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003218 struct waiting_dir_move *dm = NULL;
3219 u64 rmdir_ino = 0;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003220 u64 rmdir_gen;
Robbie Ko801bec32015-06-23 18:39:46 +08003221 u64 ancestor;
3222 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003223 int ret;
3224
Filipe Manana2b863a12014-02-16 13:43:11 +00003225 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003226 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003227 if (!name || !from_path) {
3228 ret = -ENOMEM;
3229 goto out;
3230 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003231
Filipe Manana9dc44212014-02-19 14:31:44 +00003232 dm = get_waiting_dir_move(sctx, pm->ino);
3233 ASSERT(dm);
3234 rmdir_ino = dm->rmdir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003235 rmdir_gen = dm->rmdir_gen;
Robbie Ko801bec32015-06-23 18:39:46 +08003236 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003237 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003238
Robbie Ko801bec32015-06-23 18:39:46 +08003239 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003240 ret = gen_unique_name(sctx, pm->ino,
3241 pm->gen, from_path);
3242 } else {
3243 ret = get_first_ref(sctx->parent_root, pm->ino,
3244 &parent_ino, &parent_gen, name);
3245 if (ret < 0)
3246 goto out;
3247 ret = get_cur_path(sctx, parent_ino, parent_gen,
3248 from_path);
3249 if (ret < 0)
3250 goto out;
3251 ret = fs_path_add_path(from_path, name);
3252 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003253 if (ret < 0)
3254 goto out;
3255
Filipe Mananaf9594922014-03-27 20:14:01 +00003256 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003257 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003258 if (ret < 0)
3259 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003260 if (ret) {
3261 LIST_HEAD(deleted_refs);
3262 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3263 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3264 &pm->update_refs, &deleted_refs,
3265 is_orphan);
3266 if (ret < 0)
3267 goto out;
3268 if (rmdir_ino) {
3269 dm = get_waiting_dir_move(sctx, pm->ino);
3270 ASSERT(dm);
3271 dm->rmdir_ino = rmdir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003272 dm->rmdir_gen = rmdir_gen;
Robbie Ko801bec32015-06-23 18:39:46 +08003273 }
3274 goto out;
3275 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003276 fs_path_reset(name);
3277 to_path = name;
3278 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003279 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3280 if (ret < 0)
3281 goto out;
3282
3283 ret = send_rename(sctx, from_path, to_path);
3284 if (ret < 0)
3285 goto out;
3286
Filipe Manana9dc44212014-02-19 14:31:44 +00003287 if (rmdir_ino) {
3288 struct orphan_dir_info *odi;
Robbie Ko0f96f512018-05-08 18:11:38 +08003289 u64 gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003290
Filipe Manana0b3f4072020-12-10 12:09:02 +00003291 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
Filipe Manana9dc44212014-02-19 14:31:44 +00003292 if (!odi) {
3293 /* already deleted */
3294 goto finish;
3295 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003296 gen = odi->gen;
3297
3298 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003299 if (ret < 0)
3300 goto out;
3301 if (!ret)
3302 goto finish;
3303
3304 name = fs_path_alloc();
3305 if (!name) {
3306 ret = -ENOMEM;
3307 goto out;
3308 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003309 ret = get_cur_path(sctx, rmdir_ino, gen, name);
Filipe Manana9dc44212014-02-19 14:31:44 +00003310 if (ret < 0)
3311 goto out;
3312 ret = send_rmdir(sctx, name);
3313 if (ret < 0)
3314 goto out;
Filipe Manana9dc44212014-02-19 14:31:44 +00003315 }
3316
3317finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003318 ret = send_utimes(sctx, pm->ino, pm->gen);
3319 if (ret < 0)
3320 goto out;
3321
3322 /*
3323 * After rename/move, need to update the utimes of both new parent(s)
3324 * and old parent(s).
3325 */
3326 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003327 /*
3328 * The parent inode might have been deleted in the send snapshot
3329 */
3330 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3331 NULL, NULL, NULL, NULL, NULL);
3332 if (ret == -ENOENT) {
3333 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003334 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003335 }
3336 if (ret < 0)
3337 goto out;
3338
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003339 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3340 if (ret < 0)
3341 goto out;
3342 }
3343
3344out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003345 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003346 fs_path_free(from_path);
3347 fs_path_free(to_path);
3348 sctx->send_progress = orig_progress;
3349
3350 return ret;
3351}
3352
3353static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3354{
3355 if (!list_empty(&m->list))
3356 list_del(&m->list);
3357 if (!RB_EMPTY_NODE(&m->node))
3358 rb_erase(&m->node, &sctx->pending_dir_moves);
3359 __free_recorded_refs(&m->update_refs);
3360 kfree(m);
3361}
3362
Robbie Koa4390ae2018-11-14 18:32:37 +00003363static void tail_append_pending_moves(struct send_ctx *sctx,
3364 struct pending_dir_move *moves,
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003365 struct list_head *stack)
3366{
3367 if (list_empty(&moves->list)) {
3368 list_add_tail(&moves->list, stack);
3369 } else {
3370 LIST_HEAD(list);
3371 list_splice_init(&moves->list, &list);
3372 list_add_tail(&moves->list, stack);
3373 list_splice_tail(&list, stack);
3374 }
Robbie Koa4390ae2018-11-14 18:32:37 +00003375 if (!RB_EMPTY_NODE(&moves->node)) {
3376 rb_erase(&moves->node, &sctx->pending_dir_moves);
3377 RB_CLEAR_NODE(&moves->node);
3378 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003379}
3380
3381static int apply_children_dir_moves(struct send_ctx *sctx)
3382{
3383 struct pending_dir_move *pm;
3384 struct list_head stack;
3385 u64 parent_ino = sctx->cur_ino;
3386 int ret = 0;
3387
3388 pm = get_pending_dir_moves(sctx, parent_ino);
3389 if (!pm)
3390 return 0;
3391
3392 INIT_LIST_HEAD(&stack);
Robbie Koa4390ae2018-11-14 18:32:37 +00003393 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003394
3395 while (!list_empty(&stack)) {
3396 pm = list_first_entry(&stack, struct pending_dir_move, list);
3397 parent_ino = pm->ino;
3398 ret = apply_dir_move(sctx, pm);
3399 free_pending_move(sctx, pm);
3400 if (ret)
3401 goto out;
3402 pm = get_pending_dir_moves(sctx, parent_ino);
3403 if (pm)
Robbie Koa4390ae2018-11-14 18:32:37 +00003404 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003405 }
3406 return 0;
3407
3408out:
3409 while (!list_empty(&stack)) {
3410 pm = list_first_entry(&stack, struct pending_dir_move, list);
3411 free_pending_move(sctx, pm);
3412 }
3413 return ret;
3414}
3415
Filipe Manana84471e22015-02-28 22:29:22 +00003416/*
3417 * We might need to delay a directory rename even when no ancestor directory
3418 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3419 * renamed. This happens when we rename a directory to the old name (the name
3420 * in the parent root) of some other unrelated directory that got its rename
3421 * delayed due to some ancestor with higher number that got renamed.
3422 *
3423 * Example:
3424 *
3425 * Parent snapshot:
3426 * . (ino 256)
3427 * |---- a/ (ino 257)
3428 * | |---- file (ino 260)
3429 * |
3430 * |---- b/ (ino 258)
3431 * |---- c/ (ino 259)
3432 *
3433 * Send snapshot:
3434 * . (ino 256)
3435 * |---- a/ (ino 258)
3436 * |---- x/ (ino 259)
3437 * |---- y/ (ino 257)
3438 * |----- file (ino 260)
3439 *
3440 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3441 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3442 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3443 * must issue is:
3444 *
3445 * 1 - rename 259 from 'c' to 'x'
3446 * 2 - rename 257 from 'a' to 'x/y'
3447 * 3 - rename 258 from 'b' to 'a'
3448 *
3449 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3450 * be done right away and < 0 on error.
3451 */
3452static int wait_for_dest_dir_move(struct send_ctx *sctx,
3453 struct recorded_ref *parent_ref,
3454 const bool is_orphan)
3455{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003456 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003457 struct btrfs_path *path;
3458 struct btrfs_key key;
3459 struct btrfs_key di_key;
3460 struct btrfs_dir_item *di;
3461 u64 left_gen;
3462 u64 right_gen;
3463 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003464 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003465
3466 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3467 return 0;
3468
3469 path = alloc_path_for_send();
3470 if (!path)
3471 return -ENOMEM;
3472
3473 key.objectid = parent_ref->dir;
3474 key.type = BTRFS_DIR_ITEM_KEY;
3475 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3476
3477 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3478 if (ret < 0) {
3479 goto out;
3480 } else if (ret > 0) {
3481 ret = 0;
3482 goto out;
3483 }
3484
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003485 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3486 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003487 if (!di) {
3488 ret = 0;
3489 goto out;
3490 }
3491 /*
3492 * di_key.objectid has the number of the inode that has a dentry in the
3493 * parent directory with the same name that sctx->cur_ino is being
3494 * renamed to. We need to check if that inode is in the send root as
3495 * well and if it is currently marked as an inode with a pending rename,
3496 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3497 * that it happens after that other inode is renamed.
3498 */
3499 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3500 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3501 ret = 0;
3502 goto out;
3503 }
3504
3505 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3506 &left_gen, NULL, NULL, NULL, NULL);
3507 if (ret < 0)
3508 goto out;
3509 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3510 &right_gen, NULL, NULL, NULL, NULL);
3511 if (ret < 0) {
3512 if (ret == -ENOENT)
3513 ret = 0;
3514 goto out;
3515 }
3516
3517 /* Different inode, no need to delay the rename of sctx->cur_ino */
3518 if (right_gen != left_gen) {
3519 ret = 0;
3520 goto out;
3521 }
3522
Robbie Ko801bec32015-06-23 18:39:46 +08003523 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3524 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003525 ret = add_pending_dir_move(sctx,
3526 sctx->cur_ino,
3527 sctx->cur_inode_gen,
3528 di_key.objectid,
3529 &sctx->new_refs,
3530 &sctx->deleted_refs,
3531 is_orphan);
3532 if (!ret)
3533 ret = 1;
3534 }
3535out:
3536 btrfs_free_path(path);
3537 return ret;
3538}
3539
Filipe Manana80aa6022015-03-27 17:50:45 +00003540/*
Filipe Mananaea37d592017-11-17 01:54:00 +00003541 * Check if inode ino2, or any of its ancestors, is inode ino1.
3542 * Return 1 if true, 0 if false and < 0 on error.
3543 */
3544static int check_ino_in_path(struct btrfs_root *root,
3545 const u64 ino1,
3546 const u64 ino1_gen,
3547 const u64 ino2,
3548 const u64 ino2_gen,
3549 struct fs_path *fs_path)
3550{
3551 u64 ino = ino2;
3552
3553 if (ino1 == ino2)
3554 return ino1_gen == ino2_gen;
3555
3556 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3557 u64 parent;
3558 u64 parent_gen;
3559 int ret;
3560
3561 fs_path_reset(fs_path);
3562 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3563 if (ret < 0)
3564 return ret;
3565 if (parent == ino1)
3566 return parent_gen == ino1_gen;
3567 ino = parent;
3568 }
3569 return 0;
3570}
3571
3572/*
3573 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3574 * possible path (in case ino2 is not a directory and has multiple hard links).
Filipe Manana80aa6022015-03-27 17:50:45 +00003575 * Return 1 if true, 0 if false and < 0 on error.
3576 */
3577static int is_ancestor(struct btrfs_root *root,
3578 const u64 ino1,
3579 const u64 ino1_gen,
3580 const u64 ino2,
3581 struct fs_path *fs_path)
3582{
Filipe Mananaea37d592017-11-17 01:54:00 +00003583 bool free_fs_path = false;
Filipe Manana72c36682017-06-07 11:41:29 +01003584 int ret = 0;
Filipe Mananaea37d592017-11-17 01:54:00 +00003585 struct btrfs_path *path = NULL;
3586 struct btrfs_key key;
Filipe Manana72c36682017-06-07 11:41:29 +01003587
3588 if (!fs_path) {
3589 fs_path = fs_path_alloc();
3590 if (!fs_path)
3591 return -ENOMEM;
Filipe Mananaea37d592017-11-17 01:54:00 +00003592 free_fs_path = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003593 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003594
Filipe Mananaea37d592017-11-17 01:54:00 +00003595 path = alloc_path_for_send();
3596 if (!path) {
3597 ret = -ENOMEM;
3598 goto out;
Filipe Manana80aa6022015-03-27 17:50:45 +00003599 }
Filipe Mananaea37d592017-11-17 01:54:00 +00003600
3601 key.objectid = ino2;
3602 key.type = BTRFS_INODE_REF_KEY;
3603 key.offset = 0;
3604
3605 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3606 if (ret < 0)
3607 goto out;
3608
3609 while (true) {
3610 struct extent_buffer *leaf = path->nodes[0];
3611 int slot = path->slots[0];
3612 u32 cur_offset = 0;
3613 u32 item_size;
3614
3615 if (slot >= btrfs_header_nritems(leaf)) {
3616 ret = btrfs_next_leaf(root, path);
3617 if (ret < 0)
3618 goto out;
3619 if (ret > 0)
3620 break;
3621 continue;
3622 }
3623
3624 btrfs_item_key_to_cpu(leaf, &key, slot);
3625 if (key.objectid != ino2)
3626 break;
3627 if (key.type != BTRFS_INODE_REF_KEY &&
3628 key.type != BTRFS_INODE_EXTREF_KEY)
3629 break;
3630
3631 item_size = btrfs_item_size_nr(leaf, slot);
3632 while (cur_offset < item_size) {
3633 u64 parent;
3634 u64 parent_gen;
3635
3636 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3637 unsigned long ptr;
3638 struct btrfs_inode_extref *extref;
3639
3640 ptr = btrfs_item_ptr_offset(leaf, slot);
3641 extref = (struct btrfs_inode_extref *)
3642 (ptr + cur_offset);
3643 parent = btrfs_inode_extref_parent(leaf,
3644 extref);
3645 cur_offset += sizeof(*extref);
3646 cur_offset += btrfs_inode_extref_name_len(leaf,
3647 extref);
3648 } else {
3649 parent = key.offset;
3650 cur_offset = item_size;
3651 }
3652
3653 ret = get_inode_info(root, parent, NULL, &parent_gen,
3654 NULL, NULL, NULL, NULL);
3655 if (ret < 0)
3656 goto out;
3657 ret = check_ino_in_path(root, ino1, ino1_gen,
3658 parent, parent_gen, fs_path);
3659 if (ret)
3660 goto out;
3661 }
3662 path->slots[0]++;
3663 }
3664 ret = 0;
Filipe Manana72c36682017-06-07 11:41:29 +01003665 out:
Filipe Mananaea37d592017-11-17 01:54:00 +00003666 btrfs_free_path(path);
3667 if (free_fs_path)
Filipe Manana72c36682017-06-07 11:41:29 +01003668 fs_path_free(fs_path);
3669 return ret;
Filipe Manana80aa6022015-03-27 17:50:45 +00003670}
3671
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003672static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003673 struct recorded_ref *parent_ref,
3674 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003675{
Filipe Mananaf9594922014-03-27 20:14:01 +00003676 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003677 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003678 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003679 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003680 struct fs_path *path_before = NULL;
3681 struct fs_path *path_after = NULL;
3682 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003683
3684 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003685 path_before = fs_path_alloc();
3686 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003687 ret = -ENOMEM;
3688 goto out;
3689 }
3690
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003691 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003692 * Our current directory inode may not yet be renamed/moved because some
3693 * ancestor (immediate or not) has to be renamed/moved first. So find if
3694 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003695 * that ancestor is processed to avoid path build infinite loops (done
3696 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003697 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003698 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003699 u64 parent_ino_after_gen;
3700
Filipe Mananaf9594922014-03-27 20:14:01 +00003701 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003702 /*
3703 * If the current inode is an ancestor of ino in the
3704 * parent root, we need to delay the rename of the
3705 * current inode, otherwise don't delayed the rename
3706 * because we can end up with a circular dependency
3707 * of renames, resulting in some directories never
3708 * getting the respective rename operations issued in
3709 * the send stream or getting into infinite path build
3710 * loops.
3711 */
3712 ret = is_ancestor(sctx->parent_root,
3713 sctx->cur_ino, sctx->cur_inode_gen,
3714 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003715 if (ret)
3716 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003717 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003718
3719 fs_path_reset(path_before);
3720 fs_path_reset(path_after);
3721
3722 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003723 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003724 if (ret < 0)
3725 goto out;
3726 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3727 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003728 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003729 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003730 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003731 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003732 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003733 }
3734
3735 len1 = fs_path_len(path_before);
3736 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003737 if (ino > sctx->cur_ino &&
3738 (parent_ino_before != parent_ino_after || len1 != len2 ||
3739 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003740 u64 parent_ino_gen;
3741
3742 ret = get_inode_info(sctx->parent_root, ino, NULL,
3743 &parent_ino_gen, NULL, NULL, NULL,
3744 NULL);
3745 if (ret < 0)
3746 goto out;
3747 if (ino_gen == parent_ino_gen) {
3748 ret = 1;
3749 break;
3750 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003751 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003752 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003753 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003754 }
3755
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003756out:
3757 fs_path_free(path_before);
3758 fs_path_free(path_after);
3759
Filipe Mananaf9594922014-03-27 20:14:01 +00003760 if (ret == 1) {
3761 ret = add_pending_dir_move(sctx,
3762 sctx->cur_ino,
3763 sctx->cur_inode_gen,
3764 ino,
3765 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003766 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003767 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003768 if (!ret)
3769 ret = 1;
3770 }
3771
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003772 return ret;
3773}
3774
Filipe Mananaf5962782017-06-22 20:03:51 +01003775static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3776{
3777 int ret;
3778 struct fs_path *new_path;
3779
3780 /*
3781 * Our reference's name member points to its full_path member string, so
3782 * we use here a new path.
3783 */
3784 new_path = fs_path_alloc();
3785 if (!new_path)
3786 return -ENOMEM;
3787
3788 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3789 if (ret < 0) {
3790 fs_path_free(new_path);
3791 return ret;
3792 }
3793 ret = fs_path_add(new_path, ref->name, ref->name_len);
3794 if (ret < 0) {
3795 fs_path_free(new_path);
3796 return ret;
3797 }
3798
3799 fs_path_free(ref->full_path);
3800 set_ref_path(ref, new_path);
3801
3802 return 0;
3803}
3804
Alexander Block31db9f72012-07-25 23:19:24 +02003805/*
Filipe Manana9c2b4e02020-09-21 14:13:30 +01003806 * When processing the new references for an inode we may orphanize an existing
3807 * directory inode because its old name conflicts with one of the new references
3808 * of the current inode. Later, when processing another new reference of our
3809 * inode, we might need to orphanize another inode, but the path we have in the
3810 * reference reflects the pre-orphanization name of the directory we previously
3811 * orphanized. For example:
3812 *
3813 * parent snapshot looks like:
3814 *
3815 * . (ino 256)
3816 * |----- f1 (ino 257)
3817 * |----- f2 (ino 258)
3818 * |----- d1/ (ino 259)
3819 * |----- d2/ (ino 260)
3820 *
3821 * send snapshot looks like:
3822 *
3823 * . (ino 256)
3824 * |----- d1 (ino 258)
3825 * |----- f2/ (ino 259)
3826 * |----- f2_link/ (ino 260)
3827 * | |----- f1 (ino 257)
3828 * |
3829 * |----- d2 (ino 258)
3830 *
3831 * When processing inode 257 we compute the name for inode 259 as "d1", and we
3832 * cache it in the name cache. Later when we start processing inode 258, when
3833 * collecting all its new references we set a full path of "d1/d2" for its new
3834 * reference with name "d2". When we start processing the new references we
3835 * start by processing the new reference with name "d1", and this results in
3836 * orphanizing inode 259, since its old reference causes a conflict. Then we
3837 * move on the next new reference, with name "d2", and we find out we must
3838 * orphanize inode 260, as its old reference conflicts with ours - but for the
3839 * orphanization we use a source path corresponding to the path we stored in the
3840 * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
3841 * receiver fail since the path component "d1/" no longer exists, it was renamed
3842 * to "o259-6-0/" when processing the previous new reference. So in this case we
3843 * must recompute the path in the new reference and use it for the new
3844 * orphanization operation.
3845 */
3846static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3847{
3848 char *name;
3849 int ret;
3850
3851 name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
3852 if (!name)
3853 return -ENOMEM;
3854
3855 fs_path_reset(ref->full_path);
3856 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
3857 if (ret < 0)
3858 goto out;
3859
3860 ret = fs_path_add(ref->full_path, name, ref->name_len);
3861 if (ret < 0)
3862 goto out;
3863
3864 /* Update the reference's base name pointer. */
3865 set_ref_path(ref, ref->full_path);
3866out:
3867 kfree(name);
3868 return ret;
3869}
3870
3871/*
Alexander Block31db9f72012-07-25 23:19:24 +02003872 * This does all the move/link/unlink/rmdir magic.
3873 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003874static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003875{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003876 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003877 int ret = 0;
3878 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003879 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003880 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003881 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003882 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003883 u64 ow_gen;
Filipe Mananaf5962782017-06-22 20:03:51 +01003884 u64 ow_mode;
Alexander Block31db9f72012-07-25 23:19:24 +02003885 int did_overwrite = 0;
3886 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003887 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003888 bool can_rename = true;
Filipe Mananaf5962782017-06-22 20:03:51 +01003889 bool orphanized_dir = false;
Filipe Mananafdb13882017-06-13 14:13:11 +01003890 bool orphanized_ancestor = false;
Alexander Block31db9f72012-07-25 23:19:24 +02003891
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003892 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003893
Alexander Block6d85ed052012-08-01 14:48:59 +02003894 /*
3895 * This should never happen as the root dir always has the same ref
3896 * which is always '..'
3897 */
3898 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003899 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed052012-08-01 14:48:59 +02003900
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003901 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003902 if (!valid_path) {
3903 ret = -ENOMEM;
3904 goto out;
3905 }
3906
Alexander Block31db9f72012-07-25 23:19:24 +02003907 /*
3908 * First, check if the first ref of the current inode was overwritten
3909 * before. If yes, we know that the current inode was already orphanized
3910 * and thus use the orphan name. If not, we can use get_cur_path to
3911 * get the path of the first ref as it would like while receiving at
3912 * this point in time.
3913 * New inodes are always orphan at the beginning, so force to use the
3914 * orphan name in this case.
3915 * The first ref is stored in valid_path and will be updated if it
3916 * gets moved around.
3917 */
3918 if (!sctx->cur_inode_new) {
3919 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3920 sctx->cur_inode_gen);
3921 if (ret < 0)
3922 goto out;
3923 if (ret)
3924 did_overwrite = 1;
3925 }
3926 if (sctx->cur_inode_new || did_overwrite) {
3927 ret = gen_unique_name(sctx, sctx->cur_ino,
3928 sctx->cur_inode_gen, valid_path);
3929 if (ret < 0)
3930 goto out;
3931 is_orphan = 1;
3932 } else {
3933 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3934 valid_path);
3935 if (ret < 0)
3936 goto out;
3937 }
3938
Filipe Manana98272bb2020-09-21 14:13:29 +01003939 /*
3940 * Before doing any rename and link operations, do a first pass on the
3941 * new references to orphanize any unprocessed inodes that may have a
3942 * reference that conflicts with one of the new references of the current
3943 * inode. This needs to happen first because a new reference may conflict
3944 * with the old reference of a parent directory, so we must make sure
3945 * that the path used for link and rename commands don't use an
3946 * orphanized name when an ancestor was not yet orphanized.
3947 *
3948 * Example:
3949 *
3950 * Parent snapshot:
3951 *
3952 * . (ino 256)
3953 * |----- testdir/ (ino 259)
3954 * | |----- a (ino 257)
3955 * |
3956 * |----- b (ino 258)
3957 *
3958 * Send snapshot:
3959 *
3960 * . (ino 256)
3961 * |----- testdir_2/ (ino 259)
3962 * | |----- a (ino 260)
3963 * |
3964 * |----- testdir (ino 257)
3965 * |----- b (ino 257)
3966 * |----- b2 (ino 258)
3967 *
3968 * Processing the new reference for inode 257 with name "b" may happen
3969 * before processing the new reference with name "testdir". If so, we
3970 * must make sure that by the time we send a link command to create the
3971 * hard link "b", inode 259 was already orphanized, since the generated
3972 * path in "valid_path" already contains the orphanized name for 259.
3973 * We are processing inode 257, so only later when processing 259 we do
3974 * the rename operation to change its temporary (orphanized) name to
3975 * "testdir_2".
3976 */
Alexander Block31db9f72012-07-25 23:19:24 +02003977 list_for_each_entry(cur, &sctx->new_refs, list) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003978 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3979 if (ret < 0)
3980 goto out;
Filipe Manana98272bb2020-09-21 14:13:29 +01003981 if (ret == inode_state_will_create)
3982 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02003983
3984 /*
Filipe Manana98272bb2020-09-21 14:13:29 +01003985 * Check if this new ref would overwrite the first ref of another
3986 * unprocessed inode. If yes, orphanize the overwritten inode.
3987 * If we find an overwritten ref that is not the first ref,
3988 * simply unlink it.
Alexander Block31db9f72012-07-25 23:19:24 +02003989 */
3990 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3991 cur->name, cur->name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01003992 &ow_inode, &ow_gen, &ow_mode);
Alexander Block31db9f72012-07-25 23:19:24 +02003993 if (ret < 0)
3994 goto out;
3995 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003996 ret = is_first_ref(sctx->parent_root,
3997 ow_inode, cur->dir, cur->name,
3998 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003999 if (ret < 0)
4000 goto out;
4001 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00004002 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08004003 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00004004
Filipe Manana9c2b4e02020-09-21 14:13:30 +01004005 if (orphanized_dir) {
4006 ret = refresh_ref_path(sctx, cur);
4007 if (ret < 0)
4008 goto out;
4009 }
4010
Alexander Block31db9f72012-07-25 23:19:24 +02004011 ret = orphanize_inode(sctx, ow_inode, ow_gen,
4012 cur->full_path);
4013 if (ret < 0)
4014 goto out;
Filipe Mananaf5962782017-06-22 20:03:51 +01004015 if (S_ISDIR(ow_mode))
4016 orphanized_dir = true;
Robbie Ko801bec32015-06-23 18:39:46 +08004017
4018 /*
4019 * If ow_inode has its rename operation delayed
4020 * make sure that its orphanized name is used in
4021 * the source path when performing its rename
4022 * operation.
4023 */
4024 if (is_waiting_for_move(sctx, ow_inode)) {
4025 wdm = get_waiting_dir_move(sctx,
4026 ow_inode);
4027 ASSERT(wdm);
4028 wdm->orphanized = true;
4029 }
4030
Filipe Manana8996a482015-03-12 15:16:20 +00004031 /*
4032 * Make sure we clear our orphanized inode's
4033 * name from the name cache. This is because the
4034 * inode ow_inode might be an ancestor of some
4035 * other inode that will be orphanized as well
4036 * later and has an inode number greater than
4037 * sctx->send_progress. We need to prevent
4038 * future name lookups from using the old name
4039 * and get instead the orphan name.
4040 */
4041 nce = name_cache_search(sctx, ow_inode, ow_gen);
4042 if (nce) {
4043 name_cache_delete(sctx, nce);
4044 kfree(nce);
4045 }
Robbie Ko801bec32015-06-23 18:39:46 +08004046
4047 /*
4048 * ow_inode might currently be an ancestor of
4049 * cur_ino, therefore compute valid_path (the
4050 * current path of cur_ino) again because it
4051 * might contain the pre-orphanization name of
4052 * ow_inode, which is no longer valid.
4053 */
Filipe Manana72c36682017-06-07 11:41:29 +01004054 ret = is_ancestor(sctx->parent_root,
4055 ow_inode, ow_gen,
4056 sctx->cur_ino, NULL);
4057 if (ret > 0) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004058 orphanized_ancestor = true;
Filipe Manana72c36682017-06-07 11:41:29 +01004059 fs_path_reset(valid_path);
4060 ret = get_cur_path(sctx, sctx->cur_ino,
4061 sctx->cur_inode_gen,
4062 valid_path);
4063 }
Robbie Ko801bec32015-06-23 18:39:46 +08004064 if (ret < 0)
4065 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004066 } else {
4067 ret = send_unlink(sctx, cur->full_path);
4068 if (ret < 0)
4069 goto out;
4070 }
4071 }
4072
Filipe Manana98272bb2020-09-21 14:13:29 +01004073 }
4074
4075 list_for_each_entry(cur, &sctx->new_refs, list) {
4076 /*
4077 * We may have refs where the parent directory does not exist
4078 * yet. This happens if the parent directories inum is higher
4079 * than the current inum. To handle this case, we create the
4080 * parent directory out of order. But we need to check if this
4081 * did already happen before due to other refs in the same dir.
4082 */
4083 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4084 if (ret < 0)
4085 goto out;
4086 if (ret == inode_state_will_create) {
4087 ret = 0;
4088 /*
4089 * First check if any of the current inodes refs did
4090 * already create the dir.
4091 */
4092 list_for_each_entry(cur2, &sctx->new_refs, list) {
4093 if (cur == cur2)
4094 break;
4095 if (cur2->dir == cur->dir) {
4096 ret = 1;
4097 break;
4098 }
4099 }
4100
4101 /*
4102 * If that did not happen, check if a previous inode
4103 * did already create the dir.
4104 */
4105 if (!ret)
4106 ret = did_create_dir(sctx, cur->dir);
4107 if (ret < 0)
4108 goto out;
4109 if (!ret) {
4110 ret = send_create_inode(sctx, cur->dir);
4111 if (ret < 0)
4112 goto out;
4113 }
4114 }
4115
Filipe Manana84471e22015-02-28 22:29:22 +00004116 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4117 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4118 if (ret < 0)
4119 goto out;
4120 if (ret == 1) {
4121 can_rename = false;
4122 *pending_move = 1;
4123 }
4124 }
4125
Filipe Manana8b191a62015-04-09 14:09:14 +01004126 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4127 can_rename) {
4128 ret = wait_for_parent_move(sctx, cur, is_orphan);
4129 if (ret < 0)
4130 goto out;
4131 if (ret == 1) {
4132 can_rename = false;
4133 *pending_move = 1;
4134 }
4135 }
4136
Alexander Block31db9f72012-07-25 23:19:24 +02004137 /*
4138 * link/move the ref to the new place. If we have an orphan
4139 * inode, move it and update valid_path. If not, link or move
4140 * it depending on the inode mode.
4141 */
Filipe Manana84471e22015-02-28 22:29:22 +00004142 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004143 ret = send_rename(sctx, valid_path, cur->full_path);
4144 if (ret < 0)
4145 goto out;
4146 is_orphan = 0;
4147 ret = fs_path_copy(valid_path, cur->full_path);
4148 if (ret < 0)
4149 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00004150 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004151 if (S_ISDIR(sctx->cur_inode_mode)) {
4152 /*
4153 * Dirs can't be linked, so move it. For moved
4154 * dirs, we always have one new and one deleted
4155 * ref. The deleted ref is ignored later.
4156 */
Filipe Manana8b191a62015-04-09 14:09:14 +01004157 ret = send_rename(sctx, valid_path,
4158 cur->full_path);
4159 if (!ret)
4160 ret = fs_path_copy(valid_path,
4161 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004162 if (ret < 0)
4163 goto out;
4164 } else {
Filipe Mananaf5962782017-06-22 20:03:51 +01004165 /*
4166 * We might have previously orphanized an inode
4167 * which is an ancestor of our current inode,
4168 * so our reference's full path, which was
4169 * computed before any such orphanizations, must
4170 * be updated.
4171 */
4172 if (orphanized_dir) {
4173 ret = update_ref_path(sctx, cur);
4174 if (ret < 0)
4175 goto out;
4176 }
Alexander Block31db9f72012-07-25 23:19:24 +02004177 ret = send_link(sctx, cur->full_path,
4178 valid_path);
4179 if (ret < 0)
4180 goto out;
4181 }
4182 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004183 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004184 if (ret < 0)
4185 goto out;
4186 }
4187
4188 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4189 /*
4190 * Check if we can already rmdir the directory. If not,
4191 * orphanize it. For every dir item inside that gets deleted
4192 * later, we do this check again and rmdir it then if possible.
4193 * See the use of check_dirs for more details.
4194 */
Filipe Manana9dc44212014-02-19 14:31:44 +00004195 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4196 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004197 if (ret < 0)
4198 goto out;
4199 if (ret) {
4200 ret = send_rmdir(sctx, valid_path);
4201 if (ret < 0)
4202 goto out;
4203 } else if (!is_orphan) {
4204 ret = orphanize_inode(sctx, sctx->cur_ino,
4205 sctx->cur_inode_gen, valid_path);
4206 if (ret < 0)
4207 goto out;
4208 is_orphan = 1;
4209 }
4210
4211 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004212 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004213 if (ret < 0)
4214 goto out;
4215 }
Alexander Blockccf16262012-07-28 11:46:29 +02004216 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4217 !list_empty(&sctx->deleted_refs)) {
4218 /*
4219 * We have a moved dir. Add the old parent to check_dirs
4220 */
4221 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4222 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04004223 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02004224 if (ret < 0)
4225 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004226 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4227 /*
4228 * We have a non dir inode. Go through all deleted refs and
4229 * unlink them if they were not already overwritten by other
4230 * inodes.
4231 */
4232 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4233 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4234 sctx->cur_ino, sctx->cur_inode_gen,
4235 cur->name, cur->name_len);
4236 if (ret < 0)
4237 goto out;
4238 if (!ret) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004239 /*
4240 * If we orphanized any ancestor before, we need
4241 * to recompute the full path for deleted names,
4242 * since any such path was computed before we
4243 * processed any references and orphanized any
4244 * ancestor inode.
4245 */
4246 if (orphanized_ancestor) {
Filipe Mananaf5962782017-06-22 20:03:51 +01004247 ret = update_ref_path(sctx, cur);
4248 if (ret < 0)
Filipe Mananafdb13882017-06-13 14:13:11 +01004249 goto out;
Filipe Mananafdb13882017-06-13 14:13:11 +01004250 }
Alexander Block1f4692d2012-07-28 10:42:24 +02004251 ret = send_unlink(sctx, cur->full_path);
4252 if (ret < 0)
4253 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004254 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004255 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004256 if (ret < 0)
4257 goto out;
4258 }
Alexander Block31db9f72012-07-25 23:19:24 +02004259 /*
4260 * If the inode is still orphan, unlink the orphan. This may
4261 * happen when a previous inode did overwrite the first ref
4262 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02004263 * inode. Unlinking does not mean that the inode is deleted in
4264 * all cases. There may still be links to this inode in other
4265 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02004266 */
Alexander Block1f4692d2012-07-28 10:42:24 +02004267 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02004268 ret = send_unlink(sctx, valid_path);
4269 if (ret < 0)
4270 goto out;
4271 }
4272 }
4273
4274 /*
4275 * We did collect all parent dirs where cur_inode was once located. We
4276 * now go through all these dirs and check if they are pending for
4277 * deletion and if it's finally possible to perform the rmdir now.
4278 * We also update the inode stats of the parent dirs here.
4279 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004280 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02004281 /*
4282 * In case we had refs into dirs that were not processed yet,
4283 * we don't need to do the utime and rmdir logic for these dirs.
4284 * The dir will be processed later.
4285 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004286 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02004287 continue;
4288
Josef Bacikba5e8f22013-08-16 16:52:55 -04004289 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004290 if (ret < 0)
4291 goto out;
4292
4293 if (ret == inode_state_did_create ||
4294 ret == inode_state_no_change) {
4295 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004296 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004297 if (ret < 0)
4298 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004299 } else if (ret == inode_state_did_delete &&
4300 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004301 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4302 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004303 if (ret < 0)
4304 goto out;
4305 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004306 ret = get_cur_path(sctx, cur->dir,
4307 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004308 if (ret < 0)
4309 goto out;
4310 ret = send_rmdir(sctx, valid_path);
4311 if (ret < 0)
4312 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004313 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004314 }
4315 }
4316 }
4317
Alexander Block31db9f72012-07-25 23:19:24 +02004318 ret = 0;
4319
4320out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004321 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004322 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004323 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004324 return ret;
4325}
4326
Nikolay Borisova0357512017-08-21 12:43:44 +03004327static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4328 void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004329{
4330 int ret = 0;
4331 struct send_ctx *sctx = ctx;
4332 struct fs_path *p;
4333 u64 gen;
4334
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004335 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004336 if (!p)
4337 return -ENOMEM;
4338
Liu Boa4d96d62014-03-03 21:31:03 +08004339 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004340 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004341 if (ret < 0)
4342 goto out;
4343
Alexander Block31db9f72012-07-25 23:19:24 +02004344 ret = get_cur_path(sctx, dir, gen, p);
4345 if (ret < 0)
4346 goto out;
4347 ret = fs_path_add_path(p, name);
4348 if (ret < 0)
4349 goto out;
4350
Liu Boa4d96d62014-03-03 21:31:03 +08004351 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004352
4353out:
4354 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004355 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004356 return ret;
4357}
4358
Liu Boa4d96d62014-03-03 21:31:03 +08004359static int __record_new_ref(int num, u64 dir, int index,
4360 struct fs_path *name,
4361 void *ctx)
4362{
4363 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004364 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
Liu Boa4d96d62014-03-03 21:31:03 +08004365}
4366
4367
Alexander Block31db9f72012-07-25 23:19:24 +02004368static int __record_deleted_ref(int num, u64 dir, int index,
4369 struct fs_path *name,
4370 void *ctx)
4371{
Alexander Block31db9f72012-07-25 23:19:24 +02004372 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004373 return record_ref(sctx->parent_root, dir, name, ctx,
4374 &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004375}
4376
4377static int record_new_ref(struct send_ctx *sctx)
4378{
4379 int ret;
4380
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004381 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4382 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004383 if (ret < 0)
4384 goto out;
4385 ret = 0;
4386
4387out:
4388 return ret;
4389}
4390
4391static int record_deleted_ref(struct send_ctx *sctx)
4392{
4393 int ret;
4394
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004395 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4396 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004397 if (ret < 0)
4398 goto out;
4399 ret = 0;
4400
4401out:
4402 return ret;
4403}
4404
4405struct find_ref_ctx {
4406 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004407 u64 dir_gen;
4408 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004409 struct fs_path *name;
4410 int found_idx;
4411};
4412
4413static int __find_iref(int num, u64 dir, int index,
4414 struct fs_path *name,
4415 void *ctx_)
4416{
4417 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004418 u64 dir_gen;
4419 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004420
4421 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4422 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004423 /*
4424 * To avoid doing extra lookups we'll only do this if everything
4425 * else matches.
4426 */
4427 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4428 NULL, NULL, NULL);
4429 if (ret)
4430 return ret;
4431 if (dir_gen != ctx->dir_gen)
4432 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004433 ctx->found_idx = num;
4434 return 1;
4435 }
4436 return 0;
4437}
4438
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004439static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004440 struct btrfs_path *path,
4441 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004442 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004443{
4444 int ret;
4445 struct find_ref_ctx ctx;
4446
4447 ctx.dir = dir;
4448 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004449 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004450 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004451 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004452
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004453 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004454 if (ret < 0)
4455 return ret;
4456
4457 if (ctx.found_idx == -1)
4458 return -ENOENT;
4459
4460 return ctx.found_idx;
4461}
4462
4463static int __record_changed_new_ref(int num, u64 dir, int index,
4464 struct fs_path *name,
4465 void *ctx)
4466{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004467 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004468 int ret;
4469 struct send_ctx *sctx = ctx;
4470
Josef Bacikba5e8f22013-08-16 16:52:55 -04004471 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4472 NULL, NULL, NULL);
4473 if (ret)
4474 return ret;
4475
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004476 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004477 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004478 if (ret == -ENOENT)
4479 ret = __record_new_ref(num, dir, index, name, sctx);
4480 else if (ret > 0)
4481 ret = 0;
4482
4483 return ret;
4484}
4485
4486static int __record_changed_deleted_ref(int num, u64 dir, int index,
4487 struct fs_path *name,
4488 void *ctx)
4489{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004490 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004491 int ret;
4492 struct send_ctx *sctx = ctx;
4493
Josef Bacikba5e8f22013-08-16 16:52:55 -04004494 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4495 NULL, NULL, NULL);
4496 if (ret)
4497 return ret;
4498
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004499 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004500 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004501 if (ret == -ENOENT)
4502 ret = __record_deleted_ref(num, dir, index, name, sctx);
4503 else if (ret > 0)
4504 ret = 0;
4505
4506 return ret;
4507}
4508
4509static int record_changed_ref(struct send_ctx *sctx)
4510{
4511 int ret = 0;
4512
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004513 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004514 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4515 if (ret < 0)
4516 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004517 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004518 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4519 if (ret < 0)
4520 goto out;
4521 ret = 0;
4522
4523out:
4524 return ret;
4525}
4526
4527/*
4528 * Record and process all refs at once. Needed when an inode changes the
4529 * generation number, which means that it was deleted and recreated.
4530 */
4531static int process_all_refs(struct send_ctx *sctx,
4532 enum btrfs_compare_tree_result cmd)
4533{
4534 int ret;
4535 struct btrfs_root *root;
4536 struct btrfs_path *path;
4537 struct btrfs_key key;
4538 struct btrfs_key found_key;
4539 struct extent_buffer *eb;
4540 int slot;
4541 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004542 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004543
4544 path = alloc_path_for_send();
4545 if (!path)
4546 return -ENOMEM;
4547
4548 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4549 root = sctx->send_root;
4550 cb = __record_new_ref;
4551 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4552 root = sctx->parent_root;
4553 cb = __record_deleted_ref;
4554 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004555 btrfs_err(sctx->send_root->fs_info,
4556 "Wrong command %d in process_all_refs", cmd);
4557 ret = -EINVAL;
4558 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004559 }
4560
4561 key.objectid = sctx->cmp_key->objectid;
4562 key.type = BTRFS_INODE_REF_KEY;
4563 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004564 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4565 if (ret < 0)
4566 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004567
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004568 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004569 eb = path->nodes[0];
4570 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004571 if (slot >= btrfs_header_nritems(eb)) {
4572 ret = btrfs_next_leaf(root, path);
4573 if (ret < 0)
4574 goto out;
4575 else if (ret > 0)
4576 break;
4577 continue;
4578 }
4579
Alexander Block31db9f72012-07-25 23:19:24 +02004580 btrfs_item_key_to_cpu(eb, &found_key, slot);
4581
4582 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004583 (found_key.type != BTRFS_INODE_REF_KEY &&
4584 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004585 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004586
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004587 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004588 if (ret < 0)
4589 goto out;
4590
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004591 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004592 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004593 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004594
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004595 /*
4596 * We don't actually care about pending_move as we are simply
4597 * re-creating this inode and will be rename'ing it into place once we
4598 * rename the parent directory.
4599 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004600 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004601out:
4602 btrfs_free_path(path);
4603 return ret;
4604}
4605
4606static int send_set_xattr(struct send_ctx *sctx,
4607 struct fs_path *path,
4608 const char *name, int name_len,
4609 const char *data, int data_len)
4610{
4611 int ret = 0;
4612
4613 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4614 if (ret < 0)
4615 goto out;
4616
4617 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4618 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4619 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4620
4621 ret = send_cmd(sctx);
4622
4623tlv_put_failure:
4624out:
4625 return ret;
4626}
4627
4628static int send_remove_xattr(struct send_ctx *sctx,
4629 struct fs_path *path,
4630 const char *name, int name_len)
4631{
4632 int ret = 0;
4633
4634 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4635 if (ret < 0)
4636 goto out;
4637
4638 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4639 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4640
4641 ret = send_cmd(sctx);
4642
4643tlv_put_failure:
4644out:
4645 return ret;
4646}
4647
4648static int __process_new_xattr(int num, struct btrfs_key *di_key,
4649 const char *name, int name_len,
4650 const char *data, int data_len,
4651 u8 type, void *ctx)
4652{
4653 int ret;
4654 struct send_ctx *sctx = ctx;
4655 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004656 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004657
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -03004658 /* Capabilities are emitted by finish_inode_if_needed */
4659 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4660 return 0;
4661
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004662 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004663 if (!p)
4664 return -ENOMEM;
4665
4666 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004667 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004668 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004669 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004670 * only contains the version number and no entries.
4671 */
4672 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4673 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4674 if (data_len == 0) {
4675 dummy_acl.a_version =
4676 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4677 data = (char *)&dummy_acl;
4678 data_len = sizeof(dummy_acl);
4679 }
4680 }
4681
4682 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4683 if (ret < 0)
4684 goto out;
4685
4686 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4687
4688out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004689 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004690 return ret;
4691}
4692
4693static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4694 const char *name, int name_len,
4695 const char *data, int data_len,
4696 u8 type, void *ctx)
4697{
4698 int ret;
4699 struct send_ctx *sctx = ctx;
4700 struct fs_path *p;
4701
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004702 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004703 if (!p)
4704 return -ENOMEM;
4705
4706 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4707 if (ret < 0)
4708 goto out;
4709
4710 ret = send_remove_xattr(sctx, p, name, name_len);
4711
4712out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004713 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004714 return ret;
4715}
4716
4717static int process_new_xattr(struct send_ctx *sctx)
4718{
4719 int ret = 0;
4720
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004721 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004722 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004723
4724 return ret;
4725}
4726
4727static int process_deleted_xattr(struct send_ctx *sctx)
4728{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004729 return iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004730 __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004731}
4732
4733struct find_xattr_ctx {
4734 const char *name;
4735 int name_len;
4736 int found_idx;
4737 char *found_data;
4738 int found_data_len;
4739};
4740
4741static int __find_xattr(int num, struct btrfs_key *di_key,
4742 const char *name, int name_len,
4743 const char *data, int data_len,
4744 u8 type, void *vctx)
4745{
4746 struct find_xattr_ctx *ctx = vctx;
4747
4748 if (name_len == ctx->name_len &&
4749 strncmp(name, ctx->name, name_len) == 0) {
4750 ctx->found_idx = num;
4751 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004752 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004753 if (!ctx->found_data)
4754 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004755 return 1;
4756 }
4757 return 0;
4758}
4759
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004760static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004761 struct btrfs_path *path,
4762 struct btrfs_key *key,
4763 const char *name, int name_len,
4764 char **data, int *data_len)
4765{
4766 int ret;
4767 struct find_xattr_ctx ctx;
4768
4769 ctx.name = name;
4770 ctx.name_len = name_len;
4771 ctx.found_idx = -1;
4772 ctx.found_data = NULL;
4773 ctx.found_data_len = 0;
4774
Nikolay Borisova0357512017-08-21 12:43:44 +03004775 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004776 if (ret < 0)
4777 return ret;
4778
4779 if (ctx.found_idx == -1)
4780 return -ENOENT;
4781 if (data) {
4782 *data = ctx.found_data;
4783 *data_len = ctx.found_data_len;
4784 } else {
4785 kfree(ctx.found_data);
4786 }
4787 return ctx.found_idx;
4788}
4789
4790
4791static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4792 const char *name, int name_len,
4793 const char *data, int data_len,
4794 u8 type, void *ctx)
4795{
4796 int ret;
4797 struct send_ctx *sctx = ctx;
4798 char *found_data = NULL;
4799 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004800
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004801 ret = find_xattr(sctx->parent_root, sctx->right_path,
4802 sctx->cmp_key, name, name_len, &found_data,
4803 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004804 if (ret == -ENOENT) {
4805 ret = __process_new_xattr(num, di_key, name, name_len, data,
4806 data_len, type, ctx);
4807 } else if (ret >= 0) {
4808 if (data_len != found_data_len ||
4809 memcmp(data, found_data, data_len)) {
4810 ret = __process_new_xattr(num, di_key, name, name_len,
4811 data, data_len, type, ctx);
4812 } else {
4813 ret = 0;
4814 }
4815 }
4816
4817 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004818 return ret;
4819}
4820
4821static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4822 const char *name, int name_len,
4823 const char *data, int data_len,
4824 u8 type, void *ctx)
4825{
4826 int ret;
4827 struct send_ctx *sctx = ctx;
4828
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004829 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4830 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004831 if (ret == -ENOENT)
4832 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4833 data_len, type, ctx);
4834 else if (ret >= 0)
4835 ret = 0;
4836
4837 return ret;
4838}
4839
4840static int process_changed_xattr(struct send_ctx *sctx)
4841{
4842 int ret = 0;
4843
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004844 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004845 __process_changed_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004846 if (ret < 0)
4847 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004848 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004849 __process_changed_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004850
4851out:
4852 return ret;
4853}
4854
4855static int process_all_new_xattrs(struct send_ctx *sctx)
4856{
4857 int ret;
4858 struct btrfs_root *root;
4859 struct btrfs_path *path;
4860 struct btrfs_key key;
4861 struct btrfs_key found_key;
4862 struct extent_buffer *eb;
4863 int slot;
4864
4865 path = alloc_path_for_send();
4866 if (!path)
4867 return -ENOMEM;
4868
4869 root = sctx->send_root;
4870
4871 key.objectid = sctx->cmp_key->objectid;
4872 key.type = BTRFS_XATTR_ITEM_KEY;
4873 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004874 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4875 if (ret < 0)
4876 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004877
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004878 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004879 eb = path->nodes[0];
4880 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004881 if (slot >= btrfs_header_nritems(eb)) {
4882 ret = btrfs_next_leaf(root, path);
4883 if (ret < 0) {
4884 goto out;
4885 } else if (ret > 0) {
4886 ret = 0;
4887 break;
4888 }
4889 continue;
4890 }
Alexander Block31db9f72012-07-25 23:19:24 +02004891
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004892 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004893 if (found_key.objectid != key.objectid ||
4894 found_key.type != key.type) {
4895 ret = 0;
4896 goto out;
4897 }
4898
Nikolay Borisova0357512017-08-21 12:43:44 +03004899 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004900 if (ret < 0)
4901 goto out;
4902
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004903 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004904 }
4905
4906out:
4907 btrfs_free_path(path);
4908 return ret;
4909}
4910
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07004911static inline u64 max_send_read_size(const struct send_ctx *sctx)
4912{
4913 return sctx->send_max_size - SZ_16K;
4914}
4915
4916static int put_data_header(struct send_ctx *sctx, u32 len)
4917{
4918 struct btrfs_tlv_header *hdr;
4919
4920 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
4921 return -EOVERFLOW;
4922 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
4923 put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
4924 put_unaligned_le16(len, &hdr->tlv_len);
4925 sctx->send_size += sizeof(*hdr);
4926 return 0;
4927}
4928
4929static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
Josef Baciked259092013-10-25 11:36:01 -04004930{
4931 struct btrfs_root *root = sctx->send_root;
4932 struct btrfs_fs_info *fs_info = root->fs_info;
4933 struct inode *inode;
4934 struct page *page;
4935 char *addr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004936 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004937 pgoff_t last_index;
Johannes Thumshirn70730172018-12-05 15:23:03 +01004938 unsigned pg_offset = offset_in_page(offset);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07004939 int ret;
4940
4941 ret = put_data_header(sctx, len);
4942 if (ret)
4943 return ret;
Josef Baciked259092013-10-25 11:36:01 -04004944
David Sterba0202e832020-05-15 19:35:59 +02004945 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
Josef Baciked259092013-10-25 11:36:01 -04004946 if (IS_ERR(inode))
4947 return PTR_ERR(inode);
4948
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004949 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004950
4951 /* initial readahead */
4952 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4953 file_ra_state_init(&sctx->ra, inode->i_mapping);
Liu Bo2131bcd2014-03-05 10:07:35 +08004954
Josef Baciked259092013-10-25 11:36:01 -04004955 while (index <= last_index) {
4956 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004957 PAGE_SIZE - pg_offset);
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004958
4959 page = find_lock_page(inode->i_mapping, index);
Josef Baciked259092013-10-25 11:36:01 -04004960 if (!page) {
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004961 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4962 NULL, index, last_index + 1 - index);
4963
4964 page = find_or_create_page(inode->i_mapping, index,
4965 GFP_KERNEL);
4966 if (!page) {
4967 ret = -ENOMEM;
4968 break;
4969 }
4970 }
4971
4972 if (PageReadahead(page)) {
4973 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4974 NULL, page, index, last_index + 1 - index);
Josef Baciked259092013-10-25 11:36:01 -04004975 }
4976
4977 if (!PageUptodate(page)) {
4978 btrfs_readpage(NULL, page);
4979 lock_page(page);
4980 if (!PageUptodate(page)) {
4981 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004982 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004983 ret = -EIO;
4984 break;
4985 }
4986 }
4987
4988 addr = kmap(page);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07004989 memcpy(sctx->send_buf + sctx->send_size, addr + pg_offset,
4990 cur_len);
Josef Baciked259092013-10-25 11:36:01 -04004991 kunmap(page);
4992 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004993 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004994 index++;
4995 pg_offset = 0;
4996 len -= cur_len;
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07004997 sctx->send_size += cur_len;
Josef Baciked259092013-10-25 11:36:01 -04004998 }
Josef Baciked259092013-10-25 11:36:01 -04004999 iput(inode);
5000 return ret;
5001}
5002
Alexander Block31db9f72012-07-25 23:19:24 +02005003/*
5004 * Read some bytes from the current inode/file and send a write command to
5005 * user space.
5006 */
5007static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
5008{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04005009 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02005010 int ret = 0;
5011 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02005012
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005013 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02005014 if (!p)
5015 return -ENOMEM;
5016
Jeff Mahoney04ab9562016-09-20 10:05:03 -04005017 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005018
Alexander Block31db9f72012-07-25 23:19:24 +02005019 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5020 if (ret < 0)
5021 goto out;
5022
5023 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5024 if (ret < 0)
5025 goto out;
5026
5027 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5028 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005029 ret = put_file_data(sctx, offset, len);
5030 if (ret < 0)
5031 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005032
5033 ret = send_cmd(sctx);
5034
5035tlv_put_failure:
5036out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005037 fs_path_free(p);
Omar Sandovala9b2e0d2020-08-21 00:39:51 -07005038 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005039}
5040
5041/*
5042 * Send a clone command to user space.
5043 */
5044static int send_clone(struct send_ctx *sctx,
5045 u64 offset, u32 len,
5046 struct clone_root *clone_root)
5047{
5048 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005049 struct fs_path *p;
5050 u64 gen;
5051
Jeff Mahoney04ab9562016-09-20 10:05:03 -04005052 btrfs_debug(sctx->send_root->fs_info,
5053 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005054 offset, len, clone_root->root->root_key.objectid,
5055 clone_root->ino, clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02005056
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005057 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02005058 if (!p)
5059 return -ENOMEM;
5060
5061 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5062 if (ret < 0)
5063 goto out;
5064
5065 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5066 if (ret < 0)
5067 goto out;
5068
5069 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5070 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5071 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5072
Alexander Blocke938c8a2012-07-28 16:33:49 +02005073 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02005074 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005075 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005076 if (ret < 0)
5077 goto out;
5078 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5079 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005080 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02005081 }
5082 if (ret < 0)
5083 goto out;
5084
Josef Bacik37b8d272015-06-04 17:17:25 -04005085 /*
5086 * If the parent we're using has a received_uuid set then use that as
5087 * our clone source as that is what we will look for when doing a
5088 * receive.
5089 *
5090 * This covers the case that we create a snapshot off of a received
5091 * subvolume and then use that as the parent and try to receive on a
5092 * different host.
5093 */
5094 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
5095 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5096 clone_root->root->root_item.received_uuid);
5097 else
5098 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5099 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02005100 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
David Sterba09e3a282020-09-15 14:30:15 +02005101 btrfs_root_ctransid(&clone_root->root->root_item));
Alexander Block31db9f72012-07-25 23:19:24 +02005102 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5103 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5104 clone_root->offset);
5105
5106 ret = send_cmd(sctx);
5107
5108tlv_put_failure:
5109out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005110 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02005111 return ret;
5112}
5113
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005114/*
5115 * Send an update extent command to user space.
5116 */
5117static int send_update_extent(struct send_ctx *sctx,
5118 u64 offset, u32 len)
5119{
5120 int ret = 0;
5121 struct fs_path *p;
5122
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005123 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005124 if (!p)
5125 return -ENOMEM;
5126
5127 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5128 if (ret < 0)
5129 goto out;
5130
5131 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5132 if (ret < 0)
5133 goto out;
5134
5135 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5136 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5137 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5138
5139 ret = send_cmd(sctx);
5140
5141tlv_put_failure:
5142out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005143 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005144 return ret;
5145}
5146
Josef Bacik16e75492013-10-22 12:18:51 -04005147static int send_hole(struct send_ctx *sctx, u64 end)
5148{
5149 struct fs_path *p = NULL;
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005150 u64 read_size = max_send_read_size(sctx);
Josef Bacik16e75492013-10-22 12:18:51 -04005151 u64 offset = sctx->cur_inode_last_extent;
Josef Bacik16e75492013-10-22 12:18:51 -04005152 int ret = 0;
5153
Filipe Manana22d31512018-07-30 12:39:58 +01005154 /*
5155 * A hole that starts at EOF or beyond it. Since we do not yet support
5156 * fallocate (for extent preallocation and hole punching), sending a
5157 * write of zeroes starting at EOF or beyond would later require issuing
5158 * a truncate operation which would undo the write and achieve nothing.
5159 */
5160 if (offset >= sctx->cur_inode_size)
5161 return 0;
5162
Filipe Manana6b1f72e52019-05-20 09:55:42 +01005163 /*
5164 * Don't go beyond the inode's i_size due to prealloc extents that start
5165 * after the i_size.
5166 */
5167 end = min_t(u64, end, sctx->cur_inode_size);
5168
Filipe Mananad4dfc0f2018-02-06 20:39:20 +00005169 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5170 return send_update_extent(sctx, offset, end - offset);
5171
Josef Bacik16e75492013-10-22 12:18:51 -04005172 p = fs_path_alloc();
5173 if (!p)
5174 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01005175 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5176 if (ret < 0)
5177 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04005178 while (offset < end) {
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005179 u64 len = min(end - offset, read_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005180
5181 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5182 if (ret < 0)
5183 break;
Josef Bacik16e75492013-10-22 12:18:51 -04005184 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5185 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005186 ret = put_data_header(sctx, len);
5187 if (ret < 0)
5188 break;
5189 memset(sctx->send_buf + sctx->send_size, 0, len);
5190 sctx->send_size += len;
Josef Bacik16e75492013-10-22 12:18:51 -04005191 ret = send_cmd(sctx);
5192 if (ret < 0)
5193 break;
5194 offset += len;
5195 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005196 sctx->cur_inode_next_write_offset = offset;
Josef Bacik16e75492013-10-22 12:18:51 -04005197tlv_put_failure:
5198 fs_path_free(p);
5199 return ret;
5200}
5201
Filipe Mananad906d492015-10-02 10:47:34 +01005202static int send_extent_data(struct send_ctx *sctx,
5203 const u64 offset,
5204 const u64 len)
5205{
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005206 u64 read_size = max_send_read_size(sctx);
Filipe Mananad906d492015-10-02 10:47:34 +01005207 u64 sent = 0;
5208
5209 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5210 return send_update_extent(sctx, offset, len);
5211
5212 while (sent < len) {
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005213 u64 size = min(len - sent, read_size);
Filipe Mananad906d492015-10-02 10:47:34 +01005214 int ret;
5215
Filipe Mananad906d492015-10-02 10:47:34 +01005216 ret = send_write(sctx, offset + sent, size);
5217 if (ret < 0)
5218 return ret;
Omar Sandovala9b2e0d2020-08-21 00:39:51 -07005219 sent += size;
Filipe Mananad906d492015-10-02 10:47:34 +01005220 }
5221 return 0;
5222}
5223
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -03005224/*
5225 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5226 * found, call send_set_xattr function to emit it.
5227 *
5228 * Return 0 if there isn't a capability, or when the capability was emitted
5229 * successfully, or < 0 if an error occurred.
5230 */
5231static int send_capabilities(struct send_ctx *sctx)
5232{
5233 struct fs_path *fspath = NULL;
5234 struct btrfs_path *path;
5235 struct btrfs_dir_item *di;
5236 struct extent_buffer *leaf;
5237 unsigned long data_ptr;
5238 char *buf = NULL;
5239 int buf_len;
5240 int ret = 0;
5241
5242 path = alloc_path_for_send();
5243 if (!path)
5244 return -ENOMEM;
5245
5246 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5247 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5248 if (!di) {
5249 /* There is no xattr for this inode */
5250 goto out;
5251 } else if (IS_ERR(di)) {
5252 ret = PTR_ERR(di);
5253 goto out;
5254 }
5255
5256 leaf = path->nodes[0];
5257 buf_len = btrfs_dir_data_len(leaf, di);
5258
5259 fspath = fs_path_alloc();
5260 buf = kmalloc(buf_len, GFP_KERNEL);
5261 if (!fspath || !buf) {
5262 ret = -ENOMEM;
5263 goto out;
5264 }
5265
5266 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5267 if (ret < 0)
5268 goto out;
5269
5270 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5271 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5272
5273 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5274 strlen(XATTR_NAME_CAPS), buf, buf_len);
5275out:
5276 kfree(buf);
5277 fs_path_free(fspath);
5278 btrfs_free_path(path);
5279 return ret;
5280}
5281
Filipe Mananad906d492015-10-02 10:47:34 +01005282static int clone_range(struct send_ctx *sctx,
5283 struct clone_root *clone_root,
5284 const u64 disk_byte,
5285 u64 data_offset,
5286 u64 offset,
5287 u64 len)
5288{
5289 struct btrfs_path *path;
5290 struct btrfs_key key;
5291 int ret;
Austin Kim431d3982019-09-03 12:30:19 +09005292 u64 clone_src_i_size = 0;
Filipe Mananad906d492015-10-02 10:47:34 +01005293
Filipe Manana72610b12017-08-10 22:54:51 +01005294 /*
5295 * Prevent cloning from a zero offset with a length matching the sector
5296 * size because in some scenarios this will make the receiver fail.
5297 *
5298 * For example, if in the source filesystem the extent at offset 0
5299 * has a length of sectorsize and it was written using direct IO, then
5300 * it can never be an inline extent (even if compression is enabled).
5301 * Then this extent can be cloned in the original filesystem to a non
5302 * zero file offset, but it may not be possible to clone in the
5303 * destination filesystem because it can be inlined due to compression
5304 * on the destination filesystem (as the receiver's write operations are
5305 * always done using buffered IO). The same happens when the original
5306 * filesystem does not have compression enabled but the destination
5307 * filesystem has.
5308 */
5309 if (clone_root->offset == 0 &&
5310 len == sctx->send_root->fs_info->sectorsize)
5311 return send_extent_data(sctx, offset, len);
5312
Filipe Mananad906d492015-10-02 10:47:34 +01005313 path = alloc_path_for_send();
5314 if (!path)
5315 return -ENOMEM;
5316
5317 /*
Robbie Ko040ee6122019-03-29 18:03:27 +08005318 * There are inodes that have extents that lie behind its i_size. Don't
5319 * accept clones from these extents.
5320 */
5321 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5322 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL);
5323 btrfs_release_path(path);
5324 if (ret < 0)
5325 goto out;
5326
5327 /*
Filipe Mananad906d492015-10-02 10:47:34 +01005328 * We can't send a clone operation for the entire range if we find
5329 * extent items in the respective range in the source file that
5330 * refer to different extents or if we find holes.
5331 * So check for that and do a mix of clone and regular write/copy
5332 * operations if needed.
5333 *
5334 * Example:
5335 *
5336 * mkfs.btrfs -f /dev/sda
5337 * mount /dev/sda /mnt
5338 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5339 * cp --reflink=always /mnt/foo /mnt/bar
5340 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5341 * btrfs subvolume snapshot -r /mnt /mnt/snap
5342 *
5343 * If when we send the snapshot and we are processing file bar (which
5344 * has a higher inode number than foo) we blindly send a clone operation
5345 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5346 * a file bar that matches the content of file foo - iow, doesn't match
5347 * the content from bar in the original filesystem.
5348 */
5349 key.objectid = clone_root->ino;
5350 key.type = BTRFS_EXTENT_DATA_KEY;
5351 key.offset = clone_root->offset;
5352 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5353 if (ret < 0)
5354 goto out;
5355 if (ret > 0 && path->slots[0] > 0) {
5356 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5357 if (key.objectid == clone_root->ino &&
5358 key.type == BTRFS_EXTENT_DATA_KEY)
5359 path->slots[0]--;
5360 }
5361
5362 while (true) {
5363 struct extent_buffer *leaf = path->nodes[0];
5364 int slot = path->slots[0];
5365 struct btrfs_file_extent_item *ei;
5366 u8 type;
5367 u64 ext_len;
5368 u64 clone_len;
Robbie Ko040ee6122019-03-29 18:03:27 +08005369 u64 clone_data_offset;
Filipe Mananad906d492015-10-02 10:47:34 +01005370
5371 if (slot >= btrfs_header_nritems(leaf)) {
5372 ret = btrfs_next_leaf(clone_root->root, path);
5373 if (ret < 0)
5374 goto out;
5375 else if (ret > 0)
5376 break;
5377 continue;
5378 }
5379
5380 btrfs_item_key_to_cpu(leaf, &key, slot);
5381
5382 /*
5383 * We might have an implicit trailing hole (NO_HOLES feature
5384 * enabled). We deal with it after leaving this loop.
5385 */
5386 if (key.objectid != clone_root->ino ||
5387 key.type != BTRFS_EXTENT_DATA_KEY)
5388 break;
5389
5390 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5391 type = btrfs_file_extent_type(leaf, ei);
5392 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005393 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005394 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01005395 } else {
5396 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5397 }
5398
5399 if (key.offset + ext_len <= clone_root->offset)
5400 goto next;
5401
5402 if (key.offset > clone_root->offset) {
5403 /* Implicit hole, NO_HOLES feature enabled. */
5404 u64 hole_len = key.offset - clone_root->offset;
5405
5406 if (hole_len > len)
5407 hole_len = len;
5408 ret = send_extent_data(sctx, offset, hole_len);
5409 if (ret < 0)
5410 goto out;
5411
5412 len -= hole_len;
5413 if (len == 0)
5414 break;
5415 offset += hole_len;
5416 clone_root->offset += hole_len;
5417 data_offset += hole_len;
5418 }
5419
5420 if (key.offset >= clone_root->offset + len)
5421 break;
5422
Robbie Ko040ee6122019-03-29 18:03:27 +08005423 if (key.offset >= clone_src_i_size)
5424 break;
5425
5426 if (key.offset + ext_len > clone_src_i_size)
5427 ext_len = clone_src_i_size - key.offset;
5428
5429 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5430 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5431 clone_root->offset = key.offset;
5432 if (clone_data_offset < data_offset &&
5433 clone_data_offset + ext_len > data_offset) {
5434 u64 extent_offset;
5435
5436 extent_offset = data_offset - clone_data_offset;
5437 ext_len -= extent_offset;
5438 clone_data_offset += extent_offset;
5439 clone_root->offset += extent_offset;
5440 }
5441 }
5442
Filipe Mananad906d492015-10-02 10:47:34 +01005443 clone_len = min_t(u64, ext_len, len);
5444
5445 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
Filipe Manana3c850b42019-05-20 09:57:00 +01005446 clone_data_offset == data_offset) {
5447 const u64 src_end = clone_root->offset + clone_len;
5448 const u64 sectorsize = SZ_64K;
5449
5450 /*
5451 * We can't clone the last block, when its size is not
5452 * sector size aligned, into the middle of a file. If we
5453 * do so, the receiver will get a failure (-EINVAL) when
5454 * trying to clone or will silently corrupt the data in
5455 * the destination file if it's on a kernel without the
5456 * fix introduced by commit ac765f83f1397646
5457 * ("Btrfs: fix data corruption due to cloning of eof
5458 * block).
5459 *
5460 * So issue a clone of the aligned down range plus a
5461 * regular write for the eof block, if we hit that case.
5462 *
5463 * Also, we use the maximum possible sector size, 64K,
5464 * because we don't know what's the sector size of the
5465 * filesystem that receives the stream, so we have to
5466 * assume the largest possible sector size.
5467 */
5468 if (src_end == clone_src_i_size &&
5469 !IS_ALIGNED(src_end, sectorsize) &&
5470 offset + clone_len < sctx->cur_inode_size) {
5471 u64 slen;
5472
5473 slen = ALIGN_DOWN(src_end - clone_root->offset,
5474 sectorsize);
5475 if (slen > 0) {
5476 ret = send_clone(sctx, offset, slen,
5477 clone_root);
5478 if (ret < 0)
5479 goto out;
5480 }
5481 ret = send_extent_data(sctx, offset + slen,
5482 clone_len - slen);
5483 } else {
5484 ret = send_clone(sctx, offset, clone_len,
5485 clone_root);
5486 }
5487 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005488 ret = send_extent_data(sctx, offset, clone_len);
Filipe Manana3c850b42019-05-20 09:57:00 +01005489 }
Filipe Mananad906d492015-10-02 10:47:34 +01005490
5491 if (ret < 0)
5492 goto out;
5493
5494 len -= clone_len;
5495 if (len == 0)
5496 break;
5497 offset += clone_len;
5498 clone_root->offset += clone_len;
Filipe Manana518837e2021-01-11 11:41:42 +00005499
5500 /*
5501 * If we are cloning from the file we are currently processing,
5502 * and using the send root as the clone root, we must stop once
5503 * the current clone offset reaches the current eof of the file
5504 * at the receiver, otherwise we would issue an invalid clone
5505 * operation (source range going beyond eof) and cause the
5506 * receiver to fail. So if we reach the current eof, bail out
5507 * and fallback to a regular write.
5508 */
5509 if (clone_root->root == sctx->send_root &&
5510 clone_root->ino == sctx->cur_ino &&
5511 clone_root->offset >= sctx->cur_inode_next_write_offset)
5512 break;
5513
Filipe Mananad906d492015-10-02 10:47:34 +01005514 data_offset += clone_len;
5515next:
5516 path->slots[0]++;
5517 }
5518
5519 if (len > 0)
5520 ret = send_extent_data(sctx, offset, len);
5521 else
5522 ret = 0;
5523out:
5524 btrfs_free_path(path);
5525 return ret;
5526}
5527
Alexander Block31db9f72012-07-25 23:19:24 +02005528static int send_write_or_clone(struct send_ctx *sctx,
5529 struct btrfs_path *path,
5530 struct btrfs_key *key,
5531 struct clone_root *clone_root)
5532{
5533 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005534 u64 offset = key->offset;
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005535 u64 end;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005536 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005537
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005538 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
5539 if (offset >= end)
5540 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005541
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005542 if (clone_root && IS_ALIGNED(end, bs)) {
5543 struct btrfs_file_extent_item *ei;
Filipe Mananad906d492015-10-02 10:47:34 +01005544 u64 disk_byte;
5545 u64 data_offset;
5546
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005547 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5548 struct btrfs_file_extent_item);
Filipe Mananad906d492015-10-02 10:47:34 +01005549 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5550 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5551 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005552 offset, end - offset);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005553 } else {
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005554 ret = send_extent_data(sctx, offset, end - offset);
Alexander Block31db9f72012-07-25 23:19:24 +02005555 }
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005556 sctx->cur_inode_next_write_offset = end;
Alexander Block31db9f72012-07-25 23:19:24 +02005557 return ret;
5558}
5559
5560static int is_extent_unchanged(struct send_ctx *sctx,
5561 struct btrfs_path *left_path,
5562 struct btrfs_key *ekey)
5563{
5564 int ret = 0;
5565 struct btrfs_key key;
5566 struct btrfs_path *path = NULL;
5567 struct extent_buffer *eb;
5568 int slot;
5569 struct btrfs_key found_key;
5570 struct btrfs_file_extent_item *ei;
5571 u64 left_disknr;
5572 u64 right_disknr;
5573 u64 left_offset;
5574 u64 right_offset;
5575 u64 left_offset_fixed;
5576 u64 left_len;
5577 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005578 u64 left_gen;
5579 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005580 u8 left_type;
5581 u8 right_type;
5582
5583 path = alloc_path_for_send();
5584 if (!path)
5585 return -ENOMEM;
5586
5587 eb = left_path->nodes[0];
5588 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005589 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5590 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005591
5592 if (left_type != BTRFS_FILE_EXTENT_REG) {
5593 ret = 0;
5594 goto out;
5595 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005596 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5597 left_len = btrfs_file_extent_num_bytes(eb, ei);
5598 left_offset = btrfs_file_extent_offset(eb, ei);
5599 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005600
5601 /*
5602 * Following comments will refer to these graphics. L is the left
5603 * extents which we are checking at the moment. 1-8 are the right
5604 * extents that we iterate.
5605 *
5606 * |-----L-----|
5607 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5608 *
5609 * |-----L-----|
5610 * |--1--|-2b-|...(same as above)
5611 *
5612 * Alternative situation. Happens on files where extents got split.
5613 * |-----L-----|
5614 * |-----------7-----------|-6-|
5615 *
5616 * Alternative situation. Happens on files which got larger.
5617 * |-----L-----|
5618 * |-8-|
5619 * Nothing follows after 8.
5620 */
5621
5622 key.objectid = ekey->objectid;
5623 key.type = BTRFS_EXTENT_DATA_KEY;
5624 key.offset = ekey->offset;
5625 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5626 if (ret < 0)
5627 goto out;
5628 if (ret) {
5629 ret = 0;
5630 goto out;
5631 }
5632
5633 /*
5634 * Handle special case where the right side has no extents at all.
5635 */
5636 eb = path->nodes[0];
5637 slot = path->slots[0];
5638 btrfs_item_key_to_cpu(eb, &found_key, slot);
5639 if (found_key.objectid != key.objectid ||
5640 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005641 /* If we're a hole then just pretend nothing changed */
5642 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005643 goto out;
5644 }
5645
5646 /*
5647 * We're now on 2a, 2b or 7.
5648 */
5649 key = found_key;
5650 while (key.offset < ekey->offset + left_len) {
5651 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5652 right_type = btrfs_file_extent_type(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005653 if (right_type != BTRFS_FILE_EXTENT_REG &&
5654 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005655 ret = 0;
5656 goto out;
5657 }
5658
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005659 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005660 right_len = btrfs_file_extent_ram_bytes(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005661 right_len = PAGE_ALIGN(right_len);
5662 } else {
5663 right_len = btrfs_file_extent_num_bytes(eb, ei);
5664 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005665
Alexander Block31db9f72012-07-25 23:19:24 +02005666 /*
5667 * Are we at extent 8? If yes, we know the extent is changed.
5668 * This may only happen on the first iteration.
5669 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005670 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005671 /* If we're a hole just pretend nothing changed */
5672 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005673 goto out;
5674 }
5675
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005676 /*
5677 * We just wanted to see if when we have an inline extent, what
5678 * follows it is a regular extent (wanted to check the above
5679 * condition for inline extents too). This should normally not
5680 * happen but it's possible for example when we have an inline
5681 * compressed extent representing data with a size matching
5682 * the page size (currently the same as sector size).
5683 */
5684 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5685 ret = 0;
5686 goto out;
5687 }
5688
Filipe Manana24e52b12017-07-06 15:31:46 +01005689 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5690 right_offset = btrfs_file_extent_offset(eb, ei);
5691 right_gen = btrfs_file_extent_generation(eb, ei);
5692
Alexander Block31db9f72012-07-25 23:19:24 +02005693 left_offset_fixed = left_offset;
5694 if (key.offset < ekey->offset) {
5695 /* Fix the right offset for 2a and 7. */
5696 right_offset += ekey->offset - key.offset;
5697 } else {
5698 /* Fix the left offset for all behind 2a and 2b */
5699 left_offset_fixed += key.offset - ekey->offset;
5700 }
5701
5702 /*
5703 * Check if we have the same extent.
5704 */
Alexander Block39540962012-08-01 12:46:05 +02005705 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005706 left_offset_fixed != right_offset ||
5707 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005708 ret = 0;
5709 goto out;
5710 }
5711
5712 /*
5713 * Go to the next extent.
5714 */
5715 ret = btrfs_next_item(sctx->parent_root, path);
5716 if (ret < 0)
5717 goto out;
5718 if (!ret) {
5719 eb = path->nodes[0];
5720 slot = path->slots[0];
5721 btrfs_item_key_to_cpu(eb, &found_key, slot);
5722 }
5723 if (ret || found_key.objectid != key.objectid ||
5724 found_key.type != key.type) {
5725 key.offset += right_len;
5726 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005727 }
5728 if (found_key.offset != key.offset + right_len) {
5729 ret = 0;
5730 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005731 }
5732 key = found_key;
5733 }
5734
5735 /*
5736 * We're now behind the left extent (treat as unchanged) or at the end
5737 * of the right side (treat as changed).
5738 */
5739 if (key.offset >= ekey->offset + left_len)
5740 ret = 1;
5741 else
5742 ret = 0;
5743
5744
5745out:
5746 btrfs_free_path(path);
5747 return ret;
5748}
5749
Josef Bacik16e75492013-10-22 12:18:51 -04005750static int get_last_extent(struct send_ctx *sctx, u64 offset)
5751{
5752 struct btrfs_path *path;
5753 struct btrfs_root *root = sctx->send_root;
Josef Bacik16e75492013-10-22 12:18:51 -04005754 struct btrfs_key key;
Josef Bacik16e75492013-10-22 12:18:51 -04005755 int ret;
5756
5757 path = alloc_path_for_send();
5758 if (!path)
5759 return -ENOMEM;
5760
5761 sctx->cur_inode_last_extent = 0;
5762
5763 key.objectid = sctx->cur_ino;
5764 key.type = BTRFS_EXTENT_DATA_KEY;
5765 key.offset = offset;
5766 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5767 if (ret < 0)
5768 goto out;
5769 ret = 0;
5770 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5771 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5772 goto out;
5773
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00005774 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
Josef Bacik16e75492013-10-22 12:18:51 -04005775out:
5776 btrfs_free_path(path);
5777 return ret;
5778}
5779
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005780static int range_is_hole_in_parent(struct send_ctx *sctx,
5781 const u64 start,
5782 const u64 end)
5783{
5784 struct btrfs_path *path;
5785 struct btrfs_key key;
5786 struct btrfs_root *root = sctx->parent_root;
5787 u64 search_start = start;
5788 int ret;
5789
5790 path = alloc_path_for_send();
5791 if (!path)
5792 return -ENOMEM;
5793
5794 key.objectid = sctx->cur_ino;
5795 key.type = BTRFS_EXTENT_DATA_KEY;
5796 key.offset = search_start;
5797 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5798 if (ret < 0)
5799 goto out;
5800 if (ret > 0 && path->slots[0] > 0)
5801 path->slots[0]--;
5802
5803 while (search_start < end) {
5804 struct extent_buffer *leaf = path->nodes[0];
5805 int slot = path->slots[0];
5806 struct btrfs_file_extent_item *fi;
5807 u64 extent_end;
5808
5809 if (slot >= btrfs_header_nritems(leaf)) {
5810 ret = btrfs_next_leaf(root, path);
5811 if (ret < 0)
5812 goto out;
5813 else if (ret > 0)
5814 break;
5815 continue;
5816 }
5817
5818 btrfs_item_key_to_cpu(leaf, &key, slot);
5819 if (key.objectid < sctx->cur_ino ||
5820 key.type < BTRFS_EXTENT_DATA_KEY)
5821 goto next;
5822 if (key.objectid > sctx->cur_ino ||
5823 key.type > BTRFS_EXTENT_DATA_KEY ||
5824 key.offset >= end)
5825 break;
5826
5827 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00005828 extent_end = btrfs_file_extent_end(path);
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005829 if (extent_end <= start)
5830 goto next;
5831 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5832 search_start = extent_end;
5833 goto next;
5834 }
5835 ret = 0;
5836 goto out;
5837next:
5838 path->slots[0]++;
5839 }
5840 ret = 1;
5841out:
5842 btrfs_free_path(path);
5843 return ret;
5844}
5845
Josef Bacik16e75492013-10-22 12:18:51 -04005846static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5847 struct btrfs_key *key)
5848{
Josef Bacik16e75492013-10-22 12:18:51 -04005849 int ret = 0;
5850
5851 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5852 return 0;
5853
5854 if (sctx->cur_inode_last_extent == (u64)-1) {
5855 ret = get_last_extent(sctx, key->offset - 1);
5856 if (ret)
5857 return ret;
5858 }
5859
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005860 if (path->slots[0] == 0 &&
5861 sctx->cur_inode_last_extent < key->offset) {
5862 /*
5863 * We might have skipped entire leafs that contained only
5864 * file extent items for our current inode. These leafs have
5865 * a generation number smaller (older) than the one in the
5866 * current leaf and the leaf our last extent came from, and
5867 * are located between these 2 leafs.
5868 */
5869 ret = get_last_extent(sctx, key->offset - 1);
5870 if (ret)
5871 return ret;
5872 }
5873
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005874 if (sctx->cur_inode_last_extent < key->offset) {
5875 ret = range_is_hole_in_parent(sctx,
5876 sctx->cur_inode_last_extent,
5877 key->offset);
5878 if (ret < 0)
5879 return ret;
5880 else if (ret == 0)
5881 ret = send_hole(sctx, key->offset);
5882 else
5883 ret = 0;
5884 }
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00005885 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
Josef Bacik16e75492013-10-22 12:18:51 -04005886 return ret;
5887}
5888
Alexander Block31db9f72012-07-25 23:19:24 +02005889static int process_extent(struct send_ctx *sctx,
5890 struct btrfs_path *path,
5891 struct btrfs_key *key)
5892{
Alexander Block31db9f72012-07-25 23:19:24 +02005893 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005894 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005895
5896 if (S_ISLNK(sctx->cur_inode_mode))
5897 return 0;
5898
5899 if (sctx->parent_root && !sctx->cur_inode_new) {
5900 ret = is_extent_unchanged(sctx, path, key);
5901 if (ret < 0)
5902 goto out;
5903 if (ret) {
5904 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005905 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005906 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005907 } else {
5908 struct btrfs_file_extent_item *ei;
5909 u8 type;
5910
5911 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5912 struct btrfs_file_extent_item);
5913 type = btrfs_file_extent_type(path->nodes[0], ei);
5914 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5915 type == BTRFS_FILE_EXTENT_REG) {
5916 /*
5917 * The send spec does not have a prealloc command yet,
5918 * so just leave a hole for prealloc'ed extents until
5919 * we have enough commands queued up to justify rev'ing
5920 * the send spec.
5921 */
5922 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5923 ret = 0;
5924 goto out;
5925 }
5926
5927 /* Have a hole, just skip it. */
5928 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5929 ret = 0;
5930 goto out;
5931 }
5932 }
Alexander Block31db9f72012-07-25 23:19:24 +02005933 }
5934
5935 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5936 sctx->cur_inode_size, &found_clone);
5937 if (ret != -ENOENT && ret < 0)
5938 goto out;
5939
5940 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005941 if (ret)
5942 goto out;
5943out_hole:
5944 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005945out:
5946 return ret;
5947}
5948
5949static int process_all_extents(struct send_ctx *sctx)
5950{
5951 int ret;
5952 struct btrfs_root *root;
5953 struct btrfs_path *path;
5954 struct btrfs_key key;
5955 struct btrfs_key found_key;
5956 struct extent_buffer *eb;
5957 int slot;
5958
5959 root = sctx->send_root;
5960 path = alloc_path_for_send();
5961 if (!path)
5962 return -ENOMEM;
5963
5964 key.objectid = sctx->cmp_key->objectid;
5965 key.type = BTRFS_EXTENT_DATA_KEY;
5966 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005967 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5968 if (ret < 0)
5969 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005970
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005971 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005972 eb = path->nodes[0];
5973 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005974
5975 if (slot >= btrfs_header_nritems(eb)) {
5976 ret = btrfs_next_leaf(root, path);
5977 if (ret < 0) {
5978 goto out;
5979 } else if (ret > 0) {
5980 ret = 0;
5981 break;
5982 }
5983 continue;
5984 }
5985
Alexander Block31db9f72012-07-25 23:19:24 +02005986 btrfs_item_key_to_cpu(eb, &found_key, slot);
5987
5988 if (found_key.objectid != key.objectid ||
5989 found_key.type != key.type) {
5990 ret = 0;
5991 goto out;
5992 }
5993
5994 ret = process_extent(sctx, path, &found_key);
5995 if (ret < 0)
5996 goto out;
5997
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005998 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005999 }
6000
6001out:
6002 btrfs_free_path(path);
6003 return ret;
6004}
6005
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006006static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
6007 int *pending_move,
6008 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02006009{
6010 int ret = 0;
6011
6012 if (sctx->cur_ino == 0)
6013 goto out;
6014 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00006015 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02006016 goto out;
6017 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
6018 goto out;
6019
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006020 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02006021 if (ret < 0)
6022 goto out;
6023
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006024 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006025out:
6026 return ret;
6027}
6028
6029static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
6030{
6031 int ret = 0;
6032 u64 left_mode;
6033 u64 left_uid;
6034 u64 left_gid;
6035 u64 right_mode;
6036 u64 right_uid;
6037 u64 right_gid;
6038 int need_chmod = 0;
6039 int need_chown = 0;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006040 int need_truncate = 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006041 int pending_move = 0;
6042 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006043
Filipe Manana46b2f452018-07-24 11:54:04 +01006044 if (sctx->ignore_cur_inode)
6045 return 0;
6046
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006047 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
6048 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02006049 if (ret < 0)
6050 goto out;
6051
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006052 /*
6053 * We have processed the refs and thus need to advance send_progress.
6054 * Now, calls to get_cur_xxx will take the updated refs of the current
6055 * inode into account.
6056 *
6057 * On the other hand, if our current inode is a directory and couldn't
6058 * be moved/renamed because its parent was renamed/moved too and it has
6059 * a higher inode number, we can only move/rename our current inode
6060 * after we moved/renamed its parent. Therefore in this case operate on
6061 * the old path (pre move/rename) of our current inode, and the
6062 * move/rename will be performed later.
6063 */
6064 if (refs_processed && !pending_move)
6065 sctx->send_progress = sctx->cur_ino + 1;
6066
Alexander Block31db9f72012-07-25 23:19:24 +02006067 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6068 goto out;
6069 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6070 goto out;
6071
6072 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02006073 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02006074 if (ret < 0)
6075 goto out;
6076
Alex Lyakase2d044f2012-10-17 13:52:47 +00006077 if (!sctx->parent_root || sctx->cur_inode_new) {
6078 need_chown = 1;
6079 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02006080 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006081 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6082 need_truncate = 0;
Alex Lyakase2d044f2012-10-17 13:52:47 +00006083 } else {
Filipe Mananaffa7c422018-02-06 20:40:40 +00006084 u64 old_size;
6085
Alex Lyakase2d044f2012-10-17 13:52:47 +00006086 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
Filipe Mananaffa7c422018-02-06 20:40:40 +00006087 &old_size, NULL, &right_mode, &right_uid,
Alex Lyakase2d044f2012-10-17 13:52:47 +00006088 &right_gid, NULL);
6089 if (ret < 0)
6090 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006091
Alex Lyakase2d044f2012-10-17 13:52:47 +00006092 if (left_uid != right_uid || left_gid != right_gid)
6093 need_chown = 1;
6094 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6095 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006096 if ((old_size == sctx->cur_inode_size) ||
6097 (sctx->cur_inode_size > old_size &&
6098 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6099 need_truncate = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006100 }
6101
6102 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04006103 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01006104 if (sctx->cur_inode_last_extent == (u64)-1 ||
6105 sctx->cur_inode_last_extent <
6106 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04006107 ret = get_last_extent(sctx, (u64)-1);
6108 if (ret)
6109 goto out;
6110 }
6111 if (sctx->cur_inode_last_extent <
6112 sctx->cur_inode_size) {
6113 ret = send_hole(sctx, sctx->cur_inode_size);
6114 if (ret)
6115 goto out;
6116 }
6117 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00006118 if (need_truncate) {
6119 ret = send_truncate(sctx, sctx->cur_ino,
6120 sctx->cur_inode_gen,
6121 sctx->cur_inode_size);
6122 if (ret < 0)
6123 goto out;
6124 }
Alexander Block31db9f72012-07-25 23:19:24 +02006125 }
6126
6127 if (need_chown) {
6128 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6129 left_uid, left_gid);
6130 if (ret < 0)
6131 goto out;
6132 }
6133 if (need_chmod) {
6134 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6135 left_mode);
6136 if (ret < 0)
6137 goto out;
6138 }
6139
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -03006140 ret = send_capabilities(sctx);
6141 if (ret < 0)
6142 goto out;
6143
Alexander Block31db9f72012-07-25 23:19:24 +02006144 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006145 * If other directory inodes depended on our current directory
6146 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02006147 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006148 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6149 ret = apply_children_dir_moves(sctx);
6150 if (ret)
6151 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00006152 /*
6153 * Need to send that every time, no matter if it actually
6154 * changed between the two trees as we have done changes to
6155 * the inode before. If our inode is a directory and it's
6156 * waiting to be moved/renamed, we will send its utimes when
6157 * it's moved/renamed, therefore we don't need to do it here.
6158 */
6159 sctx->send_progress = sctx->cur_ino + 1;
6160 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6161 if (ret < 0)
6162 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006163 }
6164
Alexander Block31db9f72012-07-25 23:19:24 +02006165out:
6166 return ret;
6167}
6168
Filipe Manana46b2f452018-07-24 11:54:04 +01006169struct parent_paths_ctx {
6170 struct list_head *refs;
6171 struct send_ctx *sctx;
6172};
6173
6174static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6175 void *ctx)
6176{
6177 struct parent_paths_ctx *ppctx = ctx;
6178
6179 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6180 ppctx->refs);
6181}
6182
6183/*
6184 * Issue unlink operations for all paths of the current inode found in the
6185 * parent snapshot.
6186 */
6187static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6188{
6189 LIST_HEAD(deleted_refs);
6190 struct btrfs_path *path;
6191 struct btrfs_key key;
6192 struct parent_paths_ctx ctx;
6193 int ret;
6194
6195 path = alloc_path_for_send();
6196 if (!path)
6197 return -ENOMEM;
6198
6199 key.objectid = sctx->cur_ino;
6200 key.type = BTRFS_INODE_REF_KEY;
6201 key.offset = 0;
6202 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
6203 if (ret < 0)
6204 goto out;
6205
6206 ctx.refs = &deleted_refs;
6207 ctx.sctx = sctx;
6208
6209 while (true) {
6210 struct extent_buffer *eb = path->nodes[0];
6211 int slot = path->slots[0];
6212
6213 if (slot >= btrfs_header_nritems(eb)) {
6214 ret = btrfs_next_leaf(sctx->parent_root, path);
6215 if (ret < 0)
6216 goto out;
6217 else if (ret > 0)
6218 break;
6219 continue;
6220 }
6221
6222 btrfs_item_key_to_cpu(eb, &key, slot);
6223 if (key.objectid != sctx->cur_ino)
6224 break;
6225 if (key.type != BTRFS_INODE_REF_KEY &&
6226 key.type != BTRFS_INODE_EXTREF_KEY)
6227 break;
6228
6229 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
6230 record_parent_ref, &ctx);
6231 if (ret < 0)
6232 goto out;
6233
6234 path->slots[0]++;
6235 }
6236
6237 while (!list_empty(&deleted_refs)) {
6238 struct recorded_ref *ref;
6239
6240 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6241 ret = send_unlink(sctx, ref->full_path);
6242 if (ret < 0)
6243 goto out;
6244 fs_path_free(ref->full_path);
6245 list_del(&ref->list);
6246 kfree(ref);
6247 }
6248 ret = 0;
6249out:
6250 btrfs_free_path(path);
6251 if (ret)
6252 __free_recorded_refs(&deleted_refs);
6253 return ret;
6254}
6255
Alexander Block31db9f72012-07-25 23:19:24 +02006256static int changed_inode(struct send_ctx *sctx,
6257 enum btrfs_compare_tree_result result)
6258{
6259 int ret = 0;
6260 struct btrfs_key *key = sctx->cmp_key;
6261 struct btrfs_inode_item *left_ii = NULL;
6262 struct btrfs_inode_item *right_ii = NULL;
6263 u64 left_gen = 0;
6264 u64 right_gen = 0;
6265
Alexander Block31db9f72012-07-25 23:19:24 +02006266 sctx->cur_ino = key->objectid;
6267 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006268 sctx->cur_inode_last_extent = (u64)-1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006269 sctx->cur_inode_next_write_offset = 0;
Filipe Manana46b2f452018-07-24 11:54:04 +01006270 sctx->ignore_cur_inode = false;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006271
6272 /*
6273 * Set send_progress to current inode. This will tell all get_cur_xxx
6274 * functions that the current inode's refs are not updated yet. Later,
6275 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6276 */
Alexander Block31db9f72012-07-25 23:19:24 +02006277 sctx->send_progress = sctx->cur_ino;
6278
6279 if (result == BTRFS_COMPARE_TREE_NEW ||
6280 result == BTRFS_COMPARE_TREE_CHANGED) {
6281 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6282 sctx->left_path->slots[0],
6283 struct btrfs_inode_item);
6284 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6285 left_ii);
6286 } else {
6287 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6288 sctx->right_path->slots[0],
6289 struct btrfs_inode_item);
6290 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6291 right_ii);
6292 }
6293 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6294 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6295 sctx->right_path->slots[0],
6296 struct btrfs_inode_item);
6297
6298 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6299 right_ii);
Alexander Block6d85ed052012-08-01 14:48:59 +02006300
6301 /*
6302 * The cur_ino = root dir case is special here. We can't treat
6303 * the inode as deleted+reused because it would generate a
6304 * stream that tries to delete/mkdir the root dir.
6305 */
6306 if (left_gen != right_gen &&
6307 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02006308 sctx->cur_inode_new_gen = 1;
6309 }
6310
Filipe Manana46b2f452018-07-24 11:54:04 +01006311 /*
6312 * Normally we do not find inodes with a link count of zero (orphans)
6313 * because the most common case is to create a snapshot and use it
6314 * for a send operation. However other less common use cases involve
6315 * using a subvolume and send it after turning it to RO mode just
6316 * after deleting all hard links of a file while holding an open
6317 * file descriptor against it or turning a RO snapshot into RW mode,
6318 * keep an open file descriptor against a file, delete it and then
6319 * turn the snapshot back to RO mode before using it for a send
6320 * operation. So if we find such cases, ignore the inode and all its
6321 * items completely if it's a new inode, or if it's a changed inode
6322 * make sure all its previous paths (from the parent snapshot) are all
6323 * unlinked and all other the inode items are ignored.
6324 */
6325 if (result == BTRFS_COMPARE_TREE_NEW ||
6326 result == BTRFS_COMPARE_TREE_CHANGED) {
6327 u32 nlinks;
6328
6329 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6330 if (nlinks == 0) {
6331 sctx->ignore_cur_inode = true;
6332 if (result == BTRFS_COMPARE_TREE_CHANGED)
6333 ret = btrfs_unlink_all_paths(sctx);
6334 goto out;
6335 }
6336 }
6337
Alexander Block31db9f72012-07-25 23:19:24 +02006338 if (result == BTRFS_COMPARE_TREE_NEW) {
6339 sctx->cur_inode_gen = left_gen;
6340 sctx->cur_inode_new = 1;
6341 sctx->cur_inode_deleted = 0;
6342 sctx->cur_inode_size = btrfs_inode_size(
6343 sctx->left_path->nodes[0], left_ii);
6344 sctx->cur_inode_mode = btrfs_inode_mode(
6345 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006346 sctx->cur_inode_rdev = btrfs_inode_rdev(
6347 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02006348 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02006349 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006350 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6351 sctx->cur_inode_gen = right_gen;
6352 sctx->cur_inode_new = 0;
6353 sctx->cur_inode_deleted = 1;
6354 sctx->cur_inode_size = btrfs_inode_size(
6355 sctx->right_path->nodes[0], right_ii);
6356 sctx->cur_inode_mode = btrfs_inode_mode(
6357 sctx->right_path->nodes[0], right_ii);
6358 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02006359 /*
6360 * We need to do some special handling in case the inode was
6361 * reported as changed with a changed generation number. This
6362 * means that the original inode was deleted and new inode
6363 * reused the same inum. So we have to treat the old inode as
6364 * deleted and the new one as new.
6365 */
Alexander Block31db9f72012-07-25 23:19:24 +02006366 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02006367 /*
6368 * First, process the inode as if it was deleted.
6369 */
Alexander Block31db9f72012-07-25 23:19:24 +02006370 sctx->cur_inode_gen = right_gen;
6371 sctx->cur_inode_new = 0;
6372 sctx->cur_inode_deleted = 1;
6373 sctx->cur_inode_size = btrfs_inode_size(
6374 sctx->right_path->nodes[0], right_ii);
6375 sctx->cur_inode_mode = btrfs_inode_mode(
6376 sctx->right_path->nodes[0], right_ii);
6377 ret = process_all_refs(sctx,
6378 BTRFS_COMPARE_TREE_DELETED);
6379 if (ret < 0)
6380 goto out;
6381
Alexander Block766702e2012-07-28 14:11:31 +02006382 /*
6383 * Now process the inode as if it was new.
6384 */
Alexander Block31db9f72012-07-25 23:19:24 +02006385 sctx->cur_inode_gen = left_gen;
6386 sctx->cur_inode_new = 1;
6387 sctx->cur_inode_deleted = 0;
6388 sctx->cur_inode_size = btrfs_inode_size(
6389 sctx->left_path->nodes[0], left_ii);
6390 sctx->cur_inode_mode = btrfs_inode_mode(
6391 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006392 sctx->cur_inode_rdev = btrfs_inode_rdev(
6393 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02006394 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006395 if (ret < 0)
6396 goto out;
6397
6398 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6399 if (ret < 0)
6400 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006401 /*
6402 * Advance send_progress now as we did not get into
6403 * process_recorded_refs_if_needed in the new_gen case.
6404 */
6405 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02006406
6407 /*
6408 * Now process all extents and xattrs of the inode as if
6409 * they were all new.
6410 */
Alexander Block31db9f72012-07-25 23:19:24 +02006411 ret = process_all_extents(sctx);
6412 if (ret < 0)
6413 goto out;
6414 ret = process_all_new_xattrs(sctx);
6415 if (ret < 0)
6416 goto out;
6417 } else {
6418 sctx->cur_inode_gen = left_gen;
6419 sctx->cur_inode_new = 0;
6420 sctx->cur_inode_new_gen = 0;
6421 sctx->cur_inode_deleted = 0;
6422 sctx->cur_inode_size = btrfs_inode_size(
6423 sctx->left_path->nodes[0], left_ii);
6424 sctx->cur_inode_mode = btrfs_inode_mode(
6425 sctx->left_path->nodes[0], left_ii);
6426 }
6427 }
6428
6429out:
6430 return ret;
6431}
6432
Alexander Block766702e2012-07-28 14:11:31 +02006433/*
6434 * We have to process new refs before deleted refs, but compare_trees gives us
6435 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6436 * first and later process them in process_recorded_refs.
6437 * For the cur_inode_new_gen case, we skip recording completely because
6438 * changed_inode did already initiate processing of refs. The reason for this is
6439 * that in this case, compare_tree actually compares the refs of 2 different
6440 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6441 * refs of the right tree as deleted and all refs of the left tree as new.
6442 */
Alexander Block31db9f72012-07-25 23:19:24 +02006443static int changed_ref(struct send_ctx *sctx,
6444 enum btrfs_compare_tree_result result)
6445{
6446 int ret = 0;
6447
Filipe Manana95155582016-08-01 01:50:37 +01006448 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6449 inconsistent_snapshot_error(sctx, result, "reference");
6450 return -EIO;
6451 }
Alexander Block31db9f72012-07-25 23:19:24 +02006452
6453 if (!sctx->cur_inode_new_gen &&
6454 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6455 if (result == BTRFS_COMPARE_TREE_NEW)
6456 ret = record_new_ref(sctx);
6457 else if (result == BTRFS_COMPARE_TREE_DELETED)
6458 ret = record_deleted_ref(sctx);
6459 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6460 ret = record_changed_ref(sctx);
6461 }
6462
6463 return ret;
6464}
6465
Alexander Block766702e2012-07-28 14:11:31 +02006466/*
6467 * Process new/deleted/changed xattrs. We skip processing in the
6468 * cur_inode_new_gen case because changed_inode did already initiate processing
6469 * of xattrs. The reason is the same as in changed_ref
6470 */
Alexander Block31db9f72012-07-25 23:19:24 +02006471static int changed_xattr(struct send_ctx *sctx,
6472 enum btrfs_compare_tree_result result)
6473{
6474 int ret = 0;
6475
Filipe Manana95155582016-08-01 01:50:37 +01006476 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6477 inconsistent_snapshot_error(sctx, result, "xattr");
6478 return -EIO;
6479 }
Alexander Block31db9f72012-07-25 23:19:24 +02006480
6481 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6482 if (result == BTRFS_COMPARE_TREE_NEW)
6483 ret = process_new_xattr(sctx);
6484 else if (result == BTRFS_COMPARE_TREE_DELETED)
6485 ret = process_deleted_xattr(sctx);
6486 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6487 ret = process_changed_xattr(sctx);
6488 }
6489
6490 return ret;
6491}
6492
Alexander Block766702e2012-07-28 14:11:31 +02006493/*
6494 * Process new/deleted/changed extents. We skip processing in the
6495 * cur_inode_new_gen case because changed_inode did already initiate processing
6496 * of extents. The reason is the same as in changed_ref
6497 */
Alexander Block31db9f72012-07-25 23:19:24 +02006498static int changed_extent(struct send_ctx *sctx,
6499 enum btrfs_compare_tree_result result)
6500{
6501 int ret = 0;
6502
Filipe Mananab4f9a1a2019-07-17 13:23:39 +01006503 /*
6504 * We have found an extent item that changed without the inode item
6505 * having changed. This can happen either after relocation (where the
6506 * disk_bytenr of an extent item is replaced at
6507 * relocation.c:replace_file_extents()) or after deduplication into a
6508 * file in both the parent and send snapshots (where an extent item can
6509 * get modified or replaced with a new one). Note that deduplication
6510 * updates the inode item, but it only changes the iversion (sequence
6511 * field in the inode item) of the inode, so if a file is deduplicated
6512 * the same amount of times in both the parent and send snapshots, its
6513 * iversion becames the same in both snapshots, whence the inode item is
6514 * the same on both snapshots.
6515 */
6516 if (sctx->cur_ino != sctx->cmp_key->objectid)
6517 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006518
6519 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6520 if (result != BTRFS_COMPARE_TREE_DELETED)
6521 ret = process_extent(sctx, sctx->left_path,
6522 sctx->cmp_key);
6523 }
6524
6525 return ret;
6526}
6527
Josef Bacikba5e8f22013-08-16 16:52:55 -04006528static int dir_changed(struct send_ctx *sctx, u64 dir)
6529{
6530 u64 orig_gen, new_gen;
6531 int ret;
6532
6533 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6534 NULL, NULL);
6535 if (ret)
6536 return ret;
6537
6538 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6539 NULL, NULL, NULL);
6540 if (ret)
6541 return ret;
6542
6543 return (orig_gen != new_gen) ? 1 : 0;
6544}
6545
6546static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6547 struct btrfs_key *key)
6548{
6549 struct btrfs_inode_extref *extref;
6550 struct extent_buffer *leaf;
6551 u64 dirid = 0, last_dirid = 0;
6552 unsigned long ptr;
6553 u32 item_size;
6554 u32 cur_offset = 0;
6555 int ref_name_len;
6556 int ret = 0;
6557
6558 /* Easy case, just check this one dirid */
6559 if (key->type == BTRFS_INODE_REF_KEY) {
6560 dirid = key->offset;
6561
6562 ret = dir_changed(sctx, dirid);
6563 goto out;
6564 }
6565
6566 leaf = path->nodes[0];
6567 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6568 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6569 while (cur_offset < item_size) {
6570 extref = (struct btrfs_inode_extref *)(ptr +
6571 cur_offset);
6572 dirid = btrfs_inode_extref_parent(leaf, extref);
6573 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6574 cur_offset += ref_name_len + sizeof(*extref);
6575 if (dirid == last_dirid)
6576 continue;
6577 ret = dir_changed(sctx, dirid);
6578 if (ret)
6579 break;
6580 last_dirid = dirid;
6581 }
6582out:
6583 return ret;
6584}
6585
Alexander Block766702e2012-07-28 14:11:31 +02006586/*
6587 * Updates compare related fields in sctx and simply forwards to the actual
6588 * changed_xxx functions.
6589 */
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006590static int changed_cb(struct btrfs_path *left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02006591 struct btrfs_path *right_path,
6592 struct btrfs_key *key,
6593 enum btrfs_compare_tree_result result,
Roman Anasal88980382021-01-25 20:43:25 +01006594 struct send_ctx *sctx)
Alexander Block31db9f72012-07-25 23:19:24 +02006595{
6596 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006597
Josef Bacikba5e8f22013-08-16 16:52:55 -04006598 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04006599 if (key->type == BTRFS_INODE_REF_KEY ||
6600 key->type == BTRFS_INODE_EXTREF_KEY) {
6601 ret = compare_refs(sctx, left_path, key);
6602 if (!ret)
6603 return 0;
6604 if (ret < 0)
6605 return ret;
6606 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6607 return maybe_send_hole(sctx, left_path, key);
6608 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04006609 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006610 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04006611 result = BTRFS_COMPARE_TREE_CHANGED;
6612 ret = 0;
6613 }
6614
Alexander Block31db9f72012-07-25 23:19:24 +02006615 sctx->left_path = left_path;
6616 sctx->right_path = right_path;
6617 sctx->cmp_key = key;
6618
6619 ret = finish_inode_if_needed(sctx, 0);
6620 if (ret < 0)
6621 goto out;
6622
Alexander Block2981e222012-08-01 14:47:03 +02006623 /* Ignore non-FS objects */
6624 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6625 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6626 goto out;
6627
Filipe Manana46b2f452018-07-24 11:54:04 +01006628 if (key->type == BTRFS_INODE_ITEM_KEY) {
Alexander Block31db9f72012-07-25 23:19:24 +02006629 ret = changed_inode(sctx, result);
Filipe Manana46b2f452018-07-24 11:54:04 +01006630 } else if (!sctx->ignore_cur_inode) {
6631 if (key->type == BTRFS_INODE_REF_KEY ||
6632 key->type == BTRFS_INODE_EXTREF_KEY)
6633 ret = changed_ref(sctx, result);
6634 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6635 ret = changed_xattr(sctx, result);
6636 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6637 ret = changed_extent(sctx, result);
6638 }
Alexander Block31db9f72012-07-25 23:19:24 +02006639
6640out:
6641 return ret;
6642}
6643
6644static int full_send_tree(struct send_ctx *sctx)
6645{
6646 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006647 struct btrfs_root *send_root = sctx->send_root;
6648 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006649 struct btrfs_path *path;
6650 struct extent_buffer *eb;
6651 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006652
6653 path = alloc_path_for_send();
6654 if (!path)
6655 return -ENOMEM;
6656
Alexander Block31db9f72012-07-25 23:19:24 +02006657 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6658 key.type = BTRFS_INODE_ITEM_KEY;
6659 key.offset = 0;
6660
Alexander Block31db9f72012-07-25 23:19:24 +02006661 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6662 if (ret < 0)
6663 goto out;
6664 if (ret)
6665 goto out_finish;
6666
6667 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006668 eb = path->nodes[0];
6669 slot = path->slots[0];
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006670 btrfs_item_key_to_cpu(eb, &key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02006671
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006672 ret = changed_cb(path, NULL, &key,
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006673 BTRFS_COMPARE_TREE_NEW, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006674 if (ret < 0)
6675 goto out;
6676
Alexander Block31db9f72012-07-25 23:19:24 +02006677 ret = btrfs_next_item(send_root, path);
6678 if (ret < 0)
6679 goto out;
6680 if (ret) {
6681 ret = 0;
6682 break;
6683 }
6684 }
6685
6686out_finish:
6687 ret = finish_inode_if_needed(sctx, 1);
6688
6689out:
6690 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006691 return ret;
6692}
6693
David Sterba18d0f5c2019-08-21 19:12:59 +02006694static int tree_move_down(struct btrfs_path *path, int *level)
6695{
6696 struct extent_buffer *eb;
6697
6698 BUG_ON(*level == 0);
6699 eb = btrfs_read_node_slot(path->nodes[*level], path->slots[*level]);
6700 if (IS_ERR(eb))
6701 return PTR_ERR(eb);
6702
6703 path->nodes[*level - 1] = eb;
6704 path->slots[*level - 1] = 0;
6705 (*level)--;
6706 return 0;
6707}
6708
6709static int tree_move_next_or_upnext(struct btrfs_path *path,
6710 int *level, int root_level)
6711{
6712 int ret = 0;
6713 int nritems;
6714 nritems = btrfs_header_nritems(path->nodes[*level]);
6715
6716 path->slots[*level]++;
6717
6718 while (path->slots[*level] >= nritems) {
6719 if (*level == root_level)
6720 return -1;
6721
6722 /* move upnext */
6723 path->slots[*level] = 0;
6724 free_extent_buffer(path->nodes[*level]);
6725 path->nodes[*level] = NULL;
6726 (*level)++;
6727 path->slots[*level]++;
6728
6729 nritems = btrfs_header_nritems(path->nodes[*level]);
6730 ret = 1;
6731 }
6732 return ret;
6733}
6734
6735/*
6736 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
6737 * or down.
6738 */
6739static int tree_advance(struct btrfs_path *path,
6740 int *level, int root_level,
6741 int allow_down,
6742 struct btrfs_key *key)
6743{
6744 int ret;
6745
6746 if (*level == 0 || !allow_down) {
6747 ret = tree_move_next_or_upnext(path, level, root_level);
6748 } else {
6749 ret = tree_move_down(path, level);
6750 }
6751 if (ret >= 0) {
6752 if (*level == 0)
6753 btrfs_item_key_to_cpu(path->nodes[*level], key,
6754 path->slots[*level]);
6755 else
6756 btrfs_node_key_to_cpu(path->nodes[*level], key,
6757 path->slots[*level]);
6758 }
6759 return ret;
6760}
6761
6762static int tree_compare_item(struct btrfs_path *left_path,
6763 struct btrfs_path *right_path,
6764 char *tmp_buf)
6765{
6766 int cmp;
6767 int len1, len2;
6768 unsigned long off1, off2;
6769
6770 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
6771 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
6772 if (len1 != len2)
6773 return 1;
6774
6775 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
6776 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
6777 right_path->slots[0]);
6778
6779 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
6780
6781 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
6782 if (cmp)
6783 return 1;
6784 return 0;
6785}
6786
6787/*
6788 * This function compares two trees and calls the provided callback for
6789 * every changed/new/deleted item it finds.
6790 * If shared tree blocks are encountered, whole subtrees are skipped, making
6791 * the compare pretty fast on snapshotted subvolumes.
6792 *
6793 * This currently works on commit roots only. As commit roots are read only,
6794 * we don't do any locking. The commit roots are protected with transactions.
6795 * Transactions are ended and rejoined when a commit is tried in between.
6796 *
6797 * This function checks for modifications done to the trees while comparing.
6798 * If it detects a change, it aborts immediately.
6799 */
6800static int btrfs_compare_trees(struct btrfs_root *left_root,
Roman Anasal88980382021-01-25 20:43:25 +01006801 struct btrfs_root *right_root, struct send_ctx *sctx)
David Sterba18d0f5c2019-08-21 19:12:59 +02006802{
6803 struct btrfs_fs_info *fs_info = left_root->fs_info;
6804 int ret;
6805 int cmp;
6806 struct btrfs_path *left_path = NULL;
6807 struct btrfs_path *right_path = NULL;
6808 struct btrfs_key left_key;
6809 struct btrfs_key right_key;
6810 char *tmp_buf = NULL;
6811 int left_root_level;
6812 int right_root_level;
6813 int left_level;
6814 int right_level;
6815 int left_end_reached;
6816 int right_end_reached;
6817 int advance_left;
6818 int advance_right;
6819 u64 left_blockptr;
6820 u64 right_blockptr;
6821 u64 left_gen;
6822 u64 right_gen;
6823
6824 left_path = btrfs_alloc_path();
6825 if (!left_path) {
6826 ret = -ENOMEM;
6827 goto out;
6828 }
6829 right_path = btrfs_alloc_path();
6830 if (!right_path) {
6831 ret = -ENOMEM;
6832 goto out;
6833 }
6834
6835 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
6836 if (!tmp_buf) {
6837 ret = -ENOMEM;
6838 goto out;
6839 }
6840
6841 left_path->search_commit_root = 1;
6842 left_path->skip_locking = 1;
6843 right_path->search_commit_root = 1;
6844 right_path->skip_locking = 1;
6845
6846 /*
6847 * Strategy: Go to the first items of both trees. Then do
6848 *
6849 * If both trees are at level 0
6850 * Compare keys of current items
6851 * If left < right treat left item as new, advance left tree
6852 * and repeat
6853 * If left > right treat right item as deleted, advance right tree
6854 * and repeat
6855 * If left == right do deep compare of items, treat as changed if
6856 * needed, advance both trees and repeat
6857 * If both trees are at the same level but not at level 0
6858 * Compare keys of current nodes/leafs
6859 * If left < right advance left tree and repeat
6860 * If left > right advance right tree and repeat
6861 * If left == right compare blockptrs of the next nodes/leafs
6862 * If they match advance both trees but stay at the same level
6863 * and repeat
6864 * If they don't match advance both trees while allowing to go
6865 * deeper and repeat
6866 * If tree levels are different
6867 * Advance the tree that needs it and repeat
6868 *
6869 * Advancing a tree means:
6870 * If we are at level 0, try to go to the next slot. If that's not
6871 * possible, go one level up and repeat. Stop when we found a level
6872 * where we could go to the next slot. We may at this point be on a
6873 * node or a leaf.
6874 *
6875 * If we are not at level 0 and not on shared tree blocks, go one
6876 * level deeper.
6877 *
6878 * If we are not at level 0 and on shared tree blocks, go one slot to
6879 * the right if possible or go up and right.
6880 */
6881
6882 down_read(&fs_info->commit_root_sem);
6883 left_level = btrfs_header_level(left_root->commit_root);
6884 left_root_level = left_level;
6885 left_path->nodes[left_level] =
6886 btrfs_clone_extent_buffer(left_root->commit_root);
6887 if (!left_path->nodes[left_level]) {
6888 up_read(&fs_info->commit_root_sem);
6889 ret = -ENOMEM;
6890 goto out;
6891 }
6892
6893 right_level = btrfs_header_level(right_root->commit_root);
6894 right_root_level = right_level;
6895 right_path->nodes[right_level] =
6896 btrfs_clone_extent_buffer(right_root->commit_root);
6897 if (!right_path->nodes[right_level]) {
6898 up_read(&fs_info->commit_root_sem);
6899 ret = -ENOMEM;
6900 goto out;
6901 }
6902 up_read(&fs_info->commit_root_sem);
6903
6904 if (left_level == 0)
6905 btrfs_item_key_to_cpu(left_path->nodes[left_level],
6906 &left_key, left_path->slots[left_level]);
6907 else
6908 btrfs_node_key_to_cpu(left_path->nodes[left_level],
6909 &left_key, left_path->slots[left_level]);
6910 if (right_level == 0)
6911 btrfs_item_key_to_cpu(right_path->nodes[right_level],
6912 &right_key, right_path->slots[right_level]);
6913 else
6914 btrfs_node_key_to_cpu(right_path->nodes[right_level],
6915 &right_key, right_path->slots[right_level]);
6916
6917 left_end_reached = right_end_reached = 0;
6918 advance_left = advance_right = 0;
6919
6920 while (1) {
Nikolay Borisov6af112b2019-09-04 19:33:58 +03006921 cond_resched();
David Sterba18d0f5c2019-08-21 19:12:59 +02006922 if (advance_left && !left_end_reached) {
6923 ret = tree_advance(left_path, &left_level,
6924 left_root_level,
6925 advance_left != ADVANCE_ONLY_NEXT,
6926 &left_key);
6927 if (ret == -1)
6928 left_end_reached = ADVANCE;
6929 else if (ret < 0)
6930 goto out;
6931 advance_left = 0;
6932 }
6933 if (advance_right && !right_end_reached) {
6934 ret = tree_advance(right_path, &right_level,
6935 right_root_level,
6936 advance_right != ADVANCE_ONLY_NEXT,
6937 &right_key);
6938 if (ret == -1)
6939 right_end_reached = ADVANCE;
6940 else if (ret < 0)
6941 goto out;
6942 advance_right = 0;
6943 }
6944
6945 if (left_end_reached && right_end_reached) {
6946 ret = 0;
6947 goto out;
6948 } else if (left_end_reached) {
6949 if (right_level == 0) {
6950 ret = changed_cb(left_path, right_path,
6951 &right_key,
6952 BTRFS_COMPARE_TREE_DELETED,
Roman Anasal88980382021-01-25 20:43:25 +01006953 sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02006954 if (ret < 0)
6955 goto out;
6956 }
6957 advance_right = ADVANCE;
6958 continue;
6959 } else if (right_end_reached) {
6960 if (left_level == 0) {
6961 ret = changed_cb(left_path, right_path,
6962 &left_key,
6963 BTRFS_COMPARE_TREE_NEW,
Roman Anasal88980382021-01-25 20:43:25 +01006964 sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02006965 if (ret < 0)
6966 goto out;
6967 }
6968 advance_left = ADVANCE;
6969 continue;
6970 }
6971
6972 if (left_level == 0 && right_level == 0) {
6973 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
6974 if (cmp < 0) {
6975 ret = changed_cb(left_path, right_path,
6976 &left_key,
6977 BTRFS_COMPARE_TREE_NEW,
Roman Anasal88980382021-01-25 20:43:25 +01006978 sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02006979 if (ret < 0)
6980 goto out;
6981 advance_left = ADVANCE;
6982 } else if (cmp > 0) {
6983 ret = changed_cb(left_path, right_path,
6984 &right_key,
6985 BTRFS_COMPARE_TREE_DELETED,
Roman Anasal88980382021-01-25 20:43:25 +01006986 sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02006987 if (ret < 0)
6988 goto out;
6989 advance_right = ADVANCE;
6990 } else {
6991 enum btrfs_compare_tree_result result;
6992
6993 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
6994 ret = tree_compare_item(left_path, right_path,
6995 tmp_buf);
6996 if (ret)
6997 result = BTRFS_COMPARE_TREE_CHANGED;
6998 else
6999 result = BTRFS_COMPARE_TREE_SAME;
7000 ret = changed_cb(left_path, right_path,
Roman Anasal88980382021-01-25 20:43:25 +01007001 &left_key, result, sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02007002 if (ret < 0)
7003 goto out;
7004 advance_left = ADVANCE;
7005 advance_right = ADVANCE;
7006 }
7007 } else if (left_level == right_level) {
7008 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7009 if (cmp < 0) {
7010 advance_left = ADVANCE;
7011 } else if (cmp > 0) {
7012 advance_right = ADVANCE;
7013 } else {
7014 left_blockptr = btrfs_node_blockptr(
7015 left_path->nodes[left_level],
7016 left_path->slots[left_level]);
7017 right_blockptr = btrfs_node_blockptr(
7018 right_path->nodes[right_level],
7019 right_path->slots[right_level]);
7020 left_gen = btrfs_node_ptr_generation(
7021 left_path->nodes[left_level],
7022 left_path->slots[left_level]);
7023 right_gen = btrfs_node_ptr_generation(
7024 right_path->nodes[right_level],
7025 right_path->slots[right_level]);
7026 if (left_blockptr == right_blockptr &&
7027 left_gen == right_gen) {
7028 /*
7029 * As we're on a shared block, don't
7030 * allow to go deeper.
7031 */
7032 advance_left = ADVANCE_ONLY_NEXT;
7033 advance_right = ADVANCE_ONLY_NEXT;
7034 } else {
7035 advance_left = ADVANCE;
7036 advance_right = ADVANCE;
7037 }
7038 }
7039 } else if (left_level < right_level) {
7040 advance_right = ADVANCE;
7041 } else {
7042 advance_left = ADVANCE;
7043 }
7044 }
7045
7046out:
7047 btrfs_free_path(left_path);
7048 btrfs_free_path(right_path);
7049 kvfree(tmp_buf);
7050 return ret;
7051}
7052
Alexander Block31db9f72012-07-25 23:19:24 +02007053static int send_subvol(struct send_ctx *sctx)
7054{
7055 int ret;
7056
Stefan Behrensc2c71322013-04-10 17:10:52 +00007057 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
7058 ret = send_header(sctx);
7059 if (ret < 0)
7060 goto out;
7061 }
Alexander Block31db9f72012-07-25 23:19:24 +02007062
7063 ret = send_subvol_begin(sctx);
7064 if (ret < 0)
7065 goto out;
7066
7067 if (sctx->parent_root) {
David Sterba1b51d6f2020-08-17 12:16:57 +02007068 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02007069 if (ret < 0)
7070 goto out;
7071 ret = finish_inode_if_needed(sctx, 1);
7072 if (ret < 0)
7073 goto out;
7074 } else {
7075 ret = full_send_tree(sctx);
7076 if (ret < 0)
7077 goto out;
7078 }
7079
7080out:
Alexander Block31db9f72012-07-25 23:19:24 +02007081 free_recorded_refs(sctx);
7082 return ret;
7083}
7084
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007085/*
7086 * If orphan cleanup did remove any orphans from a root, it means the tree
7087 * was modified and therefore the commit root is not the same as the current
7088 * root anymore. This is a problem, because send uses the commit root and
7089 * therefore can see inode items that don't exist in the current root anymore,
7090 * and for example make calls to btrfs_iget, which will do tree lookups based
7091 * on the current root and not on the commit root. Those lookups will fail,
7092 * returning a -ESTALE error, and making send fail with that error. So make
7093 * sure a send does not see any orphans we have just removed, and that it will
7094 * see the same inodes regardless of whether a transaction commit happened
7095 * before it started (meaning that the commit root will be the same as the
7096 * current root) or not.
7097 */
7098static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
7099{
7100 int i;
7101 struct btrfs_trans_handle *trans = NULL;
7102
7103again:
7104 if (sctx->parent_root &&
7105 sctx->parent_root->node != sctx->parent_root->commit_root)
7106 goto commit_trans;
7107
7108 for (i = 0; i < sctx->clone_roots_cnt; i++)
7109 if (sctx->clone_roots[i].root->node !=
7110 sctx->clone_roots[i].root->commit_root)
7111 goto commit_trans;
7112
7113 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007114 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007115
7116 return 0;
7117
7118commit_trans:
7119 /* Use any root, all fs roots will get their commit roots updated. */
7120 if (!trans) {
7121 trans = btrfs_join_transaction(sctx->send_root);
7122 if (IS_ERR(trans))
7123 return PTR_ERR(trans);
7124 goto again;
7125 }
7126
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007127 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007128}
7129
Filipe Manana9f89d5d2019-04-15 09:29:36 +01007130/*
7131 * Make sure any existing dellaloc is flushed for any root used by a send
7132 * operation so that we do not miss any data and we do not race with writeback
7133 * finishing and changing a tree while send is using the tree. This could
7134 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
7135 * a send operation then uses the subvolume.
7136 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
7137 */
7138static int flush_delalloc_roots(struct send_ctx *sctx)
7139{
7140 struct btrfs_root *root = sctx->parent_root;
7141 int ret;
7142 int i;
7143
7144 if (root) {
7145 ret = btrfs_start_delalloc_snapshot(root);
7146 if (ret)
7147 return ret;
7148 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7149 }
7150
7151 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7152 root = sctx->clone_roots[i].root;
7153 ret = btrfs_start_delalloc_snapshot(root);
7154 if (ret)
7155 return ret;
7156 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7157 }
7158
7159 return 0;
7160}
7161
David Sterba66ef7d62013-12-17 15:07:20 +01007162static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
7163{
7164 spin_lock(&root->root_item_lock);
7165 root->send_in_progress--;
7166 /*
7167 * Not much left to do, we don't know why it's unbalanced and
7168 * can't blindly reset it to 0.
7169 */
7170 if (root->send_in_progress < 0)
7171 btrfs_err(root->fs_info,
Colin Ian Kingf5686e32018-05-04 12:11:12 +01007172 "send_in_progress unbalanced %d root %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007173 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01007174 spin_unlock(&root->root_item_lock);
7175}
7176
Filipe Manana62d54f32019-04-22 16:43:42 +01007177static void dedupe_in_progress_warn(const struct btrfs_root *root)
7178{
7179 btrfs_warn_rl(root->fs_info,
7180"cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
7181 root->root_key.objectid, root->dedupe_in_progress);
7182}
7183
Josef Bacik2351f432017-09-27 10:43:13 -04007184long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
Alexander Block31db9f72012-07-25 23:19:24 +02007185{
7186 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007187 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
7188 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02007189 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02007190 struct send_ctx *sctx = NULL;
7191 u32 i;
7192 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01007193 int clone_sources_to_rollback = 0;
Denis Efremovbae12df2020-09-21 20:03:36 +03007194 size_t alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08007195 int sort_clone_roots = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02007196
7197 if (!capable(CAP_SYS_ADMIN))
7198 return -EPERM;
7199
Josef Bacik139f8072013-05-20 11:26:50 -04007200 /*
David Sterba2c686532013-12-16 17:34:17 +01007201 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02007202 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01007203 */
7204 spin_lock(&send_root->root_item_lock);
Filipe Manana62d54f32019-04-22 16:43:42 +01007205 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
7206 dedupe_in_progress_warn(send_root);
7207 spin_unlock(&send_root->root_item_lock);
7208 return -EAGAIN;
7209 }
David Sterba2c686532013-12-16 17:34:17 +01007210 send_root->send_in_progress++;
7211 spin_unlock(&send_root->root_item_lock);
7212
7213 /*
David Sterba2c686532013-12-16 17:34:17 +01007214 * Userspace tools do the checks and warn the user if it's
7215 * not RO.
7216 */
7217 if (!btrfs_root_readonly(send_root)) {
7218 ret = -EPERM;
7219 goto out;
7220 }
7221
Dan Carpenter457ae722017-03-17 23:51:20 +03007222 /*
7223 * Check that we don't overflow at later allocations, we request
7224 * clone_sources_count + 1 items, and compare to unsigned long inside
7225 * access_ok.
7226 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03007227 if (arg->clone_sources_count >
Dan Carpenter457ae722017-03-17 23:51:20 +03007228 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03007229 ret = -EINVAL;
7230 goto out;
7231 }
7232
Stefan Behrensc2c71322013-04-10 17:10:52 +00007233 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00007234 ret = -EINVAL;
7235 goto out;
7236 }
7237
David Sterbae780b0d2016-01-18 18:42:13 +01007238 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007239 if (!sctx) {
7240 ret = -ENOMEM;
7241 goto out;
7242 }
7243
7244 INIT_LIST_HEAD(&sctx->new_refs);
7245 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01007246 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007247 INIT_LIST_HEAD(&sctx->name_cache_list);
7248
Mark Fashehcb95e7b2013-02-04 20:54:57 +00007249 sctx->flags = arg->flags;
7250
Alexander Block31db9f72012-07-25 23:19:24 +02007251 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00007252 if (!sctx->send_filp) {
7253 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02007254 goto out;
7255 }
7256
Alexander Block31db9f72012-07-25 23:19:24 +02007257 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02007258 /*
7259 * Unlikely but possible, if the subvolume is marked for deletion but
7260 * is slow to remove the directory entry, send can still be started
7261 */
7262 if (btrfs_root_dead(sctx->send_root)) {
7263 ret = -EPERM;
7264 goto out;
7265 }
7266
Alexander Block31db9f72012-07-25 23:19:24 +02007267 sctx->clone_roots_cnt = arg->clone_sources_count;
7268
7269 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
Michal Hocko752ade62017-05-08 15:57:27 -07007270 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007271 if (!sctx->send_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07007272 ret = -ENOMEM;
7273 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007274 }
7275
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007276 sctx->pending_dir_moves = RB_ROOT;
7277 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00007278 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007279
Denis Efremovbae12df2020-09-21 20:03:36 +03007280 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots),
7281 arg->clone_sources_count + 1,
7282 GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007283 if (!sctx->clone_roots) {
David Sterba818e0102017-05-31 18:40:02 +02007284 ret = -ENOMEM;
7285 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007286 }
7287
Denis Efremovbae12df2020-09-21 20:03:36 +03007288 alloc_size = array_size(sizeof(*arg->clone_sources),
7289 arg->clone_sources_count);
David Sterbae55d1152016-04-11 18:52:02 +02007290
Alexander Block31db9f72012-07-25 23:19:24 +02007291 if (arg->clone_sources_count) {
Michal Hocko752ade62017-05-08 15:57:27 -07007292 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007293 if (!clone_sources_tmp) {
Michal Hocko752ade62017-05-08 15:57:27 -07007294 ret = -ENOMEM;
7295 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007296 }
7297
7298 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02007299 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02007300 if (ret) {
7301 ret = -EFAULT;
7302 goto out;
7303 }
7304
7305 for (i = 0; i < arg->clone_sources_count; i++) {
David Sterba56e93572020-05-15 19:35:55 +02007306 clone_root = btrfs_get_fs_root(fs_info,
7307 clone_sources_tmp[i], true);
Alexander Block31db9f72012-07-25 23:19:24 +02007308 if (IS_ERR(clone_root)) {
7309 ret = PTR_ERR(clone_root);
7310 goto out;
7311 }
David Sterba2c686532013-12-16 17:34:17 +01007312 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00007313 if (!btrfs_root_readonly(clone_root) ||
7314 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01007315 spin_unlock(&clone_root->root_item_lock);
Josef Bacik00246522020-01-24 09:33:01 -05007316 btrfs_put_root(clone_root);
David Sterba2c686532013-12-16 17:34:17 +01007317 ret = -EPERM;
7318 goto out;
7319 }
Filipe Manana62d54f32019-04-22 16:43:42 +01007320 if (clone_root->dedupe_in_progress) {
7321 dedupe_in_progress_warn(clone_root);
7322 spin_unlock(&clone_root->root_item_lock);
Josef Bacik00246522020-01-24 09:33:01 -05007323 btrfs_put_root(clone_root);
Filipe Manana62d54f32019-04-22 16:43:42 +01007324 ret = -EAGAIN;
7325 goto out;
7326 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00007327 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01007328 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08007329
Alexander Block31db9f72012-07-25 23:19:24 +02007330 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00007331 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02007332 }
David Sterba2f913062016-04-11 18:40:08 +02007333 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02007334 clone_sources_tmp = NULL;
7335 }
7336
7337 if (arg->parent_root) {
David Sterba56e93572020-05-15 19:35:55 +02007338 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
7339 true);
Stefan Behrensb1b19592013-05-13 14:42:57 +00007340 if (IS_ERR(sctx->parent_root)) {
7341 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02007342 goto out;
7343 }
Wang Shilong18f687d2014-01-07 17:25:19 +08007344
David Sterba2c686532013-12-16 17:34:17 +01007345 spin_lock(&sctx->parent_root->root_item_lock);
7346 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02007347 if (!btrfs_root_readonly(sctx->parent_root) ||
7348 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01007349 spin_unlock(&sctx->parent_root->root_item_lock);
7350 ret = -EPERM;
7351 goto out;
7352 }
Filipe Manana62d54f32019-04-22 16:43:42 +01007353 if (sctx->parent_root->dedupe_in_progress) {
7354 dedupe_in_progress_warn(sctx->parent_root);
7355 spin_unlock(&sctx->parent_root->root_item_lock);
Filipe Manana62d54f32019-04-22 16:43:42 +01007356 ret = -EAGAIN;
7357 goto out;
7358 }
David Sterba2c686532013-12-16 17:34:17 +01007359 spin_unlock(&sctx->parent_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02007360 }
7361
7362 /*
7363 * Clones from send_root are allowed, but only if the clone source
7364 * is behind the current send position. This is checked while searching
7365 * for possible clone sources.
7366 */
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007367 sctx->clone_roots[sctx->clone_roots_cnt++].root =
Josef Bacik00246522020-01-24 09:33:01 -05007368 btrfs_grab_root(sctx->send_root);
Alexander Block31db9f72012-07-25 23:19:24 +02007369
7370 /* We do a bsearch later */
7371 sort(sctx->clone_roots, sctx->clone_roots_cnt,
7372 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
7373 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08007374 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02007375
Filipe Manana9f89d5d2019-04-15 09:29:36 +01007376 ret = flush_delalloc_roots(sctx);
7377 if (ret)
7378 goto out;
7379
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007380 ret = ensure_commit_roots_uptodate(sctx);
7381 if (ret)
7382 goto out;
7383
Filipe Manana9e967492019-04-22 16:44:09 +01007384 mutex_lock(&fs_info->balance_mutex);
7385 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
7386 mutex_unlock(&fs_info->balance_mutex);
7387 btrfs_warn_rl(fs_info,
7388 "cannot run send because a balance operation is in progress");
7389 ret = -EAGAIN;
7390 goto out;
7391 }
7392 fs_info->send_in_progress++;
7393 mutex_unlock(&fs_info->balance_mutex);
7394
David Sterba2755a0d2014-07-31 00:43:18 +02007395 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02007396 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04007397 current->journal_info = NULL;
Filipe Manana9e967492019-04-22 16:44:09 +01007398 mutex_lock(&fs_info->balance_mutex);
7399 fs_info->send_in_progress--;
7400 mutex_unlock(&fs_info->balance_mutex);
Alexander Block31db9f72012-07-25 23:19:24 +02007401 if (ret < 0)
7402 goto out;
7403
Stefan Behrensc2c71322013-04-10 17:10:52 +00007404 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
7405 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
7406 if (ret < 0)
7407 goto out;
7408 ret = send_cmd(sctx);
7409 if (ret < 0)
7410 goto out;
7411 }
Alexander Block31db9f72012-07-25 23:19:24 +02007412
7413out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007414 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7415 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7416 struct rb_node *n;
7417 struct pending_dir_move *pm;
7418
7419 n = rb_first(&sctx->pending_dir_moves);
7420 pm = rb_entry(n, struct pending_dir_move, node);
7421 while (!list_empty(&pm->list)) {
7422 struct pending_dir_move *pm2;
7423
7424 pm2 = list_first_entry(&pm->list,
7425 struct pending_dir_move, list);
7426 free_pending_move(sctx, pm2);
7427 }
7428 free_pending_move(sctx, pm);
7429 }
7430
7431 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7432 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7433 struct rb_node *n;
7434 struct waiting_dir_move *dm;
7435
7436 n = rb_first(&sctx->waiting_dir_moves);
7437 dm = rb_entry(n, struct waiting_dir_move, node);
7438 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7439 kfree(dm);
7440 }
7441
Filipe Manana9dc44212014-02-19 14:31:44 +00007442 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
7443 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
7444 struct rb_node *n;
7445 struct orphan_dir_info *odi;
7446
7447 n = rb_first(&sctx->orphan_dirs);
7448 odi = rb_entry(n, struct orphan_dir_info, node);
7449 free_orphan_dir_info(sctx, odi);
7450 }
7451
Wang Shilong896c14f2014-01-07 17:25:18 +08007452 if (sort_clone_roots) {
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007453 for (i = 0; i < sctx->clone_roots_cnt; i++) {
Wang Shilong896c14f2014-01-07 17:25:18 +08007454 btrfs_root_dec_send_in_progress(
7455 sctx->clone_roots[i].root);
Josef Bacik00246522020-01-24 09:33:01 -05007456 btrfs_put_root(sctx->clone_roots[i].root);
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007457 }
Wang Shilong896c14f2014-01-07 17:25:18 +08007458 } else {
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007459 for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
Wang Shilong896c14f2014-01-07 17:25:18 +08007460 btrfs_root_dec_send_in_progress(
7461 sctx->clone_roots[i].root);
Josef Bacik00246522020-01-24 09:33:01 -05007462 btrfs_put_root(sctx->clone_roots[i].root);
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007463 }
Wang Shilong896c14f2014-01-07 17:25:18 +08007464
7465 btrfs_root_dec_send_in_progress(send_root);
7466 }
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007467 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
David Sterba66ef7d62013-12-17 15:07:20 +01007468 btrfs_root_dec_send_in_progress(sctx->parent_root);
Josef Bacik00246522020-01-24 09:33:01 -05007469 btrfs_put_root(sctx->parent_root);
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007470 }
David Sterba2c686532013-12-16 17:34:17 +01007471
David Sterba2f913062016-04-11 18:40:08 +02007472 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02007473
7474 if (sctx) {
7475 if (sctx->send_filp)
7476 fput(sctx->send_filp);
7477
David Sterbac03d01f2016-04-11 18:40:08 +02007478 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02007479 kvfree(sctx->send_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02007480
7481 name_cache_free(sctx);
7482
7483 kfree(sctx);
7484 }
7485
7486 return ret;
7487}