blob: cbf4909f5cd9cb52f41a0c62e745bfa6610f59fe [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"
Alexander Block31db9f72012-07-25 23:19:24 +020026
Alexander Block31db9f72012-07-25 23:19:24 +020027/*
28 * A fs_path is a helper to dynamically build path names with unknown size.
29 * It reallocates the internal buffer on demand.
30 * It allows fast adding of path elements on the right side (normal path) and
31 * fast adding to the left side (reversed path). A reversed path can also be
32 * unreversed if needed.
33 */
34struct fs_path {
35 union {
36 struct {
37 char *start;
38 char *end;
Alexander Block31db9f72012-07-25 23:19:24 +020039
40 char *buf;
David Sterba1f5a7ff2014-02-03 19:23:47 +010041 unsigned short buf_len:15;
42 unsigned short reversed:1;
Alexander Block31db9f72012-07-25 23:19:24 +020043 char inline_buf[];
44 };
David Sterbaace01052014-02-05 16:17:34 +010045 /*
46 * Average path length does not exceed 200 bytes, we'll have
47 * better packing in the slab and higher chance to satisfy
48 * a allocation later during send.
49 */
50 char pad[256];
Alexander Block31db9f72012-07-25 23:19:24 +020051 };
52};
53#define FS_PATH_INLINE_SIZE \
54 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
55
56
57/* reused for each extent */
58struct clone_root {
59 struct btrfs_root *root;
60 u64 ino;
61 u64 offset;
62
63 u64 found_refs;
64};
65
66#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
67#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
68
69struct send_ctx {
70 struct file *send_filp;
71 loff_t send_off;
72 char *send_buf;
73 u32 send_size;
74 u32 send_max_size;
75 u64 total_send_size;
76 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000077 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020078
Alexander Block31db9f72012-07-25 23:19:24 +020079 struct btrfs_root *send_root;
80 struct btrfs_root *parent_root;
81 struct clone_root *clone_roots;
82 int clone_roots_cnt;
83
84 /* current state of the compare_tree call */
85 struct btrfs_path *left_path;
86 struct btrfs_path *right_path;
87 struct btrfs_key *cmp_key;
88
89 /*
90 * infos of the currently processed inode. In case of deleted inodes,
91 * these are the values from the deleted inode.
92 */
93 u64 cur_ino;
94 u64 cur_inode_gen;
95 int cur_inode_new;
96 int cur_inode_new_gen;
97 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +020098 u64 cur_inode_size;
99 u64 cur_inode_mode;
Liu Bo644d1942014-02-27 17:29:01 +0800100 u64 cur_inode_rdev;
Josef Bacik16e75492013-10-22 12:18:51 -0400101 u64 cur_inode_last_extent;
Filipe Mananaffa7c422018-02-06 20:40:40 +0000102 u64 cur_inode_next_write_offset;
Filipe Manana46b2f452018-07-24 11:54:04 +0100103 bool ignore_cur_inode;
Alexander Block31db9f72012-07-25 23:19:24 +0200104
105 u64 send_progress;
106
107 struct list_head new_refs;
108 struct list_head deleted_refs;
109
110 struct radix_tree_root name_cache;
111 struct list_head name_cache_list;
112 int name_cache_size;
113
Liu Bo2131bcd2014-03-05 10:07:35 +0800114 struct file_ra_state ra;
115
Alexander Block31db9f72012-07-25 23:19:24 +0200116 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000117
118 /*
119 * We process inodes by their increasing order, so if before an
120 * incremental send we reverse the parent/child relationship of
121 * directories such that a directory with a lower inode number was
122 * the parent of a directory with a higher inode number, and the one
123 * becoming the new parent got renamed too, we can't rename/move the
124 * directory with lower inode number when we finish processing it - we
125 * must process the directory with higher inode number first, then
126 * rename/move it and then rename/move the directory with lower inode
127 * number. Example follows.
128 *
129 * Tree state when the first send was performed:
130 *
131 * .
132 * |-- a (ino 257)
133 * |-- b (ino 258)
134 * |
135 * |
136 * |-- c (ino 259)
137 * | |-- d (ino 260)
138 * |
139 * |-- c2 (ino 261)
140 *
141 * Tree state when the second (incremental) send is performed:
142 *
143 * .
144 * |-- a (ino 257)
145 * |-- b (ino 258)
146 * |-- c2 (ino 261)
147 * |-- d2 (ino 260)
148 * |-- cc (ino 259)
149 *
150 * The sequence of steps that lead to the second state was:
151 *
152 * mv /a/b/c/d /a/b/c2/d2
153 * mv /a/b/c /a/b/c2/d2/cc
154 *
155 * "c" has lower inode number, but we can't move it (2nd mv operation)
156 * before we move "d", which has higher inode number.
157 *
158 * So we just memorize which move/rename operations must be performed
159 * later when their respective parent is processed and moved/renamed.
160 */
161
162 /* Indexed by parent directory inode number. */
163 struct rb_root pending_dir_moves;
164
165 /*
166 * Reverse index, indexed by the inode number of a directory that
167 * is waiting for the move/rename of its immediate parent before its
168 * own move/rename can be performed.
169 */
170 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000171
172 /*
173 * A directory that is going to be rm'ed might have a child directory
174 * which is in the pending directory moves index above. In this case,
175 * the directory can only be removed after the move/rename of its child
176 * is performed. Example:
177 *
178 * Parent snapshot:
179 *
180 * . (ino 256)
181 * |-- a/ (ino 257)
182 * |-- b/ (ino 258)
183 * |-- c/ (ino 259)
184 * | |-- x/ (ino 260)
185 * |
186 * |-- y/ (ino 261)
187 *
188 * Send snapshot:
189 *
190 * . (ino 256)
191 * |-- a/ (ino 257)
192 * |-- b/ (ino 258)
193 * |-- YY/ (ino 261)
194 * |-- x/ (ino 260)
195 *
196 * Sequence of steps that lead to the send snapshot:
197 * rm -f /a/b/c/foo.txt
198 * mv /a/b/y /a/b/YY
199 * mv /a/b/c/x /a/b/YY
200 * rmdir /a/b/c
201 *
202 * When the child is processed, its move/rename is delayed until its
203 * parent is processed (as explained above), but all other operations
204 * like update utimes, chown, chgrp, etc, are performed and the paths
205 * that it uses for those operations must use the orphanized name of
206 * its parent (the directory we're going to rm later), so we need to
207 * memorize that name.
208 *
209 * Indexed by the inode number of the directory to be deleted.
210 */
211 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000212};
213
214struct pending_dir_move {
215 struct rb_node node;
216 struct list_head list;
217 u64 parent_ino;
218 u64 ino;
219 u64 gen;
220 struct list_head update_refs;
221};
222
223struct waiting_dir_move {
224 struct rb_node node;
225 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000226 /*
227 * There might be some directory that could not be removed because it
228 * was waiting for this directory inode to be moved first. Therefore
229 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
230 */
231 u64 rmdir_ino;
Filipe Manana8b191a62015-04-09 14:09:14 +0100232 bool orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +0000233};
234
235struct orphan_dir_info {
236 struct rb_node node;
237 u64 ino;
238 u64 gen;
Robbie Ko0f96f512018-05-08 18:11:38 +0800239 u64 last_dir_index_offset;
Alexander Block31db9f72012-07-25 23:19:24 +0200240};
241
242struct name_cache_entry {
243 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200244 /*
245 * radix_tree has only 32bit entries but we need to handle 64bit inums.
246 * We use the lower 32bit of the 64bit inum to store it in the tree. If
247 * more then one inum would fall into the same entry, we use radix_list
248 * to store the additional entries. radix_list is also used to store
249 * entries where two entries have the same inum but different
250 * generations.
251 */
252 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200253 u64 ino;
254 u64 gen;
255 u64 parent_ino;
256 u64 parent_gen;
257 int ret;
258 int need_later_update;
259 int name_len;
260 char name[];
261};
262
David Sterba18d0f5c2019-08-21 19:12:59 +0200263#define ADVANCE 1
264#define ADVANCE_ONLY_NEXT -1
265
266enum btrfs_compare_tree_result {
267 BTRFS_COMPARE_TREE_NEW,
268 BTRFS_COMPARE_TREE_DELETED,
269 BTRFS_COMPARE_TREE_CHANGED,
270 BTRFS_COMPARE_TREE_SAME,
271};
272typedef int (*btrfs_changed_cb_t)(struct btrfs_path *left_path,
273 struct btrfs_path *right_path,
274 struct btrfs_key *key,
275 enum btrfs_compare_tree_result result,
276 void *ctx);
277
David Sterbae67c7182018-02-19 17:24:18 +0100278__cold
Filipe Manana95155582016-08-01 01:50:37 +0100279static void inconsistent_snapshot_error(struct send_ctx *sctx,
280 enum btrfs_compare_tree_result result,
281 const char *what)
282{
283 const char *result_string;
284
285 switch (result) {
286 case BTRFS_COMPARE_TREE_NEW:
287 result_string = "new";
288 break;
289 case BTRFS_COMPARE_TREE_DELETED:
290 result_string = "deleted";
291 break;
292 case BTRFS_COMPARE_TREE_CHANGED:
293 result_string = "updated";
294 break;
295 case BTRFS_COMPARE_TREE_SAME:
296 ASSERT(0);
297 result_string = "unchanged";
298 break;
299 default:
300 ASSERT(0);
301 result_string = "unexpected";
302 }
303
304 btrfs_err(sctx->send_root->fs_info,
305 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
306 result_string, what, sctx->cmp_key->objectid,
307 sctx->send_root->root_key.objectid,
308 (sctx->parent_root ?
309 sctx->parent_root->root_key.objectid : 0));
310}
311
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000312static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
313
Filipe Manana9dc44212014-02-19 14:31:44 +0000314static struct waiting_dir_move *
315get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
316
317static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
318
Josef Bacik16e75492013-10-22 12:18:51 -0400319static int need_send_hole(struct send_ctx *sctx)
320{
321 return (sctx->parent_root && !sctx->cur_inode_new &&
322 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
323 S_ISREG(sctx->cur_inode_mode));
324}
325
Alexander Block31db9f72012-07-25 23:19:24 +0200326static void fs_path_reset(struct fs_path *p)
327{
328 if (p->reversed) {
329 p->start = p->buf + p->buf_len - 1;
330 p->end = p->start;
331 *p->start = 0;
332 } else {
333 p->start = p->buf;
334 p->end = p->start;
335 *p->start = 0;
336 }
337}
338
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000339static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200340{
341 struct fs_path *p;
342
David Sterbae780b0d2016-01-18 18:42:13 +0100343 p = kmalloc(sizeof(*p), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +0200344 if (!p)
345 return NULL;
346 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200347 p->buf = p->inline_buf;
348 p->buf_len = FS_PATH_INLINE_SIZE;
349 fs_path_reset(p);
350 return p;
351}
352
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000353static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200354{
355 struct fs_path *p;
356
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000357 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200358 if (!p)
359 return NULL;
360 p->reversed = 1;
361 fs_path_reset(p);
362 return p;
363}
364
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000365static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200366{
367 if (!p)
368 return;
David Sterbaace01052014-02-05 16:17:34 +0100369 if (p->buf != p->inline_buf)
370 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200371 kfree(p);
372}
373
374static int fs_path_len(struct fs_path *p)
375{
376 return p->end - p->start;
377}
378
379static int fs_path_ensure_buf(struct fs_path *p, int len)
380{
381 char *tmp_buf;
382 int path_len;
383 int old_buf_len;
384
385 len++;
386
387 if (p->buf_len >= len)
388 return 0;
389
Chris Masoncfd4a532014-04-26 05:02:03 -0700390 if (len > PATH_MAX) {
391 WARN_ON(1);
392 return -ENOMEM;
393 }
394
David Sterba1b2782c2014-02-25 19:32:59 +0100395 path_len = p->end - p->start;
396 old_buf_len = p->buf_len;
397
David Sterbaace01052014-02-05 16:17:34 +0100398 /*
399 * First time the inline_buf does not suffice
400 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100401 if (p->buf == p->inline_buf) {
David Sterbae780b0d2016-01-18 18:42:13 +0100402 tmp_buf = kmalloc(len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100403 if (tmp_buf)
404 memcpy(tmp_buf, p->buf, old_buf_len);
405 } else {
David Sterbae780b0d2016-01-18 18:42:13 +0100406 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100407 }
David Sterba9c9ca002014-02-25 19:33:08 +0100408 if (!tmp_buf)
409 return -ENOMEM;
410 p->buf = tmp_buf;
411 /*
412 * The real size of the buffer is bigger, this will let the fast path
413 * happen most of the time
414 */
415 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100416
Alexander Block31db9f72012-07-25 23:19:24 +0200417 if (p->reversed) {
418 tmp_buf = p->buf + old_buf_len - path_len - 1;
419 p->end = p->buf + p->buf_len - 1;
420 p->start = p->end - path_len;
421 memmove(p->start, tmp_buf, path_len + 1);
422 } else {
423 p->start = p->buf;
424 p->end = p->start + path_len;
425 }
426 return 0;
427}
428
David Sterbab23ab572014-02-03 19:23:19 +0100429static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
430 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200431{
432 int ret;
433 int new_len;
434
435 new_len = p->end - p->start + name_len;
436 if (p->start != p->end)
437 new_len++;
438 ret = fs_path_ensure_buf(p, new_len);
439 if (ret < 0)
440 goto out;
441
442 if (p->reversed) {
443 if (p->start != p->end)
444 *--p->start = '/';
445 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100446 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200447 } else {
448 if (p->start != p->end)
449 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100450 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200451 p->end += name_len;
452 *p->end = 0;
453 }
454
455out:
456 return ret;
457}
458
459static int fs_path_add(struct fs_path *p, const char *name, int name_len)
460{
461 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100462 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200463
David Sterbab23ab572014-02-03 19:23:19 +0100464 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200465 if (ret < 0)
466 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100467 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200468
469out:
470 return ret;
471}
472
473static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
474{
475 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100476 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200477
David Sterbab23ab572014-02-03 19:23:19 +0100478 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200479 if (ret < 0)
480 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100481 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200482
483out:
484 return ret;
485}
486
487static int fs_path_add_from_extent_buffer(struct fs_path *p,
488 struct extent_buffer *eb,
489 unsigned long off, int len)
490{
491 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100492 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200493
David Sterbab23ab572014-02-03 19:23:19 +0100494 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200495 if (ret < 0)
496 goto out;
497
David Sterbab23ab572014-02-03 19:23:19 +0100498 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200499
500out:
501 return ret;
502}
503
Alexander Block31db9f72012-07-25 23:19:24 +0200504static int fs_path_copy(struct fs_path *p, struct fs_path *from)
505{
506 int ret;
507
508 p->reversed = from->reversed;
509 fs_path_reset(p);
510
511 ret = fs_path_add_path(p, from);
512
513 return ret;
514}
515
516
517static void fs_path_unreverse(struct fs_path *p)
518{
519 char *tmp;
520 int len;
521
522 if (!p->reversed)
523 return;
524
525 tmp = p->start;
526 len = p->end - p->start;
527 p->start = p->buf;
528 p->end = p->start + len;
529 memmove(p->start, tmp, len + 1);
530 p->reversed = 0;
531}
532
533static struct btrfs_path *alloc_path_for_send(void)
534{
535 struct btrfs_path *path;
536
537 path = btrfs_alloc_path();
538 if (!path)
539 return NULL;
540 path->search_commit_root = 1;
541 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400542 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200543 return path;
544}
545
Eric Sandeen48a3b632013-04-25 20:41:01 +0000546static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200547{
548 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200549 u32 pos = 0;
550
Alexander Block31db9f72012-07-25 23:19:24 +0200551 while (pos < len) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200552 ret = kernel_write(filp, buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200553 /* TODO handle that correctly */
554 /*if (ret == -ERESTARTSYS) {
555 continue;
556 }*/
557 if (ret < 0)
Christoph Hellwig8e931572017-09-01 17:39:19 +0200558 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200559 if (ret == 0) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200560 return -EIO;
Alexander Block31db9f72012-07-25 23:19:24 +0200561 }
562 pos += ret;
563 }
564
Christoph Hellwig8e931572017-09-01 17:39:19 +0200565 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200566}
567
568static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
569{
570 struct btrfs_tlv_header *hdr;
571 int total_len = sizeof(*hdr) + len;
572 int left = sctx->send_max_size - sctx->send_size;
573
574 if (unlikely(left < total_len))
575 return -EOVERFLOW;
576
577 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
578 hdr->tlv_type = cpu_to_le16(attr);
579 hdr->tlv_len = cpu_to_le16(len);
580 memcpy(hdr + 1, data, len);
581 sctx->send_size += total_len;
582
583 return 0;
584}
585
David Sterba95bc79d2013-12-16 17:34:10 +0100586#define TLV_PUT_DEFINE_INT(bits) \
587 static int tlv_put_u##bits(struct send_ctx *sctx, \
588 u##bits attr, u##bits value) \
589 { \
590 __le##bits __tmp = cpu_to_le##bits(value); \
591 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
592 }
Alexander Block31db9f72012-07-25 23:19:24 +0200593
David Sterba95bc79d2013-12-16 17:34:10 +0100594TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200595
596static int tlv_put_string(struct send_ctx *sctx, u16 attr,
597 const char *str, int len)
598{
599 if (len == -1)
600 len = strlen(str);
601 return tlv_put(sctx, attr, str, len);
602}
603
604static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
605 const u8 *uuid)
606{
607 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
608}
609
Alexander Block31db9f72012-07-25 23:19:24 +0200610static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
611 struct extent_buffer *eb,
612 struct btrfs_timespec *ts)
613{
614 struct btrfs_timespec bts;
615 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
616 return tlv_put(sctx, attr, &bts, sizeof(bts));
617}
618
619
Liu Bo895a72b2018-03-02 18:05:49 -0700620#define TLV_PUT(sctx, attrtype, data, attrlen) \
Alexander Block31db9f72012-07-25 23:19:24 +0200621 do { \
Liu Bo895a72b2018-03-02 18:05:49 -0700622 ret = tlv_put(sctx, attrtype, data, attrlen); \
Alexander Block31db9f72012-07-25 23:19:24 +0200623 if (ret < 0) \
624 goto tlv_put_failure; \
625 } while (0)
626
627#define TLV_PUT_INT(sctx, attrtype, bits, value) \
628 do { \
629 ret = tlv_put_u##bits(sctx, attrtype, value); \
630 if (ret < 0) \
631 goto tlv_put_failure; \
632 } while (0)
633
634#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
635#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
636#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
637#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
638#define TLV_PUT_STRING(sctx, attrtype, str, len) \
639 do { \
640 ret = tlv_put_string(sctx, attrtype, str, len); \
641 if (ret < 0) \
642 goto tlv_put_failure; \
643 } while (0)
644#define TLV_PUT_PATH(sctx, attrtype, p) \
645 do { \
646 ret = tlv_put_string(sctx, attrtype, p->start, \
647 p->end - p->start); \
648 if (ret < 0) \
649 goto tlv_put_failure; \
650 } while(0)
651#define TLV_PUT_UUID(sctx, attrtype, uuid) \
652 do { \
653 ret = tlv_put_uuid(sctx, attrtype, uuid); \
654 if (ret < 0) \
655 goto tlv_put_failure; \
656 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200657#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
658 do { \
659 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
660 if (ret < 0) \
661 goto tlv_put_failure; \
662 } while (0)
663
664static int send_header(struct send_ctx *sctx)
665{
666 struct btrfs_stream_header hdr;
667
668 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
669 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
670
Anand Jain1bcea352012-09-14 00:04:21 -0600671 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
672 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200673}
674
675/*
676 * For each command/item we want to send to userspace, we call this function.
677 */
678static int begin_cmd(struct send_ctx *sctx, int cmd)
679{
680 struct btrfs_cmd_header *hdr;
681
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530682 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200683 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200684
685 BUG_ON(sctx->send_size);
686
687 sctx->send_size += sizeof(*hdr);
688 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
689 hdr->cmd = cpu_to_le16(cmd);
690
691 return 0;
692}
693
694static int send_cmd(struct send_ctx *sctx)
695{
696 int ret;
697 struct btrfs_cmd_header *hdr;
698 u32 crc;
699
700 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
701 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
702 hdr->crc = 0;
703
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200704 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200705 hdr->crc = cpu_to_le32(crc);
706
Anand Jain1bcea352012-09-14 00:04:21 -0600707 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
708 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200709
710 sctx->total_send_size += sctx->send_size;
711 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
712 sctx->send_size = 0;
713
714 return ret;
715}
716
717/*
718 * Sends a move instruction to user space
719 */
720static int send_rename(struct send_ctx *sctx,
721 struct fs_path *from, struct fs_path *to)
722{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400723 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200724 int ret;
725
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400726 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200727
728 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
729 if (ret < 0)
730 goto out;
731
732 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
733 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
734
735 ret = send_cmd(sctx);
736
737tlv_put_failure:
738out:
739 return ret;
740}
741
742/*
743 * Sends a link instruction to user space
744 */
745static int send_link(struct send_ctx *sctx,
746 struct fs_path *path, struct fs_path *lnk)
747{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400748 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200749 int ret;
750
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400751 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200752
753 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
754 if (ret < 0)
755 goto out;
756
757 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
758 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
759
760 ret = send_cmd(sctx);
761
762tlv_put_failure:
763out:
764 return ret;
765}
766
767/*
768 * Sends an unlink instruction to user space
769 */
770static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
771{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400772 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200773 int ret;
774
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400775 btrfs_debug(fs_info, "send_unlink %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200776
777 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
778 if (ret < 0)
779 goto out;
780
781 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
782
783 ret = send_cmd(sctx);
784
785tlv_put_failure:
786out:
787 return ret;
788}
789
790/*
791 * Sends a rmdir instruction to user space
792 */
793static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
794{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400795 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200796 int ret;
797
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400798 btrfs_debug(fs_info, "send_rmdir %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200799
800 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
801 if (ret < 0)
802 goto out;
803
804 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
805
806 ret = send_cmd(sctx);
807
808tlv_put_failure:
809out:
810 return ret;
811}
812
813/*
814 * Helper function to retrieve some fields from an inode item.
815 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400816static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
817 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
818 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200819{
820 int ret;
821 struct btrfs_inode_item *ii;
822 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200823
824 key.objectid = ino;
825 key.type = BTRFS_INODE_ITEM_KEY;
826 key.offset = 0;
827 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200828 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400829 if (ret > 0)
830 ret = -ENOENT;
831 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200832 }
833
834 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
835 struct btrfs_inode_item);
836 if (size)
837 *size = btrfs_inode_size(path->nodes[0], ii);
838 if (gen)
839 *gen = btrfs_inode_generation(path->nodes[0], ii);
840 if (mode)
841 *mode = btrfs_inode_mode(path->nodes[0], ii);
842 if (uid)
843 *uid = btrfs_inode_uid(path->nodes[0], ii);
844 if (gid)
845 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200846 if (rdev)
847 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200848
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400849 return ret;
850}
851
852static int get_inode_info(struct btrfs_root *root,
853 u64 ino, u64 *size, u64 *gen,
854 u64 *mode, u64 *uid, u64 *gid,
855 u64 *rdev)
856{
857 struct btrfs_path *path;
858 int ret;
859
860 path = alloc_path_for_send();
861 if (!path)
862 return -ENOMEM;
863 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
864 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200865 btrfs_free_path(path);
866 return ret;
867}
868
869typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
870 struct fs_path *p,
871 void *ctx);
872
873/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000874 * Helper function to iterate the entries in ONE btrfs_inode_ref or
875 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200876 * The iterate callback may return a non zero value to stop iteration. This can
877 * be a negative value for error codes or 1 to simply stop it.
878 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000879 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200880 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000881static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200882 struct btrfs_key *found_key, int resolve,
883 iterate_inode_ref_t iterate, void *ctx)
884{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000885 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200886 struct btrfs_item *item;
887 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000888 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200889 struct btrfs_path *tmp_path;
890 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000891 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200892 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000893 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200894 u32 name_len;
895 char *start;
896 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000897 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200898 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000899 u64 dir;
900 unsigned long name_off;
901 unsigned long elem_size;
902 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200903
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000904 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200905 if (!p)
906 return -ENOMEM;
907
908 tmp_path = alloc_path_for_send();
909 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000910 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200911 return -ENOMEM;
912 }
913
Alexander Block31db9f72012-07-25 23:19:24 +0200914
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000915 if (found_key->type == BTRFS_INODE_REF_KEY) {
916 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
917 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100918 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000919 total = btrfs_item_size(eb, item);
920 elem_size = sizeof(*iref);
921 } else {
922 ptr = btrfs_item_ptr_offset(eb, slot);
923 total = btrfs_item_size_nr(eb, slot);
924 elem_size = sizeof(*extref);
925 }
926
Alexander Block31db9f72012-07-25 23:19:24 +0200927 while (cur < total) {
928 fs_path_reset(p);
929
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000930 if (found_key->type == BTRFS_INODE_REF_KEY) {
931 iref = (struct btrfs_inode_ref *)(ptr + cur);
932 name_len = btrfs_inode_ref_name_len(eb, iref);
933 name_off = (unsigned long)(iref + 1);
934 index = btrfs_inode_ref_index(eb, iref);
935 dir = found_key->offset;
936 } else {
937 extref = (struct btrfs_inode_extref *)(ptr + cur);
938 name_len = btrfs_inode_extref_name_len(eb, extref);
939 name_off = (unsigned long)&extref->name;
940 index = btrfs_inode_extref_index(eb, extref);
941 dir = btrfs_inode_extref_parent(eb, extref);
942 }
943
Alexander Block31db9f72012-07-25 23:19:24 +0200944 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000945 start = btrfs_ref_to_path(root, tmp_path, name_len,
946 name_off, eb, dir,
947 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200948 if (IS_ERR(start)) {
949 ret = PTR_ERR(start);
950 goto out;
951 }
952 if (start < p->buf) {
953 /* overflow , try again with larger buffer */
954 ret = fs_path_ensure_buf(p,
955 p->buf_len + p->buf - start);
956 if (ret < 0)
957 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000958 start = btrfs_ref_to_path(root, tmp_path,
959 name_len, name_off,
960 eb, dir,
961 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200962 if (IS_ERR(start)) {
963 ret = PTR_ERR(start);
964 goto out;
965 }
966 BUG_ON(start < p->buf);
967 }
968 p->start = start;
969 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000970 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
971 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200972 if (ret < 0)
973 goto out;
974 }
975
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000976 cur += elem_size + name_len;
977 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200978 if (ret)
979 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200980 num++;
981 }
982
983out:
984 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000985 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200986 return ret;
987}
988
989typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
990 const char *name, int name_len,
991 const char *data, int data_len,
992 u8 type, void *ctx);
993
994/*
995 * Helper function to iterate the entries in ONE btrfs_dir_item.
996 * The iterate callback may return a non zero value to stop iteration. This can
997 * be a negative value for error codes or 1 to simply stop it.
998 *
999 * path must point to the dir item when called.
1000 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001001static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +02001002 iterate_dir_item_t iterate, void *ctx)
1003{
1004 int ret = 0;
1005 struct extent_buffer *eb;
1006 struct btrfs_item *item;
1007 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +02001008 struct btrfs_key di_key;
1009 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +01001010 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +02001011 u32 name_len;
1012 u32 data_len;
1013 u32 cur;
1014 u32 len;
1015 u32 total;
1016 int slot;
1017 int num;
1018 u8 type;
1019
Filipe Manana4395e0c2014-08-20 10:45:45 +01001020 /*
1021 * Start with a small buffer (1 page). If later we end up needing more
1022 * space, which can happen for xattrs on a fs with a leaf size greater
1023 * then the page size, attempt to increase the buffer. Typically xattr
1024 * values are small.
1025 */
1026 buf_len = PATH_MAX;
David Sterbae780b0d2016-01-18 18:42:13 +01001027 buf = kmalloc(buf_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02001028 if (!buf) {
1029 ret = -ENOMEM;
1030 goto out;
1031 }
1032
Alexander Block31db9f72012-07-25 23:19:24 +02001033 eb = path->nodes[0];
1034 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001035 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001036 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1037 cur = 0;
1038 len = 0;
1039 total = btrfs_item_size(eb, item);
1040
1041 num = 0;
1042 while (cur < total) {
1043 name_len = btrfs_dir_name_len(eb, di);
1044 data_len = btrfs_dir_data_len(eb, di);
1045 type = btrfs_dir_type(eb, di);
1046 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1047
Filipe Manana7e3ae332014-05-23 20:15:16 +01001048 if (type == BTRFS_FT_XATTR) {
1049 if (name_len > XATTR_NAME_MAX) {
1050 ret = -ENAMETOOLONG;
1051 goto out;
1052 }
Jeff Mahoneyda170662016-06-15 09:22:56 -04001053 if (name_len + data_len >
1054 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001055 ret = -E2BIG;
1056 goto out;
1057 }
1058 } else {
1059 /*
1060 * Path too long
1061 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001062 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001063 ret = -ENAMETOOLONG;
1064 goto out;
1065 }
Alexander Block31db9f72012-07-25 23:19:24 +02001066 }
1067
Filipe Manana4395e0c2014-08-20 10:45:45 +01001068 if (name_len + data_len > buf_len) {
1069 buf_len = name_len + data_len;
1070 if (is_vmalloc_addr(buf)) {
1071 vfree(buf);
1072 buf = NULL;
1073 } else {
1074 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001075 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001076
1077 if (!tmp)
1078 kfree(buf);
1079 buf = tmp;
1080 }
1081 if (!buf) {
David Sterbaf11f7442017-05-31 18:40:02 +02001082 buf = kvmalloc(buf_len, GFP_KERNEL);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001083 if (!buf) {
1084 ret = -ENOMEM;
1085 goto out;
1086 }
1087 }
1088 }
1089
Alexander Block31db9f72012-07-25 23:19:24 +02001090 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1091 name_len + data_len);
1092
1093 len = sizeof(*di) + name_len + data_len;
1094 di = (struct btrfs_dir_item *)((char *)di + len);
1095 cur += len;
1096
1097 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1098 data_len, type, ctx);
1099 if (ret < 0)
1100 goto out;
1101 if (ret) {
1102 ret = 0;
1103 goto out;
1104 }
1105
1106 num++;
1107 }
1108
1109out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001110 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001111 return ret;
1112}
1113
1114static int __copy_first_ref(int num, u64 dir, int index,
1115 struct fs_path *p, void *ctx)
1116{
1117 int ret;
1118 struct fs_path *pt = ctx;
1119
1120 ret = fs_path_copy(pt, p);
1121 if (ret < 0)
1122 return ret;
1123
1124 /* we want the first only */
1125 return 1;
1126}
1127
1128/*
1129 * Retrieve the first path of an inode. If an inode has more then one
1130 * ref/hardlink, this is ignored.
1131 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001132static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001133 u64 ino, struct fs_path *path)
1134{
1135 int ret;
1136 struct btrfs_key key, found_key;
1137 struct btrfs_path *p;
1138
1139 p = alloc_path_for_send();
1140 if (!p)
1141 return -ENOMEM;
1142
1143 fs_path_reset(path);
1144
1145 key.objectid = ino;
1146 key.type = BTRFS_INODE_REF_KEY;
1147 key.offset = 0;
1148
1149 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1150 if (ret < 0)
1151 goto out;
1152 if (ret) {
1153 ret = 1;
1154 goto out;
1155 }
1156 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1157 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001158 (found_key.type != BTRFS_INODE_REF_KEY &&
1159 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001160 ret = -ENOENT;
1161 goto out;
1162 }
1163
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001164 ret = iterate_inode_ref(root, p, &found_key, 1,
1165 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001166 if (ret < 0)
1167 goto out;
1168 ret = 0;
1169
1170out:
1171 btrfs_free_path(p);
1172 return ret;
1173}
1174
1175struct backref_ctx {
1176 struct send_ctx *sctx;
1177
1178 /* number of total found references */
1179 u64 found;
1180
1181 /*
1182 * used for clones found in send_root. clones found behind cur_objectid
1183 * and cur_offset are not considered as allowed clones.
1184 */
1185 u64 cur_objectid;
1186 u64 cur_offset;
1187
1188 /* may be truncated in case it's the last extent in a file */
1189 u64 extent_len;
1190
Filipe Manana619d8c42015-05-03 01:56:00 +01001191 /* data offset in the file extent item */
1192 u64 data_offset;
1193
Alexander Block31db9f72012-07-25 23:19:24 +02001194 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001195 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001196};
1197
1198static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1199{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001200 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001201 struct clone_root *cr = (struct clone_root *)elt;
1202
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001203 if (root < cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001204 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001205 if (root > cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001206 return 1;
1207 return 0;
1208}
1209
1210static int __clone_root_cmp_sort(const void *e1, const void *e2)
1211{
1212 struct clone_root *cr1 = (struct clone_root *)e1;
1213 struct clone_root *cr2 = (struct clone_root *)e2;
1214
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001215 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001216 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001217 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001218 return 1;
1219 return 0;
1220}
1221
1222/*
1223 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001224 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001225 */
1226static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1227{
1228 struct backref_ctx *bctx = ctx_;
1229 struct clone_root *found;
Alexander Block31db9f72012-07-25 23:19:24 +02001230
1231 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001232 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001233 bctx->sctx->clone_roots_cnt,
1234 sizeof(struct clone_root),
1235 __clone_root_cmp_bsearch);
1236 if (!found)
1237 return 0;
1238
1239 if (found->root == bctx->sctx->send_root &&
1240 ino == bctx->cur_objectid &&
1241 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001242 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001243 }
1244
1245 /*
Alexander Block31db9f72012-07-25 23:19:24 +02001246 * Make sure we don't consider clones from send_root that are
1247 * behind the current inode/offset.
1248 */
1249 if (found->root == bctx->sctx->send_root) {
1250 /*
Filipe Manana11f20692019-10-30 12:23:11 +00001251 * If the source inode was not yet processed we can't issue a
1252 * clone operation, as the source extent does not exist yet at
1253 * the destination of the stream.
Alexander Block31db9f72012-07-25 23:19:24 +02001254 */
Filipe Manana11f20692019-10-30 12:23:11 +00001255 if (ino > bctx->cur_objectid)
1256 return 0;
1257 /*
1258 * We clone from the inode currently being sent as long as the
1259 * source extent is already processed, otherwise we could try
1260 * to clone from an extent that does not exist yet at the
1261 * destination of the stream.
1262 */
1263 if (ino == bctx->cur_objectid &&
1264 offset >= bctx->sctx->cur_inode_next_write_offset)
Alexander Block31db9f72012-07-25 23:19:24 +02001265 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001266 }
1267
1268 bctx->found++;
1269 found->found_refs++;
1270 if (ino < found->ino) {
1271 found->ino = ino;
1272 found->offset = offset;
1273 } else if (found->ino == ino) {
1274 /*
1275 * same extent found more then once in the same file.
1276 */
1277 if (found->offset > offset + bctx->extent_len)
1278 found->offset = offset;
1279 }
1280
1281 return 0;
1282}
1283
1284/*
Alexander Block766702e2012-07-28 14:11:31 +02001285 * Given an inode, offset and extent item, it finds a good clone for a clone
1286 * instruction. Returns -ENOENT when none could be found. The function makes
1287 * sure that the returned clone is usable at the point where sending is at the
1288 * moment. This means, that no clones are accepted which lie behind the current
1289 * inode+offset.
1290 *
Alexander Block31db9f72012-07-25 23:19:24 +02001291 * path must point to the extent item when called.
1292 */
1293static int find_extent_clone(struct send_ctx *sctx,
1294 struct btrfs_path *path,
1295 u64 ino, u64 data_offset,
1296 u64 ino_size,
1297 struct clone_root **found)
1298{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001299 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001300 int ret;
1301 int extent_type;
1302 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001303 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001304 u64 num_bytes;
1305 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001306 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001307 struct btrfs_file_extent_item *fi;
1308 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001309 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001310 struct clone_root *cur_clone_root;
1311 struct btrfs_key found_key;
1312 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001313 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001314 u32 i;
1315
1316 tmp_path = alloc_path_for_send();
1317 if (!tmp_path)
1318 return -ENOMEM;
1319
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001320 /* We only use this path under the commit sem */
1321 tmp_path->need_commit_sem = 0;
1322
David Sterbae780b0d2016-01-18 18:42:13 +01001323 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001324 if (!backref_ctx) {
1325 ret = -ENOMEM;
1326 goto out;
1327 }
1328
Alexander Block31db9f72012-07-25 23:19:24 +02001329 if (data_offset >= ino_size) {
1330 /*
1331 * There may be extents that lie behind the file's size.
1332 * I at least had this in combination with snapshotting while
1333 * writing large files.
1334 */
1335 ret = 0;
1336 goto out;
1337 }
1338
1339 fi = btrfs_item_ptr(eb, path->slots[0],
1340 struct btrfs_file_extent_item);
1341 extent_type = btrfs_file_extent_type(eb, fi);
1342 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1343 ret = -ENOENT;
1344 goto out;
1345 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001346 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001347
1348 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001349 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1350 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001351 ret = -ENOENT;
1352 goto out;
1353 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001354 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001355
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001356 down_read(&fs_info->commit_root_sem);
1357 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001358 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001359 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001360 btrfs_release_path(tmp_path);
1361
1362 if (ret < 0)
1363 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001364 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001365 ret = -EIO;
1366 goto out;
1367 }
1368
1369 /*
1370 * Setup the clone roots.
1371 */
1372 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1373 cur_clone_root = sctx->clone_roots + i;
1374 cur_clone_root->ino = (u64)-1;
1375 cur_clone_root->offset = 0;
1376 cur_clone_root->found_refs = 0;
1377 }
1378
Alexander Block35075bb2012-07-28 12:44:34 +02001379 backref_ctx->sctx = sctx;
1380 backref_ctx->found = 0;
1381 backref_ctx->cur_objectid = ino;
1382 backref_ctx->cur_offset = data_offset;
1383 backref_ctx->found_itself = 0;
1384 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001385 /*
1386 * For non-compressed extents iterate_extent_inodes() gives us extent
1387 * offsets that already take into account the data offset, but not for
1388 * compressed extents, since the offset is logical and not relative to
1389 * the physical extent locations. We must take this into account to
1390 * avoid sending clone offsets that go beyond the source file's size,
1391 * which would result in the clone ioctl failing with -EINVAL on the
1392 * receiving end.
1393 */
1394 if (compressed == BTRFS_COMPRESS_NONE)
1395 backref_ctx->data_offset = 0;
1396 else
1397 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001398
1399 /*
1400 * The last extent of a file may be too large due to page alignment.
1401 * We need to adjust extent_len in this case so that the checks in
1402 * __iterate_backrefs work.
1403 */
1404 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001405 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001406
1407 /*
1408 * Now collect all backrefs.
1409 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001410 if (compressed == BTRFS_COMPRESS_NONE)
1411 extent_item_pos = logical - found_key.objectid;
1412 else
1413 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001414 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1415 extent_item_pos, 1, __iterate_backrefs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001416 backref_ctx, false);
Chris Mason74dd17f2012-08-07 16:25:13 -04001417
Alexander Block31db9f72012-07-25 23:19:24 +02001418 if (ret < 0)
1419 goto out;
1420
Alexander Block35075bb2012-07-28 12:44:34 +02001421 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001422 /* found a bug in backref code? */
1423 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001424 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001425 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001426 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001427 goto out;
1428 }
1429
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001430 btrfs_debug(fs_info,
1431 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1432 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001433
Alexander Block35075bb2012-07-28 12:44:34 +02001434 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001435 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001436
1437 cur_clone_root = NULL;
1438 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1439 if (sctx->clone_roots[i].found_refs) {
1440 if (!cur_clone_root)
1441 cur_clone_root = sctx->clone_roots + i;
1442 else if (sctx->clone_roots[i].root == sctx->send_root)
1443 /* prefer clones from send_root over others */
1444 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001445 }
1446
1447 }
1448
1449 if (cur_clone_root) {
1450 *found = cur_clone_root;
1451 ret = 0;
1452 } else {
1453 ret = -ENOENT;
1454 }
1455
1456out:
1457 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001458 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001459 return ret;
1460}
1461
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001462static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001463 u64 ino,
1464 struct fs_path *dest)
1465{
1466 int ret;
1467 struct btrfs_path *path;
1468 struct btrfs_key key;
1469 struct btrfs_file_extent_item *ei;
1470 u8 type;
1471 u8 compression;
1472 unsigned long off;
1473 int len;
1474
1475 path = alloc_path_for_send();
1476 if (!path)
1477 return -ENOMEM;
1478
1479 key.objectid = ino;
1480 key.type = BTRFS_EXTENT_DATA_KEY;
1481 key.offset = 0;
1482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1483 if (ret < 0)
1484 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001485 if (ret) {
1486 /*
1487 * An empty symlink inode. Can happen in rare error paths when
1488 * creating a symlink (transaction committed before the inode
1489 * eviction handler removed the symlink inode items and a crash
1490 * happened in between or the subvol was snapshoted in between).
1491 * Print an informative message to dmesg/syslog so that the user
1492 * can delete the symlink.
1493 */
1494 btrfs_err(root->fs_info,
1495 "Found empty symlink inode %llu at root %llu",
1496 ino, root->root_key.objectid);
1497 ret = -EIO;
1498 goto out;
1499 }
Alexander Block31db9f72012-07-25 23:19:24 +02001500
1501 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1502 struct btrfs_file_extent_item);
1503 type = btrfs_file_extent_type(path->nodes[0], ei);
1504 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1505 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1506 BUG_ON(compression);
1507
1508 off = btrfs_file_extent_inline_start(ei);
Qu Wenruoe41ca582018-06-06 15:41:49 +08001509 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001510
1511 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001512
1513out:
1514 btrfs_free_path(path);
1515 return ret;
1516}
1517
1518/*
1519 * Helper function to generate a file name that is unique in the root of
1520 * send_root and parent_root. This is used to generate names for orphan inodes.
1521 */
1522static int gen_unique_name(struct send_ctx *sctx,
1523 u64 ino, u64 gen,
1524 struct fs_path *dest)
1525{
1526 int ret = 0;
1527 struct btrfs_path *path;
1528 struct btrfs_dir_item *di;
1529 char tmp[64];
1530 int len;
1531 u64 idx = 0;
1532
1533 path = alloc_path_for_send();
1534 if (!path)
1535 return -ENOMEM;
1536
1537 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001538 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001539 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001540 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001541
1542 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1543 path, BTRFS_FIRST_FREE_OBJECTID,
1544 tmp, strlen(tmp), 0);
1545 btrfs_release_path(path);
1546 if (IS_ERR(di)) {
1547 ret = PTR_ERR(di);
1548 goto out;
1549 }
1550 if (di) {
1551 /* not unique, try again */
1552 idx++;
1553 continue;
1554 }
1555
1556 if (!sctx->parent_root) {
1557 /* unique */
1558 ret = 0;
1559 break;
1560 }
1561
1562 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1563 path, BTRFS_FIRST_FREE_OBJECTID,
1564 tmp, strlen(tmp), 0);
1565 btrfs_release_path(path);
1566 if (IS_ERR(di)) {
1567 ret = PTR_ERR(di);
1568 goto out;
1569 }
1570 if (di) {
1571 /* not unique, try again */
1572 idx++;
1573 continue;
1574 }
1575 /* unique */
1576 break;
1577 }
1578
1579 ret = fs_path_add(dest, tmp, strlen(tmp));
1580
1581out:
1582 btrfs_free_path(path);
1583 return ret;
1584}
1585
1586enum inode_state {
1587 inode_state_no_change,
1588 inode_state_will_create,
1589 inode_state_did_create,
1590 inode_state_will_delete,
1591 inode_state_did_delete,
1592};
1593
1594static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1595{
1596 int ret;
1597 int left_ret;
1598 int right_ret;
1599 u64 left_gen;
1600 u64 right_gen;
1601
1602 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001603 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001604 if (ret < 0 && ret != -ENOENT)
1605 goto out;
1606 left_ret = ret;
1607
1608 if (!sctx->parent_root) {
1609 right_ret = -ENOENT;
1610 } else {
1611 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001612 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001613 if (ret < 0 && ret != -ENOENT)
1614 goto out;
1615 right_ret = ret;
1616 }
1617
1618 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001619 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001620 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001621 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001622 if (ino < sctx->send_progress)
1623 ret = inode_state_did_create;
1624 else
1625 ret = inode_state_will_create;
1626 } else if (right_gen == gen) {
1627 if (ino < sctx->send_progress)
1628 ret = inode_state_did_delete;
1629 else
1630 ret = inode_state_will_delete;
1631 } else {
1632 ret = -ENOENT;
1633 }
1634 } else if (!left_ret) {
1635 if (left_gen == gen) {
1636 if (ino < sctx->send_progress)
1637 ret = inode_state_did_create;
1638 else
1639 ret = inode_state_will_create;
1640 } else {
1641 ret = -ENOENT;
1642 }
1643 } else if (!right_ret) {
1644 if (right_gen == gen) {
1645 if (ino < sctx->send_progress)
1646 ret = inode_state_did_delete;
1647 else
1648 ret = inode_state_will_delete;
1649 } else {
1650 ret = -ENOENT;
1651 }
1652 } else {
1653 ret = -ENOENT;
1654 }
1655
1656out:
1657 return ret;
1658}
1659
1660static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1661{
1662 int ret;
1663
Robbie Ko4dd99202017-01-05 16:24:55 +08001664 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1665 return 1;
1666
Alexander Block31db9f72012-07-25 23:19:24 +02001667 ret = get_cur_inode_state(sctx, ino, gen);
1668 if (ret < 0)
1669 goto out;
1670
1671 if (ret == inode_state_no_change ||
1672 ret == inode_state_did_create ||
1673 ret == inode_state_will_delete)
1674 ret = 1;
1675 else
1676 ret = 0;
1677
1678out:
1679 return ret;
1680}
1681
1682/*
1683 * Helper function to lookup a dir item in a dir.
1684 */
1685static int lookup_dir_item_inode(struct btrfs_root *root,
1686 u64 dir, const char *name, int name_len,
1687 u64 *found_inode,
1688 u8 *found_type)
1689{
1690 int ret = 0;
1691 struct btrfs_dir_item *di;
1692 struct btrfs_key key;
1693 struct btrfs_path *path;
1694
1695 path = alloc_path_for_send();
1696 if (!path)
1697 return -ENOMEM;
1698
1699 di = btrfs_lookup_dir_item(NULL, root, path,
1700 dir, name, name_len, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08001701 if (IS_ERR_OR_NULL(di)) {
1702 ret = di ? PTR_ERR(di) : -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02001703 goto out;
1704 }
1705 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001706 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1707 ret = -ENOENT;
1708 goto out;
1709 }
Alexander Block31db9f72012-07-25 23:19:24 +02001710 *found_inode = key.objectid;
1711 *found_type = btrfs_dir_type(path->nodes[0], di);
1712
1713out:
1714 btrfs_free_path(path);
1715 return ret;
1716}
1717
Alexander Block766702e2012-07-28 14:11:31 +02001718/*
1719 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1720 * generation of the parent dir and the name of the dir entry.
1721 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001722static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001723 u64 *dir, u64 *dir_gen, struct fs_path *name)
1724{
1725 int ret;
1726 struct btrfs_key key;
1727 struct btrfs_key found_key;
1728 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001729 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001730 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001731
1732 path = alloc_path_for_send();
1733 if (!path)
1734 return -ENOMEM;
1735
1736 key.objectid = ino;
1737 key.type = BTRFS_INODE_REF_KEY;
1738 key.offset = 0;
1739
1740 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1741 if (ret < 0)
1742 goto out;
1743 if (!ret)
1744 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1745 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001746 if (ret || found_key.objectid != ino ||
1747 (found_key.type != BTRFS_INODE_REF_KEY &&
1748 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001749 ret = -ENOENT;
1750 goto out;
1751 }
1752
Filipe Manana51a60252014-05-13 22:01:02 +01001753 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001754 struct btrfs_inode_ref *iref;
1755 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1756 struct btrfs_inode_ref);
1757 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1758 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1759 (unsigned long)(iref + 1),
1760 len);
1761 parent_dir = found_key.offset;
1762 } else {
1763 struct btrfs_inode_extref *extref;
1764 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1765 struct btrfs_inode_extref);
1766 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1767 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1768 (unsigned long)&extref->name, len);
1769 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1770 }
Alexander Block31db9f72012-07-25 23:19:24 +02001771 if (ret < 0)
1772 goto out;
1773 btrfs_release_path(path);
1774
Filipe Mananab46ab972014-03-21 12:46:54 +00001775 if (dir_gen) {
1776 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1777 NULL, NULL, NULL);
1778 if (ret < 0)
1779 goto out;
1780 }
Alexander Block31db9f72012-07-25 23:19:24 +02001781
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001782 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001783
1784out:
1785 btrfs_free_path(path);
1786 return ret;
1787}
1788
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001789static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001790 u64 ino, u64 dir,
1791 const char *name, int name_len)
1792{
1793 int ret;
1794 struct fs_path *tmp_name;
1795 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001796
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001797 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001798 if (!tmp_name)
1799 return -ENOMEM;
1800
Filipe Mananab46ab972014-03-21 12:46:54 +00001801 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001802 if (ret < 0)
1803 goto out;
1804
Alexander Blockb9291af2012-07-28 11:07:18 +02001805 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001806 ret = 0;
1807 goto out;
1808 }
1809
Alexander Blocke938c8a2012-07-28 16:33:49 +02001810 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001811
1812out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001813 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001814 return ret;
1815}
1816
Alexander Block766702e2012-07-28 14:11:31 +02001817/*
1818 * Used by process_recorded_refs to determine if a new ref would overwrite an
1819 * already existing ref. In case it detects an overwrite, it returns the
1820 * inode/gen in who_ino/who_gen.
1821 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1822 * to make sure later references to the overwritten inode are possible.
1823 * Orphanizing is however only required for the first ref of an inode.
1824 * process_recorded_refs does an additional is_first_ref check to see if
1825 * orphanizing is really required.
1826 */
Alexander Block31db9f72012-07-25 23:19:24 +02001827static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1828 const char *name, int name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01001829 u64 *who_ino, u64 *who_gen, u64 *who_mode)
Alexander Block31db9f72012-07-25 23:19:24 +02001830{
1831 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001832 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001833 u64 other_inode = 0;
1834 u8 other_type = 0;
1835
1836 if (!sctx->parent_root)
1837 goto out;
1838
1839 ret = is_inode_existent(sctx, dir, dir_gen);
1840 if (ret <= 0)
1841 goto out;
1842
Josef Bacikebdad912013-08-06 16:47:48 -04001843 /*
1844 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001845 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001846 * and we can just unlink this entry.
1847 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001848 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001849 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1850 NULL, NULL, NULL);
1851 if (ret < 0 && ret != -ENOENT)
1852 goto out;
1853 if (ret) {
1854 ret = 0;
1855 goto out;
1856 }
1857 if (gen != dir_gen)
1858 goto out;
1859 }
1860
Alexander Block31db9f72012-07-25 23:19:24 +02001861 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1862 &other_inode, &other_type);
1863 if (ret < 0 && ret != -ENOENT)
1864 goto out;
1865 if (ret) {
1866 ret = 0;
1867 goto out;
1868 }
1869
Alexander Block766702e2012-07-28 14:11:31 +02001870 /*
1871 * Check if the overwritten ref was already processed. If yes, the ref
1872 * was already unlinked/moved, so we can safely assume that we will not
1873 * overwrite anything at this point in time.
1874 */
Robbie Ko801bec32015-06-23 18:39:46 +08001875 if (other_inode > sctx->send_progress ||
1876 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001877 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Filipe Mananaf5962782017-06-22 20:03:51 +01001878 who_gen, who_mode, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001879 if (ret < 0)
1880 goto out;
1881
1882 ret = 1;
1883 *who_ino = other_inode;
1884 } else {
1885 ret = 0;
1886 }
1887
1888out:
1889 return ret;
1890}
1891
Alexander Block766702e2012-07-28 14:11:31 +02001892/*
1893 * Checks if the ref was overwritten by an already processed inode. This is
1894 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1895 * thus the orphan name needs be used.
1896 * process_recorded_refs also uses it to avoid unlinking of refs that were
1897 * overwritten.
1898 */
Alexander Block31db9f72012-07-25 23:19:24 +02001899static int did_overwrite_ref(struct send_ctx *sctx,
1900 u64 dir, u64 dir_gen,
1901 u64 ino, u64 ino_gen,
1902 const char *name, int name_len)
1903{
1904 int ret = 0;
1905 u64 gen;
1906 u64 ow_inode;
1907 u8 other_type;
1908
1909 if (!sctx->parent_root)
1910 goto out;
1911
1912 ret = is_inode_existent(sctx, dir, dir_gen);
1913 if (ret <= 0)
1914 goto out;
1915
Robbie Ko01914102017-01-05 16:24:58 +08001916 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1917 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1918 NULL, NULL, NULL);
1919 if (ret < 0 && ret != -ENOENT)
1920 goto out;
1921 if (ret) {
1922 ret = 0;
1923 goto out;
1924 }
1925 if (gen != dir_gen)
1926 goto out;
1927 }
1928
Alexander Block31db9f72012-07-25 23:19:24 +02001929 /* check if the ref was overwritten by another ref */
1930 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1931 &ow_inode, &other_type);
1932 if (ret < 0 && ret != -ENOENT)
1933 goto out;
1934 if (ret) {
1935 /* was never and will never be overwritten */
1936 ret = 0;
1937 goto out;
1938 }
1939
1940 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001941 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001942 if (ret < 0)
1943 goto out;
1944
1945 if (ow_inode == ino && gen == ino_gen) {
1946 ret = 0;
1947 goto out;
1948 }
1949
Filipe Manana8b191a62015-04-09 14:09:14 +01001950 /*
1951 * We know that it is or will be overwritten. Check this now.
1952 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001953 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1954 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001955 */
Filipe Mananab786f162015-09-26 15:30:23 +01001956 if ((ow_inode < sctx->send_progress) ||
1957 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1958 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001959 ret = 1;
1960 else
1961 ret = 0;
1962
1963out:
1964 return ret;
1965}
1966
Alexander Block766702e2012-07-28 14:11:31 +02001967/*
1968 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1969 * that got overwritten. This is used by process_recorded_refs to determine
1970 * if it has to use the path as returned by get_cur_path or the orphan name.
1971 */
Alexander Block31db9f72012-07-25 23:19:24 +02001972static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1973{
1974 int ret = 0;
1975 struct fs_path *name = NULL;
1976 u64 dir;
1977 u64 dir_gen;
1978
1979 if (!sctx->parent_root)
1980 goto out;
1981
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001982 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001983 if (!name)
1984 return -ENOMEM;
1985
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001986 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001987 if (ret < 0)
1988 goto out;
1989
1990 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1991 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001992
1993out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001994 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001995 return ret;
1996}
1997
Alexander Block766702e2012-07-28 14:11:31 +02001998/*
1999 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2000 * so we need to do some special handling in case we have clashes. This function
2001 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002002 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002003 */
Alexander Block31db9f72012-07-25 23:19:24 +02002004static int name_cache_insert(struct send_ctx *sctx,
2005 struct name_cache_entry *nce)
2006{
2007 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002008 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002009
Alexander Block7e0926f2012-07-28 14:20:58 +02002010 nce_head = radix_tree_lookup(&sctx->name_cache,
2011 (unsigned long)nce->ino);
2012 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002013 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002014 if (!nce_head) {
2015 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002016 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002017 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002018 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002019
Alexander Block7e0926f2012-07-28 14:20:58 +02002020 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002021 if (ret < 0) {
2022 kfree(nce_head);
2023 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002024 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002025 }
Alexander Block31db9f72012-07-25 23:19:24 +02002026 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002027 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002028 list_add_tail(&nce->list, &sctx->name_cache_list);
2029 sctx->name_cache_size++;
2030
2031 return ret;
2032}
2033
2034static void name_cache_delete(struct send_ctx *sctx,
2035 struct name_cache_entry *nce)
2036{
Alexander Block7e0926f2012-07-28 14:20:58 +02002037 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002038
Alexander Block7e0926f2012-07-28 14:20:58 +02002039 nce_head = radix_tree_lookup(&sctx->name_cache,
2040 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002041 if (!nce_head) {
2042 btrfs_err(sctx->send_root->fs_info,
2043 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2044 nce->ino, sctx->name_cache_size);
2045 }
Alexander Block31db9f72012-07-25 23:19:24 +02002046
Alexander Block7e0926f2012-07-28 14:20:58 +02002047 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002048 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002049 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002050
David Sterba57fb8912014-02-03 19:24:40 +01002051 /*
2052 * We may not get to the final release of nce_head if the lookup fails
2053 */
2054 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002055 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2056 kfree(nce_head);
2057 }
Alexander Block31db9f72012-07-25 23:19:24 +02002058}
2059
2060static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2061 u64 ino, u64 gen)
2062{
Alexander Block7e0926f2012-07-28 14:20:58 +02002063 struct list_head *nce_head;
2064 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002065
Alexander Block7e0926f2012-07-28 14:20:58 +02002066 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2067 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002068 return NULL;
2069
Alexander Block7e0926f2012-07-28 14:20:58 +02002070 list_for_each_entry(cur, nce_head, radix_list) {
2071 if (cur->ino == ino && cur->gen == gen)
2072 return cur;
2073 }
Alexander Block31db9f72012-07-25 23:19:24 +02002074 return NULL;
2075}
2076
Alexander Block766702e2012-07-28 14:11:31 +02002077/*
2078 * Removes the entry from the list and adds it back to the end. This marks the
2079 * entry as recently used so that name_cache_clean_unused does not remove it.
2080 */
Alexander Block31db9f72012-07-25 23:19:24 +02002081static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2082{
2083 list_del(&nce->list);
2084 list_add_tail(&nce->list, &sctx->name_cache_list);
2085}
2086
Alexander Block766702e2012-07-28 14:11:31 +02002087/*
2088 * Remove some entries from the beginning of name_cache_list.
2089 */
Alexander Block31db9f72012-07-25 23:19:24 +02002090static void name_cache_clean_unused(struct send_ctx *sctx)
2091{
2092 struct name_cache_entry *nce;
2093
2094 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2095 return;
2096
2097 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2098 nce = list_entry(sctx->name_cache_list.next,
2099 struct name_cache_entry, list);
2100 name_cache_delete(sctx, nce);
2101 kfree(nce);
2102 }
2103}
2104
2105static void name_cache_free(struct send_ctx *sctx)
2106{
2107 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002108
Alexander Blocke938c8a2012-07-28 16:33:49 +02002109 while (!list_empty(&sctx->name_cache_list)) {
2110 nce = list_entry(sctx->name_cache_list.next,
2111 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002112 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002113 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002114 }
2115}
2116
Alexander Block766702e2012-07-28 14:11:31 +02002117/*
2118 * Used by get_cur_path for each ref up to the root.
2119 * Returns 0 if it succeeded.
2120 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2121 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2122 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2123 * Returns <0 in case of error.
2124 */
Alexander Block31db9f72012-07-25 23:19:24 +02002125static int __get_cur_name_and_parent(struct send_ctx *sctx,
2126 u64 ino, u64 gen,
2127 u64 *parent_ino,
2128 u64 *parent_gen,
2129 struct fs_path *dest)
2130{
2131 int ret;
2132 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002133 struct name_cache_entry *nce = NULL;
2134
Alexander Block766702e2012-07-28 14:11:31 +02002135 /*
2136 * First check if we already did a call to this function with the same
2137 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2138 * return the cached result.
2139 */
Alexander Block31db9f72012-07-25 23:19:24 +02002140 nce = name_cache_search(sctx, ino, gen);
2141 if (nce) {
2142 if (ino < sctx->send_progress && nce->need_later_update) {
2143 name_cache_delete(sctx, nce);
2144 kfree(nce);
2145 nce = NULL;
2146 } else {
2147 name_cache_used(sctx, nce);
2148 *parent_ino = nce->parent_ino;
2149 *parent_gen = nce->parent_gen;
2150 ret = fs_path_add(dest, nce->name, nce->name_len);
2151 if (ret < 0)
2152 goto out;
2153 ret = nce->ret;
2154 goto out;
2155 }
2156 }
2157
Alexander Block766702e2012-07-28 14:11:31 +02002158 /*
2159 * If the inode is not existent yet, add the orphan name and return 1.
2160 * This should only happen for the parent dir that we determine in
2161 * __record_new_ref
2162 */
Alexander Block31db9f72012-07-25 23:19:24 +02002163 ret = is_inode_existent(sctx, ino, gen);
2164 if (ret < 0)
2165 goto out;
2166
2167 if (!ret) {
2168 ret = gen_unique_name(sctx, ino, gen, dest);
2169 if (ret < 0)
2170 goto out;
2171 ret = 1;
2172 goto out_cache;
2173 }
2174
Alexander Block766702e2012-07-28 14:11:31 +02002175 /*
2176 * Depending on whether the inode was already processed or not, use
2177 * send_root or parent_root for ref lookup.
2178 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002179 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002180 ret = get_first_ref(sctx->send_root, ino,
2181 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002182 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002183 ret = get_first_ref(sctx->parent_root, ino,
2184 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002185 if (ret < 0)
2186 goto out;
2187
Alexander Block766702e2012-07-28 14:11:31 +02002188 /*
2189 * Check if the ref was overwritten by an inode's ref that was processed
2190 * earlier. If yes, treat as orphan and return 1.
2191 */
Alexander Block31db9f72012-07-25 23:19:24 +02002192 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2193 dest->start, dest->end - dest->start);
2194 if (ret < 0)
2195 goto out;
2196 if (ret) {
2197 fs_path_reset(dest);
2198 ret = gen_unique_name(sctx, ino, gen, dest);
2199 if (ret < 0)
2200 goto out;
2201 ret = 1;
2202 }
2203
2204out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002205 /*
2206 * Store the result of the lookup in the name cache.
2207 */
David Sterbae780b0d2016-01-18 18:42:13 +01002208 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002209 if (!nce) {
2210 ret = -ENOMEM;
2211 goto out;
2212 }
2213
2214 nce->ino = ino;
2215 nce->gen = gen;
2216 nce->parent_ino = *parent_ino;
2217 nce->parent_gen = *parent_gen;
2218 nce->name_len = fs_path_len(dest);
2219 nce->ret = ret;
2220 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002221
2222 if (ino < sctx->send_progress)
2223 nce->need_later_update = 0;
2224 else
2225 nce->need_later_update = 1;
2226
2227 nce_ret = name_cache_insert(sctx, nce);
2228 if (nce_ret < 0)
2229 ret = nce_ret;
2230 name_cache_clean_unused(sctx);
2231
2232out:
Alexander Block31db9f72012-07-25 23:19:24 +02002233 return ret;
2234}
2235
2236/*
2237 * Magic happens here. This function returns the first ref to an inode as it
2238 * would look like while receiving the stream at this point in time.
2239 * We walk the path up to the root. For every inode in between, we check if it
2240 * was already processed/sent. If yes, we continue with the parent as found
2241 * in send_root. If not, we continue with the parent as found in parent_root.
2242 * If we encounter an inode that was deleted at this point in time, we use the
2243 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2244 * that were not created yet and overwritten inodes/refs.
2245 *
Andrea Gelmini52042d82018-11-28 12:05:13 +01002246 * When do we have orphan inodes:
Alexander Block31db9f72012-07-25 23:19:24 +02002247 * 1. When an inode is freshly created and thus no valid refs are available yet
2248 * 2. When a directory lost all it's refs (deleted) but still has dir items
2249 * inside which were not processed yet (pending for move/delete). If anyone
2250 * tried to get the path to the dir items, it would get a path inside that
2251 * orphan directory.
2252 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2253 * of an unprocessed inode. If in that case the first ref would be
2254 * overwritten, the overwritten inode gets "orphanized". Later when we
2255 * process this overwritten inode, it is restored at a new place by moving
2256 * the orphan inode.
2257 *
2258 * sctx->send_progress tells this function at which point in time receiving
2259 * would be.
2260 */
2261static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2262 struct fs_path *dest)
2263{
2264 int ret = 0;
2265 struct fs_path *name = NULL;
2266 u64 parent_inode = 0;
2267 u64 parent_gen = 0;
2268 int stop = 0;
2269
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002270 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002271 if (!name) {
2272 ret = -ENOMEM;
2273 goto out;
2274 }
2275
2276 dest->reversed = 1;
2277 fs_path_reset(dest);
2278
2279 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002280 struct waiting_dir_move *wdm;
2281
Alexander Block31db9f72012-07-25 23:19:24 +02002282 fs_path_reset(name);
2283
Filipe Manana9dc44212014-02-19 14:31:44 +00002284 if (is_waiting_for_rm(sctx, ino)) {
2285 ret = gen_unique_name(sctx, ino, gen, name);
2286 if (ret < 0)
2287 goto out;
2288 ret = fs_path_add_path(dest, name);
2289 break;
2290 }
2291
Filipe Manana8b191a62015-04-09 14:09:14 +01002292 wdm = get_waiting_dir_move(sctx, ino);
2293 if (wdm && wdm->orphanized) {
2294 ret = gen_unique_name(sctx, ino, gen, name);
2295 stop = 1;
2296 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002297 ret = get_first_ref(sctx->parent_root, ino,
2298 &parent_inode, &parent_gen, name);
2299 } else {
2300 ret = __get_cur_name_and_parent(sctx, ino, gen,
2301 &parent_inode,
2302 &parent_gen, name);
2303 if (ret)
2304 stop = 1;
2305 }
2306
Alexander Block31db9f72012-07-25 23:19:24 +02002307 if (ret < 0)
2308 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002309
Alexander Block31db9f72012-07-25 23:19:24 +02002310 ret = fs_path_add_path(dest, name);
2311 if (ret < 0)
2312 goto out;
2313
2314 ino = parent_inode;
2315 gen = parent_gen;
2316 }
2317
2318out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002319 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002320 if (!ret)
2321 fs_path_unreverse(dest);
2322 return ret;
2323}
2324
2325/*
Alexander Block31db9f72012-07-25 23:19:24 +02002326 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2327 */
2328static int send_subvol_begin(struct send_ctx *sctx)
2329{
2330 int ret;
2331 struct btrfs_root *send_root = sctx->send_root;
2332 struct btrfs_root *parent_root = sctx->parent_root;
2333 struct btrfs_path *path;
2334 struct btrfs_key key;
2335 struct btrfs_root_ref *ref;
2336 struct extent_buffer *leaf;
2337 char *name = NULL;
2338 int namelen;
2339
Wang Shilongffcfaf82014-01-15 00:26:43 +08002340 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002341 if (!path)
2342 return -ENOMEM;
2343
David Sterbae780b0d2016-01-18 18:42:13 +01002344 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002345 if (!name) {
2346 btrfs_free_path(path);
2347 return -ENOMEM;
2348 }
2349
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002350 key.objectid = send_root->root_key.objectid;
Alexander Block31db9f72012-07-25 23:19:24 +02002351 key.type = BTRFS_ROOT_BACKREF_KEY;
2352 key.offset = 0;
2353
2354 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2355 &key, path, 1, 0);
2356 if (ret < 0)
2357 goto out;
2358 if (ret) {
2359 ret = -ENOENT;
2360 goto out;
2361 }
2362
2363 leaf = path->nodes[0];
2364 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2365 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002366 key.objectid != send_root->root_key.objectid) {
Alexander Block31db9f72012-07-25 23:19:24 +02002367 ret = -ENOENT;
2368 goto out;
2369 }
2370 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2371 namelen = btrfs_root_ref_name_len(leaf, ref);
2372 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2373 btrfs_release_path(path);
2374
Alexander Block31db9f72012-07-25 23:19:24 +02002375 if (parent_root) {
2376 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2377 if (ret < 0)
2378 goto out;
2379 } else {
2380 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2381 if (ret < 0)
2382 goto out;
2383 }
2384
2385 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002386
2387 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2388 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2389 sctx->send_root->root_item.received_uuid);
2390 else
2391 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2392 sctx->send_root->root_item.uuid);
2393
Alexander Block31db9f72012-07-25 23:19:24 +02002394 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002395 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002396 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002397 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2398 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2399 parent_root->root_item.received_uuid);
2400 else
2401 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2402 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002403 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002404 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002405 }
2406
2407 ret = send_cmd(sctx);
2408
2409tlv_put_failure:
2410out:
2411 btrfs_free_path(path);
2412 kfree(name);
2413 return ret;
2414}
2415
2416static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2417{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002418 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002419 int ret = 0;
2420 struct fs_path *p;
2421
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002422 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002423
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002424 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002425 if (!p)
2426 return -ENOMEM;
2427
2428 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2429 if (ret < 0)
2430 goto out;
2431
2432 ret = get_cur_path(sctx, ino, gen, p);
2433 if (ret < 0)
2434 goto out;
2435 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2436 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2437
2438 ret = send_cmd(sctx);
2439
2440tlv_put_failure:
2441out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002442 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002443 return ret;
2444}
2445
2446static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2447{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002448 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002449 int ret = 0;
2450 struct fs_path *p;
2451
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002452 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002453
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002454 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002455 if (!p)
2456 return -ENOMEM;
2457
2458 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2459 if (ret < 0)
2460 goto out;
2461
2462 ret = get_cur_path(sctx, ino, gen, p);
2463 if (ret < 0)
2464 goto out;
2465 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2466 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2467
2468 ret = send_cmd(sctx);
2469
2470tlv_put_failure:
2471out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002472 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002473 return ret;
2474}
2475
2476static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2477{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002478 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002479 int ret = 0;
2480 struct fs_path *p;
2481
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002482 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2483 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002484
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002485 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002486 if (!p)
2487 return -ENOMEM;
2488
2489 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2490 if (ret < 0)
2491 goto out;
2492
2493 ret = get_cur_path(sctx, ino, gen, p);
2494 if (ret < 0)
2495 goto out;
2496 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2497 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2498 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2499
2500 ret = send_cmd(sctx);
2501
2502tlv_put_failure:
2503out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002504 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002505 return ret;
2506}
2507
2508static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2509{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002510 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002511 int ret = 0;
2512 struct fs_path *p = NULL;
2513 struct btrfs_inode_item *ii;
2514 struct btrfs_path *path = NULL;
2515 struct extent_buffer *eb;
2516 struct btrfs_key key;
2517 int slot;
2518
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002519 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002520
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002521 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002522 if (!p)
2523 return -ENOMEM;
2524
2525 path = alloc_path_for_send();
2526 if (!path) {
2527 ret = -ENOMEM;
2528 goto out;
2529 }
2530
2531 key.objectid = ino;
2532 key.type = BTRFS_INODE_ITEM_KEY;
2533 key.offset = 0;
2534 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002535 if (ret > 0)
2536 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002537 if (ret < 0)
2538 goto out;
2539
2540 eb = path->nodes[0];
2541 slot = path->slots[0];
2542 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2543
2544 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2545 if (ret < 0)
2546 goto out;
2547
2548 ret = get_cur_path(sctx, ino, gen, p);
2549 if (ret < 0)
2550 goto out;
2551 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002552 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2553 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2554 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002555 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002556
2557 ret = send_cmd(sctx);
2558
2559tlv_put_failure:
2560out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002561 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002562 btrfs_free_path(path);
2563 return ret;
2564}
2565
2566/*
2567 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2568 * a valid path yet because we did not process the refs yet. So, the inode
2569 * is created as orphan.
2570 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002571static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002572{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002573 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002574 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002575 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002576 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002577 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002578 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002579 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002580
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002581 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002582
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002583 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002584 if (!p)
2585 return -ENOMEM;
2586
Liu Bo644d1942014-02-27 17:29:01 +08002587 if (ino != sctx->cur_ino) {
2588 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2589 NULL, NULL, &rdev);
2590 if (ret < 0)
2591 goto out;
2592 } else {
2593 gen = sctx->cur_inode_gen;
2594 mode = sctx->cur_inode_mode;
2595 rdev = sctx->cur_inode_rdev;
2596 }
Alexander Block31db9f72012-07-25 23:19:24 +02002597
Alexander Blocke938c8a2012-07-28 16:33:49 +02002598 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002599 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002600 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002601 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002602 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002603 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002604 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002605 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002606 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002607 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002608 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002609 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002610 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002611 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002612 (int)(mode & S_IFMT));
Tsutomu Itohca6842b2016-01-22 09:13:25 +09002613 ret = -EOPNOTSUPP;
Alexander Block31db9f72012-07-25 23:19:24 +02002614 goto out;
2615 }
2616
2617 ret = begin_cmd(sctx, cmd);
2618 if (ret < 0)
2619 goto out;
2620
Alexander Block1f4692d2012-07-28 10:42:24 +02002621 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002622 if (ret < 0)
2623 goto out;
2624
2625 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002626 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002627
2628 if (S_ISLNK(mode)) {
2629 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002630 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002631 if (ret < 0)
2632 goto out;
2633 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2634 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2635 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002636 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2637 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002638 }
2639
2640 ret = send_cmd(sctx);
2641 if (ret < 0)
2642 goto out;
2643
2644
2645tlv_put_failure:
2646out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002647 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002648 return ret;
2649}
2650
Alexander Block1f4692d2012-07-28 10:42:24 +02002651/*
2652 * We need some special handling for inodes that get processed before the parent
2653 * directory got created. See process_recorded_refs for details.
2654 * This function does the check if we already created the dir out of order.
2655 */
2656static int did_create_dir(struct send_ctx *sctx, u64 dir)
2657{
2658 int ret = 0;
2659 struct btrfs_path *path = NULL;
2660 struct btrfs_key key;
2661 struct btrfs_key found_key;
2662 struct btrfs_key di_key;
2663 struct extent_buffer *eb;
2664 struct btrfs_dir_item *di;
2665 int slot;
2666
2667 path = alloc_path_for_send();
2668 if (!path) {
2669 ret = -ENOMEM;
2670 goto out;
2671 }
2672
2673 key.objectid = dir;
2674 key.type = BTRFS_DIR_INDEX_KEY;
2675 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002676 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2677 if (ret < 0)
2678 goto out;
2679
Alexander Block1f4692d2012-07-28 10:42:24 +02002680 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002681 eb = path->nodes[0];
2682 slot = path->slots[0];
2683 if (slot >= btrfs_header_nritems(eb)) {
2684 ret = btrfs_next_leaf(sctx->send_root, path);
2685 if (ret < 0) {
2686 goto out;
2687 } else if (ret > 0) {
2688 ret = 0;
2689 break;
2690 }
2691 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002692 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002693
2694 btrfs_item_key_to_cpu(eb, &found_key, slot);
2695 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002696 found_key.type != key.type) {
2697 ret = 0;
2698 goto out;
2699 }
2700
2701 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2702 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2703
Josef Bacika0525412013-08-12 10:56:14 -04002704 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2705 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002706 ret = 1;
2707 goto out;
2708 }
2709
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002710 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002711 }
2712
2713out:
2714 btrfs_free_path(path);
2715 return ret;
2716}
2717
2718/*
2719 * Only creates the inode if it is:
2720 * 1. Not a directory
2721 * 2. Or a directory which was not created already due to out of order
2722 * directories. See did_create_dir and process_recorded_refs for details.
2723 */
2724static int send_create_inode_if_needed(struct send_ctx *sctx)
2725{
2726 int ret;
2727
2728 if (S_ISDIR(sctx->cur_inode_mode)) {
2729 ret = did_create_dir(sctx, sctx->cur_ino);
2730 if (ret < 0)
2731 goto out;
2732 if (ret) {
2733 ret = 0;
2734 goto out;
2735 }
2736 }
2737
2738 ret = send_create_inode(sctx, sctx->cur_ino);
2739 if (ret < 0)
2740 goto out;
2741
2742out:
2743 return ret;
2744}
2745
Alexander Block31db9f72012-07-25 23:19:24 +02002746struct recorded_ref {
2747 struct list_head list;
Alexander Block31db9f72012-07-25 23:19:24 +02002748 char *name;
2749 struct fs_path *full_path;
2750 u64 dir;
2751 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002752 int name_len;
2753};
2754
Filipe Mananafdb13882017-06-13 14:13:11 +01002755static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2756{
2757 ref->full_path = path;
2758 ref->name = (char *)kbasename(ref->full_path->start);
2759 ref->name_len = ref->full_path->end - ref->name;
2760}
2761
Alexander Block31db9f72012-07-25 23:19:24 +02002762/*
2763 * We need to process new refs before deleted refs, but compare_tree gives us
2764 * everything mixed. So we first record all refs and later process them.
2765 * This function is a helper to record one ref.
2766 */
Liu Boa4d96d62014-03-03 21:31:03 +08002767static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002768 u64 dir_gen, struct fs_path *path)
2769{
2770 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002771
David Sterbae780b0d2016-01-18 18:42:13 +01002772 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002773 if (!ref)
2774 return -ENOMEM;
2775
2776 ref->dir = dir;
2777 ref->dir_gen = dir_gen;
Filipe Mananafdb13882017-06-13 14:13:11 +01002778 set_ref_path(ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02002779 list_add_tail(&ref->list, head);
2780 return 0;
2781}
2782
Josef Bacikba5e8f22013-08-16 16:52:55 -04002783static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2784{
2785 struct recorded_ref *new;
2786
David Sterbae780b0d2016-01-18 18:42:13 +01002787 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002788 if (!new)
2789 return -ENOMEM;
2790
2791 new->dir = ref->dir;
2792 new->dir_gen = ref->dir_gen;
2793 new->full_path = NULL;
2794 INIT_LIST_HEAD(&new->list);
2795 list_add_tail(&new->list, list);
2796 return 0;
2797}
2798
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002799static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002800{
2801 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002802
Alexander Blocke938c8a2012-07-28 16:33:49 +02002803 while (!list_empty(head)) {
2804 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002805 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002806 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002807 kfree(cur);
2808 }
Alexander Block31db9f72012-07-25 23:19:24 +02002809}
2810
2811static void free_recorded_refs(struct send_ctx *sctx)
2812{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002813 __free_recorded_refs(&sctx->new_refs);
2814 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002815}
2816
2817/*
Alexander Block766702e2012-07-28 14:11:31 +02002818 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002819 * ref of an unprocessed inode gets overwritten and for all non empty
2820 * directories.
2821 */
2822static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2823 struct fs_path *path)
2824{
2825 int ret;
2826 struct fs_path *orphan;
2827
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002828 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002829 if (!orphan)
2830 return -ENOMEM;
2831
2832 ret = gen_unique_name(sctx, ino, gen, orphan);
2833 if (ret < 0)
2834 goto out;
2835
2836 ret = send_rename(sctx, path, orphan);
2837
2838out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002839 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002840 return ret;
2841}
2842
Filipe Manana9dc44212014-02-19 14:31:44 +00002843static struct orphan_dir_info *
2844add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2845{
2846 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2847 struct rb_node *parent = NULL;
2848 struct orphan_dir_info *entry, *odi;
2849
Filipe Manana9dc44212014-02-19 14:31:44 +00002850 while (*p) {
2851 parent = *p;
2852 entry = rb_entry(parent, struct orphan_dir_info, node);
2853 if (dir_ino < entry->ino) {
2854 p = &(*p)->rb_left;
2855 } else if (dir_ino > entry->ino) {
2856 p = &(*p)->rb_right;
2857 } else {
Filipe Manana9dc44212014-02-19 14:31:44 +00002858 return entry;
2859 }
2860 }
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;
2866 odi->gen = 0;
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
2874static struct orphan_dir_info *
2875get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2876{
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;
2886 else
2887 return entry;
2888 }
2889 return NULL;
2890}
2891
2892static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2893{
2894 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2895
2896 return odi != NULL;
2897}
2898
2899static void free_orphan_dir_info(struct send_ctx *sctx,
2900 struct orphan_dir_info *odi)
2901{
2902 if (!odi)
2903 return;
2904 rb_erase(&odi->node, &sctx->orphan_dirs);
2905 kfree(odi);
2906}
2907
Alexander Block31db9f72012-07-25 23:19:24 +02002908/*
2909 * Returns 1 if a directory can be removed at this point in time.
2910 * We check this by iterating all dir items and checking if the inode behind
2911 * the dir item was already processed.
2912 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002913static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2914 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002915{
2916 int ret = 0;
2917 struct btrfs_root *root = sctx->parent_root;
2918 struct btrfs_path *path;
2919 struct btrfs_key key;
2920 struct btrfs_key found_key;
2921 struct btrfs_key loc;
2922 struct btrfs_dir_item *di;
Robbie Ko0f96f512018-05-08 18:11:38 +08002923 struct orphan_dir_info *odi = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02002924
Alexander Block6d85ed052012-08-01 14:48:59 +02002925 /*
2926 * Don't try to rmdir the top/root subvolume dir.
2927 */
2928 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2929 return 0;
2930
Alexander Block31db9f72012-07-25 23:19:24 +02002931 path = alloc_path_for_send();
2932 if (!path)
2933 return -ENOMEM;
2934
2935 key.objectid = dir;
2936 key.type = BTRFS_DIR_INDEX_KEY;
2937 key.offset = 0;
Robbie Ko0f96f512018-05-08 18:11:38 +08002938
2939 odi = get_orphan_dir_info(sctx, dir);
2940 if (odi)
2941 key.offset = odi->last_dir_index_offset;
2942
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002943 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2944 if (ret < 0)
2945 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002946
2947 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002948 struct waiting_dir_move *dm;
2949
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002950 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2951 ret = btrfs_next_leaf(root, path);
2952 if (ret < 0)
2953 goto out;
2954 else if (ret > 0)
2955 break;
2956 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002957 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002958 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2959 path->slots[0]);
2960 if (found_key.objectid != key.objectid ||
2961 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002962 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002963
2964 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2965 struct btrfs_dir_item);
2966 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2967
Filipe Manana9dc44212014-02-19 14:31:44 +00002968 dm = get_waiting_dir_move(sctx, loc.objectid);
2969 if (dm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002970 odi = add_orphan_dir_info(sctx, dir);
2971 if (IS_ERR(odi)) {
2972 ret = PTR_ERR(odi);
2973 goto out;
2974 }
2975 odi->gen = dir_gen;
Robbie Ko0f96f512018-05-08 18:11:38 +08002976 odi->last_dir_index_offset = found_key.offset;
Filipe Manana9dc44212014-02-19 14:31:44 +00002977 dm->rmdir_ino = dir;
2978 ret = 0;
2979 goto out;
2980 }
2981
Alexander Block31db9f72012-07-25 23:19:24 +02002982 if (loc.objectid > send_progress) {
Robbie Ko0f96f512018-05-08 18:11:38 +08002983 odi = add_orphan_dir_info(sctx, dir);
2984 if (IS_ERR(odi)) {
2985 ret = PTR_ERR(odi);
2986 goto out;
2987 }
2988 odi->gen = dir_gen;
2989 odi->last_dir_index_offset = found_key.offset;
Alexander Block31db9f72012-07-25 23:19:24 +02002990 ret = 0;
2991 goto out;
2992 }
2993
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002994 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002995 }
Robbie Ko0f96f512018-05-08 18:11:38 +08002996 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02002997
2998 ret = 1;
2999
3000out:
3001 btrfs_free_path(path);
3002 return ret;
3003}
3004
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003005static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3006{
Filipe Manana9dc44212014-02-19 14:31:44 +00003007 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003008
Filipe Manana9dc44212014-02-19 14:31:44 +00003009 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003010}
3011
Filipe Manana8b191a62015-04-09 14:09:14 +01003012static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003013{
3014 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3015 struct rb_node *parent = NULL;
3016 struct waiting_dir_move *entry, *dm;
3017
David Sterbae780b0d2016-01-18 18:42:13 +01003018 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003019 if (!dm)
3020 return -ENOMEM;
3021 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003022 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003023 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003024
3025 while (*p) {
3026 parent = *p;
3027 entry = rb_entry(parent, struct waiting_dir_move, node);
3028 if (ino < entry->ino) {
3029 p = &(*p)->rb_left;
3030 } else if (ino > entry->ino) {
3031 p = &(*p)->rb_right;
3032 } else {
3033 kfree(dm);
3034 return -EEXIST;
3035 }
3036 }
3037
3038 rb_link_node(&dm->node, parent, p);
3039 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3040 return 0;
3041}
3042
Filipe Manana9dc44212014-02-19 14:31:44 +00003043static struct waiting_dir_move *
3044get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003045{
3046 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3047 struct waiting_dir_move *entry;
3048
3049 while (n) {
3050 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003051 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003052 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003053 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003054 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003055 else
3056 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003057 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003058 return NULL;
3059}
3060
3061static void free_waiting_dir_move(struct send_ctx *sctx,
3062 struct waiting_dir_move *dm)
3063{
3064 if (!dm)
3065 return;
3066 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3067 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003068}
3069
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003070static int add_pending_dir_move(struct send_ctx *sctx,
3071 u64 ino,
3072 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003073 u64 parent_ino,
3074 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003075 struct list_head *deleted_refs,
3076 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003077{
3078 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3079 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003080 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003081 struct recorded_ref *cur;
3082 int exists = 0;
3083 int ret;
3084
David Sterbae780b0d2016-01-18 18:42:13 +01003085 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003086 if (!pm)
3087 return -ENOMEM;
3088 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003089 pm->ino = ino;
3090 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003091 INIT_LIST_HEAD(&pm->list);
3092 INIT_LIST_HEAD(&pm->update_refs);
3093 RB_CLEAR_NODE(&pm->node);
3094
3095 while (*p) {
3096 parent = *p;
3097 entry = rb_entry(parent, struct pending_dir_move, node);
3098 if (parent_ino < entry->parent_ino) {
3099 p = &(*p)->rb_left;
3100 } else if (parent_ino > entry->parent_ino) {
3101 p = &(*p)->rb_right;
3102 } else {
3103 exists = 1;
3104 break;
3105 }
3106 }
3107
Filipe Mananaf9594922014-03-27 20:14:01 +00003108 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003109 ret = dup_ref(cur, &pm->update_refs);
3110 if (ret < 0)
3111 goto out;
3112 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003113 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003114 ret = dup_ref(cur, &pm->update_refs);
3115 if (ret < 0)
3116 goto out;
3117 }
3118
Filipe Manana8b191a62015-04-09 14:09:14 +01003119 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003120 if (ret)
3121 goto out;
3122
3123 if (exists) {
3124 list_add_tail(&pm->list, &entry->list);
3125 } else {
3126 rb_link_node(&pm->node, parent, p);
3127 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3128 }
3129 ret = 0;
3130out:
3131 if (ret) {
3132 __free_recorded_refs(&pm->update_refs);
3133 kfree(pm);
3134 }
3135 return ret;
3136}
3137
3138static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3139 u64 parent_ino)
3140{
3141 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3142 struct pending_dir_move *entry;
3143
3144 while (n) {
3145 entry = rb_entry(n, struct pending_dir_move, node);
3146 if (parent_ino < entry->parent_ino)
3147 n = n->rb_left;
3148 else if (parent_ino > entry->parent_ino)
3149 n = n->rb_right;
3150 else
3151 return entry;
3152 }
3153 return NULL;
3154}
3155
Robbie Ko801bec32015-06-23 18:39:46 +08003156static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3157 u64 ino, u64 gen, u64 *ancestor_ino)
3158{
3159 int ret = 0;
3160 u64 parent_inode = 0;
3161 u64 parent_gen = 0;
3162 u64 start_ino = ino;
3163
3164 *ancestor_ino = 0;
3165 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3166 fs_path_reset(name);
3167
3168 if (is_waiting_for_rm(sctx, ino))
3169 break;
3170 if (is_waiting_for_move(sctx, ino)) {
3171 if (*ancestor_ino == 0)
3172 *ancestor_ino = ino;
3173 ret = get_first_ref(sctx->parent_root, ino,
3174 &parent_inode, &parent_gen, name);
3175 } else {
3176 ret = __get_cur_name_and_parent(sctx, ino, gen,
3177 &parent_inode,
3178 &parent_gen, name);
3179 if (ret > 0) {
3180 ret = 0;
3181 break;
3182 }
3183 }
3184 if (ret < 0)
3185 break;
3186 if (parent_inode == start_ino) {
3187 ret = 1;
3188 if (*ancestor_ino == 0)
3189 *ancestor_ino = ino;
3190 break;
3191 }
3192 ino = parent_inode;
3193 gen = parent_gen;
3194 }
3195 return ret;
3196}
3197
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003198static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3199{
3200 struct fs_path *from_path = NULL;
3201 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003202 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003203 u64 orig_progress = sctx->send_progress;
3204 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003205 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003206 struct waiting_dir_move *dm = NULL;
3207 u64 rmdir_ino = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003208 u64 ancestor;
3209 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003210 int ret;
3211
Filipe Manana2b863a12014-02-16 13:43:11 +00003212 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003213 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003214 if (!name || !from_path) {
3215 ret = -ENOMEM;
3216 goto out;
3217 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003218
Filipe Manana9dc44212014-02-19 14:31:44 +00003219 dm = get_waiting_dir_move(sctx, pm->ino);
3220 ASSERT(dm);
3221 rmdir_ino = dm->rmdir_ino;
Robbie Ko801bec32015-06-23 18:39:46 +08003222 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003223 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003224
Robbie Ko801bec32015-06-23 18:39:46 +08003225 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003226 ret = gen_unique_name(sctx, pm->ino,
3227 pm->gen, from_path);
3228 } else {
3229 ret = get_first_ref(sctx->parent_root, pm->ino,
3230 &parent_ino, &parent_gen, name);
3231 if (ret < 0)
3232 goto out;
3233 ret = get_cur_path(sctx, parent_ino, parent_gen,
3234 from_path);
3235 if (ret < 0)
3236 goto out;
3237 ret = fs_path_add_path(from_path, name);
3238 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003239 if (ret < 0)
3240 goto out;
3241
Filipe Mananaf9594922014-03-27 20:14:01 +00003242 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003243 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003244 if (ret < 0)
3245 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003246 if (ret) {
3247 LIST_HEAD(deleted_refs);
3248 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3249 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3250 &pm->update_refs, &deleted_refs,
3251 is_orphan);
3252 if (ret < 0)
3253 goto out;
3254 if (rmdir_ino) {
3255 dm = get_waiting_dir_move(sctx, pm->ino);
3256 ASSERT(dm);
3257 dm->rmdir_ino = rmdir_ino;
3258 }
3259 goto out;
3260 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003261 fs_path_reset(name);
3262 to_path = name;
3263 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003264 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3265 if (ret < 0)
3266 goto out;
3267
3268 ret = send_rename(sctx, from_path, to_path);
3269 if (ret < 0)
3270 goto out;
3271
Filipe Manana9dc44212014-02-19 14:31:44 +00003272 if (rmdir_ino) {
3273 struct orphan_dir_info *odi;
Robbie Ko0f96f512018-05-08 18:11:38 +08003274 u64 gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003275
3276 odi = get_orphan_dir_info(sctx, rmdir_ino);
3277 if (!odi) {
3278 /* already deleted */
3279 goto finish;
3280 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003281 gen = odi->gen;
3282
3283 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003284 if (ret < 0)
3285 goto out;
3286 if (!ret)
3287 goto finish;
3288
3289 name = fs_path_alloc();
3290 if (!name) {
3291 ret = -ENOMEM;
3292 goto out;
3293 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003294 ret = get_cur_path(sctx, rmdir_ino, gen, name);
Filipe Manana9dc44212014-02-19 14:31:44 +00003295 if (ret < 0)
3296 goto out;
3297 ret = send_rmdir(sctx, name);
3298 if (ret < 0)
3299 goto out;
Filipe Manana9dc44212014-02-19 14:31:44 +00003300 }
3301
3302finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003303 ret = send_utimes(sctx, pm->ino, pm->gen);
3304 if (ret < 0)
3305 goto out;
3306
3307 /*
3308 * After rename/move, need to update the utimes of both new parent(s)
3309 * and old parent(s).
3310 */
3311 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003312 /*
3313 * The parent inode might have been deleted in the send snapshot
3314 */
3315 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3316 NULL, NULL, NULL, NULL, NULL);
3317 if (ret == -ENOENT) {
3318 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003319 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003320 }
3321 if (ret < 0)
3322 goto out;
3323
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003324 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3325 if (ret < 0)
3326 goto out;
3327 }
3328
3329out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003330 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003331 fs_path_free(from_path);
3332 fs_path_free(to_path);
3333 sctx->send_progress = orig_progress;
3334
3335 return ret;
3336}
3337
3338static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3339{
3340 if (!list_empty(&m->list))
3341 list_del(&m->list);
3342 if (!RB_EMPTY_NODE(&m->node))
3343 rb_erase(&m->node, &sctx->pending_dir_moves);
3344 __free_recorded_refs(&m->update_refs);
3345 kfree(m);
3346}
3347
Robbie Koa4390ae2018-11-14 18:32:37 +00003348static void tail_append_pending_moves(struct send_ctx *sctx,
3349 struct pending_dir_move *moves,
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003350 struct list_head *stack)
3351{
3352 if (list_empty(&moves->list)) {
3353 list_add_tail(&moves->list, stack);
3354 } else {
3355 LIST_HEAD(list);
3356 list_splice_init(&moves->list, &list);
3357 list_add_tail(&moves->list, stack);
3358 list_splice_tail(&list, stack);
3359 }
Robbie Koa4390ae2018-11-14 18:32:37 +00003360 if (!RB_EMPTY_NODE(&moves->node)) {
3361 rb_erase(&moves->node, &sctx->pending_dir_moves);
3362 RB_CLEAR_NODE(&moves->node);
3363 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003364}
3365
3366static int apply_children_dir_moves(struct send_ctx *sctx)
3367{
3368 struct pending_dir_move *pm;
3369 struct list_head stack;
3370 u64 parent_ino = sctx->cur_ino;
3371 int ret = 0;
3372
3373 pm = get_pending_dir_moves(sctx, parent_ino);
3374 if (!pm)
3375 return 0;
3376
3377 INIT_LIST_HEAD(&stack);
Robbie Koa4390ae2018-11-14 18:32:37 +00003378 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003379
3380 while (!list_empty(&stack)) {
3381 pm = list_first_entry(&stack, struct pending_dir_move, list);
3382 parent_ino = pm->ino;
3383 ret = apply_dir_move(sctx, pm);
3384 free_pending_move(sctx, pm);
3385 if (ret)
3386 goto out;
3387 pm = get_pending_dir_moves(sctx, parent_ino);
3388 if (pm)
Robbie Koa4390ae2018-11-14 18:32:37 +00003389 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003390 }
3391 return 0;
3392
3393out:
3394 while (!list_empty(&stack)) {
3395 pm = list_first_entry(&stack, struct pending_dir_move, list);
3396 free_pending_move(sctx, pm);
3397 }
3398 return ret;
3399}
3400
Filipe Manana84471e22015-02-28 22:29:22 +00003401/*
3402 * We might need to delay a directory rename even when no ancestor directory
3403 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3404 * renamed. This happens when we rename a directory to the old name (the name
3405 * in the parent root) of some other unrelated directory that got its rename
3406 * delayed due to some ancestor with higher number that got renamed.
3407 *
3408 * Example:
3409 *
3410 * Parent snapshot:
3411 * . (ino 256)
3412 * |---- a/ (ino 257)
3413 * | |---- file (ino 260)
3414 * |
3415 * |---- b/ (ino 258)
3416 * |---- c/ (ino 259)
3417 *
3418 * Send snapshot:
3419 * . (ino 256)
3420 * |---- a/ (ino 258)
3421 * |---- x/ (ino 259)
3422 * |---- y/ (ino 257)
3423 * |----- file (ino 260)
3424 *
3425 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3426 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3427 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3428 * must issue is:
3429 *
3430 * 1 - rename 259 from 'c' to 'x'
3431 * 2 - rename 257 from 'a' to 'x/y'
3432 * 3 - rename 258 from 'b' to 'a'
3433 *
3434 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3435 * be done right away and < 0 on error.
3436 */
3437static int wait_for_dest_dir_move(struct send_ctx *sctx,
3438 struct recorded_ref *parent_ref,
3439 const bool is_orphan)
3440{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003441 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003442 struct btrfs_path *path;
3443 struct btrfs_key key;
3444 struct btrfs_key di_key;
3445 struct btrfs_dir_item *di;
3446 u64 left_gen;
3447 u64 right_gen;
3448 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003449 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003450
3451 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3452 return 0;
3453
3454 path = alloc_path_for_send();
3455 if (!path)
3456 return -ENOMEM;
3457
3458 key.objectid = parent_ref->dir;
3459 key.type = BTRFS_DIR_ITEM_KEY;
3460 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3461
3462 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3463 if (ret < 0) {
3464 goto out;
3465 } else if (ret > 0) {
3466 ret = 0;
3467 goto out;
3468 }
3469
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003470 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3471 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003472 if (!di) {
3473 ret = 0;
3474 goto out;
3475 }
3476 /*
3477 * di_key.objectid has the number of the inode that has a dentry in the
3478 * parent directory with the same name that sctx->cur_ino is being
3479 * renamed to. We need to check if that inode is in the send root as
3480 * well and if it is currently marked as an inode with a pending rename,
3481 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3482 * that it happens after that other inode is renamed.
3483 */
3484 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3485 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3486 ret = 0;
3487 goto out;
3488 }
3489
3490 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3491 &left_gen, NULL, NULL, NULL, NULL);
3492 if (ret < 0)
3493 goto out;
3494 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3495 &right_gen, NULL, NULL, NULL, NULL);
3496 if (ret < 0) {
3497 if (ret == -ENOENT)
3498 ret = 0;
3499 goto out;
3500 }
3501
3502 /* Different inode, no need to delay the rename of sctx->cur_ino */
3503 if (right_gen != left_gen) {
3504 ret = 0;
3505 goto out;
3506 }
3507
Robbie Ko801bec32015-06-23 18:39:46 +08003508 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3509 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003510 ret = add_pending_dir_move(sctx,
3511 sctx->cur_ino,
3512 sctx->cur_inode_gen,
3513 di_key.objectid,
3514 &sctx->new_refs,
3515 &sctx->deleted_refs,
3516 is_orphan);
3517 if (!ret)
3518 ret = 1;
3519 }
3520out:
3521 btrfs_free_path(path);
3522 return ret;
3523}
3524
Filipe Manana80aa6022015-03-27 17:50:45 +00003525/*
Filipe Mananaea37d592017-11-17 01:54:00 +00003526 * Check if inode ino2, or any of its ancestors, is inode ino1.
3527 * Return 1 if true, 0 if false and < 0 on error.
3528 */
3529static int check_ino_in_path(struct btrfs_root *root,
3530 const u64 ino1,
3531 const u64 ino1_gen,
3532 const u64 ino2,
3533 const u64 ino2_gen,
3534 struct fs_path *fs_path)
3535{
3536 u64 ino = ino2;
3537
3538 if (ino1 == ino2)
3539 return ino1_gen == ino2_gen;
3540
3541 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3542 u64 parent;
3543 u64 parent_gen;
3544 int ret;
3545
3546 fs_path_reset(fs_path);
3547 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3548 if (ret < 0)
3549 return ret;
3550 if (parent == ino1)
3551 return parent_gen == ino1_gen;
3552 ino = parent;
3553 }
3554 return 0;
3555}
3556
3557/*
3558 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3559 * possible path (in case ino2 is not a directory and has multiple hard links).
Filipe Manana80aa6022015-03-27 17:50:45 +00003560 * Return 1 if true, 0 if false and < 0 on error.
3561 */
3562static int is_ancestor(struct btrfs_root *root,
3563 const u64 ino1,
3564 const u64 ino1_gen,
3565 const u64 ino2,
3566 struct fs_path *fs_path)
3567{
Filipe Mananaea37d592017-11-17 01:54:00 +00003568 bool free_fs_path = false;
Filipe Manana72c36682017-06-07 11:41:29 +01003569 int ret = 0;
Filipe Mananaea37d592017-11-17 01:54:00 +00003570 struct btrfs_path *path = NULL;
3571 struct btrfs_key key;
Filipe Manana72c36682017-06-07 11:41:29 +01003572
3573 if (!fs_path) {
3574 fs_path = fs_path_alloc();
3575 if (!fs_path)
3576 return -ENOMEM;
Filipe Mananaea37d592017-11-17 01:54:00 +00003577 free_fs_path = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003578 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003579
Filipe Mananaea37d592017-11-17 01:54:00 +00003580 path = alloc_path_for_send();
3581 if (!path) {
3582 ret = -ENOMEM;
3583 goto out;
Filipe Manana80aa6022015-03-27 17:50:45 +00003584 }
Filipe Mananaea37d592017-11-17 01:54:00 +00003585
3586 key.objectid = ino2;
3587 key.type = BTRFS_INODE_REF_KEY;
3588 key.offset = 0;
3589
3590 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3591 if (ret < 0)
3592 goto out;
3593
3594 while (true) {
3595 struct extent_buffer *leaf = path->nodes[0];
3596 int slot = path->slots[0];
3597 u32 cur_offset = 0;
3598 u32 item_size;
3599
3600 if (slot >= btrfs_header_nritems(leaf)) {
3601 ret = btrfs_next_leaf(root, path);
3602 if (ret < 0)
3603 goto out;
3604 if (ret > 0)
3605 break;
3606 continue;
3607 }
3608
3609 btrfs_item_key_to_cpu(leaf, &key, slot);
3610 if (key.objectid != ino2)
3611 break;
3612 if (key.type != BTRFS_INODE_REF_KEY &&
3613 key.type != BTRFS_INODE_EXTREF_KEY)
3614 break;
3615
3616 item_size = btrfs_item_size_nr(leaf, slot);
3617 while (cur_offset < item_size) {
3618 u64 parent;
3619 u64 parent_gen;
3620
3621 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3622 unsigned long ptr;
3623 struct btrfs_inode_extref *extref;
3624
3625 ptr = btrfs_item_ptr_offset(leaf, slot);
3626 extref = (struct btrfs_inode_extref *)
3627 (ptr + cur_offset);
3628 parent = btrfs_inode_extref_parent(leaf,
3629 extref);
3630 cur_offset += sizeof(*extref);
3631 cur_offset += btrfs_inode_extref_name_len(leaf,
3632 extref);
3633 } else {
3634 parent = key.offset;
3635 cur_offset = item_size;
3636 }
3637
3638 ret = get_inode_info(root, parent, NULL, &parent_gen,
3639 NULL, NULL, NULL, NULL);
3640 if (ret < 0)
3641 goto out;
3642 ret = check_ino_in_path(root, ino1, ino1_gen,
3643 parent, parent_gen, fs_path);
3644 if (ret)
3645 goto out;
3646 }
3647 path->slots[0]++;
3648 }
3649 ret = 0;
Filipe Manana72c36682017-06-07 11:41:29 +01003650 out:
Filipe Mananaea37d592017-11-17 01:54:00 +00003651 btrfs_free_path(path);
3652 if (free_fs_path)
Filipe Manana72c36682017-06-07 11:41:29 +01003653 fs_path_free(fs_path);
3654 return ret;
Filipe Manana80aa6022015-03-27 17:50:45 +00003655}
3656
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003657static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003658 struct recorded_ref *parent_ref,
3659 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003660{
Filipe Mananaf9594922014-03-27 20:14:01 +00003661 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003662 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003663 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003664 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003665 struct fs_path *path_before = NULL;
3666 struct fs_path *path_after = NULL;
3667 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003668
3669 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003670 path_before = fs_path_alloc();
3671 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003672 ret = -ENOMEM;
3673 goto out;
3674 }
3675
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003676 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003677 * Our current directory inode may not yet be renamed/moved because some
3678 * ancestor (immediate or not) has to be renamed/moved first. So find if
3679 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003680 * that ancestor is processed to avoid path build infinite loops (done
3681 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003682 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003683 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003684 u64 parent_ino_after_gen;
3685
Filipe Mananaf9594922014-03-27 20:14:01 +00003686 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003687 /*
3688 * If the current inode is an ancestor of ino in the
3689 * parent root, we need to delay the rename of the
3690 * current inode, otherwise don't delayed the rename
3691 * because we can end up with a circular dependency
3692 * of renames, resulting in some directories never
3693 * getting the respective rename operations issued in
3694 * the send stream or getting into infinite path build
3695 * loops.
3696 */
3697 ret = is_ancestor(sctx->parent_root,
3698 sctx->cur_ino, sctx->cur_inode_gen,
3699 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003700 if (ret)
3701 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003702 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003703
3704 fs_path_reset(path_before);
3705 fs_path_reset(path_after);
3706
3707 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003708 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003709 if (ret < 0)
3710 goto out;
3711 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3712 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003713 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003714 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003715 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003716 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003717 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003718 }
3719
3720 len1 = fs_path_len(path_before);
3721 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003722 if (ino > sctx->cur_ino &&
3723 (parent_ino_before != parent_ino_after || len1 != len2 ||
3724 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003725 u64 parent_ino_gen;
3726
3727 ret = get_inode_info(sctx->parent_root, ino, NULL,
3728 &parent_ino_gen, NULL, NULL, NULL,
3729 NULL);
3730 if (ret < 0)
3731 goto out;
3732 if (ino_gen == parent_ino_gen) {
3733 ret = 1;
3734 break;
3735 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003736 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003737 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003738 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003739 }
3740
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003741out:
3742 fs_path_free(path_before);
3743 fs_path_free(path_after);
3744
Filipe Mananaf9594922014-03-27 20:14:01 +00003745 if (ret == 1) {
3746 ret = add_pending_dir_move(sctx,
3747 sctx->cur_ino,
3748 sctx->cur_inode_gen,
3749 ino,
3750 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003751 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003752 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003753 if (!ret)
3754 ret = 1;
3755 }
3756
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003757 return ret;
3758}
3759
Filipe Mananaf5962782017-06-22 20:03:51 +01003760static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3761{
3762 int ret;
3763 struct fs_path *new_path;
3764
3765 /*
3766 * Our reference's name member points to its full_path member string, so
3767 * we use here a new path.
3768 */
3769 new_path = fs_path_alloc();
3770 if (!new_path)
3771 return -ENOMEM;
3772
3773 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3774 if (ret < 0) {
3775 fs_path_free(new_path);
3776 return ret;
3777 }
3778 ret = fs_path_add(new_path, ref->name, ref->name_len);
3779 if (ret < 0) {
3780 fs_path_free(new_path);
3781 return ret;
3782 }
3783
3784 fs_path_free(ref->full_path);
3785 set_ref_path(ref, new_path);
3786
3787 return 0;
3788}
3789
Alexander Block31db9f72012-07-25 23:19:24 +02003790/*
3791 * This does all the move/link/unlink/rmdir magic.
3792 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003793static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003794{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003795 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003796 int ret = 0;
3797 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003798 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003799 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003800 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003801 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003802 u64 ow_gen;
Filipe Mananaf5962782017-06-22 20:03:51 +01003803 u64 ow_mode;
Alexander Block31db9f72012-07-25 23:19:24 +02003804 int did_overwrite = 0;
3805 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003806 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003807 bool can_rename = true;
Filipe Mananaf5962782017-06-22 20:03:51 +01003808 bool orphanized_dir = false;
Filipe Mananafdb13882017-06-13 14:13:11 +01003809 bool orphanized_ancestor = false;
Alexander Block31db9f72012-07-25 23:19:24 +02003810
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003811 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003812
Alexander Block6d85ed052012-08-01 14:48:59 +02003813 /*
3814 * This should never happen as the root dir always has the same ref
3815 * which is always '..'
3816 */
3817 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003818 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed052012-08-01 14:48:59 +02003819
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003820 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003821 if (!valid_path) {
3822 ret = -ENOMEM;
3823 goto out;
3824 }
3825
Alexander Block31db9f72012-07-25 23:19:24 +02003826 /*
3827 * First, check if the first ref of the current inode was overwritten
3828 * before. If yes, we know that the current inode was already orphanized
3829 * and thus use the orphan name. If not, we can use get_cur_path to
3830 * get the path of the first ref as it would like while receiving at
3831 * this point in time.
3832 * New inodes are always orphan at the beginning, so force to use the
3833 * orphan name in this case.
3834 * The first ref is stored in valid_path and will be updated if it
3835 * gets moved around.
3836 */
3837 if (!sctx->cur_inode_new) {
3838 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3839 sctx->cur_inode_gen);
3840 if (ret < 0)
3841 goto out;
3842 if (ret)
3843 did_overwrite = 1;
3844 }
3845 if (sctx->cur_inode_new || did_overwrite) {
3846 ret = gen_unique_name(sctx, sctx->cur_ino,
3847 sctx->cur_inode_gen, valid_path);
3848 if (ret < 0)
3849 goto out;
3850 is_orphan = 1;
3851 } else {
3852 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3853 valid_path);
3854 if (ret < 0)
3855 goto out;
3856 }
3857
3858 list_for_each_entry(cur, &sctx->new_refs, list) {
3859 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003860 * We may have refs where the parent directory does not exist
3861 * yet. This happens if the parent directories inum is higher
Andrea Gelmini52042d82018-11-28 12:05:13 +01003862 * than the current inum. To handle this case, we create the
Alexander Block1f4692d2012-07-28 10:42:24 +02003863 * parent directory out of order. But we need to check if this
3864 * did already happen before due to other refs in the same dir.
3865 */
3866 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3867 if (ret < 0)
3868 goto out;
3869 if (ret == inode_state_will_create) {
3870 ret = 0;
3871 /*
3872 * First check if any of the current inodes refs did
3873 * already create the dir.
3874 */
3875 list_for_each_entry(cur2, &sctx->new_refs, list) {
3876 if (cur == cur2)
3877 break;
3878 if (cur2->dir == cur->dir) {
3879 ret = 1;
3880 break;
3881 }
3882 }
3883
3884 /*
3885 * If that did not happen, check if a previous inode
3886 * did already create the dir.
3887 */
3888 if (!ret)
3889 ret = did_create_dir(sctx, cur->dir);
3890 if (ret < 0)
3891 goto out;
3892 if (!ret) {
3893 ret = send_create_inode(sctx, cur->dir);
3894 if (ret < 0)
3895 goto out;
3896 }
3897 }
3898
3899 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003900 * Check if this new ref would overwrite the first ref of
3901 * another unprocessed inode. If yes, orphanize the
3902 * overwritten inode. If we find an overwritten ref that is
3903 * not the first ref, simply unlink it.
3904 */
3905 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3906 cur->name, cur->name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01003907 &ow_inode, &ow_gen, &ow_mode);
Alexander Block31db9f72012-07-25 23:19:24 +02003908 if (ret < 0)
3909 goto out;
3910 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003911 ret = is_first_ref(sctx->parent_root,
3912 ow_inode, cur->dir, cur->name,
3913 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003914 if (ret < 0)
3915 goto out;
3916 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003917 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003918 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003919
Alexander Block31db9f72012-07-25 23:19:24 +02003920 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3921 cur->full_path);
3922 if (ret < 0)
3923 goto out;
Filipe Mananaf5962782017-06-22 20:03:51 +01003924 if (S_ISDIR(ow_mode))
3925 orphanized_dir = true;
Robbie Ko801bec32015-06-23 18:39:46 +08003926
3927 /*
3928 * If ow_inode has its rename operation delayed
3929 * make sure that its orphanized name is used in
3930 * the source path when performing its rename
3931 * operation.
3932 */
3933 if (is_waiting_for_move(sctx, ow_inode)) {
3934 wdm = get_waiting_dir_move(sctx,
3935 ow_inode);
3936 ASSERT(wdm);
3937 wdm->orphanized = true;
3938 }
3939
Filipe Manana8996a482015-03-12 15:16:20 +00003940 /*
3941 * Make sure we clear our orphanized inode's
3942 * name from the name cache. This is because the
3943 * inode ow_inode might be an ancestor of some
3944 * other inode that will be orphanized as well
3945 * later and has an inode number greater than
3946 * sctx->send_progress. We need to prevent
3947 * future name lookups from using the old name
3948 * and get instead the orphan name.
3949 */
3950 nce = name_cache_search(sctx, ow_inode, ow_gen);
3951 if (nce) {
3952 name_cache_delete(sctx, nce);
3953 kfree(nce);
3954 }
Robbie Ko801bec32015-06-23 18:39:46 +08003955
3956 /*
3957 * ow_inode might currently be an ancestor of
3958 * cur_ino, therefore compute valid_path (the
3959 * current path of cur_ino) again because it
3960 * might contain the pre-orphanization name of
3961 * ow_inode, which is no longer valid.
3962 */
Filipe Manana72c36682017-06-07 11:41:29 +01003963 ret = is_ancestor(sctx->parent_root,
3964 ow_inode, ow_gen,
3965 sctx->cur_ino, NULL);
3966 if (ret > 0) {
Filipe Mananafdb13882017-06-13 14:13:11 +01003967 orphanized_ancestor = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003968 fs_path_reset(valid_path);
3969 ret = get_cur_path(sctx, sctx->cur_ino,
3970 sctx->cur_inode_gen,
3971 valid_path);
3972 }
Robbie Ko801bec32015-06-23 18:39:46 +08003973 if (ret < 0)
3974 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003975 } else {
3976 ret = send_unlink(sctx, cur->full_path);
3977 if (ret < 0)
3978 goto out;
3979 }
3980 }
3981
Filipe Manana84471e22015-02-28 22:29:22 +00003982 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3983 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3984 if (ret < 0)
3985 goto out;
3986 if (ret == 1) {
3987 can_rename = false;
3988 *pending_move = 1;
3989 }
3990 }
3991
Filipe Manana8b191a62015-04-09 14:09:14 +01003992 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3993 can_rename) {
3994 ret = wait_for_parent_move(sctx, cur, is_orphan);
3995 if (ret < 0)
3996 goto out;
3997 if (ret == 1) {
3998 can_rename = false;
3999 *pending_move = 1;
4000 }
4001 }
4002
Alexander Block31db9f72012-07-25 23:19:24 +02004003 /*
4004 * link/move the ref to the new place. If we have an orphan
4005 * inode, move it and update valid_path. If not, link or move
4006 * it depending on the inode mode.
4007 */
Filipe Manana84471e22015-02-28 22:29:22 +00004008 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004009 ret = send_rename(sctx, valid_path, cur->full_path);
4010 if (ret < 0)
4011 goto out;
4012 is_orphan = 0;
4013 ret = fs_path_copy(valid_path, cur->full_path);
4014 if (ret < 0)
4015 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00004016 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004017 if (S_ISDIR(sctx->cur_inode_mode)) {
4018 /*
4019 * Dirs can't be linked, so move it. For moved
4020 * dirs, we always have one new and one deleted
4021 * ref. The deleted ref is ignored later.
4022 */
Filipe Manana8b191a62015-04-09 14:09:14 +01004023 ret = send_rename(sctx, valid_path,
4024 cur->full_path);
4025 if (!ret)
4026 ret = fs_path_copy(valid_path,
4027 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004028 if (ret < 0)
4029 goto out;
4030 } else {
Filipe Mananaf5962782017-06-22 20:03:51 +01004031 /*
4032 * We might have previously orphanized an inode
4033 * which is an ancestor of our current inode,
4034 * so our reference's full path, which was
4035 * computed before any such orphanizations, must
4036 * be updated.
4037 */
4038 if (orphanized_dir) {
4039 ret = update_ref_path(sctx, cur);
4040 if (ret < 0)
4041 goto out;
4042 }
Alexander Block31db9f72012-07-25 23:19:24 +02004043 ret = send_link(sctx, cur->full_path,
4044 valid_path);
4045 if (ret < 0)
4046 goto out;
4047 }
4048 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004049 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004050 if (ret < 0)
4051 goto out;
4052 }
4053
4054 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4055 /*
4056 * Check if we can already rmdir the directory. If not,
4057 * orphanize it. For every dir item inside that gets deleted
4058 * later, we do this check again and rmdir it then if possible.
4059 * See the use of check_dirs for more details.
4060 */
Filipe Manana9dc44212014-02-19 14:31:44 +00004061 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4062 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004063 if (ret < 0)
4064 goto out;
4065 if (ret) {
4066 ret = send_rmdir(sctx, valid_path);
4067 if (ret < 0)
4068 goto out;
4069 } else if (!is_orphan) {
4070 ret = orphanize_inode(sctx, sctx->cur_ino,
4071 sctx->cur_inode_gen, valid_path);
4072 if (ret < 0)
4073 goto out;
4074 is_orphan = 1;
4075 }
4076
4077 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004078 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004079 if (ret < 0)
4080 goto out;
4081 }
Alexander Blockccf16262012-07-28 11:46:29 +02004082 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4083 !list_empty(&sctx->deleted_refs)) {
4084 /*
4085 * We have a moved dir. Add the old parent to check_dirs
4086 */
4087 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4088 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04004089 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02004090 if (ret < 0)
4091 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004092 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4093 /*
4094 * We have a non dir inode. Go through all deleted refs and
4095 * unlink them if they were not already overwritten by other
4096 * inodes.
4097 */
4098 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4099 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4100 sctx->cur_ino, sctx->cur_inode_gen,
4101 cur->name, cur->name_len);
4102 if (ret < 0)
4103 goto out;
4104 if (!ret) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004105 /*
4106 * If we orphanized any ancestor before, we need
4107 * to recompute the full path for deleted names,
4108 * since any such path was computed before we
4109 * processed any references and orphanized any
4110 * ancestor inode.
4111 */
4112 if (orphanized_ancestor) {
Filipe Mananaf5962782017-06-22 20:03:51 +01004113 ret = update_ref_path(sctx, cur);
4114 if (ret < 0)
Filipe Mananafdb13882017-06-13 14:13:11 +01004115 goto out;
Filipe Mananafdb13882017-06-13 14:13:11 +01004116 }
Alexander Block1f4692d2012-07-28 10:42:24 +02004117 ret = send_unlink(sctx, cur->full_path);
4118 if (ret < 0)
4119 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004120 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004121 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004122 if (ret < 0)
4123 goto out;
4124 }
Alexander Block31db9f72012-07-25 23:19:24 +02004125 /*
4126 * If the inode is still orphan, unlink the orphan. This may
4127 * happen when a previous inode did overwrite the first ref
4128 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02004129 * inode. Unlinking does not mean that the inode is deleted in
4130 * all cases. There may still be links to this inode in other
4131 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02004132 */
Alexander Block1f4692d2012-07-28 10:42:24 +02004133 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02004134 ret = send_unlink(sctx, valid_path);
4135 if (ret < 0)
4136 goto out;
4137 }
4138 }
4139
4140 /*
4141 * We did collect all parent dirs where cur_inode was once located. We
4142 * now go through all these dirs and check if they are pending for
4143 * deletion and if it's finally possible to perform the rmdir now.
4144 * We also update the inode stats of the parent dirs here.
4145 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004146 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02004147 /*
4148 * In case we had refs into dirs that were not processed yet,
4149 * we don't need to do the utime and rmdir logic for these dirs.
4150 * The dir will be processed later.
4151 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004152 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02004153 continue;
4154
Josef Bacikba5e8f22013-08-16 16:52:55 -04004155 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004156 if (ret < 0)
4157 goto out;
4158
4159 if (ret == inode_state_did_create ||
4160 ret == inode_state_no_change) {
4161 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004162 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004163 if (ret < 0)
4164 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004165 } else if (ret == inode_state_did_delete &&
4166 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004167 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4168 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004169 if (ret < 0)
4170 goto out;
4171 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004172 ret = get_cur_path(sctx, cur->dir,
4173 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004174 if (ret < 0)
4175 goto out;
4176 ret = send_rmdir(sctx, valid_path);
4177 if (ret < 0)
4178 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004179 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004180 }
4181 }
4182 }
4183
Alexander Block31db9f72012-07-25 23:19:24 +02004184 ret = 0;
4185
4186out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004187 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004188 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004189 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004190 return ret;
4191}
4192
Nikolay Borisova0357512017-08-21 12:43:44 +03004193static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4194 void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004195{
4196 int ret = 0;
4197 struct send_ctx *sctx = ctx;
4198 struct fs_path *p;
4199 u64 gen;
4200
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004201 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004202 if (!p)
4203 return -ENOMEM;
4204
Liu Boa4d96d62014-03-03 21:31:03 +08004205 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004206 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004207 if (ret < 0)
4208 goto out;
4209
Alexander Block31db9f72012-07-25 23:19:24 +02004210 ret = get_cur_path(sctx, dir, gen, p);
4211 if (ret < 0)
4212 goto out;
4213 ret = fs_path_add_path(p, name);
4214 if (ret < 0)
4215 goto out;
4216
Liu Boa4d96d62014-03-03 21:31:03 +08004217 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004218
4219out:
4220 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004221 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004222 return ret;
4223}
4224
Liu Boa4d96d62014-03-03 21:31:03 +08004225static int __record_new_ref(int num, u64 dir, int index,
4226 struct fs_path *name,
4227 void *ctx)
4228{
4229 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004230 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
Liu Boa4d96d62014-03-03 21:31:03 +08004231}
4232
4233
Alexander Block31db9f72012-07-25 23:19:24 +02004234static int __record_deleted_ref(int num, u64 dir, int index,
4235 struct fs_path *name,
4236 void *ctx)
4237{
Alexander Block31db9f72012-07-25 23:19:24 +02004238 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004239 return record_ref(sctx->parent_root, dir, name, ctx,
4240 &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004241}
4242
4243static int record_new_ref(struct send_ctx *sctx)
4244{
4245 int ret;
4246
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004247 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4248 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004249 if (ret < 0)
4250 goto out;
4251 ret = 0;
4252
4253out:
4254 return ret;
4255}
4256
4257static int record_deleted_ref(struct send_ctx *sctx)
4258{
4259 int ret;
4260
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004261 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4262 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004263 if (ret < 0)
4264 goto out;
4265 ret = 0;
4266
4267out:
4268 return ret;
4269}
4270
4271struct find_ref_ctx {
4272 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004273 u64 dir_gen;
4274 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004275 struct fs_path *name;
4276 int found_idx;
4277};
4278
4279static int __find_iref(int num, u64 dir, int index,
4280 struct fs_path *name,
4281 void *ctx_)
4282{
4283 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004284 u64 dir_gen;
4285 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004286
4287 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4288 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004289 /*
4290 * To avoid doing extra lookups we'll only do this if everything
4291 * else matches.
4292 */
4293 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4294 NULL, NULL, NULL);
4295 if (ret)
4296 return ret;
4297 if (dir_gen != ctx->dir_gen)
4298 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004299 ctx->found_idx = num;
4300 return 1;
4301 }
4302 return 0;
4303}
4304
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004305static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004306 struct btrfs_path *path,
4307 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004308 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004309{
4310 int ret;
4311 struct find_ref_ctx ctx;
4312
4313 ctx.dir = dir;
4314 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004315 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004316 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004317 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004318
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004319 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004320 if (ret < 0)
4321 return ret;
4322
4323 if (ctx.found_idx == -1)
4324 return -ENOENT;
4325
4326 return ctx.found_idx;
4327}
4328
4329static int __record_changed_new_ref(int num, u64 dir, int index,
4330 struct fs_path *name,
4331 void *ctx)
4332{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004333 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004334 int ret;
4335 struct send_ctx *sctx = ctx;
4336
Josef Bacikba5e8f22013-08-16 16:52:55 -04004337 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4338 NULL, NULL, NULL);
4339 if (ret)
4340 return ret;
4341
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004342 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004343 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004344 if (ret == -ENOENT)
4345 ret = __record_new_ref(num, dir, index, name, sctx);
4346 else if (ret > 0)
4347 ret = 0;
4348
4349 return ret;
4350}
4351
4352static int __record_changed_deleted_ref(int num, u64 dir, int index,
4353 struct fs_path *name,
4354 void *ctx)
4355{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004356 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004357 int ret;
4358 struct send_ctx *sctx = ctx;
4359
Josef Bacikba5e8f22013-08-16 16:52:55 -04004360 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4361 NULL, NULL, NULL);
4362 if (ret)
4363 return ret;
4364
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004365 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004366 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004367 if (ret == -ENOENT)
4368 ret = __record_deleted_ref(num, dir, index, name, sctx);
4369 else if (ret > 0)
4370 ret = 0;
4371
4372 return ret;
4373}
4374
4375static int record_changed_ref(struct send_ctx *sctx)
4376{
4377 int ret = 0;
4378
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004379 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004380 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4381 if (ret < 0)
4382 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004383 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004384 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4385 if (ret < 0)
4386 goto out;
4387 ret = 0;
4388
4389out:
4390 return ret;
4391}
4392
4393/*
4394 * Record and process all refs at once. Needed when an inode changes the
4395 * generation number, which means that it was deleted and recreated.
4396 */
4397static int process_all_refs(struct send_ctx *sctx,
4398 enum btrfs_compare_tree_result cmd)
4399{
4400 int ret;
4401 struct btrfs_root *root;
4402 struct btrfs_path *path;
4403 struct btrfs_key key;
4404 struct btrfs_key found_key;
4405 struct extent_buffer *eb;
4406 int slot;
4407 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004408 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004409
4410 path = alloc_path_for_send();
4411 if (!path)
4412 return -ENOMEM;
4413
4414 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4415 root = sctx->send_root;
4416 cb = __record_new_ref;
4417 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4418 root = sctx->parent_root;
4419 cb = __record_deleted_ref;
4420 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004421 btrfs_err(sctx->send_root->fs_info,
4422 "Wrong command %d in process_all_refs", cmd);
4423 ret = -EINVAL;
4424 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004425 }
4426
4427 key.objectid = sctx->cmp_key->objectid;
4428 key.type = BTRFS_INODE_REF_KEY;
4429 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004430 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4431 if (ret < 0)
4432 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004433
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004434 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004435 eb = path->nodes[0];
4436 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004437 if (slot >= btrfs_header_nritems(eb)) {
4438 ret = btrfs_next_leaf(root, path);
4439 if (ret < 0)
4440 goto out;
4441 else if (ret > 0)
4442 break;
4443 continue;
4444 }
4445
Alexander Block31db9f72012-07-25 23:19:24 +02004446 btrfs_item_key_to_cpu(eb, &found_key, slot);
4447
4448 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004449 (found_key.type != BTRFS_INODE_REF_KEY &&
4450 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004451 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004452
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004453 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004454 if (ret < 0)
4455 goto out;
4456
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004457 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004458 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004459 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004460
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004461 /*
4462 * We don't actually care about pending_move as we are simply
4463 * re-creating this inode and will be rename'ing it into place once we
4464 * rename the parent directory.
4465 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004466 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004467out:
4468 btrfs_free_path(path);
4469 return ret;
4470}
4471
4472static int send_set_xattr(struct send_ctx *sctx,
4473 struct fs_path *path,
4474 const char *name, int name_len,
4475 const char *data, int data_len)
4476{
4477 int ret = 0;
4478
4479 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4480 if (ret < 0)
4481 goto out;
4482
4483 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4484 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4485 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4486
4487 ret = send_cmd(sctx);
4488
4489tlv_put_failure:
4490out:
4491 return ret;
4492}
4493
4494static int send_remove_xattr(struct send_ctx *sctx,
4495 struct fs_path *path,
4496 const char *name, int name_len)
4497{
4498 int ret = 0;
4499
4500 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4501 if (ret < 0)
4502 goto out;
4503
4504 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4505 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4506
4507 ret = send_cmd(sctx);
4508
4509tlv_put_failure:
4510out:
4511 return ret;
4512}
4513
4514static int __process_new_xattr(int num, struct btrfs_key *di_key,
4515 const char *name, int name_len,
4516 const char *data, int data_len,
4517 u8 type, void *ctx)
4518{
4519 int ret;
4520 struct send_ctx *sctx = ctx;
4521 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004522 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004523
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004524 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004525 if (!p)
4526 return -ENOMEM;
4527
4528 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004529 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004530 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004531 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004532 * only contains the version number and no entries.
4533 */
4534 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4535 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4536 if (data_len == 0) {
4537 dummy_acl.a_version =
4538 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4539 data = (char *)&dummy_acl;
4540 data_len = sizeof(dummy_acl);
4541 }
4542 }
4543
4544 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4545 if (ret < 0)
4546 goto out;
4547
4548 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4549
4550out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004551 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004552 return ret;
4553}
4554
4555static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4556 const char *name, int name_len,
4557 const char *data, int data_len,
4558 u8 type, void *ctx)
4559{
4560 int ret;
4561 struct send_ctx *sctx = ctx;
4562 struct fs_path *p;
4563
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004564 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004565 if (!p)
4566 return -ENOMEM;
4567
4568 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4569 if (ret < 0)
4570 goto out;
4571
4572 ret = send_remove_xattr(sctx, p, name, name_len);
4573
4574out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004575 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004576 return ret;
4577}
4578
4579static int process_new_xattr(struct send_ctx *sctx)
4580{
4581 int ret = 0;
4582
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004583 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004584 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004585
4586 return ret;
4587}
4588
4589static int process_deleted_xattr(struct send_ctx *sctx)
4590{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004591 return iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004592 __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004593}
4594
4595struct find_xattr_ctx {
4596 const char *name;
4597 int name_len;
4598 int found_idx;
4599 char *found_data;
4600 int found_data_len;
4601};
4602
4603static int __find_xattr(int num, struct btrfs_key *di_key,
4604 const char *name, int name_len,
4605 const char *data, int data_len,
4606 u8 type, void *vctx)
4607{
4608 struct find_xattr_ctx *ctx = vctx;
4609
4610 if (name_len == ctx->name_len &&
4611 strncmp(name, ctx->name, name_len) == 0) {
4612 ctx->found_idx = num;
4613 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004614 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004615 if (!ctx->found_data)
4616 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004617 return 1;
4618 }
4619 return 0;
4620}
4621
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004622static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004623 struct btrfs_path *path,
4624 struct btrfs_key *key,
4625 const char *name, int name_len,
4626 char **data, int *data_len)
4627{
4628 int ret;
4629 struct find_xattr_ctx ctx;
4630
4631 ctx.name = name;
4632 ctx.name_len = name_len;
4633 ctx.found_idx = -1;
4634 ctx.found_data = NULL;
4635 ctx.found_data_len = 0;
4636
Nikolay Borisova0357512017-08-21 12:43:44 +03004637 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004638 if (ret < 0)
4639 return ret;
4640
4641 if (ctx.found_idx == -1)
4642 return -ENOENT;
4643 if (data) {
4644 *data = ctx.found_data;
4645 *data_len = ctx.found_data_len;
4646 } else {
4647 kfree(ctx.found_data);
4648 }
4649 return ctx.found_idx;
4650}
4651
4652
4653static int __process_changed_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 char *found_data = NULL;
4661 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004662
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004663 ret = find_xattr(sctx->parent_root, sctx->right_path,
4664 sctx->cmp_key, name, name_len, &found_data,
4665 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004666 if (ret == -ENOENT) {
4667 ret = __process_new_xattr(num, di_key, name, name_len, data,
4668 data_len, type, ctx);
4669 } else if (ret >= 0) {
4670 if (data_len != found_data_len ||
4671 memcmp(data, found_data, data_len)) {
4672 ret = __process_new_xattr(num, di_key, name, name_len,
4673 data, data_len, type, ctx);
4674 } else {
4675 ret = 0;
4676 }
4677 }
4678
4679 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004680 return ret;
4681}
4682
4683static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4684 const char *name, int name_len,
4685 const char *data, int data_len,
4686 u8 type, void *ctx)
4687{
4688 int ret;
4689 struct send_ctx *sctx = ctx;
4690
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004691 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4692 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004693 if (ret == -ENOENT)
4694 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4695 data_len, type, ctx);
4696 else if (ret >= 0)
4697 ret = 0;
4698
4699 return ret;
4700}
4701
4702static int process_changed_xattr(struct send_ctx *sctx)
4703{
4704 int ret = 0;
4705
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004706 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004707 __process_changed_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004708 if (ret < 0)
4709 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004710 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004711 __process_changed_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004712
4713out:
4714 return ret;
4715}
4716
4717static int process_all_new_xattrs(struct send_ctx *sctx)
4718{
4719 int ret;
4720 struct btrfs_root *root;
4721 struct btrfs_path *path;
4722 struct btrfs_key key;
4723 struct btrfs_key found_key;
4724 struct extent_buffer *eb;
4725 int slot;
4726
4727 path = alloc_path_for_send();
4728 if (!path)
4729 return -ENOMEM;
4730
4731 root = sctx->send_root;
4732
4733 key.objectid = sctx->cmp_key->objectid;
4734 key.type = BTRFS_XATTR_ITEM_KEY;
4735 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004736 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4737 if (ret < 0)
4738 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004739
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004740 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004741 eb = path->nodes[0];
4742 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004743 if (slot >= btrfs_header_nritems(eb)) {
4744 ret = btrfs_next_leaf(root, path);
4745 if (ret < 0) {
4746 goto out;
4747 } else if (ret > 0) {
4748 ret = 0;
4749 break;
4750 }
4751 continue;
4752 }
Alexander Block31db9f72012-07-25 23:19:24 +02004753
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004754 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004755 if (found_key.objectid != key.objectid ||
4756 found_key.type != key.type) {
4757 ret = 0;
4758 goto out;
4759 }
4760
Nikolay Borisova0357512017-08-21 12:43:44 +03004761 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004762 if (ret < 0)
4763 goto out;
4764
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004765 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004766 }
4767
4768out:
4769 btrfs_free_path(path);
4770 return ret;
4771}
4772
Josef Baciked259092013-10-25 11:36:01 -04004773static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4774{
4775 struct btrfs_root *root = sctx->send_root;
4776 struct btrfs_fs_info *fs_info = root->fs_info;
4777 struct inode *inode;
4778 struct page *page;
4779 char *addr;
4780 struct btrfs_key key;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004781 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004782 pgoff_t last_index;
Johannes Thumshirn70730172018-12-05 15:23:03 +01004783 unsigned pg_offset = offset_in_page(offset);
Josef Baciked259092013-10-25 11:36:01 -04004784 ssize_t ret = 0;
4785
4786 key.objectid = sctx->cur_ino;
4787 key.type = BTRFS_INODE_ITEM_KEY;
4788 key.offset = 0;
4789
David Sterba4c66e0d2019-10-03 19:09:35 +02004790 inode = btrfs_iget(fs_info->sb, &key, root);
Josef Baciked259092013-10-25 11:36:01 -04004791 if (IS_ERR(inode))
4792 return PTR_ERR(inode);
4793
4794 if (offset + len > i_size_read(inode)) {
4795 if (offset > i_size_read(inode))
4796 len = 0;
4797 else
4798 len = offset - i_size_read(inode);
4799 }
4800 if (len == 0)
4801 goto out;
4802
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004803 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004804
4805 /* initial readahead */
4806 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4807 file_ra_state_init(&sctx->ra, inode->i_mapping);
Liu Bo2131bcd2014-03-05 10:07:35 +08004808
Josef Baciked259092013-10-25 11:36:01 -04004809 while (index <= last_index) {
4810 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004811 PAGE_SIZE - pg_offset);
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004812
4813 page = find_lock_page(inode->i_mapping, index);
Josef Baciked259092013-10-25 11:36:01 -04004814 if (!page) {
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004815 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4816 NULL, index, last_index + 1 - index);
4817
4818 page = find_or_create_page(inode->i_mapping, index,
4819 GFP_KERNEL);
4820 if (!page) {
4821 ret = -ENOMEM;
4822 break;
4823 }
4824 }
4825
4826 if (PageReadahead(page)) {
4827 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4828 NULL, page, index, last_index + 1 - index);
Josef Baciked259092013-10-25 11:36:01 -04004829 }
4830
4831 if (!PageUptodate(page)) {
4832 btrfs_readpage(NULL, page);
4833 lock_page(page);
4834 if (!PageUptodate(page)) {
4835 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004836 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004837 ret = -EIO;
4838 break;
4839 }
4840 }
4841
4842 addr = kmap(page);
4843 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4844 kunmap(page);
4845 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004846 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004847 index++;
4848 pg_offset = 0;
4849 len -= cur_len;
4850 ret += cur_len;
4851 }
4852out:
4853 iput(inode);
4854 return ret;
4855}
4856
Alexander Block31db9f72012-07-25 23:19:24 +02004857/*
4858 * Read some bytes from the current inode/file and send a write command to
4859 * user space.
4860 */
4861static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4862{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004863 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02004864 int ret = 0;
4865 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004866 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004867
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004868 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004869 if (!p)
4870 return -ENOMEM;
4871
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004872 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004873
Josef Baciked259092013-10-25 11:36:01 -04004874 num_read = fill_read_buf(sctx, offset, len);
4875 if (num_read <= 0) {
4876 if (num_read < 0)
4877 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004878 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004879 }
Alexander Block31db9f72012-07-25 23:19:24 +02004880
4881 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4882 if (ret < 0)
4883 goto out;
4884
4885 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4886 if (ret < 0)
4887 goto out;
4888
4889 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4890 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004891 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004892
4893 ret = send_cmd(sctx);
4894
4895tlv_put_failure:
4896out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004897 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004898 if (ret < 0)
4899 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004900 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004901}
4902
4903/*
4904 * Send a clone command to user space.
4905 */
4906static int send_clone(struct send_ctx *sctx,
4907 u64 offset, u32 len,
4908 struct clone_root *clone_root)
4909{
4910 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004911 struct fs_path *p;
4912 u64 gen;
4913
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004914 btrfs_debug(sctx->send_root->fs_info,
4915 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09004916 offset, len, clone_root->root->root_key.objectid,
4917 clone_root->ino, clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02004918
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004919 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004920 if (!p)
4921 return -ENOMEM;
4922
4923 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4924 if (ret < 0)
4925 goto out;
4926
4927 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4928 if (ret < 0)
4929 goto out;
4930
4931 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4932 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4933 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4934
Alexander Blocke938c8a2012-07-28 16:33:49 +02004935 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004936 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004937 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004938 if (ret < 0)
4939 goto out;
4940 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4941 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004942 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004943 }
4944 if (ret < 0)
4945 goto out;
4946
Josef Bacik37b8d272015-06-04 17:17:25 -04004947 /*
4948 * If the parent we're using has a received_uuid set then use that as
4949 * our clone source as that is what we will look for when doing a
4950 * receive.
4951 *
4952 * This covers the case that we create a snapshot off of a received
4953 * subvolume and then use that as the parent and try to receive on a
4954 * different host.
4955 */
4956 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4957 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4958 clone_root->root->root_item.received_uuid);
4959 else
4960 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4961 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004962 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004963 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004964 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4965 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4966 clone_root->offset);
4967
4968 ret = send_cmd(sctx);
4969
4970tlv_put_failure:
4971out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004972 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004973 return ret;
4974}
4975
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004976/*
4977 * Send an update extent command to user space.
4978 */
4979static int send_update_extent(struct send_ctx *sctx,
4980 u64 offset, u32 len)
4981{
4982 int ret = 0;
4983 struct fs_path *p;
4984
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004985 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004986 if (!p)
4987 return -ENOMEM;
4988
4989 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4990 if (ret < 0)
4991 goto out;
4992
4993 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4994 if (ret < 0)
4995 goto out;
4996
4997 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4998 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4999 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5000
5001 ret = send_cmd(sctx);
5002
5003tlv_put_failure:
5004out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005005 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005006 return ret;
5007}
5008
Josef Bacik16e75492013-10-22 12:18:51 -04005009static int send_hole(struct send_ctx *sctx, u64 end)
5010{
5011 struct fs_path *p = NULL;
5012 u64 offset = sctx->cur_inode_last_extent;
5013 u64 len;
5014 int ret = 0;
5015
Filipe Manana22d31512018-07-30 12:39:58 +01005016 /*
5017 * A hole that starts at EOF or beyond it. Since we do not yet support
5018 * fallocate (for extent preallocation and hole punching), sending a
5019 * write of zeroes starting at EOF or beyond would later require issuing
5020 * a truncate operation which would undo the write and achieve nothing.
5021 */
5022 if (offset >= sctx->cur_inode_size)
5023 return 0;
5024
Filipe Manana6b1f72e52019-05-20 09:55:42 +01005025 /*
5026 * Don't go beyond the inode's i_size due to prealloc extents that start
5027 * after the i_size.
5028 */
5029 end = min_t(u64, end, sctx->cur_inode_size);
5030
Filipe Mananad4dfc0f2018-02-06 20:39:20 +00005031 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5032 return send_update_extent(sctx, offset, end - offset);
5033
Josef Bacik16e75492013-10-22 12:18:51 -04005034 p = fs_path_alloc();
5035 if (!p)
5036 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01005037 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5038 if (ret < 0)
5039 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04005040 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
5041 while (offset < end) {
5042 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
5043
5044 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5045 if (ret < 0)
5046 break;
Josef Bacik16e75492013-10-22 12:18:51 -04005047 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5048 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5049 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
5050 ret = send_cmd(sctx);
5051 if (ret < 0)
5052 break;
5053 offset += len;
5054 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005055 sctx->cur_inode_next_write_offset = offset;
Josef Bacik16e75492013-10-22 12:18:51 -04005056tlv_put_failure:
5057 fs_path_free(p);
5058 return ret;
5059}
5060
Filipe Mananad906d492015-10-02 10:47:34 +01005061static int send_extent_data(struct send_ctx *sctx,
5062 const u64 offset,
5063 const u64 len)
5064{
5065 u64 sent = 0;
5066
5067 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5068 return send_update_extent(sctx, offset, len);
5069
5070 while (sent < len) {
5071 u64 size = len - sent;
5072 int ret;
5073
5074 if (size > BTRFS_SEND_READ_SIZE)
5075 size = BTRFS_SEND_READ_SIZE;
5076 ret = send_write(sctx, offset + sent, size);
5077 if (ret < 0)
5078 return ret;
5079 if (!ret)
5080 break;
5081 sent += ret;
5082 }
5083 return 0;
5084}
5085
5086static int clone_range(struct send_ctx *sctx,
5087 struct clone_root *clone_root,
5088 const u64 disk_byte,
5089 u64 data_offset,
5090 u64 offset,
5091 u64 len)
5092{
5093 struct btrfs_path *path;
5094 struct btrfs_key key;
5095 int ret;
Austin Kim431d3982019-09-03 12:30:19 +09005096 u64 clone_src_i_size = 0;
Filipe Mananad906d492015-10-02 10:47:34 +01005097
Filipe Manana72610b12017-08-10 22:54:51 +01005098 /*
5099 * Prevent cloning from a zero offset with a length matching the sector
5100 * size because in some scenarios this will make the receiver fail.
5101 *
5102 * For example, if in the source filesystem the extent at offset 0
5103 * has a length of sectorsize and it was written using direct IO, then
5104 * it can never be an inline extent (even if compression is enabled).
5105 * Then this extent can be cloned in the original filesystem to a non
5106 * zero file offset, but it may not be possible to clone in the
5107 * destination filesystem because it can be inlined due to compression
5108 * on the destination filesystem (as the receiver's write operations are
5109 * always done using buffered IO). The same happens when the original
5110 * filesystem does not have compression enabled but the destination
5111 * filesystem has.
5112 */
5113 if (clone_root->offset == 0 &&
5114 len == sctx->send_root->fs_info->sectorsize)
5115 return send_extent_data(sctx, offset, len);
5116
Filipe Mananad906d492015-10-02 10:47:34 +01005117 path = alloc_path_for_send();
5118 if (!path)
5119 return -ENOMEM;
5120
5121 /*
Robbie Ko040ee6122019-03-29 18:03:27 +08005122 * There are inodes that have extents that lie behind its i_size. Don't
5123 * accept clones from these extents.
5124 */
5125 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5126 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL);
5127 btrfs_release_path(path);
5128 if (ret < 0)
5129 goto out;
5130
5131 /*
Filipe Mananad906d492015-10-02 10:47:34 +01005132 * We can't send a clone operation for the entire range if we find
5133 * extent items in the respective range in the source file that
5134 * refer to different extents or if we find holes.
5135 * So check for that and do a mix of clone and regular write/copy
5136 * operations if needed.
5137 *
5138 * Example:
5139 *
5140 * mkfs.btrfs -f /dev/sda
5141 * mount /dev/sda /mnt
5142 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5143 * cp --reflink=always /mnt/foo /mnt/bar
5144 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5145 * btrfs subvolume snapshot -r /mnt /mnt/snap
5146 *
5147 * If when we send the snapshot and we are processing file bar (which
5148 * has a higher inode number than foo) we blindly send a clone operation
5149 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5150 * a file bar that matches the content of file foo - iow, doesn't match
5151 * the content from bar in the original filesystem.
5152 */
5153 key.objectid = clone_root->ino;
5154 key.type = BTRFS_EXTENT_DATA_KEY;
5155 key.offset = clone_root->offset;
5156 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5157 if (ret < 0)
5158 goto out;
5159 if (ret > 0 && path->slots[0] > 0) {
5160 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5161 if (key.objectid == clone_root->ino &&
5162 key.type == BTRFS_EXTENT_DATA_KEY)
5163 path->slots[0]--;
5164 }
5165
5166 while (true) {
5167 struct extent_buffer *leaf = path->nodes[0];
5168 int slot = path->slots[0];
5169 struct btrfs_file_extent_item *ei;
5170 u8 type;
5171 u64 ext_len;
5172 u64 clone_len;
Robbie Ko040ee6122019-03-29 18:03:27 +08005173 u64 clone_data_offset;
Filipe Mananad906d492015-10-02 10:47:34 +01005174
5175 if (slot >= btrfs_header_nritems(leaf)) {
5176 ret = btrfs_next_leaf(clone_root->root, path);
5177 if (ret < 0)
5178 goto out;
5179 else if (ret > 0)
5180 break;
5181 continue;
5182 }
5183
5184 btrfs_item_key_to_cpu(leaf, &key, slot);
5185
5186 /*
5187 * We might have an implicit trailing hole (NO_HOLES feature
5188 * enabled). We deal with it after leaving this loop.
5189 */
5190 if (key.objectid != clone_root->ino ||
5191 key.type != BTRFS_EXTENT_DATA_KEY)
5192 break;
5193
5194 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5195 type = btrfs_file_extent_type(leaf, ei);
5196 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005197 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005198 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01005199 } else {
5200 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5201 }
5202
5203 if (key.offset + ext_len <= clone_root->offset)
5204 goto next;
5205
5206 if (key.offset > clone_root->offset) {
5207 /* Implicit hole, NO_HOLES feature enabled. */
5208 u64 hole_len = key.offset - clone_root->offset;
5209
5210 if (hole_len > len)
5211 hole_len = len;
5212 ret = send_extent_data(sctx, offset, hole_len);
5213 if (ret < 0)
5214 goto out;
5215
5216 len -= hole_len;
5217 if (len == 0)
5218 break;
5219 offset += hole_len;
5220 clone_root->offset += hole_len;
5221 data_offset += hole_len;
5222 }
5223
5224 if (key.offset >= clone_root->offset + len)
5225 break;
5226
Robbie Ko040ee6122019-03-29 18:03:27 +08005227 if (key.offset >= clone_src_i_size)
5228 break;
5229
5230 if (key.offset + ext_len > clone_src_i_size)
5231 ext_len = clone_src_i_size - key.offset;
5232
5233 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5234 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5235 clone_root->offset = key.offset;
5236 if (clone_data_offset < data_offset &&
5237 clone_data_offset + ext_len > data_offset) {
5238 u64 extent_offset;
5239
5240 extent_offset = data_offset - clone_data_offset;
5241 ext_len -= extent_offset;
5242 clone_data_offset += extent_offset;
5243 clone_root->offset += extent_offset;
5244 }
5245 }
5246
Filipe Mananad906d492015-10-02 10:47:34 +01005247 clone_len = min_t(u64, ext_len, len);
5248
5249 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
Filipe Manana3c850b42019-05-20 09:57:00 +01005250 clone_data_offset == data_offset) {
5251 const u64 src_end = clone_root->offset + clone_len;
5252 const u64 sectorsize = SZ_64K;
5253
5254 /*
5255 * We can't clone the last block, when its size is not
5256 * sector size aligned, into the middle of a file. If we
5257 * do so, the receiver will get a failure (-EINVAL) when
5258 * trying to clone or will silently corrupt the data in
5259 * the destination file if it's on a kernel without the
5260 * fix introduced by commit ac765f83f1397646
5261 * ("Btrfs: fix data corruption due to cloning of eof
5262 * block).
5263 *
5264 * So issue a clone of the aligned down range plus a
5265 * regular write for the eof block, if we hit that case.
5266 *
5267 * Also, we use the maximum possible sector size, 64K,
5268 * because we don't know what's the sector size of the
5269 * filesystem that receives the stream, so we have to
5270 * assume the largest possible sector size.
5271 */
5272 if (src_end == clone_src_i_size &&
5273 !IS_ALIGNED(src_end, sectorsize) &&
5274 offset + clone_len < sctx->cur_inode_size) {
5275 u64 slen;
5276
5277 slen = ALIGN_DOWN(src_end - clone_root->offset,
5278 sectorsize);
5279 if (slen > 0) {
5280 ret = send_clone(sctx, offset, slen,
5281 clone_root);
5282 if (ret < 0)
5283 goto out;
5284 }
5285 ret = send_extent_data(sctx, offset + slen,
5286 clone_len - slen);
5287 } else {
5288 ret = send_clone(sctx, offset, clone_len,
5289 clone_root);
5290 }
5291 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005292 ret = send_extent_data(sctx, offset, clone_len);
Filipe Manana3c850b42019-05-20 09:57:00 +01005293 }
Filipe Mananad906d492015-10-02 10:47:34 +01005294
5295 if (ret < 0)
5296 goto out;
5297
5298 len -= clone_len;
5299 if (len == 0)
5300 break;
5301 offset += clone_len;
5302 clone_root->offset += clone_len;
5303 data_offset += clone_len;
5304next:
5305 path->slots[0]++;
5306 }
5307
5308 if (len > 0)
5309 ret = send_extent_data(sctx, offset, len);
5310 else
5311 ret = 0;
5312out:
5313 btrfs_free_path(path);
5314 return ret;
5315}
5316
Alexander Block31db9f72012-07-25 23:19:24 +02005317static int send_write_or_clone(struct send_ctx *sctx,
5318 struct btrfs_path *path,
5319 struct btrfs_key *key,
5320 struct clone_root *clone_root)
5321{
5322 int ret = 0;
5323 struct btrfs_file_extent_item *ei;
5324 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02005325 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02005326 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005327 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005328
5329 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5330 struct btrfs_file_extent_item);
5331 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005332 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005333 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005334 /*
5335 * it is possible the inline item won't cover the whole page,
5336 * but there may be items after this page. Make
5337 * sure to send the whole thing
5338 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005339 len = PAGE_ALIGN(len);
Chris Mason74dd17f2012-08-07 16:25:13 -04005340 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02005341 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005342 }
Alexander Block31db9f72012-07-25 23:19:24 +02005343
Filipe Mananaa6aa10c72018-04-30 19:05:07 +01005344 if (offset >= sctx->cur_inode_size) {
5345 ret = 0;
5346 goto out;
5347 }
Alexander Block31db9f72012-07-25 23:19:24 +02005348 if (offset + len > sctx->cur_inode_size)
5349 len = sctx->cur_inode_size - offset;
5350 if (len == 0) {
5351 ret = 0;
5352 goto out;
5353 }
5354
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005355 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01005356 u64 disk_byte;
5357 u64 data_offset;
5358
5359 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5360 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5361 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5362 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005363 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005364 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005365 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005366 sctx->cur_inode_next_write_offset = offset + len;
Alexander Block31db9f72012-07-25 23:19:24 +02005367out:
5368 return ret;
5369}
5370
5371static int is_extent_unchanged(struct send_ctx *sctx,
5372 struct btrfs_path *left_path,
5373 struct btrfs_key *ekey)
5374{
5375 int ret = 0;
5376 struct btrfs_key key;
5377 struct btrfs_path *path = NULL;
5378 struct extent_buffer *eb;
5379 int slot;
5380 struct btrfs_key found_key;
5381 struct btrfs_file_extent_item *ei;
5382 u64 left_disknr;
5383 u64 right_disknr;
5384 u64 left_offset;
5385 u64 right_offset;
5386 u64 left_offset_fixed;
5387 u64 left_len;
5388 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005389 u64 left_gen;
5390 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005391 u8 left_type;
5392 u8 right_type;
5393
5394 path = alloc_path_for_send();
5395 if (!path)
5396 return -ENOMEM;
5397
5398 eb = left_path->nodes[0];
5399 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005400 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5401 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005402
5403 if (left_type != BTRFS_FILE_EXTENT_REG) {
5404 ret = 0;
5405 goto out;
5406 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005407 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5408 left_len = btrfs_file_extent_num_bytes(eb, ei);
5409 left_offset = btrfs_file_extent_offset(eb, ei);
5410 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005411
5412 /*
5413 * Following comments will refer to these graphics. L is the left
5414 * extents which we are checking at the moment. 1-8 are the right
5415 * extents that we iterate.
5416 *
5417 * |-----L-----|
5418 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5419 *
5420 * |-----L-----|
5421 * |--1--|-2b-|...(same as above)
5422 *
5423 * Alternative situation. Happens on files where extents got split.
5424 * |-----L-----|
5425 * |-----------7-----------|-6-|
5426 *
5427 * Alternative situation. Happens on files which got larger.
5428 * |-----L-----|
5429 * |-8-|
5430 * Nothing follows after 8.
5431 */
5432
5433 key.objectid = ekey->objectid;
5434 key.type = BTRFS_EXTENT_DATA_KEY;
5435 key.offset = ekey->offset;
5436 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5437 if (ret < 0)
5438 goto out;
5439 if (ret) {
5440 ret = 0;
5441 goto out;
5442 }
5443
5444 /*
5445 * Handle special case where the right side has no extents at all.
5446 */
5447 eb = path->nodes[0];
5448 slot = path->slots[0];
5449 btrfs_item_key_to_cpu(eb, &found_key, slot);
5450 if (found_key.objectid != key.objectid ||
5451 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005452 /* If we're a hole then just pretend nothing changed */
5453 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005454 goto out;
5455 }
5456
5457 /*
5458 * We're now on 2a, 2b or 7.
5459 */
5460 key = found_key;
5461 while (key.offset < ekey->offset + left_len) {
5462 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5463 right_type = btrfs_file_extent_type(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005464 if (right_type != BTRFS_FILE_EXTENT_REG &&
5465 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005466 ret = 0;
5467 goto out;
5468 }
5469
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005470 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005471 right_len = btrfs_file_extent_ram_bytes(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005472 right_len = PAGE_ALIGN(right_len);
5473 } else {
5474 right_len = btrfs_file_extent_num_bytes(eb, ei);
5475 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005476
Alexander Block31db9f72012-07-25 23:19:24 +02005477 /*
5478 * Are we at extent 8? If yes, we know the extent is changed.
5479 * This may only happen on the first iteration.
5480 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005481 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005482 /* If we're a hole just pretend nothing changed */
5483 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005484 goto out;
5485 }
5486
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005487 /*
5488 * We just wanted to see if when we have an inline extent, what
5489 * follows it is a regular extent (wanted to check the above
5490 * condition for inline extents too). This should normally not
5491 * happen but it's possible for example when we have an inline
5492 * compressed extent representing data with a size matching
5493 * the page size (currently the same as sector size).
5494 */
5495 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5496 ret = 0;
5497 goto out;
5498 }
5499
Filipe Manana24e52b12017-07-06 15:31:46 +01005500 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5501 right_offset = btrfs_file_extent_offset(eb, ei);
5502 right_gen = btrfs_file_extent_generation(eb, ei);
5503
Alexander Block31db9f72012-07-25 23:19:24 +02005504 left_offset_fixed = left_offset;
5505 if (key.offset < ekey->offset) {
5506 /* Fix the right offset for 2a and 7. */
5507 right_offset += ekey->offset - key.offset;
5508 } else {
5509 /* Fix the left offset for all behind 2a and 2b */
5510 left_offset_fixed += key.offset - ekey->offset;
5511 }
5512
5513 /*
5514 * Check if we have the same extent.
5515 */
Alexander Block39540962012-08-01 12:46:05 +02005516 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005517 left_offset_fixed != right_offset ||
5518 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005519 ret = 0;
5520 goto out;
5521 }
5522
5523 /*
5524 * Go to the next extent.
5525 */
5526 ret = btrfs_next_item(sctx->parent_root, path);
5527 if (ret < 0)
5528 goto out;
5529 if (!ret) {
5530 eb = path->nodes[0];
5531 slot = path->slots[0];
5532 btrfs_item_key_to_cpu(eb, &found_key, slot);
5533 }
5534 if (ret || found_key.objectid != key.objectid ||
5535 found_key.type != key.type) {
5536 key.offset += right_len;
5537 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005538 }
5539 if (found_key.offset != key.offset + right_len) {
5540 ret = 0;
5541 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005542 }
5543 key = found_key;
5544 }
5545
5546 /*
5547 * We're now behind the left extent (treat as unchanged) or at the end
5548 * of the right side (treat as changed).
5549 */
5550 if (key.offset >= ekey->offset + left_len)
5551 ret = 1;
5552 else
5553 ret = 0;
5554
5555
5556out:
5557 btrfs_free_path(path);
5558 return ret;
5559}
5560
Josef Bacik16e75492013-10-22 12:18:51 -04005561static int get_last_extent(struct send_ctx *sctx, u64 offset)
5562{
5563 struct btrfs_path *path;
5564 struct btrfs_root *root = sctx->send_root;
5565 struct btrfs_file_extent_item *fi;
5566 struct btrfs_key key;
5567 u64 extent_end;
5568 u8 type;
5569 int ret;
5570
5571 path = alloc_path_for_send();
5572 if (!path)
5573 return -ENOMEM;
5574
5575 sctx->cur_inode_last_extent = 0;
5576
5577 key.objectid = sctx->cur_ino;
5578 key.type = BTRFS_EXTENT_DATA_KEY;
5579 key.offset = offset;
5580 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5581 if (ret < 0)
5582 goto out;
5583 ret = 0;
5584 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5585 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5586 goto out;
5587
5588 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5589 struct btrfs_file_extent_item);
5590 type = btrfs_file_extent_type(path->nodes[0], fi);
5591 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005592 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005593 extent_end = ALIGN(key.offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005594 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005595 } else {
5596 extent_end = key.offset +
5597 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5598 }
5599 sctx->cur_inode_last_extent = extent_end;
5600out:
5601 btrfs_free_path(path);
5602 return ret;
5603}
5604
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005605static int range_is_hole_in_parent(struct send_ctx *sctx,
5606 const u64 start,
5607 const u64 end)
5608{
5609 struct btrfs_path *path;
5610 struct btrfs_key key;
5611 struct btrfs_root *root = sctx->parent_root;
5612 u64 search_start = start;
5613 int ret;
5614
5615 path = alloc_path_for_send();
5616 if (!path)
5617 return -ENOMEM;
5618
5619 key.objectid = sctx->cur_ino;
5620 key.type = BTRFS_EXTENT_DATA_KEY;
5621 key.offset = search_start;
5622 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5623 if (ret < 0)
5624 goto out;
5625 if (ret > 0 && path->slots[0] > 0)
5626 path->slots[0]--;
5627
5628 while (search_start < end) {
5629 struct extent_buffer *leaf = path->nodes[0];
5630 int slot = path->slots[0];
5631 struct btrfs_file_extent_item *fi;
5632 u64 extent_end;
5633
5634 if (slot >= btrfs_header_nritems(leaf)) {
5635 ret = btrfs_next_leaf(root, path);
5636 if (ret < 0)
5637 goto out;
5638 else if (ret > 0)
5639 break;
5640 continue;
5641 }
5642
5643 btrfs_item_key_to_cpu(leaf, &key, slot);
5644 if (key.objectid < sctx->cur_ino ||
5645 key.type < BTRFS_EXTENT_DATA_KEY)
5646 goto next;
5647 if (key.objectid > sctx->cur_ino ||
5648 key.type > BTRFS_EXTENT_DATA_KEY ||
5649 key.offset >= end)
5650 break;
5651
5652 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5653 if (btrfs_file_extent_type(leaf, fi) ==
5654 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005655 u64 size = btrfs_file_extent_ram_bytes(leaf, fi);
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005656
5657 extent_end = ALIGN(key.offset + size,
5658 root->fs_info->sectorsize);
5659 } else {
5660 extent_end = key.offset +
5661 btrfs_file_extent_num_bytes(leaf, fi);
5662 }
5663 if (extent_end <= start)
5664 goto next;
5665 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5666 search_start = extent_end;
5667 goto next;
5668 }
5669 ret = 0;
5670 goto out;
5671next:
5672 path->slots[0]++;
5673 }
5674 ret = 1;
5675out:
5676 btrfs_free_path(path);
5677 return ret;
5678}
5679
Josef Bacik16e75492013-10-22 12:18:51 -04005680static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5681 struct btrfs_key *key)
5682{
5683 struct btrfs_file_extent_item *fi;
5684 u64 extent_end;
5685 u8 type;
5686 int ret = 0;
5687
5688 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5689 return 0;
5690
5691 if (sctx->cur_inode_last_extent == (u64)-1) {
5692 ret = get_last_extent(sctx, key->offset - 1);
5693 if (ret)
5694 return ret;
5695 }
5696
5697 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5698 struct btrfs_file_extent_item);
5699 type = btrfs_file_extent_type(path->nodes[0], fi);
5700 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005701 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005702 extent_end = ALIGN(key->offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005703 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005704 } else {
5705 extent_end = key->offset +
5706 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5707 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005708
5709 if (path->slots[0] == 0 &&
5710 sctx->cur_inode_last_extent < key->offset) {
5711 /*
5712 * We might have skipped entire leafs that contained only
5713 * file extent items for our current inode. These leafs have
5714 * a generation number smaller (older) than the one in the
5715 * current leaf and the leaf our last extent came from, and
5716 * are located between these 2 leafs.
5717 */
5718 ret = get_last_extent(sctx, key->offset - 1);
5719 if (ret)
5720 return ret;
5721 }
5722
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005723 if (sctx->cur_inode_last_extent < key->offset) {
5724 ret = range_is_hole_in_parent(sctx,
5725 sctx->cur_inode_last_extent,
5726 key->offset);
5727 if (ret < 0)
5728 return ret;
5729 else if (ret == 0)
5730 ret = send_hole(sctx, key->offset);
5731 else
5732 ret = 0;
5733 }
Josef Bacik16e75492013-10-22 12:18:51 -04005734 sctx->cur_inode_last_extent = extent_end;
5735 return ret;
5736}
5737
Alexander Block31db9f72012-07-25 23:19:24 +02005738static int process_extent(struct send_ctx *sctx,
5739 struct btrfs_path *path,
5740 struct btrfs_key *key)
5741{
Alexander Block31db9f72012-07-25 23:19:24 +02005742 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005743 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005744
5745 if (S_ISLNK(sctx->cur_inode_mode))
5746 return 0;
5747
5748 if (sctx->parent_root && !sctx->cur_inode_new) {
5749 ret = is_extent_unchanged(sctx, path, key);
5750 if (ret < 0)
5751 goto out;
5752 if (ret) {
5753 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005754 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005755 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005756 } else {
5757 struct btrfs_file_extent_item *ei;
5758 u8 type;
5759
5760 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5761 struct btrfs_file_extent_item);
5762 type = btrfs_file_extent_type(path->nodes[0], ei);
5763 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5764 type == BTRFS_FILE_EXTENT_REG) {
5765 /*
5766 * The send spec does not have a prealloc command yet,
5767 * so just leave a hole for prealloc'ed extents until
5768 * we have enough commands queued up to justify rev'ing
5769 * the send spec.
5770 */
5771 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5772 ret = 0;
5773 goto out;
5774 }
5775
5776 /* Have a hole, just skip it. */
5777 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5778 ret = 0;
5779 goto out;
5780 }
5781 }
Alexander Block31db9f72012-07-25 23:19:24 +02005782 }
5783
5784 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5785 sctx->cur_inode_size, &found_clone);
5786 if (ret != -ENOENT && ret < 0)
5787 goto out;
5788
5789 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005790 if (ret)
5791 goto out;
5792out_hole:
5793 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005794out:
5795 return ret;
5796}
5797
5798static int process_all_extents(struct send_ctx *sctx)
5799{
5800 int ret;
5801 struct btrfs_root *root;
5802 struct btrfs_path *path;
5803 struct btrfs_key key;
5804 struct btrfs_key found_key;
5805 struct extent_buffer *eb;
5806 int slot;
5807
5808 root = sctx->send_root;
5809 path = alloc_path_for_send();
5810 if (!path)
5811 return -ENOMEM;
5812
5813 key.objectid = sctx->cmp_key->objectid;
5814 key.type = BTRFS_EXTENT_DATA_KEY;
5815 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005816 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5817 if (ret < 0)
5818 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005819
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005820 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005821 eb = path->nodes[0];
5822 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005823
5824 if (slot >= btrfs_header_nritems(eb)) {
5825 ret = btrfs_next_leaf(root, path);
5826 if (ret < 0) {
5827 goto out;
5828 } else if (ret > 0) {
5829 ret = 0;
5830 break;
5831 }
5832 continue;
5833 }
5834
Alexander Block31db9f72012-07-25 23:19:24 +02005835 btrfs_item_key_to_cpu(eb, &found_key, slot);
5836
5837 if (found_key.objectid != key.objectid ||
5838 found_key.type != key.type) {
5839 ret = 0;
5840 goto out;
5841 }
5842
5843 ret = process_extent(sctx, path, &found_key);
5844 if (ret < 0)
5845 goto out;
5846
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005847 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005848 }
5849
5850out:
5851 btrfs_free_path(path);
5852 return ret;
5853}
5854
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005855static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5856 int *pending_move,
5857 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005858{
5859 int ret = 0;
5860
5861 if (sctx->cur_ino == 0)
5862 goto out;
5863 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005864 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005865 goto out;
5866 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5867 goto out;
5868
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005869 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005870 if (ret < 0)
5871 goto out;
5872
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005873 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005874out:
5875 return ret;
5876}
5877
5878static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5879{
5880 int ret = 0;
5881 u64 left_mode;
5882 u64 left_uid;
5883 u64 left_gid;
5884 u64 right_mode;
5885 u64 right_uid;
5886 u64 right_gid;
5887 int need_chmod = 0;
5888 int need_chown = 0;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005889 int need_truncate = 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005890 int pending_move = 0;
5891 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005892
Filipe Manana46b2f452018-07-24 11:54:04 +01005893 if (sctx->ignore_cur_inode)
5894 return 0;
5895
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005896 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5897 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005898 if (ret < 0)
5899 goto out;
5900
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005901 /*
5902 * We have processed the refs and thus need to advance send_progress.
5903 * Now, calls to get_cur_xxx will take the updated refs of the current
5904 * inode into account.
5905 *
5906 * On the other hand, if our current inode is a directory and couldn't
5907 * be moved/renamed because its parent was renamed/moved too and it has
5908 * a higher inode number, we can only move/rename our current inode
5909 * after we moved/renamed its parent. Therefore in this case operate on
5910 * the old path (pre move/rename) of our current inode, and the
5911 * move/rename will be performed later.
5912 */
5913 if (refs_processed && !pending_move)
5914 sctx->send_progress = sctx->cur_ino + 1;
5915
Alexander Block31db9f72012-07-25 23:19:24 +02005916 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5917 goto out;
5918 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5919 goto out;
5920
5921 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005922 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005923 if (ret < 0)
5924 goto out;
5925
Alex Lyakase2d044f2012-10-17 13:52:47 +00005926 if (!sctx->parent_root || sctx->cur_inode_new) {
5927 need_chown = 1;
5928 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005929 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005930 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
5931 need_truncate = 0;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005932 } else {
Filipe Mananaffa7c422018-02-06 20:40:40 +00005933 u64 old_size;
5934
Alex Lyakase2d044f2012-10-17 13:52:47 +00005935 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
Filipe Mananaffa7c422018-02-06 20:40:40 +00005936 &old_size, NULL, &right_mode, &right_uid,
Alex Lyakase2d044f2012-10-17 13:52:47 +00005937 &right_gid, NULL);
5938 if (ret < 0)
5939 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005940
Alex Lyakase2d044f2012-10-17 13:52:47 +00005941 if (left_uid != right_uid || left_gid != right_gid)
5942 need_chown = 1;
5943 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5944 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005945 if ((old_size == sctx->cur_inode_size) ||
5946 (sctx->cur_inode_size > old_size &&
5947 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
5948 need_truncate = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005949 }
5950
5951 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005952 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005953 if (sctx->cur_inode_last_extent == (u64)-1 ||
5954 sctx->cur_inode_last_extent <
5955 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005956 ret = get_last_extent(sctx, (u64)-1);
5957 if (ret)
5958 goto out;
5959 }
5960 if (sctx->cur_inode_last_extent <
5961 sctx->cur_inode_size) {
5962 ret = send_hole(sctx, sctx->cur_inode_size);
5963 if (ret)
5964 goto out;
5965 }
5966 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005967 if (need_truncate) {
5968 ret = send_truncate(sctx, sctx->cur_ino,
5969 sctx->cur_inode_gen,
5970 sctx->cur_inode_size);
5971 if (ret < 0)
5972 goto out;
5973 }
Alexander Block31db9f72012-07-25 23:19:24 +02005974 }
5975
5976 if (need_chown) {
5977 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5978 left_uid, left_gid);
5979 if (ret < 0)
5980 goto out;
5981 }
5982 if (need_chmod) {
5983 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5984 left_mode);
5985 if (ret < 0)
5986 goto out;
5987 }
5988
5989 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005990 * If other directory inodes depended on our current directory
5991 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005992 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005993 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5994 ret = apply_children_dir_moves(sctx);
5995 if (ret)
5996 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005997 /*
5998 * Need to send that every time, no matter if it actually
5999 * changed between the two trees as we have done changes to
6000 * the inode before. If our inode is a directory and it's
6001 * waiting to be moved/renamed, we will send its utimes when
6002 * it's moved/renamed, therefore we don't need to do it here.
6003 */
6004 sctx->send_progress = sctx->cur_ino + 1;
6005 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6006 if (ret < 0)
6007 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006008 }
6009
Alexander Block31db9f72012-07-25 23:19:24 +02006010out:
6011 return ret;
6012}
6013
Filipe Manana46b2f452018-07-24 11:54:04 +01006014struct parent_paths_ctx {
6015 struct list_head *refs;
6016 struct send_ctx *sctx;
6017};
6018
6019static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6020 void *ctx)
6021{
6022 struct parent_paths_ctx *ppctx = ctx;
6023
6024 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6025 ppctx->refs);
6026}
6027
6028/*
6029 * Issue unlink operations for all paths of the current inode found in the
6030 * parent snapshot.
6031 */
6032static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6033{
6034 LIST_HEAD(deleted_refs);
6035 struct btrfs_path *path;
6036 struct btrfs_key key;
6037 struct parent_paths_ctx ctx;
6038 int ret;
6039
6040 path = alloc_path_for_send();
6041 if (!path)
6042 return -ENOMEM;
6043
6044 key.objectid = sctx->cur_ino;
6045 key.type = BTRFS_INODE_REF_KEY;
6046 key.offset = 0;
6047 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
6048 if (ret < 0)
6049 goto out;
6050
6051 ctx.refs = &deleted_refs;
6052 ctx.sctx = sctx;
6053
6054 while (true) {
6055 struct extent_buffer *eb = path->nodes[0];
6056 int slot = path->slots[0];
6057
6058 if (slot >= btrfs_header_nritems(eb)) {
6059 ret = btrfs_next_leaf(sctx->parent_root, path);
6060 if (ret < 0)
6061 goto out;
6062 else if (ret > 0)
6063 break;
6064 continue;
6065 }
6066
6067 btrfs_item_key_to_cpu(eb, &key, slot);
6068 if (key.objectid != sctx->cur_ino)
6069 break;
6070 if (key.type != BTRFS_INODE_REF_KEY &&
6071 key.type != BTRFS_INODE_EXTREF_KEY)
6072 break;
6073
6074 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
6075 record_parent_ref, &ctx);
6076 if (ret < 0)
6077 goto out;
6078
6079 path->slots[0]++;
6080 }
6081
6082 while (!list_empty(&deleted_refs)) {
6083 struct recorded_ref *ref;
6084
6085 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6086 ret = send_unlink(sctx, ref->full_path);
6087 if (ret < 0)
6088 goto out;
6089 fs_path_free(ref->full_path);
6090 list_del(&ref->list);
6091 kfree(ref);
6092 }
6093 ret = 0;
6094out:
6095 btrfs_free_path(path);
6096 if (ret)
6097 __free_recorded_refs(&deleted_refs);
6098 return ret;
6099}
6100
Alexander Block31db9f72012-07-25 23:19:24 +02006101static int changed_inode(struct send_ctx *sctx,
6102 enum btrfs_compare_tree_result result)
6103{
6104 int ret = 0;
6105 struct btrfs_key *key = sctx->cmp_key;
6106 struct btrfs_inode_item *left_ii = NULL;
6107 struct btrfs_inode_item *right_ii = NULL;
6108 u64 left_gen = 0;
6109 u64 right_gen = 0;
6110
Alexander Block31db9f72012-07-25 23:19:24 +02006111 sctx->cur_ino = key->objectid;
6112 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006113 sctx->cur_inode_last_extent = (u64)-1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006114 sctx->cur_inode_next_write_offset = 0;
Filipe Manana46b2f452018-07-24 11:54:04 +01006115 sctx->ignore_cur_inode = false;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006116
6117 /*
6118 * Set send_progress to current inode. This will tell all get_cur_xxx
6119 * functions that the current inode's refs are not updated yet. Later,
6120 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6121 */
Alexander Block31db9f72012-07-25 23:19:24 +02006122 sctx->send_progress = sctx->cur_ino;
6123
6124 if (result == BTRFS_COMPARE_TREE_NEW ||
6125 result == BTRFS_COMPARE_TREE_CHANGED) {
6126 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6127 sctx->left_path->slots[0],
6128 struct btrfs_inode_item);
6129 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6130 left_ii);
6131 } else {
6132 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6133 sctx->right_path->slots[0],
6134 struct btrfs_inode_item);
6135 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6136 right_ii);
6137 }
6138 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6139 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6140 sctx->right_path->slots[0],
6141 struct btrfs_inode_item);
6142
6143 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6144 right_ii);
Alexander Block6d85ed052012-08-01 14:48:59 +02006145
6146 /*
6147 * The cur_ino = root dir case is special here. We can't treat
6148 * the inode as deleted+reused because it would generate a
6149 * stream that tries to delete/mkdir the root dir.
6150 */
6151 if (left_gen != right_gen &&
6152 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02006153 sctx->cur_inode_new_gen = 1;
6154 }
6155
Filipe Manana46b2f452018-07-24 11:54:04 +01006156 /*
6157 * Normally we do not find inodes with a link count of zero (orphans)
6158 * because the most common case is to create a snapshot and use it
6159 * for a send operation. However other less common use cases involve
6160 * using a subvolume and send it after turning it to RO mode just
6161 * after deleting all hard links of a file while holding an open
6162 * file descriptor against it or turning a RO snapshot into RW mode,
6163 * keep an open file descriptor against a file, delete it and then
6164 * turn the snapshot back to RO mode before using it for a send
6165 * operation. So if we find such cases, ignore the inode and all its
6166 * items completely if it's a new inode, or if it's a changed inode
6167 * make sure all its previous paths (from the parent snapshot) are all
6168 * unlinked and all other the inode items are ignored.
6169 */
6170 if (result == BTRFS_COMPARE_TREE_NEW ||
6171 result == BTRFS_COMPARE_TREE_CHANGED) {
6172 u32 nlinks;
6173
6174 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6175 if (nlinks == 0) {
6176 sctx->ignore_cur_inode = true;
6177 if (result == BTRFS_COMPARE_TREE_CHANGED)
6178 ret = btrfs_unlink_all_paths(sctx);
6179 goto out;
6180 }
6181 }
6182
Alexander Block31db9f72012-07-25 23:19:24 +02006183 if (result == BTRFS_COMPARE_TREE_NEW) {
6184 sctx->cur_inode_gen = left_gen;
6185 sctx->cur_inode_new = 1;
6186 sctx->cur_inode_deleted = 0;
6187 sctx->cur_inode_size = btrfs_inode_size(
6188 sctx->left_path->nodes[0], left_ii);
6189 sctx->cur_inode_mode = btrfs_inode_mode(
6190 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006191 sctx->cur_inode_rdev = btrfs_inode_rdev(
6192 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02006193 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02006194 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006195 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6196 sctx->cur_inode_gen = right_gen;
6197 sctx->cur_inode_new = 0;
6198 sctx->cur_inode_deleted = 1;
6199 sctx->cur_inode_size = btrfs_inode_size(
6200 sctx->right_path->nodes[0], right_ii);
6201 sctx->cur_inode_mode = btrfs_inode_mode(
6202 sctx->right_path->nodes[0], right_ii);
6203 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02006204 /*
6205 * We need to do some special handling in case the inode was
6206 * reported as changed with a changed generation number. This
6207 * means that the original inode was deleted and new inode
6208 * reused the same inum. So we have to treat the old inode as
6209 * deleted and the new one as new.
6210 */
Alexander Block31db9f72012-07-25 23:19:24 +02006211 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02006212 /*
6213 * First, process the inode as if it was deleted.
6214 */
Alexander Block31db9f72012-07-25 23:19:24 +02006215 sctx->cur_inode_gen = right_gen;
6216 sctx->cur_inode_new = 0;
6217 sctx->cur_inode_deleted = 1;
6218 sctx->cur_inode_size = btrfs_inode_size(
6219 sctx->right_path->nodes[0], right_ii);
6220 sctx->cur_inode_mode = btrfs_inode_mode(
6221 sctx->right_path->nodes[0], right_ii);
6222 ret = process_all_refs(sctx,
6223 BTRFS_COMPARE_TREE_DELETED);
6224 if (ret < 0)
6225 goto out;
6226
Alexander Block766702e2012-07-28 14:11:31 +02006227 /*
6228 * Now process the inode as if it was new.
6229 */
Alexander Block31db9f72012-07-25 23:19:24 +02006230 sctx->cur_inode_gen = left_gen;
6231 sctx->cur_inode_new = 1;
6232 sctx->cur_inode_deleted = 0;
6233 sctx->cur_inode_size = btrfs_inode_size(
6234 sctx->left_path->nodes[0], left_ii);
6235 sctx->cur_inode_mode = btrfs_inode_mode(
6236 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006237 sctx->cur_inode_rdev = btrfs_inode_rdev(
6238 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02006239 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006240 if (ret < 0)
6241 goto out;
6242
6243 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6244 if (ret < 0)
6245 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006246 /*
6247 * Advance send_progress now as we did not get into
6248 * process_recorded_refs_if_needed in the new_gen case.
6249 */
6250 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02006251
6252 /*
6253 * Now process all extents and xattrs of the inode as if
6254 * they were all new.
6255 */
Alexander Block31db9f72012-07-25 23:19:24 +02006256 ret = process_all_extents(sctx);
6257 if (ret < 0)
6258 goto out;
6259 ret = process_all_new_xattrs(sctx);
6260 if (ret < 0)
6261 goto out;
6262 } else {
6263 sctx->cur_inode_gen = left_gen;
6264 sctx->cur_inode_new = 0;
6265 sctx->cur_inode_new_gen = 0;
6266 sctx->cur_inode_deleted = 0;
6267 sctx->cur_inode_size = btrfs_inode_size(
6268 sctx->left_path->nodes[0], left_ii);
6269 sctx->cur_inode_mode = btrfs_inode_mode(
6270 sctx->left_path->nodes[0], left_ii);
6271 }
6272 }
6273
6274out:
6275 return ret;
6276}
6277
Alexander Block766702e2012-07-28 14:11:31 +02006278/*
6279 * We have to process new refs before deleted refs, but compare_trees gives us
6280 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6281 * first and later process them in process_recorded_refs.
6282 * For the cur_inode_new_gen case, we skip recording completely because
6283 * changed_inode did already initiate processing of refs. The reason for this is
6284 * that in this case, compare_tree actually compares the refs of 2 different
6285 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6286 * refs of the right tree as deleted and all refs of the left tree as new.
6287 */
Alexander Block31db9f72012-07-25 23:19:24 +02006288static int changed_ref(struct send_ctx *sctx,
6289 enum btrfs_compare_tree_result result)
6290{
6291 int ret = 0;
6292
Filipe Manana95155582016-08-01 01:50:37 +01006293 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6294 inconsistent_snapshot_error(sctx, result, "reference");
6295 return -EIO;
6296 }
Alexander Block31db9f72012-07-25 23:19:24 +02006297
6298 if (!sctx->cur_inode_new_gen &&
6299 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6300 if (result == BTRFS_COMPARE_TREE_NEW)
6301 ret = record_new_ref(sctx);
6302 else if (result == BTRFS_COMPARE_TREE_DELETED)
6303 ret = record_deleted_ref(sctx);
6304 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6305 ret = record_changed_ref(sctx);
6306 }
6307
6308 return ret;
6309}
6310
Alexander Block766702e2012-07-28 14:11:31 +02006311/*
6312 * Process new/deleted/changed xattrs. We skip processing in the
6313 * cur_inode_new_gen case because changed_inode did already initiate processing
6314 * of xattrs. The reason is the same as in changed_ref
6315 */
Alexander Block31db9f72012-07-25 23:19:24 +02006316static int changed_xattr(struct send_ctx *sctx,
6317 enum btrfs_compare_tree_result result)
6318{
6319 int ret = 0;
6320
Filipe Manana95155582016-08-01 01:50:37 +01006321 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6322 inconsistent_snapshot_error(sctx, result, "xattr");
6323 return -EIO;
6324 }
Alexander Block31db9f72012-07-25 23:19:24 +02006325
6326 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6327 if (result == BTRFS_COMPARE_TREE_NEW)
6328 ret = process_new_xattr(sctx);
6329 else if (result == BTRFS_COMPARE_TREE_DELETED)
6330 ret = process_deleted_xattr(sctx);
6331 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6332 ret = process_changed_xattr(sctx);
6333 }
6334
6335 return ret;
6336}
6337
Alexander Block766702e2012-07-28 14:11:31 +02006338/*
6339 * Process new/deleted/changed extents. We skip processing in the
6340 * cur_inode_new_gen case because changed_inode did already initiate processing
6341 * of extents. The reason is the same as in changed_ref
6342 */
Alexander Block31db9f72012-07-25 23:19:24 +02006343static int changed_extent(struct send_ctx *sctx,
6344 enum btrfs_compare_tree_result result)
6345{
6346 int ret = 0;
6347
Filipe Mananab4f9a1a2019-07-17 13:23:39 +01006348 /*
6349 * We have found an extent item that changed without the inode item
6350 * having changed. This can happen either after relocation (where the
6351 * disk_bytenr of an extent item is replaced at
6352 * relocation.c:replace_file_extents()) or after deduplication into a
6353 * file in both the parent and send snapshots (where an extent item can
6354 * get modified or replaced with a new one). Note that deduplication
6355 * updates the inode item, but it only changes the iversion (sequence
6356 * field in the inode item) of the inode, so if a file is deduplicated
6357 * the same amount of times in both the parent and send snapshots, its
6358 * iversion becames the same in both snapshots, whence the inode item is
6359 * the same on both snapshots.
6360 */
6361 if (sctx->cur_ino != sctx->cmp_key->objectid)
6362 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006363
6364 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6365 if (result != BTRFS_COMPARE_TREE_DELETED)
6366 ret = process_extent(sctx, sctx->left_path,
6367 sctx->cmp_key);
6368 }
6369
6370 return ret;
6371}
6372
Josef Bacikba5e8f22013-08-16 16:52:55 -04006373static int dir_changed(struct send_ctx *sctx, u64 dir)
6374{
6375 u64 orig_gen, new_gen;
6376 int ret;
6377
6378 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6379 NULL, NULL);
6380 if (ret)
6381 return ret;
6382
6383 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6384 NULL, NULL, NULL);
6385 if (ret)
6386 return ret;
6387
6388 return (orig_gen != new_gen) ? 1 : 0;
6389}
6390
6391static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6392 struct btrfs_key *key)
6393{
6394 struct btrfs_inode_extref *extref;
6395 struct extent_buffer *leaf;
6396 u64 dirid = 0, last_dirid = 0;
6397 unsigned long ptr;
6398 u32 item_size;
6399 u32 cur_offset = 0;
6400 int ref_name_len;
6401 int ret = 0;
6402
6403 /* Easy case, just check this one dirid */
6404 if (key->type == BTRFS_INODE_REF_KEY) {
6405 dirid = key->offset;
6406
6407 ret = dir_changed(sctx, dirid);
6408 goto out;
6409 }
6410
6411 leaf = path->nodes[0];
6412 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6413 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6414 while (cur_offset < item_size) {
6415 extref = (struct btrfs_inode_extref *)(ptr +
6416 cur_offset);
6417 dirid = btrfs_inode_extref_parent(leaf, extref);
6418 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6419 cur_offset += ref_name_len + sizeof(*extref);
6420 if (dirid == last_dirid)
6421 continue;
6422 ret = dir_changed(sctx, dirid);
6423 if (ret)
6424 break;
6425 last_dirid = dirid;
6426 }
6427out:
6428 return ret;
6429}
6430
Alexander Block766702e2012-07-28 14:11:31 +02006431/*
6432 * Updates compare related fields in sctx and simply forwards to the actual
6433 * changed_xxx functions.
6434 */
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006435static int changed_cb(struct btrfs_path *left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02006436 struct btrfs_path *right_path,
6437 struct btrfs_key *key,
6438 enum btrfs_compare_tree_result result,
6439 void *ctx)
6440{
6441 int ret = 0;
6442 struct send_ctx *sctx = ctx;
6443
Josef Bacikba5e8f22013-08-16 16:52:55 -04006444 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04006445 if (key->type == BTRFS_INODE_REF_KEY ||
6446 key->type == BTRFS_INODE_EXTREF_KEY) {
6447 ret = compare_refs(sctx, left_path, key);
6448 if (!ret)
6449 return 0;
6450 if (ret < 0)
6451 return ret;
6452 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6453 return maybe_send_hole(sctx, left_path, key);
6454 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04006455 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006456 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04006457 result = BTRFS_COMPARE_TREE_CHANGED;
6458 ret = 0;
6459 }
6460
Alexander Block31db9f72012-07-25 23:19:24 +02006461 sctx->left_path = left_path;
6462 sctx->right_path = right_path;
6463 sctx->cmp_key = key;
6464
6465 ret = finish_inode_if_needed(sctx, 0);
6466 if (ret < 0)
6467 goto out;
6468
Alexander Block2981e222012-08-01 14:47:03 +02006469 /* Ignore non-FS objects */
6470 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6471 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6472 goto out;
6473
Filipe Manana46b2f452018-07-24 11:54:04 +01006474 if (key->type == BTRFS_INODE_ITEM_KEY) {
Alexander Block31db9f72012-07-25 23:19:24 +02006475 ret = changed_inode(sctx, result);
Filipe Manana46b2f452018-07-24 11:54:04 +01006476 } else if (!sctx->ignore_cur_inode) {
6477 if (key->type == BTRFS_INODE_REF_KEY ||
6478 key->type == BTRFS_INODE_EXTREF_KEY)
6479 ret = changed_ref(sctx, result);
6480 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6481 ret = changed_xattr(sctx, result);
6482 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6483 ret = changed_extent(sctx, result);
6484 }
Alexander Block31db9f72012-07-25 23:19:24 +02006485
6486out:
6487 return ret;
6488}
6489
6490static int full_send_tree(struct send_ctx *sctx)
6491{
6492 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006493 struct btrfs_root *send_root = sctx->send_root;
6494 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006495 struct btrfs_path *path;
6496 struct extent_buffer *eb;
6497 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006498
6499 path = alloc_path_for_send();
6500 if (!path)
6501 return -ENOMEM;
6502
Alexander Block31db9f72012-07-25 23:19:24 +02006503 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6504 key.type = BTRFS_INODE_ITEM_KEY;
6505 key.offset = 0;
6506
Alexander Block31db9f72012-07-25 23:19:24 +02006507 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6508 if (ret < 0)
6509 goto out;
6510 if (ret)
6511 goto out_finish;
6512
6513 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006514 eb = path->nodes[0];
6515 slot = path->slots[0];
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006516 btrfs_item_key_to_cpu(eb, &key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02006517
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006518 ret = changed_cb(path, NULL, &key,
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006519 BTRFS_COMPARE_TREE_NEW, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006520 if (ret < 0)
6521 goto out;
6522
Alexander Block31db9f72012-07-25 23:19:24 +02006523 ret = btrfs_next_item(send_root, path);
6524 if (ret < 0)
6525 goto out;
6526 if (ret) {
6527 ret = 0;
6528 break;
6529 }
6530 }
6531
6532out_finish:
6533 ret = finish_inode_if_needed(sctx, 1);
6534
6535out:
6536 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006537 return ret;
6538}
6539
David Sterba18d0f5c2019-08-21 19:12:59 +02006540static int tree_move_down(struct btrfs_path *path, int *level)
6541{
6542 struct extent_buffer *eb;
6543
6544 BUG_ON(*level == 0);
6545 eb = btrfs_read_node_slot(path->nodes[*level], path->slots[*level]);
6546 if (IS_ERR(eb))
6547 return PTR_ERR(eb);
6548
6549 path->nodes[*level - 1] = eb;
6550 path->slots[*level - 1] = 0;
6551 (*level)--;
6552 return 0;
6553}
6554
6555static int tree_move_next_or_upnext(struct btrfs_path *path,
6556 int *level, int root_level)
6557{
6558 int ret = 0;
6559 int nritems;
6560 nritems = btrfs_header_nritems(path->nodes[*level]);
6561
6562 path->slots[*level]++;
6563
6564 while (path->slots[*level] >= nritems) {
6565 if (*level == root_level)
6566 return -1;
6567
6568 /* move upnext */
6569 path->slots[*level] = 0;
6570 free_extent_buffer(path->nodes[*level]);
6571 path->nodes[*level] = NULL;
6572 (*level)++;
6573 path->slots[*level]++;
6574
6575 nritems = btrfs_header_nritems(path->nodes[*level]);
6576 ret = 1;
6577 }
6578 return ret;
6579}
6580
6581/*
6582 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
6583 * or down.
6584 */
6585static int tree_advance(struct btrfs_path *path,
6586 int *level, int root_level,
6587 int allow_down,
6588 struct btrfs_key *key)
6589{
6590 int ret;
6591
6592 if (*level == 0 || !allow_down) {
6593 ret = tree_move_next_or_upnext(path, level, root_level);
6594 } else {
6595 ret = tree_move_down(path, level);
6596 }
6597 if (ret >= 0) {
6598 if (*level == 0)
6599 btrfs_item_key_to_cpu(path->nodes[*level], key,
6600 path->slots[*level]);
6601 else
6602 btrfs_node_key_to_cpu(path->nodes[*level], key,
6603 path->slots[*level]);
6604 }
6605 return ret;
6606}
6607
6608static int tree_compare_item(struct btrfs_path *left_path,
6609 struct btrfs_path *right_path,
6610 char *tmp_buf)
6611{
6612 int cmp;
6613 int len1, len2;
6614 unsigned long off1, off2;
6615
6616 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
6617 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
6618 if (len1 != len2)
6619 return 1;
6620
6621 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
6622 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
6623 right_path->slots[0]);
6624
6625 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
6626
6627 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
6628 if (cmp)
6629 return 1;
6630 return 0;
6631}
6632
6633/*
6634 * This function compares two trees and calls the provided callback for
6635 * every changed/new/deleted item it finds.
6636 * If shared tree blocks are encountered, whole subtrees are skipped, making
6637 * the compare pretty fast on snapshotted subvolumes.
6638 *
6639 * This currently works on commit roots only. As commit roots are read only,
6640 * we don't do any locking. The commit roots are protected with transactions.
6641 * Transactions are ended and rejoined when a commit is tried in between.
6642 *
6643 * This function checks for modifications done to the trees while comparing.
6644 * If it detects a change, it aborts immediately.
6645 */
6646static int btrfs_compare_trees(struct btrfs_root *left_root,
6647 struct btrfs_root *right_root,
6648 btrfs_changed_cb_t changed_cb, void *ctx)
6649{
6650 struct btrfs_fs_info *fs_info = left_root->fs_info;
6651 int ret;
6652 int cmp;
6653 struct btrfs_path *left_path = NULL;
6654 struct btrfs_path *right_path = NULL;
6655 struct btrfs_key left_key;
6656 struct btrfs_key right_key;
6657 char *tmp_buf = NULL;
6658 int left_root_level;
6659 int right_root_level;
6660 int left_level;
6661 int right_level;
6662 int left_end_reached;
6663 int right_end_reached;
6664 int advance_left;
6665 int advance_right;
6666 u64 left_blockptr;
6667 u64 right_blockptr;
6668 u64 left_gen;
6669 u64 right_gen;
6670
6671 left_path = btrfs_alloc_path();
6672 if (!left_path) {
6673 ret = -ENOMEM;
6674 goto out;
6675 }
6676 right_path = btrfs_alloc_path();
6677 if (!right_path) {
6678 ret = -ENOMEM;
6679 goto out;
6680 }
6681
6682 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
6683 if (!tmp_buf) {
6684 ret = -ENOMEM;
6685 goto out;
6686 }
6687
6688 left_path->search_commit_root = 1;
6689 left_path->skip_locking = 1;
6690 right_path->search_commit_root = 1;
6691 right_path->skip_locking = 1;
6692
6693 /*
6694 * Strategy: Go to the first items of both trees. Then do
6695 *
6696 * If both trees are at level 0
6697 * Compare keys of current items
6698 * If left < right treat left item as new, advance left tree
6699 * and repeat
6700 * If left > right treat right item as deleted, advance right tree
6701 * and repeat
6702 * If left == right do deep compare of items, treat as changed if
6703 * needed, advance both trees and repeat
6704 * If both trees are at the same level but not at level 0
6705 * Compare keys of current nodes/leafs
6706 * If left < right advance left tree and repeat
6707 * If left > right advance right tree and repeat
6708 * If left == right compare blockptrs of the next nodes/leafs
6709 * If they match advance both trees but stay at the same level
6710 * and repeat
6711 * If they don't match advance both trees while allowing to go
6712 * deeper and repeat
6713 * If tree levels are different
6714 * Advance the tree that needs it and repeat
6715 *
6716 * Advancing a tree means:
6717 * If we are at level 0, try to go to the next slot. If that's not
6718 * possible, go one level up and repeat. Stop when we found a level
6719 * where we could go to the next slot. We may at this point be on a
6720 * node or a leaf.
6721 *
6722 * If we are not at level 0 and not on shared tree blocks, go one
6723 * level deeper.
6724 *
6725 * If we are not at level 0 and on shared tree blocks, go one slot to
6726 * the right if possible or go up and right.
6727 */
6728
6729 down_read(&fs_info->commit_root_sem);
6730 left_level = btrfs_header_level(left_root->commit_root);
6731 left_root_level = left_level;
6732 left_path->nodes[left_level] =
6733 btrfs_clone_extent_buffer(left_root->commit_root);
6734 if (!left_path->nodes[left_level]) {
6735 up_read(&fs_info->commit_root_sem);
6736 ret = -ENOMEM;
6737 goto out;
6738 }
6739
6740 right_level = btrfs_header_level(right_root->commit_root);
6741 right_root_level = right_level;
6742 right_path->nodes[right_level] =
6743 btrfs_clone_extent_buffer(right_root->commit_root);
6744 if (!right_path->nodes[right_level]) {
6745 up_read(&fs_info->commit_root_sem);
6746 ret = -ENOMEM;
6747 goto out;
6748 }
6749 up_read(&fs_info->commit_root_sem);
6750
6751 if (left_level == 0)
6752 btrfs_item_key_to_cpu(left_path->nodes[left_level],
6753 &left_key, left_path->slots[left_level]);
6754 else
6755 btrfs_node_key_to_cpu(left_path->nodes[left_level],
6756 &left_key, left_path->slots[left_level]);
6757 if (right_level == 0)
6758 btrfs_item_key_to_cpu(right_path->nodes[right_level],
6759 &right_key, right_path->slots[right_level]);
6760 else
6761 btrfs_node_key_to_cpu(right_path->nodes[right_level],
6762 &right_key, right_path->slots[right_level]);
6763
6764 left_end_reached = right_end_reached = 0;
6765 advance_left = advance_right = 0;
6766
6767 while (1) {
Nikolay Borisov6af112b2019-09-04 19:33:58 +03006768 cond_resched();
David Sterba18d0f5c2019-08-21 19:12:59 +02006769 if (advance_left && !left_end_reached) {
6770 ret = tree_advance(left_path, &left_level,
6771 left_root_level,
6772 advance_left != ADVANCE_ONLY_NEXT,
6773 &left_key);
6774 if (ret == -1)
6775 left_end_reached = ADVANCE;
6776 else if (ret < 0)
6777 goto out;
6778 advance_left = 0;
6779 }
6780 if (advance_right && !right_end_reached) {
6781 ret = tree_advance(right_path, &right_level,
6782 right_root_level,
6783 advance_right != ADVANCE_ONLY_NEXT,
6784 &right_key);
6785 if (ret == -1)
6786 right_end_reached = ADVANCE;
6787 else if (ret < 0)
6788 goto out;
6789 advance_right = 0;
6790 }
6791
6792 if (left_end_reached && right_end_reached) {
6793 ret = 0;
6794 goto out;
6795 } else if (left_end_reached) {
6796 if (right_level == 0) {
6797 ret = changed_cb(left_path, right_path,
6798 &right_key,
6799 BTRFS_COMPARE_TREE_DELETED,
6800 ctx);
6801 if (ret < 0)
6802 goto out;
6803 }
6804 advance_right = ADVANCE;
6805 continue;
6806 } else if (right_end_reached) {
6807 if (left_level == 0) {
6808 ret = changed_cb(left_path, right_path,
6809 &left_key,
6810 BTRFS_COMPARE_TREE_NEW,
6811 ctx);
6812 if (ret < 0)
6813 goto out;
6814 }
6815 advance_left = ADVANCE;
6816 continue;
6817 }
6818
6819 if (left_level == 0 && right_level == 0) {
6820 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
6821 if (cmp < 0) {
6822 ret = changed_cb(left_path, right_path,
6823 &left_key,
6824 BTRFS_COMPARE_TREE_NEW,
6825 ctx);
6826 if (ret < 0)
6827 goto out;
6828 advance_left = ADVANCE;
6829 } else if (cmp > 0) {
6830 ret = changed_cb(left_path, right_path,
6831 &right_key,
6832 BTRFS_COMPARE_TREE_DELETED,
6833 ctx);
6834 if (ret < 0)
6835 goto out;
6836 advance_right = ADVANCE;
6837 } else {
6838 enum btrfs_compare_tree_result result;
6839
6840 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
6841 ret = tree_compare_item(left_path, right_path,
6842 tmp_buf);
6843 if (ret)
6844 result = BTRFS_COMPARE_TREE_CHANGED;
6845 else
6846 result = BTRFS_COMPARE_TREE_SAME;
6847 ret = changed_cb(left_path, right_path,
6848 &left_key, result, ctx);
6849 if (ret < 0)
6850 goto out;
6851 advance_left = ADVANCE;
6852 advance_right = ADVANCE;
6853 }
6854 } else if (left_level == right_level) {
6855 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
6856 if (cmp < 0) {
6857 advance_left = ADVANCE;
6858 } else if (cmp > 0) {
6859 advance_right = ADVANCE;
6860 } else {
6861 left_blockptr = btrfs_node_blockptr(
6862 left_path->nodes[left_level],
6863 left_path->slots[left_level]);
6864 right_blockptr = btrfs_node_blockptr(
6865 right_path->nodes[right_level],
6866 right_path->slots[right_level]);
6867 left_gen = btrfs_node_ptr_generation(
6868 left_path->nodes[left_level],
6869 left_path->slots[left_level]);
6870 right_gen = btrfs_node_ptr_generation(
6871 right_path->nodes[right_level],
6872 right_path->slots[right_level]);
6873 if (left_blockptr == right_blockptr &&
6874 left_gen == right_gen) {
6875 /*
6876 * As we're on a shared block, don't
6877 * allow to go deeper.
6878 */
6879 advance_left = ADVANCE_ONLY_NEXT;
6880 advance_right = ADVANCE_ONLY_NEXT;
6881 } else {
6882 advance_left = ADVANCE;
6883 advance_right = ADVANCE;
6884 }
6885 }
6886 } else if (left_level < right_level) {
6887 advance_right = ADVANCE;
6888 } else {
6889 advance_left = ADVANCE;
6890 }
6891 }
6892
6893out:
6894 btrfs_free_path(left_path);
6895 btrfs_free_path(right_path);
6896 kvfree(tmp_buf);
6897 return ret;
6898}
6899
Alexander Block31db9f72012-07-25 23:19:24 +02006900static int send_subvol(struct send_ctx *sctx)
6901{
6902 int ret;
6903
Stefan Behrensc2c71322013-04-10 17:10:52 +00006904 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6905 ret = send_header(sctx);
6906 if (ret < 0)
6907 goto out;
6908 }
Alexander Block31db9f72012-07-25 23:19:24 +02006909
6910 ret = send_subvol_begin(sctx);
6911 if (ret < 0)
6912 goto out;
6913
6914 if (sctx->parent_root) {
6915 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6916 changed_cb, sctx);
6917 if (ret < 0)
6918 goto out;
6919 ret = finish_inode_if_needed(sctx, 1);
6920 if (ret < 0)
6921 goto out;
6922 } else {
6923 ret = full_send_tree(sctx);
6924 if (ret < 0)
6925 goto out;
6926 }
6927
6928out:
Alexander Block31db9f72012-07-25 23:19:24 +02006929 free_recorded_refs(sctx);
6930 return ret;
6931}
6932
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006933/*
6934 * If orphan cleanup did remove any orphans from a root, it means the tree
6935 * was modified and therefore the commit root is not the same as the current
6936 * root anymore. This is a problem, because send uses the commit root and
6937 * therefore can see inode items that don't exist in the current root anymore,
6938 * and for example make calls to btrfs_iget, which will do tree lookups based
6939 * on the current root and not on the commit root. Those lookups will fail,
6940 * returning a -ESTALE error, and making send fail with that error. So make
6941 * sure a send does not see any orphans we have just removed, and that it will
6942 * see the same inodes regardless of whether a transaction commit happened
6943 * before it started (meaning that the commit root will be the same as the
6944 * current root) or not.
6945 */
6946static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6947{
6948 int i;
6949 struct btrfs_trans_handle *trans = NULL;
6950
6951again:
6952 if (sctx->parent_root &&
6953 sctx->parent_root->node != sctx->parent_root->commit_root)
6954 goto commit_trans;
6955
6956 for (i = 0; i < sctx->clone_roots_cnt; i++)
6957 if (sctx->clone_roots[i].root->node !=
6958 sctx->clone_roots[i].root->commit_root)
6959 goto commit_trans;
6960
6961 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006962 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006963
6964 return 0;
6965
6966commit_trans:
6967 /* Use any root, all fs roots will get their commit roots updated. */
6968 if (!trans) {
6969 trans = btrfs_join_transaction(sctx->send_root);
6970 if (IS_ERR(trans))
6971 return PTR_ERR(trans);
6972 goto again;
6973 }
6974
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006975 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006976}
6977
Filipe Manana9f89d5d2019-04-15 09:29:36 +01006978/*
6979 * Make sure any existing dellaloc is flushed for any root used by a send
6980 * operation so that we do not miss any data and we do not race with writeback
6981 * finishing and changing a tree while send is using the tree. This could
6982 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
6983 * a send operation then uses the subvolume.
6984 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
6985 */
6986static int flush_delalloc_roots(struct send_ctx *sctx)
6987{
6988 struct btrfs_root *root = sctx->parent_root;
6989 int ret;
6990 int i;
6991
6992 if (root) {
6993 ret = btrfs_start_delalloc_snapshot(root);
6994 if (ret)
6995 return ret;
6996 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
6997 }
6998
6999 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7000 root = sctx->clone_roots[i].root;
7001 ret = btrfs_start_delalloc_snapshot(root);
7002 if (ret)
7003 return ret;
7004 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7005 }
7006
7007 return 0;
7008}
7009
David Sterba66ef7d62013-12-17 15:07:20 +01007010static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
7011{
7012 spin_lock(&root->root_item_lock);
7013 root->send_in_progress--;
7014 /*
7015 * Not much left to do, we don't know why it's unbalanced and
7016 * can't blindly reset it to 0.
7017 */
7018 if (root->send_in_progress < 0)
7019 btrfs_err(root->fs_info,
Colin Ian Kingf5686e32018-05-04 12:11:12 +01007020 "send_in_progress unbalanced %d root %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007021 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01007022 spin_unlock(&root->root_item_lock);
7023}
7024
Filipe Manana62d54f32019-04-22 16:43:42 +01007025static void dedupe_in_progress_warn(const struct btrfs_root *root)
7026{
7027 btrfs_warn_rl(root->fs_info,
7028"cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
7029 root->root_key.objectid, root->dedupe_in_progress);
7030}
7031
Josef Bacik2351f432017-09-27 10:43:13 -04007032long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
Alexander Block31db9f72012-07-25 23:19:24 +02007033{
7034 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007035 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
7036 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02007037 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02007038 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02007039 struct send_ctx *sctx = NULL;
7040 u32 i;
7041 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01007042 int clone_sources_to_rollback = 0;
David Sterbae55d1152016-04-11 18:52:02 +02007043 unsigned alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08007044 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08007045 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02007046
7047 if (!capable(CAP_SYS_ADMIN))
7048 return -EPERM;
7049
Josef Bacik139f8072013-05-20 11:26:50 -04007050 /*
David Sterba2c686532013-12-16 17:34:17 +01007051 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02007052 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01007053 */
7054 spin_lock(&send_root->root_item_lock);
Filipe Manana62d54f32019-04-22 16:43:42 +01007055 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
7056 dedupe_in_progress_warn(send_root);
7057 spin_unlock(&send_root->root_item_lock);
7058 return -EAGAIN;
7059 }
David Sterba2c686532013-12-16 17:34:17 +01007060 send_root->send_in_progress++;
7061 spin_unlock(&send_root->root_item_lock);
7062
7063 /*
Josef Bacik139f8072013-05-20 11:26:50 -04007064 * This is done when we lookup the root, it should already be complete
7065 * by the time we get here.
7066 */
7067 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
7068
7069 /*
David Sterba2c686532013-12-16 17:34:17 +01007070 * Userspace tools do the checks and warn the user if it's
7071 * not RO.
7072 */
7073 if (!btrfs_root_readonly(send_root)) {
7074 ret = -EPERM;
7075 goto out;
7076 }
7077
Dan Carpenter457ae722017-03-17 23:51:20 +03007078 /*
7079 * Check that we don't overflow at later allocations, we request
7080 * clone_sources_count + 1 items, and compare to unsigned long inside
7081 * access_ok.
7082 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03007083 if (arg->clone_sources_count >
Dan Carpenter457ae722017-03-17 23:51:20 +03007084 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03007085 ret = -EINVAL;
7086 goto out;
7087 }
7088
Linus Torvalds96d4f262019-01-03 18:57:57 -08007089 if (!access_ok(arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05007090 sizeof(*arg->clone_sources) *
7091 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02007092 ret = -EFAULT;
7093 goto out;
7094 }
7095
Stefan Behrensc2c71322013-04-10 17:10:52 +00007096 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00007097 ret = -EINVAL;
7098 goto out;
7099 }
7100
David Sterbae780b0d2016-01-18 18:42:13 +01007101 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007102 if (!sctx) {
7103 ret = -ENOMEM;
7104 goto out;
7105 }
7106
7107 INIT_LIST_HEAD(&sctx->new_refs);
7108 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01007109 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007110 INIT_LIST_HEAD(&sctx->name_cache_list);
7111
Mark Fashehcb95e7b2013-02-04 20:54:57 +00007112 sctx->flags = arg->flags;
7113
Alexander Block31db9f72012-07-25 23:19:24 +02007114 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00007115 if (!sctx->send_filp) {
7116 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02007117 goto out;
7118 }
7119
Alexander Block31db9f72012-07-25 23:19:24 +02007120 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02007121 /*
7122 * Unlikely but possible, if the subvolume is marked for deletion but
7123 * is slow to remove the directory entry, send can still be started
7124 */
7125 if (btrfs_root_dead(sctx->send_root)) {
7126 ret = -EPERM;
7127 goto out;
7128 }
7129
Alexander Block31db9f72012-07-25 23:19:24 +02007130 sctx->clone_roots_cnt = arg->clone_sources_count;
7131
7132 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
Michal Hocko752ade62017-05-08 15:57:27 -07007133 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007134 if (!sctx->send_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07007135 ret = -ENOMEM;
7136 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007137 }
7138
Michal Hocko752ade62017-05-08 15:57:27 -07007139 sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007140 if (!sctx->read_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07007141 ret = -ENOMEM;
7142 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007143 }
7144
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007145 sctx->pending_dir_moves = RB_ROOT;
7146 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00007147 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007148
David Sterbae55d1152016-04-11 18:52:02 +02007149 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
7150
David Sterba818e0102017-05-31 18:40:02 +02007151 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007152 if (!sctx->clone_roots) {
David Sterba818e0102017-05-31 18:40:02 +02007153 ret = -ENOMEM;
7154 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007155 }
7156
David Sterbae55d1152016-04-11 18:52:02 +02007157 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
7158
Alexander Block31db9f72012-07-25 23:19:24 +02007159 if (arg->clone_sources_count) {
Michal Hocko752ade62017-05-08 15:57:27 -07007160 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02007161 if (!clone_sources_tmp) {
Michal Hocko752ade62017-05-08 15:57:27 -07007162 ret = -ENOMEM;
7163 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02007164 }
7165
7166 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02007167 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02007168 if (ret) {
7169 ret = -EFAULT;
7170 goto out;
7171 }
7172
7173 for (i = 0; i < arg->clone_sources_count; i++) {
7174 key.objectid = clone_sources_tmp[i];
7175 key.type = BTRFS_ROOT_ITEM_KEY;
7176 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08007177
7178 index = srcu_read_lock(&fs_info->subvol_srcu);
7179
Alexander Block31db9f72012-07-25 23:19:24 +02007180 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02007181 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08007182 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02007183 ret = PTR_ERR(clone_root);
7184 goto out;
7185 }
David Sterba2c686532013-12-16 17:34:17 +01007186 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00007187 if (!btrfs_root_readonly(clone_root) ||
7188 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01007189 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08007190 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01007191 ret = -EPERM;
7192 goto out;
7193 }
Filipe Manana62d54f32019-04-22 16:43:42 +01007194 if (clone_root->dedupe_in_progress) {
7195 dedupe_in_progress_warn(clone_root);
7196 spin_unlock(&clone_root->root_item_lock);
7197 srcu_read_unlock(&fs_info->subvol_srcu, index);
7198 ret = -EAGAIN;
7199 goto out;
7200 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00007201 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01007202 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08007203 srcu_read_unlock(&fs_info->subvol_srcu, index);
7204
Alexander Block31db9f72012-07-25 23:19:24 +02007205 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00007206 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02007207 }
David Sterba2f913062016-04-11 18:40:08 +02007208 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02007209 clone_sources_tmp = NULL;
7210 }
7211
7212 if (arg->parent_root) {
7213 key.objectid = arg->parent_root;
7214 key.type = BTRFS_ROOT_ITEM_KEY;
7215 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08007216
7217 index = srcu_read_lock(&fs_info->subvol_srcu);
7218
Alexander Block31db9f72012-07-25 23:19:24 +02007219 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00007220 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08007221 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00007222 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02007223 goto out;
7224 }
Wang Shilong18f687d2014-01-07 17:25:19 +08007225
David Sterba2c686532013-12-16 17:34:17 +01007226 spin_lock(&sctx->parent_root->root_item_lock);
7227 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02007228 if (!btrfs_root_readonly(sctx->parent_root) ||
7229 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01007230 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08007231 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01007232 ret = -EPERM;
7233 goto out;
7234 }
Filipe Manana62d54f32019-04-22 16:43:42 +01007235 if (sctx->parent_root->dedupe_in_progress) {
7236 dedupe_in_progress_warn(sctx->parent_root);
7237 spin_unlock(&sctx->parent_root->root_item_lock);
7238 srcu_read_unlock(&fs_info->subvol_srcu, index);
7239 ret = -EAGAIN;
7240 goto out;
7241 }
David Sterba2c686532013-12-16 17:34:17 +01007242 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08007243
7244 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02007245 }
7246
7247 /*
7248 * Clones from send_root are allowed, but only if the clone source
7249 * is behind the current send position. This is checked while searching
7250 * for possible clone sources.
7251 */
7252 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
7253
7254 /* We do a bsearch later */
7255 sort(sctx->clone_roots, sctx->clone_roots_cnt,
7256 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
7257 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08007258 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02007259
Filipe Manana9f89d5d2019-04-15 09:29:36 +01007260 ret = flush_delalloc_roots(sctx);
7261 if (ret)
7262 goto out;
7263
Filipe Mananae5fa8f82014-10-21 11:11:41 +01007264 ret = ensure_commit_roots_uptodate(sctx);
7265 if (ret)
7266 goto out;
7267
Filipe Manana9e967492019-04-22 16:44:09 +01007268 mutex_lock(&fs_info->balance_mutex);
7269 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
7270 mutex_unlock(&fs_info->balance_mutex);
7271 btrfs_warn_rl(fs_info,
7272 "cannot run send because a balance operation is in progress");
7273 ret = -EAGAIN;
7274 goto out;
7275 }
7276 fs_info->send_in_progress++;
7277 mutex_unlock(&fs_info->balance_mutex);
7278
David Sterba2755a0d2014-07-31 00:43:18 +02007279 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02007280 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04007281 current->journal_info = NULL;
Filipe Manana9e967492019-04-22 16:44:09 +01007282 mutex_lock(&fs_info->balance_mutex);
7283 fs_info->send_in_progress--;
7284 mutex_unlock(&fs_info->balance_mutex);
Alexander Block31db9f72012-07-25 23:19:24 +02007285 if (ret < 0)
7286 goto out;
7287
Stefan Behrensc2c71322013-04-10 17:10:52 +00007288 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
7289 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
7290 if (ret < 0)
7291 goto out;
7292 ret = send_cmd(sctx);
7293 if (ret < 0)
7294 goto out;
7295 }
Alexander Block31db9f72012-07-25 23:19:24 +02007296
7297out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00007298 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7299 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7300 struct rb_node *n;
7301 struct pending_dir_move *pm;
7302
7303 n = rb_first(&sctx->pending_dir_moves);
7304 pm = rb_entry(n, struct pending_dir_move, node);
7305 while (!list_empty(&pm->list)) {
7306 struct pending_dir_move *pm2;
7307
7308 pm2 = list_first_entry(&pm->list,
7309 struct pending_dir_move, list);
7310 free_pending_move(sctx, pm2);
7311 }
7312 free_pending_move(sctx, pm);
7313 }
7314
7315 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7316 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7317 struct rb_node *n;
7318 struct waiting_dir_move *dm;
7319
7320 n = rb_first(&sctx->waiting_dir_moves);
7321 dm = rb_entry(n, struct waiting_dir_move, node);
7322 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7323 kfree(dm);
7324 }
7325
Filipe Manana9dc44212014-02-19 14:31:44 +00007326 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
7327 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
7328 struct rb_node *n;
7329 struct orphan_dir_info *odi;
7330
7331 n = rb_first(&sctx->orphan_dirs);
7332 odi = rb_entry(n, struct orphan_dir_info, node);
7333 free_orphan_dir_info(sctx, odi);
7334 }
7335
Wang Shilong896c14f2014-01-07 17:25:18 +08007336 if (sort_clone_roots) {
7337 for (i = 0; i < sctx->clone_roots_cnt; i++)
7338 btrfs_root_dec_send_in_progress(
7339 sctx->clone_roots[i].root);
7340 } else {
7341 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
7342 btrfs_root_dec_send_in_progress(
7343 sctx->clone_roots[i].root);
7344
7345 btrfs_root_dec_send_in_progress(send_root);
7346 }
David Sterba66ef7d62013-12-17 15:07:20 +01007347 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
7348 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01007349
David Sterba2f913062016-04-11 18:40:08 +02007350 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02007351
7352 if (sctx) {
7353 if (sctx->send_filp)
7354 fput(sctx->send_filp);
7355
David Sterbac03d01f2016-04-11 18:40:08 +02007356 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02007357 kvfree(sctx->send_buf);
David Sterbaeb5b75f2016-04-11 18:40:08 +02007358 kvfree(sctx->read_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02007359
7360 name_cache_free(sctx);
7361
7362 kfree(sctx);
7363 }
7364
7365 return ret;
7366}