blob: 78a35374d492914d94004d65268bda8d4da54a01 [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
Filipe Manana619d8c42015-05-03 01:56:00 +01001194 /* data offset in the file extent item */
1195 u64 data_offset;
1196
Alexander Block31db9f72012-07-25 23:19:24 +02001197 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001198 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001199};
1200
1201static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1202{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001203 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001204 struct clone_root *cr = (struct clone_root *)elt;
1205
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001206 if (root < cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001207 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001208 if (root > cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001209 return 1;
1210 return 0;
1211}
1212
1213static int __clone_root_cmp_sort(const void *e1, const void *e2)
1214{
1215 struct clone_root *cr1 = (struct clone_root *)e1;
1216 struct clone_root *cr2 = (struct clone_root *)e2;
1217
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001218 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001219 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001220 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001221 return 1;
1222 return 0;
1223}
1224
1225/*
1226 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001227 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001228 */
1229static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1230{
1231 struct backref_ctx *bctx = ctx_;
1232 struct clone_root *found;
Alexander Block31db9f72012-07-25 23:19:24 +02001233
1234 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001235 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001236 bctx->sctx->clone_roots_cnt,
1237 sizeof(struct clone_root),
1238 __clone_root_cmp_bsearch);
1239 if (!found)
1240 return 0;
1241
1242 if (found->root == bctx->sctx->send_root &&
1243 ino == bctx->cur_objectid &&
1244 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001245 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001246 }
1247
1248 /*
Alexander Block31db9f72012-07-25 23:19:24 +02001249 * Make sure we don't consider clones from send_root that are
1250 * behind the current inode/offset.
1251 */
1252 if (found->root == bctx->sctx->send_root) {
1253 /*
Filipe Manana11f20692019-10-30 12:23:11 +00001254 * If the source inode was not yet processed we can't issue a
1255 * clone operation, as the source extent does not exist yet at
1256 * the destination of the stream.
Alexander Block31db9f72012-07-25 23:19:24 +02001257 */
Filipe Manana11f20692019-10-30 12:23:11 +00001258 if (ino > bctx->cur_objectid)
1259 return 0;
1260 /*
1261 * We clone from the inode currently being sent as long as the
1262 * source extent is already processed, otherwise we could try
1263 * to clone from an extent that does not exist yet at the
1264 * destination of the stream.
1265 */
1266 if (ino == bctx->cur_objectid &&
Filipe Manana9722b102020-01-29 17:09:53 +00001267 offset + bctx->extent_len >
1268 bctx->sctx->cur_inode_next_write_offset)
Alexander Block31db9f72012-07-25 23:19:24 +02001269 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001270 }
1271
1272 bctx->found++;
1273 found->found_refs++;
1274 if (ino < found->ino) {
1275 found->ino = ino;
1276 found->offset = offset;
1277 } else if (found->ino == ino) {
1278 /*
1279 * same extent found more then once in the same file.
1280 */
1281 if (found->offset > offset + bctx->extent_len)
1282 found->offset = offset;
1283 }
1284
1285 return 0;
1286}
1287
1288/*
Alexander Block766702e2012-07-28 14:11:31 +02001289 * Given an inode, offset and extent item, it finds a good clone for a clone
1290 * instruction. Returns -ENOENT when none could be found. The function makes
1291 * sure that the returned clone is usable at the point where sending is at the
1292 * moment. This means, that no clones are accepted which lie behind the current
1293 * inode+offset.
1294 *
Alexander Block31db9f72012-07-25 23:19:24 +02001295 * path must point to the extent item when called.
1296 */
1297static int find_extent_clone(struct send_ctx *sctx,
1298 struct btrfs_path *path,
1299 u64 ino, u64 data_offset,
1300 u64 ino_size,
1301 struct clone_root **found)
1302{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001303 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001304 int ret;
1305 int extent_type;
1306 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001307 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001308 u64 num_bytes;
1309 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001310 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001311 struct btrfs_file_extent_item *fi;
1312 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001313 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001314 struct clone_root *cur_clone_root;
1315 struct btrfs_key found_key;
1316 struct btrfs_path *tmp_path;
Filipe Mananafd0ddbe2019-10-30 12:23:01 +00001317 struct btrfs_extent_item *ei;
Chris Mason74dd17f2012-08-07 16:25:13 -04001318 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001319 u32 i;
1320
1321 tmp_path = alloc_path_for_send();
1322 if (!tmp_path)
1323 return -ENOMEM;
1324
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001325 /* We only use this path under the commit sem */
1326 tmp_path->need_commit_sem = 0;
1327
David Sterbae780b0d2016-01-18 18:42:13 +01001328 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001329 if (!backref_ctx) {
1330 ret = -ENOMEM;
1331 goto out;
1332 }
1333
Alexander Block31db9f72012-07-25 23:19:24 +02001334 if (data_offset >= ino_size) {
1335 /*
1336 * There may be extents that lie behind the file's size.
1337 * I at least had this in combination with snapshotting while
1338 * writing large files.
1339 */
1340 ret = 0;
1341 goto out;
1342 }
1343
1344 fi = btrfs_item_ptr(eb, path->slots[0],
1345 struct btrfs_file_extent_item);
1346 extent_type = btrfs_file_extent_type(eb, fi);
1347 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1348 ret = -ENOENT;
1349 goto out;
1350 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001351 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001352
1353 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001354 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1355 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001356 ret = -ENOENT;
1357 goto out;
1358 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001359 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001360
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001361 down_read(&fs_info->commit_root_sem);
1362 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001363 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001364 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001365
1366 if (ret < 0)
1367 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001368 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001369 ret = -EIO;
1370 goto out;
1371 }
1372
Filipe Mananafd0ddbe2019-10-30 12:23:01 +00001373 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1374 struct btrfs_extent_item);
1375 /*
1376 * Backreference walking (iterate_extent_inodes() below) is currently
1377 * too expensive when an extent has a large number of references, both
1378 * in time spent and used memory. So for now just fallback to write
1379 * operations instead of clone operations when an extent has more than
1380 * a certain amount of references.
1381 */
1382 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1383 ret = -ENOENT;
1384 goto out;
1385 }
1386 btrfs_release_path(tmp_path);
1387
Alexander Block31db9f72012-07-25 23:19:24 +02001388 /*
1389 * Setup the clone roots.
1390 */
1391 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1392 cur_clone_root = sctx->clone_roots + i;
1393 cur_clone_root->ino = (u64)-1;
1394 cur_clone_root->offset = 0;
1395 cur_clone_root->found_refs = 0;
1396 }
1397
Alexander Block35075bb2012-07-28 12:44:34 +02001398 backref_ctx->sctx = sctx;
1399 backref_ctx->found = 0;
1400 backref_ctx->cur_objectid = ino;
1401 backref_ctx->cur_offset = data_offset;
1402 backref_ctx->found_itself = 0;
1403 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001404 /*
1405 * For non-compressed extents iterate_extent_inodes() gives us extent
1406 * offsets that already take into account the data offset, but not for
1407 * compressed extents, since the offset is logical and not relative to
1408 * the physical extent locations. We must take this into account to
1409 * avoid sending clone offsets that go beyond the source file's size,
1410 * which would result in the clone ioctl failing with -EINVAL on the
1411 * receiving end.
1412 */
1413 if (compressed == BTRFS_COMPRESS_NONE)
1414 backref_ctx->data_offset = 0;
1415 else
1416 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001417
1418 /*
1419 * The last extent of a file may be too large due to page alignment.
1420 * We need to adjust extent_len in this case so that the checks in
1421 * __iterate_backrefs work.
1422 */
1423 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001424 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001425
1426 /*
1427 * Now collect all backrefs.
1428 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001429 if (compressed == BTRFS_COMPRESS_NONE)
1430 extent_item_pos = logical - found_key.objectid;
1431 else
1432 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001433 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1434 extent_item_pos, 1, __iterate_backrefs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001435 backref_ctx, false);
Chris Mason74dd17f2012-08-07 16:25:13 -04001436
Alexander Block31db9f72012-07-25 23:19:24 +02001437 if (ret < 0)
1438 goto out;
1439
Alexander Block35075bb2012-07-28 12:44:34 +02001440 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001441 /* found a bug in backref code? */
1442 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001443 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001444 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001445 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001446 goto out;
1447 }
1448
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001449 btrfs_debug(fs_info,
1450 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1451 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001452
Alexander Block35075bb2012-07-28 12:44:34 +02001453 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001454 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001455
1456 cur_clone_root = NULL;
1457 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1458 if (sctx->clone_roots[i].found_refs) {
1459 if (!cur_clone_root)
1460 cur_clone_root = sctx->clone_roots + i;
1461 else if (sctx->clone_roots[i].root == sctx->send_root)
1462 /* prefer clones from send_root over others */
1463 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001464 }
1465
1466 }
1467
1468 if (cur_clone_root) {
1469 *found = cur_clone_root;
1470 ret = 0;
1471 } else {
1472 ret = -ENOENT;
1473 }
1474
1475out:
1476 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001477 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001478 return ret;
1479}
1480
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001481static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001482 u64 ino,
1483 struct fs_path *dest)
1484{
1485 int ret;
1486 struct btrfs_path *path;
1487 struct btrfs_key key;
1488 struct btrfs_file_extent_item *ei;
1489 u8 type;
1490 u8 compression;
1491 unsigned long off;
1492 int len;
1493
1494 path = alloc_path_for_send();
1495 if (!path)
1496 return -ENOMEM;
1497
1498 key.objectid = ino;
1499 key.type = BTRFS_EXTENT_DATA_KEY;
1500 key.offset = 0;
1501 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1502 if (ret < 0)
1503 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001504 if (ret) {
1505 /*
1506 * An empty symlink inode. Can happen in rare error paths when
1507 * creating a symlink (transaction committed before the inode
1508 * eviction handler removed the symlink inode items and a crash
1509 * happened in between or the subvol was snapshoted in between).
1510 * Print an informative message to dmesg/syslog so that the user
1511 * can delete the symlink.
1512 */
1513 btrfs_err(root->fs_info,
1514 "Found empty symlink inode %llu at root %llu",
1515 ino, root->root_key.objectid);
1516 ret = -EIO;
1517 goto out;
1518 }
Alexander Block31db9f72012-07-25 23:19:24 +02001519
1520 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1521 struct btrfs_file_extent_item);
1522 type = btrfs_file_extent_type(path->nodes[0], ei);
1523 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1524 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1525 BUG_ON(compression);
1526
1527 off = btrfs_file_extent_inline_start(ei);
Qu Wenruoe41ca582018-06-06 15:41:49 +08001528 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001529
1530 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001531
1532out:
1533 btrfs_free_path(path);
1534 return ret;
1535}
1536
1537/*
1538 * Helper function to generate a file name that is unique in the root of
1539 * send_root and parent_root. This is used to generate names for orphan inodes.
1540 */
1541static int gen_unique_name(struct send_ctx *sctx,
1542 u64 ino, u64 gen,
1543 struct fs_path *dest)
1544{
1545 int ret = 0;
1546 struct btrfs_path *path;
1547 struct btrfs_dir_item *di;
1548 char tmp[64];
1549 int len;
1550 u64 idx = 0;
1551
1552 path = alloc_path_for_send();
1553 if (!path)
1554 return -ENOMEM;
1555
1556 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001557 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001558 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001559 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001560
1561 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1562 path, BTRFS_FIRST_FREE_OBJECTID,
1563 tmp, strlen(tmp), 0);
1564 btrfs_release_path(path);
1565 if (IS_ERR(di)) {
1566 ret = PTR_ERR(di);
1567 goto out;
1568 }
1569 if (di) {
1570 /* not unique, try again */
1571 idx++;
1572 continue;
1573 }
1574
1575 if (!sctx->parent_root) {
1576 /* unique */
1577 ret = 0;
1578 break;
1579 }
1580
1581 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1582 path, BTRFS_FIRST_FREE_OBJECTID,
1583 tmp, strlen(tmp), 0);
1584 btrfs_release_path(path);
1585 if (IS_ERR(di)) {
1586 ret = PTR_ERR(di);
1587 goto out;
1588 }
1589 if (di) {
1590 /* not unique, try again */
1591 idx++;
1592 continue;
1593 }
1594 /* unique */
1595 break;
1596 }
1597
1598 ret = fs_path_add(dest, tmp, strlen(tmp));
1599
1600out:
1601 btrfs_free_path(path);
1602 return ret;
1603}
1604
1605enum inode_state {
1606 inode_state_no_change,
1607 inode_state_will_create,
1608 inode_state_did_create,
1609 inode_state_will_delete,
1610 inode_state_did_delete,
1611};
1612
1613static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1614{
1615 int ret;
1616 int left_ret;
1617 int right_ret;
1618 u64 left_gen;
1619 u64 right_gen;
1620
1621 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001622 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001623 if (ret < 0 && ret != -ENOENT)
1624 goto out;
1625 left_ret = ret;
1626
1627 if (!sctx->parent_root) {
1628 right_ret = -ENOENT;
1629 } else {
1630 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001631 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001632 if (ret < 0 && ret != -ENOENT)
1633 goto out;
1634 right_ret = ret;
1635 }
1636
1637 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001638 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001639 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001640 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001641 if (ino < sctx->send_progress)
1642 ret = inode_state_did_create;
1643 else
1644 ret = inode_state_will_create;
1645 } else if (right_gen == gen) {
1646 if (ino < sctx->send_progress)
1647 ret = inode_state_did_delete;
1648 else
1649 ret = inode_state_will_delete;
1650 } else {
1651 ret = -ENOENT;
1652 }
1653 } else if (!left_ret) {
1654 if (left_gen == gen) {
1655 if (ino < sctx->send_progress)
1656 ret = inode_state_did_create;
1657 else
1658 ret = inode_state_will_create;
1659 } else {
1660 ret = -ENOENT;
1661 }
1662 } else if (!right_ret) {
1663 if (right_gen == gen) {
1664 if (ino < sctx->send_progress)
1665 ret = inode_state_did_delete;
1666 else
1667 ret = inode_state_will_delete;
1668 } else {
1669 ret = -ENOENT;
1670 }
1671 } else {
1672 ret = -ENOENT;
1673 }
1674
1675out:
1676 return ret;
1677}
1678
1679static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1680{
1681 int ret;
1682
Robbie Ko4dd99202017-01-05 16:24:55 +08001683 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1684 return 1;
1685
Alexander Block31db9f72012-07-25 23:19:24 +02001686 ret = get_cur_inode_state(sctx, ino, gen);
1687 if (ret < 0)
1688 goto out;
1689
1690 if (ret == inode_state_no_change ||
1691 ret == inode_state_did_create ||
1692 ret == inode_state_will_delete)
1693 ret = 1;
1694 else
1695 ret = 0;
1696
1697out:
1698 return ret;
1699}
1700
1701/*
1702 * Helper function to lookup a dir item in a dir.
1703 */
1704static int lookup_dir_item_inode(struct btrfs_root *root,
1705 u64 dir, const char *name, int name_len,
1706 u64 *found_inode,
1707 u8 *found_type)
1708{
1709 int ret = 0;
1710 struct btrfs_dir_item *di;
1711 struct btrfs_key key;
1712 struct btrfs_path *path;
1713
1714 path = alloc_path_for_send();
1715 if (!path)
1716 return -ENOMEM;
1717
1718 di = btrfs_lookup_dir_item(NULL, root, path,
1719 dir, name, name_len, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08001720 if (IS_ERR_OR_NULL(di)) {
1721 ret = di ? PTR_ERR(di) : -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02001722 goto out;
1723 }
1724 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001725 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1726 ret = -ENOENT;
1727 goto out;
1728 }
Alexander Block31db9f72012-07-25 23:19:24 +02001729 *found_inode = key.objectid;
1730 *found_type = btrfs_dir_type(path->nodes[0], di);
1731
1732out:
1733 btrfs_free_path(path);
1734 return ret;
1735}
1736
Alexander Block766702e2012-07-28 14:11:31 +02001737/*
1738 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1739 * generation of the parent dir and the name of the dir entry.
1740 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001741static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001742 u64 *dir, u64 *dir_gen, struct fs_path *name)
1743{
1744 int ret;
1745 struct btrfs_key key;
1746 struct btrfs_key found_key;
1747 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001748 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001749 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001750
1751 path = alloc_path_for_send();
1752 if (!path)
1753 return -ENOMEM;
1754
1755 key.objectid = ino;
1756 key.type = BTRFS_INODE_REF_KEY;
1757 key.offset = 0;
1758
1759 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1760 if (ret < 0)
1761 goto out;
1762 if (!ret)
1763 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1764 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001765 if (ret || found_key.objectid != ino ||
1766 (found_key.type != BTRFS_INODE_REF_KEY &&
1767 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001768 ret = -ENOENT;
1769 goto out;
1770 }
1771
Filipe Manana51a60252014-05-13 22:01:02 +01001772 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001773 struct btrfs_inode_ref *iref;
1774 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1775 struct btrfs_inode_ref);
1776 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1777 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1778 (unsigned long)(iref + 1),
1779 len);
1780 parent_dir = found_key.offset;
1781 } else {
1782 struct btrfs_inode_extref *extref;
1783 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1784 struct btrfs_inode_extref);
1785 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1786 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1787 (unsigned long)&extref->name, len);
1788 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1789 }
Alexander Block31db9f72012-07-25 23:19:24 +02001790 if (ret < 0)
1791 goto out;
1792 btrfs_release_path(path);
1793
Filipe Mananab46ab972014-03-21 12:46:54 +00001794 if (dir_gen) {
1795 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1796 NULL, NULL, NULL);
1797 if (ret < 0)
1798 goto out;
1799 }
Alexander Block31db9f72012-07-25 23:19:24 +02001800
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001801 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001802
1803out:
1804 btrfs_free_path(path);
1805 return ret;
1806}
1807
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001808static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001809 u64 ino, u64 dir,
1810 const char *name, int name_len)
1811{
1812 int ret;
1813 struct fs_path *tmp_name;
1814 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001815
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001816 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001817 if (!tmp_name)
1818 return -ENOMEM;
1819
Filipe Mananab46ab972014-03-21 12:46:54 +00001820 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001821 if (ret < 0)
1822 goto out;
1823
Alexander Blockb9291af2012-07-28 11:07:18 +02001824 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001825 ret = 0;
1826 goto out;
1827 }
1828
Alexander Blocke938c8a2012-07-28 16:33:49 +02001829 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001830
1831out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001832 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001833 return ret;
1834}
1835
Alexander Block766702e2012-07-28 14:11:31 +02001836/*
1837 * Used by process_recorded_refs to determine if a new ref would overwrite an
1838 * already existing ref. In case it detects an overwrite, it returns the
1839 * inode/gen in who_ino/who_gen.
1840 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1841 * to make sure later references to the overwritten inode are possible.
1842 * Orphanizing is however only required for the first ref of an inode.
1843 * process_recorded_refs does an additional is_first_ref check to see if
1844 * orphanizing is really required.
1845 */
Alexander Block31db9f72012-07-25 23:19:24 +02001846static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1847 const char *name, int name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01001848 u64 *who_ino, u64 *who_gen, u64 *who_mode)
Alexander Block31db9f72012-07-25 23:19:24 +02001849{
1850 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001851 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001852 u64 other_inode = 0;
1853 u8 other_type = 0;
1854
1855 if (!sctx->parent_root)
1856 goto out;
1857
1858 ret = is_inode_existent(sctx, dir, dir_gen);
1859 if (ret <= 0)
1860 goto out;
1861
Josef Bacikebdad912013-08-06 16:47:48 -04001862 /*
1863 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001864 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001865 * and we can just unlink this entry.
1866 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001867 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001868 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1869 NULL, NULL, NULL);
1870 if (ret < 0 && ret != -ENOENT)
1871 goto out;
1872 if (ret) {
1873 ret = 0;
1874 goto out;
1875 }
1876 if (gen != dir_gen)
1877 goto out;
1878 }
1879
Alexander Block31db9f72012-07-25 23:19:24 +02001880 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1881 &other_inode, &other_type);
1882 if (ret < 0 && ret != -ENOENT)
1883 goto out;
1884 if (ret) {
1885 ret = 0;
1886 goto out;
1887 }
1888
Alexander Block766702e2012-07-28 14:11:31 +02001889 /*
1890 * Check if the overwritten ref was already processed. If yes, the ref
1891 * was already unlinked/moved, so we can safely assume that we will not
1892 * overwrite anything at this point in time.
1893 */
Robbie Ko801bec32015-06-23 18:39:46 +08001894 if (other_inode > sctx->send_progress ||
1895 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001896 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Filipe Mananaf5962782017-06-22 20:03:51 +01001897 who_gen, who_mode, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001898 if (ret < 0)
1899 goto out;
1900
1901 ret = 1;
1902 *who_ino = other_inode;
1903 } else {
1904 ret = 0;
1905 }
1906
1907out:
1908 return ret;
1909}
1910
Alexander Block766702e2012-07-28 14:11:31 +02001911/*
1912 * Checks if the ref was overwritten by an already processed inode. This is
1913 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1914 * thus the orphan name needs be used.
1915 * process_recorded_refs also uses it to avoid unlinking of refs that were
1916 * overwritten.
1917 */
Alexander Block31db9f72012-07-25 23:19:24 +02001918static int did_overwrite_ref(struct send_ctx *sctx,
1919 u64 dir, u64 dir_gen,
1920 u64 ino, u64 ino_gen,
1921 const char *name, int name_len)
1922{
1923 int ret = 0;
1924 u64 gen;
1925 u64 ow_inode;
1926 u8 other_type;
1927
1928 if (!sctx->parent_root)
1929 goto out;
1930
1931 ret = is_inode_existent(sctx, dir, dir_gen);
1932 if (ret <= 0)
1933 goto out;
1934
Robbie Ko01914102017-01-05 16:24:58 +08001935 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1936 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1937 NULL, NULL, NULL);
1938 if (ret < 0 && ret != -ENOENT)
1939 goto out;
1940 if (ret) {
1941 ret = 0;
1942 goto out;
1943 }
1944 if (gen != dir_gen)
1945 goto out;
1946 }
1947
Alexander Block31db9f72012-07-25 23:19:24 +02001948 /* check if the ref was overwritten by another ref */
1949 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1950 &ow_inode, &other_type);
1951 if (ret < 0 && ret != -ENOENT)
1952 goto out;
1953 if (ret) {
1954 /* was never and will never be overwritten */
1955 ret = 0;
1956 goto out;
1957 }
1958
1959 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001960 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001961 if (ret < 0)
1962 goto out;
1963
1964 if (ow_inode == ino && gen == ino_gen) {
1965 ret = 0;
1966 goto out;
1967 }
1968
Filipe Manana8b191a62015-04-09 14:09:14 +01001969 /*
1970 * We know that it is or will be overwritten. Check this now.
1971 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001972 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1973 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001974 */
Filipe Mananab786f162015-09-26 15:30:23 +01001975 if ((ow_inode < sctx->send_progress) ||
1976 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1977 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001978 ret = 1;
1979 else
1980 ret = 0;
1981
1982out:
1983 return ret;
1984}
1985
Alexander Block766702e2012-07-28 14:11:31 +02001986/*
1987 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1988 * that got overwritten. This is used by process_recorded_refs to determine
1989 * if it has to use the path as returned by get_cur_path or the orphan name.
1990 */
Alexander Block31db9f72012-07-25 23:19:24 +02001991static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1992{
1993 int ret = 0;
1994 struct fs_path *name = NULL;
1995 u64 dir;
1996 u64 dir_gen;
1997
1998 if (!sctx->parent_root)
1999 goto out;
2000
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002001 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002002 if (!name)
2003 return -ENOMEM;
2004
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002005 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02002006 if (ret < 0)
2007 goto out;
2008
2009 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2010 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02002011
2012out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002013 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002014 return ret;
2015}
2016
Alexander Block766702e2012-07-28 14:11:31 +02002017/*
2018 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2019 * so we need to do some special handling in case we have clashes. This function
2020 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002021 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002022 */
Alexander Block31db9f72012-07-25 23:19:24 +02002023static int name_cache_insert(struct send_ctx *sctx,
2024 struct name_cache_entry *nce)
2025{
2026 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002027 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002028
Alexander Block7e0926f2012-07-28 14:20:58 +02002029 nce_head = radix_tree_lookup(&sctx->name_cache,
2030 (unsigned long)nce->ino);
2031 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002032 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002033 if (!nce_head) {
2034 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002035 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002036 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002037 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002038
Alexander Block7e0926f2012-07-28 14:20:58 +02002039 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002040 if (ret < 0) {
2041 kfree(nce_head);
2042 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002043 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002044 }
Alexander Block31db9f72012-07-25 23:19:24 +02002045 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002046 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002047 list_add_tail(&nce->list, &sctx->name_cache_list);
2048 sctx->name_cache_size++;
2049
2050 return ret;
2051}
2052
2053static void name_cache_delete(struct send_ctx *sctx,
2054 struct name_cache_entry *nce)
2055{
Alexander Block7e0926f2012-07-28 14:20:58 +02002056 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002057
Alexander Block7e0926f2012-07-28 14:20:58 +02002058 nce_head = radix_tree_lookup(&sctx->name_cache,
2059 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002060 if (!nce_head) {
2061 btrfs_err(sctx->send_root->fs_info,
2062 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2063 nce->ino, sctx->name_cache_size);
2064 }
Alexander Block31db9f72012-07-25 23:19:24 +02002065
Alexander Block7e0926f2012-07-28 14:20:58 +02002066 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002067 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002068 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002069
David Sterba57fb8912014-02-03 19:24:40 +01002070 /*
2071 * We may not get to the final release of nce_head if the lookup fails
2072 */
2073 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002074 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2075 kfree(nce_head);
2076 }
Alexander Block31db9f72012-07-25 23:19:24 +02002077}
2078
2079static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2080 u64 ino, u64 gen)
2081{
Alexander Block7e0926f2012-07-28 14:20:58 +02002082 struct list_head *nce_head;
2083 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002084
Alexander Block7e0926f2012-07-28 14:20:58 +02002085 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2086 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002087 return NULL;
2088
Alexander Block7e0926f2012-07-28 14:20:58 +02002089 list_for_each_entry(cur, nce_head, radix_list) {
2090 if (cur->ino == ino && cur->gen == gen)
2091 return cur;
2092 }
Alexander Block31db9f72012-07-25 23:19:24 +02002093 return NULL;
2094}
2095
Alexander Block766702e2012-07-28 14:11:31 +02002096/*
2097 * Removes the entry from the list and adds it back to the end. This marks the
2098 * entry as recently used so that name_cache_clean_unused does not remove it.
2099 */
Alexander Block31db9f72012-07-25 23:19:24 +02002100static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2101{
2102 list_del(&nce->list);
2103 list_add_tail(&nce->list, &sctx->name_cache_list);
2104}
2105
Alexander Block766702e2012-07-28 14:11:31 +02002106/*
2107 * Remove some entries from the beginning of name_cache_list.
2108 */
Alexander Block31db9f72012-07-25 23:19:24 +02002109static void name_cache_clean_unused(struct send_ctx *sctx)
2110{
2111 struct name_cache_entry *nce;
2112
2113 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2114 return;
2115
2116 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2117 nce = list_entry(sctx->name_cache_list.next,
2118 struct name_cache_entry, list);
2119 name_cache_delete(sctx, nce);
2120 kfree(nce);
2121 }
2122}
2123
2124static void name_cache_free(struct send_ctx *sctx)
2125{
2126 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002127
Alexander Blocke938c8a2012-07-28 16:33:49 +02002128 while (!list_empty(&sctx->name_cache_list)) {
2129 nce = list_entry(sctx->name_cache_list.next,
2130 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002131 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002132 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002133 }
2134}
2135
Alexander Block766702e2012-07-28 14:11:31 +02002136/*
2137 * Used by get_cur_path for each ref up to the root.
2138 * Returns 0 if it succeeded.
2139 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2140 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2141 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2142 * Returns <0 in case of error.
2143 */
Alexander Block31db9f72012-07-25 23:19:24 +02002144static int __get_cur_name_and_parent(struct send_ctx *sctx,
2145 u64 ino, u64 gen,
2146 u64 *parent_ino,
2147 u64 *parent_gen,
2148 struct fs_path *dest)
2149{
2150 int ret;
2151 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002152 struct name_cache_entry *nce = NULL;
2153
Alexander Block766702e2012-07-28 14:11:31 +02002154 /*
2155 * First check if we already did a call to this function with the same
2156 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2157 * return the cached result.
2158 */
Alexander Block31db9f72012-07-25 23:19:24 +02002159 nce = name_cache_search(sctx, ino, gen);
2160 if (nce) {
2161 if (ino < sctx->send_progress && nce->need_later_update) {
2162 name_cache_delete(sctx, nce);
2163 kfree(nce);
2164 nce = NULL;
2165 } else {
2166 name_cache_used(sctx, nce);
2167 *parent_ino = nce->parent_ino;
2168 *parent_gen = nce->parent_gen;
2169 ret = fs_path_add(dest, nce->name, nce->name_len);
2170 if (ret < 0)
2171 goto out;
2172 ret = nce->ret;
2173 goto out;
2174 }
2175 }
2176
Alexander Block766702e2012-07-28 14:11:31 +02002177 /*
2178 * If the inode is not existent yet, add the orphan name and return 1.
2179 * This should only happen for the parent dir that we determine in
2180 * __record_new_ref
2181 */
Alexander Block31db9f72012-07-25 23:19:24 +02002182 ret = is_inode_existent(sctx, ino, gen);
2183 if (ret < 0)
2184 goto out;
2185
2186 if (!ret) {
2187 ret = gen_unique_name(sctx, ino, gen, dest);
2188 if (ret < 0)
2189 goto out;
2190 ret = 1;
2191 goto out_cache;
2192 }
2193
Alexander Block766702e2012-07-28 14:11:31 +02002194 /*
2195 * Depending on whether the inode was already processed or not, use
2196 * send_root or parent_root for ref lookup.
2197 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002198 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002199 ret = get_first_ref(sctx->send_root, ino,
2200 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002201 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002202 ret = get_first_ref(sctx->parent_root, ino,
2203 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002204 if (ret < 0)
2205 goto out;
2206
Alexander Block766702e2012-07-28 14:11:31 +02002207 /*
2208 * Check if the ref was overwritten by an inode's ref that was processed
2209 * earlier. If yes, treat as orphan and return 1.
2210 */
Alexander Block31db9f72012-07-25 23:19:24 +02002211 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2212 dest->start, dest->end - dest->start);
2213 if (ret < 0)
2214 goto out;
2215 if (ret) {
2216 fs_path_reset(dest);
2217 ret = gen_unique_name(sctx, ino, gen, dest);
2218 if (ret < 0)
2219 goto out;
2220 ret = 1;
2221 }
2222
2223out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002224 /*
2225 * Store the result of the lookup in the name cache.
2226 */
David Sterbae780b0d2016-01-18 18:42:13 +01002227 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002228 if (!nce) {
2229 ret = -ENOMEM;
2230 goto out;
2231 }
2232
2233 nce->ino = ino;
2234 nce->gen = gen;
2235 nce->parent_ino = *parent_ino;
2236 nce->parent_gen = *parent_gen;
2237 nce->name_len = fs_path_len(dest);
2238 nce->ret = ret;
2239 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002240
2241 if (ino < sctx->send_progress)
2242 nce->need_later_update = 0;
2243 else
2244 nce->need_later_update = 1;
2245
2246 nce_ret = name_cache_insert(sctx, nce);
2247 if (nce_ret < 0)
2248 ret = nce_ret;
2249 name_cache_clean_unused(sctx);
2250
2251out:
Alexander Block31db9f72012-07-25 23:19:24 +02002252 return ret;
2253}
2254
2255/*
2256 * Magic happens here. This function returns the first ref to an inode as it
2257 * would look like while receiving the stream at this point in time.
2258 * We walk the path up to the root. For every inode in between, we check if it
2259 * was already processed/sent. If yes, we continue with the parent as found
2260 * in send_root. If not, we continue with the parent as found in parent_root.
2261 * If we encounter an inode that was deleted at this point in time, we use the
2262 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2263 * that were not created yet and overwritten inodes/refs.
2264 *
Andrea Gelmini52042d82018-11-28 12:05:13 +01002265 * When do we have orphan inodes:
Alexander Block31db9f72012-07-25 23:19:24 +02002266 * 1. When an inode is freshly created and thus no valid refs are available yet
2267 * 2. When a directory lost all it's refs (deleted) but still has dir items
2268 * inside which were not processed yet (pending for move/delete). If anyone
2269 * tried to get the path to the dir items, it would get a path inside that
2270 * orphan directory.
2271 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2272 * of an unprocessed inode. If in that case the first ref would be
2273 * overwritten, the overwritten inode gets "orphanized". Later when we
2274 * process this overwritten inode, it is restored at a new place by moving
2275 * the orphan inode.
2276 *
2277 * sctx->send_progress tells this function at which point in time receiving
2278 * would be.
2279 */
2280static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2281 struct fs_path *dest)
2282{
2283 int ret = 0;
2284 struct fs_path *name = NULL;
2285 u64 parent_inode = 0;
2286 u64 parent_gen = 0;
2287 int stop = 0;
2288
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002289 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002290 if (!name) {
2291 ret = -ENOMEM;
2292 goto out;
2293 }
2294
2295 dest->reversed = 1;
2296 fs_path_reset(dest);
2297
2298 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002299 struct waiting_dir_move *wdm;
2300
Alexander Block31db9f72012-07-25 23:19:24 +02002301 fs_path_reset(name);
2302
Filipe Manana0b3f4072020-12-10 12:09:02 +00002303 if (is_waiting_for_rm(sctx, ino, gen)) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002304 ret = gen_unique_name(sctx, ino, gen, name);
2305 if (ret < 0)
2306 goto out;
2307 ret = fs_path_add_path(dest, name);
2308 break;
2309 }
2310
Filipe Manana8b191a62015-04-09 14:09:14 +01002311 wdm = get_waiting_dir_move(sctx, ino);
2312 if (wdm && wdm->orphanized) {
2313 ret = gen_unique_name(sctx, ino, gen, name);
2314 stop = 1;
2315 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002316 ret = get_first_ref(sctx->parent_root, ino,
2317 &parent_inode, &parent_gen, name);
2318 } else {
2319 ret = __get_cur_name_and_parent(sctx, ino, gen,
2320 &parent_inode,
2321 &parent_gen, name);
2322 if (ret)
2323 stop = 1;
2324 }
2325
Alexander Block31db9f72012-07-25 23:19:24 +02002326 if (ret < 0)
2327 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002328
Alexander Block31db9f72012-07-25 23:19:24 +02002329 ret = fs_path_add_path(dest, name);
2330 if (ret < 0)
2331 goto out;
2332
2333 ino = parent_inode;
2334 gen = parent_gen;
2335 }
2336
2337out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002338 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002339 if (!ret)
2340 fs_path_unreverse(dest);
2341 return ret;
2342}
2343
2344/*
Alexander Block31db9f72012-07-25 23:19:24 +02002345 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2346 */
2347static int send_subvol_begin(struct send_ctx *sctx)
2348{
2349 int ret;
2350 struct btrfs_root *send_root = sctx->send_root;
2351 struct btrfs_root *parent_root = sctx->parent_root;
2352 struct btrfs_path *path;
2353 struct btrfs_key key;
2354 struct btrfs_root_ref *ref;
2355 struct extent_buffer *leaf;
2356 char *name = NULL;
2357 int namelen;
2358
Wang Shilongffcfaf82014-01-15 00:26:43 +08002359 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002360 if (!path)
2361 return -ENOMEM;
2362
David Sterbae780b0d2016-01-18 18:42:13 +01002363 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002364 if (!name) {
2365 btrfs_free_path(path);
2366 return -ENOMEM;
2367 }
2368
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002369 key.objectid = send_root->root_key.objectid;
Alexander Block31db9f72012-07-25 23:19:24 +02002370 key.type = BTRFS_ROOT_BACKREF_KEY;
2371 key.offset = 0;
2372
2373 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2374 &key, path, 1, 0);
2375 if (ret < 0)
2376 goto out;
2377 if (ret) {
2378 ret = -ENOENT;
2379 goto out;
2380 }
2381
2382 leaf = path->nodes[0];
2383 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2384 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002385 key.objectid != send_root->root_key.objectid) {
Alexander Block31db9f72012-07-25 23:19:24 +02002386 ret = -ENOENT;
2387 goto out;
2388 }
2389 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2390 namelen = btrfs_root_ref_name_len(leaf, ref);
2391 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2392 btrfs_release_path(path);
2393
Alexander Block31db9f72012-07-25 23:19:24 +02002394 if (parent_root) {
2395 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2396 if (ret < 0)
2397 goto out;
2398 } else {
2399 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2400 if (ret < 0)
2401 goto out;
2402 }
2403
2404 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002405
2406 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2407 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2408 sctx->send_root->root_item.received_uuid);
2409 else
2410 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2411 sctx->send_root->root_item.uuid);
2412
Alexander Block31db9f72012-07-25 23:19:24 +02002413 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
David Sterba09e3a282020-09-15 14:30:15 +02002414 btrfs_root_ctransid(&sctx->send_root->root_item));
Alexander Block31db9f72012-07-25 23:19:24 +02002415 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002416 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2417 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2418 parent_root->root_item.received_uuid);
2419 else
2420 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2421 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002422 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
David Sterba09e3a282020-09-15 14:30:15 +02002423 btrfs_root_ctransid(&sctx->parent_root->root_item));
Alexander Block31db9f72012-07-25 23:19:24 +02002424 }
2425
2426 ret = send_cmd(sctx);
2427
2428tlv_put_failure:
2429out:
2430 btrfs_free_path(path);
2431 kfree(name);
2432 return ret;
2433}
2434
2435static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2436{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002437 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002438 int ret = 0;
2439 struct fs_path *p;
2440
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002441 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002442
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002443 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002444 if (!p)
2445 return -ENOMEM;
2446
2447 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2448 if (ret < 0)
2449 goto out;
2450
2451 ret = get_cur_path(sctx, ino, gen, p);
2452 if (ret < 0)
2453 goto out;
2454 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2455 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2456
2457 ret = send_cmd(sctx);
2458
2459tlv_put_failure:
2460out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002461 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002462 return ret;
2463}
2464
2465static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2466{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002467 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002468 int ret = 0;
2469 struct fs_path *p;
2470
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002471 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002472
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002473 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002474 if (!p)
2475 return -ENOMEM;
2476
2477 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2478 if (ret < 0)
2479 goto out;
2480
2481 ret = get_cur_path(sctx, ino, gen, p);
2482 if (ret < 0)
2483 goto out;
2484 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2485 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2486
2487 ret = send_cmd(sctx);
2488
2489tlv_put_failure:
2490out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002491 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002492 return ret;
2493}
2494
2495static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2496{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002497 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002498 int ret = 0;
2499 struct fs_path *p;
2500
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002501 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2502 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002503
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002504 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002505 if (!p)
2506 return -ENOMEM;
2507
2508 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2509 if (ret < 0)
2510 goto out;
2511
2512 ret = get_cur_path(sctx, ino, gen, p);
2513 if (ret < 0)
2514 goto out;
2515 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2516 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2517 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2518
2519 ret = send_cmd(sctx);
2520
2521tlv_put_failure:
2522out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002523 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002524 return ret;
2525}
2526
2527static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2528{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002529 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002530 int ret = 0;
2531 struct fs_path *p = NULL;
2532 struct btrfs_inode_item *ii;
2533 struct btrfs_path *path = NULL;
2534 struct extent_buffer *eb;
2535 struct btrfs_key key;
2536 int slot;
2537
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002538 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002539
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002540 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002541 if (!p)
2542 return -ENOMEM;
2543
2544 path = alloc_path_for_send();
2545 if (!path) {
2546 ret = -ENOMEM;
2547 goto out;
2548 }
2549
2550 key.objectid = ino;
2551 key.type = BTRFS_INODE_ITEM_KEY;
2552 key.offset = 0;
2553 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002554 if (ret > 0)
2555 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002556 if (ret < 0)
2557 goto out;
2558
2559 eb = path->nodes[0];
2560 slot = path->slots[0];
2561 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2562
2563 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2564 if (ret < 0)
2565 goto out;
2566
2567 ret = get_cur_path(sctx, ino, gen, p);
2568 if (ret < 0)
2569 goto out;
2570 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002571 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2572 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2573 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002574 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002575
2576 ret = send_cmd(sctx);
2577
2578tlv_put_failure:
2579out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002580 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002581 btrfs_free_path(path);
2582 return ret;
2583}
2584
2585/*
2586 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2587 * a valid path yet because we did not process the refs yet. So, the inode
2588 * is created as orphan.
2589 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002590static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002591{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002592 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002593 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002594 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002595 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002596 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002597 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002598 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002599
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002600 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002601
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002602 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002603 if (!p)
2604 return -ENOMEM;
2605
Liu Bo644d1942014-02-27 17:29:01 +08002606 if (ino != sctx->cur_ino) {
2607 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2608 NULL, NULL, &rdev);
2609 if (ret < 0)
2610 goto out;
2611 } else {
2612 gen = sctx->cur_inode_gen;
2613 mode = sctx->cur_inode_mode;
2614 rdev = sctx->cur_inode_rdev;
2615 }
Alexander Block31db9f72012-07-25 23:19:24 +02002616
Alexander Blocke938c8a2012-07-28 16:33:49 +02002617 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002618 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002619 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002620 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002621 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002622 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002623 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002624 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002625 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002626 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002627 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002628 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002629 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002630 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002631 (int)(mode & S_IFMT));
Tsutomu Itohca6842b2016-01-22 09:13:25 +09002632 ret = -EOPNOTSUPP;
Alexander Block31db9f72012-07-25 23:19:24 +02002633 goto out;
2634 }
2635
2636 ret = begin_cmd(sctx, cmd);
2637 if (ret < 0)
2638 goto out;
2639
Alexander Block1f4692d2012-07-28 10:42:24 +02002640 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002641 if (ret < 0)
2642 goto out;
2643
2644 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002645 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002646
2647 if (S_ISLNK(mode)) {
2648 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002649 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002650 if (ret < 0)
2651 goto out;
2652 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2653 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2654 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002655 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2656 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002657 }
2658
2659 ret = send_cmd(sctx);
2660 if (ret < 0)
2661 goto out;
2662
2663
2664tlv_put_failure:
2665out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002666 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002667 return ret;
2668}
2669
Alexander Block1f4692d2012-07-28 10:42:24 +02002670/*
2671 * We need some special handling for inodes that get processed before the parent
2672 * directory got created. See process_recorded_refs for details.
2673 * This function does the check if we already created the dir out of order.
2674 */
2675static int did_create_dir(struct send_ctx *sctx, u64 dir)
2676{
2677 int ret = 0;
2678 struct btrfs_path *path = NULL;
2679 struct btrfs_key key;
2680 struct btrfs_key found_key;
2681 struct btrfs_key di_key;
2682 struct extent_buffer *eb;
2683 struct btrfs_dir_item *di;
2684 int slot;
2685
2686 path = alloc_path_for_send();
2687 if (!path) {
2688 ret = -ENOMEM;
2689 goto out;
2690 }
2691
2692 key.objectid = dir;
2693 key.type = BTRFS_DIR_INDEX_KEY;
2694 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002695 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2696 if (ret < 0)
2697 goto out;
2698
Alexander Block1f4692d2012-07-28 10:42:24 +02002699 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002700 eb = path->nodes[0];
2701 slot = path->slots[0];
2702 if (slot >= btrfs_header_nritems(eb)) {
2703 ret = btrfs_next_leaf(sctx->send_root, path);
2704 if (ret < 0) {
2705 goto out;
2706 } else if (ret > 0) {
2707 ret = 0;
2708 break;
2709 }
2710 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002711 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002712
2713 btrfs_item_key_to_cpu(eb, &found_key, slot);
2714 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002715 found_key.type != key.type) {
2716 ret = 0;
2717 goto out;
2718 }
2719
2720 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2721 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2722
Josef Bacika0525412013-08-12 10:56:14 -04002723 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2724 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002725 ret = 1;
2726 goto out;
2727 }
2728
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002729 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002730 }
2731
2732out:
2733 btrfs_free_path(path);
2734 return ret;
2735}
2736
2737/*
2738 * Only creates the inode if it is:
2739 * 1. Not a directory
2740 * 2. Or a directory which was not created already due to out of order
2741 * directories. See did_create_dir and process_recorded_refs for details.
2742 */
2743static int send_create_inode_if_needed(struct send_ctx *sctx)
2744{
2745 int ret;
2746
2747 if (S_ISDIR(sctx->cur_inode_mode)) {
2748 ret = did_create_dir(sctx, sctx->cur_ino);
2749 if (ret < 0)
2750 goto out;
2751 if (ret) {
2752 ret = 0;
2753 goto out;
2754 }
2755 }
2756
2757 ret = send_create_inode(sctx, sctx->cur_ino);
2758 if (ret < 0)
2759 goto out;
2760
2761out:
2762 return ret;
2763}
2764
Alexander Block31db9f72012-07-25 23:19:24 +02002765struct recorded_ref {
2766 struct list_head list;
Alexander Block31db9f72012-07-25 23:19:24 +02002767 char *name;
2768 struct fs_path *full_path;
2769 u64 dir;
2770 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002771 int name_len;
2772};
2773
Filipe Mananafdb13882017-06-13 14:13:11 +01002774static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2775{
2776 ref->full_path = path;
2777 ref->name = (char *)kbasename(ref->full_path->start);
2778 ref->name_len = ref->full_path->end - ref->name;
2779}
2780
Alexander Block31db9f72012-07-25 23:19:24 +02002781/*
2782 * We need to process new refs before deleted refs, but compare_tree gives us
2783 * everything mixed. So we first record all refs and later process them.
2784 * This function is a helper to record one ref.
2785 */
Liu Boa4d96d62014-03-03 21:31:03 +08002786static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002787 u64 dir_gen, struct fs_path *path)
2788{
2789 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002790
David Sterbae780b0d2016-01-18 18:42:13 +01002791 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002792 if (!ref)
2793 return -ENOMEM;
2794
2795 ref->dir = dir;
2796 ref->dir_gen = dir_gen;
Filipe Mananafdb13882017-06-13 14:13:11 +01002797 set_ref_path(ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02002798 list_add_tail(&ref->list, head);
2799 return 0;
2800}
2801
Josef Bacikba5e8f22013-08-16 16:52:55 -04002802static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2803{
2804 struct recorded_ref *new;
2805
David Sterbae780b0d2016-01-18 18:42:13 +01002806 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002807 if (!new)
2808 return -ENOMEM;
2809
2810 new->dir = ref->dir;
2811 new->dir_gen = ref->dir_gen;
2812 new->full_path = NULL;
2813 INIT_LIST_HEAD(&new->list);
2814 list_add_tail(&new->list, list);
2815 return 0;
2816}
2817
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002818static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002819{
2820 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002821
Alexander Blocke938c8a2012-07-28 16:33:49 +02002822 while (!list_empty(head)) {
2823 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002824 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002825 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002826 kfree(cur);
2827 }
Alexander Block31db9f72012-07-25 23:19:24 +02002828}
2829
2830static void free_recorded_refs(struct send_ctx *sctx)
2831{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002832 __free_recorded_refs(&sctx->new_refs);
2833 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002834}
2835
2836/*
Alexander Block766702e2012-07-28 14:11:31 +02002837 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002838 * ref of an unprocessed inode gets overwritten and for all non empty
2839 * directories.
2840 */
2841static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2842 struct fs_path *path)
2843{
2844 int ret;
2845 struct fs_path *orphan;
2846
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002847 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002848 if (!orphan)
2849 return -ENOMEM;
2850
2851 ret = gen_unique_name(sctx, ino, gen, orphan);
2852 if (ret < 0)
2853 goto out;
2854
2855 ret = send_rename(sctx, path, orphan);
2856
2857out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002858 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002859 return ret;
2860}
2861
Filipe Manana0b3f4072020-12-10 12:09:02 +00002862static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
2863 u64 dir_ino, u64 dir_gen)
Filipe Manana9dc44212014-02-19 14:31:44 +00002864{
2865 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2866 struct rb_node *parent = NULL;
2867 struct orphan_dir_info *entry, *odi;
2868
Filipe Manana9dc44212014-02-19 14:31:44 +00002869 while (*p) {
2870 parent = *p;
2871 entry = rb_entry(parent, struct orphan_dir_info, node);
Filipe Manana0b3f4072020-12-10 12:09:02 +00002872 if (dir_ino < entry->ino)
Filipe Manana9dc44212014-02-19 14:31:44 +00002873 p = &(*p)->rb_left;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002874 else if (dir_ino > entry->ino)
Filipe Manana9dc44212014-02-19 14:31:44 +00002875 p = &(*p)->rb_right;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002876 else if (dir_gen < entry->gen)
2877 p = &(*p)->rb_left;
2878 else if (dir_gen > entry->gen)
2879 p = &(*p)->rb_right;
2880 else
Filipe Manana9dc44212014-02-19 14:31:44 +00002881 return entry;
Filipe Manana9dc44212014-02-19 14:31:44 +00002882 }
2883
Robbie Ko35c8eda2018-05-08 18:11:37 +08002884 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2885 if (!odi)
2886 return ERR_PTR(-ENOMEM);
2887 odi->ino = dir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002888 odi->gen = dir_gen;
Robbie Ko0f96f512018-05-08 18:11:38 +08002889 odi->last_dir_index_offset = 0;
Robbie Ko35c8eda2018-05-08 18:11:37 +08002890
Filipe Manana9dc44212014-02-19 14:31:44 +00002891 rb_link_node(&odi->node, parent, p);
2892 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2893 return odi;
2894}
2895
Filipe Manana0b3f4072020-12-10 12:09:02 +00002896static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
2897 u64 dir_ino, u64 gen)
Filipe Manana9dc44212014-02-19 14:31:44 +00002898{
2899 struct rb_node *n = sctx->orphan_dirs.rb_node;
2900 struct orphan_dir_info *entry;
2901
2902 while (n) {
2903 entry = rb_entry(n, struct orphan_dir_info, node);
2904 if (dir_ino < entry->ino)
2905 n = n->rb_left;
2906 else if (dir_ino > entry->ino)
2907 n = n->rb_right;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002908 else if (gen < entry->gen)
2909 n = n->rb_left;
2910 else if (gen > entry->gen)
2911 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002912 else
2913 return entry;
2914 }
2915 return NULL;
2916}
2917
Filipe Manana0b3f4072020-12-10 12:09:02 +00002918static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
Filipe Manana9dc44212014-02-19 14:31:44 +00002919{
Filipe Manana0b3f4072020-12-10 12:09:02 +00002920 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
Filipe Manana9dc44212014-02-19 14:31:44 +00002921
2922 return odi != NULL;
2923}
2924
2925static void free_orphan_dir_info(struct send_ctx *sctx,
2926 struct orphan_dir_info *odi)
2927{
2928 if (!odi)
2929 return;
2930 rb_erase(&odi->node, &sctx->orphan_dirs);
2931 kfree(odi);
2932}
2933
Alexander Block31db9f72012-07-25 23:19:24 +02002934/*
2935 * Returns 1 if a directory can be removed at this point in time.
2936 * We check this by iterating all dir items and checking if the inode behind
2937 * the dir item was already processed.
2938 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002939static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2940 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002941{
2942 int ret = 0;
2943 struct btrfs_root *root = sctx->parent_root;
2944 struct btrfs_path *path;
2945 struct btrfs_key key;
2946 struct btrfs_key found_key;
2947 struct btrfs_key loc;
2948 struct btrfs_dir_item *di;
Robbie Ko0f96f512018-05-08 18:11:38 +08002949 struct orphan_dir_info *odi = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02002950
Alexander Block6d85ed052012-08-01 14:48:59 +02002951 /*
2952 * Don't try to rmdir the top/root subvolume dir.
2953 */
2954 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2955 return 0;
2956
Alexander Block31db9f72012-07-25 23:19:24 +02002957 path = alloc_path_for_send();
2958 if (!path)
2959 return -ENOMEM;
2960
2961 key.objectid = dir;
2962 key.type = BTRFS_DIR_INDEX_KEY;
2963 key.offset = 0;
Robbie Ko0f96f512018-05-08 18:11:38 +08002964
Filipe Manana0b3f4072020-12-10 12:09:02 +00002965 odi = get_orphan_dir_info(sctx, dir, dir_gen);
Robbie Ko0f96f512018-05-08 18:11:38 +08002966 if (odi)
2967 key.offset = odi->last_dir_index_offset;
2968
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002969 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2970 if (ret < 0)
2971 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002972
2973 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002974 struct waiting_dir_move *dm;
2975
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002976 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2977 ret = btrfs_next_leaf(root, path);
2978 if (ret < 0)
2979 goto out;
2980 else if (ret > 0)
2981 break;
2982 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002983 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002984 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2985 path->slots[0]);
2986 if (found_key.objectid != key.objectid ||
2987 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002988 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002989
2990 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2991 struct btrfs_dir_item);
2992 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2993
Filipe Manana9dc44212014-02-19 14:31:44 +00002994 dm = get_waiting_dir_move(sctx, loc.objectid);
2995 if (dm) {
Filipe Manana0b3f4072020-12-10 12:09:02 +00002996 odi = add_orphan_dir_info(sctx, dir, dir_gen);
Filipe Manana9dc44212014-02-19 14:31:44 +00002997 if (IS_ERR(odi)) {
2998 ret = PTR_ERR(odi);
2999 goto out;
3000 }
3001 odi->gen = dir_gen;
Robbie Ko0f96f512018-05-08 18:11:38 +08003002 odi->last_dir_index_offset = found_key.offset;
Filipe Manana9dc44212014-02-19 14:31:44 +00003003 dm->rmdir_ino = dir;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003004 dm->rmdir_gen = dir_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003005 ret = 0;
3006 goto out;
3007 }
3008
Alexander Block31db9f72012-07-25 23:19:24 +02003009 if (loc.objectid > send_progress) {
Filipe Manana0b3f4072020-12-10 12:09:02 +00003010 odi = add_orphan_dir_info(sctx, dir, dir_gen);
Robbie Ko0f96f512018-05-08 18:11:38 +08003011 if (IS_ERR(odi)) {
3012 ret = PTR_ERR(odi);
3013 goto out;
3014 }
3015 odi->gen = dir_gen;
3016 odi->last_dir_index_offset = found_key.offset;
Alexander Block31db9f72012-07-25 23:19:24 +02003017 ret = 0;
3018 goto out;
3019 }
3020
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003021 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003022 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003023 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02003024
3025 ret = 1;
3026
3027out:
3028 btrfs_free_path(path);
3029 return ret;
3030}
3031
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003032static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3033{
Filipe Manana9dc44212014-02-19 14:31:44 +00003034 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003035
Filipe Manana9dc44212014-02-19 14:31:44 +00003036 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003037}
3038
Filipe Manana8b191a62015-04-09 14:09:14 +01003039static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003040{
3041 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3042 struct rb_node *parent = NULL;
3043 struct waiting_dir_move *entry, *dm;
3044
David Sterbae780b0d2016-01-18 18:42:13 +01003045 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003046 if (!dm)
3047 return -ENOMEM;
3048 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003049 dm->rmdir_ino = 0;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003050 dm->rmdir_gen = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003051 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003052
3053 while (*p) {
3054 parent = *p;
3055 entry = rb_entry(parent, struct waiting_dir_move, node);
3056 if (ino < entry->ino) {
3057 p = &(*p)->rb_left;
3058 } else if (ino > entry->ino) {
3059 p = &(*p)->rb_right;
3060 } else {
3061 kfree(dm);
3062 return -EEXIST;
3063 }
3064 }
3065
3066 rb_link_node(&dm->node, parent, p);
3067 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3068 return 0;
3069}
3070
Filipe Manana9dc44212014-02-19 14:31:44 +00003071static struct waiting_dir_move *
3072get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003073{
3074 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3075 struct waiting_dir_move *entry;
3076
3077 while (n) {
3078 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003079 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003080 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003081 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003082 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003083 else
3084 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003085 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003086 return NULL;
3087}
3088
3089static void free_waiting_dir_move(struct send_ctx *sctx,
3090 struct waiting_dir_move *dm)
3091{
3092 if (!dm)
3093 return;
3094 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3095 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003096}
3097
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003098static int add_pending_dir_move(struct send_ctx *sctx,
3099 u64 ino,
3100 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003101 u64 parent_ino,
3102 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003103 struct list_head *deleted_refs,
3104 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003105{
3106 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3107 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003108 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003109 struct recorded_ref *cur;
3110 int exists = 0;
3111 int ret;
3112
David Sterbae780b0d2016-01-18 18:42:13 +01003113 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003114 if (!pm)
3115 return -ENOMEM;
3116 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003117 pm->ino = ino;
3118 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003119 INIT_LIST_HEAD(&pm->list);
3120 INIT_LIST_HEAD(&pm->update_refs);
3121 RB_CLEAR_NODE(&pm->node);
3122
3123 while (*p) {
3124 parent = *p;
3125 entry = rb_entry(parent, struct pending_dir_move, node);
3126 if (parent_ino < entry->parent_ino) {
3127 p = &(*p)->rb_left;
3128 } else if (parent_ino > entry->parent_ino) {
3129 p = &(*p)->rb_right;
3130 } else {
3131 exists = 1;
3132 break;
3133 }
3134 }
3135
Filipe Mananaf9594922014-03-27 20:14:01 +00003136 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003137 ret = dup_ref(cur, &pm->update_refs);
3138 if (ret < 0)
3139 goto out;
3140 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003141 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003142 ret = dup_ref(cur, &pm->update_refs);
3143 if (ret < 0)
3144 goto out;
3145 }
3146
Filipe Manana8b191a62015-04-09 14:09:14 +01003147 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003148 if (ret)
3149 goto out;
3150
3151 if (exists) {
3152 list_add_tail(&pm->list, &entry->list);
3153 } else {
3154 rb_link_node(&pm->node, parent, p);
3155 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3156 }
3157 ret = 0;
3158out:
3159 if (ret) {
3160 __free_recorded_refs(&pm->update_refs);
3161 kfree(pm);
3162 }
3163 return ret;
3164}
3165
3166static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3167 u64 parent_ino)
3168{
3169 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3170 struct pending_dir_move *entry;
3171
3172 while (n) {
3173 entry = rb_entry(n, struct pending_dir_move, node);
3174 if (parent_ino < entry->parent_ino)
3175 n = n->rb_left;
3176 else if (parent_ino > entry->parent_ino)
3177 n = n->rb_right;
3178 else
3179 return entry;
3180 }
3181 return NULL;
3182}
3183
Robbie Ko801bec32015-06-23 18:39:46 +08003184static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3185 u64 ino, u64 gen, u64 *ancestor_ino)
3186{
3187 int ret = 0;
3188 u64 parent_inode = 0;
3189 u64 parent_gen = 0;
3190 u64 start_ino = ino;
3191
3192 *ancestor_ino = 0;
3193 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3194 fs_path_reset(name);
3195
Filipe Manana0b3f4072020-12-10 12:09:02 +00003196 if (is_waiting_for_rm(sctx, ino, gen))
Robbie Ko801bec32015-06-23 18:39:46 +08003197 break;
3198 if (is_waiting_for_move(sctx, ino)) {
3199 if (*ancestor_ino == 0)
3200 *ancestor_ino = ino;
3201 ret = get_first_ref(sctx->parent_root, ino,
3202 &parent_inode, &parent_gen, name);
3203 } else {
3204 ret = __get_cur_name_and_parent(sctx, ino, gen,
3205 &parent_inode,
3206 &parent_gen, name);
3207 if (ret > 0) {
3208 ret = 0;
3209 break;
3210 }
3211 }
3212 if (ret < 0)
3213 break;
3214 if (parent_inode == start_ino) {
3215 ret = 1;
3216 if (*ancestor_ino == 0)
3217 *ancestor_ino = ino;
3218 break;
3219 }
3220 ino = parent_inode;
3221 gen = parent_gen;
3222 }
3223 return ret;
3224}
3225
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003226static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3227{
3228 struct fs_path *from_path = NULL;
3229 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003230 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003231 u64 orig_progress = sctx->send_progress;
3232 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003233 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003234 struct waiting_dir_move *dm = NULL;
3235 u64 rmdir_ino = 0;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003236 u64 rmdir_gen;
Robbie Ko801bec32015-06-23 18:39:46 +08003237 u64 ancestor;
3238 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003239 int ret;
3240
Filipe Manana2b863a12014-02-16 13:43:11 +00003241 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003242 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003243 if (!name || !from_path) {
3244 ret = -ENOMEM;
3245 goto out;
3246 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003247
Filipe Manana9dc44212014-02-19 14:31:44 +00003248 dm = get_waiting_dir_move(sctx, pm->ino);
3249 ASSERT(dm);
3250 rmdir_ino = dm->rmdir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003251 rmdir_gen = dm->rmdir_gen;
Robbie Ko801bec32015-06-23 18:39:46 +08003252 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003253 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003254
Robbie Ko801bec32015-06-23 18:39:46 +08003255 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003256 ret = gen_unique_name(sctx, pm->ino,
3257 pm->gen, from_path);
3258 } else {
3259 ret = get_first_ref(sctx->parent_root, pm->ino,
3260 &parent_ino, &parent_gen, name);
3261 if (ret < 0)
3262 goto out;
3263 ret = get_cur_path(sctx, parent_ino, parent_gen,
3264 from_path);
3265 if (ret < 0)
3266 goto out;
3267 ret = fs_path_add_path(from_path, name);
3268 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003269 if (ret < 0)
3270 goto out;
3271
Filipe Mananaf9594922014-03-27 20:14:01 +00003272 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003273 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003274 if (ret < 0)
3275 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003276 if (ret) {
3277 LIST_HEAD(deleted_refs);
3278 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3279 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3280 &pm->update_refs, &deleted_refs,
3281 is_orphan);
3282 if (ret < 0)
3283 goto out;
3284 if (rmdir_ino) {
3285 dm = get_waiting_dir_move(sctx, pm->ino);
3286 ASSERT(dm);
3287 dm->rmdir_ino = rmdir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003288 dm->rmdir_gen = rmdir_gen;
Robbie Ko801bec32015-06-23 18:39:46 +08003289 }
3290 goto out;
3291 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003292 fs_path_reset(name);
3293 to_path = name;
3294 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003295 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3296 if (ret < 0)
3297 goto out;
3298
3299 ret = send_rename(sctx, from_path, to_path);
3300 if (ret < 0)
3301 goto out;
3302
Filipe Manana9dc44212014-02-19 14:31:44 +00003303 if (rmdir_ino) {
3304 struct orphan_dir_info *odi;
Robbie Ko0f96f512018-05-08 18:11:38 +08003305 u64 gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003306
Filipe Manana0b3f4072020-12-10 12:09:02 +00003307 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
Filipe Manana9dc44212014-02-19 14:31:44 +00003308 if (!odi) {
3309 /* already deleted */
3310 goto finish;
3311 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003312 gen = odi->gen;
3313
3314 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003315 if (ret < 0)
3316 goto out;
3317 if (!ret)
3318 goto finish;
3319
3320 name = fs_path_alloc();
3321 if (!name) {
3322 ret = -ENOMEM;
3323 goto out;
3324 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003325 ret = get_cur_path(sctx, rmdir_ino, gen, name);
Filipe Manana9dc44212014-02-19 14:31:44 +00003326 if (ret < 0)
3327 goto out;
3328 ret = send_rmdir(sctx, name);
3329 if (ret < 0)
3330 goto out;
Filipe Manana9dc44212014-02-19 14:31:44 +00003331 }
3332
3333finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003334 ret = send_utimes(sctx, pm->ino, pm->gen);
3335 if (ret < 0)
3336 goto out;
3337
3338 /*
3339 * After rename/move, need to update the utimes of both new parent(s)
3340 * and old parent(s).
3341 */
3342 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003343 /*
3344 * The parent inode might have been deleted in the send snapshot
3345 */
3346 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3347 NULL, NULL, NULL, NULL, NULL);
3348 if (ret == -ENOENT) {
3349 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003350 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003351 }
3352 if (ret < 0)
3353 goto out;
3354
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003355 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3356 if (ret < 0)
3357 goto out;
3358 }
3359
3360out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003361 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003362 fs_path_free(from_path);
3363 fs_path_free(to_path);
3364 sctx->send_progress = orig_progress;
3365
3366 return ret;
3367}
3368
3369static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3370{
3371 if (!list_empty(&m->list))
3372 list_del(&m->list);
3373 if (!RB_EMPTY_NODE(&m->node))
3374 rb_erase(&m->node, &sctx->pending_dir_moves);
3375 __free_recorded_refs(&m->update_refs);
3376 kfree(m);
3377}
3378
Robbie Koa4390ae2018-11-14 18:32:37 +00003379static void tail_append_pending_moves(struct send_ctx *sctx,
3380 struct pending_dir_move *moves,
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003381 struct list_head *stack)
3382{
3383 if (list_empty(&moves->list)) {
3384 list_add_tail(&moves->list, stack);
3385 } else {
3386 LIST_HEAD(list);
3387 list_splice_init(&moves->list, &list);
3388 list_add_tail(&moves->list, stack);
3389 list_splice_tail(&list, stack);
3390 }
Robbie Koa4390ae2018-11-14 18:32:37 +00003391 if (!RB_EMPTY_NODE(&moves->node)) {
3392 rb_erase(&moves->node, &sctx->pending_dir_moves);
3393 RB_CLEAR_NODE(&moves->node);
3394 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003395}
3396
3397static int apply_children_dir_moves(struct send_ctx *sctx)
3398{
3399 struct pending_dir_move *pm;
3400 struct list_head stack;
3401 u64 parent_ino = sctx->cur_ino;
3402 int ret = 0;
3403
3404 pm = get_pending_dir_moves(sctx, parent_ino);
3405 if (!pm)
3406 return 0;
3407
3408 INIT_LIST_HEAD(&stack);
Robbie Koa4390ae2018-11-14 18:32:37 +00003409 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003410
3411 while (!list_empty(&stack)) {
3412 pm = list_first_entry(&stack, struct pending_dir_move, list);
3413 parent_ino = pm->ino;
3414 ret = apply_dir_move(sctx, pm);
3415 free_pending_move(sctx, pm);
3416 if (ret)
3417 goto out;
3418 pm = get_pending_dir_moves(sctx, parent_ino);
3419 if (pm)
Robbie Koa4390ae2018-11-14 18:32:37 +00003420 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003421 }
3422 return 0;
3423
3424out:
3425 while (!list_empty(&stack)) {
3426 pm = list_first_entry(&stack, struct pending_dir_move, list);
3427 free_pending_move(sctx, pm);
3428 }
3429 return ret;
3430}
3431
Filipe Manana84471e22015-02-28 22:29:22 +00003432/*
3433 * We might need to delay a directory rename even when no ancestor directory
3434 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3435 * renamed. This happens when we rename a directory to the old name (the name
3436 * in the parent root) of some other unrelated directory that got its rename
3437 * delayed due to some ancestor with higher number that got renamed.
3438 *
3439 * Example:
3440 *
3441 * Parent snapshot:
3442 * . (ino 256)
3443 * |---- a/ (ino 257)
3444 * | |---- file (ino 260)
3445 * |
3446 * |---- b/ (ino 258)
3447 * |---- c/ (ino 259)
3448 *
3449 * Send snapshot:
3450 * . (ino 256)
3451 * |---- a/ (ino 258)
3452 * |---- x/ (ino 259)
3453 * |---- y/ (ino 257)
3454 * |----- file (ino 260)
3455 *
3456 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3457 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3458 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3459 * must issue is:
3460 *
3461 * 1 - rename 259 from 'c' to 'x'
3462 * 2 - rename 257 from 'a' to 'x/y'
3463 * 3 - rename 258 from 'b' to 'a'
3464 *
3465 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3466 * be done right away and < 0 on error.
3467 */
3468static int wait_for_dest_dir_move(struct send_ctx *sctx,
3469 struct recorded_ref *parent_ref,
3470 const bool is_orphan)
3471{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003472 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003473 struct btrfs_path *path;
3474 struct btrfs_key key;
3475 struct btrfs_key di_key;
3476 struct btrfs_dir_item *di;
3477 u64 left_gen;
3478 u64 right_gen;
3479 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003480 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003481
3482 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3483 return 0;
3484
3485 path = alloc_path_for_send();
3486 if (!path)
3487 return -ENOMEM;
3488
3489 key.objectid = parent_ref->dir;
3490 key.type = BTRFS_DIR_ITEM_KEY;
3491 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3492
3493 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3494 if (ret < 0) {
3495 goto out;
3496 } else if (ret > 0) {
3497 ret = 0;
3498 goto out;
3499 }
3500
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003501 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3502 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003503 if (!di) {
3504 ret = 0;
3505 goto out;
3506 }
3507 /*
3508 * di_key.objectid has the number of the inode that has a dentry in the
3509 * parent directory with the same name that sctx->cur_ino is being
3510 * renamed to. We need to check if that inode is in the send root as
3511 * well and if it is currently marked as an inode with a pending rename,
3512 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3513 * that it happens after that other inode is renamed.
3514 */
3515 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3516 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3517 ret = 0;
3518 goto out;
3519 }
3520
3521 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3522 &left_gen, NULL, NULL, NULL, NULL);
3523 if (ret < 0)
3524 goto out;
3525 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3526 &right_gen, NULL, NULL, NULL, NULL);
3527 if (ret < 0) {
3528 if (ret == -ENOENT)
3529 ret = 0;
3530 goto out;
3531 }
3532
3533 /* Different inode, no need to delay the rename of sctx->cur_ino */
3534 if (right_gen != left_gen) {
3535 ret = 0;
3536 goto out;
3537 }
3538
Robbie Ko801bec32015-06-23 18:39:46 +08003539 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3540 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003541 ret = add_pending_dir_move(sctx,
3542 sctx->cur_ino,
3543 sctx->cur_inode_gen,
3544 di_key.objectid,
3545 &sctx->new_refs,
3546 &sctx->deleted_refs,
3547 is_orphan);
3548 if (!ret)
3549 ret = 1;
3550 }
3551out:
3552 btrfs_free_path(path);
3553 return ret;
3554}
3555
Filipe Manana80aa6022015-03-27 17:50:45 +00003556/*
Filipe Mananaea37d592017-11-17 01:54:00 +00003557 * Check if inode ino2, or any of its ancestors, is inode ino1.
3558 * Return 1 if true, 0 if false and < 0 on error.
3559 */
3560static int check_ino_in_path(struct btrfs_root *root,
3561 const u64 ino1,
3562 const u64 ino1_gen,
3563 const u64 ino2,
3564 const u64 ino2_gen,
3565 struct fs_path *fs_path)
3566{
3567 u64 ino = ino2;
3568
3569 if (ino1 == ino2)
3570 return ino1_gen == ino2_gen;
3571
3572 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3573 u64 parent;
3574 u64 parent_gen;
3575 int ret;
3576
3577 fs_path_reset(fs_path);
3578 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3579 if (ret < 0)
3580 return ret;
3581 if (parent == ino1)
3582 return parent_gen == ino1_gen;
3583 ino = parent;
3584 }
3585 return 0;
3586}
3587
3588/*
3589 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3590 * possible path (in case ino2 is not a directory and has multiple hard links).
Filipe Manana80aa6022015-03-27 17:50:45 +00003591 * Return 1 if true, 0 if false and < 0 on error.
3592 */
3593static int is_ancestor(struct btrfs_root *root,
3594 const u64 ino1,
3595 const u64 ino1_gen,
3596 const u64 ino2,
3597 struct fs_path *fs_path)
3598{
Filipe Mananaea37d592017-11-17 01:54:00 +00003599 bool free_fs_path = false;
Filipe Manana72c36682017-06-07 11:41:29 +01003600 int ret = 0;
Filipe Mananaea37d592017-11-17 01:54:00 +00003601 struct btrfs_path *path = NULL;
3602 struct btrfs_key key;
Filipe Manana72c36682017-06-07 11:41:29 +01003603
3604 if (!fs_path) {
3605 fs_path = fs_path_alloc();
3606 if (!fs_path)
3607 return -ENOMEM;
Filipe Mananaea37d592017-11-17 01:54:00 +00003608 free_fs_path = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003609 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003610
Filipe Mananaea37d592017-11-17 01:54:00 +00003611 path = alloc_path_for_send();
3612 if (!path) {
3613 ret = -ENOMEM;
3614 goto out;
Filipe Manana80aa6022015-03-27 17:50:45 +00003615 }
Filipe Mananaea37d592017-11-17 01:54:00 +00003616
3617 key.objectid = ino2;
3618 key.type = BTRFS_INODE_REF_KEY;
3619 key.offset = 0;
3620
3621 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3622 if (ret < 0)
3623 goto out;
3624
3625 while (true) {
3626 struct extent_buffer *leaf = path->nodes[0];
3627 int slot = path->slots[0];
3628 u32 cur_offset = 0;
3629 u32 item_size;
3630
3631 if (slot >= btrfs_header_nritems(leaf)) {
3632 ret = btrfs_next_leaf(root, path);
3633 if (ret < 0)
3634 goto out;
3635 if (ret > 0)
3636 break;
3637 continue;
3638 }
3639
3640 btrfs_item_key_to_cpu(leaf, &key, slot);
3641 if (key.objectid != ino2)
3642 break;
3643 if (key.type != BTRFS_INODE_REF_KEY &&
3644 key.type != BTRFS_INODE_EXTREF_KEY)
3645 break;
3646
3647 item_size = btrfs_item_size_nr(leaf, slot);
3648 while (cur_offset < item_size) {
3649 u64 parent;
3650 u64 parent_gen;
3651
3652 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3653 unsigned long ptr;
3654 struct btrfs_inode_extref *extref;
3655
3656 ptr = btrfs_item_ptr_offset(leaf, slot);
3657 extref = (struct btrfs_inode_extref *)
3658 (ptr + cur_offset);
3659 parent = btrfs_inode_extref_parent(leaf,
3660 extref);
3661 cur_offset += sizeof(*extref);
3662 cur_offset += btrfs_inode_extref_name_len(leaf,
3663 extref);
3664 } else {
3665 parent = key.offset;
3666 cur_offset = item_size;
3667 }
3668
3669 ret = get_inode_info(root, parent, NULL, &parent_gen,
3670 NULL, NULL, NULL, NULL);
3671 if (ret < 0)
3672 goto out;
3673 ret = check_ino_in_path(root, ino1, ino1_gen,
3674 parent, parent_gen, fs_path);
3675 if (ret)
3676 goto out;
3677 }
3678 path->slots[0]++;
3679 }
3680 ret = 0;
Filipe Manana72c36682017-06-07 11:41:29 +01003681 out:
Filipe Mananaea37d592017-11-17 01:54:00 +00003682 btrfs_free_path(path);
3683 if (free_fs_path)
Filipe Manana72c36682017-06-07 11:41:29 +01003684 fs_path_free(fs_path);
3685 return ret;
Filipe Manana80aa6022015-03-27 17:50:45 +00003686}
3687
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003688static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003689 struct recorded_ref *parent_ref,
3690 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003691{
Filipe Mananaf9594922014-03-27 20:14:01 +00003692 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003693 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003694 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003695 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003696 struct fs_path *path_before = NULL;
3697 struct fs_path *path_after = NULL;
3698 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003699
3700 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003701 path_before = fs_path_alloc();
3702 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003703 ret = -ENOMEM;
3704 goto out;
3705 }
3706
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003707 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003708 * Our current directory inode may not yet be renamed/moved because some
3709 * ancestor (immediate or not) has to be renamed/moved first. So find if
3710 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003711 * that ancestor is processed to avoid path build infinite loops (done
3712 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003713 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003714 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003715 u64 parent_ino_after_gen;
3716
Filipe Mananaf9594922014-03-27 20:14:01 +00003717 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003718 /*
3719 * If the current inode is an ancestor of ino in the
3720 * parent root, we need to delay the rename of the
3721 * current inode, otherwise don't delayed the rename
3722 * because we can end up with a circular dependency
3723 * of renames, resulting in some directories never
3724 * getting the respective rename operations issued in
3725 * the send stream or getting into infinite path build
3726 * loops.
3727 */
3728 ret = is_ancestor(sctx->parent_root,
3729 sctx->cur_ino, sctx->cur_inode_gen,
3730 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003731 if (ret)
3732 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003733 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003734
3735 fs_path_reset(path_before);
3736 fs_path_reset(path_after);
3737
3738 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003739 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003740 if (ret < 0)
3741 goto out;
3742 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3743 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003744 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003745 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003746 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003747 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003748 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003749 }
3750
3751 len1 = fs_path_len(path_before);
3752 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003753 if (ino > sctx->cur_ino &&
3754 (parent_ino_before != parent_ino_after || len1 != len2 ||
3755 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003756 u64 parent_ino_gen;
3757
3758 ret = get_inode_info(sctx->parent_root, ino, NULL,
3759 &parent_ino_gen, NULL, NULL, NULL,
3760 NULL);
3761 if (ret < 0)
3762 goto out;
3763 if (ino_gen == parent_ino_gen) {
3764 ret = 1;
3765 break;
3766 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003767 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003768 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003769 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003770 }
3771
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003772out:
3773 fs_path_free(path_before);
3774 fs_path_free(path_after);
3775
Filipe Mananaf9594922014-03-27 20:14:01 +00003776 if (ret == 1) {
3777 ret = add_pending_dir_move(sctx,
3778 sctx->cur_ino,
3779 sctx->cur_inode_gen,
3780 ino,
3781 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003782 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003783 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003784 if (!ret)
3785 ret = 1;
3786 }
3787
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003788 return ret;
3789}
3790
Filipe Mananaf5962782017-06-22 20:03:51 +01003791static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3792{
3793 int ret;
3794 struct fs_path *new_path;
3795
3796 /*
3797 * Our reference's name member points to its full_path member string, so
3798 * we use here a new path.
3799 */
3800 new_path = fs_path_alloc();
3801 if (!new_path)
3802 return -ENOMEM;
3803
3804 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3805 if (ret < 0) {
3806 fs_path_free(new_path);
3807 return ret;
3808 }
3809 ret = fs_path_add(new_path, ref->name, ref->name_len);
3810 if (ret < 0) {
3811 fs_path_free(new_path);
3812 return ret;
3813 }
3814
3815 fs_path_free(ref->full_path);
3816 set_ref_path(ref, new_path);
3817
3818 return 0;
3819}
3820
Alexander Block31db9f72012-07-25 23:19:24 +02003821/*
Filipe Manana9c2b4e02020-09-21 14:13:30 +01003822 * When processing the new references for an inode we may orphanize an existing
3823 * directory inode because its old name conflicts with one of the new references
3824 * of the current inode. Later, when processing another new reference of our
3825 * inode, we might need to orphanize another inode, but the path we have in the
3826 * reference reflects the pre-orphanization name of the directory we previously
3827 * orphanized. For example:
3828 *
3829 * parent snapshot looks like:
3830 *
3831 * . (ino 256)
3832 * |----- f1 (ino 257)
3833 * |----- f2 (ino 258)
3834 * |----- d1/ (ino 259)
3835 * |----- d2/ (ino 260)
3836 *
3837 * send snapshot looks like:
3838 *
3839 * . (ino 256)
3840 * |----- d1 (ino 258)
3841 * |----- f2/ (ino 259)
3842 * |----- f2_link/ (ino 260)
3843 * | |----- f1 (ino 257)
3844 * |
3845 * |----- d2 (ino 258)
3846 *
3847 * When processing inode 257 we compute the name for inode 259 as "d1", and we
3848 * cache it in the name cache. Later when we start processing inode 258, when
3849 * collecting all its new references we set a full path of "d1/d2" for its new
3850 * reference with name "d2". When we start processing the new references we
3851 * start by processing the new reference with name "d1", and this results in
3852 * orphanizing inode 259, since its old reference causes a conflict. Then we
3853 * move on the next new reference, with name "d2", and we find out we must
3854 * orphanize inode 260, as its old reference conflicts with ours - but for the
3855 * orphanization we use a source path corresponding to the path we stored in the
3856 * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
3857 * receiver fail since the path component "d1/" no longer exists, it was renamed
3858 * to "o259-6-0/" when processing the previous new reference. So in this case we
3859 * must recompute the path in the new reference and use it for the new
3860 * orphanization operation.
3861 */
3862static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3863{
3864 char *name;
3865 int ret;
3866
3867 name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
3868 if (!name)
3869 return -ENOMEM;
3870
3871 fs_path_reset(ref->full_path);
3872 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
3873 if (ret < 0)
3874 goto out;
3875
3876 ret = fs_path_add(ref->full_path, name, ref->name_len);
3877 if (ret < 0)
3878 goto out;
3879
3880 /* Update the reference's base name pointer. */
3881 set_ref_path(ref, ref->full_path);
3882out:
3883 kfree(name);
3884 return ret;
3885}
3886
3887/*
Alexander Block31db9f72012-07-25 23:19:24 +02003888 * This does all the move/link/unlink/rmdir magic.
3889 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003890static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003891{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003892 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003893 int ret = 0;
3894 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003895 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003896 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003897 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003898 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003899 u64 ow_gen;
Filipe Mananaf5962782017-06-22 20:03:51 +01003900 u64 ow_mode;
Alexander Block31db9f72012-07-25 23:19:24 +02003901 int did_overwrite = 0;
3902 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003903 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003904 bool can_rename = true;
Filipe Mananaf5962782017-06-22 20:03:51 +01003905 bool orphanized_dir = false;
Filipe Mananafdb13882017-06-13 14:13:11 +01003906 bool orphanized_ancestor = false;
Alexander Block31db9f72012-07-25 23:19:24 +02003907
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003908 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003909
Alexander Block6d85ed052012-08-01 14:48:59 +02003910 /*
3911 * This should never happen as the root dir always has the same ref
3912 * which is always '..'
3913 */
3914 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003915 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed052012-08-01 14:48:59 +02003916
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003917 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003918 if (!valid_path) {
3919 ret = -ENOMEM;
3920 goto out;
3921 }
3922
Alexander Block31db9f72012-07-25 23:19:24 +02003923 /*
3924 * First, check if the first ref of the current inode was overwritten
3925 * before. If yes, we know that the current inode was already orphanized
3926 * and thus use the orphan name. If not, we can use get_cur_path to
3927 * get the path of the first ref as it would like while receiving at
3928 * this point in time.
3929 * New inodes are always orphan at the beginning, so force to use the
3930 * orphan name in this case.
3931 * The first ref is stored in valid_path and will be updated if it
3932 * gets moved around.
3933 */
3934 if (!sctx->cur_inode_new) {
3935 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3936 sctx->cur_inode_gen);
3937 if (ret < 0)
3938 goto out;
3939 if (ret)
3940 did_overwrite = 1;
3941 }
3942 if (sctx->cur_inode_new || did_overwrite) {
3943 ret = gen_unique_name(sctx, sctx->cur_ino,
3944 sctx->cur_inode_gen, valid_path);
3945 if (ret < 0)
3946 goto out;
3947 is_orphan = 1;
3948 } else {
3949 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3950 valid_path);
3951 if (ret < 0)
3952 goto out;
3953 }
3954
Filipe Manana98272bb2020-09-21 14:13:29 +01003955 /*
3956 * Before doing any rename and link operations, do a first pass on the
3957 * new references to orphanize any unprocessed inodes that may have a
3958 * reference that conflicts with one of the new references of the current
3959 * inode. This needs to happen first because a new reference may conflict
3960 * with the old reference of a parent directory, so we must make sure
3961 * that the path used for link and rename commands don't use an
3962 * orphanized name when an ancestor was not yet orphanized.
3963 *
3964 * Example:
3965 *
3966 * Parent snapshot:
3967 *
3968 * . (ino 256)
3969 * |----- testdir/ (ino 259)
3970 * | |----- a (ino 257)
3971 * |
3972 * |----- b (ino 258)
3973 *
3974 * Send snapshot:
3975 *
3976 * . (ino 256)
3977 * |----- testdir_2/ (ino 259)
3978 * | |----- a (ino 260)
3979 * |
3980 * |----- testdir (ino 257)
3981 * |----- b (ino 257)
3982 * |----- b2 (ino 258)
3983 *
3984 * Processing the new reference for inode 257 with name "b" may happen
3985 * before processing the new reference with name "testdir". If so, we
3986 * must make sure that by the time we send a link command to create the
3987 * hard link "b", inode 259 was already orphanized, since the generated
3988 * path in "valid_path" already contains the orphanized name for 259.
3989 * We are processing inode 257, so only later when processing 259 we do
3990 * the rename operation to change its temporary (orphanized) name to
3991 * "testdir_2".
3992 */
Alexander Block31db9f72012-07-25 23:19:24 +02003993 list_for_each_entry(cur, &sctx->new_refs, list) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003994 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3995 if (ret < 0)
3996 goto out;
Filipe Manana98272bb2020-09-21 14:13:29 +01003997 if (ret == inode_state_will_create)
3998 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02003999
4000 /*
Filipe Manana98272bb2020-09-21 14:13:29 +01004001 * Check if this new ref would overwrite the first ref of another
4002 * unprocessed inode. If yes, orphanize the overwritten inode.
4003 * If we find an overwritten ref that is not the first ref,
4004 * simply unlink it.
Alexander Block31db9f72012-07-25 23:19:24 +02004005 */
4006 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4007 cur->name, cur->name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01004008 &ow_inode, &ow_gen, &ow_mode);
Alexander Block31db9f72012-07-25 23:19:24 +02004009 if (ret < 0)
4010 goto out;
4011 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004012 ret = is_first_ref(sctx->parent_root,
4013 ow_inode, cur->dir, cur->name,
4014 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004015 if (ret < 0)
4016 goto out;
4017 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00004018 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08004019 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00004020
Filipe Manana9c2b4e02020-09-21 14:13:30 +01004021 if (orphanized_dir) {
4022 ret = refresh_ref_path(sctx, cur);
4023 if (ret < 0)
4024 goto out;
4025 }
4026
Alexander Block31db9f72012-07-25 23:19:24 +02004027 ret = orphanize_inode(sctx, ow_inode, ow_gen,
4028 cur->full_path);
4029 if (ret < 0)
4030 goto out;
Filipe Mananaf5962782017-06-22 20:03:51 +01004031 if (S_ISDIR(ow_mode))
4032 orphanized_dir = true;
Robbie Ko801bec32015-06-23 18:39:46 +08004033
4034 /*
4035 * If ow_inode has its rename operation delayed
4036 * make sure that its orphanized name is used in
4037 * the source path when performing its rename
4038 * operation.
4039 */
4040 if (is_waiting_for_move(sctx, ow_inode)) {
4041 wdm = get_waiting_dir_move(sctx,
4042 ow_inode);
4043 ASSERT(wdm);
4044 wdm->orphanized = true;
4045 }
4046
Filipe Manana8996a482015-03-12 15:16:20 +00004047 /*
4048 * Make sure we clear our orphanized inode's
4049 * name from the name cache. This is because the
4050 * inode ow_inode might be an ancestor of some
4051 * other inode that will be orphanized as well
4052 * later and has an inode number greater than
4053 * sctx->send_progress. We need to prevent
4054 * future name lookups from using the old name
4055 * and get instead the orphan name.
4056 */
4057 nce = name_cache_search(sctx, ow_inode, ow_gen);
4058 if (nce) {
4059 name_cache_delete(sctx, nce);
4060 kfree(nce);
4061 }
Robbie Ko801bec32015-06-23 18:39:46 +08004062
4063 /*
4064 * ow_inode might currently be an ancestor of
4065 * cur_ino, therefore compute valid_path (the
4066 * current path of cur_ino) again because it
4067 * might contain the pre-orphanization name of
4068 * ow_inode, which is no longer valid.
4069 */
Filipe Manana72c36682017-06-07 11:41:29 +01004070 ret = is_ancestor(sctx->parent_root,
4071 ow_inode, ow_gen,
4072 sctx->cur_ino, NULL);
4073 if (ret > 0) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004074 orphanized_ancestor = true;
Filipe Manana72c36682017-06-07 11:41:29 +01004075 fs_path_reset(valid_path);
4076 ret = get_cur_path(sctx, sctx->cur_ino,
4077 sctx->cur_inode_gen,
4078 valid_path);
4079 }
Robbie Ko801bec32015-06-23 18:39:46 +08004080 if (ret < 0)
4081 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004082 } else {
4083 ret = send_unlink(sctx, cur->full_path);
4084 if (ret < 0)
4085 goto out;
4086 }
4087 }
4088
Filipe Manana98272bb2020-09-21 14:13:29 +01004089 }
4090
4091 list_for_each_entry(cur, &sctx->new_refs, list) {
4092 /*
4093 * We may have refs where the parent directory does not exist
4094 * yet. This happens if the parent directories inum is higher
4095 * than the current inum. To handle this case, we create the
4096 * parent directory out of order. But we need to check if this
4097 * did already happen before due to other refs in the same dir.
4098 */
4099 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4100 if (ret < 0)
4101 goto out;
4102 if (ret == inode_state_will_create) {
4103 ret = 0;
4104 /*
4105 * First check if any of the current inodes refs did
4106 * already create the dir.
4107 */
4108 list_for_each_entry(cur2, &sctx->new_refs, list) {
4109 if (cur == cur2)
4110 break;
4111 if (cur2->dir == cur->dir) {
4112 ret = 1;
4113 break;
4114 }
4115 }
4116
4117 /*
4118 * If that did not happen, check if a previous inode
4119 * did already create the dir.
4120 */
4121 if (!ret)
4122 ret = did_create_dir(sctx, cur->dir);
4123 if (ret < 0)
4124 goto out;
4125 if (!ret) {
4126 ret = send_create_inode(sctx, cur->dir);
4127 if (ret < 0)
4128 goto out;
4129 }
4130 }
4131
Filipe Manana84471e22015-02-28 22:29:22 +00004132 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4133 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4134 if (ret < 0)
4135 goto out;
4136 if (ret == 1) {
4137 can_rename = false;
4138 *pending_move = 1;
4139 }
4140 }
4141
Filipe Manana8b191a62015-04-09 14:09:14 +01004142 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4143 can_rename) {
4144 ret = wait_for_parent_move(sctx, cur, is_orphan);
4145 if (ret < 0)
4146 goto out;
4147 if (ret == 1) {
4148 can_rename = false;
4149 *pending_move = 1;
4150 }
4151 }
4152
Alexander Block31db9f72012-07-25 23:19:24 +02004153 /*
4154 * link/move the ref to the new place. If we have an orphan
4155 * inode, move it and update valid_path. If not, link or move
4156 * it depending on the inode mode.
4157 */
Filipe Manana84471e22015-02-28 22:29:22 +00004158 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004159 ret = send_rename(sctx, valid_path, cur->full_path);
4160 if (ret < 0)
4161 goto out;
4162 is_orphan = 0;
4163 ret = fs_path_copy(valid_path, cur->full_path);
4164 if (ret < 0)
4165 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00004166 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004167 if (S_ISDIR(sctx->cur_inode_mode)) {
4168 /*
4169 * Dirs can't be linked, so move it. For moved
4170 * dirs, we always have one new and one deleted
4171 * ref. The deleted ref is ignored later.
4172 */
Filipe Manana8b191a62015-04-09 14:09:14 +01004173 ret = send_rename(sctx, valid_path,
4174 cur->full_path);
4175 if (!ret)
4176 ret = fs_path_copy(valid_path,
4177 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004178 if (ret < 0)
4179 goto out;
4180 } else {
Filipe Mananaf5962782017-06-22 20:03:51 +01004181 /*
4182 * We might have previously orphanized an inode
4183 * which is an ancestor of our current inode,
4184 * so our reference's full path, which was
4185 * computed before any such orphanizations, must
4186 * be updated.
4187 */
4188 if (orphanized_dir) {
4189 ret = update_ref_path(sctx, cur);
4190 if (ret < 0)
4191 goto out;
4192 }
Alexander Block31db9f72012-07-25 23:19:24 +02004193 ret = send_link(sctx, cur->full_path,
4194 valid_path);
4195 if (ret < 0)
4196 goto out;
4197 }
4198 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004199 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004200 if (ret < 0)
4201 goto out;
4202 }
4203
4204 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4205 /*
4206 * Check if we can already rmdir the directory. If not,
4207 * orphanize it. For every dir item inside that gets deleted
4208 * later, we do this check again and rmdir it then if possible.
4209 * See the use of check_dirs for more details.
4210 */
Filipe Manana9dc44212014-02-19 14:31:44 +00004211 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4212 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004213 if (ret < 0)
4214 goto out;
4215 if (ret) {
4216 ret = send_rmdir(sctx, valid_path);
4217 if (ret < 0)
4218 goto out;
4219 } else if (!is_orphan) {
4220 ret = orphanize_inode(sctx, sctx->cur_ino,
4221 sctx->cur_inode_gen, valid_path);
4222 if (ret < 0)
4223 goto out;
4224 is_orphan = 1;
4225 }
4226
4227 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004228 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004229 if (ret < 0)
4230 goto out;
4231 }
Alexander Blockccf16262012-07-28 11:46:29 +02004232 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4233 !list_empty(&sctx->deleted_refs)) {
4234 /*
4235 * We have a moved dir. Add the old parent to check_dirs
4236 */
4237 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4238 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04004239 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02004240 if (ret < 0)
4241 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004242 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4243 /*
4244 * We have a non dir inode. Go through all deleted refs and
4245 * unlink them if they were not already overwritten by other
4246 * inodes.
4247 */
4248 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4249 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4250 sctx->cur_ino, sctx->cur_inode_gen,
4251 cur->name, cur->name_len);
4252 if (ret < 0)
4253 goto out;
4254 if (!ret) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004255 /*
4256 * If we orphanized any ancestor before, we need
4257 * to recompute the full path for deleted names,
4258 * since any such path was computed before we
4259 * processed any references and orphanized any
4260 * ancestor inode.
4261 */
4262 if (orphanized_ancestor) {
Filipe Mananaf5962782017-06-22 20:03:51 +01004263 ret = update_ref_path(sctx, cur);
4264 if (ret < 0)
Filipe Mananafdb13882017-06-13 14:13:11 +01004265 goto out;
Filipe Mananafdb13882017-06-13 14:13:11 +01004266 }
Alexander Block1f4692d2012-07-28 10:42:24 +02004267 ret = send_unlink(sctx, cur->full_path);
4268 if (ret < 0)
4269 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004270 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004271 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004272 if (ret < 0)
4273 goto out;
4274 }
Alexander Block31db9f72012-07-25 23:19:24 +02004275 /*
4276 * If the inode is still orphan, unlink the orphan. This may
4277 * happen when a previous inode did overwrite the first ref
4278 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02004279 * inode. Unlinking does not mean that the inode is deleted in
4280 * all cases. There may still be links to this inode in other
4281 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02004282 */
Alexander Block1f4692d2012-07-28 10:42:24 +02004283 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02004284 ret = send_unlink(sctx, valid_path);
4285 if (ret < 0)
4286 goto out;
4287 }
4288 }
4289
4290 /*
4291 * We did collect all parent dirs where cur_inode was once located. We
4292 * now go through all these dirs and check if they are pending for
4293 * deletion and if it's finally possible to perform the rmdir now.
4294 * We also update the inode stats of the parent dirs here.
4295 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004296 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02004297 /*
4298 * In case we had refs into dirs that were not processed yet,
4299 * we don't need to do the utime and rmdir logic for these dirs.
4300 * The dir will be processed later.
4301 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004302 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02004303 continue;
4304
Josef Bacikba5e8f22013-08-16 16:52:55 -04004305 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004306 if (ret < 0)
4307 goto out;
4308
4309 if (ret == inode_state_did_create ||
4310 ret == inode_state_no_change) {
4311 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004312 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004313 if (ret < 0)
4314 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004315 } else if (ret == inode_state_did_delete &&
4316 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004317 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4318 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004319 if (ret < 0)
4320 goto out;
4321 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004322 ret = get_cur_path(sctx, cur->dir,
4323 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004324 if (ret < 0)
4325 goto out;
4326 ret = send_rmdir(sctx, valid_path);
4327 if (ret < 0)
4328 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004329 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004330 }
4331 }
4332 }
4333
Alexander Block31db9f72012-07-25 23:19:24 +02004334 ret = 0;
4335
4336out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004337 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004338 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004339 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004340 return ret;
4341}
4342
Nikolay Borisova0357512017-08-21 12:43:44 +03004343static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4344 void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004345{
4346 int ret = 0;
4347 struct send_ctx *sctx = ctx;
4348 struct fs_path *p;
4349 u64 gen;
4350
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004351 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004352 if (!p)
4353 return -ENOMEM;
4354
Liu Boa4d96d62014-03-03 21:31:03 +08004355 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004356 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004357 if (ret < 0)
4358 goto out;
4359
Alexander Block31db9f72012-07-25 23:19:24 +02004360 ret = get_cur_path(sctx, dir, gen, p);
4361 if (ret < 0)
4362 goto out;
4363 ret = fs_path_add_path(p, name);
4364 if (ret < 0)
4365 goto out;
4366
Liu Boa4d96d62014-03-03 21:31:03 +08004367 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004368
4369out:
4370 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004371 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004372 return ret;
4373}
4374
Liu Boa4d96d62014-03-03 21:31:03 +08004375static int __record_new_ref(int num, u64 dir, int index,
4376 struct fs_path *name,
4377 void *ctx)
4378{
4379 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004380 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
Liu Boa4d96d62014-03-03 21:31:03 +08004381}
4382
4383
Alexander Block31db9f72012-07-25 23:19:24 +02004384static int __record_deleted_ref(int num, u64 dir, int index,
4385 struct fs_path *name,
4386 void *ctx)
4387{
Alexander Block31db9f72012-07-25 23:19:24 +02004388 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004389 return record_ref(sctx->parent_root, dir, name, ctx,
4390 &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004391}
4392
4393static int record_new_ref(struct send_ctx *sctx)
4394{
4395 int ret;
4396
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004397 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4398 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004399 if (ret < 0)
4400 goto out;
4401 ret = 0;
4402
4403out:
4404 return ret;
4405}
4406
4407static int record_deleted_ref(struct send_ctx *sctx)
4408{
4409 int ret;
4410
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004411 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4412 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004413 if (ret < 0)
4414 goto out;
4415 ret = 0;
4416
4417out:
4418 return ret;
4419}
4420
4421struct find_ref_ctx {
4422 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004423 u64 dir_gen;
4424 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004425 struct fs_path *name;
4426 int found_idx;
4427};
4428
4429static int __find_iref(int num, u64 dir, int index,
4430 struct fs_path *name,
4431 void *ctx_)
4432{
4433 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004434 u64 dir_gen;
4435 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004436
4437 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4438 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004439 /*
4440 * To avoid doing extra lookups we'll only do this if everything
4441 * else matches.
4442 */
4443 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4444 NULL, NULL, NULL);
4445 if (ret)
4446 return ret;
4447 if (dir_gen != ctx->dir_gen)
4448 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004449 ctx->found_idx = num;
4450 return 1;
4451 }
4452 return 0;
4453}
4454
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004455static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004456 struct btrfs_path *path,
4457 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004458 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004459{
4460 int ret;
4461 struct find_ref_ctx ctx;
4462
4463 ctx.dir = dir;
4464 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004465 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004466 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004467 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004468
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004469 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004470 if (ret < 0)
4471 return ret;
4472
4473 if (ctx.found_idx == -1)
4474 return -ENOENT;
4475
4476 return ctx.found_idx;
4477}
4478
4479static int __record_changed_new_ref(int num, u64 dir, int index,
4480 struct fs_path *name,
4481 void *ctx)
4482{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004483 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004484 int ret;
4485 struct send_ctx *sctx = ctx;
4486
Josef Bacikba5e8f22013-08-16 16:52:55 -04004487 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4488 NULL, NULL, NULL);
4489 if (ret)
4490 return ret;
4491
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004492 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004493 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004494 if (ret == -ENOENT)
4495 ret = __record_new_ref(num, dir, index, name, sctx);
4496 else if (ret > 0)
4497 ret = 0;
4498
4499 return ret;
4500}
4501
4502static int __record_changed_deleted_ref(int num, u64 dir, int index,
4503 struct fs_path *name,
4504 void *ctx)
4505{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004506 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004507 int ret;
4508 struct send_ctx *sctx = ctx;
4509
Josef Bacikba5e8f22013-08-16 16:52:55 -04004510 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4511 NULL, NULL, NULL);
4512 if (ret)
4513 return ret;
4514
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004515 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004516 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004517 if (ret == -ENOENT)
4518 ret = __record_deleted_ref(num, dir, index, name, sctx);
4519 else if (ret > 0)
4520 ret = 0;
4521
4522 return ret;
4523}
4524
4525static int record_changed_ref(struct send_ctx *sctx)
4526{
4527 int ret = 0;
4528
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004529 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004530 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4531 if (ret < 0)
4532 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004533 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004534 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4535 if (ret < 0)
4536 goto out;
4537 ret = 0;
4538
4539out:
4540 return ret;
4541}
4542
4543/*
4544 * Record and process all refs at once. Needed when an inode changes the
4545 * generation number, which means that it was deleted and recreated.
4546 */
4547static int process_all_refs(struct send_ctx *sctx,
4548 enum btrfs_compare_tree_result cmd)
4549{
4550 int ret;
4551 struct btrfs_root *root;
4552 struct btrfs_path *path;
4553 struct btrfs_key key;
4554 struct btrfs_key found_key;
4555 struct extent_buffer *eb;
4556 int slot;
4557 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004558 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004559
4560 path = alloc_path_for_send();
4561 if (!path)
4562 return -ENOMEM;
4563
4564 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4565 root = sctx->send_root;
4566 cb = __record_new_ref;
4567 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4568 root = sctx->parent_root;
4569 cb = __record_deleted_ref;
4570 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004571 btrfs_err(sctx->send_root->fs_info,
4572 "Wrong command %d in process_all_refs", cmd);
4573 ret = -EINVAL;
4574 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004575 }
4576
4577 key.objectid = sctx->cmp_key->objectid;
4578 key.type = BTRFS_INODE_REF_KEY;
4579 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004580 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4581 if (ret < 0)
4582 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004583
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004584 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004585 eb = path->nodes[0];
4586 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004587 if (slot >= btrfs_header_nritems(eb)) {
4588 ret = btrfs_next_leaf(root, path);
4589 if (ret < 0)
4590 goto out;
4591 else if (ret > 0)
4592 break;
4593 continue;
4594 }
4595
Alexander Block31db9f72012-07-25 23:19:24 +02004596 btrfs_item_key_to_cpu(eb, &found_key, slot);
4597
4598 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004599 (found_key.type != BTRFS_INODE_REF_KEY &&
4600 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004601 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004602
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004603 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004604 if (ret < 0)
4605 goto out;
4606
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004607 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004608 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004609 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004610
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004611 /*
4612 * We don't actually care about pending_move as we are simply
4613 * re-creating this inode and will be rename'ing it into place once we
4614 * rename the parent directory.
4615 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004616 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004617out:
4618 btrfs_free_path(path);
4619 return ret;
4620}
4621
4622static int send_set_xattr(struct send_ctx *sctx,
4623 struct fs_path *path,
4624 const char *name, int name_len,
4625 const char *data, int data_len)
4626{
4627 int ret = 0;
4628
4629 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4630 if (ret < 0)
4631 goto out;
4632
4633 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4634 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4635 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4636
4637 ret = send_cmd(sctx);
4638
4639tlv_put_failure:
4640out:
4641 return ret;
4642}
4643
4644static int send_remove_xattr(struct send_ctx *sctx,
4645 struct fs_path *path,
4646 const char *name, int name_len)
4647{
4648 int ret = 0;
4649
4650 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4651 if (ret < 0)
4652 goto out;
4653
4654 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4655 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4656
4657 ret = send_cmd(sctx);
4658
4659tlv_put_failure:
4660out:
4661 return ret;
4662}
4663
4664static int __process_new_xattr(int num, struct btrfs_key *di_key,
4665 const char *name, int name_len,
4666 const char *data, int data_len,
4667 u8 type, void *ctx)
4668{
4669 int ret;
4670 struct send_ctx *sctx = ctx;
4671 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004672 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004673
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -03004674 /* Capabilities are emitted by finish_inode_if_needed */
4675 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4676 return 0;
4677
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004678 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004679 if (!p)
4680 return -ENOMEM;
4681
4682 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004683 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004684 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004685 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004686 * only contains the version number and no entries.
4687 */
4688 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4689 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4690 if (data_len == 0) {
4691 dummy_acl.a_version =
4692 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4693 data = (char *)&dummy_acl;
4694 data_len = sizeof(dummy_acl);
4695 }
4696 }
4697
4698 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4699 if (ret < 0)
4700 goto out;
4701
4702 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4703
4704out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004705 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004706 return ret;
4707}
4708
4709static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4710 const char *name, int name_len,
4711 const char *data, int data_len,
4712 u8 type, void *ctx)
4713{
4714 int ret;
4715 struct send_ctx *sctx = ctx;
4716 struct fs_path *p;
4717
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004718 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004719 if (!p)
4720 return -ENOMEM;
4721
4722 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4723 if (ret < 0)
4724 goto out;
4725
4726 ret = send_remove_xattr(sctx, p, name, name_len);
4727
4728out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004729 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004730 return ret;
4731}
4732
4733static int process_new_xattr(struct send_ctx *sctx)
4734{
4735 int ret = 0;
4736
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004737 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004738 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004739
4740 return ret;
4741}
4742
4743static int process_deleted_xattr(struct send_ctx *sctx)
4744{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004745 return iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004746 __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004747}
4748
4749struct find_xattr_ctx {
4750 const char *name;
4751 int name_len;
4752 int found_idx;
4753 char *found_data;
4754 int found_data_len;
4755};
4756
4757static int __find_xattr(int num, struct btrfs_key *di_key,
4758 const char *name, int name_len,
4759 const char *data, int data_len,
4760 u8 type, void *vctx)
4761{
4762 struct find_xattr_ctx *ctx = vctx;
4763
4764 if (name_len == ctx->name_len &&
4765 strncmp(name, ctx->name, name_len) == 0) {
4766 ctx->found_idx = num;
4767 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004768 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004769 if (!ctx->found_data)
4770 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004771 return 1;
4772 }
4773 return 0;
4774}
4775
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004776static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004777 struct btrfs_path *path,
4778 struct btrfs_key *key,
4779 const char *name, int name_len,
4780 char **data, int *data_len)
4781{
4782 int ret;
4783 struct find_xattr_ctx ctx;
4784
4785 ctx.name = name;
4786 ctx.name_len = name_len;
4787 ctx.found_idx = -1;
4788 ctx.found_data = NULL;
4789 ctx.found_data_len = 0;
4790
Nikolay Borisova0357512017-08-21 12:43:44 +03004791 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004792 if (ret < 0)
4793 return ret;
4794
4795 if (ctx.found_idx == -1)
4796 return -ENOENT;
4797 if (data) {
4798 *data = ctx.found_data;
4799 *data_len = ctx.found_data_len;
4800 } else {
4801 kfree(ctx.found_data);
4802 }
4803 return ctx.found_idx;
4804}
4805
4806
4807static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4808 const char *name, int name_len,
4809 const char *data, int data_len,
4810 u8 type, void *ctx)
4811{
4812 int ret;
4813 struct send_ctx *sctx = ctx;
4814 char *found_data = NULL;
4815 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004816
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004817 ret = find_xattr(sctx->parent_root, sctx->right_path,
4818 sctx->cmp_key, name, name_len, &found_data,
4819 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004820 if (ret == -ENOENT) {
4821 ret = __process_new_xattr(num, di_key, name, name_len, data,
4822 data_len, type, ctx);
4823 } else if (ret >= 0) {
4824 if (data_len != found_data_len ||
4825 memcmp(data, found_data, data_len)) {
4826 ret = __process_new_xattr(num, di_key, name, name_len,
4827 data, data_len, type, ctx);
4828 } else {
4829 ret = 0;
4830 }
4831 }
4832
4833 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004834 return ret;
4835}
4836
4837static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4838 const char *name, int name_len,
4839 const char *data, int data_len,
4840 u8 type, void *ctx)
4841{
4842 int ret;
4843 struct send_ctx *sctx = ctx;
4844
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004845 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4846 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004847 if (ret == -ENOENT)
4848 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4849 data_len, type, ctx);
4850 else if (ret >= 0)
4851 ret = 0;
4852
4853 return ret;
4854}
4855
4856static int process_changed_xattr(struct send_ctx *sctx)
4857{
4858 int ret = 0;
4859
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004860 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004861 __process_changed_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004862 if (ret < 0)
4863 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004864 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004865 __process_changed_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004866
4867out:
4868 return ret;
4869}
4870
4871static int process_all_new_xattrs(struct send_ctx *sctx)
4872{
4873 int ret;
4874 struct btrfs_root *root;
4875 struct btrfs_path *path;
4876 struct btrfs_key key;
4877 struct btrfs_key found_key;
4878 struct extent_buffer *eb;
4879 int slot;
4880
4881 path = alloc_path_for_send();
4882 if (!path)
4883 return -ENOMEM;
4884
4885 root = sctx->send_root;
4886
4887 key.objectid = sctx->cmp_key->objectid;
4888 key.type = BTRFS_XATTR_ITEM_KEY;
4889 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004890 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4891 if (ret < 0)
4892 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004893
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004894 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004895 eb = path->nodes[0];
4896 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004897 if (slot >= btrfs_header_nritems(eb)) {
4898 ret = btrfs_next_leaf(root, path);
4899 if (ret < 0) {
4900 goto out;
4901 } else if (ret > 0) {
4902 ret = 0;
4903 break;
4904 }
4905 continue;
4906 }
Alexander Block31db9f72012-07-25 23:19:24 +02004907
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004908 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004909 if (found_key.objectid != key.objectid ||
4910 found_key.type != key.type) {
4911 ret = 0;
4912 goto out;
4913 }
4914
Nikolay Borisova0357512017-08-21 12:43:44 +03004915 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004916 if (ret < 0)
4917 goto out;
4918
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004919 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004920 }
4921
4922out:
4923 btrfs_free_path(path);
4924 return ret;
4925}
4926
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07004927static inline u64 max_send_read_size(const struct send_ctx *sctx)
4928{
4929 return sctx->send_max_size - SZ_16K;
4930}
4931
4932static int put_data_header(struct send_ctx *sctx, u32 len)
4933{
4934 struct btrfs_tlv_header *hdr;
4935
4936 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
4937 return -EOVERFLOW;
4938 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
4939 put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
4940 put_unaligned_le16(len, &hdr->tlv_len);
4941 sctx->send_size += sizeof(*hdr);
4942 return 0;
4943}
4944
4945static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
Josef Baciked259092013-10-25 11:36:01 -04004946{
4947 struct btrfs_root *root = sctx->send_root;
4948 struct btrfs_fs_info *fs_info = root->fs_info;
4949 struct inode *inode;
4950 struct page *page;
4951 char *addr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004952 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004953 pgoff_t last_index;
Johannes Thumshirn70730172018-12-05 15:23:03 +01004954 unsigned pg_offset = offset_in_page(offset);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07004955 int ret;
4956
4957 ret = put_data_header(sctx, len);
4958 if (ret)
4959 return ret;
Josef Baciked259092013-10-25 11:36:01 -04004960
David Sterba0202e832020-05-15 19:35:59 +02004961 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
Josef Baciked259092013-10-25 11:36:01 -04004962 if (IS_ERR(inode))
4963 return PTR_ERR(inode);
4964
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004965 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004966
4967 /* initial readahead */
4968 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4969 file_ra_state_init(&sctx->ra, inode->i_mapping);
Liu Bo2131bcd2014-03-05 10:07:35 +08004970
Josef Baciked259092013-10-25 11:36:01 -04004971 while (index <= last_index) {
4972 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004973 PAGE_SIZE - pg_offset);
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004974
4975 page = find_lock_page(inode->i_mapping, index);
Josef Baciked259092013-10-25 11:36:01 -04004976 if (!page) {
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004977 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4978 NULL, index, last_index + 1 - index);
4979
4980 page = find_or_create_page(inode->i_mapping, index,
4981 GFP_KERNEL);
4982 if (!page) {
4983 ret = -ENOMEM;
4984 break;
4985 }
4986 }
4987
4988 if (PageReadahead(page)) {
4989 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4990 NULL, page, index, last_index + 1 - index);
Josef Baciked259092013-10-25 11:36:01 -04004991 }
4992
4993 if (!PageUptodate(page)) {
4994 btrfs_readpage(NULL, page);
4995 lock_page(page);
4996 if (!PageUptodate(page)) {
4997 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004998 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004999 ret = -EIO;
5000 break;
5001 }
5002 }
5003
5004 addr = kmap(page);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005005 memcpy(sctx->send_buf + sctx->send_size, addr + pg_offset,
5006 cur_len);
Josef Baciked259092013-10-25 11:36:01 -04005007 kunmap(page);
5008 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005009 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04005010 index++;
5011 pg_offset = 0;
5012 len -= cur_len;
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005013 sctx->send_size += cur_len;
Josef Baciked259092013-10-25 11:36:01 -04005014 }
Josef Baciked259092013-10-25 11:36:01 -04005015 iput(inode);
5016 return ret;
5017}
5018
Alexander Block31db9f72012-07-25 23:19:24 +02005019/*
5020 * Read some bytes from the current inode/file and send a write command to
5021 * user space.
5022 */
5023static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
5024{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04005025 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02005026 int ret = 0;
5027 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02005028
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005029 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02005030 if (!p)
5031 return -ENOMEM;
5032
Jeff Mahoney04ab9562016-09-20 10:05:03 -04005033 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005034
Alexander Block31db9f72012-07-25 23:19:24 +02005035 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5036 if (ret < 0)
5037 goto out;
5038
5039 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5040 if (ret < 0)
5041 goto out;
5042
5043 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5044 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005045 ret = put_file_data(sctx, offset, len);
5046 if (ret < 0)
5047 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005048
5049 ret = send_cmd(sctx);
5050
5051tlv_put_failure:
5052out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005053 fs_path_free(p);
Omar Sandovala9b2e0d2020-08-21 00:39:51 -07005054 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005055}
5056
5057/*
5058 * Send a clone command to user space.
5059 */
5060static int send_clone(struct send_ctx *sctx,
5061 u64 offset, u32 len,
5062 struct clone_root *clone_root)
5063{
5064 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005065 struct fs_path *p;
5066 u64 gen;
5067
Jeff Mahoney04ab9562016-09-20 10:05:03 -04005068 btrfs_debug(sctx->send_root->fs_info,
5069 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005070 offset, len, clone_root->root->root_key.objectid,
5071 clone_root->ino, clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02005072
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005073 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02005074 if (!p)
5075 return -ENOMEM;
5076
5077 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5078 if (ret < 0)
5079 goto out;
5080
5081 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5082 if (ret < 0)
5083 goto out;
5084
5085 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5086 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5087 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5088
Alexander Blocke938c8a2012-07-28 16:33:49 +02005089 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02005090 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005091 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005092 if (ret < 0)
5093 goto out;
5094 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5095 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005096 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02005097 }
5098 if (ret < 0)
5099 goto out;
5100
Josef Bacik37b8d272015-06-04 17:17:25 -04005101 /*
5102 * If the parent we're using has a received_uuid set then use that as
5103 * our clone source as that is what we will look for when doing a
5104 * receive.
5105 *
5106 * This covers the case that we create a snapshot off of a received
5107 * subvolume and then use that as the parent and try to receive on a
5108 * different host.
5109 */
5110 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
5111 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5112 clone_root->root->root_item.received_uuid);
5113 else
5114 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5115 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02005116 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
David Sterba09e3a282020-09-15 14:30:15 +02005117 btrfs_root_ctransid(&clone_root->root->root_item));
Alexander Block31db9f72012-07-25 23:19:24 +02005118 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5119 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5120 clone_root->offset);
5121
5122 ret = send_cmd(sctx);
5123
5124tlv_put_failure:
5125out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005126 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02005127 return ret;
5128}
5129
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005130/*
5131 * Send an update extent command to user space.
5132 */
5133static int send_update_extent(struct send_ctx *sctx,
5134 u64 offset, u32 len)
5135{
5136 int ret = 0;
5137 struct fs_path *p;
5138
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005139 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005140 if (!p)
5141 return -ENOMEM;
5142
5143 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5144 if (ret < 0)
5145 goto out;
5146
5147 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5148 if (ret < 0)
5149 goto out;
5150
5151 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5152 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5153 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5154
5155 ret = send_cmd(sctx);
5156
5157tlv_put_failure:
5158out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005159 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005160 return ret;
5161}
5162
Josef Bacik16e75492013-10-22 12:18:51 -04005163static int send_hole(struct send_ctx *sctx, u64 end)
5164{
5165 struct fs_path *p = NULL;
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005166 u64 read_size = max_send_read_size(sctx);
Josef Bacik16e75492013-10-22 12:18:51 -04005167 u64 offset = sctx->cur_inode_last_extent;
Josef Bacik16e75492013-10-22 12:18:51 -04005168 int ret = 0;
5169
Filipe Manana22d31512018-07-30 12:39:58 +01005170 /*
5171 * A hole that starts at EOF or beyond it. Since we do not yet support
5172 * fallocate (for extent preallocation and hole punching), sending a
5173 * write of zeroes starting at EOF or beyond would later require issuing
5174 * a truncate operation which would undo the write and achieve nothing.
5175 */
5176 if (offset >= sctx->cur_inode_size)
5177 return 0;
5178
Filipe Manana6b1f72e52019-05-20 09:55:42 +01005179 /*
5180 * Don't go beyond the inode's i_size due to prealloc extents that start
5181 * after the i_size.
5182 */
5183 end = min_t(u64, end, sctx->cur_inode_size);
5184
Filipe Mananad4dfc0f2018-02-06 20:39:20 +00005185 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5186 return send_update_extent(sctx, offset, end - offset);
5187
Josef Bacik16e75492013-10-22 12:18:51 -04005188 p = fs_path_alloc();
5189 if (!p)
5190 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01005191 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5192 if (ret < 0)
5193 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04005194 while (offset < end) {
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005195 u64 len = min(end - offset, read_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005196
5197 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5198 if (ret < 0)
5199 break;
Josef Bacik16e75492013-10-22 12:18:51 -04005200 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5201 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005202 ret = put_data_header(sctx, len);
5203 if (ret < 0)
5204 break;
5205 memset(sctx->send_buf + sctx->send_size, 0, len);
5206 sctx->send_size += len;
Josef Bacik16e75492013-10-22 12:18:51 -04005207 ret = send_cmd(sctx);
5208 if (ret < 0)
5209 break;
5210 offset += len;
5211 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005212 sctx->cur_inode_next_write_offset = offset;
Josef Bacik16e75492013-10-22 12:18:51 -04005213tlv_put_failure:
5214 fs_path_free(p);
5215 return ret;
5216}
5217
Filipe Mananad906d492015-10-02 10:47:34 +01005218static int send_extent_data(struct send_ctx *sctx,
5219 const u64 offset,
5220 const u64 len)
5221{
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005222 u64 read_size = max_send_read_size(sctx);
Filipe Mananad906d492015-10-02 10:47:34 +01005223 u64 sent = 0;
5224
5225 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5226 return send_update_extent(sctx, offset, len);
5227
5228 while (sent < len) {
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005229 u64 size = min(len - sent, read_size);
Filipe Mananad906d492015-10-02 10:47:34 +01005230 int ret;
5231
Filipe Mananad906d492015-10-02 10:47:34 +01005232 ret = send_write(sctx, offset + sent, size);
5233 if (ret < 0)
5234 return ret;
Omar Sandovala9b2e0d2020-08-21 00:39:51 -07005235 sent += size;
Filipe Mananad906d492015-10-02 10:47:34 +01005236 }
5237 return 0;
5238}
5239
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -03005240/*
5241 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5242 * found, call send_set_xattr function to emit it.
5243 *
5244 * Return 0 if there isn't a capability, or when the capability was emitted
5245 * successfully, or < 0 if an error occurred.
5246 */
5247static int send_capabilities(struct send_ctx *sctx)
5248{
5249 struct fs_path *fspath = NULL;
5250 struct btrfs_path *path;
5251 struct btrfs_dir_item *di;
5252 struct extent_buffer *leaf;
5253 unsigned long data_ptr;
5254 char *buf = NULL;
5255 int buf_len;
5256 int ret = 0;
5257
5258 path = alloc_path_for_send();
5259 if (!path)
5260 return -ENOMEM;
5261
5262 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5263 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5264 if (!di) {
5265 /* There is no xattr for this inode */
5266 goto out;
5267 } else if (IS_ERR(di)) {
5268 ret = PTR_ERR(di);
5269 goto out;
5270 }
5271
5272 leaf = path->nodes[0];
5273 buf_len = btrfs_dir_data_len(leaf, di);
5274
5275 fspath = fs_path_alloc();
5276 buf = kmalloc(buf_len, GFP_KERNEL);
5277 if (!fspath || !buf) {
5278 ret = -ENOMEM;
5279 goto out;
5280 }
5281
5282 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5283 if (ret < 0)
5284 goto out;
5285
5286 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5287 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5288
5289 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5290 strlen(XATTR_NAME_CAPS), buf, buf_len);
5291out:
5292 kfree(buf);
5293 fs_path_free(fspath);
5294 btrfs_free_path(path);
5295 return ret;
5296}
5297
Filipe Mananad906d492015-10-02 10:47:34 +01005298static int clone_range(struct send_ctx *sctx,
5299 struct clone_root *clone_root,
5300 const u64 disk_byte,
5301 u64 data_offset,
5302 u64 offset,
5303 u64 len)
5304{
5305 struct btrfs_path *path;
5306 struct btrfs_key key;
5307 int ret;
Austin Kim431d3982019-09-03 12:30:19 +09005308 u64 clone_src_i_size = 0;
Filipe Mananad906d492015-10-02 10:47:34 +01005309
Filipe Manana72610b12017-08-10 22:54:51 +01005310 /*
5311 * Prevent cloning from a zero offset with a length matching the sector
5312 * size because in some scenarios this will make the receiver fail.
5313 *
5314 * For example, if in the source filesystem the extent at offset 0
5315 * has a length of sectorsize and it was written using direct IO, then
5316 * it can never be an inline extent (even if compression is enabled).
5317 * Then this extent can be cloned in the original filesystem to a non
5318 * zero file offset, but it may not be possible to clone in the
5319 * destination filesystem because it can be inlined due to compression
5320 * on the destination filesystem (as the receiver's write operations are
5321 * always done using buffered IO). The same happens when the original
5322 * filesystem does not have compression enabled but the destination
5323 * filesystem has.
5324 */
5325 if (clone_root->offset == 0 &&
5326 len == sctx->send_root->fs_info->sectorsize)
5327 return send_extent_data(sctx, offset, len);
5328
Filipe Mananad906d492015-10-02 10:47:34 +01005329 path = alloc_path_for_send();
5330 if (!path)
5331 return -ENOMEM;
5332
5333 /*
Robbie Ko040ee6122019-03-29 18:03:27 +08005334 * There are inodes that have extents that lie behind its i_size. Don't
5335 * accept clones from these extents.
5336 */
5337 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5338 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL);
5339 btrfs_release_path(path);
5340 if (ret < 0)
5341 goto out;
5342
5343 /*
Filipe Mananad906d492015-10-02 10:47:34 +01005344 * We can't send a clone operation for the entire range if we find
5345 * extent items in the respective range in the source file that
5346 * refer to different extents or if we find holes.
5347 * So check for that and do a mix of clone and regular write/copy
5348 * operations if needed.
5349 *
5350 * Example:
5351 *
5352 * mkfs.btrfs -f /dev/sda
5353 * mount /dev/sda /mnt
5354 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5355 * cp --reflink=always /mnt/foo /mnt/bar
5356 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5357 * btrfs subvolume snapshot -r /mnt /mnt/snap
5358 *
5359 * If when we send the snapshot and we are processing file bar (which
5360 * has a higher inode number than foo) we blindly send a clone operation
5361 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5362 * a file bar that matches the content of file foo - iow, doesn't match
5363 * the content from bar in the original filesystem.
5364 */
5365 key.objectid = clone_root->ino;
5366 key.type = BTRFS_EXTENT_DATA_KEY;
5367 key.offset = clone_root->offset;
5368 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5369 if (ret < 0)
5370 goto out;
5371 if (ret > 0 && path->slots[0] > 0) {
5372 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5373 if (key.objectid == clone_root->ino &&
5374 key.type == BTRFS_EXTENT_DATA_KEY)
5375 path->slots[0]--;
5376 }
5377
5378 while (true) {
5379 struct extent_buffer *leaf = path->nodes[0];
5380 int slot = path->slots[0];
5381 struct btrfs_file_extent_item *ei;
5382 u8 type;
5383 u64 ext_len;
5384 u64 clone_len;
Robbie Ko040ee6122019-03-29 18:03:27 +08005385 u64 clone_data_offset;
Filipe Mananad906d492015-10-02 10:47:34 +01005386
5387 if (slot >= btrfs_header_nritems(leaf)) {
5388 ret = btrfs_next_leaf(clone_root->root, path);
5389 if (ret < 0)
5390 goto out;
5391 else if (ret > 0)
5392 break;
5393 continue;
5394 }
5395
5396 btrfs_item_key_to_cpu(leaf, &key, slot);
5397
5398 /*
5399 * We might have an implicit trailing hole (NO_HOLES feature
5400 * enabled). We deal with it after leaving this loop.
5401 */
5402 if (key.objectid != clone_root->ino ||
5403 key.type != BTRFS_EXTENT_DATA_KEY)
5404 break;
5405
5406 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5407 type = btrfs_file_extent_type(leaf, ei);
5408 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005409 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005410 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01005411 } else {
5412 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5413 }
5414
5415 if (key.offset + ext_len <= clone_root->offset)
5416 goto next;
5417
5418 if (key.offset > clone_root->offset) {
5419 /* Implicit hole, NO_HOLES feature enabled. */
5420 u64 hole_len = key.offset - clone_root->offset;
5421
5422 if (hole_len > len)
5423 hole_len = len;
5424 ret = send_extent_data(sctx, offset, hole_len);
5425 if (ret < 0)
5426 goto out;
5427
5428 len -= hole_len;
5429 if (len == 0)
5430 break;
5431 offset += hole_len;
5432 clone_root->offset += hole_len;
5433 data_offset += hole_len;
5434 }
5435
5436 if (key.offset >= clone_root->offset + len)
5437 break;
5438
Robbie Ko040ee6122019-03-29 18:03:27 +08005439 if (key.offset >= clone_src_i_size)
5440 break;
5441
5442 if (key.offset + ext_len > clone_src_i_size)
5443 ext_len = clone_src_i_size - key.offset;
5444
5445 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5446 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5447 clone_root->offset = key.offset;
5448 if (clone_data_offset < data_offset &&
5449 clone_data_offset + ext_len > data_offset) {
5450 u64 extent_offset;
5451
5452 extent_offset = data_offset - clone_data_offset;
5453 ext_len -= extent_offset;
5454 clone_data_offset += extent_offset;
5455 clone_root->offset += extent_offset;
5456 }
5457 }
5458
Filipe Mananad906d492015-10-02 10:47:34 +01005459 clone_len = min_t(u64, ext_len, len);
5460
5461 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
Filipe Manana3c850b42019-05-20 09:57:00 +01005462 clone_data_offset == data_offset) {
5463 const u64 src_end = clone_root->offset + clone_len;
5464 const u64 sectorsize = SZ_64K;
5465
5466 /*
5467 * We can't clone the last block, when its size is not
5468 * sector size aligned, into the middle of a file. If we
5469 * do so, the receiver will get a failure (-EINVAL) when
5470 * trying to clone or will silently corrupt the data in
5471 * the destination file if it's on a kernel without the
5472 * fix introduced by commit ac765f83f1397646
5473 * ("Btrfs: fix data corruption due to cloning of eof
5474 * block).
5475 *
5476 * So issue a clone of the aligned down range plus a
5477 * regular write for the eof block, if we hit that case.
5478 *
5479 * Also, we use the maximum possible sector size, 64K,
5480 * because we don't know what's the sector size of the
5481 * filesystem that receives the stream, so we have to
5482 * assume the largest possible sector size.
5483 */
5484 if (src_end == clone_src_i_size &&
5485 !IS_ALIGNED(src_end, sectorsize) &&
5486 offset + clone_len < sctx->cur_inode_size) {
5487 u64 slen;
5488
5489 slen = ALIGN_DOWN(src_end - clone_root->offset,
5490 sectorsize);
5491 if (slen > 0) {
5492 ret = send_clone(sctx, offset, slen,
5493 clone_root);
5494 if (ret < 0)
5495 goto out;
5496 }
5497 ret = send_extent_data(sctx, offset + slen,
5498 clone_len - slen);
5499 } else {
5500 ret = send_clone(sctx, offset, clone_len,
5501 clone_root);
5502 }
5503 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005504 ret = send_extent_data(sctx, offset, clone_len);
Filipe Manana3c850b42019-05-20 09:57:00 +01005505 }
Filipe Mananad906d492015-10-02 10:47:34 +01005506
5507 if (ret < 0)
5508 goto out;
5509
5510 len -= clone_len;
5511 if (len == 0)
5512 break;
5513 offset += clone_len;
5514 clone_root->offset += clone_len;
Filipe Manana518837e2021-01-11 11:41:42 +00005515
5516 /*
5517 * If we are cloning from the file we are currently processing,
5518 * and using the send root as the clone root, we must stop once
5519 * the current clone offset reaches the current eof of the file
5520 * at the receiver, otherwise we would issue an invalid clone
5521 * operation (source range going beyond eof) and cause the
5522 * receiver to fail. So if we reach the current eof, bail out
5523 * and fallback to a regular write.
5524 */
5525 if (clone_root->root == sctx->send_root &&
5526 clone_root->ino == sctx->cur_ino &&
5527 clone_root->offset >= sctx->cur_inode_next_write_offset)
5528 break;
5529
Filipe Mananad906d492015-10-02 10:47:34 +01005530 data_offset += clone_len;
5531next:
5532 path->slots[0]++;
5533 }
5534
5535 if (len > 0)
5536 ret = send_extent_data(sctx, offset, len);
5537 else
5538 ret = 0;
5539out:
5540 btrfs_free_path(path);
5541 return ret;
5542}
5543
Alexander Block31db9f72012-07-25 23:19:24 +02005544static int send_write_or_clone(struct send_ctx *sctx,
5545 struct btrfs_path *path,
5546 struct btrfs_key *key,
5547 struct clone_root *clone_root)
5548{
5549 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005550 u64 offset = key->offset;
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005551 u64 end;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005552 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005553
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005554 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
5555 if (offset >= end)
5556 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005557
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005558 if (clone_root && IS_ALIGNED(end, bs)) {
5559 struct btrfs_file_extent_item *ei;
Filipe Mananad906d492015-10-02 10:47:34 +01005560 u64 disk_byte;
5561 u64 data_offset;
5562
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005563 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5564 struct btrfs_file_extent_item);
Filipe Mananad906d492015-10-02 10:47:34 +01005565 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5566 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5567 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005568 offset, end - offset);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005569 } else {
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005570 ret = send_extent_data(sctx, offset, end - offset);
Alexander Block31db9f72012-07-25 23:19:24 +02005571 }
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005572 sctx->cur_inode_next_write_offset = end;
Alexander Block31db9f72012-07-25 23:19:24 +02005573 return ret;
5574}
5575
5576static int is_extent_unchanged(struct send_ctx *sctx,
5577 struct btrfs_path *left_path,
5578 struct btrfs_key *ekey)
5579{
5580 int ret = 0;
5581 struct btrfs_key key;
5582 struct btrfs_path *path = NULL;
5583 struct extent_buffer *eb;
5584 int slot;
5585 struct btrfs_key found_key;
5586 struct btrfs_file_extent_item *ei;
5587 u64 left_disknr;
5588 u64 right_disknr;
5589 u64 left_offset;
5590 u64 right_offset;
5591 u64 left_offset_fixed;
5592 u64 left_len;
5593 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005594 u64 left_gen;
5595 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005596 u8 left_type;
5597 u8 right_type;
5598
5599 path = alloc_path_for_send();
5600 if (!path)
5601 return -ENOMEM;
5602
5603 eb = left_path->nodes[0];
5604 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005605 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5606 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005607
5608 if (left_type != BTRFS_FILE_EXTENT_REG) {
5609 ret = 0;
5610 goto out;
5611 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005612 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5613 left_len = btrfs_file_extent_num_bytes(eb, ei);
5614 left_offset = btrfs_file_extent_offset(eb, ei);
5615 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005616
5617 /*
5618 * Following comments will refer to these graphics. L is the left
5619 * extents which we are checking at the moment. 1-8 are the right
5620 * extents that we iterate.
5621 *
5622 * |-----L-----|
5623 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5624 *
5625 * |-----L-----|
5626 * |--1--|-2b-|...(same as above)
5627 *
5628 * Alternative situation. Happens on files where extents got split.
5629 * |-----L-----|
5630 * |-----------7-----------|-6-|
5631 *
5632 * Alternative situation. Happens on files which got larger.
5633 * |-----L-----|
5634 * |-8-|
5635 * Nothing follows after 8.
5636 */
5637
5638 key.objectid = ekey->objectid;
5639 key.type = BTRFS_EXTENT_DATA_KEY;
5640 key.offset = ekey->offset;
5641 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5642 if (ret < 0)
5643 goto out;
5644 if (ret) {
5645 ret = 0;
5646 goto out;
5647 }
5648
5649 /*
5650 * Handle special case where the right side has no extents at all.
5651 */
5652 eb = path->nodes[0];
5653 slot = path->slots[0];
5654 btrfs_item_key_to_cpu(eb, &found_key, slot);
5655 if (found_key.objectid != key.objectid ||
5656 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005657 /* If we're a hole then just pretend nothing changed */
5658 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005659 goto out;
5660 }
5661
5662 /*
5663 * We're now on 2a, 2b or 7.
5664 */
5665 key = found_key;
5666 while (key.offset < ekey->offset + left_len) {
5667 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5668 right_type = btrfs_file_extent_type(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005669 if (right_type != BTRFS_FILE_EXTENT_REG &&
5670 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005671 ret = 0;
5672 goto out;
5673 }
5674
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005675 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005676 right_len = btrfs_file_extent_ram_bytes(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005677 right_len = PAGE_ALIGN(right_len);
5678 } else {
5679 right_len = btrfs_file_extent_num_bytes(eb, ei);
5680 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005681
Alexander Block31db9f72012-07-25 23:19:24 +02005682 /*
5683 * Are we at extent 8? If yes, we know the extent is changed.
5684 * This may only happen on the first iteration.
5685 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005686 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005687 /* If we're a hole just pretend nothing changed */
5688 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005689 goto out;
5690 }
5691
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005692 /*
5693 * We just wanted to see if when we have an inline extent, what
5694 * follows it is a regular extent (wanted to check the above
5695 * condition for inline extents too). This should normally not
5696 * happen but it's possible for example when we have an inline
5697 * compressed extent representing data with a size matching
5698 * the page size (currently the same as sector size).
5699 */
5700 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5701 ret = 0;
5702 goto out;
5703 }
5704
Filipe Manana24e52b12017-07-06 15:31:46 +01005705 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5706 right_offset = btrfs_file_extent_offset(eb, ei);
5707 right_gen = btrfs_file_extent_generation(eb, ei);
5708
Alexander Block31db9f72012-07-25 23:19:24 +02005709 left_offset_fixed = left_offset;
5710 if (key.offset < ekey->offset) {
5711 /* Fix the right offset for 2a and 7. */
5712 right_offset += ekey->offset - key.offset;
5713 } else {
5714 /* Fix the left offset for all behind 2a and 2b */
5715 left_offset_fixed += key.offset - ekey->offset;
5716 }
5717
5718 /*
5719 * Check if we have the same extent.
5720 */
Alexander Block39540962012-08-01 12:46:05 +02005721 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005722 left_offset_fixed != right_offset ||
5723 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005724 ret = 0;
5725 goto out;
5726 }
5727
5728 /*
5729 * Go to the next extent.
5730 */
5731 ret = btrfs_next_item(sctx->parent_root, path);
5732 if (ret < 0)
5733 goto out;
5734 if (!ret) {
5735 eb = path->nodes[0];
5736 slot = path->slots[0];
5737 btrfs_item_key_to_cpu(eb, &found_key, slot);
5738 }
5739 if (ret || found_key.objectid != key.objectid ||
5740 found_key.type != key.type) {
5741 key.offset += right_len;
5742 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005743 }
5744 if (found_key.offset != key.offset + right_len) {
5745 ret = 0;
5746 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005747 }
5748 key = found_key;
5749 }
5750
5751 /*
5752 * We're now behind the left extent (treat as unchanged) or at the end
5753 * of the right side (treat as changed).
5754 */
5755 if (key.offset >= ekey->offset + left_len)
5756 ret = 1;
5757 else
5758 ret = 0;
5759
5760
5761out:
5762 btrfs_free_path(path);
5763 return ret;
5764}
5765
Josef Bacik16e75492013-10-22 12:18:51 -04005766static int get_last_extent(struct send_ctx *sctx, u64 offset)
5767{
5768 struct btrfs_path *path;
5769 struct btrfs_root *root = sctx->send_root;
Josef Bacik16e75492013-10-22 12:18:51 -04005770 struct btrfs_key key;
Josef Bacik16e75492013-10-22 12:18:51 -04005771 int ret;
5772
5773 path = alloc_path_for_send();
5774 if (!path)
5775 return -ENOMEM;
5776
5777 sctx->cur_inode_last_extent = 0;
5778
5779 key.objectid = sctx->cur_ino;
5780 key.type = BTRFS_EXTENT_DATA_KEY;
5781 key.offset = offset;
5782 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5783 if (ret < 0)
5784 goto out;
5785 ret = 0;
5786 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5787 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5788 goto out;
5789
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00005790 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
Josef Bacik16e75492013-10-22 12:18:51 -04005791out:
5792 btrfs_free_path(path);
5793 return ret;
5794}
5795
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005796static int range_is_hole_in_parent(struct send_ctx *sctx,
5797 const u64 start,
5798 const u64 end)
5799{
5800 struct btrfs_path *path;
5801 struct btrfs_key key;
5802 struct btrfs_root *root = sctx->parent_root;
5803 u64 search_start = start;
5804 int ret;
5805
5806 path = alloc_path_for_send();
5807 if (!path)
5808 return -ENOMEM;
5809
5810 key.objectid = sctx->cur_ino;
5811 key.type = BTRFS_EXTENT_DATA_KEY;
5812 key.offset = search_start;
5813 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5814 if (ret < 0)
5815 goto out;
5816 if (ret > 0 && path->slots[0] > 0)
5817 path->slots[0]--;
5818
5819 while (search_start < end) {
5820 struct extent_buffer *leaf = path->nodes[0];
5821 int slot = path->slots[0];
5822 struct btrfs_file_extent_item *fi;
5823 u64 extent_end;
5824
5825 if (slot >= btrfs_header_nritems(leaf)) {
5826 ret = btrfs_next_leaf(root, path);
5827 if (ret < 0)
5828 goto out;
5829 else if (ret > 0)
5830 break;
5831 continue;
5832 }
5833
5834 btrfs_item_key_to_cpu(leaf, &key, slot);
5835 if (key.objectid < sctx->cur_ino ||
5836 key.type < BTRFS_EXTENT_DATA_KEY)
5837 goto next;
5838 if (key.objectid > sctx->cur_ino ||
5839 key.type > BTRFS_EXTENT_DATA_KEY ||
5840 key.offset >= end)
5841 break;
5842
5843 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00005844 extent_end = btrfs_file_extent_end(path);
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005845 if (extent_end <= start)
5846 goto next;
5847 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5848 search_start = extent_end;
5849 goto next;
5850 }
5851 ret = 0;
5852 goto out;
5853next:
5854 path->slots[0]++;
5855 }
5856 ret = 1;
5857out:
5858 btrfs_free_path(path);
5859 return ret;
5860}
5861
Josef Bacik16e75492013-10-22 12:18:51 -04005862static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5863 struct btrfs_key *key)
5864{
Josef Bacik16e75492013-10-22 12:18:51 -04005865 int ret = 0;
5866
5867 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5868 return 0;
5869
5870 if (sctx->cur_inode_last_extent == (u64)-1) {
5871 ret = get_last_extent(sctx, key->offset - 1);
5872 if (ret)
5873 return ret;
5874 }
5875
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005876 if (path->slots[0] == 0 &&
5877 sctx->cur_inode_last_extent < key->offset) {
5878 /*
5879 * We might have skipped entire leafs that contained only
5880 * file extent items for our current inode. These leafs have
5881 * a generation number smaller (older) than the one in the
5882 * current leaf and the leaf our last extent came from, and
5883 * are located between these 2 leafs.
5884 */
5885 ret = get_last_extent(sctx, key->offset - 1);
5886 if (ret)
5887 return ret;
5888 }
5889
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005890 if (sctx->cur_inode_last_extent < key->offset) {
5891 ret = range_is_hole_in_parent(sctx,
5892 sctx->cur_inode_last_extent,
5893 key->offset);
5894 if (ret < 0)
5895 return ret;
5896 else if (ret == 0)
5897 ret = send_hole(sctx, key->offset);
5898 else
5899 ret = 0;
5900 }
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00005901 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
Josef Bacik16e75492013-10-22 12:18:51 -04005902 return ret;
5903}
5904
Alexander Block31db9f72012-07-25 23:19:24 +02005905static int process_extent(struct send_ctx *sctx,
5906 struct btrfs_path *path,
5907 struct btrfs_key *key)
5908{
Alexander Block31db9f72012-07-25 23:19:24 +02005909 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005910 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005911
5912 if (S_ISLNK(sctx->cur_inode_mode))
5913 return 0;
5914
5915 if (sctx->parent_root && !sctx->cur_inode_new) {
5916 ret = is_extent_unchanged(sctx, path, key);
5917 if (ret < 0)
5918 goto out;
5919 if (ret) {
5920 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005921 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005922 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005923 } else {
5924 struct btrfs_file_extent_item *ei;
5925 u8 type;
5926
5927 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5928 struct btrfs_file_extent_item);
5929 type = btrfs_file_extent_type(path->nodes[0], ei);
5930 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5931 type == BTRFS_FILE_EXTENT_REG) {
5932 /*
5933 * The send spec does not have a prealloc command yet,
5934 * so just leave a hole for prealloc'ed extents until
5935 * we have enough commands queued up to justify rev'ing
5936 * the send spec.
5937 */
5938 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5939 ret = 0;
5940 goto out;
5941 }
5942
5943 /* Have a hole, just skip it. */
5944 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5945 ret = 0;
5946 goto out;
5947 }
5948 }
Alexander Block31db9f72012-07-25 23:19:24 +02005949 }
5950
5951 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5952 sctx->cur_inode_size, &found_clone);
5953 if (ret != -ENOENT && ret < 0)
5954 goto out;
5955
5956 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005957 if (ret)
5958 goto out;
5959out_hole:
5960 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005961out:
5962 return ret;
5963}
5964
5965static int process_all_extents(struct send_ctx *sctx)
5966{
5967 int ret;
5968 struct btrfs_root *root;
5969 struct btrfs_path *path;
5970 struct btrfs_key key;
5971 struct btrfs_key found_key;
5972 struct extent_buffer *eb;
5973 int slot;
5974
5975 root = sctx->send_root;
5976 path = alloc_path_for_send();
5977 if (!path)
5978 return -ENOMEM;
5979
5980 key.objectid = sctx->cmp_key->objectid;
5981 key.type = BTRFS_EXTENT_DATA_KEY;
5982 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005983 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5984 if (ret < 0)
5985 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005986
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005987 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005988 eb = path->nodes[0];
5989 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005990
5991 if (slot >= btrfs_header_nritems(eb)) {
5992 ret = btrfs_next_leaf(root, path);
5993 if (ret < 0) {
5994 goto out;
5995 } else if (ret > 0) {
5996 ret = 0;
5997 break;
5998 }
5999 continue;
6000 }
6001
Alexander Block31db9f72012-07-25 23:19:24 +02006002 btrfs_item_key_to_cpu(eb, &found_key, slot);
6003
6004 if (found_key.objectid != key.objectid ||
6005 found_key.type != key.type) {
6006 ret = 0;
6007 goto out;
6008 }
6009
6010 ret = process_extent(sctx, path, &found_key);
6011 if (ret < 0)
6012 goto out;
6013
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00006014 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02006015 }
6016
6017out:
6018 btrfs_free_path(path);
6019 return ret;
6020}
6021
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006022static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
6023 int *pending_move,
6024 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02006025{
6026 int ret = 0;
6027
6028 if (sctx->cur_ino == 0)
6029 goto out;
6030 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00006031 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02006032 goto out;
6033 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
6034 goto out;
6035
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006036 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02006037 if (ret < 0)
6038 goto out;
6039
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006040 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006041out:
6042 return ret;
6043}
6044
6045static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
6046{
6047 int ret = 0;
6048 u64 left_mode;
6049 u64 left_uid;
6050 u64 left_gid;
6051 u64 right_mode;
6052 u64 right_uid;
6053 u64 right_gid;
6054 int need_chmod = 0;
6055 int need_chown = 0;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006056 int need_truncate = 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006057 int pending_move = 0;
6058 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006059
Filipe Manana46b2f452018-07-24 11:54:04 +01006060 if (sctx->ignore_cur_inode)
6061 return 0;
6062
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006063 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
6064 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02006065 if (ret < 0)
6066 goto out;
6067
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006068 /*
6069 * We have processed the refs and thus need to advance send_progress.
6070 * Now, calls to get_cur_xxx will take the updated refs of the current
6071 * inode into account.
6072 *
6073 * On the other hand, if our current inode is a directory and couldn't
6074 * be moved/renamed because its parent was renamed/moved too and it has
6075 * a higher inode number, we can only move/rename our current inode
6076 * after we moved/renamed its parent. Therefore in this case operate on
6077 * the old path (pre move/rename) of our current inode, and the
6078 * move/rename will be performed later.
6079 */
6080 if (refs_processed && !pending_move)
6081 sctx->send_progress = sctx->cur_ino + 1;
6082
Alexander Block31db9f72012-07-25 23:19:24 +02006083 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6084 goto out;
6085 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6086 goto out;
6087
6088 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02006089 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02006090 if (ret < 0)
6091 goto out;
6092
Alex Lyakase2d044f2012-10-17 13:52:47 +00006093 if (!sctx->parent_root || sctx->cur_inode_new) {
6094 need_chown = 1;
6095 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02006096 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006097 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6098 need_truncate = 0;
Alex Lyakase2d044f2012-10-17 13:52:47 +00006099 } else {
Filipe Mananaffa7c422018-02-06 20:40:40 +00006100 u64 old_size;
6101
Alex Lyakase2d044f2012-10-17 13:52:47 +00006102 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
Filipe Mananaffa7c422018-02-06 20:40:40 +00006103 &old_size, NULL, &right_mode, &right_uid,
Alex Lyakase2d044f2012-10-17 13:52:47 +00006104 &right_gid, NULL);
6105 if (ret < 0)
6106 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006107
Alex Lyakase2d044f2012-10-17 13:52:47 +00006108 if (left_uid != right_uid || left_gid != right_gid)
6109 need_chown = 1;
6110 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6111 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006112 if ((old_size == sctx->cur_inode_size) ||
6113 (sctx->cur_inode_size > old_size &&
6114 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6115 need_truncate = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006116 }
6117
6118 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04006119 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01006120 if (sctx->cur_inode_last_extent == (u64)-1 ||
6121 sctx->cur_inode_last_extent <
6122 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04006123 ret = get_last_extent(sctx, (u64)-1);
6124 if (ret)
6125 goto out;
6126 }
6127 if (sctx->cur_inode_last_extent <
6128 sctx->cur_inode_size) {
6129 ret = send_hole(sctx, sctx->cur_inode_size);
6130 if (ret)
6131 goto out;
6132 }
6133 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00006134 if (need_truncate) {
6135 ret = send_truncate(sctx, sctx->cur_ino,
6136 sctx->cur_inode_gen,
6137 sctx->cur_inode_size);
6138 if (ret < 0)
6139 goto out;
6140 }
Alexander Block31db9f72012-07-25 23:19:24 +02006141 }
6142
6143 if (need_chown) {
6144 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6145 left_uid, left_gid);
6146 if (ret < 0)
6147 goto out;
6148 }
6149 if (need_chmod) {
6150 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6151 left_mode);
6152 if (ret < 0)
6153 goto out;
6154 }
6155
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -03006156 ret = send_capabilities(sctx);
6157 if (ret < 0)
6158 goto out;
6159
Alexander Block31db9f72012-07-25 23:19:24 +02006160 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006161 * If other directory inodes depended on our current directory
6162 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02006163 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006164 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6165 ret = apply_children_dir_moves(sctx);
6166 if (ret)
6167 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00006168 /*
6169 * Need to send that every time, no matter if it actually
6170 * changed between the two trees as we have done changes to
6171 * the inode before. If our inode is a directory and it's
6172 * waiting to be moved/renamed, we will send its utimes when
6173 * it's moved/renamed, therefore we don't need to do it here.
6174 */
6175 sctx->send_progress = sctx->cur_ino + 1;
6176 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6177 if (ret < 0)
6178 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006179 }
6180
Alexander Block31db9f72012-07-25 23:19:24 +02006181out:
6182 return ret;
6183}
6184
Filipe Manana46b2f452018-07-24 11:54:04 +01006185struct parent_paths_ctx {
6186 struct list_head *refs;
6187 struct send_ctx *sctx;
6188};
6189
6190static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6191 void *ctx)
6192{
6193 struct parent_paths_ctx *ppctx = ctx;
6194
6195 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6196 ppctx->refs);
6197}
6198
6199/*
6200 * Issue unlink operations for all paths of the current inode found in the
6201 * parent snapshot.
6202 */
6203static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6204{
6205 LIST_HEAD(deleted_refs);
6206 struct btrfs_path *path;
6207 struct btrfs_key key;
6208 struct parent_paths_ctx ctx;
6209 int ret;
6210
6211 path = alloc_path_for_send();
6212 if (!path)
6213 return -ENOMEM;
6214
6215 key.objectid = sctx->cur_ino;
6216 key.type = BTRFS_INODE_REF_KEY;
6217 key.offset = 0;
6218 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
6219 if (ret < 0)
6220 goto out;
6221
6222 ctx.refs = &deleted_refs;
6223 ctx.sctx = sctx;
6224
6225 while (true) {
6226 struct extent_buffer *eb = path->nodes[0];
6227 int slot = path->slots[0];
6228
6229 if (slot >= btrfs_header_nritems(eb)) {
6230 ret = btrfs_next_leaf(sctx->parent_root, path);
6231 if (ret < 0)
6232 goto out;
6233 else if (ret > 0)
6234 break;
6235 continue;
6236 }
6237
6238 btrfs_item_key_to_cpu(eb, &key, slot);
6239 if (key.objectid != sctx->cur_ino)
6240 break;
6241 if (key.type != BTRFS_INODE_REF_KEY &&
6242 key.type != BTRFS_INODE_EXTREF_KEY)
6243 break;
6244
6245 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
6246 record_parent_ref, &ctx);
6247 if (ret < 0)
6248 goto out;
6249
6250 path->slots[0]++;
6251 }
6252
6253 while (!list_empty(&deleted_refs)) {
6254 struct recorded_ref *ref;
6255
6256 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6257 ret = send_unlink(sctx, ref->full_path);
6258 if (ret < 0)
6259 goto out;
6260 fs_path_free(ref->full_path);
6261 list_del(&ref->list);
6262 kfree(ref);
6263 }
6264 ret = 0;
6265out:
6266 btrfs_free_path(path);
6267 if (ret)
6268 __free_recorded_refs(&deleted_refs);
6269 return ret;
6270}
6271
Alexander Block31db9f72012-07-25 23:19:24 +02006272static int changed_inode(struct send_ctx *sctx,
6273 enum btrfs_compare_tree_result result)
6274{
6275 int ret = 0;
6276 struct btrfs_key *key = sctx->cmp_key;
6277 struct btrfs_inode_item *left_ii = NULL;
6278 struct btrfs_inode_item *right_ii = NULL;
6279 u64 left_gen = 0;
6280 u64 right_gen = 0;
6281
Alexander Block31db9f72012-07-25 23:19:24 +02006282 sctx->cur_ino = key->objectid;
6283 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006284 sctx->cur_inode_last_extent = (u64)-1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006285 sctx->cur_inode_next_write_offset = 0;
Filipe Manana46b2f452018-07-24 11:54:04 +01006286 sctx->ignore_cur_inode = false;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006287
6288 /*
6289 * Set send_progress to current inode. This will tell all get_cur_xxx
6290 * functions that the current inode's refs are not updated yet. Later,
6291 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6292 */
Alexander Block31db9f72012-07-25 23:19:24 +02006293 sctx->send_progress = sctx->cur_ino;
6294
6295 if (result == BTRFS_COMPARE_TREE_NEW ||
6296 result == BTRFS_COMPARE_TREE_CHANGED) {
6297 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6298 sctx->left_path->slots[0],
6299 struct btrfs_inode_item);
6300 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6301 left_ii);
6302 } else {
6303 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6304 sctx->right_path->slots[0],
6305 struct btrfs_inode_item);
6306 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6307 right_ii);
6308 }
6309 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6310 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6311 sctx->right_path->slots[0],
6312 struct btrfs_inode_item);
6313
6314 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6315 right_ii);
Alexander Block6d85ed052012-08-01 14:48:59 +02006316
6317 /*
6318 * The cur_ino = root dir case is special here. We can't treat
6319 * the inode as deleted+reused because it would generate a
6320 * stream that tries to delete/mkdir the root dir.
6321 */
6322 if (left_gen != right_gen &&
6323 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02006324 sctx->cur_inode_new_gen = 1;
6325 }
6326
Filipe Manana46b2f452018-07-24 11:54:04 +01006327 /*
6328 * Normally we do not find inodes with a link count of zero (orphans)
6329 * because the most common case is to create a snapshot and use it
6330 * for a send operation. However other less common use cases involve
6331 * using a subvolume and send it after turning it to RO mode just
6332 * after deleting all hard links of a file while holding an open
6333 * file descriptor against it or turning a RO snapshot into RW mode,
6334 * keep an open file descriptor against a file, delete it and then
6335 * turn the snapshot back to RO mode before using it for a send
6336 * operation. So if we find such cases, ignore the inode and all its
6337 * items completely if it's a new inode, or if it's a changed inode
6338 * make sure all its previous paths (from the parent snapshot) are all
6339 * unlinked and all other the inode items are ignored.
6340 */
6341 if (result == BTRFS_COMPARE_TREE_NEW ||
6342 result == BTRFS_COMPARE_TREE_CHANGED) {
6343 u32 nlinks;
6344
6345 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6346 if (nlinks == 0) {
6347 sctx->ignore_cur_inode = true;
6348 if (result == BTRFS_COMPARE_TREE_CHANGED)
6349 ret = btrfs_unlink_all_paths(sctx);
6350 goto out;
6351 }
6352 }
6353
Alexander Block31db9f72012-07-25 23:19:24 +02006354 if (result == BTRFS_COMPARE_TREE_NEW) {
6355 sctx->cur_inode_gen = left_gen;
6356 sctx->cur_inode_new = 1;
6357 sctx->cur_inode_deleted = 0;
6358 sctx->cur_inode_size = btrfs_inode_size(
6359 sctx->left_path->nodes[0], left_ii);
6360 sctx->cur_inode_mode = btrfs_inode_mode(
6361 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006362 sctx->cur_inode_rdev = btrfs_inode_rdev(
6363 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02006364 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02006365 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006366 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6367 sctx->cur_inode_gen = right_gen;
6368 sctx->cur_inode_new = 0;
6369 sctx->cur_inode_deleted = 1;
6370 sctx->cur_inode_size = btrfs_inode_size(
6371 sctx->right_path->nodes[0], right_ii);
6372 sctx->cur_inode_mode = btrfs_inode_mode(
6373 sctx->right_path->nodes[0], right_ii);
6374 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02006375 /*
6376 * We need to do some special handling in case the inode was
6377 * reported as changed with a changed generation number. This
6378 * means that the original inode was deleted and new inode
6379 * reused the same inum. So we have to treat the old inode as
6380 * deleted and the new one as new.
6381 */
Alexander Block31db9f72012-07-25 23:19:24 +02006382 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02006383 /*
6384 * First, process the inode as if it was deleted.
6385 */
Alexander Block31db9f72012-07-25 23:19:24 +02006386 sctx->cur_inode_gen = right_gen;
6387 sctx->cur_inode_new = 0;
6388 sctx->cur_inode_deleted = 1;
6389 sctx->cur_inode_size = btrfs_inode_size(
6390 sctx->right_path->nodes[0], right_ii);
6391 sctx->cur_inode_mode = btrfs_inode_mode(
6392 sctx->right_path->nodes[0], right_ii);
6393 ret = process_all_refs(sctx,
6394 BTRFS_COMPARE_TREE_DELETED);
6395 if (ret < 0)
6396 goto out;
6397
Alexander Block766702e2012-07-28 14:11:31 +02006398 /*
6399 * Now process the inode as if it was new.
6400 */
Alexander Block31db9f72012-07-25 23:19:24 +02006401 sctx->cur_inode_gen = left_gen;
6402 sctx->cur_inode_new = 1;
6403 sctx->cur_inode_deleted = 0;
6404 sctx->cur_inode_size = btrfs_inode_size(
6405 sctx->left_path->nodes[0], left_ii);
6406 sctx->cur_inode_mode = btrfs_inode_mode(
6407 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006408 sctx->cur_inode_rdev = btrfs_inode_rdev(
6409 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02006410 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006411 if (ret < 0)
6412 goto out;
6413
6414 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6415 if (ret < 0)
6416 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006417 /*
6418 * Advance send_progress now as we did not get into
6419 * process_recorded_refs_if_needed in the new_gen case.
6420 */
6421 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02006422
6423 /*
6424 * Now process all extents and xattrs of the inode as if
6425 * they were all new.
6426 */
Alexander Block31db9f72012-07-25 23:19:24 +02006427 ret = process_all_extents(sctx);
6428 if (ret < 0)
6429 goto out;
6430 ret = process_all_new_xattrs(sctx);
6431 if (ret < 0)
6432 goto out;
6433 } else {
6434 sctx->cur_inode_gen = left_gen;
6435 sctx->cur_inode_new = 0;
6436 sctx->cur_inode_new_gen = 0;
6437 sctx->cur_inode_deleted = 0;
6438 sctx->cur_inode_size = btrfs_inode_size(
6439 sctx->left_path->nodes[0], left_ii);
6440 sctx->cur_inode_mode = btrfs_inode_mode(
6441 sctx->left_path->nodes[0], left_ii);
6442 }
6443 }
6444
6445out:
6446 return ret;
6447}
6448
Alexander Block766702e2012-07-28 14:11:31 +02006449/*
6450 * We have to process new refs before deleted refs, but compare_trees gives us
6451 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6452 * first and later process them in process_recorded_refs.
6453 * For the cur_inode_new_gen case, we skip recording completely because
6454 * changed_inode did already initiate processing of refs. The reason for this is
6455 * that in this case, compare_tree actually compares the refs of 2 different
6456 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6457 * refs of the right tree as deleted and all refs of the left tree as new.
6458 */
Alexander Block31db9f72012-07-25 23:19:24 +02006459static int changed_ref(struct send_ctx *sctx,
6460 enum btrfs_compare_tree_result result)
6461{
6462 int ret = 0;
6463
Filipe Manana95155582016-08-01 01:50:37 +01006464 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6465 inconsistent_snapshot_error(sctx, result, "reference");
6466 return -EIO;
6467 }
Alexander Block31db9f72012-07-25 23:19:24 +02006468
6469 if (!sctx->cur_inode_new_gen &&
6470 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6471 if (result == BTRFS_COMPARE_TREE_NEW)
6472 ret = record_new_ref(sctx);
6473 else if (result == BTRFS_COMPARE_TREE_DELETED)
6474 ret = record_deleted_ref(sctx);
6475 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6476 ret = record_changed_ref(sctx);
6477 }
6478
6479 return ret;
6480}
6481
Alexander Block766702e2012-07-28 14:11:31 +02006482/*
6483 * Process new/deleted/changed xattrs. We skip processing in the
6484 * cur_inode_new_gen case because changed_inode did already initiate processing
6485 * of xattrs. The reason is the same as in changed_ref
6486 */
Alexander Block31db9f72012-07-25 23:19:24 +02006487static int changed_xattr(struct send_ctx *sctx,
6488 enum btrfs_compare_tree_result result)
6489{
6490 int ret = 0;
6491
Filipe Manana95155582016-08-01 01:50:37 +01006492 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6493 inconsistent_snapshot_error(sctx, result, "xattr");
6494 return -EIO;
6495 }
Alexander Block31db9f72012-07-25 23:19:24 +02006496
6497 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6498 if (result == BTRFS_COMPARE_TREE_NEW)
6499 ret = process_new_xattr(sctx);
6500 else if (result == BTRFS_COMPARE_TREE_DELETED)
6501 ret = process_deleted_xattr(sctx);
6502 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6503 ret = process_changed_xattr(sctx);
6504 }
6505
6506 return ret;
6507}
6508
Alexander Block766702e2012-07-28 14:11:31 +02006509/*
6510 * Process new/deleted/changed extents. We skip processing in the
6511 * cur_inode_new_gen case because changed_inode did already initiate processing
6512 * of extents. The reason is the same as in changed_ref
6513 */
Alexander Block31db9f72012-07-25 23:19:24 +02006514static int changed_extent(struct send_ctx *sctx,
6515 enum btrfs_compare_tree_result result)
6516{
6517 int ret = 0;
6518
Filipe Mananab4f9a1a2019-07-17 13:23:39 +01006519 /*
6520 * We have found an extent item that changed without the inode item
6521 * having changed. This can happen either after relocation (where the
6522 * disk_bytenr of an extent item is replaced at
6523 * relocation.c:replace_file_extents()) or after deduplication into a
6524 * file in both the parent and send snapshots (where an extent item can
6525 * get modified or replaced with a new one). Note that deduplication
6526 * updates the inode item, but it only changes the iversion (sequence
6527 * field in the inode item) of the inode, so if a file is deduplicated
6528 * the same amount of times in both the parent and send snapshots, its
6529 * iversion becames the same in both snapshots, whence the inode item is
6530 * the same on both snapshots.
6531 */
6532 if (sctx->cur_ino != sctx->cmp_key->objectid)
6533 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006534
6535 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6536 if (result != BTRFS_COMPARE_TREE_DELETED)
6537 ret = process_extent(sctx, sctx->left_path,
6538 sctx->cmp_key);
6539 }
6540
6541 return ret;
6542}
6543
Josef Bacikba5e8f22013-08-16 16:52:55 -04006544static int dir_changed(struct send_ctx *sctx, u64 dir)
6545{
6546 u64 orig_gen, new_gen;
6547 int ret;
6548
6549 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6550 NULL, NULL);
6551 if (ret)
6552 return ret;
6553
6554 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6555 NULL, NULL, NULL);
6556 if (ret)
6557 return ret;
6558
6559 return (orig_gen != new_gen) ? 1 : 0;
6560}
6561
6562static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6563 struct btrfs_key *key)
6564{
6565 struct btrfs_inode_extref *extref;
6566 struct extent_buffer *leaf;
6567 u64 dirid = 0, last_dirid = 0;
6568 unsigned long ptr;
6569 u32 item_size;
6570 u32 cur_offset = 0;
6571 int ref_name_len;
6572 int ret = 0;
6573
6574 /* Easy case, just check this one dirid */
6575 if (key->type == BTRFS_INODE_REF_KEY) {
6576 dirid = key->offset;
6577
6578 ret = dir_changed(sctx, dirid);
6579 goto out;
6580 }
6581
6582 leaf = path->nodes[0];
6583 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6584 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6585 while (cur_offset < item_size) {
6586 extref = (struct btrfs_inode_extref *)(ptr +
6587 cur_offset);
6588 dirid = btrfs_inode_extref_parent(leaf, extref);
6589 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6590 cur_offset += ref_name_len + sizeof(*extref);
6591 if (dirid == last_dirid)
6592 continue;
6593 ret = dir_changed(sctx, dirid);
6594 if (ret)
6595 break;
6596 last_dirid = dirid;
6597 }
6598out:
6599 return ret;
6600}
6601
Alexander Block766702e2012-07-28 14:11:31 +02006602/*
6603 * Updates compare related fields in sctx and simply forwards to the actual
6604 * changed_xxx functions.
6605 */
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006606static int changed_cb(struct btrfs_path *left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02006607 struct btrfs_path *right_path,
6608 struct btrfs_key *key,
6609 enum btrfs_compare_tree_result result,
6610 void *ctx)
6611{
6612 int ret = 0;
6613 struct send_ctx *sctx = ctx;
6614
Josef Bacikba5e8f22013-08-16 16:52:55 -04006615 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04006616 if (key->type == BTRFS_INODE_REF_KEY ||
6617 key->type == BTRFS_INODE_EXTREF_KEY) {
6618 ret = compare_refs(sctx, left_path, key);
6619 if (!ret)
6620 return 0;
6621 if (ret < 0)
6622 return ret;
6623 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6624 return maybe_send_hole(sctx, left_path, key);
6625 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04006626 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006627 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04006628 result = BTRFS_COMPARE_TREE_CHANGED;
6629 ret = 0;
6630 }
6631
Alexander Block31db9f72012-07-25 23:19:24 +02006632 sctx->left_path = left_path;
6633 sctx->right_path = right_path;
6634 sctx->cmp_key = key;
6635
6636 ret = finish_inode_if_needed(sctx, 0);
6637 if (ret < 0)
6638 goto out;
6639
Alexander Block2981e222012-08-01 14:47:03 +02006640 /* Ignore non-FS objects */
6641 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6642 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6643 goto out;
6644
Filipe Manana46b2f452018-07-24 11:54:04 +01006645 if (key->type == BTRFS_INODE_ITEM_KEY) {
Alexander Block31db9f72012-07-25 23:19:24 +02006646 ret = changed_inode(sctx, result);
Filipe Manana46b2f452018-07-24 11:54:04 +01006647 } else if (!sctx->ignore_cur_inode) {
6648 if (key->type == BTRFS_INODE_REF_KEY ||
6649 key->type == BTRFS_INODE_EXTREF_KEY)
6650 ret = changed_ref(sctx, result);
6651 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6652 ret = changed_xattr(sctx, result);
6653 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6654 ret = changed_extent(sctx, result);
6655 }
Alexander Block31db9f72012-07-25 23:19:24 +02006656
6657out:
6658 return ret;
6659}
6660
6661static int full_send_tree(struct send_ctx *sctx)
6662{
6663 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006664 struct btrfs_root *send_root = sctx->send_root;
6665 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006666 struct btrfs_path *path;
6667 struct extent_buffer *eb;
6668 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006669
6670 path = alloc_path_for_send();
6671 if (!path)
6672 return -ENOMEM;
6673
Alexander Block31db9f72012-07-25 23:19:24 +02006674 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6675 key.type = BTRFS_INODE_ITEM_KEY;
6676 key.offset = 0;
6677
Alexander Block31db9f72012-07-25 23:19:24 +02006678 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6679 if (ret < 0)
6680 goto out;
6681 if (ret)
6682 goto out_finish;
6683
6684 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006685 eb = path->nodes[0];
6686 slot = path->slots[0];
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006687 btrfs_item_key_to_cpu(eb, &key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02006688
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006689 ret = changed_cb(path, NULL, &key,
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006690 BTRFS_COMPARE_TREE_NEW, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006691 if (ret < 0)
6692 goto out;
6693
Alexander Block31db9f72012-07-25 23:19:24 +02006694 ret = btrfs_next_item(send_root, path);
6695 if (ret < 0)
6696 goto out;
6697 if (ret) {
6698 ret = 0;
6699 break;
6700 }
6701 }
6702
6703out_finish:
6704 ret = finish_inode_if_needed(sctx, 1);
6705
6706out:
6707 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006708 return ret;
6709}
6710
David Sterba18d0f5c2019-08-21 19:12:59 +02006711static int tree_move_down(struct btrfs_path *path, int *level)
6712{
6713 struct extent_buffer *eb;
6714
6715 BUG_ON(*level == 0);
6716 eb = btrfs_read_node_slot(path->nodes[*level], path->slots[*level]);
6717 if (IS_ERR(eb))
6718 return PTR_ERR(eb);
6719
6720 path->nodes[*level - 1] = eb;
6721 path->slots[*level - 1] = 0;
6722 (*level)--;
6723 return 0;
6724}
6725
6726static int tree_move_next_or_upnext(struct btrfs_path *path,
6727 int *level, int root_level)
6728{
6729 int ret = 0;
6730 int nritems;
6731 nritems = btrfs_header_nritems(path->nodes[*level]);
6732
6733 path->slots[*level]++;
6734
6735 while (path->slots[*level] >= nritems) {
6736 if (*level == root_level)
6737 return -1;
6738
6739 /* move upnext */
6740 path->slots[*level] = 0;
6741 free_extent_buffer(path->nodes[*level]);
6742 path->nodes[*level] = NULL;
6743 (*level)++;
6744 path->slots[*level]++;
6745
6746 nritems = btrfs_header_nritems(path->nodes[*level]);
6747 ret = 1;
6748 }
6749 return ret;
6750}
6751
6752/*
6753 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
6754 * or down.
6755 */
6756static int tree_advance(struct btrfs_path *path,
6757 int *level, int root_level,
6758 int allow_down,
6759 struct btrfs_key *key)
6760{
6761 int ret;
6762
6763 if (*level == 0 || !allow_down) {
6764 ret = tree_move_next_or_upnext(path, level, root_level);
6765 } else {
6766 ret = tree_move_down(path, level);
6767 }
6768 if (ret >= 0) {
6769 if (*level == 0)
6770 btrfs_item_key_to_cpu(path->nodes[*level], key,
6771 path->slots[*level]);
6772 else
6773 btrfs_node_key_to_cpu(path->nodes[*level], key,
6774 path->slots[*level]);
6775 }
6776 return ret;
6777}
6778
6779static int tree_compare_item(struct btrfs_path *left_path,
6780 struct btrfs_path *right_path,
6781 char *tmp_buf)
6782{
6783 int cmp;
6784 int len1, len2;
6785 unsigned long off1, off2;
6786
6787 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
6788 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
6789 if (len1 != len2)
6790 return 1;
6791
6792 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
6793 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
6794 right_path->slots[0]);
6795
6796 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
6797
6798 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
6799 if (cmp)
6800 return 1;
6801 return 0;
6802}
6803
6804/*
6805 * This function compares two trees and calls the provided callback for
6806 * every changed/new/deleted item it finds.
6807 * If shared tree blocks are encountered, whole subtrees are skipped, making
6808 * the compare pretty fast on snapshotted subvolumes.
6809 *
6810 * This currently works on commit roots only. As commit roots are read only,
6811 * we don't do any locking. The commit roots are protected with transactions.
6812 * Transactions are ended and rejoined when a commit is tried in between.
6813 *
6814 * This function checks for modifications done to the trees while comparing.
6815 * If it detects a change, it aborts immediately.
6816 */
6817static int btrfs_compare_trees(struct btrfs_root *left_root,
David Sterba1b51d6f2020-08-17 12:16:57 +02006818 struct btrfs_root *right_root, void *ctx)
David Sterba18d0f5c2019-08-21 19:12:59 +02006819{
6820 struct btrfs_fs_info *fs_info = left_root->fs_info;
6821 int ret;
6822 int cmp;
6823 struct btrfs_path *left_path = NULL;
6824 struct btrfs_path *right_path = NULL;
6825 struct btrfs_key left_key;
6826 struct btrfs_key right_key;
6827 char *tmp_buf = NULL;
6828 int left_root_level;
6829 int right_root_level;
6830 int left_level;
6831 int right_level;
6832 int left_end_reached;
6833 int right_end_reached;
6834 int advance_left;
6835 int advance_right;
6836 u64 left_blockptr;
6837 u64 right_blockptr;
6838 u64 left_gen;
6839 u64 right_gen;
6840
6841 left_path = btrfs_alloc_path();
6842 if (!left_path) {
6843 ret = -ENOMEM;
6844 goto out;
6845 }
6846 right_path = btrfs_alloc_path();
6847 if (!right_path) {
6848 ret = -ENOMEM;
6849 goto out;
6850 }
6851
6852 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
6853 if (!tmp_buf) {
6854 ret = -ENOMEM;
6855 goto out;
6856 }
6857
6858 left_path->search_commit_root = 1;
6859 left_path->skip_locking = 1;
6860 right_path->search_commit_root = 1;
6861 right_path->skip_locking = 1;
6862
6863 /*
6864 * Strategy: Go to the first items of both trees. Then do
6865 *
6866 * If both trees are at level 0
6867 * Compare keys of current items
6868 * If left < right treat left item as new, advance left tree
6869 * and repeat
6870 * If left > right treat right item as deleted, advance right tree
6871 * and repeat
6872 * If left == right do deep compare of items, treat as changed if
6873 * needed, advance both trees and repeat
6874 * If both trees are at the same level but not at level 0
6875 * Compare keys of current nodes/leafs
6876 * If left < right advance left tree and repeat
6877 * If left > right advance right tree and repeat
6878 * If left == right compare blockptrs of the next nodes/leafs
6879 * If they match advance both trees but stay at the same level
6880 * and repeat
6881 * If they don't match advance both trees while allowing to go
6882 * deeper and repeat
6883 * If tree levels are different
6884 * Advance the tree that needs it and repeat
6885 *
6886 * Advancing a tree means:
6887 * If we are at level 0, try to go to the next slot. If that's not
6888 * possible, go one level up and repeat. Stop when we found a level
6889 * where we could go to the next slot. We may at this point be on a
6890 * node or a leaf.
6891 *
6892 * If we are not at level 0 and not on shared tree blocks, go one
6893 * level deeper.
6894 *
6895 * If we are not at level 0 and on shared tree blocks, go one slot to
6896 * the right if possible or go up and right.
6897 */
6898
6899 down_read(&fs_info->commit_root_sem);
6900 left_level = btrfs_header_level(left_root->commit_root);
6901 left_root_level = left_level;
6902 left_path->nodes[left_level] =
6903 btrfs_clone_extent_buffer(left_root->commit_root);
6904 if (!left_path->nodes[left_level]) {
6905 up_read(&fs_info->commit_root_sem);
6906 ret = -ENOMEM;
6907 goto out;
6908 }
6909
6910 right_level = btrfs_header_level(right_root->commit_root);
6911 right_root_level = right_level;
6912 right_path->nodes[right_level] =
6913 btrfs_clone_extent_buffer(right_root->commit_root);
6914 if (!right_path->nodes[right_level]) {
6915 up_read(&fs_info->commit_root_sem);
6916 ret = -ENOMEM;
6917 goto out;
6918 }
6919 up_read(&fs_info->commit_root_sem);
6920
6921 if (left_level == 0)
6922 btrfs_item_key_to_cpu(left_path->nodes[left_level],
6923 &left_key, left_path->slots[left_level]);
6924 else
6925 btrfs_node_key_to_cpu(left_path->nodes[left_level],
6926 &left_key, left_path->slots[left_level]);
6927 if (right_level == 0)
6928 btrfs_item_key_to_cpu(right_path->nodes[right_level],
6929 &right_key, right_path->slots[right_level]);
6930 else
6931 btrfs_node_key_to_cpu(right_path->nodes[right_level],
6932 &right_key, right_path->slots[right_level]);
6933
6934 left_end_reached = right_end_reached = 0;
6935 advance_left = advance_right = 0;
6936
6937 while (1) {
Nikolay Borisov6af112b2019-09-04 19:33:58 +03006938 cond_resched();
David Sterba18d0f5c2019-08-21 19:12:59 +02006939 if (advance_left && !left_end_reached) {
6940 ret = tree_advance(left_path, &left_level,
6941 left_root_level,
6942 advance_left != ADVANCE_ONLY_NEXT,
6943 &left_key);
6944 if (ret == -1)
6945 left_end_reached = ADVANCE;
6946 else if (ret < 0)
6947 goto out;
6948 advance_left = 0;
6949 }
6950 if (advance_right && !right_end_reached) {
6951 ret = tree_advance(right_path, &right_level,
6952 right_root_level,
6953 advance_right != ADVANCE_ONLY_NEXT,
6954 &right_key);
6955 if (ret == -1)
6956 right_end_reached = ADVANCE;
6957 else if (ret < 0)
6958 goto out;
6959 advance_right = 0;
6960 }
6961
6962 if (left_end_reached && right_end_reached) {
6963 ret = 0;
6964 goto out;
6965 } else if (left_end_reached) {
6966 if (right_level == 0) {
6967 ret = changed_cb(left_path, right_path,
6968 &right_key,
6969 BTRFS_COMPARE_TREE_DELETED,
6970 ctx);
6971 if (ret < 0)
6972 goto out;
6973 }
6974 advance_right = ADVANCE;
6975 continue;
6976 } else if (right_end_reached) {
6977 if (left_level == 0) {
6978 ret = changed_cb(left_path, right_path,
6979 &left_key,
6980 BTRFS_COMPARE_TREE_NEW,
6981 ctx);
6982 if (ret < 0)
6983 goto out;
6984 }
6985 advance_left = ADVANCE;
6986 continue;
6987 }
6988
6989 if (left_level == 0 && right_level == 0) {
6990 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
6991 if (cmp < 0) {
6992 ret = changed_cb(left_path, right_path,
6993 &left_key,
6994 BTRFS_COMPARE_TREE_NEW,
6995 ctx);
6996 if (ret < 0)
6997 goto out;
6998 advance_left = ADVANCE;
6999 } else if (cmp > 0) {
7000 ret = changed_cb(left_path, right_path,
7001 &right_key,
7002 BTRFS_COMPARE_TREE_DELETED,
7003 ctx);
7004 if (ret < 0)
7005 goto out;
7006 advance_right = ADVANCE;
7007 } else {
7008 enum btrfs_compare_tree_result result;
7009
7010 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
7011 ret = tree_compare_item(left_path, right_path,
7012 tmp_buf);
7013 if (ret)
7014 result = BTRFS_COMPARE_TREE_CHANGED;
7015 else
7016 result = BTRFS_COMPARE_TREE_SAME;
7017 ret = changed_cb(left_path, right_path,
7018 &left_key, result, ctx);
7019 if (ret < 0)
7020 goto out;
7021 advance_left = ADVANCE;
7022 advance_right = ADVANCE;
7023 }
7024 } else if (left_level == right_level) {
7025 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7026 if (cmp < 0) {
7027 advance_left = ADVANCE;
7028 } else if (cmp > 0) {
7029 advance_right = ADVANCE;
7030 } else {
7031 left_blockptr = btrfs_node_blockptr(
7032 left_path->nodes[left_level],
7033 left_path->slots[left_level]);
7034 right_blockptr = btrfs_node_blockptr(
7035 right_path->nodes[right_level],
7036 right_path->slots[right_level]);
7037 left_gen = btrfs_node_ptr_generation(
7038 left_path->nodes[left_level],
7039 left_path->slots[left_level]);
7040 right_gen = btrfs_node_ptr_generation(
7041 right_path->nodes[right_level],
7042 right_path->slots[right_level]);
7043 if (left_blockptr == right_blockptr &&
7044 left_gen == right_gen) {
7045 /*
7046 * As we're on a shared block, don't
7047 * allow to go deeper.
7048 */
7049 advance_left = ADVANCE_ONLY_NEXT;
7050 advance_right = ADVANCE_ONLY_NEXT;
7051 } else {
7052 advance_left = ADVANCE;
7053 advance_right = ADVANCE;
7054 }
7055 }
7056 } else if (left_level < right_level) {
7057 advance_right = ADVANCE;
7058 } else {
7059 advance_left = ADVANCE;
7060 }
7061 }
7062
7063out:
7064 btrfs_free_path(left_path);
7065 btrfs_free_path(right_path);
7066 kvfree(tmp_buf);
7067 return ret;
7068}
7069
Alexander Block31db9f72012-07-25 23:19:24 +02007070static int send_subvol(struct send_ctx *sctx)
7071{
7072 int ret;
7073
Stefan Behrensc2c71322013-04-10 17:10:52 +00007074 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
7075 ret = send_header(sctx);
7076 if (ret < 0)
7077 goto out;
7078 }
Alexander Block31db9f72012-07-25 23:19:24 +02007079
7080 ret = send_subvol_begin(sctx);
7081 if (ret < 0)
7082 goto out;
7083
7084 if (sctx->parent_root) {
David Sterba1b51d6f2020-08-17 12:16:57 +02007085 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02007086 if (ret < 0)
7087 goto out;
7088 ret = finish_inode_if_needed(sctx, 1);
7089 if (ret < 0)
7090 goto out;
7091 } else {
7092 ret = full_send_tree(sctx);
7093 if (ret < 0)
7094 goto out;
7095 }
7096
7097out:
Alexander Block31db9f72012-07-25 23:19:24 +02007098 free_recorded_refs(sctx);
7099 return ret;
7100}
7101
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007102/*
7103 * If orphan cleanup did remove any orphans from a root, it means the tree
7104 * was modified and therefore the commit root is not the same as the current
7105 * root anymore. This is a problem, because send uses the commit root and
7106 * therefore can see inode items that don't exist in the current root anymore,
7107 * and for example make calls to btrfs_iget, which will do tree lookups based
7108 * on the current root and not on the commit root. Those lookups will fail,
7109 * returning a -ESTALE error, and making send fail with that error. So make
7110 * sure a send does not see any orphans we have just removed, and that it will
7111 * see the same inodes regardless of whether a transaction commit happened
7112 * before it started (meaning that the commit root will be the same as the
7113 * current root) or not.
7114 */
7115static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
7116{
7117 int i;
7118 struct btrfs_trans_handle *trans = NULL;
7119
7120again:
7121 if (sctx->parent_root &&
7122 sctx->parent_root->node != sctx->parent_root->commit_root)
7123 goto commit_trans;
7124
7125 for (i = 0; i < sctx->clone_roots_cnt; i++)
7126 if (sctx->clone_roots[i].root->node !=
7127 sctx->clone_roots[i].root->commit_root)
7128 goto commit_trans;
7129
7130 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007131 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007132
7133 return 0;
7134
7135commit_trans:
7136 /* Use any root, all fs roots will get their commit roots updated. */
7137 if (!trans) {
7138 trans = btrfs_join_transaction(sctx->send_root);
7139 if (IS_ERR(trans))
7140 return PTR_ERR(trans);
7141 goto again;
7142 }
7143
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007144 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007145}
7146
Filipe Manana9f89d5d2019-04-15 09:29:36 +01007147/*
7148 * Make sure any existing dellaloc is flushed for any root used by a send
7149 * operation so that we do not miss any data and we do not race with writeback
7150 * finishing and changing a tree while send is using the tree. This could
7151 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
7152 * a send operation then uses the subvolume.
7153 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
7154 */
7155static int flush_delalloc_roots(struct send_ctx *sctx)
7156{
7157 struct btrfs_root *root = sctx->parent_root;
7158 int ret;
7159 int i;
7160
7161 if (root) {
7162 ret = btrfs_start_delalloc_snapshot(root);
7163 if (ret)
7164 return ret;
7165 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7166 }
7167
7168 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7169 root = sctx->clone_roots[i].root;
7170 ret = btrfs_start_delalloc_snapshot(root);
7171 if (ret)
7172 return ret;
7173 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7174 }
7175
7176 return 0;
7177}
7178
David Sterba66ef7d62013-12-17 15:07:20 +01007179static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
7180{
7181 spin_lock(&root->root_item_lock);
7182 root->send_in_progress--;
7183 /*
7184 * Not much left to do, we don't know why it's unbalanced and
7185 * can't blindly reset it to 0.
7186 */
7187 if (root->send_in_progress < 0)
7188 btrfs_err(root->fs_info,
Colin Ian Kingf5686e32018-05-04 12:11:12 +01007189 "send_in_progress unbalanced %d root %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007190 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01007191 spin_unlock(&root->root_item_lock);
7192}
7193
Filipe Manana62d54f32019-04-22 16:43:42 +01007194static void dedupe_in_progress_warn(const struct btrfs_root *root)
7195{
7196 btrfs_warn_rl(root->fs_info,
7197"cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
7198 root->root_key.objectid, root->dedupe_in_progress);
7199}
7200
Josef Bacik2351f432017-09-27 10:43:13 -04007201long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
Alexander Block31db9f72012-07-25 23:19:24 +02007202{
7203 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007204 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
7205 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02007206 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02007207 struct send_ctx *sctx = NULL;
7208 u32 i;
7209 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01007210 int clone_sources_to_rollback = 0;
Denis Efremovbae12df2020-09-21 20:03:36 +03007211 size_t alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08007212 int sort_clone_roots = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02007213
7214 if (!capable(CAP_SYS_ADMIN))
7215 return -EPERM;
7216
Josef Bacik139f8072013-05-20 11:26:50 -04007217 /*
David Sterba2c686532013-12-16 17:34:17 +01007218 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02007219 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01007220 */
7221 spin_lock(&send_root->root_item_lock);
Filipe Manana62d54f32019-04-22 16:43:42 +01007222 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
7223 dedupe_in_progress_warn(send_root);
7224 spin_unlock(&send_root->root_item_lock);
7225 return -EAGAIN;
7226 }
David Sterba2c686532013-12-16 17:34:17 +01007227 send_root->send_in_progress++;
7228 spin_unlock(&send_root->root_item_lock);
7229
7230 /*
David Sterba2c686532013-12-16 17:34:17 +01007231 * Userspace tools do the checks and warn the user if it's
7232 * not RO.
7233 */
7234 if (!btrfs_root_readonly(send_root)) {
7235 ret = -EPERM;
7236 goto out;
7237 }
7238
Dan Carpenter457ae722017-03-17 23:51:20 +03007239 /*
7240 * Check that we don't overflow at later allocations, we request
7241 * clone_sources_count + 1 items, and compare to unsigned long inside
7242 * access_ok.
7243 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03007244 if (arg->clone_sources_count >
Dan Carpenter457ae722017-03-17 23:51:20 +03007245 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03007246 ret = -EINVAL;
7247 goto out;
7248 }
7249
Stefan Behrensc2c71322013-04-10 17:10:52 +00007250 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00007251 ret = -EINVAL;
7252 goto out;
7253 }
7254
David Sterbae780b0d2016-01-18 18:42:13 +01007255 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007256 if (!sctx) {
7257 ret = -ENOMEM;
7258 goto out;
7259 }
7260
7261 INIT_LIST_HEAD(&sctx->new_refs);
7262 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01007263 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007264 INIT_LIST_HEAD(&sctx->name_cache_list);
7265
Mark Fashehcb95e7b2013-02-04 20:54:57 +00007266 sctx->flags = arg->flags;
7267
Alexander Block31db9f72012-07-25 23:19:24 +02007268 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00007269 if (!sctx->send_filp) {
7270 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02007271 goto out;
7272 }
7273
Alexander Block31db9f72012-07-25 23:19:24 +02007274 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02007275 /*
7276 * Unlikely but possible, if the subvolume is marked for deletion but
7277 * is slow to remove the directory entry, send can still be started
7278 */
7279 if (btrfs_root_dead(sctx->send_root)) {
7280 ret = -EPERM;
7281 goto out;
7282 }
7283
Alexander Block31db9f72012-07-25 23:19:24 +02007284 sctx->clone_roots_cnt = arg->clone_sources_count;
7285
7286 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
Michal Hocko752ade62017-05-08 15:57:27 -07007287 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007288 if (!sctx->send_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07007289 ret = -ENOMEM;
7290 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007291 }
7292
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007293 sctx->pending_dir_moves = RB_ROOT;
7294 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00007295 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007296
Denis Efremovbae12df2020-09-21 20:03:36 +03007297 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots),
7298 arg->clone_sources_count + 1,
7299 GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007300 if (!sctx->clone_roots) {
David Sterba818e0102017-05-31 18:40:02 +02007301 ret = -ENOMEM;
7302 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007303 }
7304
Denis Efremovbae12df2020-09-21 20:03:36 +03007305 alloc_size = array_size(sizeof(*arg->clone_sources),
7306 arg->clone_sources_count);
David Sterbae55d1152016-04-11 18:52:02 +02007307
Alexander Block31db9f72012-07-25 23:19:24 +02007308 if (arg->clone_sources_count) {
Michal Hocko752ade62017-05-08 15:57:27 -07007309 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007310 if (!clone_sources_tmp) {
Michal Hocko752ade62017-05-08 15:57:27 -07007311 ret = -ENOMEM;
7312 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007313 }
7314
7315 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02007316 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02007317 if (ret) {
7318 ret = -EFAULT;
7319 goto out;
7320 }
7321
7322 for (i = 0; i < arg->clone_sources_count; i++) {
David Sterba56e93572020-05-15 19:35:55 +02007323 clone_root = btrfs_get_fs_root(fs_info,
7324 clone_sources_tmp[i], true);
Alexander Block31db9f72012-07-25 23:19:24 +02007325 if (IS_ERR(clone_root)) {
7326 ret = PTR_ERR(clone_root);
7327 goto out;
7328 }
David Sterba2c686532013-12-16 17:34:17 +01007329 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00007330 if (!btrfs_root_readonly(clone_root) ||
7331 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01007332 spin_unlock(&clone_root->root_item_lock);
Josef Bacik00246522020-01-24 09:33:01 -05007333 btrfs_put_root(clone_root);
David Sterba2c686532013-12-16 17:34:17 +01007334 ret = -EPERM;
7335 goto out;
7336 }
Filipe Manana62d54f32019-04-22 16:43:42 +01007337 if (clone_root->dedupe_in_progress) {
7338 dedupe_in_progress_warn(clone_root);
7339 spin_unlock(&clone_root->root_item_lock);
Josef Bacik00246522020-01-24 09:33:01 -05007340 btrfs_put_root(clone_root);
Filipe Manana62d54f32019-04-22 16:43:42 +01007341 ret = -EAGAIN;
7342 goto out;
7343 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00007344 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01007345 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08007346
Alexander Block31db9f72012-07-25 23:19:24 +02007347 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00007348 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02007349 }
David Sterba2f913062016-04-11 18:40:08 +02007350 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02007351 clone_sources_tmp = NULL;
7352 }
7353
7354 if (arg->parent_root) {
David Sterba56e93572020-05-15 19:35:55 +02007355 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
7356 true);
Stefan Behrensb1b19592013-05-13 14:42:57 +00007357 if (IS_ERR(sctx->parent_root)) {
7358 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02007359 goto out;
7360 }
Wang Shilong18f687d2014-01-07 17:25:19 +08007361
David Sterba2c686532013-12-16 17:34:17 +01007362 spin_lock(&sctx->parent_root->root_item_lock);
7363 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02007364 if (!btrfs_root_readonly(sctx->parent_root) ||
7365 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01007366 spin_unlock(&sctx->parent_root->root_item_lock);
7367 ret = -EPERM;
7368 goto out;
7369 }
Filipe Manana62d54f32019-04-22 16:43:42 +01007370 if (sctx->parent_root->dedupe_in_progress) {
7371 dedupe_in_progress_warn(sctx->parent_root);
7372 spin_unlock(&sctx->parent_root->root_item_lock);
Filipe Manana62d54f32019-04-22 16:43:42 +01007373 ret = -EAGAIN;
7374 goto out;
7375 }
David Sterba2c686532013-12-16 17:34:17 +01007376 spin_unlock(&sctx->parent_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02007377 }
7378
7379 /*
7380 * Clones from send_root are allowed, but only if the clone source
7381 * is behind the current send position. This is checked while searching
7382 * for possible clone sources.
7383 */
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007384 sctx->clone_roots[sctx->clone_roots_cnt++].root =
Josef Bacik00246522020-01-24 09:33:01 -05007385 btrfs_grab_root(sctx->send_root);
Alexander Block31db9f72012-07-25 23:19:24 +02007386
7387 /* We do a bsearch later */
7388 sort(sctx->clone_roots, sctx->clone_roots_cnt,
7389 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
7390 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08007391 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02007392
Filipe Manana9f89d5d2019-04-15 09:29:36 +01007393 ret = flush_delalloc_roots(sctx);
7394 if (ret)
7395 goto out;
7396
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007397 ret = ensure_commit_roots_uptodate(sctx);
7398 if (ret)
7399 goto out;
7400
Filipe Manana9e967492019-04-22 16:44:09 +01007401 mutex_lock(&fs_info->balance_mutex);
7402 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
7403 mutex_unlock(&fs_info->balance_mutex);
7404 btrfs_warn_rl(fs_info,
7405 "cannot run send because a balance operation is in progress");
7406 ret = -EAGAIN;
7407 goto out;
7408 }
7409 fs_info->send_in_progress++;
7410 mutex_unlock(&fs_info->balance_mutex);
7411
David Sterba2755a0d2014-07-31 00:43:18 +02007412 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02007413 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04007414 current->journal_info = NULL;
Filipe Manana9e967492019-04-22 16:44:09 +01007415 mutex_lock(&fs_info->balance_mutex);
7416 fs_info->send_in_progress--;
7417 mutex_unlock(&fs_info->balance_mutex);
Alexander Block31db9f72012-07-25 23:19:24 +02007418 if (ret < 0)
7419 goto out;
7420
Stefan Behrensc2c71322013-04-10 17:10:52 +00007421 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
7422 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
7423 if (ret < 0)
7424 goto out;
7425 ret = send_cmd(sctx);
7426 if (ret < 0)
7427 goto out;
7428 }
Alexander Block31db9f72012-07-25 23:19:24 +02007429
7430out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007431 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7432 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7433 struct rb_node *n;
7434 struct pending_dir_move *pm;
7435
7436 n = rb_first(&sctx->pending_dir_moves);
7437 pm = rb_entry(n, struct pending_dir_move, node);
7438 while (!list_empty(&pm->list)) {
7439 struct pending_dir_move *pm2;
7440
7441 pm2 = list_first_entry(&pm->list,
7442 struct pending_dir_move, list);
7443 free_pending_move(sctx, pm2);
7444 }
7445 free_pending_move(sctx, pm);
7446 }
7447
7448 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7449 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7450 struct rb_node *n;
7451 struct waiting_dir_move *dm;
7452
7453 n = rb_first(&sctx->waiting_dir_moves);
7454 dm = rb_entry(n, struct waiting_dir_move, node);
7455 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7456 kfree(dm);
7457 }
7458
Filipe Manana9dc44212014-02-19 14:31:44 +00007459 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
7460 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
7461 struct rb_node *n;
7462 struct orphan_dir_info *odi;
7463
7464 n = rb_first(&sctx->orphan_dirs);
7465 odi = rb_entry(n, struct orphan_dir_info, node);
7466 free_orphan_dir_info(sctx, odi);
7467 }
7468
Wang Shilong896c14f2014-01-07 17:25:18 +08007469 if (sort_clone_roots) {
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007470 for (i = 0; i < sctx->clone_roots_cnt; i++) {
Wang Shilong896c14f2014-01-07 17:25:18 +08007471 btrfs_root_dec_send_in_progress(
7472 sctx->clone_roots[i].root);
Josef Bacik00246522020-01-24 09:33:01 -05007473 btrfs_put_root(sctx->clone_roots[i].root);
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007474 }
Wang Shilong896c14f2014-01-07 17:25:18 +08007475 } else {
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007476 for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
Wang Shilong896c14f2014-01-07 17:25:18 +08007477 btrfs_root_dec_send_in_progress(
7478 sctx->clone_roots[i].root);
Josef Bacik00246522020-01-24 09:33:01 -05007479 btrfs_put_root(sctx->clone_roots[i].root);
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007480 }
Wang Shilong896c14f2014-01-07 17:25:18 +08007481
7482 btrfs_root_dec_send_in_progress(send_root);
7483 }
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007484 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
David Sterba66ef7d62013-12-17 15:07:20 +01007485 btrfs_root_dec_send_in_progress(sctx->parent_root);
Josef Bacik00246522020-01-24 09:33:01 -05007486 btrfs_put_root(sctx->parent_root);
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007487 }
David Sterba2c686532013-12-16 17:34:17 +01007488
David Sterba2f913062016-04-11 18:40:08 +02007489 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02007490
7491 if (sctx) {
7492 if (sctx->send_filp)
7493 fput(sctx->send_filp);
7494
David Sterbac03d01f2016-04-11 18:40:08 +02007495 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02007496 kvfree(sctx->send_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02007497
7498 name_cache_free(sctx);
7499
7500 kfree(sctx);
7501 }
7502
7503 return ret;
7504}