blob: 040324d71118893420674d78818d98bdf889fa65 [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 */
David Sterbae77fbf92021-10-22 16:53:36 +020087 /* Protocol version compatibility requested */
88 u32 proto;
Alexander Block31db9f72012-07-25 23:19:24 +020089
Alexander Block31db9f72012-07-25 23:19:24 +020090 struct btrfs_root *send_root;
91 struct btrfs_root *parent_root;
92 struct clone_root *clone_roots;
93 int clone_roots_cnt;
94
95 /* current state of the compare_tree call */
96 struct btrfs_path *left_path;
97 struct btrfs_path *right_path;
98 struct btrfs_key *cmp_key;
99
100 /*
101 * infos of the currently processed inode. In case of deleted inodes,
102 * these are the values from the deleted inode.
103 */
104 u64 cur_ino;
105 u64 cur_inode_gen;
106 int cur_inode_new;
107 int cur_inode_new_gen;
108 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200109 u64 cur_inode_size;
110 u64 cur_inode_mode;
Liu Bo644d1942014-02-27 17:29:01 +0800111 u64 cur_inode_rdev;
Josef Bacik16e75492013-10-22 12:18:51 -0400112 u64 cur_inode_last_extent;
Filipe Mananaffa7c422018-02-06 20:40:40 +0000113 u64 cur_inode_next_write_offset;
Filipe Manana46b2f452018-07-24 11:54:04 +0100114 bool ignore_cur_inode;
Alexander Block31db9f72012-07-25 23:19:24 +0200115
116 u64 send_progress;
117
118 struct list_head new_refs;
119 struct list_head deleted_refs;
120
121 struct radix_tree_root name_cache;
122 struct list_head name_cache_list;
123 int name_cache_size;
124
Liu Bo2131bcd2014-03-05 10:07:35 +0800125 struct file_ra_state ra;
126
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000127 /*
128 * We process inodes by their increasing order, so if before an
129 * incremental send we reverse the parent/child relationship of
130 * directories such that a directory with a lower inode number was
131 * the parent of a directory with a higher inode number, and the one
132 * becoming the new parent got renamed too, we can't rename/move the
133 * directory with lower inode number when we finish processing it - we
134 * must process the directory with higher inode number first, then
135 * rename/move it and then rename/move the directory with lower inode
136 * number. Example follows.
137 *
138 * Tree state when the first send was performed:
139 *
140 * .
141 * |-- a (ino 257)
142 * |-- b (ino 258)
143 * |
144 * |
145 * |-- c (ino 259)
146 * | |-- d (ino 260)
147 * |
148 * |-- c2 (ino 261)
149 *
150 * Tree state when the second (incremental) send is performed:
151 *
152 * .
153 * |-- a (ino 257)
154 * |-- b (ino 258)
155 * |-- c2 (ino 261)
156 * |-- d2 (ino 260)
157 * |-- cc (ino 259)
158 *
159 * The sequence of steps that lead to the second state was:
160 *
161 * mv /a/b/c/d /a/b/c2/d2
162 * mv /a/b/c /a/b/c2/d2/cc
163 *
164 * "c" has lower inode number, but we can't move it (2nd mv operation)
165 * before we move "d", which has higher inode number.
166 *
167 * So we just memorize which move/rename operations must be performed
168 * later when their respective parent is processed and moved/renamed.
169 */
170
171 /* Indexed by parent directory inode number. */
172 struct rb_root pending_dir_moves;
173
174 /*
175 * Reverse index, indexed by the inode number of a directory that
176 * is waiting for the move/rename of its immediate parent before its
177 * own move/rename can be performed.
178 */
179 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000180
181 /*
182 * A directory that is going to be rm'ed might have a child directory
183 * which is in the pending directory moves index above. In this case,
184 * the directory can only be removed after the move/rename of its child
185 * is performed. Example:
186 *
187 * Parent snapshot:
188 *
189 * . (ino 256)
190 * |-- a/ (ino 257)
191 * |-- b/ (ino 258)
192 * |-- c/ (ino 259)
193 * | |-- x/ (ino 260)
194 * |
195 * |-- y/ (ino 261)
196 *
197 * Send snapshot:
198 *
199 * . (ino 256)
200 * |-- a/ (ino 257)
201 * |-- b/ (ino 258)
202 * |-- YY/ (ino 261)
203 * |-- x/ (ino 260)
204 *
205 * Sequence of steps that lead to the send snapshot:
206 * rm -f /a/b/c/foo.txt
207 * mv /a/b/y /a/b/YY
208 * mv /a/b/c/x /a/b/YY
209 * rmdir /a/b/c
210 *
211 * When the child is processed, its move/rename is delayed until its
212 * parent is processed (as explained above), but all other operations
213 * like update utimes, chown, chgrp, etc, are performed and the paths
214 * that it uses for those operations must use the orphanized name of
215 * its parent (the directory we're going to rm later), so we need to
216 * memorize that name.
217 *
218 * Indexed by the inode number of the directory to be deleted.
219 */
220 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000221};
222
223struct pending_dir_move {
224 struct rb_node node;
225 struct list_head list;
226 u64 parent_ino;
227 u64 ino;
228 u64 gen;
229 struct list_head update_refs;
230};
231
232struct waiting_dir_move {
233 struct rb_node node;
234 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000235 /*
236 * There might be some directory that could not be removed because it
237 * was waiting for this directory inode to be moved first. Therefore
238 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
239 */
240 u64 rmdir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +0000241 u64 rmdir_gen;
Filipe Manana8b191a62015-04-09 14:09:14 +0100242 bool orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +0000243};
244
245struct orphan_dir_info {
246 struct rb_node node;
247 u64 ino;
248 u64 gen;
Robbie Ko0f96f512018-05-08 18:11:38 +0800249 u64 last_dir_index_offset;
Alexander Block31db9f72012-07-25 23:19:24 +0200250};
251
252struct name_cache_entry {
253 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200254 /*
255 * radix_tree has only 32bit entries but we need to handle 64bit inums.
256 * We use the lower 32bit of the 64bit inum to store it in the tree. If
257 * more then one inum would fall into the same entry, we use radix_list
258 * to store the additional entries. radix_list is also used to store
259 * entries where two entries have the same inum but different
260 * generations.
261 */
262 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200263 u64 ino;
264 u64 gen;
265 u64 parent_ino;
266 u64 parent_gen;
267 int ret;
268 int need_later_update;
269 int name_len;
270 char name[];
271};
272
David Sterba18d0f5c2019-08-21 19:12:59 +0200273#define ADVANCE 1
274#define ADVANCE_ONLY_NEXT -1
275
276enum btrfs_compare_tree_result {
277 BTRFS_COMPARE_TREE_NEW,
278 BTRFS_COMPARE_TREE_DELETED,
279 BTRFS_COMPARE_TREE_CHANGED,
280 BTRFS_COMPARE_TREE_SAME,
281};
David Sterba18d0f5c2019-08-21 19:12:59 +0200282
David Sterbae67c7182018-02-19 17:24:18 +0100283__cold
Filipe Manana95155582016-08-01 01:50:37 +0100284static void inconsistent_snapshot_error(struct send_ctx *sctx,
285 enum btrfs_compare_tree_result result,
286 const char *what)
287{
288 const char *result_string;
289
290 switch (result) {
291 case BTRFS_COMPARE_TREE_NEW:
292 result_string = "new";
293 break;
294 case BTRFS_COMPARE_TREE_DELETED:
295 result_string = "deleted";
296 break;
297 case BTRFS_COMPARE_TREE_CHANGED:
298 result_string = "updated";
299 break;
300 case BTRFS_COMPARE_TREE_SAME:
301 ASSERT(0);
302 result_string = "unchanged";
303 break;
304 default:
305 ASSERT(0);
306 result_string = "unexpected";
307 }
308
309 btrfs_err(sctx->send_root->fs_info,
310 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
311 result_string, what, sctx->cmp_key->objectid,
312 sctx->send_root->root_key.objectid,
313 (sctx->parent_root ?
314 sctx->parent_root->root_key.objectid : 0));
315}
316
David Sterbae77fbf92021-10-22 16:53:36 +0200317__maybe_unused
318static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd)
319{
320 switch (sctx->proto) {
321 case 1: return cmd < __BTRFS_SEND_C_MAX_V1;
322 case 2: return cmd < __BTRFS_SEND_C_MAX_V2;
323 default: return false;
324 }
325}
326
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000327static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
328
Filipe Manana9dc44212014-02-19 14:31:44 +0000329static struct waiting_dir_move *
330get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
331
Filipe Manana0b3f4072020-12-10 12:09:02 +0000332static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
Filipe Manana9dc44212014-02-19 14:31:44 +0000333
Josef Bacik16e75492013-10-22 12:18:51 -0400334static int need_send_hole(struct send_ctx *sctx)
335{
336 return (sctx->parent_root && !sctx->cur_inode_new &&
337 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
338 S_ISREG(sctx->cur_inode_mode));
339}
340
Alexander Block31db9f72012-07-25 23:19:24 +0200341static void fs_path_reset(struct fs_path *p)
342{
343 if (p->reversed) {
344 p->start = p->buf + p->buf_len - 1;
345 p->end = p->start;
346 *p->start = 0;
347 } else {
348 p->start = p->buf;
349 p->end = p->start;
350 *p->start = 0;
351 }
352}
353
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000354static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200355{
356 struct fs_path *p;
357
David Sterbae780b0d2016-01-18 18:42:13 +0100358 p = kmalloc(sizeof(*p), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +0200359 if (!p)
360 return NULL;
361 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200362 p->buf = p->inline_buf;
363 p->buf_len = FS_PATH_INLINE_SIZE;
364 fs_path_reset(p);
365 return p;
366}
367
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000368static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200369{
370 struct fs_path *p;
371
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000372 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200373 if (!p)
374 return NULL;
375 p->reversed = 1;
376 fs_path_reset(p);
377 return p;
378}
379
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000380static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200381{
382 if (!p)
383 return;
David Sterbaace01052014-02-05 16:17:34 +0100384 if (p->buf != p->inline_buf)
385 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200386 kfree(p);
387}
388
389static int fs_path_len(struct fs_path *p)
390{
391 return p->end - p->start;
392}
393
394static int fs_path_ensure_buf(struct fs_path *p, int len)
395{
396 char *tmp_buf;
397 int path_len;
398 int old_buf_len;
399
400 len++;
401
402 if (p->buf_len >= len)
403 return 0;
404
Chris Masoncfd4a532014-04-26 05:02:03 -0700405 if (len > PATH_MAX) {
406 WARN_ON(1);
407 return -ENOMEM;
408 }
409
David Sterba1b2782c2014-02-25 19:32:59 +0100410 path_len = p->end - p->start;
411 old_buf_len = p->buf_len;
412
David Sterbaace01052014-02-05 16:17:34 +0100413 /*
414 * First time the inline_buf does not suffice
415 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100416 if (p->buf == p->inline_buf) {
David Sterbae780b0d2016-01-18 18:42:13 +0100417 tmp_buf = kmalloc(len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100418 if (tmp_buf)
419 memcpy(tmp_buf, p->buf, old_buf_len);
420 } else {
David Sterbae780b0d2016-01-18 18:42:13 +0100421 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100422 }
David Sterba9c9ca002014-02-25 19:33:08 +0100423 if (!tmp_buf)
424 return -ENOMEM;
425 p->buf = tmp_buf;
426 /*
427 * The real size of the buffer is bigger, this will let the fast path
428 * happen most of the time
429 */
430 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100431
Alexander Block31db9f72012-07-25 23:19:24 +0200432 if (p->reversed) {
433 tmp_buf = p->buf + old_buf_len - path_len - 1;
434 p->end = p->buf + p->buf_len - 1;
435 p->start = p->end - path_len;
436 memmove(p->start, tmp_buf, path_len + 1);
437 } else {
438 p->start = p->buf;
439 p->end = p->start + path_len;
440 }
441 return 0;
442}
443
David Sterbab23ab572014-02-03 19:23:19 +0100444static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
445 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200446{
447 int ret;
448 int new_len;
449
450 new_len = p->end - p->start + name_len;
451 if (p->start != p->end)
452 new_len++;
453 ret = fs_path_ensure_buf(p, new_len);
454 if (ret < 0)
455 goto out;
456
457 if (p->reversed) {
458 if (p->start != p->end)
459 *--p->start = '/';
460 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100461 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200462 } else {
463 if (p->start != p->end)
464 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100465 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200466 p->end += name_len;
467 *p->end = 0;
468 }
469
470out:
471 return ret;
472}
473
474static int fs_path_add(struct fs_path *p, const char *name, int name_len)
475{
476 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100477 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200478
David Sterbab23ab572014-02-03 19:23:19 +0100479 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200480 if (ret < 0)
481 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100482 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200483
484out:
485 return ret;
486}
487
488static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
489{
490 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100491 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200492
David Sterbab23ab572014-02-03 19:23:19 +0100493 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200494 if (ret < 0)
495 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100496 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200497
498out:
499 return ret;
500}
501
502static int fs_path_add_from_extent_buffer(struct fs_path *p,
503 struct extent_buffer *eb,
504 unsigned long off, int len)
505{
506 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100507 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200508
David Sterbab23ab572014-02-03 19:23:19 +0100509 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200510 if (ret < 0)
511 goto out;
512
David Sterbab23ab572014-02-03 19:23:19 +0100513 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200514
515out:
516 return ret;
517}
518
Alexander Block31db9f72012-07-25 23:19:24 +0200519static int fs_path_copy(struct fs_path *p, struct fs_path *from)
520{
521 int ret;
522
523 p->reversed = from->reversed;
524 fs_path_reset(p);
525
526 ret = fs_path_add_path(p, from);
527
528 return ret;
529}
530
531
532static void fs_path_unreverse(struct fs_path *p)
533{
534 char *tmp;
535 int len;
536
537 if (!p->reversed)
538 return;
539
540 tmp = p->start;
541 len = p->end - p->start;
542 p->start = p->buf;
543 p->end = p->start + len;
544 memmove(p->start, tmp, len + 1);
545 p->reversed = 0;
546}
547
548static struct btrfs_path *alloc_path_for_send(void)
549{
550 struct btrfs_path *path;
551
552 path = btrfs_alloc_path();
553 if (!path)
554 return NULL;
555 path->search_commit_root = 1;
556 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400557 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200558 return path;
559}
560
Eric Sandeen48a3b632013-04-25 20:41:01 +0000561static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200562{
563 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200564 u32 pos = 0;
565
Alexander Block31db9f72012-07-25 23:19:24 +0200566 while (pos < len) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200567 ret = kernel_write(filp, buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200568 /* TODO handle that correctly */
569 /*if (ret == -ERESTARTSYS) {
570 continue;
571 }*/
572 if (ret < 0)
Christoph Hellwig8e931572017-09-01 17:39:19 +0200573 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200574 if (ret == 0) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200575 return -EIO;
Alexander Block31db9f72012-07-25 23:19:24 +0200576 }
577 pos += ret;
578 }
579
Christoph Hellwig8e931572017-09-01 17:39:19 +0200580 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200581}
582
583static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
584{
585 struct btrfs_tlv_header *hdr;
586 int total_len = sizeof(*hdr) + len;
587 int left = sctx->send_max_size - sctx->send_size;
588
589 if (unlikely(left < total_len))
590 return -EOVERFLOW;
591
592 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
David Sterbae2f896b2020-09-15 10:54:23 +0200593 put_unaligned_le16(attr, &hdr->tlv_type);
594 put_unaligned_le16(len, &hdr->tlv_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200595 memcpy(hdr + 1, data, len);
596 sctx->send_size += total_len;
597
598 return 0;
599}
600
David Sterba95bc79d2013-12-16 17:34:10 +0100601#define TLV_PUT_DEFINE_INT(bits) \
602 static int tlv_put_u##bits(struct send_ctx *sctx, \
603 u##bits attr, u##bits value) \
604 { \
605 __le##bits __tmp = cpu_to_le##bits(value); \
606 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
607 }
Alexander Block31db9f72012-07-25 23:19:24 +0200608
David Sterba95bc79d2013-12-16 17:34:10 +0100609TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200610
611static int tlv_put_string(struct send_ctx *sctx, u16 attr,
612 const char *str, int len)
613{
614 if (len == -1)
615 len = strlen(str);
616 return tlv_put(sctx, attr, str, len);
617}
618
619static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
620 const u8 *uuid)
621{
622 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
623}
624
Alexander Block31db9f72012-07-25 23:19:24 +0200625static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
626 struct extent_buffer *eb,
627 struct btrfs_timespec *ts)
628{
629 struct btrfs_timespec bts;
630 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
631 return tlv_put(sctx, attr, &bts, sizeof(bts));
632}
633
634
Liu Bo895a72b2018-03-02 18:05:49 -0700635#define TLV_PUT(sctx, attrtype, data, attrlen) \
Alexander Block31db9f72012-07-25 23:19:24 +0200636 do { \
Liu Bo895a72b2018-03-02 18:05:49 -0700637 ret = tlv_put(sctx, attrtype, data, attrlen); \
Alexander Block31db9f72012-07-25 23:19:24 +0200638 if (ret < 0) \
639 goto tlv_put_failure; \
640 } while (0)
641
642#define TLV_PUT_INT(sctx, attrtype, bits, value) \
643 do { \
644 ret = tlv_put_u##bits(sctx, attrtype, value); \
645 if (ret < 0) \
646 goto tlv_put_failure; \
647 } while (0)
648
649#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
650#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
651#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
652#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
653#define TLV_PUT_STRING(sctx, attrtype, str, len) \
654 do { \
655 ret = tlv_put_string(sctx, attrtype, str, len); \
656 if (ret < 0) \
657 goto tlv_put_failure; \
658 } while (0)
659#define TLV_PUT_PATH(sctx, attrtype, p) \
660 do { \
661 ret = tlv_put_string(sctx, attrtype, p->start, \
662 p->end - p->start); \
663 if (ret < 0) \
664 goto tlv_put_failure; \
665 } while(0)
666#define TLV_PUT_UUID(sctx, attrtype, uuid) \
667 do { \
668 ret = tlv_put_uuid(sctx, attrtype, uuid); \
669 if (ret < 0) \
670 goto tlv_put_failure; \
671 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200672#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
673 do { \
674 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
675 if (ret < 0) \
676 goto tlv_put_failure; \
677 } while (0)
678
679static int send_header(struct send_ctx *sctx)
680{
681 struct btrfs_stream_header hdr;
682
683 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
684 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
685
Anand Jain1bcea352012-09-14 00:04:21 -0600686 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
687 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200688}
689
690/*
691 * For each command/item we want to send to userspace, we call this function.
692 */
693static int begin_cmd(struct send_ctx *sctx, int cmd)
694{
695 struct btrfs_cmd_header *hdr;
696
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530697 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200698 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200699
700 BUG_ON(sctx->send_size);
701
702 sctx->send_size += sizeof(*hdr);
703 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
David Sterbae2f896b2020-09-15 10:54:23 +0200704 put_unaligned_le16(cmd, &hdr->cmd);
Alexander Block31db9f72012-07-25 23:19:24 +0200705
706 return 0;
707}
708
709static int send_cmd(struct send_ctx *sctx)
710{
711 int ret;
712 struct btrfs_cmd_header *hdr;
713 u32 crc;
714
715 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
David Sterbae2f896b2020-09-15 10:54:23 +0200716 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
717 put_unaligned_le32(0, &hdr->crc);
Alexander Block31db9f72012-07-25 23:19:24 +0200718
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200719 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
David Sterbae2f896b2020-09-15 10:54:23 +0200720 put_unaligned_le32(crc, &hdr->crc);
Alexander Block31db9f72012-07-25 23:19:24 +0200721
Anand Jain1bcea352012-09-14 00:04:21 -0600722 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
723 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200724
725 sctx->total_send_size += sctx->send_size;
David Sterbae2f896b2020-09-15 10:54:23 +0200726 sctx->cmd_send_size[get_unaligned_le16(&hdr->cmd)] += sctx->send_size;
Alexander Block31db9f72012-07-25 23:19:24 +0200727 sctx->send_size = 0;
728
729 return ret;
730}
731
732/*
733 * Sends a move instruction to user space
734 */
735static int send_rename(struct send_ctx *sctx,
736 struct fs_path *from, struct fs_path *to)
737{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400738 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200739 int ret;
740
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400741 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200742
743 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
744 if (ret < 0)
745 goto out;
746
747 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
748 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
749
750 ret = send_cmd(sctx);
751
752tlv_put_failure:
753out:
754 return ret;
755}
756
757/*
758 * Sends a link instruction to user space
759 */
760static int send_link(struct send_ctx *sctx,
761 struct fs_path *path, struct fs_path *lnk)
762{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400763 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200764 int ret;
765
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400766 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200767
768 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
769 if (ret < 0)
770 goto out;
771
772 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
773 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
774
775 ret = send_cmd(sctx);
776
777tlv_put_failure:
778out:
779 return ret;
780}
781
782/*
783 * Sends an unlink instruction to user space
784 */
785static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
786{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400787 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200788 int ret;
789
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400790 btrfs_debug(fs_info, "send_unlink %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200791
792 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
793 if (ret < 0)
794 goto out;
795
796 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
797
798 ret = send_cmd(sctx);
799
800tlv_put_failure:
801out:
802 return ret;
803}
804
805/*
806 * Sends a rmdir instruction to user space
807 */
808static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
809{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400810 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200811 int ret;
812
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400813 btrfs_debug(fs_info, "send_rmdir %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200814
815 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
816 if (ret < 0)
817 goto out;
818
819 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
820
821 ret = send_cmd(sctx);
822
823tlv_put_failure:
824out:
825 return ret;
826}
827
828/*
829 * Helper function to retrieve some fields from an inode item.
830 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400831static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
832 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
833 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200834{
835 int ret;
836 struct btrfs_inode_item *ii;
837 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200838
839 key.objectid = ino;
840 key.type = BTRFS_INODE_ITEM_KEY;
841 key.offset = 0;
842 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200843 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400844 if (ret > 0)
845 ret = -ENOENT;
846 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200847 }
848
849 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
850 struct btrfs_inode_item);
851 if (size)
852 *size = btrfs_inode_size(path->nodes[0], ii);
853 if (gen)
854 *gen = btrfs_inode_generation(path->nodes[0], ii);
855 if (mode)
856 *mode = btrfs_inode_mode(path->nodes[0], ii);
857 if (uid)
858 *uid = btrfs_inode_uid(path->nodes[0], ii);
859 if (gid)
860 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200861 if (rdev)
862 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200863
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400864 return ret;
865}
866
867static int get_inode_info(struct btrfs_root *root,
868 u64 ino, u64 *size, u64 *gen,
869 u64 *mode, u64 *uid, u64 *gid,
870 u64 *rdev)
871{
872 struct btrfs_path *path;
873 int ret;
874
875 path = alloc_path_for_send();
876 if (!path)
877 return -ENOMEM;
878 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
879 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200880 btrfs_free_path(path);
881 return ret;
882}
883
884typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
885 struct fs_path *p,
886 void *ctx);
887
888/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000889 * Helper function to iterate the entries in ONE btrfs_inode_ref or
890 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200891 * The iterate callback may return a non zero value to stop iteration. This can
892 * be a negative value for error codes or 1 to simply stop it.
893 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000894 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200895 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000896static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200897 struct btrfs_key *found_key, int resolve,
898 iterate_inode_ref_t iterate, void *ctx)
899{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000900 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200901 struct btrfs_item *item;
902 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000903 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200904 struct btrfs_path *tmp_path;
905 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000906 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200907 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000908 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200909 u32 name_len;
910 char *start;
911 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000912 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200913 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000914 u64 dir;
915 unsigned long name_off;
916 unsigned long elem_size;
917 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200918
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000919 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200920 if (!p)
921 return -ENOMEM;
922
923 tmp_path = alloc_path_for_send();
924 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000925 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200926 return -ENOMEM;
927 }
928
Alexander Block31db9f72012-07-25 23:19:24 +0200929
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000930 if (found_key->type == BTRFS_INODE_REF_KEY) {
931 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
932 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100933 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000934 total = btrfs_item_size(eb, item);
935 elem_size = sizeof(*iref);
936 } else {
937 ptr = btrfs_item_ptr_offset(eb, slot);
938 total = btrfs_item_size_nr(eb, slot);
939 elem_size = sizeof(*extref);
940 }
941
Alexander Block31db9f72012-07-25 23:19:24 +0200942 while (cur < total) {
943 fs_path_reset(p);
944
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000945 if (found_key->type == BTRFS_INODE_REF_KEY) {
946 iref = (struct btrfs_inode_ref *)(ptr + cur);
947 name_len = btrfs_inode_ref_name_len(eb, iref);
948 name_off = (unsigned long)(iref + 1);
949 index = btrfs_inode_ref_index(eb, iref);
950 dir = found_key->offset;
951 } else {
952 extref = (struct btrfs_inode_extref *)(ptr + cur);
953 name_len = btrfs_inode_extref_name_len(eb, extref);
954 name_off = (unsigned long)&extref->name;
955 index = btrfs_inode_extref_index(eb, extref);
956 dir = btrfs_inode_extref_parent(eb, extref);
957 }
958
Alexander Block31db9f72012-07-25 23:19:24 +0200959 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000960 start = btrfs_ref_to_path(root, tmp_path, name_len,
961 name_off, eb, dir,
962 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200963 if (IS_ERR(start)) {
964 ret = PTR_ERR(start);
965 goto out;
966 }
967 if (start < p->buf) {
968 /* overflow , try again with larger buffer */
969 ret = fs_path_ensure_buf(p,
970 p->buf_len + p->buf - start);
971 if (ret < 0)
972 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000973 start = btrfs_ref_to_path(root, tmp_path,
974 name_len, name_off,
975 eb, dir,
976 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200977 if (IS_ERR(start)) {
978 ret = PTR_ERR(start);
979 goto out;
980 }
981 BUG_ON(start < p->buf);
982 }
983 p->start = start;
984 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000985 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
986 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200987 if (ret < 0)
988 goto out;
989 }
990
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000991 cur += elem_size + name_len;
992 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200993 if (ret)
994 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200995 num++;
996 }
997
998out:
999 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001000 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02001001 return ret;
1002}
1003
1004typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
1005 const char *name, int name_len,
1006 const char *data, int data_len,
1007 u8 type, void *ctx);
1008
1009/*
1010 * Helper function to iterate the entries in ONE btrfs_dir_item.
1011 * The iterate callback may return a non zero value to stop iteration. This can
1012 * be a negative value for error codes or 1 to simply stop it.
1013 *
1014 * path must point to the dir item when called.
1015 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001016static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +02001017 iterate_dir_item_t iterate, void *ctx)
1018{
1019 int ret = 0;
1020 struct extent_buffer *eb;
1021 struct btrfs_item *item;
1022 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +02001023 struct btrfs_key di_key;
1024 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +01001025 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +02001026 u32 name_len;
1027 u32 data_len;
1028 u32 cur;
1029 u32 len;
1030 u32 total;
1031 int slot;
1032 int num;
1033 u8 type;
1034
Filipe Manana4395e0c2014-08-20 10:45:45 +01001035 /*
1036 * Start with a small buffer (1 page). If later we end up needing more
1037 * space, which can happen for xattrs on a fs with a leaf size greater
1038 * then the page size, attempt to increase the buffer. Typically xattr
1039 * values are small.
1040 */
1041 buf_len = PATH_MAX;
David Sterbae780b0d2016-01-18 18:42:13 +01001042 buf = kmalloc(buf_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02001043 if (!buf) {
1044 ret = -ENOMEM;
1045 goto out;
1046 }
1047
Alexander Block31db9f72012-07-25 23:19:24 +02001048 eb = path->nodes[0];
1049 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001050 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001051 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1052 cur = 0;
1053 len = 0;
1054 total = btrfs_item_size(eb, item);
1055
1056 num = 0;
1057 while (cur < total) {
1058 name_len = btrfs_dir_name_len(eb, di);
1059 data_len = btrfs_dir_data_len(eb, di);
1060 type = btrfs_dir_type(eb, di);
1061 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1062
Filipe Manana7e3ae332014-05-23 20:15:16 +01001063 if (type == BTRFS_FT_XATTR) {
1064 if (name_len > XATTR_NAME_MAX) {
1065 ret = -ENAMETOOLONG;
1066 goto out;
1067 }
Jeff Mahoneyda170662016-06-15 09:22:56 -04001068 if (name_len + data_len >
1069 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001070 ret = -E2BIG;
1071 goto out;
1072 }
1073 } else {
1074 /*
1075 * Path too long
1076 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001077 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001078 ret = -ENAMETOOLONG;
1079 goto out;
1080 }
Alexander Block31db9f72012-07-25 23:19:24 +02001081 }
1082
Filipe Manana4395e0c2014-08-20 10:45:45 +01001083 if (name_len + data_len > buf_len) {
1084 buf_len = name_len + data_len;
1085 if (is_vmalloc_addr(buf)) {
1086 vfree(buf);
1087 buf = NULL;
1088 } else {
1089 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001090 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001091
1092 if (!tmp)
1093 kfree(buf);
1094 buf = tmp;
1095 }
1096 if (!buf) {
David Sterbaf11f7442017-05-31 18:40:02 +02001097 buf = kvmalloc(buf_len, GFP_KERNEL);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001098 if (!buf) {
1099 ret = -ENOMEM;
1100 goto out;
1101 }
1102 }
1103 }
1104
Alexander Block31db9f72012-07-25 23:19:24 +02001105 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1106 name_len + data_len);
1107
1108 len = sizeof(*di) + name_len + data_len;
1109 di = (struct btrfs_dir_item *)((char *)di + len);
1110 cur += len;
1111
1112 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1113 data_len, type, ctx);
1114 if (ret < 0)
1115 goto out;
1116 if (ret) {
1117 ret = 0;
1118 goto out;
1119 }
1120
1121 num++;
1122 }
1123
1124out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001125 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001126 return ret;
1127}
1128
1129static int __copy_first_ref(int num, u64 dir, int index,
1130 struct fs_path *p, void *ctx)
1131{
1132 int ret;
1133 struct fs_path *pt = ctx;
1134
1135 ret = fs_path_copy(pt, p);
1136 if (ret < 0)
1137 return ret;
1138
1139 /* we want the first only */
1140 return 1;
1141}
1142
1143/*
1144 * Retrieve the first path of an inode. If an inode has more then one
1145 * ref/hardlink, this is ignored.
1146 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001147static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001148 u64 ino, struct fs_path *path)
1149{
1150 int ret;
1151 struct btrfs_key key, found_key;
1152 struct btrfs_path *p;
1153
1154 p = alloc_path_for_send();
1155 if (!p)
1156 return -ENOMEM;
1157
1158 fs_path_reset(path);
1159
1160 key.objectid = ino;
1161 key.type = BTRFS_INODE_REF_KEY;
1162 key.offset = 0;
1163
1164 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1165 if (ret < 0)
1166 goto out;
1167 if (ret) {
1168 ret = 1;
1169 goto out;
1170 }
1171 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1172 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001173 (found_key.type != BTRFS_INODE_REF_KEY &&
1174 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001175 ret = -ENOENT;
1176 goto out;
1177 }
1178
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001179 ret = iterate_inode_ref(root, p, &found_key, 1,
1180 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001181 if (ret < 0)
1182 goto out;
1183 ret = 0;
1184
1185out:
1186 btrfs_free_path(p);
1187 return ret;
1188}
1189
1190struct backref_ctx {
1191 struct send_ctx *sctx;
1192
1193 /* number of total found references */
1194 u64 found;
1195
1196 /*
1197 * used for clones found in send_root. clones found behind cur_objectid
1198 * and cur_offset are not considered as allowed clones.
1199 */
1200 u64 cur_objectid;
1201 u64 cur_offset;
1202
1203 /* may be truncated in case it's the last extent in a file */
1204 u64 extent_len;
1205
1206 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001207 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001208};
1209
1210static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1211{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001212 u64 root = (u64)(uintptr_t)key;
David Sterba214cc182021-07-26 14:15:26 +02001213 const struct clone_root *cr = elt;
Alexander Block31db9f72012-07-25 23:19:24 +02001214
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001215 if (root < cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001216 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001217 if (root > cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001218 return 1;
1219 return 0;
1220}
1221
1222static int __clone_root_cmp_sort(const void *e1, const void *e2)
1223{
David Sterba214cc182021-07-26 14:15:26 +02001224 const struct clone_root *cr1 = e1;
1225 const struct clone_root *cr2 = e2;
Alexander Block31db9f72012-07-25 23:19:24 +02001226
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001227 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001228 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001229 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001230 return 1;
1231 return 0;
1232}
1233
1234/*
1235 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001236 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001237 */
1238static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1239{
1240 struct backref_ctx *bctx = ctx_;
1241 struct clone_root *found;
Alexander Block31db9f72012-07-25 23:19:24 +02001242
1243 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001244 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001245 bctx->sctx->clone_roots_cnt,
1246 sizeof(struct clone_root),
1247 __clone_root_cmp_bsearch);
1248 if (!found)
1249 return 0;
1250
1251 if (found->root == bctx->sctx->send_root &&
1252 ino == bctx->cur_objectid &&
1253 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001254 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001255 }
1256
1257 /*
Alexander Block31db9f72012-07-25 23:19:24 +02001258 * Make sure we don't consider clones from send_root that are
1259 * behind the current inode/offset.
1260 */
1261 if (found->root == bctx->sctx->send_root) {
1262 /*
Filipe Manana11f20692019-10-30 12:23:11 +00001263 * If the source inode was not yet processed we can't issue a
1264 * clone operation, as the source extent does not exist yet at
1265 * the destination of the stream.
Alexander Block31db9f72012-07-25 23:19:24 +02001266 */
Filipe Manana11f20692019-10-30 12:23:11 +00001267 if (ino > bctx->cur_objectid)
1268 return 0;
1269 /*
1270 * We clone from the inode currently being sent as long as the
1271 * source extent is already processed, otherwise we could try
1272 * to clone from an extent that does not exist yet at the
1273 * destination of the stream.
1274 */
1275 if (ino == bctx->cur_objectid &&
Filipe Manana9722b102020-01-29 17:09:53 +00001276 offset + bctx->extent_len >
1277 bctx->sctx->cur_inode_next_write_offset)
Alexander Block31db9f72012-07-25 23:19:24 +02001278 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001279 }
1280
1281 bctx->found++;
1282 found->found_refs++;
1283 if (ino < found->ino) {
1284 found->ino = ino;
1285 found->offset = offset;
1286 } else if (found->ino == ino) {
1287 /*
1288 * same extent found more then once in the same file.
1289 */
1290 if (found->offset > offset + bctx->extent_len)
1291 found->offset = offset;
1292 }
1293
1294 return 0;
1295}
1296
1297/*
Alexander Block766702e2012-07-28 14:11:31 +02001298 * Given an inode, offset and extent item, it finds a good clone for a clone
1299 * instruction. Returns -ENOENT when none could be found. The function makes
1300 * sure that the returned clone is usable at the point where sending is at the
1301 * moment. This means, that no clones are accepted which lie behind the current
1302 * inode+offset.
1303 *
Alexander Block31db9f72012-07-25 23:19:24 +02001304 * path must point to the extent item when called.
1305 */
1306static int find_extent_clone(struct send_ctx *sctx,
1307 struct btrfs_path *path,
1308 u64 ino, u64 data_offset,
1309 u64 ino_size,
1310 struct clone_root **found)
1311{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001312 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001313 int ret;
1314 int extent_type;
1315 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001316 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001317 u64 num_bytes;
1318 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001319 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001320 struct btrfs_file_extent_item *fi;
1321 struct extent_buffer *eb = path->nodes[0];
Goldwyn Rodriguesdce28152021-07-27 16:17:31 -05001322 struct backref_ctx backref_ctx = {0};
Alexander Block31db9f72012-07-25 23:19:24 +02001323 struct clone_root *cur_clone_root;
1324 struct btrfs_key found_key;
1325 struct btrfs_path *tmp_path;
Filipe Mananafd0ddbe2019-10-30 12:23:01 +00001326 struct btrfs_extent_item *ei;
Chris Mason74dd17f2012-08-07 16:25:13 -04001327 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001328 u32 i;
1329
1330 tmp_path = alloc_path_for_send();
1331 if (!tmp_path)
1332 return -ENOMEM;
1333
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001334 /* We only use this path under the commit sem */
1335 tmp_path->need_commit_sem = 0;
1336
Alexander Block31db9f72012-07-25 23:19:24 +02001337 if (data_offset >= ino_size) {
1338 /*
1339 * There may be extents that lie behind the file's size.
1340 * I at least had this in combination with snapshotting while
1341 * writing large files.
1342 */
1343 ret = 0;
1344 goto out;
1345 }
1346
1347 fi = btrfs_item_ptr(eb, path->slots[0],
1348 struct btrfs_file_extent_item);
1349 extent_type = btrfs_file_extent_type(eb, fi);
1350 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1351 ret = -ENOENT;
1352 goto out;
1353 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001354 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001355
1356 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001357 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1358 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001359 ret = -ENOENT;
1360 goto out;
1361 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001362 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001363
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001364 down_read(&fs_info->commit_root_sem);
1365 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001366 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001367 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001368
1369 if (ret < 0)
1370 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001371 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001372 ret = -EIO;
1373 goto out;
1374 }
1375
Filipe Mananafd0ddbe2019-10-30 12:23:01 +00001376 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1377 struct btrfs_extent_item);
1378 /*
1379 * Backreference walking (iterate_extent_inodes() below) is currently
1380 * too expensive when an extent has a large number of references, both
1381 * in time spent and used memory. So for now just fallback to write
1382 * operations instead of clone operations when an extent has more than
1383 * a certain amount of references.
1384 */
1385 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1386 ret = -ENOENT;
1387 goto out;
1388 }
1389 btrfs_release_path(tmp_path);
1390
Alexander Block31db9f72012-07-25 23:19:24 +02001391 /*
1392 * Setup the clone roots.
1393 */
1394 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1395 cur_clone_root = sctx->clone_roots + i;
1396 cur_clone_root->ino = (u64)-1;
1397 cur_clone_root->offset = 0;
1398 cur_clone_root->found_refs = 0;
1399 }
1400
Goldwyn Rodriguesdce28152021-07-27 16:17:31 -05001401 backref_ctx.sctx = sctx;
1402 backref_ctx.found = 0;
1403 backref_ctx.cur_objectid = ino;
1404 backref_ctx.cur_offset = data_offset;
1405 backref_ctx.found_itself = 0;
1406 backref_ctx.extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001407
1408 /*
1409 * The last extent of a file may be too large due to page alignment.
1410 * We need to adjust extent_len in this case so that the checks in
1411 * __iterate_backrefs work.
1412 */
1413 if (data_offset + num_bytes >= ino_size)
Goldwyn Rodriguesdce28152021-07-27 16:17:31 -05001414 backref_ctx.extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001415
1416 /*
1417 * Now collect all backrefs.
1418 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001419 if (compressed == BTRFS_COMPRESS_NONE)
1420 extent_item_pos = logical - found_key.objectid;
1421 else
1422 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001423 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1424 extent_item_pos, 1, __iterate_backrefs,
Goldwyn Rodriguesdce28152021-07-27 16:17:31 -05001425 &backref_ctx, false);
Chris Mason74dd17f2012-08-07 16:25:13 -04001426
Alexander Block31db9f72012-07-25 23:19:24 +02001427 if (ret < 0)
1428 goto out;
1429
Goldwyn Rodriguesdce28152021-07-27 16:17:31 -05001430 if (!backref_ctx.found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001431 /* found a bug in backref code? */
1432 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001433 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001434 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001435 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001436 goto out;
1437 }
1438
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001439 btrfs_debug(fs_info,
1440 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1441 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001442
Goldwyn Rodriguesdce28152021-07-27 16:17:31 -05001443 if (!backref_ctx.found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001444 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001445
1446 cur_clone_root = NULL;
1447 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1448 if (sctx->clone_roots[i].found_refs) {
1449 if (!cur_clone_root)
1450 cur_clone_root = sctx->clone_roots + i;
1451 else if (sctx->clone_roots[i].root == sctx->send_root)
1452 /* prefer clones from send_root over others */
1453 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001454 }
1455
1456 }
1457
1458 if (cur_clone_root) {
1459 *found = cur_clone_root;
1460 ret = 0;
1461 } else {
1462 ret = -ENOENT;
1463 }
1464
1465out:
1466 btrfs_free_path(tmp_path);
1467 return ret;
1468}
1469
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001470static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001471 u64 ino,
1472 struct fs_path *dest)
1473{
1474 int ret;
1475 struct btrfs_path *path;
1476 struct btrfs_key key;
1477 struct btrfs_file_extent_item *ei;
1478 u8 type;
1479 u8 compression;
1480 unsigned long off;
1481 int len;
1482
1483 path = alloc_path_for_send();
1484 if (!path)
1485 return -ENOMEM;
1486
1487 key.objectid = ino;
1488 key.type = BTRFS_EXTENT_DATA_KEY;
1489 key.offset = 0;
1490 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1491 if (ret < 0)
1492 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001493 if (ret) {
1494 /*
1495 * An empty symlink inode. Can happen in rare error paths when
1496 * creating a symlink (transaction committed before the inode
1497 * eviction handler removed the symlink inode items and a crash
1498 * happened in between or the subvol was snapshoted in between).
1499 * Print an informative message to dmesg/syslog so that the user
1500 * can delete the symlink.
1501 */
1502 btrfs_err(root->fs_info,
1503 "Found empty symlink inode %llu at root %llu",
1504 ino, root->root_key.objectid);
1505 ret = -EIO;
1506 goto out;
1507 }
Alexander Block31db9f72012-07-25 23:19:24 +02001508
1509 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1510 struct btrfs_file_extent_item);
1511 type = btrfs_file_extent_type(path->nodes[0], ei);
1512 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1513 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1514 BUG_ON(compression);
1515
1516 off = btrfs_file_extent_inline_start(ei);
Qu Wenruoe41ca582018-06-06 15:41:49 +08001517 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001518
1519 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001520
1521out:
1522 btrfs_free_path(path);
1523 return ret;
1524}
1525
1526/*
1527 * Helper function to generate a file name that is unique in the root of
1528 * send_root and parent_root. This is used to generate names for orphan inodes.
1529 */
1530static int gen_unique_name(struct send_ctx *sctx,
1531 u64 ino, u64 gen,
1532 struct fs_path *dest)
1533{
1534 int ret = 0;
1535 struct btrfs_path *path;
1536 struct btrfs_dir_item *di;
1537 char tmp[64];
1538 int len;
1539 u64 idx = 0;
1540
1541 path = alloc_path_for_send();
1542 if (!path)
1543 return -ENOMEM;
1544
1545 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001546 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001547 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001548 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001549
1550 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1551 path, BTRFS_FIRST_FREE_OBJECTID,
1552 tmp, strlen(tmp), 0);
1553 btrfs_release_path(path);
1554 if (IS_ERR(di)) {
1555 ret = PTR_ERR(di);
1556 goto out;
1557 }
1558 if (di) {
1559 /* not unique, try again */
1560 idx++;
1561 continue;
1562 }
1563
1564 if (!sctx->parent_root) {
1565 /* unique */
1566 ret = 0;
1567 break;
1568 }
1569
1570 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1571 path, BTRFS_FIRST_FREE_OBJECTID,
1572 tmp, strlen(tmp), 0);
1573 btrfs_release_path(path);
1574 if (IS_ERR(di)) {
1575 ret = PTR_ERR(di);
1576 goto out;
1577 }
1578 if (di) {
1579 /* not unique, try again */
1580 idx++;
1581 continue;
1582 }
1583 /* unique */
1584 break;
1585 }
1586
1587 ret = fs_path_add(dest, tmp, strlen(tmp));
1588
1589out:
1590 btrfs_free_path(path);
1591 return ret;
1592}
1593
1594enum inode_state {
1595 inode_state_no_change,
1596 inode_state_will_create,
1597 inode_state_did_create,
1598 inode_state_will_delete,
1599 inode_state_did_delete,
1600};
1601
1602static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1603{
1604 int ret;
1605 int left_ret;
1606 int right_ret;
1607 u64 left_gen;
1608 u64 right_gen;
1609
1610 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001611 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001612 if (ret < 0 && ret != -ENOENT)
1613 goto out;
1614 left_ret = ret;
1615
1616 if (!sctx->parent_root) {
1617 right_ret = -ENOENT;
1618 } else {
1619 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001620 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001621 if (ret < 0 && ret != -ENOENT)
1622 goto out;
1623 right_ret = ret;
1624 }
1625
1626 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001627 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001628 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001629 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001630 if (ino < sctx->send_progress)
1631 ret = inode_state_did_create;
1632 else
1633 ret = inode_state_will_create;
1634 } else if (right_gen == gen) {
1635 if (ino < sctx->send_progress)
1636 ret = inode_state_did_delete;
1637 else
1638 ret = inode_state_will_delete;
1639 } else {
1640 ret = -ENOENT;
1641 }
1642 } else if (!left_ret) {
1643 if (left_gen == gen) {
1644 if (ino < sctx->send_progress)
1645 ret = inode_state_did_create;
1646 else
1647 ret = inode_state_will_create;
1648 } else {
1649 ret = -ENOENT;
1650 }
1651 } else if (!right_ret) {
1652 if (right_gen == gen) {
1653 if (ino < sctx->send_progress)
1654 ret = inode_state_did_delete;
1655 else
1656 ret = inode_state_will_delete;
1657 } else {
1658 ret = -ENOENT;
1659 }
1660 } else {
1661 ret = -ENOENT;
1662 }
1663
1664out:
1665 return ret;
1666}
1667
1668static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1669{
1670 int ret;
1671
Robbie Ko4dd99202017-01-05 16:24:55 +08001672 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1673 return 1;
1674
Alexander Block31db9f72012-07-25 23:19:24 +02001675 ret = get_cur_inode_state(sctx, ino, gen);
1676 if (ret < 0)
1677 goto out;
1678
1679 if (ret == inode_state_no_change ||
1680 ret == inode_state_did_create ||
1681 ret == inode_state_will_delete)
1682 ret = 1;
1683 else
1684 ret = 0;
1685
1686out:
1687 return ret;
1688}
1689
1690/*
1691 * Helper function to lookup a dir item in a dir.
1692 */
1693static int lookup_dir_item_inode(struct btrfs_root *root,
1694 u64 dir, const char *name, int name_len,
1695 u64 *found_inode,
1696 u8 *found_type)
1697{
1698 int ret = 0;
1699 struct btrfs_dir_item *di;
1700 struct btrfs_key key;
1701 struct btrfs_path *path;
1702
1703 path = alloc_path_for_send();
1704 if (!path)
1705 return -ENOMEM;
1706
1707 di = btrfs_lookup_dir_item(NULL, root, path,
1708 dir, name, name_len, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08001709 if (IS_ERR_OR_NULL(di)) {
1710 ret = di ? PTR_ERR(di) : -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02001711 goto out;
1712 }
1713 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001714 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1715 ret = -ENOENT;
1716 goto out;
1717 }
Alexander Block31db9f72012-07-25 23:19:24 +02001718 *found_inode = key.objectid;
1719 *found_type = btrfs_dir_type(path->nodes[0], di);
1720
1721out:
1722 btrfs_free_path(path);
1723 return ret;
1724}
1725
Alexander Block766702e2012-07-28 14:11:31 +02001726/*
1727 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1728 * generation of the parent dir and the name of the dir entry.
1729 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001730static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001731 u64 *dir, u64 *dir_gen, struct fs_path *name)
1732{
1733 int ret;
1734 struct btrfs_key key;
1735 struct btrfs_key found_key;
1736 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001737 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001738 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001739
1740 path = alloc_path_for_send();
1741 if (!path)
1742 return -ENOMEM;
1743
1744 key.objectid = ino;
1745 key.type = BTRFS_INODE_REF_KEY;
1746 key.offset = 0;
1747
1748 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1749 if (ret < 0)
1750 goto out;
1751 if (!ret)
1752 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1753 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001754 if (ret || found_key.objectid != ino ||
1755 (found_key.type != BTRFS_INODE_REF_KEY &&
1756 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001757 ret = -ENOENT;
1758 goto out;
1759 }
1760
Filipe Manana51a60252014-05-13 22:01:02 +01001761 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001762 struct btrfs_inode_ref *iref;
1763 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1764 struct btrfs_inode_ref);
1765 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1766 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1767 (unsigned long)(iref + 1),
1768 len);
1769 parent_dir = found_key.offset;
1770 } else {
1771 struct btrfs_inode_extref *extref;
1772 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1773 struct btrfs_inode_extref);
1774 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1775 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1776 (unsigned long)&extref->name, len);
1777 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1778 }
Alexander Block31db9f72012-07-25 23:19:24 +02001779 if (ret < 0)
1780 goto out;
1781 btrfs_release_path(path);
1782
Filipe Mananab46ab972014-03-21 12:46:54 +00001783 if (dir_gen) {
1784 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1785 NULL, NULL, NULL);
1786 if (ret < 0)
1787 goto out;
1788 }
Alexander Block31db9f72012-07-25 23:19:24 +02001789
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001790 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001791
1792out:
1793 btrfs_free_path(path);
1794 return ret;
1795}
1796
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001797static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001798 u64 ino, u64 dir,
1799 const char *name, int name_len)
1800{
1801 int ret;
1802 struct fs_path *tmp_name;
1803 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001804
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001805 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001806 if (!tmp_name)
1807 return -ENOMEM;
1808
Filipe Mananab46ab972014-03-21 12:46:54 +00001809 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001810 if (ret < 0)
1811 goto out;
1812
Alexander Blockb9291af2012-07-28 11:07:18 +02001813 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001814 ret = 0;
1815 goto out;
1816 }
1817
Alexander Blocke938c8a2012-07-28 16:33:49 +02001818 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001819
1820out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001821 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001822 return ret;
1823}
1824
Alexander Block766702e2012-07-28 14:11:31 +02001825/*
1826 * Used by process_recorded_refs to determine if a new ref would overwrite an
1827 * already existing ref. In case it detects an overwrite, it returns the
1828 * inode/gen in who_ino/who_gen.
1829 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1830 * to make sure later references to the overwritten inode are possible.
1831 * Orphanizing is however only required for the first ref of an inode.
1832 * process_recorded_refs does an additional is_first_ref check to see if
1833 * orphanizing is really required.
1834 */
Alexander Block31db9f72012-07-25 23:19:24 +02001835static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1836 const char *name, int name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01001837 u64 *who_ino, u64 *who_gen, u64 *who_mode)
Alexander Block31db9f72012-07-25 23:19:24 +02001838{
1839 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001840 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001841 u64 other_inode = 0;
1842 u8 other_type = 0;
1843
1844 if (!sctx->parent_root)
1845 goto out;
1846
1847 ret = is_inode_existent(sctx, dir, dir_gen);
1848 if (ret <= 0)
1849 goto out;
1850
Josef Bacikebdad912013-08-06 16:47:48 -04001851 /*
1852 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001853 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001854 * and we can just unlink this entry.
1855 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001856 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001857 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1858 NULL, NULL, NULL);
1859 if (ret < 0 && ret != -ENOENT)
1860 goto out;
1861 if (ret) {
1862 ret = 0;
1863 goto out;
1864 }
1865 if (gen != dir_gen)
1866 goto out;
1867 }
1868
Alexander Block31db9f72012-07-25 23:19:24 +02001869 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1870 &other_inode, &other_type);
1871 if (ret < 0 && ret != -ENOENT)
1872 goto out;
1873 if (ret) {
1874 ret = 0;
1875 goto out;
1876 }
1877
Alexander Block766702e2012-07-28 14:11:31 +02001878 /*
1879 * Check if the overwritten ref was already processed. If yes, the ref
1880 * was already unlinked/moved, so we can safely assume that we will not
1881 * overwrite anything at this point in time.
1882 */
Robbie Ko801bec32015-06-23 18:39:46 +08001883 if (other_inode > sctx->send_progress ||
1884 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001885 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Filipe Mananaf5962782017-06-22 20:03:51 +01001886 who_gen, who_mode, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001887 if (ret < 0)
1888 goto out;
1889
1890 ret = 1;
1891 *who_ino = other_inode;
1892 } else {
1893 ret = 0;
1894 }
1895
1896out:
1897 return ret;
1898}
1899
Alexander Block766702e2012-07-28 14:11:31 +02001900/*
1901 * Checks if the ref was overwritten by an already processed inode. This is
1902 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1903 * thus the orphan name needs be used.
1904 * process_recorded_refs also uses it to avoid unlinking of refs that were
1905 * overwritten.
1906 */
Alexander Block31db9f72012-07-25 23:19:24 +02001907static int did_overwrite_ref(struct send_ctx *sctx,
1908 u64 dir, u64 dir_gen,
1909 u64 ino, u64 ino_gen,
1910 const char *name, int name_len)
1911{
1912 int ret = 0;
1913 u64 gen;
1914 u64 ow_inode;
1915 u8 other_type;
1916
1917 if (!sctx->parent_root)
1918 goto out;
1919
1920 ret = is_inode_existent(sctx, dir, dir_gen);
1921 if (ret <= 0)
1922 goto out;
1923
Robbie Ko01914102017-01-05 16:24:58 +08001924 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1925 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1926 NULL, NULL, NULL);
1927 if (ret < 0 && ret != -ENOENT)
1928 goto out;
1929 if (ret) {
1930 ret = 0;
1931 goto out;
1932 }
1933 if (gen != dir_gen)
1934 goto out;
1935 }
1936
Alexander Block31db9f72012-07-25 23:19:24 +02001937 /* check if the ref was overwritten by another ref */
1938 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1939 &ow_inode, &other_type);
1940 if (ret < 0 && ret != -ENOENT)
1941 goto out;
1942 if (ret) {
1943 /* was never and will never be overwritten */
1944 ret = 0;
1945 goto out;
1946 }
1947
1948 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001949 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001950 if (ret < 0)
1951 goto out;
1952
1953 if (ow_inode == ino && gen == ino_gen) {
1954 ret = 0;
1955 goto out;
1956 }
1957
Filipe Manana8b191a62015-04-09 14:09:14 +01001958 /*
1959 * We know that it is or will be overwritten. Check this now.
1960 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001961 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1962 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001963 */
Filipe Mananab786f162015-09-26 15:30:23 +01001964 if ((ow_inode < sctx->send_progress) ||
1965 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1966 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001967 ret = 1;
1968 else
1969 ret = 0;
1970
1971out:
1972 return ret;
1973}
1974
Alexander Block766702e2012-07-28 14:11:31 +02001975/*
1976 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1977 * that got overwritten. This is used by process_recorded_refs to determine
1978 * if it has to use the path as returned by get_cur_path or the orphan name.
1979 */
Alexander Block31db9f72012-07-25 23:19:24 +02001980static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1981{
1982 int ret = 0;
1983 struct fs_path *name = NULL;
1984 u64 dir;
1985 u64 dir_gen;
1986
1987 if (!sctx->parent_root)
1988 goto out;
1989
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001990 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001991 if (!name)
1992 return -ENOMEM;
1993
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001994 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001995 if (ret < 0)
1996 goto out;
1997
1998 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1999 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02002000
2001out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002002 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002003 return ret;
2004}
2005
Alexander Block766702e2012-07-28 14:11:31 +02002006/*
2007 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2008 * so we need to do some special handling in case we have clashes. This function
2009 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002010 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002011 */
Alexander Block31db9f72012-07-25 23:19:24 +02002012static int name_cache_insert(struct send_ctx *sctx,
2013 struct name_cache_entry *nce)
2014{
2015 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002016 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002017
Alexander Block7e0926f2012-07-28 14:20:58 +02002018 nce_head = radix_tree_lookup(&sctx->name_cache,
2019 (unsigned long)nce->ino);
2020 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002021 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002022 if (!nce_head) {
2023 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002024 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002025 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002026 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002027
Alexander Block7e0926f2012-07-28 14:20:58 +02002028 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002029 if (ret < 0) {
2030 kfree(nce_head);
2031 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002032 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002033 }
Alexander Block31db9f72012-07-25 23:19:24 +02002034 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002035 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002036 list_add_tail(&nce->list, &sctx->name_cache_list);
2037 sctx->name_cache_size++;
2038
2039 return ret;
2040}
2041
2042static void name_cache_delete(struct send_ctx *sctx,
2043 struct name_cache_entry *nce)
2044{
Alexander Block7e0926f2012-07-28 14:20:58 +02002045 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002046
Alexander Block7e0926f2012-07-28 14:20:58 +02002047 nce_head = radix_tree_lookup(&sctx->name_cache,
2048 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002049 if (!nce_head) {
2050 btrfs_err(sctx->send_root->fs_info,
2051 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2052 nce->ino, sctx->name_cache_size);
2053 }
Alexander Block31db9f72012-07-25 23:19:24 +02002054
Alexander Block7e0926f2012-07-28 14:20:58 +02002055 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002056 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002057 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002058
David Sterba57fb8912014-02-03 19:24:40 +01002059 /*
2060 * We may not get to the final release of nce_head if the lookup fails
2061 */
2062 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002063 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2064 kfree(nce_head);
2065 }
Alexander Block31db9f72012-07-25 23:19:24 +02002066}
2067
2068static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2069 u64 ino, u64 gen)
2070{
Alexander Block7e0926f2012-07-28 14:20:58 +02002071 struct list_head *nce_head;
2072 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002073
Alexander Block7e0926f2012-07-28 14:20:58 +02002074 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2075 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002076 return NULL;
2077
Alexander Block7e0926f2012-07-28 14:20:58 +02002078 list_for_each_entry(cur, nce_head, radix_list) {
2079 if (cur->ino == ino && cur->gen == gen)
2080 return cur;
2081 }
Alexander Block31db9f72012-07-25 23:19:24 +02002082 return NULL;
2083}
2084
Alexander Block766702e2012-07-28 14:11:31 +02002085/*
Alexander Block766702e2012-07-28 14:11:31 +02002086 * Remove some entries from the beginning of name_cache_list.
2087 */
Alexander Block31db9f72012-07-25 23:19:24 +02002088static void name_cache_clean_unused(struct send_ctx *sctx)
2089{
2090 struct name_cache_entry *nce;
2091
2092 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2093 return;
2094
2095 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2096 nce = list_entry(sctx->name_cache_list.next,
2097 struct name_cache_entry, list);
2098 name_cache_delete(sctx, nce);
2099 kfree(nce);
2100 }
2101}
2102
2103static void name_cache_free(struct send_ctx *sctx)
2104{
2105 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002106
Alexander Blocke938c8a2012-07-28 16:33:49 +02002107 while (!list_empty(&sctx->name_cache_list)) {
2108 nce = list_entry(sctx->name_cache_list.next,
2109 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002110 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002111 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002112 }
2113}
2114
Alexander Block766702e2012-07-28 14:11:31 +02002115/*
2116 * Used by get_cur_path for each ref up to the root.
2117 * Returns 0 if it succeeded.
2118 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2119 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2120 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2121 * Returns <0 in case of error.
2122 */
Alexander Block31db9f72012-07-25 23:19:24 +02002123static int __get_cur_name_and_parent(struct send_ctx *sctx,
2124 u64 ino, u64 gen,
2125 u64 *parent_ino,
2126 u64 *parent_gen,
2127 struct fs_path *dest)
2128{
2129 int ret;
2130 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002131 struct name_cache_entry *nce = NULL;
2132
Alexander Block766702e2012-07-28 14:11:31 +02002133 /*
2134 * First check if we already did a call to this function with the same
2135 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2136 * return the cached result.
2137 */
Alexander Block31db9f72012-07-25 23:19:24 +02002138 nce = name_cache_search(sctx, ino, gen);
2139 if (nce) {
2140 if (ino < sctx->send_progress && nce->need_later_update) {
2141 name_cache_delete(sctx, nce);
2142 kfree(nce);
2143 nce = NULL;
2144 } else {
Baokun Libb930002021-06-11 14:51:15 +08002145 /*
2146 * Removes the entry from the list and adds it back to
2147 * the end. This marks the entry as recently used so
2148 * that name_cache_clean_unused does not remove it.
2149 */
2150 list_move_tail(&nce->list, &sctx->name_cache_list);
2151
Alexander Block31db9f72012-07-25 23:19:24 +02002152 *parent_ino = nce->parent_ino;
2153 *parent_gen = nce->parent_gen;
2154 ret = fs_path_add(dest, nce->name, nce->name_len);
2155 if (ret < 0)
2156 goto out;
2157 ret = nce->ret;
2158 goto out;
2159 }
2160 }
2161
Alexander Block766702e2012-07-28 14:11:31 +02002162 /*
2163 * If the inode is not existent yet, add the orphan name and return 1.
2164 * This should only happen for the parent dir that we determine in
2165 * __record_new_ref
2166 */
Alexander Block31db9f72012-07-25 23:19:24 +02002167 ret = is_inode_existent(sctx, ino, gen);
2168 if (ret < 0)
2169 goto out;
2170
2171 if (!ret) {
2172 ret = gen_unique_name(sctx, ino, gen, dest);
2173 if (ret < 0)
2174 goto out;
2175 ret = 1;
2176 goto out_cache;
2177 }
2178
Alexander Block766702e2012-07-28 14:11:31 +02002179 /*
2180 * Depending on whether the inode was already processed or not, use
2181 * send_root or parent_root for ref lookup.
2182 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002183 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002184 ret = get_first_ref(sctx->send_root, ino,
2185 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002186 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002187 ret = get_first_ref(sctx->parent_root, ino,
2188 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002189 if (ret < 0)
2190 goto out;
2191
Alexander Block766702e2012-07-28 14:11:31 +02002192 /*
2193 * Check if the ref was overwritten by an inode's ref that was processed
2194 * earlier. If yes, treat as orphan and return 1.
2195 */
Alexander Block31db9f72012-07-25 23:19:24 +02002196 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2197 dest->start, dest->end - dest->start);
2198 if (ret < 0)
2199 goto out;
2200 if (ret) {
2201 fs_path_reset(dest);
2202 ret = gen_unique_name(sctx, ino, gen, dest);
2203 if (ret < 0)
2204 goto out;
2205 ret = 1;
2206 }
2207
2208out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002209 /*
2210 * Store the result of the lookup in the name cache.
2211 */
David Sterbae780b0d2016-01-18 18:42:13 +01002212 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002213 if (!nce) {
2214 ret = -ENOMEM;
2215 goto out;
2216 }
2217
2218 nce->ino = ino;
2219 nce->gen = gen;
2220 nce->parent_ino = *parent_ino;
2221 nce->parent_gen = *parent_gen;
2222 nce->name_len = fs_path_len(dest);
2223 nce->ret = ret;
2224 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002225
2226 if (ino < sctx->send_progress)
2227 nce->need_later_update = 0;
2228 else
2229 nce->need_later_update = 1;
2230
2231 nce_ret = name_cache_insert(sctx, nce);
2232 if (nce_ret < 0)
2233 ret = nce_ret;
2234 name_cache_clean_unused(sctx);
2235
2236out:
Alexander Block31db9f72012-07-25 23:19:24 +02002237 return ret;
2238}
2239
2240/*
2241 * Magic happens here. This function returns the first ref to an inode as it
2242 * would look like while receiving the stream at this point in time.
2243 * We walk the path up to the root. For every inode in between, we check if it
2244 * was already processed/sent. If yes, we continue with the parent as found
2245 * in send_root. If not, we continue with the parent as found in parent_root.
2246 * If we encounter an inode that was deleted at this point in time, we use the
2247 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2248 * that were not created yet and overwritten inodes/refs.
2249 *
Andrea Gelmini52042d82018-11-28 12:05:13 +01002250 * When do we have orphan inodes:
Alexander Block31db9f72012-07-25 23:19:24 +02002251 * 1. When an inode is freshly created and thus no valid refs are available yet
2252 * 2. When a directory lost all it's refs (deleted) but still has dir items
2253 * inside which were not processed yet (pending for move/delete). If anyone
2254 * tried to get the path to the dir items, it would get a path inside that
2255 * orphan directory.
2256 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2257 * of an unprocessed inode. If in that case the first ref would be
2258 * overwritten, the overwritten inode gets "orphanized". Later when we
2259 * process this overwritten inode, it is restored at a new place by moving
2260 * the orphan inode.
2261 *
2262 * sctx->send_progress tells this function at which point in time receiving
2263 * would be.
2264 */
2265static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2266 struct fs_path *dest)
2267{
2268 int ret = 0;
2269 struct fs_path *name = NULL;
2270 u64 parent_inode = 0;
2271 u64 parent_gen = 0;
2272 int stop = 0;
2273
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002274 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002275 if (!name) {
2276 ret = -ENOMEM;
2277 goto out;
2278 }
2279
2280 dest->reversed = 1;
2281 fs_path_reset(dest);
2282
2283 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002284 struct waiting_dir_move *wdm;
2285
Alexander Block31db9f72012-07-25 23:19:24 +02002286 fs_path_reset(name);
2287
Filipe Manana0b3f4072020-12-10 12:09:02 +00002288 if (is_waiting_for_rm(sctx, ino, gen)) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002289 ret = gen_unique_name(sctx, ino, gen, name);
2290 if (ret < 0)
2291 goto out;
2292 ret = fs_path_add_path(dest, name);
2293 break;
2294 }
2295
Filipe Manana8b191a62015-04-09 14:09:14 +01002296 wdm = get_waiting_dir_move(sctx, ino);
2297 if (wdm && wdm->orphanized) {
2298 ret = gen_unique_name(sctx, ino, gen, name);
2299 stop = 1;
2300 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002301 ret = get_first_ref(sctx->parent_root, ino,
2302 &parent_inode, &parent_gen, name);
2303 } else {
2304 ret = __get_cur_name_and_parent(sctx, ino, gen,
2305 &parent_inode,
2306 &parent_gen, name);
2307 if (ret)
2308 stop = 1;
2309 }
2310
Alexander Block31db9f72012-07-25 23:19:24 +02002311 if (ret < 0)
2312 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002313
Alexander Block31db9f72012-07-25 23:19:24 +02002314 ret = fs_path_add_path(dest, name);
2315 if (ret < 0)
2316 goto out;
2317
2318 ino = parent_inode;
2319 gen = parent_gen;
2320 }
2321
2322out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002323 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002324 if (!ret)
2325 fs_path_unreverse(dest);
2326 return ret;
2327}
2328
2329/*
Alexander Block31db9f72012-07-25 23:19:24 +02002330 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2331 */
2332static int send_subvol_begin(struct send_ctx *sctx)
2333{
2334 int ret;
2335 struct btrfs_root *send_root = sctx->send_root;
2336 struct btrfs_root *parent_root = sctx->parent_root;
2337 struct btrfs_path *path;
2338 struct btrfs_key key;
2339 struct btrfs_root_ref *ref;
2340 struct extent_buffer *leaf;
2341 char *name = NULL;
2342 int namelen;
2343
Wang Shilongffcfaf82014-01-15 00:26:43 +08002344 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002345 if (!path)
2346 return -ENOMEM;
2347
David Sterbae780b0d2016-01-18 18:42:13 +01002348 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002349 if (!name) {
2350 btrfs_free_path(path);
2351 return -ENOMEM;
2352 }
2353
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002354 key.objectid = send_root->root_key.objectid;
Alexander Block31db9f72012-07-25 23:19:24 +02002355 key.type = BTRFS_ROOT_BACKREF_KEY;
2356 key.offset = 0;
2357
2358 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2359 &key, path, 1, 0);
2360 if (ret < 0)
2361 goto out;
2362 if (ret) {
2363 ret = -ENOENT;
2364 goto out;
2365 }
2366
2367 leaf = path->nodes[0];
2368 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2369 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002370 key.objectid != send_root->root_key.objectid) {
Alexander Block31db9f72012-07-25 23:19:24 +02002371 ret = -ENOENT;
2372 goto out;
2373 }
2374 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2375 namelen = btrfs_root_ref_name_len(leaf, ref);
2376 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2377 btrfs_release_path(path);
2378
Alexander Block31db9f72012-07-25 23:19:24 +02002379 if (parent_root) {
2380 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2381 if (ret < 0)
2382 goto out;
2383 } else {
2384 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2385 if (ret < 0)
2386 goto out;
2387 }
2388
2389 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002390
2391 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2392 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2393 sctx->send_root->root_item.received_uuid);
2394 else
2395 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2396 sctx->send_root->root_item.uuid);
2397
Alexander Block31db9f72012-07-25 23:19:24 +02002398 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
David Sterba09e3a282020-09-15 14:30:15 +02002399 btrfs_root_ctransid(&sctx->send_root->root_item));
Alexander Block31db9f72012-07-25 23:19:24 +02002400 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002401 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2402 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2403 parent_root->root_item.received_uuid);
2404 else
2405 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2406 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002407 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
David Sterba09e3a282020-09-15 14:30:15 +02002408 btrfs_root_ctransid(&sctx->parent_root->root_item));
Alexander Block31db9f72012-07-25 23:19:24 +02002409 }
2410
2411 ret = send_cmd(sctx);
2412
2413tlv_put_failure:
2414out:
2415 btrfs_free_path(path);
2416 kfree(name);
2417 return ret;
2418}
2419
2420static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2421{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002422 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002423 int ret = 0;
2424 struct fs_path *p;
2425
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002426 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002427
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002428 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002429 if (!p)
2430 return -ENOMEM;
2431
2432 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2433 if (ret < 0)
2434 goto out;
2435
2436 ret = get_cur_path(sctx, ino, gen, p);
2437 if (ret < 0)
2438 goto out;
2439 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2440 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2441
2442 ret = send_cmd(sctx);
2443
2444tlv_put_failure:
2445out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002446 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002447 return ret;
2448}
2449
2450static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2451{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002452 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002453 int ret = 0;
2454 struct fs_path *p;
2455
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002456 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002457
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002458 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002459 if (!p)
2460 return -ENOMEM;
2461
2462 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2463 if (ret < 0)
2464 goto out;
2465
2466 ret = get_cur_path(sctx, ino, gen, p);
2467 if (ret < 0)
2468 goto out;
2469 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2470 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2471
2472 ret = send_cmd(sctx);
2473
2474tlv_put_failure:
2475out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002476 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002477 return ret;
2478}
2479
2480static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2481{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002482 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002483 int ret = 0;
2484 struct fs_path *p;
2485
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002486 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2487 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002488
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002489 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002490 if (!p)
2491 return -ENOMEM;
2492
2493 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2494 if (ret < 0)
2495 goto out;
2496
2497 ret = get_cur_path(sctx, ino, gen, p);
2498 if (ret < 0)
2499 goto out;
2500 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2501 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2502 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2503
2504 ret = send_cmd(sctx);
2505
2506tlv_put_failure:
2507out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002508 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002509 return ret;
2510}
2511
2512static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2513{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002514 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002515 int ret = 0;
2516 struct fs_path *p = NULL;
2517 struct btrfs_inode_item *ii;
2518 struct btrfs_path *path = NULL;
2519 struct extent_buffer *eb;
2520 struct btrfs_key key;
2521 int slot;
2522
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002523 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002524
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002525 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002526 if (!p)
2527 return -ENOMEM;
2528
2529 path = alloc_path_for_send();
2530 if (!path) {
2531 ret = -ENOMEM;
2532 goto out;
2533 }
2534
2535 key.objectid = ino;
2536 key.type = BTRFS_INODE_ITEM_KEY;
2537 key.offset = 0;
2538 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002539 if (ret > 0)
2540 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002541 if (ret < 0)
2542 goto out;
2543
2544 eb = path->nodes[0];
2545 slot = path->slots[0];
2546 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2547
2548 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2549 if (ret < 0)
2550 goto out;
2551
2552 ret = get_cur_path(sctx, ino, gen, p);
2553 if (ret < 0)
2554 goto out;
2555 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002556 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2557 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2558 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002559 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002560
2561 ret = send_cmd(sctx);
2562
2563tlv_put_failure:
2564out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002565 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002566 btrfs_free_path(path);
2567 return ret;
2568}
2569
2570/*
2571 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2572 * a valid path yet because we did not process the refs yet. So, the inode
2573 * is created as orphan.
2574 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002575static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002576{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002577 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002578 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002579 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002580 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002581 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002582 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002583 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002584
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002585 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002586
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002587 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002588 if (!p)
2589 return -ENOMEM;
2590
Liu Bo644d1942014-02-27 17:29:01 +08002591 if (ino != sctx->cur_ino) {
2592 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2593 NULL, NULL, &rdev);
2594 if (ret < 0)
2595 goto out;
2596 } else {
2597 gen = sctx->cur_inode_gen;
2598 mode = sctx->cur_inode_mode;
2599 rdev = sctx->cur_inode_rdev;
2600 }
Alexander Block31db9f72012-07-25 23:19:24 +02002601
Alexander Blocke938c8a2012-07-28 16:33:49 +02002602 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002603 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002604 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002605 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002606 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002607 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002608 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002609 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002610 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002611 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002612 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002613 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002614 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002615 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002616 (int)(mode & S_IFMT));
Tsutomu Itohca6842b2016-01-22 09:13:25 +09002617 ret = -EOPNOTSUPP;
Alexander Block31db9f72012-07-25 23:19:24 +02002618 goto out;
2619 }
2620
2621 ret = begin_cmd(sctx, cmd);
2622 if (ret < 0)
2623 goto out;
2624
Alexander Block1f4692d2012-07-28 10:42:24 +02002625 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002626 if (ret < 0)
2627 goto out;
2628
2629 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002630 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002631
2632 if (S_ISLNK(mode)) {
2633 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002634 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002635 if (ret < 0)
2636 goto out;
2637 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2638 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2639 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002640 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2641 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002642 }
2643
2644 ret = send_cmd(sctx);
2645 if (ret < 0)
2646 goto out;
2647
2648
2649tlv_put_failure:
2650out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002651 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002652 return ret;
2653}
2654
Alexander Block1f4692d2012-07-28 10:42:24 +02002655/*
2656 * We need some special handling for inodes that get processed before the parent
2657 * directory got created. See process_recorded_refs for details.
2658 * This function does the check if we already created the dir out of order.
2659 */
2660static int did_create_dir(struct send_ctx *sctx, u64 dir)
2661{
2662 int ret = 0;
2663 struct btrfs_path *path = NULL;
2664 struct btrfs_key key;
2665 struct btrfs_key found_key;
2666 struct btrfs_key di_key;
2667 struct extent_buffer *eb;
2668 struct btrfs_dir_item *di;
2669 int slot;
2670
2671 path = alloc_path_for_send();
2672 if (!path) {
2673 ret = -ENOMEM;
2674 goto out;
2675 }
2676
2677 key.objectid = dir;
2678 key.type = BTRFS_DIR_INDEX_KEY;
2679 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002680 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2681 if (ret < 0)
2682 goto out;
2683
Alexander Block1f4692d2012-07-28 10:42:24 +02002684 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002685 eb = path->nodes[0];
2686 slot = path->slots[0];
2687 if (slot >= btrfs_header_nritems(eb)) {
2688 ret = btrfs_next_leaf(sctx->send_root, path);
2689 if (ret < 0) {
2690 goto out;
2691 } else if (ret > 0) {
2692 ret = 0;
2693 break;
2694 }
2695 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002696 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002697
2698 btrfs_item_key_to_cpu(eb, &found_key, slot);
2699 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002700 found_key.type != key.type) {
2701 ret = 0;
2702 goto out;
2703 }
2704
2705 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2706 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2707
Josef Bacika0525412013-08-12 10:56:14 -04002708 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2709 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002710 ret = 1;
2711 goto out;
2712 }
2713
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002714 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002715 }
2716
2717out:
2718 btrfs_free_path(path);
2719 return ret;
2720}
2721
2722/*
2723 * Only creates the inode if it is:
2724 * 1. Not a directory
2725 * 2. Or a directory which was not created already due to out of order
2726 * directories. See did_create_dir and process_recorded_refs for details.
2727 */
2728static int send_create_inode_if_needed(struct send_ctx *sctx)
2729{
2730 int ret;
2731
2732 if (S_ISDIR(sctx->cur_inode_mode)) {
2733 ret = did_create_dir(sctx, sctx->cur_ino);
2734 if (ret < 0)
Marcos Paulo de Souza0e3dd5b2021-08-01 20:35:49 -03002735 return ret;
2736 else if (ret > 0)
2737 return 0;
Alexander Block1f4692d2012-07-28 10:42:24 +02002738 }
2739
Marcos Paulo de Souza0e3dd5b2021-08-01 20:35:49 -03002740 return send_create_inode(sctx, sctx->cur_ino);
Alexander Block1f4692d2012-07-28 10:42:24 +02002741}
2742
Alexander Block31db9f72012-07-25 23:19:24 +02002743struct recorded_ref {
2744 struct list_head list;
Alexander Block31db9f72012-07-25 23:19:24 +02002745 char *name;
2746 struct fs_path *full_path;
2747 u64 dir;
2748 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002749 int name_len;
2750};
2751
Filipe Mananafdb13882017-06-13 14:13:11 +01002752static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2753{
2754 ref->full_path = path;
2755 ref->name = (char *)kbasename(ref->full_path->start);
2756 ref->name_len = ref->full_path->end - ref->name;
2757}
2758
Alexander Block31db9f72012-07-25 23:19:24 +02002759/*
2760 * We need to process new refs before deleted refs, but compare_tree gives us
2761 * everything mixed. So we first record all refs and later process them.
2762 * This function is a helper to record one ref.
2763 */
Liu Boa4d96d62014-03-03 21:31:03 +08002764static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002765 u64 dir_gen, struct fs_path *path)
2766{
2767 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002768
David Sterbae780b0d2016-01-18 18:42:13 +01002769 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002770 if (!ref)
2771 return -ENOMEM;
2772
2773 ref->dir = dir;
2774 ref->dir_gen = dir_gen;
Filipe Mananafdb13882017-06-13 14:13:11 +01002775 set_ref_path(ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02002776 list_add_tail(&ref->list, head);
2777 return 0;
2778}
2779
Josef Bacikba5e8f22013-08-16 16:52:55 -04002780static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2781{
2782 struct recorded_ref *new;
2783
David Sterbae780b0d2016-01-18 18:42:13 +01002784 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002785 if (!new)
2786 return -ENOMEM;
2787
2788 new->dir = ref->dir;
2789 new->dir_gen = ref->dir_gen;
2790 new->full_path = NULL;
2791 INIT_LIST_HEAD(&new->list);
2792 list_add_tail(&new->list, list);
2793 return 0;
2794}
2795
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002796static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002797{
2798 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002799
Alexander Blocke938c8a2012-07-28 16:33:49 +02002800 while (!list_empty(head)) {
2801 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002802 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002803 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002804 kfree(cur);
2805 }
Alexander Block31db9f72012-07-25 23:19:24 +02002806}
2807
2808static void free_recorded_refs(struct send_ctx *sctx)
2809{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002810 __free_recorded_refs(&sctx->new_refs);
2811 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002812}
2813
2814/*
Alexander Block766702e2012-07-28 14:11:31 +02002815 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002816 * ref of an unprocessed inode gets overwritten and for all non empty
2817 * directories.
2818 */
2819static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2820 struct fs_path *path)
2821{
2822 int ret;
2823 struct fs_path *orphan;
2824
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002825 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002826 if (!orphan)
2827 return -ENOMEM;
2828
2829 ret = gen_unique_name(sctx, ino, gen, orphan);
2830 if (ret < 0)
2831 goto out;
2832
2833 ret = send_rename(sctx, path, orphan);
2834
2835out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002836 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002837 return ret;
2838}
2839
Filipe Manana0b3f4072020-12-10 12:09:02 +00002840static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
2841 u64 dir_ino, u64 dir_gen)
Filipe Manana9dc44212014-02-19 14:31:44 +00002842{
2843 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2844 struct rb_node *parent = NULL;
2845 struct orphan_dir_info *entry, *odi;
2846
Filipe Manana9dc44212014-02-19 14:31:44 +00002847 while (*p) {
2848 parent = *p;
2849 entry = rb_entry(parent, struct orphan_dir_info, node);
Filipe Manana0b3f4072020-12-10 12:09:02 +00002850 if (dir_ino < entry->ino)
Filipe Manana9dc44212014-02-19 14:31:44 +00002851 p = &(*p)->rb_left;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002852 else if (dir_ino > entry->ino)
Filipe Manana9dc44212014-02-19 14:31:44 +00002853 p = &(*p)->rb_right;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002854 else if (dir_gen < entry->gen)
2855 p = &(*p)->rb_left;
2856 else if (dir_gen > entry->gen)
2857 p = &(*p)->rb_right;
2858 else
Filipe Manana9dc44212014-02-19 14:31:44 +00002859 return entry;
Filipe Manana9dc44212014-02-19 14:31:44 +00002860 }
2861
Robbie Ko35c8eda2018-05-08 18:11:37 +08002862 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2863 if (!odi)
2864 return ERR_PTR(-ENOMEM);
2865 odi->ino = dir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002866 odi->gen = dir_gen;
Robbie Ko0f96f512018-05-08 18:11:38 +08002867 odi->last_dir_index_offset = 0;
Robbie Ko35c8eda2018-05-08 18:11:37 +08002868
Filipe Manana9dc44212014-02-19 14:31:44 +00002869 rb_link_node(&odi->node, parent, p);
2870 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2871 return odi;
2872}
2873
Filipe Manana0b3f4072020-12-10 12:09:02 +00002874static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
2875 u64 dir_ino, u64 gen)
Filipe Manana9dc44212014-02-19 14:31:44 +00002876{
2877 struct rb_node *n = sctx->orphan_dirs.rb_node;
2878 struct orphan_dir_info *entry;
2879
2880 while (n) {
2881 entry = rb_entry(n, struct orphan_dir_info, node);
2882 if (dir_ino < entry->ino)
2883 n = n->rb_left;
2884 else if (dir_ino > entry->ino)
2885 n = n->rb_right;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002886 else if (gen < entry->gen)
2887 n = n->rb_left;
2888 else if (gen > entry->gen)
2889 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002890 else
2891 return entry;
2892 }
2893 return NULL;
2894}
2895
Filipe Manana0b3f4072020-12-10 12:09:02 +00002896static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
Filipe Manana9dc44212014-02-19 14:31:44 +00002897{
Filipe Manana0b3f4072020-12-10 12:09:02 +00002898 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
Filipe Manana9dc44212014-02-19 14:31:44 +00002899
2900 return odi != NULL;
2901}
2902
2903static void free_orphan_dir_info(struct send_ctx *sctx,
2904 struct orphan_dir_info *odi)
2905{
2906 if (!odi)
2907 return;
2908 rb_erase(&odi->node, &sctx->orphan_dirs);
2909 kfree(odi);
2910}
2911
Alexander Block31db9f72012-07-25 23:19:24 +02002912/*
2913 * Returns 1 if a directory can be removed at this point in time.
2914 * We check this by iterating all dir items and checking if the inode behind
2915 * the dir item was already processed.
2916 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002917static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2918 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002919{
2920 int ret = 0;
2921 struct btrfs_root *root = sctx->parent_root;
2922 struct btrfs_path *path;
2923 struct btrfs_key key;
2924 struct btrfs_key found_key;
2925 struct btrfs_key loc;
2926 struct btrfs_dir_item *di;
Robbie Ko0f96f512018-05-08 18:11:38 +08002927 struct orphan_dir_info *odi = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02002928
Alexander Block6d85ed052012-08-01 14:48:59 +02002929 /*
2930 * Don't try to rmdir the top/root subvolume dir.
2931 */
2932 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2933 return 0;
2934
Alexander Block31db9f72012-07-25 23:19:24 +02002935 path = alloc_path_for_send();
2936 if (!path)
2937 return -ENOMEM;
2938
2939 key.objectid = dir;
2940 key.type = BTRFS_DIR_INDEX_KEY;
2941 key.offset = 0;
Robbie Ko0f96f512018-05-08 18:11:38 +08002942
Filipe Manana0b3f4072020-12-10 12:09:02 +00002943 odi = get_orphan_dir_info(sctx, dir, dir_gen);
Robbie Ko0f96f512018-05-08 18:11:38 +08002944 if (odi)
2945 key.offset = odi->last_dir_index_offset;
2946
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002947 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2948 if (ret < 0)
2949 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002950
2951 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002952 struct waiting_dir_move *dm;
2953
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002954 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2955 ret = btrfs_next_leaf(root, path);
2956 if (ret < 0)
2957 goto out;
2958 else if (ret > 0)
2959 break;
2960 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002961 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002962 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2963 path->slots[0]);
2964 if (found_key.objectid != key.objectid ||
2965 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002966 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002967
2968 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2969 struct btrfs_dir_item);
2970 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2971
Filipe Manana9dc44212014-02-19 14:31:44 +00002972 dm = get_waiting_dir_move(sctx, loc.objectid);
2973 if (dm) {
Filipe Manana0b3f4072020-12-10 12:09:02 +00002974 odi = add_orphan_dir_info(sctx, dir, dir_gen);
Filipe Manana9dc44212014-02-19 14:31:44 +00002975 if (IS_ERR(odi)) {
2976 ret = PTR_ERR(odi);
2977 goto out;
2978 }
2979 odi->gen = dir_gen;
Robbie Ko0f96f512018-05-08 18:11:38 +08002980 odi->last_dir_index_offset = found_key.offset;
Filipe Manana9dc44212014-02-19 14:31:44 +00002981 dm->rmdir_ino = dir;
Filipe Manana0b3f4072020-12-10 12:09:02 +00002982 dm->rmdir_gen = dir_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00002983 ret = 0;
2984 goto out;
2985 }
2986
Alexander Block31db9f72012-07-25 23:19:24 +02002987 if (loc.objectid > send_progress) {
Filipe Manana0b3f4072020-12-10 12:09:02 +00002988 odi = add_orphan_dir_info(sctx, dir, dir_gen);
Robbie Ko0f96f512018-05-08 18:11:38 +08002989 if (IS_ERR(odi)) {
2990 ret = PTR_ERR(odi);
2991 goto out;
2992 }
2993 odi->gen = dir_gen;
2994 odi->last_dir_index_offset = found_key.offset;
Alexander Block31db9f72012-07-25 23:19:24 +02002995 ret = 0;
2996 goto out;
2997 }
2998
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002999 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003000 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003001 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02003002
3003 ret = 1;
3004
3005out:
3006 btrfs_free_path(path);
3007 return ret;
3008}
3009
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003010static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3011{
Filipe Manana9dc44212014-02-19 14:31:44 +00003012 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003013
Filipe Manana9dc44212014-02-19 14:31:44 +00003014 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003015}
3016
Filipe Manana8b191a62015-04-09 14:09:14 +01003017static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003018{
3019 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3020 struct rb_node *parent = NULL;
3021 struct waiting_dir_move *entry, *dm;
3022
David Sterbae780b0d2016-01-18 18:42:13 +01003023 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003024 if (!dm)
3025 return -ENOMEM;
3026 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003027 dm->rmdir_ino = 0;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003028 dm->rmdir_gen = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003029 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003030
3031 while (*p) {
3032 parent = *p;
3033 entry = rb_entry(parent, struct waiting_dir_move, node);
3034 if (ino < entry->ino) {
3035 p = &(*p)->rb_left;
3036 } else if (ino > entry->ino) {
3037 p = &(*p)->rb_right;
3038 } else {
3039 kfree(dm);
3040 return -EEXIST;
3041 }
3042 }
3043
3044 rb_link_node(&dm->node, parent, p);
3045 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3046 return 0;
3047}
3048
Filipe Manana9dc44212014-02-19 14:31:44 +00003049static struct waiting_dir_move *
3050get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003051{
3052 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3053 struct waiting_dir_move *entry;
3054
3055 while (n) {
3056 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003057 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003058 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003059 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003060 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003061 else
3062 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003063 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003064 return NULL;
3065}
3066
3067static void free_waiting_dir_move(struct send_ctx *sctx,
3068 struct waiting_dir_move *dm)
3069{
3070 if (!dm)
3071 return;
3072 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3073 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003074}
3075
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003076static int add_pending_dir_move(struct send_ctx *sctx,
3077 u64 ino,
3078 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003079 u64 parent_ino,
3080 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003081 struct list_head *deleted_refs,
3082 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003083{
3084 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3085 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003086 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003087 struct recorded_ref *cur;
3088 int exists = 0;
3089 int ret;
3090
David Sterbae780b0d2016-01-18 18:42:13 +01003091 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003092 if (!pm)
3093 return -ENOMEM;
3094 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003095 pm->ino = ino;
3096 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003097 INIT_LIST_HEAD(&pm->list);
3098 INIT_LIST_HEAD(&pm->update_refs);
3099 RB_CLEAR_NODE(&pm->node);
3100
3101 while (*p) {
3102 parent = *p;
3103 entry = rb_entry(parent, struct pending_dir_move, node);
3104 if (parent_ino < entry->parent_ino) {
3105 p = &(*p)->rb_left;
3106 } else if (parent_ino > entry->parent_ino) {
3107 p = &(*p)->rb_right;
3108 } else {
3109 exists = 1;
3110 break;
3111 }
3112 }
3113
Filipe Mananaf9594922014-03-27 20:14:01 +00003114 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003115 ret = dup_ref(cur, &pm->update_refs);
3116 if (ret < 0)
3117 goto out;
3118 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003119 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003120 ret = dup_ref(cur, &pm->update_refs);
3121 if (ret < 0)
3122 goto out;
3123 }
3124
Filipe Manana8b191a62015-04-09 14:09:14 +01003125 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003126 if (ret)
3127 goto out;
3128
3129 if (exists) {
3130 list_add_tail(&pm->list, &entry->list);
3131 } else {
3132 rb_link_node(&pm->node, parent, p);
3133 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3134 }
3135 ret = 0;
3136out:
3137 if (ret) {
3138 __free_recorded_refs(&pm->update_refs);
3139 kfree(pm);
3140 }
3141 return ret;
3142}
3143
3144static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3145 u64 parent_ino)
3146{
3147 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3148 struct pending_dir_move *entry;
3149
3150 while (n) {
3151 entry = rb_entry(n, struct pending_dir_move, node);
3152 if (parent_ino < entry->parent_ino)
3153 n = n->rb_left;
3154 else if (parent_ino > entry->parent_ino)
3155 n = n->rb_right;
3156 else
3157 return entry;
3158 }
3159 return NULL;
3160}
3161
Robbie Ko801bec32015-06-23 18:39:46 +08003162static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3163 u64 ino, u64 gen, u64 *ancestor_ino)
3164{
3165 int ret = 0;
3166 u64 parent_inode = 0;
3167 u64 parent_gen = 0;
3168 u64 start_ino = ino;
3169
3170 *ancestor_ino = 0;
3171 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3172 fs_path_reset(name);
3173
Filipe Manana0b3f4072020-12-10 12:09:02 +00003174 if (is_waiting_for_rm(sctx, ino, gen))
Robbie Ko801bec32015-06-23 18:39:46 +08003175 break;
3176 if (is_waiting_for_move(sctx, ino)) {
3177 if (*ancestor_ino == 0)
3178 *ancestor_ino = ino;
3179 ret = get_first_ref(sctx->parent_root, ino,
3180 &parent_inode, &parent_gen, name);
3181 } else {
3182 ret = __get_cur_name_and_parent(sctx, ino, gen,
3183 &parent_inode,
3184 &parent_gen, name);
3185 if (ret > 0) {
3186 ret = 0;
3187 break;
3188 }
3189 }
3190 if (ret < 0)
3191 break;
3192 if (parent_inode == start_ino) {
3193 ret = 1;
3194 if (*ancestor_ino == 0)
3195 *ancestor_ino = ino;
3196 break;
3197 }
3198 ino = parent_inode;
3199 gen = parent_gen;
3200 }
3201 return ret;
3202}
3203
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003204static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3205{
3206 struct fs_path *from_path = NULL;
3207 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003208 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003209 u64 orig_progress = sctx->send_progress;
3210 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003211 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003212 struct waiting_dir_move *dm = NULL;
3213 u64 rmdir_ino = 0;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003214 u64 rmdir_gen;
Robbie Ko801bec32015-06-23 18:39:46 +08003215 u64 ancestor;
3216 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003217 int ret;
3218
Filipe Manana2b863a12014-02-16 13:43:11 +00003219 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003220 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003221 if (!name || !from_path) {
3222 ret = -ENOMEM;
3223 goto out;
3224 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003225
Filipe Manana9dc44212014-02-19 14:31:44 +00003226 dm = get_waiting_dir_move(sctx, pm->ino);
3227 ASSERT(dm);
3228 rmdir_ino = dm->rmdir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003229 rmdir_gen = dm->rmdir_gen;
Robbie Ko801bec32015-06-23 18:39:46 +08003230 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003231 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003232
Robbie Ko801bec32015-06-23 18:39:46 +08003233 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003234 ret = gen_unique_name(sctx, pm->ino,
3235 pm->gen, from_path);
3236 } else {
3237 ret = get_first_ref(sctx->parent_root, pm->ino,
3238 &parent_ino, &parent_gen, name);
3239 if (ret < 0)
3240 goto out;
3241 ret = get_cur_path(sctx, parent_ino, parent_gen,
3242 from_path);
3243 if (ret < 0)
3244 goto out;
3245 ret = fs_path_add_path(from_path, name);
3246 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003247 if (ret < 0)
3248 goto out;
3249
Filipe Mananaf9594922014-03-27 20:14:01 +00003250 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003251 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003252 if (ret < 0)
3253 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003254 if (ret) {
3255 LIST_HEAD(deleted_refs);
3256 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3257 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3258 &pm->update_refs, &deleted_refs,
3259 is_orphan);
3260 if (ret < 0)
3261 goto out;
3262 if (rmdir_ino) {
3263 dm = get_waiting_dir_move(sctx, pm->ino);
3264 ASSERT(dm);
3265 dm->rmdir_ino = rmdir_ino;
Filipe Manana0b3f4072020-12-10 12:09:02 +00003266 dm->rmdir_gen = rmdir_gen;
Robbie Ko801bec32015-06-23 18:39:46 +08003267 }
3268 goto out;
3269 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003270 fs_path_reset(name);
3271 to_path = name;
3272 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003273 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3274 if (ret < 0)
3275 goto out;
3276
3277 ret = send_rename(sctx, from_path, to_path);
3278 if (ret < 0)
3279 goto out;
3280
Filipe Manana9dc44212014-02-19 14:31:44 +00003281 if (rmdir_ino) {
3282 struct orphan_dir_info *odi;
Robbie Ko0f96f512018-05-08 18:11:38 +08003283 u64 gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003284
Filipe Manana0b3f4072020-12-10 12:09:02 +00003285 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
Filipe Manana9dc44212014-02-19 14:31:44 +00003286 if (!odi) {
3287 /* already deleted */
3288 goto finish;
3289 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003290 gen = odi->gen;
3291
3292 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003293 if (ret < 0)
3294 goto out;
3295 if (!ret)
3296 goto finish;
3297
3298 name = fs_path_alloc();
3299 if (!name) {
3300 ret = -ENOMEM;
3301 goto out;
3302 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003303 ret = get_cur_path(sctx, rmdir_ino, gen, name);
Filipe Manana9dc44212014-02-19 14:31:44 +00003304 if (ret < 0)
3305 goto out;
3306 ret = send_rmdir(sctx, name);
3307 if (ret < 0)
3308 goto out;
Filipe Manana9dc44212014-02-19 14:31:44 +00003309 }
3310
3311finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003312 ret = send_utimes(sctx, pm->ino, pm->gen);
3313 if (ret < 0)
3314 goto out;
3315
3316 /*
3317 * After rename/move, need to update the utimes of both new parent(s)
3318 * and old parent(s).
3319 */
3320 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003321 /*
3322 * The parent inode might have been deleted in the send snapshot
3323 */
3324 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3325 NULL, NULL, NULL, NULL, NULL);
3326 if (ret == -ENOENT) {
3327 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003328 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003329 }
3330 if (ret < 0)
3331 goto out;
3332
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003333 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3334 if (ret < 0)
3335 goto out;
3336 }
3337
3338out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003339 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003340 fs_path_free(from_path);
3341 fs_path_free(to_path);
3342 sctx->send_progress = orig_progress;
3343
3344 return ret;
3345}
3346
3347static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3348{
3349 if (!list_empty(&m->list))
3350 list_del(&m->list);
3351 if (!RB_EMPTY_NODE(&m->node))
3352 rb_erase(&m->node, &sctx->pending_dir_moves);
3353 __free_recorded_refs(&m->update_refs);
3354 kfree(m);
3355}
3356
Robbie Koa4390ae2018-11-14 18:32:37 +00003357static void tail_append_pending_moves(struct send_ctx *sctx,
3358 struct pending_dir_move *moves,
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003359 struct list_head *stack)
3360{
3361 if (list_empty(&moves->list)) {
3362 list_add_tail(&moves->list, stack);
3363 } else {
3364 LIST_HEAD(list);
3365 list_splice_init(&moves->list, &list);
3366 list_add_tail(&moves->list, stack);
3367 list_splice_tail(&list, stack);
3368 }
Robbie Koa4390ae2018-11-14 18:32:37 +00003369 if (!RB_EMPTY_NODE(&moves->node)) {
3370 rb_erase(&moves->node, &sctx->pending_dir_moves);
3371 RB_CLEAR_NODE(&moves->node);
3372 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003373}
3374
3375static int apply_children_dir_moves(struct send_ctx *sctx)
3376{
3377 struct pending_dir_move *pm;
3378 struct list_head stack;
3379 u64 parent_ino = sctx->cur_ino;
3380 int ret = 0;
3381
3382 pm = get_pending_dir_moves(sctx, parent_ino);
3383 if (!pm)
3384 return 0;
3385
3386 INIT_LIST_HEAD(&stack);
Robbie Koa4390ae2018-11-14 18:32:37 +00003387 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003388
3389 while (!list_empty(&stack)) {
3390 pm = list_first_entry(&stack, struct pending_dir_move, list);
3391 parent_ino = pm->ino;
3392 ret = apply_dir_move(sctx, pm);
3393 free_pending_move(sctx, pm);
3394 if (ret)
3395 goto out;
3396 pm = get_pending_dir_moves(sctx, parent_ino);
3397 if (pm)
Robbie Koa4390ae2018-11-14 18:32:37 +00003398 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003399 }
3400 return 0;
3401
3402out:
3403 while (!list_empty(&stack)) {
3404 pm = list_first_entry(&stack, struct pending_dir_move, list);
3405 free_pending_move(sctx, pm);
3406 }
3407 return ret;
3408}
3409
Filipe Manana84471e22015-02-28 22:29:22 +00003410/*
3411 * We might need to delay a directory rename even when no ancestor directory
3412 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3413 * renamed. This happens when we rename a directory to the old name (the name
3414 * in the parent root) of some other unrelated directory that got its rename
3415 * delayed due to some ancestor with higher number that got renamed.
3416 *
3417 * Example:
3418 *
3419 * Parent snapshot:
3420 * . (ino 256)
3421 * |---- a/ (ino 257)
3422 * | |---- file (ino 260)
3423 * |
3424 * |---- b/ (ino 258)
3425 * |---- c/ (ino 259)
3426 *
3427 * Send snapshot:
3428 * . (ino 256)
3429 * |---- a/ (ino 258)
3430 * |---- x/ (ino 259)
3431 * |---- y/ (ino 257)
3432 * |----- file (ino 260)
3433 *
3434 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3435 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3436 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3437 * must issue is:
3438 *
3439 * 1 - rename 259 from 'c' to 'x'
3440 * 2 - rename 257 from 'a' to 'x/y'
3441 * 3 - rename 258 from 'b' to 'a'
3442 *
3443 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3444 * be done right away and < 0 on error.
3445 */
3446static int wait_for_dest_dir_move(struct send_ctx *sctx,
3447 struct recorded_ref *parent_ref,
3448 const bool is_orphan)
3449{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003450 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003451 struct btrfs_path *path;
3452 struct btrfs_key key;
3453 struct btrfs_key di_key;
3454 struct btrfs_dir_item *di;
3455 u64 left_gen;
3456 u64 right_gen;
3457 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003458 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003459
3460 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3461 return 0;
3462
3463 path = alloc_path_for_send();
3464 if (!path)
3465 return -ENOMEM;
3466
3467 key.objectid = parent_ref->dir;
3468 key.type = BTRFS_DIR_ITEM_KEY;
3469 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3470
3471 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3472 if (ret < 0) {
3473 goto out;
3474 } else if (ret > 0) {
3475 ret = 0;
3476 goto out;
3477 }
3478
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003479 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3480 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003481 if (!di) {
3482 ret = 0;
3483 goto out;
3484 }
3485 /*
3486 * di_key.objectid has the number of the inode that has a dentry in the
3487 * parent directory with the same name that sctx->cur_ino is being
3488 * renamed to. We need to check if that inode is in the send root as
3489 * well and if it is currently marked as an inode with a pending rename,
3490 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3491 * that it happens after that other inode is renamed.
3492 */
3493 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3494 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3495 ret = 0;
3496 goto out;
3497 }
3498
3499 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3500 &left_gen, NULL, NULL, NULL, NULL);
3501 if (ret < 0)
3502 goto out;
3503 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3504 &right_gen, NULL, NULL, NULL, NULL);
3505 if (ret < 0) {
3506 if (ret == -ENOENT)
3507 ret = 0;
3508 goto out;
3509 }
3510
3511 /* Different inode, no need to delay the rename of sctx->cur_ino */
3512 if (right_gen != left_gen) {
3513 ret = 0;
3514 goto out;
3515 }
3516
Robbie Ko801bec32015-06-23 18:39:46 +08003517 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3518 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003519 ret = add_pending_dir_move(sctx,
3520 sctx->cur_ino,
3521 sctx->cur_inode_gen,
3522 di_key.objectid,
3523 &sctx->new_refs,
3524 &sctx->deleted_refs,
3525 is_orphan);
3526 if (!ret)
3527 ret = 1;
3528 }
3529out:
3530 btrfs_free_path(path);
3531 return ret;
3532}
3533
Filipe Manana80aa6022015-03-27 17:50:45 +00003534/*
Filipe Mananaea37d592017-11-17 01:54:00 +00003535 * Check if inode ino2, or any of its ancestors, is inode ino1.
3536 * Return 1 if true, 0 if false and < 0 on error.
3537 */
3538static int check_ino_in_path(struct btrfs_root *root,
3539 const u64 ino1,
3540 const u64 ino1_gen,
3541 const u64 ino2,
3542 const u64 ino2_gen,
3543 struct fs_path *fs_path)
3544{
3545 u64 ino = ino2;
3546
3547 if (ino1 == ino2)
3548 return ino1_gen == ino2_gen;
3549
3550 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3551 u64 parent;
3552 u64 parent_gen;
3553 int ret;
3554
3555 fs_path_reset(fs_path);
3556 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3557 if (ret < 0)
3558 return ret;
3559 if (parent == ino1)
3560 return parent_gen == ino1_gen;
3561 ino = parent;
3562 }
3563 return 0;
3564}
3565
3566/*
3567 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3568 * possible path (in case ino2 is not a directory and has multiple hard links).
Filipe Manana80aa6022015-03-27 17:50:45 +00003569 * Return 1 if true, 0 if false and < 0 on error.
3570 */
3571static int is_ancestor(struct btrfs_root *root,
3572 const u64 ino1,
3573 const u64 ino1_gen,
3574 const u64 ino2,
3575 struct fs_path *fs_path)
3576{
Filipe Mananaea37d592017-11-17 01:54:00 +00003577 bool free_fs_path = false;
Filipe Manana72c36682017-06-07 11:41:29 +01003578 int ret = 0;
Filipe Mananaea37d592017-11-17 01:54:00 +00003579 struct btrfs_path *path = NULL;
3580 struct btrfs_key key;
Filipe Manana72c36682017-06-07 11:41:29 +01003581
3582 if (!fs_path) {
3583 fs_path = fs_path_alloc();
3584 if (!fs_path)
3585 return -ENOMEM;
Filipe Mananaea37d592017-11-17 01:54:00 +00003586 free_fs_path = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003587 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003588
Filipe Mananaea37d592017-11-17 01:54:00 +00003589 path = alloc_path_for_send();
3590 if (!path) {
3591 ret = -ENOMEM;
3592 goto out;
Filipe Manana80aa6022015-03-27 17:50:45 +00003593 }
Filipe Mananaea37d592017-11-17 01:54:00 +00003594
3595 key.objectid = ino2;
3596 key.type = BTRFS_INODE_REF_KEY;
3597 key.offset = 0;
3598
3599 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3600 if (ret < 0)
3601 goto out;
3602
3603 while (true) {
3604 struct extent_buffer *leaf = path->nodes[0];
3605 int slot = path->slots[0];
3606 u32 cur_offset = 0;
3607 u32 item_size;
3608
3609 if (slot >= btrfs_header_nritems(leaf)) {
3610 ret = btrfs_next_leaf(root, path);
3611 if (ret < 0)
3612 goto out;
3613 if (ret > 0)
3614 break;
3615 continue;
3616 }
3617
3618 btrfs_item_key_to_cpu(leaf, &key, slot);
3619 if (key.objectid != ino2)
3620 break;
3621 if (key.type != BTRFS_INODE_REF_KEY &&
3622 key.type != BTRFS_INODE_EXTREF_KEY)
3623 break;
3624
3625 item_size = btrfs_item_size_nr(leaf, slot);
3626 while (cur_offset < item_size) {
3627 u64 parent;
3628 u64 parent_gen;
3629
3630 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3631 unsigned long ptr;
3632 struct btrfs_inode_extref *extref;
3633
3634 ptr = btrfs_item_ptr_offset(leaf, slot);
3635 extref = (struct btrfs_inode_extref *)
3636 (ptr + cur_offset);
3637 parent = btrfs_inode_extref_parent(leaf,
3638 extref);
3639 cur_offset += sizeof(*extref);
3640 cur_offset += btrfs_inode_extref_name_len(leaf,
3641 extref);
3642 } else {
3643 parent = key.offset;
3644 cur_offset = item_size;
3645 }
3646
3647 ret = get_inode_info(root, parent, NULL, &parent_gen,
3648 NULL, NULL, NULL, NULL);
3649 if (ret < 0)
3650 goto out;
3651 ret = check_ino_in_path(root, ino1, ino1_gen,
3652 parent, parent_gen, fs_path);
3653 if (ret)
3654 goto out;
3655 }
3656 path->slots[0]++;
3657 }
3658 ret = 0;
Filipe Manana72c36682017-06-07 11:41:29 +01003659 out:
Filipe Mananaea37d592017-11-17 01:54:00 +00003660 btrfs_free_path(path);
3661 if (free_fs_path)
Filipe Manana72c36682017-06-07 11:41:29 +01003662 fs_path_free(fs_path);
3663 return ret;
Filipe Manana80aa6022015-03-27 17:50:45 +00003664}
3665
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003666static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003667 struct recorded_ref *parent_ref,
3668 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003669{
Filipe Mananaf9594922014-03-27 20:14:01 +00003670 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003671 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003672 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003673 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003674 struct fs_path *path_before = NULL;
3675 struct fs_path *path_after = NULL;
3676 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003677
3678 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003679 path_before = fs_path_alloc();
3680 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003681 ret = -ENOMEM;
3682 goto out;
3683 }
3684
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003685 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003686 * Our current directory inode may not yet be renamed/moved because some
3687 * ancestor (immediate or not) has to be renamed/moved first. So find if
3688 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003689 * that ancestor is processed to avoid path build infinite loops (done
3690 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003691 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003692 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003693 u64 parent_ino_after_gen;
3694
Filipe Mananaf9594922014-03-27 20:14:01 +00003695 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003696 /*
3697 * If the current inode is an ancestor of ino in the
3698 * parent root, we need to delay the rename of the
3699 * current inode, otherwise don't delayed the rename
3700 * because we can end up with a circular dependency
3701 * of renames, resulting in some directories never
3702 * getting the respective rename operations issued in
3703 * the send stream or getting into infinite path build
3704 * loops.
3705 */
3706 ret = is_ancestor(sctx->parent_root,
3707 sctx->cur_ino, sctx->cur_inode_gen,
3708 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003709 if (ret)
3710 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003711 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003712
3713 fs_path_reset(path_before);
3714 fs_path_reset(path_after);
3715
3716 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003717 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003718 if (ret < 0)
3719 goto out;
3720 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3721 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003722 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003723 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003724 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003725 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003726 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003727 }
3728
3729 len1 = fs_path_len(path_before);
3730 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003731 if (ino > sctx->cur_ino &&
3732 (parent_ino_before != parent_ino_after || len1 != len2 ||
3733 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003734 u64 parent_ino_gen;
3735
3736 ret = get_inode_info(sctx->parent_root, ino, NULL,
3737 &parent_ino_gen, NULL, NULL, NULL,
3738 NULL);
3739 if (ret < 0)
3740 goto out;
3741 if (ino_gen == parent_ino_gen) {
3742 ret = 1;
3743 break;
3744 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003745 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003746 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003747 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003748 }
3749
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003750out:
3751 fs_path_free(path_before);
3752 fs_path_free(path_after);
3753
Filipe Mananaf9594922014-03-27 20:14:01 +00003754 if (ret == 1) {
3755 ret = add_pending_dir_move(sctx,
3756 sctx->cur_ino,
3757 sctx->cur_inode_gen,
3758 ino,
3759 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003760 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003761 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003762 if (!ret)
3763 ret = 1;
3764 }
3765
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003766 return ret;
3767}
3768
Filipe Mananaf5962782017-06-22 20:03:51 +01003769static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3770{
3771 int ret;
3772 struct fs_path *new_path;
3773
3774 /*
3775 * Our reference's name member points to its full_path member string, so
3776 * we use here a new path.
3777 */
3778 new_path = fs_path_alloc();
3779 if (!new_path)
3780 return -ENOMEM;
3781
3782 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3783 if (ret < 0) {
3784 fs_path_free(new_path);
3785 return ret;
3786 }
3787 ret = fs_path_add(new_path, ref->name, ref->name_len);
3788 if (ret < 0) {
3789 fs_path_free(new_path);
3790 return ret;
3791 }
3792
3793 fs_path_free(ref->full_path);
3794 set_ref_path(ref, new_path);
3795
3796 return 0;
3797}
3798
Alexander Block31db9f72012-07-25 23:19:24 +02003799/*
Filipe Manana9c2b4e02020-09-21 14:13:30 +01003800 * When processing the new references for an inode we may orphanize an existing
3801 * directory inode because its old name conflicts with one of the new references
3802 * of the current inode. Later, when processing another new reference of our
3803 * inode, we might need to orphanize another inode, but the path we have in the
3804 * reference reflects the pre-orphanization name of the directory we previously
3805 * orphanized. For example:
3806 *
3807 * parent snapshot looks like:
3808 *
3809 * . (ino 256)
3810 * |----- f1 (ino 257)
3811 * |----- f2 (ino 258)
3812 * |----- d1/ (ino 259)
3813 * |----- d2/ (ino 260)
3814 *
3815 * send snapshot looks like:
3816 *
3817 * . (ino 256)
3818 * |----- d1 (ino 258)
3819 * |----- f2/ (ino 259)
3820 * |----- f2_link/ (ino 260)
3821 * | |----- f1 (ino 257)
3822 * |
3823 * |----- d2 (ino 258)
3824 *
3825 * When processing inode 257 we compute the name for inode 259 as "d1", and we
3826 * cache it in the name cache. Later when we start processing inode 258, when
3827 * collecting all its new references we set a full path of "d1/d2" for its new
3828 * reference with name "d2". When we start processing the new references we
3829 * start by processing the new reference with name "d1", and this results in
3830 * orphanizing inode 259, since its old reference causes a conflict. Then we
3831 * move on the next new reference, with name "d2", and we find out we must
3832 * orphanize inode 260, as its old reference conflicts with ours - but for the
3833 * orphanization we use a source path corresponding to the path we stored in the
3834 * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
3835 * receiver fail since the path component "d1/" no longer exists, it was renamed
3836 * to "o259-6-0/" when processing the previous new reference. So in this case we
3837 * must recompute the path in the new reference and use it for the new
3838 * orphanization operation.
3839 */
3840static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3841{
3842 char *name;
3843 int ret;
3844
3845 name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
3846 if (!name)
3847 return -ENOMEM;
3848
3849 fs_path_reset(ref->full_path);
3850 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
3851 if (ret < 0)
3852 goto out;
3853
3854 ret = fs_path_add(ref->full_path, name, ref->name_len);
3855 if (ret < 0)
3856 goto out;
3857
3858 /* Update the reference's base name pointer. */
3859 set_ref_path(ref, ref->full_path);
3860out:
3861 kfree(name);
3862 return ret;
3863}
3864
3865/*
Alexander Block31db9f72012-07-25 23:19:24 +02003866 * This does all the move/link/unlink/rmdir magic.
3867 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003868static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003869{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003870 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003871 int ret = 0;
3872 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003873 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003874 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003875 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003876 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003877 u64 ow_gen;
Filipe Mananaf5962782017-06-22 20:03:51 +01003878 u64 ow_mode;
Alexander Block31db9f72012-07-25 23:19:24 +02003879 int did_overwrite = 0;
3880 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003881 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003882 bool can_rename = true;
Filipe Mananaf5962782017-06-22 20:03:51 +01003883 bool orphanized_dir = false;
Filipe Mananafdb13882017-06-13 14:13:11 +01003884 bool orphanized_ancestor = false;
Alexander Block31db9f72012-07-25 23:19:24 +02003885
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003886 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003887
Alexander Block6d85ed052012-08-01 14:48:59 +02003888 /*
3889 * This should never happen as the root dir always has the same ref
3890 * which is always '..'
3891 */
3892 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003893 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed052012-08-01 14:48:59 +02003894
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003895 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003896 if (!valid_path) {
3897 ret = -ENOMEM;
3898 goto out;
3899 }
3900
Alexander Block31db9f72012-07-25 23:19:24 +02003901 /*
3902 * First, check if the first ref of the current inode was overwritten
3903 * before. If yes, we know that the current inode was already orphanized
3904 * and thus use the orphan name. If not, we can use get_cur_path to
3905 * get the path of the first ref as it would like while receiving at
3906 * this point in time.
3907 * New inodes are always orphan at the beginning, so force to use the
3908 * orphan name in this case.
3909 * The first ref is stored in valid_path and will be updated if it
3910 * gets moved around.
3911 */
3912 if (!sctx->cur_inode_new) {
3913 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3914 sctx->cur_inode_gen);
3915 if (ret < 0)
3916 goto out;
3917 if (ret)
3918 did_overwrite = 1;
3919 }
3920 if (sctx->cur_inode_new || did_overwrite) {
3921 ret = gen_unique_name(sctx, sctx->cur_ino,
3922 sctx->cur_inode_gen, valid_path);
3923 if (ret < 0)
3924 goto out;
3925 is_orphan = 1;
3926 } else {
3927 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3928 valid_path);
3929 if (ret < 0)
3930 goto out;
3931 }
3932
Filipe Manana98272bb2020-09-21 14:13:29 +01003933 /*
3934 * Before doing any rename and link operations, do a first pass on the
3935 * new references to orphanize any unprocessed inodes that may have a
3936 * reference that conflicts with one of the new references of the current
3937 * inode. This needs to happen first because a new reference may conflict
3938 * with the old reference of a parent directory, so we must make sure
3939 * that the path used for link and rename commands don't use an
3940 * orphanized name when an ancestor was not yet orphanized.
3941 *
3942 * Example:
3943 *
3944 * Parent snapshot:
3945 *
3946 * . (ino 256)
3947 * |----- testdir/ (ino 259)
3948 * | |----- a (ino 257)
3949 * |
3950 * |----- b (ino 258)
3951 *
3952 * Send snapshot:
3953 *
3954 * . (ino 256)
3955 * |----- testdir_2/ (ino 259)
3956 * | |----- a (ino 260)
3957 * |
3958 * |----- testdir (ino 257)
3959 * |----- b (ino 257)
3960 * |----- b2 (ino 258)
3961 *
3962 * Processing the new reference for inode 257 with name "b" may happen
3963 * before processing the new reference with name "testdir". If so, we
3964 * must make sure that by the time we send a link command to create the
3965 * hard link "b", inode 259 was already orphanized, since the generated
3966 * path in "valid_path" already contains the orphanized name for 259.
3967 * We are processing inode 257, so only later when processing 259 we do
3968 * the rename operation to change its temporary (orphanized) name to
3969 * "testdir_2".
3970 */
Alexander Block31db9f72012-07-25 23:19:24 +02003971 list_for_each_entry(cur, &sctx->new_refs, list) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003972 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3973 if (ret < 0)
3974 goto out;
Filipe Manana98272bb2020-09-21 14:13:29 +01003975 if (ret == inode_state_will_create)
3976 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02003977
3978 /*
Filipe Manana98272bb2020-09-21 14:13:29 +01003979 * Check if this new ref would overwrite the first ref of another
3980 * unprocessed inode. If yes, orphanize the overwritten inode.
3981 * If we find an overwritten ref that is not the first ref,
3982 * simply unlink it.
Alexander Block31db9f72012-07-25 23:19:24 +02003983 */
3984 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3985 cur->name, cur->name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01003986 &ow_inode, &ow_gen, &ow_mode);
Alexander Block31db9f72012-07-25 23:19:24 +02003987 if (ret < 0)
3988 goto out;
3989 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003990 ret = is_first_ref(sctx->parent_root,
3991 ow_inode, cur->dir, cur->name,
3992 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003993 if (ret < 0)
3994 goto out;
3995 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003996 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003997 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003998
Filipe Manana9c2b4e02020-09-21 14:13:30 +01003999 if (orphanized_dir) {
4000 ret = refresh_ref_path(sctx, cur);
4001 if (ret < 0)
4002 goto out;
4003 }
4004
Alexander Block31db9f72012-07-25 23:19:24 +02004005 ret = orphanize_inode(sctx, ow_inode, ow_gen,
4006 cur->full_path);
4007 if (ret < 0)
4008 goto out;
Filipe Mananaf5962782017-06-22 20:03:51 +01004009 if (S_ISDIR(ow_mode))
4010 orphanized_dir = true;
Robbie Ko801bec32015-06-23 18:39:46 +08004011
4012 /*
4013 * If ow_inode has its rename operation delayed
4014 * make sure that its orphanized name is used in
4015 * the source path when performing its rename
4016 * operation.
4017 */
4018 if (is_waiting_for_move(sctx, ow_inode)) {
4019 wdm = get_waiting_dir_move(sctx,
4020 ow_inode);
4021 ASSERT(wdm);
4022 wdm->orphanized = true;
4023 }
4024
Filipe Manana8996a482015-03-12 15:16:20 +00004025 /*
4026 * Make sure we clear our orphanized inode's
4027 * name from the name cache. This is because the
4028 * inode ow_inode might be an ancestor of some
4029 * other inode that will be orphanized as well
4030 * later and has an inode number greater than
4031 * sctx->send_progress. We need to prevent
4032 * future name lookups from using the old name
4033 * and get instead the orphan name.
4034 */
4035 nce = name_cache_search(sctx, ow_inode, ow_gen);
4036 if (nce) {
4037 name_cache_delete(sctx, nce);
4038 kfree(nce);
4039 }
Robbie Ko801bec32015-06-23 18:39:46 +08004040
4041 /*
4042 * ow_inode might currently be an ancestor of
4043 * cur_ino, therefore compute valid_path (the
4044 * current path of cur_ino) again because it
4045 * might contain the pre-orphanization name of
4046 * ow_inode, which is no longer valid.
4047 */
Filipe Manana72c36682017-06-07 11:41:29 +01004048 ret = is_ancestor(sctx->parent_root,
4049 ow_inode, ow_gen,
4050 sctx->cur_ino, NULL);
4051 if (ret > 0) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004052 orphanized_ancestor = true;
Filipe Manana72c36682017-06-07 11:41:29 +01004053 fs_path_reset(valid_path);
4054 ret = get_cur_path(sctx, sctx->cur_ino,
4055 sctx->cur_inode_gen,
4056 valid_path);
4057 }
Robbie Ko801bec32015-06-23 18:39:46 +08004058 if (ret < 0)
4059 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004060 } else {
Filipe Mananad8ac76c2021-06-09 11:25:03 +01004061 /*
4062 * If we previously orphanized a directory that
4063 * collided with a new reference that we already
4064 * processed, recompute the current path because
4065 * that directory may be part of the path.
4066 */
4067 if (orphanized_dir) {
4068 ret = refresh_ref_path(sctx, cur);
4069 if (ret < 0)
4070 goto out;
4071 }
Alexander Block31db9f72012-07-25 23:19:24 +02004072 ret = send_unlink(sctx, cur->full_path);
4073 if (ret < 0)
4074 goto out;
4075 }
4076 }
4077
Filipe Manana98272bb2020-09-21 14:13:29 +01004078 }
4079
4080 list_for_each_entry(cur, &sctx->new_refs, list) {
4081 /*
4082 * We may have refs where the parent directory does not exist
4083 * yet. This happens if the parent directories inum is higher
4084 * than the current inum. To handle this case, we create the
4085 * parent directory out of order. But we need to check if this
4086 * did already happen before due to other refs in the same dir.
4087 */
4088 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4089 if (ret < 0)
4090 goto out;
4091 if (ret == inode_state_will_create) {
4092 ret = 0;
4093 /*
4094 * First check if any of the current inodes refs did
4095 * already create the dir.
4096 */
4097 list_for_each_entry(cur2, &sctx->new_refs, list) {
4098 if (cur == cur2)
4099 break;
4100 if (cur2->dir == cur->dir) {
4101 ret = 1;
4102 break;
4103 }
4104 }
4105
4106 /*
4107 * If that did not happen, check if a previous inode
4108 * did already create the dir.
4109 */
4110 if (!ret)
4111 ret = did_create_dir(sctx, cur->dir);
4112 if (ret < 0)
4113 goto out;
4114 if (!ret) {
4115 ret = send_create_inode(sctx, cur->dir);
4116 if (ret < 0)
4117 goto out;
4118 }
4119 }
4120
Filipe Manana84471e22015-02-28 22:29:22 +00004121 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4122 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4123 if (ret < 0)
4124 goto out;
4125 if (ret == 1) {
4126 can_rename = false;
4127 *pending_move = 1;
4128 }
4129 }
4130
Filipe Manana8b191a62015-04-09 14:09:14 +01004131 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4132 can_rename) {
4133 ret = wait_for_parent_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
Alexander Block31db9f72012-07-25 23:19:24 +02004142 /*
4143 * link/move the ref to the new place. If we have an orphan
4144 * inode, move it and update valid_path. If not, link or move
4145 * it depending on the inode mode.
4146 */
Filipe Manana84471e22015-02-28 22:29:22 +00004147 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004148 ret = send_rename(sctx, valid_path, cur->full_path);
4149 if (ret < 0)
4150 goto out;
4151 is_orphan = 0;
4152 ret = fs_path_copy(valid_path, cur->full_path);
4153 if (ret < 0)
4154 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00004155 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004156 if (S_ISDIR(sctx->cur_inode_mode)) {
4157 /*
4158 * Dirs can't be linked, so move it. For moved
4159 * dirs, we always have one new and one deleted
4160 * ref. The deleted ref is ignored later.
4161 */
Filipe Manana8b191a62015-04-09 14:09:14 +01004162 ret = send_rename(sctx, valid_path,
4163 cur->full_path);
4164 if (!ret)
4165 ret = fs_path_copy(valid_path,
4166 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004167 if (ret < 0)
4168 goto out;
4169 } else {
Filipe Mananaf5962782017-06-22 20:03:51 +01004170 /*
4171 * We might have previously orphanized an inode
4172 * which is an ancestor of our current inode,
4173 * so our reference's full path, which was
4174 * computed before any such orphanizations, must
4175 * be updated.
4176 */
4177 if (orphanized_dir) {
4178 ret = update_ref_path(sctx, cur);
4179 if (ret < 0)
4180 goto out;
4181 }
Alexander Block31db9f72012-07-25 23:19:24 +02004182 ret = send_link(sctx, cur->full_path,
4183 valid_path);
4184 if (ret < 0)
4185 goto out;
4186 }
4187 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004188 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004189 if (ret < 0)
4190 goto out;
4191 }
4192
4193 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4194 /*
4195 * Check if we can already rmdir the directory. If not,
4196 * orphanize it. For every dir item inside that gets deleted
4197 * later, we do this check again and rmdir it then if possible.
4198 * See the use of check_dirs for more details.
4199 */
Filipe Manana9dc44212014-02-19 14:31:44 +00004200 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4201 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004202 if (ret < 0)
4203 goto out;
4204 if (ret) {
4205 ret = send_rmdir(sctx, valid_path);
4206 if (ret < 0)
4207 goto out;
4208 } else if (!is_orphan) {
4209 ret = orphanize_inode(sctx, sctx->cur_ino,
4210 sctx->cur_inode_gen, valid_path);
4211 if (ret < 0)
4212 goto out;
4213 is_orphan = 1;
4214 }
4215
4216 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004217 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004218 if (ret < 0)
4219 goto out;
4220 }
Alexander Blockccf16262012-07-28 11:46:29 +02004221 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4222 !list_empty(&sctx->deleted_refs)) {
4223 /*
4224 * We have a moved dir. Add the old parent to check_dirs
4225 */
4226 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4227 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04004228 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02004229 if (ret < 0)
4230 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004231 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4232 /*
4233 * We have a non dir inode. Go through all deleted refs and
4234 * unlink them if they were not already overwritten by other
4235 * inodes.
4236 */
4237 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4238 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4239 sctx->cur_ino, sctx->cur_inode_gen,
4240 cur->name, cur->name_len);
4241 if (ret < 0)
4242 goto out;
4243 if (!ret) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004244 /*
4245 * If we orphanized any ancestor before, we need
4246 * to recompute the full path for deleted names,
4247 * since any such path was computed before we
4248 * processed any references and orphanized any
4249 * ancestor inode.
4250 */
4251 if (orphanized_ancestor) {
Filipe Mananaf5962782017-06-22 20:03:51 +01004252 ret = update_ref_path(sctx, cur);
4253 if (ret < 0)
Filipe Mananafdb13882017-06-13 14:13:11 +01004254 goto out;
Filipe Mananafdb13882017-06-13 14:13:11 +01004255 }
Alexander Block1f4692d2012-07-28 10:42:24 +02004256 ret = send_unlink(sctx, cur->full_path);
4257 if (ret < 0)
4258 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004259 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004260 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004261 if (ret < 0)
4262 goto out;
4263 }
Alexander Block31db9f72012-07-25 23:19:24 +02004264 /*
4265 * If the inode is still orphan, unlink the orphan. This may
4266 * happen when a previous inode did overwrite the first ref
4267 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02004268 * inode. Unlinking does not mean that the inode is deleted in
4269 * all cases. There may still be links to this inode in other
4270 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02004271 */
Alexander Block1f4692d2012-07-28 10:42:24 +02004272 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02004273 ret = send_unlink(sctx, valid_path);
4274 if (ret < 0)
4275 goto out;
4276 }
4277 }
4278
4279 /*
4280 * We did collect all parent dirs where cur_inode was once located. We
4281 * now go through all these dirs and check if they are pending for
4282 * deletion and if it's finally possible to perform the rmdir now.
4283 * We also update the inode stats of the parent dirs here.
4284 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004285 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02004286 /*
4287 * In case we had refs into dirs that were not processed yet,
4288 * we don't need to do the utime and rmdir logic for these dirs.
4289 * The dir will be processed later.
4290 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004291 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02004292 continue;
4293
Josef Bacikba5e8f22013-08-16 16:52:55 -04004294 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004295 if (ret < 0)
4296 goto out;
4297
4298 if (ret == inode_state_did_create ||
4299 ret == inode_state_no_change) {
4300 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004301 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004302 if (ret < 0)
4303 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004304 } else if (ret == inode_state_did_delete &&
4305 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004306 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4307 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004308 if (ret < 0)
4309 goto out;
4310 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004311 ret = get_cur_path(sctx, cur->dir,
4312 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004313 if (ret < 0)
4314 goto out;
4315 ret = send_rmdir(sctx, valid_path);
4316 if (ret < 0)
4317 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004318 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004319 }
4320 }
4321 }
4322
Alexander Block31db9f72012-07-25 23:19:24 +02004323 ret = 0;
4324
4325out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004326 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004327 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004328 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004329 return ret;
4330}
4331
Nikolay Borisova0357512017-08-21 12:43:44 +03004332static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4333 void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004334{
4335 int ret = 0;
4336 struct send_ctx *sctx = ctx;
4337 struct fs_path *p;
4338 u64 gen;
4339
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004340 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004341 if (!p)
4342 return -ENOMEM;
4343
Liu Boa4d96d62014-03-03 21:31:03 +08004344 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004345 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004346 if (ret < 0)
4347 goto out;
4348
Alexander Block31db9f72012-07-25 23:19:24 +02004349 ret = get_cur_path(sctx, dir, gen, p);
4350 if (ret < 0)
4351 goto out;
4352 ret = fs_path_add_path(p, name);
4353 if (ret < 0)
4354 goto out;
4355
Liu Boa4d96d62014-03-03 21:31:03 +08004356 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004357
4358out:
4359 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004360 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004361 return ret;
4362}
4363
Liu Boa4d96d62014-03-03 21:31:03 +08004364static int __record_new_ref(int num, u64 dir, int index,
4365 struct fs_path *name,
4366 void *ctx)
4367{
4368 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004369 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
Liu Boa4d96d62014-03-03 21:31:03 +08004370}
4371
4372
Alexander Block31db9f72012-07-25 23:19:24 +02004373static int __record_deleted_ref(int num, u64 dir, int index,
4374 struct fs_path *name,
4375 void *ctx)
4376{
Alexander Block31db9f72012-07-25 23:19:24 +02004377 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004378 return record_ref(sctx->parent_root, dir, name, ctx,
4379 &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004380}
4381
4382static int record_new_ref(struct send_ctx *sctx)
4383{
4384 int ret;
4385
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004386 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4387 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004388 if (ret < 0)
4389 goto out;
4390 ret = 0;
4391
4392out:
4393 return ret;
4394}
4395
4396static int record_deleted_ref(struct send_ctx *sctx)
4397{
4398 int ret;
4399
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004400 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4401 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004402 if (ret < 0)
4403 goto out;
4404 ret = 0;
4405
4406out:
4407 return ret;
4408}
4409
4410struct find_ref_ctx {
4411 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004412 u64 dir_gen;
4413 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004414 struct fs_path *name;
4415 int found_idx;
4416};
4417
4418static int __find_iref(int num, u64 dir, int index,
4419 struct fs_path *name,
4420 void *ctx_)
4421{
4422 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004423 u64 dir_gen;
4424 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004425
4426 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4427 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004428 /*
4429 * To avoid doing extra lookups we'll only do this if everything
4430 * else matches.
4431 */
4432 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4433 NULL, NULL, NULL);
4434 if (ret)
4435 return ret;
4436 if (dir_gen != ctx->dir_gen)
4437 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004438 ctx->found_idx = num;
4439 return 1;
4440 }
4441 return 0;
4442}
4443
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004444static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004445 struct btrfs_path *path,
4446 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004447 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004448{
4449 int ret;
4450 struct find_ref_ctx ctx;
4451
4452 ctx.dir = dir;
4453 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004454 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004455 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004456 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004457
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004458 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004459 if (ret < 0)
4460 return ret;
4461
4462 if (ctx.found_idx == -1)
4463 return -ENOENT;
4464
4465 return ctx.found_idx;
4466}
4467
4468static int __record_changed_new_ref(int num, u64 dir, int index,
4469 struct fs_path *name,
4470 void *ctx)
4471{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004472 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004473 int ret;
4474 struct send_ctx *sctx = ctx;
4475
Josef Bacikba5e8f22013-08-16 16:52:55 -04004476 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4477 NULL, NULL, NULL);
4478 if (ret)
4479 return ret;
4480
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004481 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004482 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004483 if (ret == -ENOENT)
4484 ret = __record_new_ref(num, dir, index, name, sctx);
4485 else if (ret > 0)
4486 ret = 0;
4487
4488 return ret;
4489}
4490
4491static int __record_changed_deleted_ref(int num, u64 dir, int index,
4492 struct fs_path *name,
4493 void *ctx)
4494{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004495 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004496 int ret;
4497 struct send_ctx *sctx = ctx;
4498
Josef Bacikba5e8f22013-08-16 16:52:55 -04004499 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4500 NULL, NULL, NULL);
4501 if (ret)
4502 return ret;
4503
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004504 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004505 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004506 if (ret == -ENOENT)
4507 ret = __record_deleted_ref(num, dir, index, name, sctx);
4508 else if (ret > 0)
4509 ret = 0;
4510
4511 return ret;
4512}
4513
4514static int record_changed_ref(struct send_ctx *sctx)
4515{
4516 int ret = 0;
4517
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004518 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004519 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4520 if (ret < 0)
4521 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004522 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004523 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4524 if (ret < 0)
4525 goto out;
4526 ret = 0;
4527
4528out:
4529 return ret;
4530}
4531
4532/*
4533 * Record and process all refs at once. Needed when an inode changes the
4534 * generation number, which means that it was deleted and recreated.
4535 */
4536static int process_all_refs(struct send_ctx *sctx,
4537 enum btrfs_compare_tree_result cmd)
4538{
4539 int ret;
4540 struct btrfs_root *root;
4541 struct btrfs_path *path;
4542 struct btrfs_key key;
4543 struct btrfs_key found_key;
4544 struct extent_buffer *eb;
4545 int slot;
4546 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004547 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004548
4549 path = alloc_path_for_send();
4550 if (!path)
4551 return -ENOMEM;
4552
4553 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4554 root = sctx->send_root;
4555 cb = __record_new_ref;
4556 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4557 root = sctx->parent_root;
4558 cb = __record_deleted_ref;
4559 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004560 btrfs_err(sctx->send_root->fs_info,
4561 "Wrong command %d in process_all_refs", cmd);
4562 ret = -EINVAL;
4563 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004564 }
4565
4566 key.objectid = sctx->cmp_key->objectid;
4567 key.type = BTRFS_INODE_REF_KEY;
4568 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004569 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4570 if (ret < 0)
4571 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004572
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004573 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004574 eb = path->nodes[0];
4575 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004576 if (slot >= btrfs_header_nritems(eb)) {
4577 ret = btrfs_next_leaf(root, path);
4578 if (ret < 0)
4579 goto out;
4580 else if (ret > 0)
4581 break;
4582 continue;
4583 }
4584
Alexander Block31db9f72012-07-25 23:19:24 +02004585 btrfs_item_key_to_cpu(eb, &found_key, slot);
4586
4587 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004588 (found_key.type != BTRFS_INODE_REF_KEY &&
4589 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004590 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004591
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004592 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004593 if (ret < 0)
4594 goto out;
4595
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004596 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004597 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004598 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004599
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004600 /*
4601 * We don't actually care about pending_move as we are simply
4602 * re-creating this inode and will be rename'ing it into place once we
4603 * rename the parent directory.
4604 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004605 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004606out:
4607 btrfs_free_path(path);
4608 return ret;
4609}
4610
4611static int send_set_xattr(struct send_ctx *sctx,
4612 struct fs_path *path,
4613 const char *name, int name_len,
4614 const char *data, int data_len)
4615{
4616 int ret = 0;
4617
4618 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4619 if (ret < 0)
4620 goto out;
4621
4622 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4623 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4624 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4625
4626 ret = send_cmd(sctx);
4627
4628tlv_put_failure:
4629out:
4630 return ret;
4631}
4632
4633static int send_remove_xattr(struct send_ctx *sctx,
4634 struct fs_path *path,
4635 const char *name, int name_len)
4636{
4637 int ret = 0;
4638
4639 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4640 if (ret < 0)
4641 goto out;
4642
4643 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4644 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4645
4646 ret = send_cmd(sctx);
4647
4648tlv_put_failure:
4649out:
4650 return ret;
4651}
4652
4653static int __process_new_xattr(int num, struct btrfs_key *di_key,
4654 const char *name, int name_len,
4655 const char *data, int data_len,
4656 u8 type, void *ctx)
4657{
4658 int ret;
4659 struct send_ctx *sctx = ctx;
4660 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004661 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004662
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -03004663 /* Capabilities are emitted by finish_inode_if_needed */
4664 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4665 return 0;
4666
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004667 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004668 if (!p)
4669 return -ENOMEM;
4670
4671 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004672 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004673 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004674 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004675 * only contains the version number and no entries.
4676 */
4677 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4678 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4679 if (data_len == 0) {
4680 dummy_acl.a_version =
4681 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4682 data = (char *)&dummy_acl;
4683 data_len = sizeof(dummy_acl);
4684 }
4685 }
4686
4687 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4688 if (ret < 0)
4689 goto out;
4690
4691 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4692
4693out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004694 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004695 return ret;
4696}
4697
4698static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4699 const char *name, int name_len,
4700 const char *data, int data_len,
4701 u8 type, void *ctx)
4702{
4703 int ret;
4704 struct send_ctx *sctx = ctx;
4705 struct fs_path *p;
4706
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004707 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004708 if (!p)
4709 return -ENOMEM;
4710
4711 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4712 if (ret < 0)
4713 goto out;
4714
4715 ret = send_remove_xattr(sctx, p, name, name_len);
4716
4717out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004718 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004719 return ret;
4720}
4721
4722static int process_new_xattr(struct send_ctx *sctx)
4723{
4724 int ret = 0;
4725
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004726 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004727 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004728
4729 return ret;
4730}
4731
4732static int process_deleted_xattr(struct send_ctx *sctx)
4733{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004734 return iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004735 __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004736}
4737
4738struct find_xattr_ctx {
4739 const char *name;
4740 int name_len;
4741 int found_idx;
4742 char *found_data;
4743 int found_data_len;
4744};
4745
4746static int __find_xattr(int num, struct btrfs_key *di_key,
4747 const char *name, int name_len,
4748 const char *data, int data_len,
4749 u8 type, void *vctx)
4750{
4751 struct find_xattr_ctx *ctx = vctx;
4752
4753 if (name_len == ctx->name_len &&
4754 strncmp(name, ctx->name, name_len) == 0) {
4755 ctx->found_idx = num;
4756 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004757 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004758 if (!ctx->found_data)
4759 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004760 return 1;
4761 }
4762 return 0;
4763}
4764
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004765static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004766 struct btrfs_path *path,
4767 struct btrfs_key *key,
4768 const char *name, int name_len,
4769 char **data, int *data_len)
4770{
4771 int ret;
4772 struct find_xattr_ctx ctx;
4773
4774 ctx.name = name;
4775 ctx.name_len = name_len;
4776 ctx.found_idx = -1;
4777 ctx.found_data = NULL;
4778 ctx.found_data_len = 0;
4779
Nikolay Borisova0357512017-08-21 12:43:44 +03004780 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004781 if (ret < 0)
4782 return ret;
4783
4784 if (ctx.found_idx == -1)
4785 return -ENOENT;
4786 if (data) {
4787 *data = ctx.found_data;
4788 *data_len = ctx.found_data_len;
4789 } else {
4790 kfree(ctx.found_data);
4791 }
4792 return ctx.found_idx;
4793}
4794
4795
4796static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4797 const char *name, int name_len,
4798 const char *data, int data_len,
4799 u8 type, void *ctx)
4800{
4801 int ret;
4802 struct send_ctx *sctx = ctx;
4803 char *found_data = NULL;
4804 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004805
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004806 ret = find_xattr(sctx->parent_root, sctx->right_path,
4807 sctx->cmp_key, name, name_len, &found_data,
4808 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004809 if (ret == -ENOENT) {
4810 ret = __process_new_xattr(num, di_key, name, name_len, data,
4811 data_len, type, ctx);
4812 } else if (ret >= 0) {
4813 if (data_len != found_data_len ||
4814 memcmp(data, found_data, data_len)) {
4815 ret = __process_new_xattr(num, di_key, name, name_len,
4816 data, data_len, type, ctx);
4817 } else {
4818 ret = 0;
4819 }
4820 }
4821
4822 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004823 return ret;
4824}
4825
4826static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4827 const char *name, int name_len,
4828 const char *data, int data_len,
4829 u8 type, void *ctx)
4830{
4831 int ret;
4832 struct send_ctx *sctx = ctx;
4833
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004834 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4835 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004836 if (ret == -ENOENT)
4837 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4838 data_len, type, ctx);
4839 else if (ret >= 0)
4840 ret = 0;
4841
4842 return ret;
4843}
4844
4845static int process_changed_xattr(struct send_ctx *sctx)
4846{
4847 int ret = 0;
4848
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004849 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004850 __process_changed_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004851 if (ret < 0)
4852 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004853 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004854 __process_changed_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004855
4856out:
4857 return ret;
4858}
4859
4860static int process_all_new_xattrs(struct send_ctx *sctx)
4861{
4862 int ret;
4863 struct btrfs_root *root;
4864 struct btrfs_path *path;
4865 struct btrfs_key key;
4866 struct btrfs_key found_key;
4867 struct extent_buffer *eb;
4868 int slot;
4869
4870 path = alloc_path_for_send();
4871 if (!path)
4872 return -ENOMEM;
4873
4874 root = sctx->send_root;
4875
4876 key.objectid = sctx->cmp_key->objectid;
4877 key.type = BTRFS_XATTR_ITEM_KEY;
4878 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004879 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4880 if (ret < 0)
4881 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004882
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004883 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004884 eb = path->nodes[0];
4885 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004886 if (slot >= btrfs_header_nritems(eb)) {
4887 ret = btrfs_next_leaf(root, path);
4888 if (ret < 0) {
4889 goto out;
4890 } else if (ret > 0) {
4891 ret = 0;
4892 break;
4893 }
4894 continue;
4895 }
Alexander Block31db9f72012-07-25 23:19:24 +02004896
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004897 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004898 if (found_key.objectid != key.objectid ||
4899 found_key.type != key.type) {
4900 ret = 0;
4901 goto out;
4902 }
4903
Nikolay Borisova0357512017-08-21 12:43:44 +03004904 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004905 if (ret < 0)
4906 goto out;
4907
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004908 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004909 }
4910
4911out:
4912 btrfs_free_path(path);
4913 return ret;
4914}
4915
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07004916static inline u64 max_send_read_size(const struct send_ctx *sctx)
4917{
4918 return sctx->send_max_size - SZ_16K;
4919}
4920
4921static int put_data_header(struct send_ctx *sctx, u32 len)
4922{
4923 struct btrfs_tlv_header *hdr;
4924
4925 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
4926 return -EOVERFLOW;
4927 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
4928 put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
4929 put_unaligned_le16(len, &hdr->tlv_len);
4930 sctx->send_size += sizeof(*hdr);
4931 return 0;
4932}
4933
4934static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
Josef Baciked259092013-10-25 11:36:01 -04004935{
4936 struct btrfs_root *root = sctx->send_root;
4937 struct btrfs_fs_info *fs_info = root->fs_info;
4938 struct inode *inode;
4939 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004940 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004941 pgoff_t last_index;
Johannes Thumshirn70730172018-12-05 15:23:03 +01004942 unsigned pg_offset = offset_in_page(offset);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07004943 int ret;
4944
4945 ret = put_data_header(sctx, len);
4946 if (ret)
4947 return ret;
Josef Baciked259092013-10-25 11:36:01 -04004948
David Sterba0202e832020-05-15 19:35:59 +02004949 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
Josef Baciked259092013-10-25 11:36:01 -04004950 if (IS_ERR(inode))
4951 return PTR_ERR(inode);
4952
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004953 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004954
4955 /* initial readahead */
4956 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4957 file_ra_state_init(&sctx->ra, inode->i_mapping);
Liu Bo2131bcd2014-03-05 10:07:35 +08004958
Josef Baciked259092013-10-25 11:36:01 -04004959 while (index <= last_index) {
4960 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004961 PAGE_SIZE - pg_offset);
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004962
4963 page = find_lock_page(inode->i_mapping, index);
Josef Baciked259092013-10-25 11:36:01 -04004964 if (!page) {
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004965 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4966 NULL, index, last_index + 1 - index);
4967
4968 page = find_or_create_page(inode->i_mapping, index,
4969 GFP_KERNEL);
4970 if (!page) {
4971 ret = -ENOMEM;
4972 break;
4973 }
4974 }
4975
4976 if (PageReadahead(page)) {
4977 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4978 NULL, page, index, last_index + 1 - index);
Josef Baciked259092013-10-25 11:36:01 -04004979 }
4980
4981 if (!PageUptodate(page)) {
4982 btrfs_readpage(NULL, page);
4983 lock_page(page);
4984 if (!PageUptodate(page)) {
4985 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004986 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004987 ret = -EIO;
4988 break;
4989 }
4990 }
4991
Ira Weiny3590ec52021-02-09 22:22:19 -08004992 memcpy_from_page(sctx->send_buf + sctx->send_size, page,
4993 pg_offset, cur_len);
Josef Baciked259092013-10-25 11:36:01 -04004994 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004995 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004996 index++;
4997 pg_offset = 0;
4998 len -= cur_len;
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07004999 sctx->send_size += cur_len;
Josef Baciked259092013-10-25 11:36:01 -04005000 }
Josef Baciked259092013-10-25 11:36:01 -04005001 iput(inode);
5002 return ret;
5003}
5004
Alexander Block31db9f72012-07-25 23:19:24 +02005005/*
5006 * Read some bytes from the current inode/file and send a write command to
5007 * user space.
5008 */
5009static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
5010{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04005011 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02005012 int ret = 0;
5013 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02005014
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005015 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02005016 if (!p)
5017 return -ENOMEM;
5018
Jeff Mahoney04ab9562016-09-20 10:05:03 -04005019 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005020
Alexander Block31db9f72012-07-25 23:19:24 +02005021 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5022 if (ret < 0)
5023 goto out;
5024
5025 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5026 if (ret < 0)
5027 goto out;
5028
5029 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5030 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005031 ret = put_file_data(sctx, offset, len);
5032 if (ret < 0)
5033 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005034
5035 ret = send_cmd(sctx);
5036
5037tlv_put_failure:
5038out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005039 fs_path_free(p);
Omar Sandovala9b2e0d2020-08-21 00:39:51 -07005040 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005041}
5042
5043/*
5044 * Send a clone command to user space.
5045 */
5046static int send_clone(struct send_ctx *sctx,
5047 u64 offset, u32 len,
5048 struct clone_root *clone_root)
5049{
5050 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005051 struct fs_path *p;
5052 u64 gen;
5053
Jeff Mahoney04ab9562016-09-20 10:05:03 -04005054 btrfs_debug(sctx->send_root->fs_info,
5055 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005056 offset, len, clone_root->root->root_key.objectid,
5057 clone_root->ino, clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02005058
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005059 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02005060 if (!p)
5061 return -ENOMEM;
5062
5063 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5064 if (ret < 0)
5065 goto out;
5066
5067 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5068 if (ret < 0)
5069 goto out;
5070
5071 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5072 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5073 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5074
Alexander Blocke938c8a2012-07-28 16:33:49 +02005075 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02005076 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005077 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005078 if (ret < 0)
5079 goto out;
5080 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5081 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005082 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02005083 }
5084 if (ret < 0)
5085 goto out;
5086
Josef Bacik37b8d272015-06-04 17:17:25 -04005087 /*
5088 * If the parent we're using has a received_uuid set then use that as
5089 * our clone source as that is what we will look for when doing a
5090 * receive.
5091 *
5092 * This covers the case that we create a snapshot off of a received
5093 * subvolume and then use that as the parent and try to receive on a
5094 * different host.
5095 */
5096 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
5097 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5098 clone_root->root->root_item.received_uuid);
5099 else
5100 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5101 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02005102 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
David Sterba09e3a282020-09-15 14:30:15 +02005103 btrfs_root_ctransid(&clone_root->root->root_item));
Alexander Block31db9f72012-07-25 23:19:24 +02005104 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5105 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5106 clone_root->offset);
5107
5108 ret = send_cmd(sctx);
5109
5110tlv_put_failure:
5111out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005112 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02005113 return ret;
5114}
5115
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005116/*
5117 * Send an update extent command to user space.
5118 */
5119static int send_update_extent(struct send_ctx *sctx,
5120 u64 offset, u32 len)
5121{
5122 int ret = 0;
5123 struct fs_path *p;
5124
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005125 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005126 if (!p)
5127 return -ENOMEM;
5128
5129 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5130 if (ret < 0)
5131 goto out;
5132
5133 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5134 if (ret < 0)
5135 goto out;
5136
5137 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5138 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5139 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5140
5141 ret = send_cmd(sctx);
5142
5143tlv_put_failure:
5144out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005145 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005146 return ret;
5147}
5148
Josef Bacik16e75492013-10-22 12:18:51 -04005149static int send_hole(struct send_ctx *sctx, u64 end)
5150{
5151 struct fs_path *p = NULL;
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005152 u64 read_size = max_send_read_size(sctx);
Josef Bacik16e75492013-10-22 12:18:51 -04005153 u64 offset = sctx->cur_inode_last_extent;
Josef Bacik16e75492013-10-22 12:18:51 -04005154 int ret = 0;
5155
Filipe Manana22d31512018-07-30 12:39:58 +01005156 /*
5157 * A hole that starts at EOF or beyond it. Since we do not yet support
5158 * fallocate (for extent preallocation and hole punching), sending a
5159 * write of zeroes starting at EOF or beyond would later require issuing
5160 * a truncate operation which would undo the write and achieve nothing.
5161 */
5162 if (offset >= sctx->cur_inode_size)
5163 return 0;
5164
Filipe Manana6b1f72e52019-05-20 09:55:42 +01005165 /*
5166 * Don't go beyond the inode's i_size due to prealloc extents that start
5167 * after the i_size.
5168 */
5169 end = min_t(u64, end, sctx->cur_inode_size);
5170
Filipe Mananad4dfc0f2018-02-06 20:39:20 +00005171 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5172 return send_update_extent(sctx, offset, end - offset);
5173
Josef Bacik16e75492013-10-22 12:18:51 -04005174 p = fs_path_alloc();
5175 if (!p)
5176 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01005177 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5178 if (ret < 0)
5179 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04005180 while (offset < end) {
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005181 u64 len = min(end - offset, read_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005182
5183 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5184 if (ret < 0)
5185 break;
Josef Bacik16e75492013-10-22 12:18:51 -04005186 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5187 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005188 ret = put_data_header(sctx, len);
5189 if (ret < 0)
5190 break;
5191 memset(sctx->send_buf + sctx->send_size, 0, len);
5192 sctx->send_size += len;
Josef Bacik16e75492013-10-22 12:18:51 -04005193 ret = send_cmd(sctx);
5194 if (ret < 0)
5195 break;
5196 offset += len;
5197 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005198 sctx->cur_inode_next_write_offset = offset;
Josef Bacik16e75492013-10-22 12:18:51 -04005199tlv_put_failure:
5200 fs_path_free(p);
5201 return ret;
5202}
5203
Filipe Mananad906d492015-10-02 10:47:34 +01005204static int send_extent_data(struct send_ctx *sctx,
5205 const u64 offset,
5206 const u64 len)
5207{
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005208 u64 read_size = max_send_read_size(sctx);
Filipe Mananad906d492015-10-02 10:47:34 +01005209 u64 sent = 0;
5210
5211 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5212 return send_update_extent(sctx, offset, len);
5213
5214 while (sent < len) {
Omar Sandoval8c7d9fe2020-08-21 00:39:52 -07005215 u64 size = min(len - sent, read_size);
Filipe Mananad906d492015-10-02 10:47:34 +01005216 int ret;
5217
Filipe Mananad906d492015-10-02 10:47:34 +01005218 ret = send_write(sctx, offset + sent, size);
5219 if (ret < 0)
5220 return ret;
Omar Sandovala9b2e0d2020-08-21 00:39:51 -07005221 sent += size;
Filipe Mananad906d492015-10-02 10:47:34 +01005222 }
5223 return 0;
5224}
5225
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -03005226/*
5227 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5228 * found, call send_set_xattr function to emit it.
5229 *
5230 * Return 0 if there isn't a capability, or when the capability was emitted
5231 * successfully, or < 0 if an error occurred.
5232 */
5233static int send_capabilities(struct send_ctx *sctx)
5234{
5235 struct fs_path *fspath = NULL;
5236 struct btrfs_path *path;
5237 struct btrfs_dir_item *di;
5238 struct extent_buffer *leaf;
5239 unsigned long data_ptr;
5240 char *buf = NULL;
5241 int buf_len;
5242 int ret = 0;
5243
5244 path = alloc_path_for_send();
5245 if (!path)
5246 return -ENOMEM;
5247
5248 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5249 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5250 if (!di) {
5251 /* There is no xattr for this inode */
5252 goto out;
5253 } else if (IS_ERR(di)) {
5254 ret = PTR_ERR(di);
5255 goto out;
5256 }
5257
5258 leaf = path->nodes[0];
5259 buf_len = btrfs_dir_data_len(leaf, di);
5260
5261 fspath = fs_path_alloc();
5262 buf = kmalloc(buf_len, GFP_KERNEL);
5263 if (!fspath || !buf) {
5264 ret = -ENOMEM;
5265 goto out;
5266 }
5267
5268 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5269 if (ret < 0)
5270 goto out;
5271
5272 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5273 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5274
5275 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5276 strlen(XATTR_NAME_CAPS), buf, buf_len);
5277out:
5278 kfree(buf);
5279 fs_path_free(fspath);
5280 btrfs_free_path(path);
5281 return ret;
5282}
5283
Filipe Mananad906d492015-10-02 10:47:34 +01005284static int clone_range(struct send_ctx *sctx,
5285 struct clone_root *clone_root,
5286 const u64 disk_byte,
5287 u64 data_offset,
5288 u64 offset,
5289 u64 len)
5290{
5291 struct btrfs_path *path;
5292 struct btrfs_key key;
5293 int ret;
Austin Kim431d3982019-09-03 12:30:19 +09005294 u64 clone_src_i_size = 0;
Filipe Mananad906d492015-10-02 10:47:34 +01005295
Filipe Manana72610b12017-08-10 22:54:51 +01005296 /*
5297 * Prevent cloning from a zero offset with a length matching the sector
5298 * size because in some scenarios this will make the receiver fail.
5299 *
5300 * For example, if in the source filesystem the extent at offset 0
5301 * has a length of sectorsize and it was written using direct IO, then
5302 * it can never be an inline extent (even if compression is enabled).
5303 * Then this extent can be cloned in the original filesystem to a non
5304 * zero file offset, but it may not be possible to clone in the
5305 * destination filesystem because it can be inlined due to compression
5306 * on the destination filesystem (as the receiver's write operations are
5307 * always done using buffered IO). The same happens when the original
5308 * filesystem does not have compression enabled but the destination
5309 * filesystem has.
5310 */
5311 if (clone_root->offset == 0 &&
5312 len == sctx->send_root->fs_info->sectorsize)
5313 return send_extent_data(sctx, offset, len);
5314
Filipe Mananad906d492015-10-02 10:47:34 +01005315 path = alloc_path_for_send();
5316 if (!path)
5317 return -ENOMEM;
5318
5319 /*
Robbie Ko040ee6122019-03-29 18:03:27 +08005320 * There are inodes that have extents that lie behind its i_size. Don't
5321 * accept clones from these extents.
5322 */
5323 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5324 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL);
5325 btrfs_release_path(path);
5326 if (ret < 0)
5327 goto out;
5328
5329 /*
Filipe Mananad906d492015-10-02 10:47:34 +01005330 * We can't send a clone operation for the entire range if we find
5331 * extent items in the respective range in the source file that
5332 * refer to different extents or if we find holes.
5333 * So check for that and do a mix of clone and regular write/copy
5334 * operations if needed.
5335 *
5336 * Example:
5337 *
5338 * mkfs.btrfs -f /dev/sda
5339 * mount /dev/sda /mnt
5340 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5341 * cp --reflink=always /mnt/foo /mnt/bar
5342 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5343 * btrfs subvolume snapshot -r /mnt /mnt/snap
5344 *
5345 * If when we send the snapshot and we are processing file bar (which
5346 * has a higher inode number than foo) we blindly send a clone operation
5347 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5348 * a file bar that matches the content of file foo - iow, doesn't match
5349 * the content from bar in the original filesystem.
5350 */
5351 key.objectid = clone_root->ino;
5352 key.type = BTRFS_EXTENT_DATA_KEY;
5353 key.offset = clone_root->offset;
5354 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5355 if (ret < 0)
5356 goto out;
5357 if (ret > 0 && path->slots[0] > 0) {
5358 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5359 if (key.objectid == clone_root->ino &&
5360 key.type == BTRFS_EXTENT_DATA_KEY)
5361 path->slots[0]--;
5362 }
5363
5364 while (true) {
5365 struct extent_buffer *leaf = path->nodes[0];
5366 int slot = path->slots[0];
5367 struct btrfs_file_extent_item *ei;
5368 u8 type;
5369 u64 ext_len;
5370 u64 clone_len;
Robbie Ko040ee6122019-03-29 18:03:27 +08005371 u64 clone_data_offset;
Filipe Mananad906d492015-10-02 10:47:34 +01005372
5373 if (slot >= btrfs_header_nritems(leaf)) {
5374 ret = btrfs_next_leaf(clone_root->root, path);
5375 if (ret < 0)
5376 goto out;
5377 else if (ret > 0)
5378 break;
5379 continue;
5380 }
5381
5382 btrfs_item_key_to_cpu(leaf, &key, slot);
5383
5384 /*
5385 * We might have an implicit trailing hole (NO_HOLES feature
5386 * enabled). We deal with it after leaving this loop.
5387 */
5388 if (key.objectid != clone_root->ino ||
5389 key.type != BTRFS_EXTENT_DATA_KEY)
5390 break;
5391
5392 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5393 type = btrfs_file_extent_type(leaf, ei);
5394 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005395 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005396 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01005397 } else {
5398 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5399 }
5400
5401 if (key.offset + ext_len <= clone_root->offset)
5402 goto next;
5403
5404 if (key.offset > clone_root->offset) {
5405 /* Implicit hole, NO_HOLES feature enabled. */
5406 u64 hole_len = key.offset - clone_root->offset;
5407
5408 if (hole_len > len)
5409 hole_len = len;
5410 ret = send_extent_data(sctx, offset, hole_len);
5411 if (ret < 0)
5412 goto out;
5413
5414 len -= hole_len;
5415 if (len == 0)
5416 break;
5417 offset += hole_len;
5418 clone_root->offset += hole_len;
5419 data_offset += hole_len;
5420 }
5421
5422 if (key.offset >= clone_root->offset + len)
5423 break;
5424
Robbie Ko040ee6122019-03-29 18:03:27 +08005425 if (key.offset >= clone_src_i_size)
5426 break;
5427
5428 if (key.offset + ext_len > clone_src_i_size)
5429 ext_len = clone_src_i_size - key.offset;
5430
5431 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5432 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5433 clone_root->offset = key.offset;
5434 if (clone_data_offset < data_offset &&
5435 clone_data_offset + ext_len > data_offset) {
5436 u64 extent_offset;
5437
5438 extent_offset = data_offset - clone_data_offset;
5439 ext_len -= extent_offset;
5440 clone_data_offset += extent_offset;
5441 clone_root->offset += extent_offset;
5442 }
5443 }
5444
Filipe Mananad906d492015-10-02 10:47:34 +01005445 clone_len = min_t(u64, ext_len, len);
5446
5447 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
Filipe Manana3c850b42019-05-20 09:57:00 +01005448 clone_data_offset == data_offset) {
5449 const u64 src_end = clone_root->offset + clone_len;
5450 const u64 sectorsize = SZ_64K;
5451
5452 /*
5453 * We can't clone the last block, when its size is not
5454 * sector size aligned, into the middle of a file. If we
5455 * do so, the receiver will get a failure (-EINVAL) when
5456 * trying to clone or will silently corrupt the data in
5457 * the destination file if it's on a kernel without the
5458 * fix introduced by commit ac765f83f1397646
5459 * ("Btrfs: fix data corruption due to cloning of eof
5460 * block).
5461 *
5462 * So issue a clone of the aligned down range plus a
5463 * regular write for the eof block, if we hit that case.
5464 *
5465 * Also, we use the maximum possible sector size, 64K,
5466 * because we don't know what's the sector size of the
5467 * filesystem that receives the stream, so we have to
5468 * assume the largest possible sector size.
5469 */
5470 if (src_end == clone_src_i_size &&
5471 !IS_ALIGNED(src_end, sectorsize) &&
5472 offset + clone_len < sctx->cur_inode_size) {
5473 u64 slen;
5474
5475 slen = ALIGN_DOWN(src_end - clone_root->offset,
5476 sectorsize);
5477 if (slen > 0) {
5478 ret = send_clone(sctx, offset, slen,
5479 clone_root);
5480 if (ret < 0)
5481 goto out;
5482 }
5483 ret = send_extent_data(sctx, offset + slen,
5484 clone_len - slen);
5485 } else {
5486 ret = send_clone(sctx, offset, clone_len,
5487 clone_root);
5488 }
5489 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005490 ret = send_extent_data(sctx, offset, clone_len);
Filipe Manana3c850b42019-05-20 09:57:00 +01005491 }
Filipe Mananad906d492015-10-02 10:47:34 +01005492
5493 if (ret < 0)
5494 goto out;
5495
5496 len -= clone_len;
5497 if (len == 0)
5498 break;
5499 offset += clone_len;
5500 clone_root->offset += clone_len;
Filipe Manana518837e2021-01-11 11:41:42 +00005501
5502 /*
5503 * If we are cloning from the file we are currently processing,
5504 * and using the send root as the clone root, we must stop once
5505 * the current clone offset reaches the current eof of the file
5506 * at the receiver, otherwise we would issue an invalid clone
5507 * operation (source range going beyond eof) and cause the
5508 * receiver to fail. So if we reach the current eof, bail out
5509 * and fallback to a regular write.
5510 */
5511 if (clone_root->root == sctx->send_root &&
5512 clone_root->ino == sctx->cur_ino &&
5513 clone_root->offset >= sctx->cur_inode_next_write_offset)
5514 break;
5515
Filipe Mananad906d492015-10-02 10:47:34 +01005516 data_offset += clone_len;
5517next:
5518 path->slots[0]++;
5519 }
5520
5521 if (len > 0)
5522 ret = send_extent_data(sctx, offset, len);
5523 else
5524 ret = 0;
5525out:
5526 btrfs_free_path(path);
5527 return ret;
5528}
5529
Alexander Block31db9f72012-07-25 23:19:24 +02005530static int send_write_or_clone(struct send_ctx *sctx,
5531 struct btrfs_path *path,
5532 struct btrfs_key *key,
5533 struct clone_root *clone_root)
5534{
5535 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005536 u64 offset = key->offset;
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005537 u64 end;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005538 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005539
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005540 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
5541 if (offset >= end)
5542 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005543
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005544 if (clone_root && IS_ALIGNED(end, bs)) {
5545 struct btrfs_file_extent_item *ei;
Filipe Mananad906d492015-10-02 10:47:34 +01005546 u64 disk_byte;
5547 u64 data_offset;
5548
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005549 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5550 struct btrfs_file_extent_item);
Filipe Mananad906d492015-10-02 10:47:34 +01005551 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5552 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5553 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005554 offset, end - offset);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005555 } else {
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005556 ret = send_extent_data(sctx, offset, end - offset);
Alexander Block31db9f72012-07-25 23:19:24 +02005557 }
Omar Sandovalc9a949a2020-08-21 00:39:53 -07005558 sctx->cur_inode_next_write_offset = end;
Alexander Block31db9f72012-07-25 23:19:24 +02005559 return ret;
5560}
5561
5562static int is_extent_unchanged(struct send_ctx *sctx,
5563 struct btrfs_path *left_path,
5564 struct btrfs_key *ekey)
5565{
5566 int ret = 0;
5567 struct btrfs_key key;
5568 struct btrfs_path *path = NULL;
5569 struct extent_buffer *eb;
5570 int slot;
5571 struct btrfs_key found_key;
5572 struct btrfs_file_extent_item *ei;
5573 u64 left_disknr;
5574 u64 right_disknr;
5575 u64 left_offset;
5576 u64 right_offset;
5577 u64 left_offset_fixed;
5578 u64 left_len;
5579 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005580 u64 left_gen;
5581 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005582 u8 left_type;
5583 u8 right_type;
5584
5585 path = alloc_path_for_send();
5586 if (!path)
5587 return -ENOMEM;
5588
5589 eb = left_path->nodes[0];
5590 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005591 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5592 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005593
5594 if (left_type != BTRFS_FILE_EXTENT_REG) {
5595 ret = 0;
5596 goto out;
5597 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005598 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5599 left_len = btrfs_file_extent_num_bytes(eb, ei);
5600 left_offset = btrfs_file_extent_offset(eb, ei);
5601 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005602
5603 /*
5604 * Following comments will refer to these graphics. L is the left
5605 * extents which we are checking at the moment. 1-8 are the right
5606 * extents that we iterate.
5607 *
5608 * |-----L-----|
5609 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5610 *
5611 * |-----L-----|
5612 * |--1--|-2b-|...(same as above)
5613 *
5614 * Alternative situation. Happens on files where extents got split.
5615 * |-----L-----|
5616 * |-----------7-----------|-6-|
5617 *
5618 * Alternative situation. Happens on files which got larger.
5619 * |-----L-----|
5620 * |-8-|
5621 * Nothing follows after 8.
5622 */
5623
5624 key.objectid = ekey->objectid;
5625 key.type = BTRFS_EXTENT_DATA_KEY;
5626 key.offset = ekey->offset;
5627 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5628 if (ret < 0)
5629 goto out;
5630 if (ret) {
5631 ret = 0;
5632 goto out;
5633 }
5634
5635 /*
5636 * Handle special case where the right side has no extents at all.
5637 */
5638 eb = path->nodes[0];
5639 slot = path->slots[0];
5640 btrfs_item_key_to_cpu(eb, &found_key, slot);
5641 if (found_key.objectid != key.objectid ||
5642 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005643 /* If we're a hole then just pretend nothing changed */
5644 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005645 goto out;
5646 }
5647
5648 /*
5649 * We're now on 2a, 2b or 7.
5650 */
5651 key = found_key;
5652 while (key.offset < ekey->offset + left_len) {
5653 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5654 right_type = btrfs_file_extent_type(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005655 if (right_type != BTRFS_FILE_EXTENT_REG &&
5656 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005657 ret = 0;
5658 goto out;
5659 }
5660
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005661 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005662 right_len = btrfs_file_extent_ram_bytes(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005663 right_len = PAGE_ALIGN(right_len);
5664 } else {
5665 right_len = btrfs_file_extent_num_bytes(eb, ei);
5666 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005667
Alexander Block31db9f72012-07-25 23:19:24 +02005668 /*
5669 * Are we at extent 8? If yes, we know the extent is changed.
5670 * This may only happen on the first iteration.
5671 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005672 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005673 /* If we're a hole just pretend nothing changed */
5674 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005675 goto out;
5676 }
5677
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005678 /*
5679 * We just wanted to see if when we have an inline extent, what
5680 * follows it is a regular extent (wanted to check the above
5681 * condition for inline extents too). This should normally not
5682 * happen but it's possible for example when we have an inline
5683 * compressed extent representing data with a size matching
5684 * the page size (currently the same as sector size).
5685 */
5686 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5687 ret = 0;
5688 goto out;
5689 }
5690
Filipe Manana24e52b12017-07-06 15:31:46 +01005691 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5692 right_offset = btrfs_file_extent_offset(eb, ei);
5693 right_gen = btrfs_file_extent_generation(eb, ei);
5694
Alexander Block31db9f72012-07-25 23:19:24 +02005695 left_offset_fixed = left_offset;
5696 if (key.offset < ekey->offset) {
5697 /* Fix the right offset for 2a and 7. */
5698 right_offset += ekey->offset - key.offset;
5699 } else {
5700 /* Fix the left offset for all behind 2a and 2b */
5701 left_offset_fixed += key.offset - ekey->offset;
5702 }
5703
5704 /*
5705 * Check if we have the same extent.
5706 */
Alexander Block39540962012-08-01 12:46:05 +02005707 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005708 left_offset_fixed != right_offset ||
5709 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005710 ret = 0;
5711 goto out;
5712 }
5713
5714 /*
5715 * Go to the next extent.
5716 */
5717 ret = btrfs_next_item(sctx->parent_root, path);
5718 if (ret < 0)
5719 goto out;
5720 if (!ret) {
5721 eb = path->nodes[0];
5722 slot = path->slots[0];
5723 btrfs_item_key_to_cpu(eb, &found_key, slot);
5724 }
5725 if (ret || found_key.objectid != key.objectid ||
5726 found_key.type != key.type) {
5727 key.offset += right_len;
5728 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005729 }
5730 if (found_key.offset != key.offset + right_len) {
5731 ret = 0;
5732 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005733 }
5734 key = found_key;
5735 }
5736
5737 /*
5738 * We're now behind the left extent (treat as unchanged) or at the end
5739 * of the right side (treat as changed).
5740 */
5741 if (key.offset >= ekey->offset + left_len)
5742 ret = 1;
5743 else
5744 ret = 0;
5745
5746
5747out:
5748 btrfs_free_path(path);
5749 return ret;
5750}
5751
Josef Bacik16e75492013-10-22 12:18:51 -04005752static int get_last_extent(struct send_ctx *sctx, u64 offset)
5753{
5754 struct btrfs_path *path;
5755 struct btrfs_root *root = sctx->send_root;
Josef Bacik16e75492013-10-22 12:18:51 -04005756 struct btrfs_key key;
Josef Bacik16e75492013-10-22 12:18:51 -04005757 int ret;
5758
5759 path = alloc_path_for_send();
5760 if (!path)
5761 return -ENOMEM;
5762
5763 sctx->cur_inode_last_extent = 0;
5764
5765 key.objectid = sctx->cur_ino;
5766 key.type = BTRFS_EXTENT_DATA_KEY;
5767 key.offset = offset;
5768 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5769 if (ret < 0)
5770 goto out;
5771 ret = 0;
5772 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5773 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5774 goto out;
5775
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00005776 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
Josef Bacik16e75492013-10-22 12:18:51 -04005777out:
5778 btrfs_free_path(path);
5779 return ret;
5780}
5781
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005782static int range_is_hole_in_parent(struct send_ctx *sctx,
5783 const u64 start,
5784 const u64 end)
5785{
5786 struct btrfs_path *path;
5787 struct btrfs_key key;
5788 struct btrfs_root *root = sctx->parent_root;
5789 u64 search_start = start;
5790 int ret;
5791
5792 path = alloc_path_for_send();
5793 if (!path)
5794 return -ENOMEM;
5795
5796 key.objectid = sctx->cur_ino;
5797 key.type = BTRFS_EXTENT_DATA_KEY;
5798 key.offset = search_start;
5799 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5800 if (ret < 0)
5801 goto out;
5802 if (ret > 0 && path->slots[0] > 0)
5803 path->slots[0]--;
5804
5805 while (search_start < end) {
5806 struct extent_buffer *leaf = path->nodes[0];
5807 int slot = path->slots[0];
5808 struct btrfs_file_extent_item *fi;
5809 u64 extent_end;
5810
5811 if (slot >= btrfs_header_nritems(leaf)) {
5812 ret = btrfs_next_leaf(root, path);
5813 if (ret < 0)
5814 goto out;
5815 else if (ret > 0)
5816 break;
5817 continue;
5818 }
5819
5820 btrfs_item_key_to_cpu(leaf, &key, slot);
5821 if (key.objectid < sctx->cur_ino ||
5822 key.type < BTRFS_EXTENT_DATA_KEY)
5823 goto next;
5824 if (key.objectid > sctx->cur_ino ||
5825 key.type > BTRFS_EXTENT_DATA_KEY ||
5826 key.offset >= end)
5827 break;
5828
5829 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00005830 extent_end = btrfs_file_extent_end(path);
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005831 if (extent_end <= start)
5832 goto next;
5833 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5834 search_start = extent_end;
5835 goto next;
5836 }
5837 ret = 0;
5838 goto out;
5839next:
5840 path->slots[0]++;
5841 }
5842 ret = 1;
5843out:
5844 btrfs_free_path(path);
5845 return ret;
5846}
5847
Josef Bacik16e75492013-10-22 12:18:51 -04005848static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5849 struct btrfs_key *key)
5850{
Josef Bacik16e75492013-10-22 12:18:51 -04005851 int ret = 0;
5852
5853 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5854 return 0;
5855
5856 if (sctx->cur_inode_last_extent == (u64)-1) {
5857 ret = get_last_extent(sctx, key->offset - 1);
5858 if (ret)
5859 return ret;
5860 }
5861
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005862 if (path->slots[0] == 0 &&
5863 sctx->cur_inode_last_extent < key->offset) {
5864 /*
5865 * We might have skipped entire leafs that contained only
5866 * file extent items for our current inode. These leafs have
5867 * a generation number smaller (older) than the one in the
5868 * current leaf and the leaf our last extent came from, and
5869 * are located between these 2 leafs.
5870 */
5871 ret = get_last_extent(sctx, key->offset - 1);
5872 if (ret)
5873 return ret;
5874 }
5875
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005876 if (sctx->cur_inode_last_extent < key->offset) {
5877 ret = range_is_hole_in_parent(sctx,
5878 sctx->cur_inode_last_extent,
5879 key->offset);
5880 if (ret < 0)
5881 return ret;
5882 else if (ret == 0)
5883 ret = send_hole(sctx, key->offset);
5884 else
5885 ret = 0;
5886 }
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00005887 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
Josef Bacik16e75492013-10-22 12:18:51 -04005888 return ret;
5889}
5890
Alexander Block31db9f72012-07-25 23:19:24 +02005891static int process_extent(struct send_ctx *sctx,
5892 struct btrfs_path *path,
5893 struct btrfs_key *key)
5894{
Alexander Block31db9f72012-07-25 23:19:24 +02005895 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005896 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005897
5898 if (S_ISLNK(sctx->cur_inode_mode))
5899 return 0;
5900
5901 if (sctx->parent_root && !sctx->cur_inode_new) {
5902 ret = is_extent_unchanged(sctx, path, key);
5903 if (ret < 0)
5904 goto out;
5905 if (ret) {
5906 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005907 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005908 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005909 } else {
5910 struct btrfs_file_extent_item *ei;
5911 u8 type;
5912
5913 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5914 struct btrfs_file_extent_item);
5915 type = btrfs_file_extent_type(path->nodes[0], ei);
5916 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5917 type == BTRFS_FILE_EXTENT_REG) {
5918 /*
5919 * The send spec does not have a prealloc command yet,
5920 * so just leave a hole for prealloc'ed extents until
5921 * we have enough commands queued up to justify rev'ing
5922 * the send spec.
5923 */
5924 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5925 ret = 0;
5926 goto out;
5927 }
5928
5929 /* Have a hole, just skip it. */
5930 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5931 ret = 0;
5932 goto out;
5933 }
5934 }
Alexander Block31db9f72012-07-25 23:19:24 +02005935 }
5936
5937 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5938 sctx->cur_inode_size, &found_clone);
5939 if (ret != -ENOENT && ret < 0)
5940 goto out;
5941
5942 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005943 if (ret)
5944 goto out;
5945out_hole:
5946 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005947out:
5948 return ret;
5949}
5950
5951static int process_all_extents(struct send_ctx *sctx)
5952{
5953 int ret;
5954 struct btrfs_root *root;
5955 struct btrfs_path *path;
5956 struct btrfs_key key;
5957 struct btrfs_key found_key;
5958 struct extent_buffer *eb;
5959 int slot;
5960
5961 root = sctx->send_root;
5962 path = alloc_path_for_send();
5963 if (!path)
5964 return -ENOMEM;
5965
5966 key.objectid = sctx->cmp_key->objectid;
5967 key.type = BTRFS_EXTENT_DATA_KEY;
5968 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005969 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5970 if (ret < 0)
5971 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005972
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005973 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005974 eb = path->nodes[0];
5975 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005976
5977 if (slot >= btrfs_header_nritems(eb)) {
5978 ret = btrfs_next_leaf(root, path);
5979 if (ret < 0) {
5980 goto out;
5981 } else if (ret > 0) {
5982 ret = 0;
5983 break;
5984 }
5985 continue;
5986 }
5987
Alexander Block31db9f72012-07-25 23:19:24 +02005988 btrfs_item_key_to_cpu(eb, &found_key, slot);
5989
5990 if (found_key.objectid != key.objectid ||
5991 found_key.type != key.type) {
5992 ret = 0;
5993 goto out;
5994 }
5995
5996 ret = process_extent(sctx, path, &found_key);
5997 if (ret < 0)
5998 goto out;
5999
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00006000 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02006001 }
6002
6003out:
6004 btrfs_free_path(path);
6005 return ret;
6006}
6007
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006008static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
6009 int *pending_move,
6010 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02006011{
6012 int ret = 0;
6013
6014 if (sctx->cur_ino == 0)
6015 goto out;
6016 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00006017 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02006018 goto out;
6019 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
6020 goto out;
6021
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006022 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02006023 if (ret < 0)
6024 goto out;
6025
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006026 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006027out:
6028 return ret;
6029}
6030
6031static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
6032{
6033 int ret = 0;
6034 u64 left_mode;
6035 u64 left_uid;
6036 u64 left_gid;
6037 u64 right_mode;
6038 u64 right_uid;
6039 u64 right_gid;
6040 int need_chmod = 0;
6041 int need_chown = 0;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006042 int need_truncate = 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006043 int pending_move = 0;
6044 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006045
Filipe Manana46b2f452018-07-24 11:54:04 +01006046 if (sctx->ignore_cur_inode)
6047 return 0;
6048
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006049 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
6050 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02006051 if (ret < 0)
6052 goto out;
6053
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006054 /*
6055 * We have processed the refs and thus need to advance send_progress.
6056 * Now, calls to get_cur_xxx will take the updated refs of the current
6057 * inode into account.
6058 *
6059 * On the other hand, if our current inode is a directory and couldn't
6060 * be moved/renamed because its parent was renamed/moved too and it has
6061 * a higher inode number, we can only move/rename our current inode
6062 * after we moved/renamed its parent. Therefore in this case operate on
6063 * the old path (pre move/rename) of our current inode, and the
6064 * move/rename will be performed later.
6065 */
6066 if (refs_processed && !pending_move)
6067 sctx->send_progress = sctx->cur_ino + 1;
6068
Alexander Block31db9f72012-07-25 23:19:24 +02006069 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6070 goto out;
6071 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6072 goto out;
6073
6074 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02006075 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02006076 if (ret < 0)
6077 goto out;
6078
Alex Lyakase2d044f2012-10-17 13:52:47 +00006079 if (!sctx->parent_root || sctx->cur_inode_new) {
6080 need_chown = 1;
6081 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02006082 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006083 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6084 need_truncate = 0;
Alex Lyakase2d044f2012-10-17 13:52:47 +00006085 } else {
Filipe Mananaffa7c422018-02-06 20:40:40 +00006086 u64 old_size;
6087
Alex Lyakase2d044f2012-10-17 13:52:47 +00006088 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
Filipe Mananaffa7c422018-02-06 20:40:40 +00006089 &old_size, NULL, &right_mode, &right_uid,
Alex Lyakase2d044f2012-10-17 13:52:47 +00006090 &right_gid, NULL);
6091 if (ret < 0)
6092 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006093
Alex Lyakase2d044f2012-10-17 13:52:47 +00006094 if (left_uid != right_uid || left_gid != right_gid)
6095 need_chown = 1;
6096 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6097 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006098 if ((old_size == sctx->cur_inode_size) ||
6099 (sctx->cur_inode_size > old_size &&
6100 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6101 need_truncate = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006102 }
6103
6104 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04006105 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01006106 if (sctx->cur_inode_last_extent == (u64)-1 ||
6107 sctx->cur_inode_last_extent <
6108 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04006109 ret = get_last_extent(sctx, (u64)-1);
6110 if (ret)
6111 goto out;
6112 }
6113 if (sctx->cur_inode_last_extent <
6114 sctx->cur_inode_size) {
6115 ret = send_hole(sctx, sctx->cur_inode_size);
6116 if (ret)
6117 goto out;
6118 }
6119 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00006120 if (need_truncate) {
6121 ret = send_truncate(sctx, sctx->cur_ino,
6122 sctx->cur_inode_gen,
6123 sctx->cur_inode_size);
6124 if (ret < 0)
6125 goto out;
6126 }
Alexander Block31db9f72012-07-25 23:19:24 +02006127 }
6128
6129 if (need_chown) {
6130 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6131 left_uid, left_gid);
6132 if (ret < 0)
6133 goto out;
6134 }
6135 if (need_chmod) {
6136 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6137 left_mode);
6138 if (ret < 0)
6139 goto out;
6140 }
6141
Marcos Paulo de Souza89efda52020-05-10 23:15:07 -03006142 ret = send_capabilities(sctx);
6143 if (ret < 0)
6144 goto out;
6145
Alexander Block31db9f72012-07-25 23:19:24 +02006146 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006147 * If other directory inodes depended on our current directory
6148 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02006149 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006150 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6151 ret = apply_children_dir_moves(sctx);
6152 if (ret)
6153 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00006154 /*
6155 * Need to send that every time, no matter if it actually
6156 * changed between the two trees as we have done changes to
6157 * the inode before. If our inode is a directory and it's
6158 * waiting to be moved/renamed, we will send its utimes when
6159 * it's moved/renamed, therefore we don't need to do it here.
6160 */
6161 sctx->send_progress = sctx->cur_ino + 1;
6162 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6163 if (ret < 0)
6164 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006165 }
6166
Alexander Block31db9f72012-07-25 23:19:24 +02006167out:
6168 return ret;
6169}
6170
Filipe Manana46b2f452018-07-24 11:54:04 +01006171struct parent_paths_ctx {
6172 struct list_head *refs;
6173 struct send_ctx *sctx;
6174};
6175
6176static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6177 void *ctx)
6178{
6179 struct parent_paths_ctx *ppctx = ctx;
6180
6181 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6182 ppctx->refs);
6183}
6184
6185/*
6186 * Issue unlink operations for all paths of the current inode found in the
6187 * parent snapshot.
6188 */
6189static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6190{
6191 LIST_HEAD(deleted_refs);
6192 struct btrfs_path *path;
6193 struct btrfs_key key;
6194 struct parent_paths_ctx ctx;
6195 int ret;
6196
6197 path = alloc_path_for_send();
6198 if (!path)
6199 return -ENOMEM;
6200
6201 key.objectid = sctx->cur_ino;
6202 key.type = BTRFS_INODE_REF_KEY;
6203 key.offset = 0;
6204 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
6205 if (ret < 0)
6206 goto out;
6207
6208 ctx.refs = &deleted_refs;
6209 ctx.sctx = sctx;
6210
6211 while (true) {
6212 struct extent_buffer *eb = path->nodes[0];
6213 int slot = path->slots[0];
6214
6215 if (slot >= btrfs_header_nritems(eb)) {
6216 ret = btrfs_next_leaf(sctx->parent_root, path);
6217 if (ret < 0)
6218 goto out;
6219 else if (ret > 0)
6220 break;
6221 continue;
6222 }
6223
6224 btrfs_item_key_to_cpu(eb, &key, slot);
6225 if (key.objectid != sctx->cur_ino)
6226 break;
6227 if (key.type != BTRFS_INODE_REF_KEY &&
6228 key.type != BTRFS_INODE_EXTREF_KEY)
6229 break;
6230
6231 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
6232 record_parent_ref, &ctx);
6233 if (ret < 0)
6234 goto out;
6235
6236 path->slots[0]++;
6237 }
6238
6239 while (!list_empty(&deleted_refs)) {
6240 struct recorded_ref *ref;
6241
6242 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6243 ret = send_unlink(sctx, ref->full_path);
6244 if (ret < 0)
6245 goto out;
6246 fs_path_free(ref->full_path);
6247 list_del(&ref->list);
6248 kfree(ref);
6249 }
6250 ret = 0;
6251out:
6252 btrfs_free_path(path);
6253 if (ret)
6254 __free_recorded_refs(&deleted_refs);
6255 return ret;
6256}
6257
Alexander Block31db9f72012-07-25 23:19:24 +02006258static int changed_inode(struct send_ctx *sctx,
6259 enum btrfs_compare_tree_result result)
6260{
6261 int ret = 0;
6262 struct btrfs_key *key = sctx->cmp_key;
6263 struct btrfs_inode_item *left_ii = NULL;
6264 struct btrfs_inode_item *right_ii = NULL;
6265 u64 left_gen = 0;
6266 u64 right_gen = 0;
6267
Alexander Block31db9f72012-07-25 23:19:24 +02006268 sctx->cur_ino = key->objectid;
6269 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006270 sctx->cur_inode_last_extent = (u64)-1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006271 sctx->cur_inode_next_write_offset = 0;
Filipe Manana46b2f452018-07-24 11:54:04 +01006272 sctx->ignore_cur_inode = false;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006273
6274 /*
6275 * Set send_progress to current inode. This will tell all get_cur_xxx
6276 * functions that the current inode's refs are not updated yet. Later,
6277 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6278 */
Alexander Block31db9f72012-07-25 23:19:24 +02006279 sctx->send_progress = sctx->cur_ino;
6280
6281 if (result == BTRFS_COMPARE_TREE_NEW ||
6282 result == BTRFS_COMPARE_TREE_CHANGED) {
6283 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6284 sctx->left_path->slots[0],
6285 struct btrfs_inode_item);
6286 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6287 left_ii);
6288 } else {
6289 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6290 sctx->right_path->slots[0],
6291 struct btrfs_inode_item);
6292 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6293 right_ii);
6294 }
6295 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6296 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6297 sctx->right_path->slots[0],
6298 struct btrfs_inode_item);
6299
6300 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6301 right_ii);
Alexander Block6d85ed052012-08-01 14:48:59 +02006302
6303 /*
6304 * The cur_ino = root dir case is special here. We can't treat
6305 * the inode as deleted+reused because it would generate a
6306 * stream that tries to delete/mkdir the root dir.
6307 */
6308 if (left_gen != right_gen &&
6309 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02006310 sctx->cur_inode_new_gen = 1;
6311 }
6312
Filipe Manana46b2f452018-07-24 11:54:04 +01006313 /*
6314 * Normally we do not find inodes with a link count of zero (orphans)
6315 * because the most common case is to create a snapshot and use it
6316 * for a send operation. However other less common use cases involve
6317 * using a subvolume and send it after turning it to RO mode just
6318 * after deleting all hard links of a file while holding an open
6319 * file descriptor against it or turning a RO snapshot into RW mode,
6320 * keep an open file descriptor against a file, delete it and then
6321 * turn the snapshot back to RO mode before using it for a send
6322 * operation. So if we find such cases, ignore the inode and all its
6323 * items completely if it's a new inode, or if it's a changed inode
6324 * make sure all its previous paths (from the parent snapshot) are all
6325 * unlinked and all other the inode items are ignored.
6326 */
6327 if (result == BTRFS_COMPARE_TREE_NEW ||
6328 result == BTRFS_COMPARE_TREE_CHANGED) {
6329 u32 nlinks;
6330
6331 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6332 if (nlinks == 0) {
6333 sctx->ignore_cur_inode = true;
6334 if (result == BTRFS_COMPARE_TREE_CHANGED)
6335 ret = btrfs_unlink_all_paths(sctx);
6336 goto out;
6337 }
6338 }
6339
Alexander Block31db9f72012-07-25 23:19:24 +02006340 if (result == BTRFS_COMPARE_TREE_NEW) {
6341 sctx->cur_inode_gen = left_gen;
6342 sctx->cur_inode_new = 1;
6343 sctx->cur_inode_deleted = 0;
6344 sctx->cur_inode_size = btrfs_inode_size(
6345 sctx->left_path->nodes[0], left_ii);
6346 sctx->cur_inode_mode = btrfs_inode_mode(
6347 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006348 sctx->cur_inode_rdev = btrfs_inode_rdev(
6349 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02006350 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02006351 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006352 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6353 sctx->cur_inode_gen = right_gen;
6354 sctx->cur_inode_new = 0;
6355 sctx->cur_inode_deleted = 1;
6356 sctx->cur_inode_size = btrfs_inode_size(
6357 sctx->right_path->nodes[0], right_ii);
6358 sctx->cur_inode_mode = btrfs_inode_mode(
6359 sctx->right_path->nodes[0], right_ii);
6360 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02006361 /*
6362 * We need to do some special handling in case the inode was
6363 * reported as changed with a changed generation number. This
6364 * means that the original inode was deleted and new inode
6365 * reused the same inum. So we have to treat the old inode as
6366 * deleted and the new one as new.
6367 */
Alexander Block31db9f72012-07-25 23:19:24 +02006368 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02006369 /*
6370 * First, process the inode as if it was deleted.
6371 */
Alexander Block31db9f72012-07-25 23:19:24 +02006372 sctx->cur_inode_gen = right_gen;
6373 sctx->cur_inode_new = 0;
6374 sctx->cur_inode_deleted = 1;
6375 sctx->cur_inode_size = btrfs_inode_size(
6376 sctx->right_path->nodes[0], right_ii);
6377 sctx->cur_inode_mode = btrfs_inode_mode(
6378 sctx->right_path->nodes[0], right_ii);
6379 ret = process_all_refs(sctx,
6380 BTRFS_COMPARE_TREE_DELETED);
6381 if (ret < 0)
6382 goto out;
6383
Alexander Block766702e2012-07-28 14:11:31 +02006384 /*
6385 * Now process the inode as if it was new.
6386 */
Alexander Block31db9f72012-07-25 23:19:24 +02006387 sctx->cur_inode_gen = left_gen;
6388 sctx->cur_inode_new = 1;
6389 sctx->cur_inode_deleted = 0;
6390 sctx->cur_inode_size = btrfs_inode_size(
6391 sctx->left_path->nodes[0], left_ii);
6392 sctx->cur_inode_mode = btrfs_inode_mode(
6393 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006394 sctx->cur_inode_rdev = btrfs_inode_rdev(
6395 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02006396 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006397 if (ret < 0)
6398 goto out;
6399
6400 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6401 if (ret < 0)
6402 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006403 /*
6404 * Advance send_progress now as we did not get into
6405 * process_recorded_refs_if_needed in the new_gen case.
6406 */
6407 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02006408
6409 /*
6410 * Now process all extents and xattrs of the inode as if
6411 * they were all new.
6412 */
Alexander Block31db9f72012-07-25 23:19:24 +02006413 ret = process_all_extents(sctx);
6414 if (ret < 0)
6415 goto out;
6416 ret = process_all_new_xattrs(sctx);
6417 if (ret < 0)
6418 goto out;
6419 } else {
6420 sctx->cur_inode_gen = left_gen;
6421 sctx->cur_inode_new = 0;
6422 sctx->cur_inode_new_gen = 0;
6423 sctx->cur_inode_deleted = 0;
6424 sctx->cur_inode_size = btrfs_inode_size(
6425 sctx->left_path->nodes[0], left_ii);
6426 sctx->cur_inode_mode = btrfs_inode_mode(
6427 sctx->left_path->nodes[0], left_ii);
6428 }
6429 }
6430
6431out:
6432 return ret;
6433}
6434
Alexander Block766702e2012-07-28 14:11:31 +02006435/*
6436 * We have to process new refs before deleted refs, but compare_trees gives us
6437 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6438 * first and later process them in process_recorded_refs.
6439 * For the cur_inode_new_gen case, we skip recording completely because
6440 * changed_inode did already initiate processing of refs. The reason for this is
6441 * that in this case, compare_tree actually compares the refs of 2 different
6442 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6443 * refs of the right tree as deleted and all refs of the left tree as new.
6444 */
Alexander Block31db9f72012-07-25 23:19:24 +02006445static int changed_ref(struct send_ctx *sctx,
6446 enum btrfs_compare_tree_result result)
6447{
6448 int ret = 0;
6449
Filipe Manana95155582016-08-01 01:50:37 +01006450 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6451 inconsistent_snapshot_error(sctx, result, "reference");
6452 return -EIO;
6453 }
Alexander Block31db9f72012-07-25 23:19:24 +02006454
6455 if (!sctx->cur_inode_new_gen &&
6456 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6457 if (result == BTRFS_COMPARE_TREE_NEW)
6458 ret = record_new_ref(sctx);
6459 else if (result == BTRFS_COMPARE_TREE_DELETED)
6460 ret = record_deleted_ref(sctx);
6461 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6462 ret = record_changed_ref(sctx);
6463 }
6464
6465 return ret;
6466}
6467
Alexander Block766702e2012-07-28 14:11:31 +02006468/*
6469 * Process new/deleted/changed xattrs. We skip processing in the
6470 * cur_inode_new_gen case because changed_inode did already initiate processing
6471 * of xattrs. The reason is the same as in changed_ref
6472 */
Alexander Block31db9f72012-07-25 23:19:24 +02006473static int changed_xattr(struct send_ctx *sctx,
6474 enum btrfs_compare_tree_result result)
6475{
6476 int ret = 0;
6477
Filipe Manana95155582016-08-01 01:50:37 +01006478 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6479 inconsistent_snapshot_error(sctx, result, "xattr");
6480 return -EIO;
6481 }
Alexander Block31db9f72012-07-25 23:19:24 +02006482
6483 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6484 if (result == BTRFS_COMPARE_TREE_NEW)
6485 ret = process_new_xattr(sctx);
6486 else if (result == BTRFS_COMPARE_TREE_DELETED)
6487 ret = process_deleted_xattr(sctx);
6488 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6489 ret = process_changed_xattr(sctx);
6490 }
6491
6492 return ret;
6493}
6494
Alexander Block766702e2012-07-28 14:11:31 +02006495/*
6496 * Process new/deleted/changed extents. We skip processing in the
6497 * cur_inode_new_gen case because changed_inode did already initiate processing
6498 * of extents. The reason is the same as in changed_ref
6499 */
Alexander Block31db9f72012-07-25 23:19:24 +02006500static int changed_extent(struct send_ctx *sctx,
6501 enum btrfs_compare_tree_result result)
6502{
6503 int ret = 0;
6504
Filipe Mananab4f9a1a2019-07-17 13:23:39 +01006505 /*
6506 * We have found an extent item that changed without the inode item
6507 * having changed. This can happen either after relocation (where the
6508 * disk_bytenr of an extent item is replaced at
6509 * relocation.c:replace_file_extents()) or after deduplication into a
6510 * file in both the parent and send snapshots (where an extent item can
6511 * get modified or replaced with a new one). Note that deduplication
6512 * updates the inode item, but it only changes the iversion (sequence
6513 * field in the inode item) of the inode, so if a file is deduplicated
6514 * the same amount of times in both the parent and send snapshots, its
David Sterba1a9fd412021-05-21 17:42:23 +02006515 * iversion becomes the same in both snapshots, whence the inode item is
Filipe Mananab4f9a1a2019-07-17 13:23:39 +01006516 * the same on both snapshots.
6517 */
6518 if (sctx->cur_ino != sctx->cmp_key->objectid)
6519 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006520
6521 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6522 if (result != BTRFS_COMPARE_TREE_DELETED)
6523 ret = process_extent(sctx, sctx->left_path,
6524 sctx->cmp_key);
6525 }
6526
6527 return ret;
6528}
6529
Josef Bacikba5e8f22013-08-16 16:52:55 -04006530static int dir_changed(struct send_ctx *sctx, u64 dir)
6531{
6532 u64 orig_gen, new_gen;
6533 int ret;
6534
6535 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6536 NULL, NULL);
6537 if (ret)
6538 return ret;
6539
6540 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6541 NULL, NULL, NULL);
6542 if (ret)
6543 return ret;
6544
6545 return (orig_gen != new_gen) ? 1 : 0;
6546}
6547
6548static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6549 struct btrfs_key *key)
6550{
6551 struct btrfs_inode_extref *extref;
6552 struct extent_buffer *leaf;
6553 u64 dirid = 0, last_dirid = 0;
6554 unsigned long ptr;
6555 u32 item_size;
6556 u32 cur_offset = 0;
6557 int ref_name_len;
6558 int ret = 0;
6559
6560 /* Easy case, just check this one dirid */
6561 if (key->type == BTRFS_INODE_REF_KEY) {
6562 dirid = key->offset;
6563
6564 ret = dir_changed(sctx, dirid);
6565 goto out;
6566 }
6567
6568 leaf = path->nodes[0];
6569 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6570 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6571 while (cur_offset < item_size) {
6572 extref = (struct btrfs_inode_extref *)(ptr +
6573 cur_offset);
6574 dirid = btrfs_inode_extref_parent(leaf, extref);
6575 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6576 cur_offset += ref_name_len + sizeof(*extref);
6577 if (dirid == last_dirid)
6578 continue;
6579 ret = dir_changed(sctx, dirid);
6580 if (ret)
6581 break;
6582 last_dirid = dirid;
6583 }
6584out:
6585 return ret;
6586}
6587
Alexander Block766702e2012-07-28 14:11:31 +02006588/*
6589 * Updates compare related fields in sctx and simply forwards to the actual
6590 * changed_xxx functions.
6591 */
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006592static int changed_cb(struct btrfs_path *left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02006593 struct btrfs_path *right_path,
6594 struct btrfs_key *key,
6595 enum btrfs_compare_tree_result result,
Roman Anasal88980382021-01-25 20:43:25 +01006596 struct send_ctx *sctx)
Alexander Block31db9f72012-07-25 23:19:24 +02006597{
6598 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006599
Josef Bacikba5e8f22013-08-16 16:52:55 -04006600 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04006601 if (key->type == BTRFS_INODE_REF_KEY ||
6602 key->type == BTRFS_INODE_EXTREF_KEY) {
6603 ret = compare_refs(sctx, left_path, key);
6604 if (!ret)
6605 return 0;
6606 if (ret < 0)
6607 return ret;
6608 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6609 return maybe_send_hole(sctx, left_path, key);
6610 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04006611 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006612 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04006613 result = BTRFS_COMPARE_TREE_CHANGED;
6614 ret = 0;
6615 }
6616
Alexander Block31db9f72012-07-25 23:19:24 +02006617 sctx->left_path = left_path;
6618 sctx->right_path = right_path;
6619 sctx->cmp_key = key;
6620
6621 ret = finish_inode_if_needed(sctx, 0);
6622 if (ret < 0)
6623 goto out;
6624
Alexander Block2981e222012-08-01 14:47:03 +02006625 /* Ignore non-FS objects */
6626 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6627 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6628 goto out;
6629
Filipe Manana46b2f452018-07-24 11:54:04 +01006630 if (key->type == BTRFS_INODE_ITEM_KEY) {
Alexander Block31db9f72012-07-25 23:19:24 +02006631 ret = changed_inode(sctx, result);
Filipe Manana46b2f452018-07-24 11:54:04 +01006632 } else if (!sctx->ignore_cur_inode) {
6633 if (key->type == BTRFS_INODE_REF_KEY ||
6634 key->type == BTRFS_INODE_EXTREF_KEY)
6635 ret = changed_ref(sctx, result);
6636 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6637 ret = changed_xattr(sctx, result);
6638 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6639 ret = changed_extent(sctx, result);
6640 }
Alexander Block31db9f72012-07-25 23:19:24 +02006641
6642out:
6643 return ret;
6644}
6645
6646static int full_send_tree(struct send_ctx *sctx)
6647{
6648 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006649 struct btrfs_root *send_root = sctx->send_root;
6650 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006651 struct btrfs_path *path;
6652 struct extent_buffer *eb;
6653 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006654
6655 path = alloc_path_for_send();
6656 if (!path)
6657 return -ENOMEM;
Filipe Mananaace75062021-03-31 11:56:21 +01006658 path->reada = READA_FORWARD_ALWAYS;
Alexander Block31db9f72012-07-25 23:19:24 +02006659
Alexander Block31db9f72012-07-25 23:19:24 +02006660 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6661 key.type = BTRFS_INODE_ITEM_KEY;
6662 key.offset = 0;
6663
Alexander Block31db9f72012-07-25 23:19:24 +02006664 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6665 if (ret < 0)
6666 goto out;
6667 if (ret)
6668 goto out_finish;
6669
6670 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006671 eb = path->nodes[0];
6672 slot = path->slots[0];
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006673 btrfs_item_key_to_cpu(eb, &key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02006674
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006675 ret = changed_cb(path, NULL, &key,
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006676 BTRFS_COMPARE_TREE_NEW, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006677 if (ret < 0)
6678 goto out;
6679
Alexander Block31db9f72012-07-25 23:19:24 +02006680 ret = btrfs_next_item(send_root, path);
6681 if (ret < 0)
6682 goto out;
6683 if (ret) {
6684 ret = 0;
6685 break;
6686 }
6687 }
6688
6689out_finish:
6690 ret = finish_inode_if_needed(sctx, 1);
6691
6692out:
6693 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006694 return ret;
6695}
6696
Filipe Manana2ce73c62021-03-01 09:26:43 +00006697static int tree_move_down(struct btrfs_path *path, int *level, u64 reada_min_gen)
David Sterba18d0f5c2019-08-21 19:12:59 +02006698{
6699 struct extent_buffer *eb;
Filipe Manana2ce73c62021-03-01 09:26:43 +00006700 struct extent_buffer *parent = path->nodes[*level];
6701 int slot = path->slots[*level];
6702 const int nritems = btrfs_header_nritems(parent);
6703 u64 reada_max;
6704 u64 reada_done = 0;
David Sterba18d0f5c2019-08-21 19:12:59 +02006705
6706 BUG_ON(*level == 0);
Filipe Manana2ce73c62021-03-01 09:26:43 +00006707 eb = btrfs_read_node_slot(parent, slot);
David Sterba18d0f5c2019-08-21 19:12:59 +02006708 if (IS_ERR(eb))
6709 return PTR_ERR(eb);
6710
Filipe Manana2ce73c62021-03-01 09:26:43 +00006711 /*
6712 * Trigger readahead for the next leaves we will process, so that it is
6713 * very likely that when we need them they are already in memory and we
6714 * will not block on disk IO. For nodes we only do readahead for one,
6715 * since the time window between processing nodes is typically larger.
6716 */
6717 reada_max = (*level == 1 ? SZ_128K : eb->fs_info->nodesize);
6718
6719 for (slot++; slot < nritems && reada_done < reada_max; slot++) {
6720 if (btrfs_node_ptr_generation(parent, slot) > reada_min_gen) {
6721 btrfs_readahead_node_child(parent, slot);
6722 reada_done += eb->fs_info->nodesize;
6723 }
6724 }
6725
David Sterba18d0f5c2019-08-21 19:12:59 +02006726 path->nodes[*level - 1] = eb;
6727 path->slots[*level - 1] = 0;
6728 (*level)--;
6729 return 0;
6730}
6731
6732static int tree_move_next_or_upnext(struct btrfs_path *path,
6733 int *level, int root_level)
6734{
6735 int ret = 0;
6736 int nritems;
6737 nritems = btrfs_header_nritems(path->nodes[*level]);
6738
6739 path->slots[*level]++;
6740
6741 while (path->slots[*level] >= nritems) {
6742 if (*level == root_level)
6743 return -1;
6744
6745 /* move upnext */
6746 path->slots[*level] = 0;
6747 free_extent_buffer(path->nodes[*level]);
6748 path->nodes[*level] = NULL;
6749 (*level)++;
6750 path->slots[*level]++;
6751
6752 nritems = btrfs_header_nritems(path->nodes[*level]);
6753 ret = 1;
6754 }
6755 return ret;
6756}
6757
6758/*
6759 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
6760 * or down.
6761 */
6762static int tree_advance(struct btrfs_path *path,
6763 int *level, int root_level,
6764 int allow_down,
Filipe Manana2ce73c62021-03-01 09:26:43 +00006765 struct btrfs_key *key,
6766 u64 reada_min_gen)
David Sterba18d0f5c2019-08-21 19:12:59 +02006767{
6768 int ret;
6769
6770 if (*level == 0 || !allow_down) {
6771 ret = tree_move_next_or_upnext(path, level, root_level);
6772 } else {
Filipe Manana2ce73c62021-03-01 09:26:43 +00006773 ret = tree_move_down(path, level, reada_min_gen);
David Sterba18d0f5c2019-08-21 19:12:59 +02006774 }
6775 if (ret >= 0) {
6776 if (*level == 0)
6777 btrfs_item_key_to_cpu(path->nodes[*level], key,
6778 path->slots[*level]);
6779 else
6780 btrfs_node_key_to_cpu(path->nodes[*level], key,
6781 path->slots[*level]);
6782 }
6783 return ret;
6784}
6785
6786static int tree_compare_item(struct btrfs_path *left_path,
6787 struct btrfs_path *right_path,
6788 char *tmp_buf)
6789{
6790 int cmp;
6791 int len1, len2;
6792 unsigned long off1, off2;
6793
6794 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
6795 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
6796 if (len1 != len2)
6797 return 1;
6798
6799 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
6800 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
6801 right_path->slots[0]);
6802
6803 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
6804
6805 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
6806 if (cmp)
6807 return 1;
6808 return 0;
6809}
6810
6811/*
6812 * This function compares two trees and calls the provided callback for
6813 * every changed/new/deleted item it finds.
6814 * If shared tree blocks are encountered, whole subtrees are skipped, making
6815 * the compare pretty fast on snapshotted subvolumes.
6816 *
6817 * This currently works on commit roots only. As commit roots are read only,
6818 * we don't do any locking. The commit roots are protected with transactions.
6819 * Transactions are ended and rejoined when a commit is tried in between.
6820 *
6821 * This function checks for modifications done to the trees while comparing.
6822 * If it detects a change, it aborts immediately.
6823 */
6824static int btrfs_compare_trees(struct btrfs_root *left_root,
Roman Anasal88980382021-01-25 20:43:25 +01006825 struct btrfs_root *right_root, struct send_ctx *sctx)
David Sterba18d0f5c2019-08-21 19:12:59 +02006826{
6827 struct btrfs_fs_info *fs_info = left_root->fs_info;
6828 int ret;
6829 int cmp;
6830 struct btrfs_path *left_path = NULL;
6831 struct btrfs_path *right_path = NULL;
6832 struct btrfs_key left_key;
6833 struct btrfs_key right_key;
6834 char *tmp_buf = NULL;
6835 int left_root_level;
6836 int right_root_level;
6837 int left_level;
6838 int right_level;
6839 int left_end_reached;
6840 int right_end_reached;
6841 int advance_left;
6842 int advance_right;
6843 u64 left_blockptr;
6844 u64 right_blockptr;
6845 u64 left_gen;
6846 u64 right_gen;
Filipe Manana2ce73c62021-03-01 09:26:43 +00006847 u64 reada_min_gen;
David Sterba18d0f5c2019-08-21 19:12:59 +02006848
6849 left_path = btrfs_alloc_path();
6850 if (!left_path) {
6851 ret = -ENOMEM;
6852 goto out;
6853 }
6854 right_path = btrfs_alloc_path();
6855 if (!right_path) {
6856 ret = -ENOMEM;
6857 goto out;
6858 }
6859
6860 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
6861 if (!tmp_buf) {
6862 ret = -ENOMEM;
6863 goto out;
6864 }
6865
6866 left_path->search_commit_root = 1;
6867 left_path->skip_locking = 1;
6868 right_path->search_commit_root = 1;
6869 right_path->skip_locking = 1;
6870
6871 /*
6872 * Strategy: Go to the first items of both trees. Then do
6873 *
6874 * If both trees are at level 0
6875 * Compare keys of current items
6876 * If left < right treat left item as new, advance left tree
6877 * and repeat
6878 * If left > right treat right item as deleted, advance right tree
6879 * and repeat
6880 * If left == right do deep compare of items, treat as changed if
6881 * needed, advance both trees and repeat
6882 * If both trees are at the same level but not at level 0
6883 * Compare keys of current nodes/leafs
6884 * If left < right advance left tree and repeat
6885 * If left > right advance right tree and repeat
6886 * If left == right compare blockptrs of the next nodes/leafs
6887 * If they match advance both trees but stay at the same level
6888 * and repeat
6889 * If they don't match advance both trees while allowing to go
6890 * deeper and repeat
6891 * If tree levels are different
6892 * Advance the tree that needs it and repeat
6893 *
6894 * Advancing a tree means:
6895 * If we are at level 0, try to go to the next slot. If that's not
6896 * possible, go one level up and repeat. Stop when we found a level
6897 * where we could go to the next slot. We may at this point be on a
6898 * node or a leaf.
6899 *
6900 * If we are not at level 0 and not on shared tree blocks, go one
6901 * level deeper.
6902 *
6903 * If we are not at level 0 and on shared tree blocks, go one slot to
6904 * the right if possible or go up and right.
6905 */
6906
6907 down_read(&fs_info->commit_root_sem);
6908 left_level = btrfs_header_level(left_root->commit_root);
6909 left_root_level = left_level;
6910 left_path->nodes[left_level] =
6911 btrfs_clone_extent_buffer(left_root->commit_root);
6912 if (!left_path->nodes[left_level]) {
6913 up_read(&fs_info->commit_root_sem);
6914 ret = -ENOMEM;
6915 goto out;
6916 }
6917
6918 right_level = btrfs_header_level(right_root->commit_root);
6919 right_root_level = right_level;
6920 right_path->nodes[right_level] =
6921 btrfs_clone_extent_buffer(right_root->commit_root);
6922 if (!right_path->nodes[right_level]) {
6923 up_read(&fs_info->commit_root_sem);
6924 ret = -ENOMEM;
6925 goto out;
6926 }
Filipe Manana2ce73c62021-03-01 09:26:43 +00006927 /*
6928 * Our right root is the parent root, while the left root is the "send"
6929 * root. We know that all new nodes/leaves in the left root must have
6930 * a generation greater than the right root's generation, so we trigger
6931 * readahead for those nodes and leaves of the left root, as we know we
6932 * will need to read them at some point.
6933 */
6934 reada_min_gen = btrfs_header_generation(right_root->commit_root);
David Sterba18d0f5c2019-08-21 19:12:59 +02006935 up_read(&fs_info->commit_root_sem);
6936
6937 if (left_level == 0)
6938 btrfs_item_key_to_cpu(left_path->nodes[left_level],
6939 &left_key, left_path->slots[left_level]);
6940 else
6941 btrfs_node_key_to_cpu(left_path->nodes[left_level],
6942 &left_key, left_path->slots[left_level]);
6943 if (right_level == 0)
6944 btrfs_item_key_to_cpu(right_path->nodes[right_level],
6945 &right_key, right_path->slots[right_level]);
6946 else
6947 btrfs_node_key_to_cpu(right_path->nodes[right_level],
6948 &right_key, right_path->slots[right_level]);
6949
6950 left_end_reached = right_end_reached = 0;
6951 advance_left = advance_right = 0;
6952
6953 while (1) {
Nikolay Borisov6af112b2019-09-04 19:33:58 +03006954 cond_resched();
David Sterba18d0f5c2019-08-21 19:12:59 +02006955 if (advance_left && !left_end_reached) {
6956 ret = tree_advance(left_path, &left_level,
6957 left_root_level,
6958 advance_left != ADVANCE_ONLY_NEXT,
Filipe Manana2ce73c62021-03-01 09:26:43 +00006959 &left_key, reada_min_gen);
David Sterba18d0f5c2019-08-21 19:12:59 +02006960 if (ret == -1)
6961 left_end_reached = ADVANCE;
6962 else if (ret < 0)
6963 goto out;
6964 advance_left = 0;
6965 }
6966 if (advance_right && !right_end_reached) {
6967 ret = tree_advance(right_path, &right_level,
6968 right_root_level,
6969 advance_right != ADVANCE_ONLY_NEXT,
Filipe Manana2ce73c62021-03-01 09:26:43 +00006970 &right_key, reada_min_gen);
David Sterba18d0f5c2019-08-21 19:12:59 +02006971 if (ret == -1)
6972 right_end_reached = ADVANCE;
6973 else if (ret < 0)
6974 goto out;
6975 advance_right = 0;
6976 }
6977
6978 if (left_end_reached && right_end_reached) {
6979 ret = 0;
6980 goto out;
6981 } else if (left_end_reached) {
6982 if (right_level == 0) {
6983 ret = changed_cb(left_path, right_path,
6984 &right_key,
6985 BTRFS_COMPARE_TREE_DELETED,
Roman Anasal88980382021-01-25 20:43:25 +01006986 sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02006987 if (ret < 0)
6988 goto out;
6989 }
6990 advance_right = ADVANCE;
6991 continue;
6992 } else if (right_end_reached) {
6993 if (left_level == 0) {
6994 ret = changed_cb(left_path, right_path,
6995 &left_key,
6996 BTRFS_COMPARE_TREE_NEW,
Roman Anasal88980382021-01-25 20:43:25 +01006997 sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02006998 if (ret < 0)
6999 goto out;
7000 }
7001 advance_left = ADVANCE;
7002 continue;
7003 }
7004
7005 if (left_level == 0 && right_level == 0) {
7006 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7007 if (cmp < 0) {
7008 ret = changed_cb(left_path, right_path,
7009 &left_key,
7010 BTRFS_COMPARE_TREE_NEW,
Roman Anasal88980382021-01-25 20:43:25 +01007011 sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02007012 if (ret < 0)
7013 goto out;
7014 advance_left = ADVANCE;
7015 } else if (cmp > 0) {
7016 ret = changed_cb(left_path, right_path,
7017 &right_key,
7018 BTRFS_COMPARE_TREE_DELETED,
Roman Anasal88980382021-01-25 20:43:25 +01007019 sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02007020 if (ret < 0)
7021 goto out;
7022 advance_right = ADVANCE;
7023 } else {
7024 enum btrfs_compare_tree_result result;
7025
7026 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
7027 ret = tree_compare_item(left_path, right_path,
7028 tmp_buf);
7029 if (ret)
7030 result = BTRFS_COMPARE_TREE_CHANGED;
7031 else
7032 result = BTRFS_COMPARE_TREE_SAME;
7033 ret = changed_cb(left_path, right_path,
Roman Anasal88980382021-01-25 20:43:25 +01007034 &left_key, result, sctx);
David Sterba18d0f5c2019-08-21 19:12:59 +02007035 if (ret < 0)
7036 goto out;
7037 advance_left = ADVANCE;
7038 advance_right = ADVANCE;
7039 }
7040 } else if (left_level == right_level) {
7041 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7042 if (cmp < 0) {
7043 advance_left = ADVANCE;
7044 } else if (cmp > 0) {
7045 advance_right = ADVANCE;
7046 } else {
7047 left_blockptr = btrfs_node_blockptr(
7048 left_path->nodes[left_level],
7049 left_path->slots[left_level]);
7050 right_blockptr = btrfs_node_blockptr(
7051 right_path->nodes[right_level],
7052 right_path->slots[right_level]);
7053 left_gen = btrfs_node_ptr_generation(
7054 left_path->nodes[left_level],
7055 left_path->slots[left_level]);
7056 right_gen = btrfs_node_ptr_generation(
7057 right_path->nodes[right_level],
7058 right_path->slots[right_level]);
7059 if (left_blockptr == right_blockptr &&
7060 left_gen == right_gen) {
7061 /*
7062 * As we're on a shared block, don't
7063 * allow to go deeper.
7064 */
7065 advance_left = ADVANCE_ONLY_NEXT;
7066 advance_right = ADVANCE_ONLY_NEXT;
7067 } else {
7068 advance_left = ADVANCE;
7069 advance_right = ADVANCE;
7070 }
7071 }
7072 } else if (left_level < right_level) {
7073 advance_right = ADVANCE;
7074 } else {
7075 advance_left = ADVANCE;
7076 }
7077 }
7078
7079out:
7080 btrfs_free_path(left_path);
7081 btrfs_free_path(right_path);
7082 kvfree(tmp_buf);
7083 return ret;
7084}
7085
Alexander Block31db9f72012-07-25 23:19:24 +02007086static int send_subvol(struct send_ctx *sctx)
7087{
7088 int ret;
7089
Stefan Behrensc2c71322013-04-10 17:10:52 +00007090 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
7091 ret = send_header(sctx);
7092 if (ret < 0)
7093 goto out;
7094 }
Alexander Block31db9f72012-07-25 23:19:24 +02007095
7096 ret = send_subvol_begin(sctx);
7097 if (ret < 0)
7098 goto out;
7099
7100 if (sctx->parent_root) {
David Sterba1b51d6f2020-08-17 12:16:57 +02007101 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02007102 if (ret < 0)
7103 goto out;
7104 ret = finish_inode_if_needed(sctx, 1);
7105 if (ret < 0)
7106 goto out;
7107 } else {
7108 ret = full_send_tree(sctx);
7109 if (ret < 0)
7110 goto out;
7111 }
7112
7113out:
Alexander Block31db9f72012-07-25 23:19:24 +02007114 free_recorded_refs(sctx);
7115 return ret;
7116}
7117
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007118/*
7119 * If orphan cleanup did remove any orphans from a root, it means the tree
7120 * was modified and therefore the commit root is not the same as the current
7121 * root anymore. This is a problem, because send uses the commit root and
7122 * therefore can see inode items that don't exist in the current root anymore,
7123 * and for example make calls to btrfs_iget, which will do tree lookups based
7124 * on the current root and not on the commit root. Those lookups will fail,
7125 * returning a -ESTALE error, and making send fail with that error. So make
7126 * sure a send does not see any orphans we have just removed, and that it will
7127 * see the same inodes regardless of whether a transaction commit happened
7128 * before it started (meaning that the commit root will be the same as the
7129 * current root) or not.
7130 */
7131static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
7132{
7133 int i;
7134 struct btrfs_trans_handle *trans = NULL;
7135
7136again:
7137 if (sctx->parent_root &&
7138 sctx->parent_root->node != sctx->parent_root->commit_root)
7139 goto commit_trans;
7140
7141 for (i = 0; i < sctx->clone_roots_cnt; i++)
7142 if (sctx->clone_roots[i].root->node !=
7143 sctx->clone_roots[i].root->commit_root)
7144 goto commit_trans;
7145
7146 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007147 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007148
7149 return 0;
7150
7151commit_trans:
7152 /* Use any root, all fs roots will get their commit roots updated. */
7153 if (!trans) {
7154 trans = btrfs_join_transaction(sctx->send_root);
7155 if (IS_ERR(trans))
7156 return PTR_ERR(trans);
7157 goto again;
7158 }
7159
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007160 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007161}
7162
Filipe Manana9f89d5d2019-04-15 09:29:36 +01007163/*
7164 * Make sure any existing dellaloc is flushed for any root used by a send
7165 * operation so that we do not miss any data and we do not race with writeback
7166 * finishing and changing a tree while send is using the tree. This could
7167 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
7168 * a send operation then uses the subvolume.
7169 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
7170 */
7171static int flush_delalloc_roots(struct send_ctx *sctx)
7172{
7173 struct btrfs_root *root = sctx->parent_root;
7174 int ret;
7175 int i;
7176
7177 if (root) {
Filipe Mananaf9baa502021-04-22 12:08:05 +01007178 ret = btrfs_start_delalloc_snapshot(root, false);
Filipe Manana9f89d5d2019-04-15 09:29:36 +01007179 if (ret)
7180 return ret;
7181 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7182 }
7183
7184 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7185 root = sctx->clone_roots[i].root;
Filipe Mananaf9baa502021-04-22 12:08:05 +01007186 ret = btrfs_start_delalloc_snapshot(root, false);
Filipe Manana9f89d5d2019-04-15 09:29:36 +01007187 if (ret)
7188 return ret;
7189 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7190 }
7191
7192 return 0;
7193}
7194
David Sterba66ef7d62013-12-17 15:07:20 +01007195static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
7196{
7197 spin_lock(&root->root_item_lock);
7198 root->send_in_progress--;
7199 /*
7200 * Not much left to do, we don't know why it's unbalanced and
7201 * can't blindly reset it to 0.
7202 */
7203 if (root->send_in_progress < 0)
7204 btrfs_err(root->fs_info,
Colin Ian Kingf5686e32018-05-04 12:11:12 +01007205 "send_in_progress unbalanced %d root %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007206 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01007207 spin_unlock(&root->root_item_lock);
7208}
7209
Filipe Manana62d54f32019-04-22 16:43:42 +01007210static void dedupe_in_progress_warn(const struct btrfs_root *root)
7211{
7212 btrfs_warn_rl(root->fs_info,
7213"cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
7214 root->root_key.objectid, root->dedupe_in_progress);
7215}
7216
Josef Bacik2351f432017-09-27 10:43:13 -04007217long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
Alexander Block31db9f72012-07-25 23:19:24 +02007218{
7219 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007220 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
7221 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02007222 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02007223 struct send_ctx *sctx = NULL;
7224 u32 i;
7225 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01007226 int clone_sources_to_rollback = 0;
Denis Efremovbae12df2020-09-21 20:03:36 +03007227 size_t alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08007228 int sort_clone_roots = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02007229
7230 if (!capable(CAP_SYS_ADMIN))
7231 return -EPERM;
7232
Josef Bacik139f8072013-05-20 11:26:50 -04007233 /*
David Sterba2c686532013-12-16 17:34:17 +01007234 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02007235 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01007236 */
7237 spin_lock(&send_root->root_item_lock);
Filipe Manana62d54f32019-04-22 16:43:42 +01007238 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
7239 dedupe_in_progress_warn(send_root);
7240 spin_unlock(&send_root->root_item_lock);
7241 return -EAGAIN;
7242 }
David Sterba2c686532013-12-16 17:34:17 +01007243 send_root->send_in_progress++;
7244 spin_unlock(&send_root->root_item_lock);
7245
7246 /*
David Sterba2c686532013-12-16 17:34:17 +01007247 * Userspace tools do the checks and warn the user if it's
7248 * not RO.
7249 */
7250 if (!btrfs_root_readonly(send_root)) {
7251 ret = -EPERM;
7252 goto out;
7253 }
7254
Dan Carpenter457ae722017-03-17 23:51:20 +03007255 /*
7256 * Check that we don't overflow at later allocations, we request
7257 * clone_sources_count + 1 items, and compare to unsigned long inside
7258 * access_ok.
7259 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03007260 if (arg->clone_sources_count >
Dan Carpenter457ae722017-03-17 23:51:20 +03007261 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03007262 ret = -EINVAL;
7263 goto out;
7264 }
7265
Stefan Behrensc2c71322013-04-10 17:10:52 +00007266 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00007267 ret = -EINVAL;
7268 goto out;
7269 }
7270
David Sterbae780b0d2016-01-18 18:42:13 +01007271 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007272 if (!sctx) {
7273 ret = -ENOMEM;
7274 goto out;
7275 }
7276
7277 INIT_LIST_HEAD(&sctx->new_refs);
7278 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01007279 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007280 INIT_LIST_HEAD(&sctx->name_cache_list);
7281
Mark Fashehcb95e7b2013-02-04 20:54:57 +00007282 sctx->flags = arg->flags;
7283
David Sterbae77fbf92021-10-22 16:53:36 +02007284 if (arg->flags & BTRFS_SEND_FLAG_VERSION) {
7285 if (arg->version > BTRFS_SEND_STREAM_VERSION) {
7286 ret = -EPROTO;
7287 goto out;
7288 }
7289 /* Zero means "use the highest version" */
7290 sctx->proto = arg->version ?: BTRFS_SEND_STREAM_VERSION;
7291 } else {
7292 sctx->proto = 1;
7293 }
7294
Alexander Block31db9f72012-07-25 23:19:24 +02007295 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00007296 if (!sctx->send_filp) {
7297 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02007298 goto out;
7299 }
7300
Alexander Block31db9f72012-07-25 23:19:24 +02007301 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02007302 /*
7303 * Unlikely but possible, if the subvolume is marked for deletion but
7304 * is slow to remove the directory entry, send can still be started
7305 */
7306 if (btrfs_root_dead(sctx->send_root)) {
7307 ret = -EPERM;
7308 goto out;
7309 }
7310
Alexander Block31db9f72012-07-25 23:19:24 +02007311 sctx->clone_roots_cnt = arg->clone_sources_count;
7312
7313 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
Michal Hocko752ade62017-05-08 15:57:27 -07007314 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007315 if (!sctx->send_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07007316 ret = -ENOMEM;
7317 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007318 }
7319
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007320 sctx->pending_dir_moves = RB_ROOT;
7321 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00007322 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007323
Denis Efremovbae12df2020-09-21 20:03:36 +03007324 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots),
7325 arg->clone_sources_count + 1,
7326 GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007327 if (!sctx->clone_roots) {
David Sterba818e0102017-05-31 18:40:02 +02007328 ret = -ENOMEM;
7329 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007330 }
7331
Denis Efremovbae12df2020-09-21 20:03:36 +03007332 alloc_size = array_size(sizeof(*arg->clone_sources),
7333 arg->clone_sources_count);
David Sterbae55d1152016-04-11 18:52:02 +02007334
Alexander Block31db9f72012-07-25 23:19:24 +02007335 if (arg->clone_sources_count) {
Michal Hocko752ade62017-05-08 15:57:27 -07007336 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007337 if (!clone_sources_tmp) {
Michal Hocko752ade62017-05-08 15:57:27 -07007338 ret = -ENOMEM;
7339 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007340 }
7341
7342 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02007343 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02007344 if (ret) {
7345 ret = -EFAULT;
7346 goto out;
7347 }
7348
7349 for (i = 0; i < arg->clone_sources_count; i++) {
David Sterba56e93572020-05-15 19:35:55 +02007350 clone_root = btrfs_get_fs_root(fs_info,
7351 clone_sources_tmp[i], true);
Alexander Block31db9f72012-07-25 23:19:24 +02007352 if (IS_ERR(clone_root)) {
7353 ret = PTR_ERR(clone_root);
7354 goto out;
7355 }
David Sterba2c686532013-12-16 17:34:17 +01007356 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00007357 if (!btrfs_root_readonly(clone_root) ||
7358 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01007359 spin_unlock(&clone_root->root_item_lock);
Josef Bacik00246522020-01-24 09:33:01 -05007360 btrfs_put_root(clone_root);
David Sterba2c686532013-12-16 17:34:17 +01007361 ret = -EPERM;
7362 goto out;
7363 }
Filipe Manana62d54f32019-04-22 16:43:42 +01007364 if (clone_root->dedupe_in_progress) {
7365 dedupe_in_progress_warn(clone_root);
7366 spin_unlock(&clone_root->root_item_lock);
Josef Bacik00246522020-01-24 09:33:01 -05007367 btrfs_put_root(clone_root);
Filipe Manana62d54f32019-04-22 16:43:42 +01007368 ret = -EAGAIN;
7369 goto out;
7370 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00007371 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01007372 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08007373
Alexander Block31db9f72012-07-25 23:19:24 +02007374 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00007375 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02007376 }
David Sterba2f913062016-04-11 18:40:08 +02007377 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02007378 clone_sources_tmp = NULL;
7379 }
7380
7381 if (arg->parent_root) {
David Sterba56e93572020-05-15 19:35:55 +02007382 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
7383 true);
Stefan Behrensb1b19592013-05-13 14:42:57 +00007384 if (IS_ERR(sctx->parent_root)) {
7385 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02007386 goto out;
7387 }
Wang Shilong18f687d2014-01-07 17:25:19 +08007388
David Sterba2c686532013-12-16 17:34:17 +01007389 spin_lock(&sctx->parent_root->root_item_lock);
7390 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02007391 if (!btrfs_root_readonly(sctx->parent_root) ||
7392 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01007393 spin_unlock(&sctx->parent_root->root_item_lock);
7394 ret = -EPERM;
7395 goto out;
7396 }
Filipe Manana62d54f32019-04-22 16:43:42 +01007397 if (sctx->parent_root->dedupe_in_progress) {
7398 dedupe_in_progress_warn(sctx->parent_root);
7399 spin_unlock(&sctx->parent_root->root_item_lock);
Filipe Manana62d54f32019-04-22 16:43:42 +01007400 ret = -EAGAIN;
7401 goto out;
7402 }
David Sterba2c686532013-12-16 17:34:17 +01007403 spin_unlock(&sctx->parent_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02007404 }
7405
7406 /*
7407 * Clones from send_root are allowed, but only if the clone source
7408 * is behind the current send position. This is checked while searching
7409 * for possible clone sources.
7410 */
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007411 sctx->clone_roots[sctx->clone_roots_cnt++].root =
Josef Bacik00246522020-01-24 09:33:01 -05007412 btrfs_grab_root(sctx->send_root);
Alexander Block31db9f72012-07-25 23:19:24 +02007413
7414 /* We do a bsearch later */
7415 sort(sctx->clone_roots, sctx->clone_roots_cnt,
7416 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
7417 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08007418 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02007419
Filipe Manana9f89d5d2019-04-15 09:29:36 +01007420 ret = flush_delalloc_roots(sctx);
7421 if (ret)
7422 goto out;
7423
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007424 ret = ensure_commit_roots_uptodate(sctx);
7425 if (ret)
7426 goto out;
7427
Filipe Manana1cea5cf2021-06-21 11:10:38 +01007428 spin_lock(&fs_info->send_reloc_lock);
7429 if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags)) {
7430 spin_unlock(&fs_info->send_reloc_lock);
Filipe Manana9e967492019-04-22 16:44:09 +01007431 btrfs_warn_rl(fs_info,
Filipe Manana1cea5cf2021-06-21 11:10:38 +01007432 "cannot run send because a relocation operation is in progress");
Filipe Manana9e967492019-04-22 16:44:09 +01007433 ret = -EAGAIN;
7434 goto out;
7435 }
7436 fs_info->send_in_progress++;
Filipe Manana1cea5cf2021-06-21 11:10:38 +01007437 spin_unlock(&fs_info->send_reloc_lock);
Filipe Manana9e967492019-04-22 16:44:09 +01007438
Alexander Block31db9f72012-07-25 23:19:24 +02007439 ret = send_subvol(sctx);
Filipe Manana1cea5cf2021-06-21 11:10:38 +01007440 spin_lock(&fs_info->send_reloc_lock);
Filipe Manana9e967492019-04-22 16:44:09 +01007441 fs_info->send_in_progress--;
Filipe Manana1cea5cf2021-06-21 11:10:38 +01007442 spin_unlock(&fs_info->send_reloc_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02007443 if (ret < 0)
7444 goto out;
7445
Stefan Behrensc2c71322013-04-10 17:10:52 +00007446 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
7447 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
7448 if (ret < 0)
7449 goto out;
7450 ret = send_cmd(sctx);
7451 if (ret < 0)
7452 goto out;
7453 }
Alexander Block31db9f72012-07-25 23:19:24 +02007454
7455out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007456 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7457 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7458 struct rb_node *n;
7459 struct pending_dir_move *pm;
7460
7461 n = rb_first(&sctx->pending_dir_moves);
7462 pm = rb_entry(n, struct pending_dir_move, node);
7463 while (!list_empty(&pm->list)) {
7464 struct pending_dir_move *pm2;
7465
7466 pm2 = list_first_entry(&pm->list,
7467 struct pending_dir_move, list);
7468 free_pending_move(sctx, pm2);
7469 }
7470 free_pending_move(sctx, pm);
7471 }
7472
7473 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7474 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7475 struct rb_node *n;
7476 struct waiting_dir_move *dm;
7477
7478 n = rb_first(&sctx->waiting_dir_moves);
7479 dm = rb_entry(n, struct waiting_dir_move, node);
7480 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7481 kfree(dm);
7482 }
7483
Filipe Manana9dc44212014-02-19 14:31:44 +00007484 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
7485 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
7486 struct rb_node *n;
7487 struct orphan_dir_info *odi;
7488
7489 n = rb_first(&sctx->orphan_dirs);
7490 odi = rb_entry(n, struct orphan_dir_info, node);
7491 free_orphan_dir_info(sctx, odi);
7492 }
7493
Wang Shilong896c14f2014-01-07 17:25:18 +08007494 if (sort_clone_roots) {
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007495 for (i = 0; i < sctx->clone_roots_cnt; i++) {
Wang Shilong896c14f2014-01-07 17:25:18 +08007496 btrfs_root_dec_send_in_progress(
7497 sctx->clone_roots[i].root);
Josef Bacik00246522020-01-24 09:33:01 -05007498 btrfs_put_root(sctx->clone_roots[i].root);
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007499 }
Wang Shilong896c14f2014-01-07 17:25:18 +08007500 } else {
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007501 for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
Wang Shilong896c14f2014-01-07 17:25:18 +08007502 btrfs_root_dec_send_in_progress(
7503 sctx->clone_roots[i].root);
Josef Bacik00246522020-01-24 09:33:01 -05007504 btrfs_put_root(sctx->clone_roots[i].root);
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007505 }
Wang Shilong896c14f2014-01-07 17:25:18 +08007506
7507 btrfs_root_dec_send_in_progress(send_root);
7508 }
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007509 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
David Sterba66ef7d62013-12-17 15:07:20 +01007510 btrfs_root_dec_send_in_progress(sctx->parent_root);
Josef Bacik00246522020-01-24 09:33:01 -05007511 btrfs_put_root(sctx->parent_root);
Josef Bacik6f9a3da2020-01-24 09:32:48 -05007512 }
David Sterba2c686532013-12-16 17:34:17 +01007513
David Sterba2f913062016-04-11 18:40:08 +02007514 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02007515
7516 if (sctx) {
7517 if (sctx->send_filp)
7518 fput(sctx->send_filp);
7519
David Sterbac03d01f2016-04-11 18:40:08 +02007520 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02007521 kvfree(sctx->send_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02007522
7523 name_cache_free(sctx);
7524
7525 kfree(sctx);
7526 }
7527
7528 return ret;
7529}