blob: c3c0c064c25dadd317841e1d2761e2629b6ec9f3 [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 Sterbae67c7182018-02-19 17:24:18 +0100263__cold
Filipe Manana95155582016-08-01 01:50:37 +0100264static void inconsistent_snapshot_error(struct send_ctx *sctx,
265 enum btrfs_compare_tree_result result,
266 const char *what)
267{
268 const char *result_string;
269
270 switch (result) {
271 case BTRFS_COMPARE_TREE_NEW:
272 result_string = "new";
273 break;
274 case BTRFS_COMPARE_TREE_DELETED:
275 result_string = "deleted";
276 break;
277 case BTRFS_COMPARE_TREE_CHANGED:
278 result_string = "updated";
279 break;
280 case BTRFS_COMPARE_TREE_SAME:
281 ASSERT(0);
282 result_string = "unchanged";
283 break;
284 default:
285 ASSERT(0);
286 result_string = "unexpected";
287 }
288
289 btrfs_err(sctx->send_root->fs_info,
290 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
291 result_string, what, sctx->cmp_key->objectid,
292 sctx->send_root->root_key.objectid,
293 (sctx->parent_root ?
294 sctx->parent_root->root_key.objectid : 0));
295}
296
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000297static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
298
Filipe Manana9dc44212014-02-19 14:31:44 +0000299static struct waiting_dir_move *
300get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
301
302static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
303
Josef Bacik16e75492013-10-22 12:18:51 -0400304static int need_send_hole(struct send_ctx *sctx)
305{
306 return (sctx->parent_root && !sctx->cur_inode_new &&
307 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
308 S_ISREG(sctx->cur_inode_mode));
309}
310
Alexander Block31db9f72012-07-25 23:19:24 +0200311static void fs_path_reset(struct fs_path *p)
312{
313 if (p->reversed) {
314 p->start = p->buf + p->buf_len - 1;
315 p->end = p->start;
316 *p->start = 0;
317 } else {
318 p->start = p->buf;
319 p->end = p->start;
320 *p->start = 0;
321 }
322}
323
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000324static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200325{
326 struct fs_path *p;
327
David Sterbae780b0d2016-01-18 18:42:13 +0100328 p = kmalloc(sizeof(*p), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +0200329 if (!p)
330 return NULL;
331 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200332 p->buf = p->inline_buf;
333 p->buf_len = FS_PATH_INLINE_SIZE;
334 fs_path_reset(p);
335 return p;
336}
337
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000338static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200339{
340 struct fs_path *p;
341
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000342 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200343 if (!p)
344 return NULL;
345 p->reversed = 1;
346 fs_path_reset(p);
347 return p;
348}
349
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000350static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200351{
352 if (!p)
353 return;
David Sterbaace01052014-02-05 16:17:34 +0100354 if (p->buf != p->inline_buf)
355 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200356 kfree(p);
357}
358
359static int fs_path_len(struct fs_path *p)
360{
361 return p->end - p->start;
362}
363
364static int fs_path_ensure_buf(struct fs_path *p, int len)
365{
366 char *tmp_buf;
367 int path_len;
368 int old_buf_len;
369
370 len++;
371
372 if (p->buf_len >= len)
373 return 0;
374
Chris Masoncfd4a532014-04-26 05:02:03 -0700375 if (len > PATH_MAX) {
376 WARN_ON(1);
377 return -ENOMEM;
378 }
379
David Sterba1b2782c2014-02-25 19:32:59 +0100380 path_len = p->end - p->start;
381 old_buf_len = p->buf_len;
382
David Sterbaace01052014-02-05 16:17:34 +0100383 /*
384 * First time the inline_buf does not suffice
385 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100386 if (p->buf == p->inline_buf) {
David Sterbae780b0d2016-01-18 18:42:13 +0100387 tmp_buf = kmalloc(len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100388 if (tmp_buf)
389 memcpy(tmp_buf, p->buf, old_buf_len);
390 } else {
David Sterbae780b0d2016-01-18 18:42:13 +0100391 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100392 }
David Sterba9c9ca002014-02-25 19:33:08 +0100393 if (!tmp_buf)
394 return -ENOMEM;
395 p->buf = tmp_buf;
396 /*
397 * The real size of the buffer is bigger, this will let the fast path
398 * happen most of the time
399 */
400 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100401
Alexander Block31db9f72012-07-25 23:19:24 +0200402 if (p->reversed) {
403 tmp_buf = p->buf + old_buf_len - path_len - 1;
404 p->end = p->buf + p->buf_len - 1;
405 p->start = p->end - path_len;
406 memmove(p->start, tmp_buf, path_len + 1);
407 } else {
408 p->start = p->buf;
409 p->end = p->start + path_len;
410 }
411 return 0;
412}
413
David Sterbab23ab572014-02-03 19:23:19 +0100414static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
415 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200416{
417 int ret;
418 int new_len;
419
420 new_len = p->end - p->start + name_len;
421 if (p->start != p->end)
422 new_len++;
423 ret = fs_path_ensure_buf(p, new_len);
424 if (ret < 0)
425 goto out;
426
427 if (p->reversed) {
428 if (p->start != p->end)
429 *--p->start = '/';
430 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100431 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200432 } else {
433 if (p->start != p->end)
434 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100435 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200436 p->end += name_len;
437 *p->end = 0;
438 }
439
440out:
441 return ret;
442}
443
444static int fs_path_add(struct fs_path *p, const char *name, int name_len)
445{
446 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100447 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200448
David Sterbab23ab572014-02-03 19:23:19 +0100449 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200450 if (ret < 0)
451 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100452 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200453
454out:
455 return ret;
456}
457
458static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
459{
460 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100461 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200462
David Sterbab23ab572014-02-03 19:23:19 +0100463 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200464 if (ret < 0)
465 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100466 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200467
468out:
469 return ret;
470}
471
472static int fs_path_add_from_extent_buffer(struct fs_path *p,
473 struct extent_buffer *eb,
474 unsigned long off, int len)
475{
476 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100477 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200478
David Sterbab23ab572014-02-03 19:23:19 +0100479 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200480 if (ret < 0)
481 goto out;
482
David Sterbab23ab572014-02-03 19:23:19 +0100483 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200484
485out:
486 return ret;
487}
488
Alexander Block31db9f72012-07-25 23:19:24 +0200489static int fs_path_copy(struct fs_path *p, struct fs_path *from)
490{
491 int ret;
492
493 p->reversed = from->reversed;
494 fs_path_reset(p);
495
496 ret = fs_path_add_path(p, from);
497
498 return ret;
499}
500
501
502static void fs_path_unreverse(struct fs_path *p)
503{
504 char *tmp;
505 int len;
506
507 if (!p->reversed)
508 return;
509
510 tmp = p->start;
511 len = p->end - p->start;
512 p->start = p->buf;
513 p->end = p->start + len;
514 memmove(p->start, tmp, len + 1);
515 p->reversed = 0;
516}
517
518static struct btrfs_path *alloc_path_for_send(void)
519{
520 struct btrfs_path *path;
521
522 path = btrfs_alloc_path();
523 if (!path)
524 return NULL;
525 path->search_commit_root = 1;
526 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400527 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200528 return path;
529}
530
Eric Sandeen48a3b632013-04-25 20:41:01 +0000531static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200532{
533 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200534 u32 pos = 0;
535
Alexander Block31db9f72012-07-25 23:19:24 +0200536 while (pos < len) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200537 ret = kernel_write(filp, buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200538 /* TODO handle that correctly */
539 /*if (ret == -ERESTARTSYS) {
540 continue;
541 }*/
542 if (ret < 0)
Christoph Hellwig8e931572017-09-01 17:39:19 +0200543 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200544 if (ret == 0) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200545 return -EIO;
Alexander Block31db9f72012-07-25 23:19:24 +0200546 }
547 pos += ret;
548 }
549
Christoph Hellwig8e931572017-09-01 17:39:19 +0200550 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200551}
552
553static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
554{
555 struct btrfs_tlv_header *hdr;
556 int total_len = sizeof(*hdr) + len;
557 int left = sctx->send_max_size - sctx->send_size;
558
559 if (unlikely(left < total_len))
560 return -EOVERFLOW;
561
562 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
563 hdr->tlv_type = cpu_to_le16(attr);
564 hdr->tlv_len = cpu_to_le16(len);
565 memcpy(hdr + 1, data, len);
566 sctx->send_size += total_len;
567
568 return 0;
569}
570
David Sterba95bc79d2013-12-16 17:34:10 +0100571#define TLV_PUT_DEFINE_INT(bits) \
572 static int tlv_put_u##bits(struct send_ctx *sctx, \
573 u##bits attr, u##bits value) \
574 { \
575 __le##bits __tmp = cpu_to_le##bits(value); \
576 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
577 }
Alexander Block31db9f72012-07-25 23:19:24 +0200578
David Sterba95bc79d2013-12-16 17:34:10 +0100579TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200580
581static int tlv_put_string(struct send_ctx *sctx, u16 attr,
582 const char *str, int len)
583{
584 if (len == -1)
585 len = strlen(str);
586 return tlv_put(sctx, attr, str, len);
587}
588
589static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
590 const u8 *uuid)
591{
592 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
593}
594
Alexander Block31db9f72012-07-25 23:19:24 +0200595static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
596 struct extent_buffer *eb,
597 struct btrfs_timespec *ts)
598{
599 struct btrfs_timespec bts;
600 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
601 return tlv_put(sctx, attr, &bts, sizeof(bts));
602}
603
604
Liu Bo895a72b2018-03-02 18:05:49 -0700605#define TLV_PUT(sctx, attrtype, data, attrlen) \
Alexander Block31db9f72012-07-25 23:19:24 +0200606 do { \
Liu Bo895a72b2018-03-02 18:05:49 -0700607 ret = tlv_put(sctx, attrtype, data, attrlen); \
Alexander Block31db9f72012-07-25 23:19:24 +0200608 if (ret < 0) \
609 goto tlv_put_failure; \
610 } while (0)
611
612#define TLV_PUT_INT(sctx, attrtype, bits, value) \
613 do { \
614 ret = tlv_put_u##bits(sctx, attrtype, value); \
615 if (ret < 0) \
616 goto tlv_put_failure; \
617 } while (0)
618
619#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
620#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
621#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
622#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
623#define TLV_PUT_STRING(sctx, attrtype, str, len) \
624 do { \
625 ret = tlv_put_string(sctx, attrtype, str, len); \
626 if (ret < 0) \
627 goto tlv_put_failure; \
628 } while (0)
629#define TLV_PUT_PATH(sctx, attrtype, p) \
630 do { \
631 ret = tlv_put_string(sctx, attrtype, p->start, \
632 p->end - p->start); \
633 if (ret < 0) \
634 goto tlv_put_failure; \
635 } while(0)
636#define TLV_PUT_UUID(sctx, attrtype, uuid) \
637 do { \
638 ret = tlv_put_uuid(sctx, attrtype, uuid); \
639 if (ret < 0) \
640 goto tlv_put_failure; \
641 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200642#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
643 do { \
644 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
645 if (ret < 0) \
646 goto tlv_put_failure; \
647 } while (0)
648
649static int send_header(struct send_ctx *sctx)
650{
651 struct btrfs_stream_header hdr;
652
653 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
654 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
655
Anand Jain1bcea352012-09-14 00:04:21 -0600656 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
657 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200658}
659
660/*
661 * For each command/item we want to send to userspace, we call this function.
662 */
663static int begin_cmd(struct send_ctx *sctx, int cmd)
664{
665 struct btrfs_cmd_header *hdr;
666
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530667 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200668 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200669
670 BUG_ON(sctx->send_size);
671
672 sctx->send_size += sizeof(*hdr);
673 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
674 hdr->cmd = cpu_to_le16(cmd);
675
676 return 0;
677}
678
679static int send_cmd(struct send_ctx *sctx)
680{
681 int ret;
682 struct btrfs_cmd_header *hdr;
683 u32 crc;
684
685 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
686 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
687 hdr->crc = 0;
688
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200689 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200690 hdr->crc = cpu_to_le32(crc);
691
Anand Jain1bcea352012-09-14 00:04:21 -0600692 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
693 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200694
695 sctx->total_send_size += sctx->send_size;
696 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
697 sctx->send_size = 0;
698
699 return ret;
700}
701
702/*
703 * Sends a move instruction to user space
704 */
705static int send_rename(struct send_ctx *sctx,
706 struct fs_path *from, struct fs_path *to)
707{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400708 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200709 int ret;
710
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400711 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200712
713 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
714 if (ret < 0)
715 goto out;
716
717 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
718 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
719
720 ret = send_cmd(sctx);
721
722tlv_put_failure:
723out:
724 return ret;
725}
726
727/*
728 * Sends a link instruction to user space
729 */
730static int send_link(struct send_ctx *sctx,
731 struct fs_path *path, struct fs_path *lnk)
732{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400733 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200734 int ret;
735
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400736 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200737
738 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
739 if (ret < 0)
740 goto out;
741
742 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
743 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
744
745 ret = send_cmd(sctx);
746
747tlv_put_failure:
748out:
749 return ret;
750}
751
752/*
753 * Sends an unlink instruction to user space
754 */
755static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
756{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400757 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200758 int ret;
759
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400760 btrfs_debug(fs_info, "send_unlink %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200761
762 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
763 if (ret < 0)
764 goto out;
765
766 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
767
768 ret = send_cmd(sctx);
769
770tlv_put_failure:
771out:
772 return ret;
773}
774
775/*
776 * Sends a rmdir instruction to user space
777 */
778static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
779{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400780 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200781 int ret;
782
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400783 btrfs_debug(fs_info, "send_rmdir %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200784
785 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
786 if (ret < 0)
787 goto out;
788
789 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
790
791 ret = send_cmd(sctx);
792
793tlv_put_failure:
794out:
795 return ret;
796}
797
798/*
799 * Helper function to retrieve some fields from an inode item.
800 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400801static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
802 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
803 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200804{
805 int ret;
806 struct btrfs_inode_item *ii;
807 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200808
809 key.objectid = ino;
810 key.type = BTRFS_INODE_ITEM_KEY;
811 key.offset = 0;
812 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200813 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400814 if (ret > 0)
815 ret = -ENOENT;
816 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200817 }
818
819 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
820 struct btrfs_inode_item);
821 if (size)
822 *size = btrfs_inode_size(path->nodes[0], ii);
823 if (gen)
824 *gen = btrfs_inode_generation(path->nodes[0], ii);
825 if (mode)
826 *mode = btrfs_inode_mode(path->nodes[0], ii);
827 if (uid)
828 *uid = btrfs_inode_uid(path->nodes[0], ii);
829 if (gid)
830 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200831 if (rdev)
832 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200833
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400834 return ret;
835}
836
837static int get_inode_info(struct btrfs_root *root,
838 u64 ino, u64 *size, u64 *gen,
839 u64 *mode, u64 *uid, u64 *gid,
840 u64 *rdev)
841{
842 struct btrfs_path *path;
843 int ret;
844
845 path = alloc_path_for_send();
846 if (!path)
847 return -ENOMEM;
848 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
849 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200850 btrfs_free_path(path);
851 return ret;
852}
853
854typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
855 struct fs_path *p,
856 void *ctx);
857
858/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000859 * Helper function to iterate the entries in ONE btrfs_inode_ref or
860 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200861 * The iterate callback may return a non zero value to stop iteration. This can
862 * be a negative value for error codes or 1 to simply stop it.
863 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000864 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200865 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000866static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200867 struct btrfs_key *found_key, int resolve,
868 iterate_inode_ref_t iterate, void *ctx)
869{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000870 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200871 struct btrfs_item *item;
872 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000873 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200874 struct btrfs_path *tmp_path;
875 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000876 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200877 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000878 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200879 u32 name_len;
880 char *start;
881 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000882 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200883 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000884 u64 dir;
885 unsigned long name_off;
886 unsigned long elem_size;
887 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200888
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000889 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200890 if (!p)
891 return -ENOMEM;
892
893 tmp_path = alloc_path_for_send();
894 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000895 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200896 return -ENOMEM;
897 }
898
Alexander Block31db9f72012-07-25 23:19:24 +0200899
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000900 if (found_key->type == BTRFS_INODE_REF_KEY) {
901 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
902 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100903 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000904 total = btrfs_item_size(eb, item);
905 elem_size = sizeof(*iref);
906 } else {
907 ptr = btrfs_item_ptr_offset(eb, slot);
908 total = btrfs_item_size_nr(eb, slot);
909 elem_size = sizeof(*extref);
910 }
911
Alexander Block31db9f72012-07-25 23:19:24 +0200912 while (cur < total) {
913 fs_path_reset(p);
914
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000915 if (found_key->type == BTRFS_INODE_REF_KEY) {
916 iref = (struct btrfs_inode_ref *)(ptr + cur);
917 name_len = btrfs_inode_ref_name_len(eb, iref);
918 name_off = (unsigned long)(iref + 1);
919 index = btrfs_inode_ref_index(eb, iref);
920 dir = found_key->offset;
921 } else {
922 extref = (struct btrfs_inode_extref *)(ptr + cur);
923 name_len = btrfs_inode_extref_name_len(eb, extref);
924 name_off = (unsigned long)&extref->name;
925 index = btrfs_inode_extref_index(eb, extref);
926 dir = btrfs_inode_extref_parent(eb, extref);
927 }
928
Alexander Block31db9f72012-07-25 23:19:24 +0200929 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000930 start = btrfs_ref_to_path(root, tmp_path, name_len,
931 name_off, eb, dir,
932 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200933 if (IS_ERR(start)) {
934 ret = PTR_ERR(start);
935 goto out;
936 }
937 if (start < p->buf) {
938 /* overflow , try again with larger buffer */
939 ret = fs_path_ensure_buf(p,
940 p->buf_len + p->buf - start);
941 if (ret < 0)
942 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000943 start = btrfs_ref_to_path(root, tmp_path,
944 name_len, name_off,
945 eb, dir,
946 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200947 if (IS_ERR(start)) {
948 ret = PTR_ERR(start);
949 goto out;
950 }
951 BUG_ON(start < p->buf);
952 }
953 p->start = start;
954 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000955 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
956 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200957 if (ret < 0)
958 goto out;
959 }
960
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000961 cur += elem_size + name_len;
962 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200963 if (ret)
964 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200965 num++;
966 }
967
968out:
969 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000970 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200971 return ret;
972}
973
974typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
975 const char *name, int name_len,
976 const char *data, int data_len,
977 u8 type, void *ctx);
978
979/*
980 * Helper function to iterate the entries in ONE btrfs_dir_item.
981 * The iterate callback may return a non zero value to stop iteration. This can
982 * be a negative value for error codes or 1 to simply stop it.
983 *
984 * path must point to the dir item when called.
985 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000986static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200987 iterate_dir_item_t iterate, void *ctx)
988{
989 int ret = 0;
990 struct extent_buffer *eb;
991 struct btrfs_item *item;
992 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200993 struct btrfs_key di_key;
994 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +0100995 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +0200996 u32 name_len;
997 u32 data_len;
998 u32 cur;
999 u32 len;
1000 u32 total;
1001 int slot;
1002 int num;
1003 u8 type;
1004
Filipe Manana4395e0c2014-08-20 10:45:45 +01001005 /*
1006 * Start with a small buffer (1 page). If later we end up needing more
1007 * space, which can happen for xattrs on a fs with a leaf size greater
1008 * then the page size, attempt to increase the buffer. Typically xattr
1009 * values are small.
1010 */
1011 buf_len = PATH_MAX;
David Sterbae780b0d2016-01-18 18:42:13 +01001012 buf = kmalloc(buf_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02001013 if (!buf) {
1014 ret = -ENOMEM;
1015 goto out;
1016 }
1017
Alexander Block31db9f72012-07-25 23:19:24 +02001018 eb = path->nodes[0];
1019 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001020 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001021 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1022 cur = 0;
1023 len = 0;
1024 total = btrfs_item_size(eb, item);
1025
1026 num = 0;
1027 while (cur < total) {
1028 name_len = btrfs_dir_name_len(eb, di);
1029 data_len = btrfs_dir_data_len(eb, di);
1030 type = btrfs_dir_type(eb, di);
1031 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1032
Filipe Manana7e3ae332014-05-23 20:15:16 +01001033 if (type == BTRFS_FT_XATTR) {
1034 if (name_len > XATTR_NAME_MAX) {
1035 ret = -ENAMETOOLONG;
1036 goto out;
1037 }
Jeff Mahoneyda170662016-06-15 09:22:56 -04001038 if (name_len + data_len >
1039 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001040 ret = -E2BIG;
1041 goto out;
1042 }
1043 } else {
1044 /*
1045 * Path too long
1046 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001047 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001048 ret = -ENAMETOOLONG;
1049 goto out;
1050 }
Alexander Block31db9f72012-07-25 23:19:24 +02001051 }
1052
Filipe Manana4395e0c2014-08-20 10:45:45 +01001053 if (name_len + data_len > buf_len) {
1054 buf_len = name_len + data_len;
1055 if (is_vmalloc_addr(buf)) {
1056 vfree(buf);
1057 buf = NULL;
1058 } else {
1059 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001060 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001061
1062 if (!tmp)
1063 kfree(buf);
1064 buf = tmp;
1065 }
1066 if (!buf) {
David Sterbaf11f7442017-05-31 18:40:02 +02001067 buf = kvmalloc(buf_len, GFP_KERNEL);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001068 if (!buf) {
1069 ret = -ENOMEM;
1070 goto out;
1071 }
1072 }
1073 }
1074
Alexander Block31db9f72012-07-25 23:19:24 +02001075 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1076 name_len + data_len);
1077
1078 len = sizeof(*di) + name_len + data_len;
1079 di = (struct btrfs_dir_item *)((char *)di + len);
1080 cur += len;
1081
1082 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1083 data_len, type, ctx);
1084 if (ret < 0)
1085 goto out;
1086 if (ret) {
1087 ret = 0;
1088 goto out;
1089 }
1090
1091 num++;
1092 }
1093
1094out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001095 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001096 return ret;
1097}
1098
1099static int __copy_first_ref(int num, u64 dir, int index,
1100 struct fs_path *p, void *ctx)
1101{
1102 int ret;
1103 struct fs_path *pt = ctx;
1104
1105 ret = fs_path_copy(pt, p);
1106 if (ret < 0)
1107 return ret;
1108
1109 /* we want the first only */
1110 return 1;
1111}
1112
1113/*
1114 * Retrieve the first path of an inode. If an inode has more then one
1115 * ref/hardlink, this is ignored.
1116 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001117static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001118 u64 ino, struct fs_path *path)
1119{
1120 int ret;
1121 struct btrfs_key key, found_key;
1122 struct btrfs_path *p;
1123
1124 p = alloc_path_for_send();
1125 if (!p)
1126 return -ENOMEM;
1127
1128 fs_path_reset(path);
1129
1130 key.objectid = ino;
1131 key.type = BTRFS_INODE_REF_KEY;
1132 key.offset = 0;
1133
1134 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1135 if (ret < 0)
1136 goto out;
1137 if (ret) {
1138 ret = 1;
1139 goto out;
1140 }
1141 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1142 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001143 (found_key.type != BTRFS_INODE_REF_KEY &&
1144 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001145 ret = -ENOENT;
1146 goto out;
1147 }
1148
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001149 ret = iterate_inode_ref(root, p, &found_key, 1,
1150 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001151 if (ret < 0)
1152 goto out;
1153 ret = 0;
1154
1155out:
1156 btrfs_free_path(p);
1157 return ret;
1158}
1159
1160struct backref_ctx {
1161 struct send_ctx *sctx;
1162
1163 /* number of total found references */
1164 u64 found;
1165
1166 /*
1167 * used for clones found in send_root. clones found behind cur_objectid
1168 * and cur_offset are not considered as allowed clones.
1169 */
1170 u64 cur_objectid;
1171 u64 cur_offset;
1172
1173 /* may be truncated in case it's the last extent in a file */
1174 u64 extent_len;
1175
Filipe Manana619d8c42015-05-03 01:56:00 +01001176 /* data offset in the file extent item */
1177 u64 data_offset;
1178
Alexander Block31db9f72012-07-25 23:19:24 +02001179 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001180 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001181};
1182
1183static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1184{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001185 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001186 struct clone_root *cr = (struct clone_root *)elt;
1187
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001188 if (root < cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001189 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001190 if (root > cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001191 return 1;
1192 return 0;
1193}
1194
1195static int __clone_root_cmp_sort(const void *e1, const void *e2)
1196{
1197 struct clone_root *cr1 = (struct clone_root *)e1;
1198 struct clone_root *cr2 = (struct clone_root *)e2;
1199
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001200 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001201 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001202 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001203 return 1;
1204 return 0;
1205}
1206
1207/*
1208 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001209 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001210 */
1211static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1212{
1213 struct backref_ctx *bctx = ctx_;
1214 struct clone_root *found;
Alexander Block31db9f72012-07-25 23:19:24 +02001215
1216 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001217 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001218 bctx->sctx->clone_roots_cnt,
1219 sizeof(struct clone_root),
1220 __clone_root_cmp_bsearch);
1221 if (!found)
1222 return 0;
1223
1224 if (found->root == bctx->sctx->send_root &&
1225 ino == bctx->cur_objectid &&
1226 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001227 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001228 }
1229
1230 /*
Alexander Block31db9f72012-07-25 23:19:24 +02001231 * Make sure we don't consider clones from send_root that are
1232 * behind the current inode/offset.
1233 */
1234 if (found->root == bctx->sctx->send_root) {
1235 /*
1236 * TODO for the moment we don't accept clones from the inode
1237 * that is currently send. We may change this when
1238 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1239 * file.
1240 */
1241 if (ino >= bctx->cur_objectid)
1242 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001243 }
1244
1245 bctx->found++;
1246 found->found_refs++;
1247 if (ino < found->ino) {
1248 found->ino = ino;
1249 found->offset = offset;
1250 } else if (found->ino == ino) {
1251 /*
1252 * same extent found more then once in the same file.
1253 */
1254 if (found->offset > offset + bctx->extent_len)
1255 found->offset = offset;
1256 }
1257
1258 return 0;
1259}
1260
1261/*
Alexander Block766702e2012-07-28 14:11:31 +02001262 * Given an inode, offset and extent item, it finds a good clone for a clone
1263 * instruction. Returns -ENOENT when none could be found. The function makes
1264 * sure that the returned clone is usable at the point where sending is at the
1265 * moment. This means, that no clones are accepted which lie behind the current
1266 * inode+offset.
1267 *
Alexander Block31db9f72012-07-25 23:19:24 +02001268 * path must point to the extent item when called.
1269 */
1270static int find_extent_clone(struct send_ctx *sctx,
1271 struct btrfs_path *path,
1272 u64 ino, u64 data_offset,
1273 u64 ino_size,
1274 struct clone_root **found)
1275{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001276 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001277 int ret;
1278 int extent_type;
1279 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001280 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001281 u64 num_bytes;
1282 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001283 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001284 struct btrfs_file_extent_item *fi;
1285 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001286 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001287 struct clone_root *cur_clone_root;
1288 struct btrfs_key found_key;
1289 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001290 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001291 u32 i;
1292
1293 tmp_path = alloc_path_for_send();
1294 if (!tmp_path)
1295 return -ENOMEM;
1296
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001297 /* We only use this path under the commit sem */
1298 tmp_path->need_commit_sem = 0;
1299
David Sterbae780b0d2016-01-18 18:42:13 +01001300 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001301 if (!backref_ctx) {
1302 ret = -ENOMEM;
1303 goto out;
1304 }
1305
Alexander Block31db9f72012-07-25 23:19:24 +02001306 if (data_offset >= ino_size) {
1307 /*
1308 * There may be extents that lie behind the file's size.
1309 * I at least had this in combination with snapshotting while
1310 * writing large files.
1311 */
1312 ret = 0;
1313 goto out;
1314 }
1315
1316 fi = btrfs_item_ptr(eb, path->slots[0],
1317 struct btrfs_file_extent_item);
1318 extent_type = btrfs_file_extent_type(eb, fi);
1319 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1320 ret = -ENOENT;
1321 goto out;
1322 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001323 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001324
1325 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001326 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1327 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001328 ret = -ENOENT;
1329 goto out;
1330 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001331 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001332
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001333 down_read(&fs_info->commit_root_sem);
1334 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001335 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001336 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001337 btrfs_release_path(tmp_path);
1338
1339 if (ret < 0)
1340 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001341 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001342 ret = -EIO;
1343 goto out;
1344 }
1345
1346 /*
1347 * Setup the clone roots.
1348 */
1349 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1350 cur_clone_root = sctx->clone_roots + i;
1351 cur_clone_root->ino = (u64)-1;
1352 cur_clone_root->offset = 0;
1353 cur_clone_root->found_refs = 0;
1354 }
1355
Alexander Block35075bb2012-07-28 12:44:34 +02001356 backref_ctx->sctx = sctx;
1357 backref_ctx->found = 0;
1358 backref_ctx->cur_objectid = ino;
1359 backref_ctx->cur_offset = data_offset;
1360 backref_ctx->found_itself = 0;
1361 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001362 /*
1363 * For non-compressed extents iterate_extent_inodes() gives us extent
1364 * offsets that already take into account the data offset, but not for
1365 * compressed extents, since the offset is logical and not relative to
1366 * the physical extent locations. We must take this into account to
1367 * avoid sending clone offsets that go beyond the source file's size,
1368 * which would result in the clone ioctl failing with -EINVAL on the
1369 * receiving end.
1370 */
1371 if (compressed == BTRFS_COMPRESS_NONE)
1372 backref_ctx->data_offset = 0;
1373 else
1374 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001375
1376 /*
1377 * The last extent of a file may be too large due to page alignment.
1378 * We need to adjust extent_len in this case so that the checks in
1379 * __iterate_backrefs work.
1380 */
1381 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001382 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001383
1384 /*
1385 * Now collect all backrefs.
1386 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001387 if (compressed == BTRFS_COMPRESS_NONE)
1388 extent_item_pos = logical - found_key.objectid;
1389 else
1390 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001391 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1392 extent_item_pos, 1, __iterate_backrefs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001393 backref_ctx, false);
Chris Mason74dd17f2012-08-07 16:25:13 -04001394
Alexander Block31db9f72012-07-25 23:19:24 +02001395 if (ret < 0)
1396 goto out;
1397
Alexander Block35075bb2012-07-28 12:44:34 +02001398 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001399 /* found a bug in backref code? */
1400 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001401 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001402 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001403 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001404 goto out;
1405 }
1406
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001407 btrfs_debug(fs_info,
1408 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1409 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001410
Alexander Block35075bb2012-07-28 12:44:34 +02001411 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001412 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001413
1414 cur_clone_root = NULL;
1415 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1416 if (sctx->clone_roots[i].found_refs) {
1417 if (!cur_clone_root)
1418 cur_clone_root = sctx->clone_roots + i;
1419 else if (sctx->clone_roots[i].root == sctx->send_root)
1420 /* prefer clones from send_root over others */
1421 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001422 }
1423
1424 }
1425
1426 if (cur_clone_root) {
1427 *found = cur_clone_root;
1428 ret = 0;
1429 } else {
1430 ret = -ENOENT;
1431 }
1432
1433out:
1434 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001435 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001436 return ret;
1437}
1438
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001439static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001440 u64 ino,
1441 struct fs_path *dest)
1442{
1443 int ret;
1444 struct btrfs_path *path;
1445 struct btrfs_key key;
1446 struct btrfs_file_extent_item *ei;
1447 u8 type;
1448 u8 compression;
1449 unsigned long off;
1450 int len;
1451
1452 path = alloc_path_for_send();
1453 if (!path)
1454 return -ENOMEM;
1455
1456 key.objectid = ino;
1457 key.type = BTRFS_EXTENT_DATA_KEY;
1458 key.offset = 0;
1459 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1460 if (ret < 0)
1461 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001462 if (ret) {
1463 /*
1464 * An empty symlink inode. Can happen in rare error paths when
1465 * creating a symlink (transaction committed before the inode
1466 * eviction handler removed the symlink inode items and a crash
1467 * happened in between or the subvol was snapshoted in between).
1468 * Print an informative message to dmesg/syslog so that the user
1469 * can delete the symlink.
1470 */
1471 btrfs_err(root->fs_info,
1472 "Found empty symlink inode %llu at root %llu",
1473 ino, root->root_key.objectid);
1474 ret = -EIO;
1475 goto out;
1476 }
Alexander Block31db9f72012-07-25 23:19:24 +02001477
1478 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1479 struct btrfs_file_extent_item);
1480 type = btrfs_file_extent_type(path->nodes[0], ei);
1481 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1482 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1483 BUG_ON(compression);
1484
1485 off = btrfs_file_extent_inline_start(ei);
Qu Wenruoe41ca582018-06-06 15:41:49 +08001486 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001487
1488 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001489
1490out:
1491 btrfs_free_path(path);
1492 return ret;
1493}
1494
1495/*
1496 * Helper function to generate a file name that is unique in the root of
1497 * send_root and parent_root. This is used to generate names for orphan inodes.
1498 */
1499static int gen_unique_name(struct send_ctx *sctx,
1500 u64 ino, u64 gen,
1501 struct fs_path *dest)
1502{
1503 int ret = 0;
1504 struct btrfs_path *path;
1505 struct btrfs_dir_item *di;
1506 char tmp[64];
1507 int len;
1508 u64 idx = 0;
1509
1510 path = alloc_path_for_send();
1511 if (!path)
1512 return -ENOMEM;
1513
1514 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001515 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001516 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001517 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001518
1519 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1520 path, BTRFS_FIRST_FREE_OBJECTID,
1521 tmp, strlen(tmp), 0);
1522 btrfs_release_path(path);
1523 if (IS_ERR(di)) {
1524 ret = PTR_ERR(di);
1525 goto out;
1526 }
1527 if (di) {
1528 /* not unique, try again */
1529 idx++;
1530 continue;
1531 }
1532
1533 if (!sctx->parent_root) {
1534 /* unique */
1535 ret = 0;
1536 break;
1537 }
1538
1539 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1540 path, BTRFS_FIRST_FREE_OBJECTID,
1541 tmp, strlen(tmp), 0);
1542 btrfs_release_path(path);
1543 if (IS_ERR(di)) {
1544 ret = PTR_ERR(di);
1545 goto out;
1546 }
1547 if (di) {
1548 /* not unique, try again */
1549 idx++;
1550 continue;
1551 }
1552 /* unique */
1553 break;
1554 }
1555
1556 ret = fs_path_add(dest, tmp, strlen(tmp));
1557
1558out:
1559 btrfs_free_path(path);
1560 return ret;
1561}
1562
1563enum inode_state {
1564 inode_state_no_change,
1565 inode_state_will_create,
1566 inode_state_did_create,
1567 inode_state_will_delete,
1568 inode_state_did_delete,
1569};
1570
1571static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1572{
1573 int ret;
1574 int left_ret;
1575 int right_ret;
1576 u64 left_gen;
1577 u64 right_gen;
1578
1579 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001580 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001581 if (ret < 0 && ret != -ENOENT)
1582 goto out;
1583 left_ret = ret;
1584
1585 if (!sctx->parent_root) {
1586 right_ret = -ENOENT;
1587 } else {
1588 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001589 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001590 if (ret < 0 && ret != -ENOENT)
1591 goto out;
1592 right_ret = ret;
1593 }
1594
1595 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001596 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001597 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001598 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001599 if (ino < sctx->send_progress)
1600 ret = inode_state_did_create;
1601 else
1602 ret = inode_state_will_create;
1603 } else if (right_gen == gen) {
1604 if (ino < sctx->send_progress)
1605 ret = inode_state_did_delete;
1606 else
1607 ret = inode_state_will_delete;
1608 } else {
1609 ret = -ENOENT;
1610 }
1611 } else if (!left_ret) {
1612 if (left_gen == gen) {
1613 if (ino < sctx->send_progress)
1614 ret = inode_state_did_create;
1615 else
1616 ret = inode_state_will_create;
1617 } else {
1618 ret = -ENOENT;
1619 }
1620 } else if (!right_ret) {
1621 if (right_gen == gen) {
1622 if (ino < sctx->send_progress)
1623 ret = inode_state_did_delete;
1624 else
1625 ret = inode_state_will_delete;
1626 } else {
1627 ret = -ENOENT;
1628 }
1629 } else {
1630 ret = -ENOENT;
1631 }
1632
1633out:
1634 return ret;
1635}
1636
1637static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1638{
1639 int ret;
1640
Robbie Ko4dd99202017-01-05 16:24:55 +08001641 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1642 return 1;
1643
Alexander Block31db9f72012-07-25 23:19:24 +02001644 ret = get_cur_inode_state(sctx, ino, gen);
1645 if (ret < 0)
1646 goto out;
1647
1648 if (ret == inode_state_no_change ||
1649 ret == inode_state_did_create ||
1650 ret == inode_state_will_delete)
1651 ret = 1;
1652 else
1653 ret = 0;
1654
1655out:
1656 return ret;
1657}
1658
1659/*
1660 * Helper function to lookup a dir item in a dir.
1661 */
1662static int lookup_dir_item_inode(struct btrfs_root *root,
1663 u64 dir, const char *name, int name_len,
1664 u64 *found_inode,
1665 u8 *found_type)
1666{
1667 int ret = 0;
1668 struct btrfs_dir_item *di;
1669 struct btrfs_key key;
1670 struct btrfs_path *path;
1671
1672 path = alloc_path_for_send();
1673 if (!path)
1674 return -ENOMEM;
1675
1676 di = btrfs_lookup_dir_item(NULL, root, path,
1677 dir, name, name_len, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08001678 if (IS_ERR_OR_NULL(di)) {
1679 ret = di ? PTR_ERR(di) : -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02001680 goto out;
1681 }
1682 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001683 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1684 ret = -ENOENT;
1685 goto out;
1686 }
Alexander Block31db9f72012-07-25 23:19:24 +02001687 *found_inode = key.objectid;
1688 *found_type = btrfs_dir_type(path->nodes[0], di);
1689
1690out:
1691 btrfs_free_path(path);
1692 return ret;
1693}
1694
Alexander Block766702e2012-07-28 14:11:31 +02001695/*
1696 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1697 * generation of the parent dir and the name of the dir entry.
1698 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001699static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001700 u64 *dir, u64 *dir_gen, struct fs_path *name)
1701{
1702 int ret;
1703 struct btrfs_key key;
1704 struct btrfs_key found_key;
1705 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001706 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001707 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001708
1709 path = alloc_path_for_send();
1710 if (!path)
1711 return -ENOMEM;
1712
1713 key.objectid = ino;
1714 key.type = BTRFS_INODE_REF_KEY;
1715 key.offset = 0;
1716
1717 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1718 if (ret < 0)
1719 goto out;
1720 if (!ret)
1721 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1722 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001723 if (ret || found_key.objectid != ino ||
1724 (found_key.type != BTRFS_INODE_REF_KEY &&
1725 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001726 ret = -ENOENT;
1727 goto out;
1728 }
1729
Filipe Manana51a60252014-05-13 22:01:02 +01001730 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001731 struct btrfs_inode_ref *iref;
1732 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1733 struct btrfs_inode_ref);
1734 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1735 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1736 (unsigned long)(iref + 1),
1737 len);
1738 parent_dir = found_key.offset;
1739 } else {
1740 struct btrfs_inode_extref *extref;
1741 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1742 struct btrfs_inode_extref);
1743 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1744 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1745 (unsigned long)&extref->name, len);
1746 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1747 }
Alexander Block31db9f72012-07-25 23:19:24 +02001748 if (ret < 0)
1749 goto out;
1750 btrfs_release_path(path);
1751
Filipe Mananab46ab972014-03-21 12:46:54 +00001752 if (dir_gen) {
1753 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1754 NULL, NULL, NULL);
1755 if (ret < 0)
1756 goto out;
1757 }
Alexander Block31db9f72012-07-25 23:19:24 +02001758
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001759 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001760
1761out:
1762 btrfs_free_path(path);
1763 return ret;
1764}
1765
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001766static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001767 u64 ino, u64 dir,
1768 const char *name, int name_len)
1769{
1770 int ret;
1771 struct fs_path *tmp_name;
1772 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001773
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001774 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001775 if (!tmp_name)
1776 return -ENOMEM;
1777
Filipe Mananab46ab972014-03-21 12:46:54 +00001778 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001779 if (ret < 0)
1780 goto out;
1781
Alexander Blockb9291af2012-07-28 11:07:18 +02001782 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001783 ret = 0;
1784 goto out;
1785 }
1786
Alexander Blocke938c8a2012-07-28 16:33:49 +02001787 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001788
1789out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001790 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001791 return ret;
1792}
1793
Alexander Block766702e2012-07-28 14:11:31 +02001794/*
1795 * Used by process_recorded_refs to determine if a new ref would overwrite an
1796 * already existing ref. In case it detects an overwrite, it returns the
1797 * inode/gen in who_ino/who_gen.
1798 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1799 * to make sure later references to the overwritten inode are possible.
1800 * Orphanizing is however only required for the first ref of an inode.
1801 * process_recorded_refs does an additional is_first_ref check to see if
1802 * orphanizing is really required.
1803 */
Alexander Block31db9f72012-07-25 23:19:24 +02001804static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1805 const char *name, int name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01001806 u64 *who_ino, u64 *who_gen, u64 *who_mode)
Alexander Block31db9f72012-07-25 23:19:24 +02001807{
1808 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001809 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001810 u64 other_inode = 0;
1811 u8 other_type = 0;
1812
1813 if (!sctx->parent_root)
1814 goto out;
1815
1816 ret = is_inode_existent(sctx, dir, dir_gen);
1817 if (ret <= 0)
1818 goto out;
1819
Josef Bacikebdad912013-08-06 16:47:48 -04001820 /*
1821 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001822 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001823 * and we can just unlink this entry.
1824 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001825 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001826 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1827 NULL, NULL, NULL);
1828 if (ret < 0 && ret != -ENOENT)
1829 goto out;
1830 if (ret) {
1831 ret = 0;
1832 goto out;
1833 }
1834 if (gen != dir_gen)
1835 goto out;
1836 }
1837
Alexander Block31db9f72012-07-25 23:19:24 +02001838 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1839 &other_inode, &other_type);
1840 if (ret < 0 && ret != -ENOENT)
1841 goto out;
1842 if (ret) {
1843 ret = 0;
1844 goto out;
1845 }
1846
Alexander Block766702e2012-07-28 14:11:31 +02001847 /*
1848 * Check if the overwritten ref was already processed. If yes, the ref
1849 * was already unlinked/moved, so we can safely assume that we will not
1850 * overwrite anything at this point in time.
1851 */
Robbie Ko801bec32015-06-23 18:39:46 +08001852 if (other_inode > sctx->send_progress ||
1853 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001854 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Filipe Mananaf5962782017-06-22 20:03:51 +01001855 who_gen, who_mode, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001856 if (ret < 0)
1857 goto out;
1858
1859 ret = 1;
1860 *who_ino = other_inode;
1861 } else {
1862 ret = 0;
1863 }
1864
1865out:
1866 return ret;
1867}
1868
Alexander Block766702e2012-07-28 14:11:31 +02001869/*
1870 * Checks if the ref was overwritten by an already processed inode. This is
1871 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1872 * thus the orphan name needs be used.
1873 * process_recorded_refs also uses it to avoid unlinking of refs that were
1874 * overwritten.
1875 */
Alexander Block31db9f72012-07-25 23:19:24 +02001876static int did_overwrite_ref(struct send_ctx *sctx,
1877 u64 dir, u64 dir_gen,
1878 u64 ino, u64 ino_gen,
1879 const char *name, int name_len)
1880{
1881 int ret = 0;
1882 u64 gen;
1883 u64 ow_inode;
1884 u8 other_type;
1885
1886 if (!sctx->parent_root)
1887 goto out;
1888
1889 ret = is_inode_existent(sctx, dir, dir_gen);
1890 if (ret <= 0)
1891 goto out;
1892
Robbie Ko01914102017-01-05 16:24:58 +08001893 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1894 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1895 NULL, NULL, NULL);
1896 if (ret < 0 && ret != -ENOENT)
1897 goto out;
1898 if (ret) {
1899 ret = 0;
1900 goto out;
1901 }
1902 if (gen != dir_gen)
1903 goto out;
1904 }
1905
Alexander Block31db9f72012-07-25 23:19:24 +02001906 /* check if the ref was overwritten by another ref */
1907 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1908 &ow_inode, &other_type);
1909 if (ret < 0 && ret != -ENOENT)
1910 goto out;
1911 if (ret) {
1912 /* was never and will never be overwritten */
1913 ret = 0;
1914 goto out;
1915 }
1916
1917 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001918 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001919 if (ret < 0)
1920 goto out;
1921
1922 if (ow_inode == ino && gen == ino_gen) {
1923 ret = 0;
1924 goto out;
1925 }
1926
Filipe Manana8b191a62015-04-09 14:09:14 +01001927 /*
1928 * We know that it is or will be overwritten. Check this now.
1929 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001930 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1931 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001932 */
Filipe Mananab786f162015-09-26 15:30:23 +01001933 if ((ow_inode < sctx->send_progress) ||
1934 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1935 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001936 ret = 1;
1937 else
1938 ret = 0;
1939
1940out:
1941 return ret;
1942}
1943
Alexander Block766702e2012-07-28 14:11:31 +02001944/*
1945 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1946 * that got overwritten. This is used by process_recorded_refs to determine
1947 * if it has to use the path as returned by get_cur_path or the orphan name.
1948 */
Alexander Block31db9f72012-07-25 23:19:24 +02001949static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1950{
1951 int ret = 0;
1952 struct fs_path *name = NULL;
1953 u64 dir;
1954 u64 dir_gen;
1955
1956 if (!sctx->parent_root)
1957 goto out;
1958
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001959 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001960 if (!name)
1961 return -ENOMEM;
1962
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001963 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001964 if (ret < 0)
1965 goto out;
1966
1967 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1968 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001969
1970out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001971 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001972 return ret;
1973}
1974
Alexander Block766702e2012-07-28 14:11:31 +02001975/*
1976 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1977 * so we need to do some special handling in case we have clashes. This function
1978 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001979 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001980 */
Alexander Block31db9f72012-07-25 23:19:24 +02001981static int name_cache_insert(struct send_ctx *sctx,
1982 struct name_cache_entry *nce)
1983{
1984 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001985 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001986
Alexander Block7e0926f2012-07-28 14:20:58 +02001987 nce_head = radix_tree_lookup(&sctx->name_cache,
1988 (unsigned long)nce->ino);
1989 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01001990 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001991 if (!nce_head) {
1992 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001993 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001994 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001995 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001996
Alexander Block7e0926f2012-07-28 14:20:58 +02001997 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001998 if (ret < 0) {
1999 kfree(nce_head);
2000 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002001 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002002 }
Alexander Block31db9f72012-07-25 23:19:24 +02002003 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002004 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002005 list_add_tail(&nce->list, &sctx->name_cache_list);
2006 sctx->name_cache_size++;
2007
2008 return ret;
2009}
2010
2011static void name_cache_delete(struct send_ctx *sctx,
2012 struct name_cache_entry *nce)
2013{
Alexander Block7e0926f2012-07-28 14:20:58 +02002014 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002015
Alexander Block7e0926f2012-07-28 14:20:58 +02002016 nce_head = radix_tree_lookup(&sctx->name_cache,
2017 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002018 if (!nce_head) {
2019 btrfs_err(sctx->send_root->fs_info,
2020 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2021 nce->ino, sctx->name_cache_size);
2022 }
Alexander Block31db9f72012-07-25 23:19:24 +02002023
Alexander Block7e0926f2012-07-28 14:20:58 +02002024 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002025 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002026 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002027
David Sterba57fb8912014-02-03 19:24:40 +01002028 /*
2029 * We may not get to the final release of nce_head if the lookup fails
2030 */
2031 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002032 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2033 kfree(nce_head);
2034 }
Alexander Block31db9f72012-07-25 23:19:24 +02002035}
2036
2037static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2038 u64 ino, u64 gen)
2039{
Alexander Block7e0926f2012-07-28 14:20:58 +02002040 struct list_head *nce_head;
2041 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002042
Alexander Block7e0926f2012-07-28 14:20:58 +02002043 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2044 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002045 return NULL;
2046
Alexander Block7e0926f2012-07-28 14:20:58 +02002047 list_for_each_entry(cur, nce_head, radix_list) {
2048 if (cur->ino == ino && cur->gen == gen)
2049 return cur;
2050 }
Alexander Block31db9f72012-07-25 23:19:24 +02002051 return NULL;
2052}
2053
Alexander Block766702e2012-07-28 14:11:31 +02002054/*
2055 * Removes the entry from the list and adds it back to the end. This marks the
2056 * entry as recently used so that name_cache_clean_unused does not remove it.
2057 */
Alexander Block31db9f72012-07-25 23:19:24 +02002058static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2059{
2060 list_del(&nce->list);
2061 list_add_tail(&nce->list, &sctx->name_cache_list);
2062}
2063
Alexander Block766702e2012-07-28 14:11:31 +02002064/*
2065 * Remove some entries from the beginning of name_cache_list.
2066 */
Alexander Block31db9f72012-07-25 23:19:24 +02002067static void name_cache_clean_unused(struct send_ctx *sctx)
2068{
2069 struct name_cache_entry *nce;
2070
2071 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2072 return;
2073
2074 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2075 nce = list_entry(sctx->name_cache_list.next,
2076 struct name_cache_entry, list);
2077 name_cache_delete(sctx, nce);
2078 kfree(nce);
2079 }
2080}
2081
2082static void name_cache_free(struct send_ctx *sctx)
2083{
2084 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002085
Alexander Blocke938c8a2012-07-28 16:33:49 +02002086 while (!list_empty(&sctx->name_cache_list)) {
2087 nce = list_entry(sctx->name_cache_list.next,
2088 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002089 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002090 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002091 }
2092}
2093
Alexander Block766702e2012-07-28 14:11:31 +02002094/*
2095 * Used by get_cur_path for each ref up to the root.
2096 * Returns 0 if it succeeded.
2097 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2098 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2099 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2100 * Returns <0 in case of error.
2101 */
Alexander Block31db9f72012-07-25 23:19:24 +02002102static int __get_cur_name_and_parent(struct send_ctx *sctx,
2103 u64 ino, u64 gen,
2104 u64 *parent_ino,
2105 u64 *parent_gen,
2106 struct fs_path *dest)
2107{
2108 int ret;
2109 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002110 struct name_cache_entry *nce = NULL;
2111
Alexander Block766702e2012-07-28 14:11:31 +02002112 /*
2113 * First check if we already did a call to this function with the same
2114 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2115 * return the cached result.
2116 */
Alexander Block31db9f72012-07-25 23:19:24 +02002117 nce = name_cache_search(sctx, ino, gen);
2118 if (nce) {
2119 if (ino < sctx->send_progress && nce->need_later_update) {
2120 name_cache_delete(sctx, nce);
2121 kfree(nce);
2122 nce = NULL;
2123 } else {
2124 name_cache_used(sctx, nce);
2125 *parent_ino = nce->parent_ino;
2126 *parent_gen = nce->parent_gen;
2127 ret = fs_path_add(dest, nce->name, nce->name_len);
2128 if (ret < 0)
2129 goto out;
2130 ret = nce->ret;
2131 goto out;
2132 }
2133 }
2134
Alexander Block766702e2012-07-28 14:11:31 +02002135 /*
2136 * If the inode is not existent yet, add the orphan name and return 1.
2137 * This should only happen for the parent dir that we determine in
2138 * __record_new_ref
2139 */
Alexander Block31db9f72012-07-25 23:19:24 +02002140 ret = is_inode_existent(sctx, ino, gen);
2141 if (ret < 0)
2142 goto out;
2143
2144 if (!ret) {
2145 ret = gen_unique_name(sctx, ino, gen, dest);
2146 if (ret < 0)
2147 goto out;
2148 ret = 1;
2149 goto out_cache;
2150 }
2151
Alexander Block766702e2012-07-28 14:11:31 +02002152 /*
2153 * Depending on whether the inode was already processed or not, use
2154 * send_root or parent_root for ref lookup.
2155 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002156 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002157 ret = get_first_ref(sctx->send_root, ino,
2158 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002159 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002160 ret = get_first_ref(sctx->parent_root, ino,
2161 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002162 if (ret < 0)
2163 goto out;
2164
Alexander Block766702e2012-07-28 14:11:31 +02002165 /*
2166 * Check if the ref was overwritten by an inode's ref that was processed
2167 * earlier. If yes, treat as orphan and return 1.
2168 */
Alexander Block31db9f72012-07-25 23:19:24 +02002169 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2170 dest->start, dest->end - dest->start);
2171 if (ret < 0)
2172 goto out;
2173 if (ret) {
2174 fs_path_reset(dest);
2175 ret = gen_unique_name(sctx, ino, gen, dest);
2176 if (ret < 0)
2177 goto out;
2178 ret = 1;
2179 }
2180
2181out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002182 /*
2183 * Store the result of the lookup in the name cache.
2184 */
David Sterbae780b0d2016-01-18 18:42:13 +01002185 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002186 if (!nce) {
2187 ret = -ENOMEM;
2188 goto out;
2189 }
2190
2191 nce->ino = ino;
2192 nce->gen = gen;
2193 nce->parent_ino = *parent_ino;
2194 nce->parent_gen = *parent_gen;
2195 nce->name_len = fs_path_len(dest);
2196 nce->ret = ret;
2197 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002198
2199 if (ino < sctx->send_progress)
2200 nce->need_later_update = 0;
2201 else
2202 nce->need_later_update = 1;
2203
2204 nce_ret = name_cache_insert(sctx, nce);
2205 if (nce_ret < 0)
2206 ret = nce_ret;
2207 name_cache_clean_unused(sctx);
2208
2209out:
Alexander Block31db9f72012-07-25 23:19:24 +02002210 return ret;
2211}
2212
2213/*
2214 * Magic happens here. This function returns the first ref to an inode as it
2215 * would look like while receiving the stream at this point in time.
2216 * We walk the path up to the root. For every inode in between, we check if it
2217 * was already processed/sent. If yes, we continue with the parent as found
2218 * in send_root. If not, we continue with the parent as found in parent_root.
2219 * If we encounter an inode that was deleted at this point in time, we use the
2220 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2221 * that were not created yet and overwritten inodes/refs.
2222 *
Andrea Gelmini52042d82018-11-28 12:05:13 +01002223 * When do we have orphan inodes:
Alexander Block31db9f72012-07-25 23:19:24 +02002224 * 1. When an inode is freshly created and thus no valid refs are available yet
2225 * 2. When a directory lost all it's refs (deleted) but still has dir items
2226 * inside which were not processed yet (pending for move/delete). If anyone
2227 * tried to get the path to the dir items, it would get a path inside that
2228 * orphan directory.
2229 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2230 * of an unprocessed inode. If in that case the first ref would be
2231 * overwritten, the overwritten inode gets "orphanized". Later when we
2232 * process this overwritten inode, it is restored at a new place by moving
2233 * the orphan inode.
2234 *
2235 * sctx->send_progress tells this function at which point in time receiving
2236 * would be.
2237 */
2238static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2239 struct fs_path *dest)
2240{
2241 int ret = 0;
2242 struct fs_path *name = NULL;
2243 u64 parent_inode = 0;
2244 u64 parent_gen = 0;
2245 int stop = 0;
2246
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002247 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002248 if (!name) {
2249 ret = -ENOMEM;
2250 goto out;
2251 }
2252
2253 dest->reversed = 1;
2254 fs_path_reset(dest);
2255
2256 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002257 struct waiting_dir_move *wdm;
2258
Alexander Block31db9f72012-07-25 23:19:24 +02002259 fs_path_reset(name);
2260
Filipe Manana9dc44212014-02-19 14:31:44 +00002261 if (is_waiting_for_rm(sctx, ino)) {
2262 ret = gen_unique_name(sctx, ino, gen, name);
2263 if (ret < 0)
2264 goto out;
2265 ret = fs_path_add_path(dest, name);
2266 break;
2267 }
2268
Filipe Manana8b191a62015-04-09 14:09:14 +01002269 wdm = get_waiting_dir_move(sctx, ino);
2270 if (wdm && wdm->orphanized) {
2271 ret = gen_unique_name(sctx, ino, gen, name);
2272 stop = 1;
2273 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002274 ret = get_first_ref(sctx->parent_root, ino,
2275 &parent_inode, &parent_gen, name);
2276 } else {
2277 ret = __get_cur_name_and_parent(sctx, ino, gen,
2278 &parent_inode,
2279 &parent_gen, name);
2280 if (ret)
2281 stop = 1;
2282 }
2283
Alexander Block31db9f72012-07-25 23:19:24 +02002284 if (ret < 0)
2285 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002286
Alexander Block31db9f72012-07-25 23:19:24 +02002287 ret = fs_path_add_path(dest, name);
2288 if (ret < 0)
2289 goto out;
2290
2291 ino = parent_inode;
2292 gen = parent_gen;
2293 }
2294
2295out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002296 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002297 if (!ret)
2298 fs_path_unreverse(dest);
2299 return ret;
2300}
2301
2302/*
Alexander Block31db9f72012-07-25 23:19:24 +02002303 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2304 */
2305static int send_subvol_begin(struct send_ctx *sctx)
2306{
2307 int ret;
2308 struct btrfs_root *send_root = sctx->send_root;
2309 struct btrfs_root *parent_root = sctx->parent_root;
2310 struct btrfs_path *path;
2311 struct btrfs_key key;
2312 struct btrfs_root_ref *ref;
2313 struct extent_buffer *leaf;
2314 char *name = NULL;
2315 int namelen;
2316
Wang Shilongffcfaf82014-01-15 00:26:43 +08002317 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002318 if (!path)
2319 return -ENOMEM;
2320
David Sterbae780b0d2016-01-18 18:42:13 +01002321 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002322 if (!name) {
2323 btrfs_free_path(path);
2324 return -ENOMEM;
2325 }
2326
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002327 key.objectid = send_root->root_key.objectid;
Alexander Block31db9f72012-07-25 23:19:24 +02002328 key.type = BTRFS_ROOT_BACKREF_KEY;
2329 key.offset = 0;
2330
2331 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2332 &key, path, 1, 0);
2333 if (ret < 0)
2334 goto out;
2335 if (ret) {
2336 ret = -ENOENT;
2337 goto out;
2338 }
2339
2340 leaf = path->nodes[0];
2341 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2342 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002343 key.objectid != send_root->root_key.objectid) {
Alexander Block31db9f72012-07-25 23:19:24 +02002344 ret = -ENOENT;
2345 goto out;
2346 }
2347 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2348 namelen = btrfs_root_ref_name_len(leaf, ref);
2349 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2350 btrfs_release_path(path);
2351
Alexander Block31db9f72012-07-25 23:19:24 +02002352 if (parent_root) {
2353 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2354 if (ret < 0)
2355 goto out;
2356 } else {
2357 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2358 if (ret < 0)
2359 goto out;
2360 }
2361
2362 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002363
2364 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2365 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2366 sctx->send_root->root_item.received_uuid);
2367 else
2368 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2369 sctx->send_root->root_item.uuid);
2370
Alexander Block31db9f72012-07-25 23:19:24 +02002371 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002372 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002373 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002374 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2375 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2376 parent_root->root_item.received_uuid);
2377 else
2378 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2379 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002380 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002381 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002382 }
2383
2384 ret = send_cmd(sctx);
2385
2386tlv_put_failure:
2387out:
2388 btrfs_free_path(path);
2389 kfree(name);
2390 return ret;
2391}
2392
2393static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2394{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002395 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002396 int ret = 0;
2397 struct fs_path *p;
2398
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002399 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002400
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002401 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002402 if (!p)
2403 return -ENOMEM;
2404
2405 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2406 if (ret < 0)
2407 goto out;
2408
2409 ret = get_cur_path(sctx, ino, gen, p);
2410 if (ret < 0)
2411 goto out;
2412 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2413 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2414
2415 ret = send_cmd(sctx);
2416
2417tlv_put_failure:
2418out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002419 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002420 return ret;
2421}
2422
2423static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2424{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002425 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002426 int ret = 0;
2427 struct fs_path *p;
2428
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002429 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002430
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002431 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002432 if (!p)
2433 return -ENOMEM;
2434
2435 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2436 if (ret < 0)
2437 goto out;
2438
2439 ret = get_cur_path(sctx, ino, gen, p);
2440 if (ret < 0)
2441 goto out;
2442 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2443 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2444
2445 ret = send_cmd(sctx);
2446
2447tlv_put_failure:
2448out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002449 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002450 return ret;
2451}
2452
2453static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2454{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002455 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002456 int ret = 0;
2457 struct fs_path *p;
2458
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002459 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2460 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002461
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002462 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002463 if (!p)
2464 return -ENOMEM;
2465
2466 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2467 if (ret < 0)
2468 goto out;
2469
2470 ret = get_cur_path(sctx, ino, gen, p);
2471 if (ret < 0)
2472 goto out;
2473 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2474 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2475 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2476
2477 ret = send_cmd(sctx);
2478
2479tlv_put_failure:
2480out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002481 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002482 return ret;
2483}
2484
2485static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2486{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002487 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002488 int ret = 0;
2489 struct fs_path *p = NULL;
2490 struct btrfs_inode_item *ii;
2491 struct btrfs_path *path = NULL;
2492 struct extent_buffer *eb;
2493 struct btrfs_key key;
2494 int slot;
2495
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002496 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002497
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002498 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002499 if (!p)
2500 return -ENOMEM;
2501
2502 path = alloc_path_for_send();
2503 if (!path) {
2504 ret = -ENOMEM;
2505 goto out;
2506 }
2507
2508 key.objectid = ino;
2509 key.type = BTRFS_INODE_ITEM_KEY;
2510 key.offset = 0;
2511 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002512 if (ret > 0)
2513 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002514 if (ret < 0)
2515 goto out;
2516
2517 eb = path->nodes[0];
2518 slot = path->slots[0];
2519 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2520
2521 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2522 if (ret < 0)
2523 goto out;
2524
2525 ret = get_cur_path(sctx, ino, gen, p);
2526 if (ret < 0)
2527 goto out;
2528 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002529 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2530 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2531 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002532 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002533
2534 ret = send_cmd(sctx);
2535
2536tlv_put_failure:
2537out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002538 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002539 btrfs_free_path(path);
2540 return ret;
2541}
2542
2543/*
2544 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2545 * a valid path yet because we did not process the refs yet. So, the inode
2546 * is created as orphan.
2547 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002548static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002549{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002550 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002551 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002552 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002553 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002554 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002555 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002556 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002557
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002558 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002559
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002560 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002561 if (!p)
2562 return -ENOMEM;
2563
Liu Bo644d1942014-02-27 17:29:01 +08002564 if (ino != sctx->cur_ino) {
2565 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2566 NULL, NULL, &rdev);
2567 if (ret < 0)
2568 goto out;
2569 } else {
2570 gen = sctx->cur_inode_gen;
2571 mode = sctx->cur_inode_mode;
2572 rdev = sctx->cur_inode_rdev;
2573 }
Alexander Block31db9f72012-07-25 23:19:24 +02002574
Alexander Blocke938c8a2012-07-28 16:33:49 +02002575 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002576 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002577 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002578 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002579 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002580 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002581 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002582 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002583 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002584 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002585 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002586 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002587 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002588 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002589 (int)(mode & S_IFMT));
Tsutomu Itohca6842b2016-01-22 09:13:25 +09002590 ret = -EOPNOTSUPP;
Alexander Block31db9f72012-07-25 23:19:24 +02002591 goto out;
2592 }
2593
2594 ret = begin_cmd(sctx, cmd);
2595 if (ret < 0)
2596 goto out;
2597
Alexander Block1f4692d2012-07-28 10:42:24 +02002598 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002599 if (ret < 0)
2600 goto out;
2601
2602 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002603 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002604
2605 if (S_ISLNK(mode)) {
2606 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002607 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002608 if (ret < 0)
2609 goto out;
2610 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2611 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2612 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002613 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2614 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002615 }
2616
2617 ret = send_cmd(sctx);
2618 if (ret < 0)
2619 goto out;
2620
2621
2622tlv_put_failure:
2623out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002624 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002625 return ret;
2626}
2627
Alexander Block1f4692d2012-07-28 10:42:24 +02002628/*
2629 * We need some special handling for inodes that get processed before the parent
2630 * directory got created. See process_recorded_refs for details.
2631 * This function does the check if we already created the dir out of order.
2632 */
2633static int did_create_dir(struct send_ctx *sctx, u64 dir)
2634{
2635 int ret = 0;
2636 struct btrfs_path *path = NULL;
2637 struct btrfs_key key;
2638 struct btrfs_key found_key;
2639 struct btrfs_key di_key;
2640 struct extent_buffer *eb;
2641 struct btrfs_dir_item *di;
2642 int slot;
2643
2644 path = alloc_path_for_send();
2645 if (!path) {
2646 ret = -ENOMEM;
2647 goto out;
2648 }
2649
2650 key.objectid = dir;
2651 key.type = BTRFS_DIR_INDEX_KEY;
2652 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002653 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2654 if (ret < 0)
2655 goto out;
2656
Alexander Block1f4692d2012-07-28 10:42:24 +02002657 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002658 eb = path->nodes[0];
2659 slot = path->slots[0];
2660 if (slot >= btrfs_header_nritems(eb)) {
2661 ret = btrfs_next_leaf(sctx->send_root, path);
2662 if (ret < 0) {
2663 goto out;
2664 } else if (ret > 0) {
2665 ret = 0;
2666 break;
2667 }
2668 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002669 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002670
2671 btrfs_item_key_to_cpu(eb, &found_key, slot);
2672 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002673 found_key.type != key.type) {
2674 ret = 0;
2675 goto out;
2676 }
2677
2678 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2679 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2680
Josef Bacika0525412013-08-12 10:56:14 -04002681 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2682 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002683 ret = 1;
2684 goto out;
2685 }
2686
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002687 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002688 }
2689
2690out:
2691 btrfs_free_path(path);
2692 return ret;
2693}
2694
2695/*
2696 * Only creates the inode if it is:
2697 * 1. Not a directory
2698 * 2. Or a directory which was not created already due to out of order
2699 * directories. See did_create_dir and process_recorded_refs for details.
2700 */
2701static int send_create_inode_if_needed(struct send_ctx *sctx)
2702{
2703 int ret;
2704
2705 if (S_ISDIR(sctx->cur_inode_mode)) {
2706 ret = did_create_dir(sctx, sctx->cur_ino);
2707 if (ret < 0)
2708 goto out;
2709 if (ret) {
2710 ret = 0;
2711 goto out;
2712 }
2713 }
2714
2715 ret = send_create_inode(sctx, sctx->cur_ino);
2716 if (ret < 0)
2717 goto out;
2718
2719out:
2720 return ret;
2721}
2722
Alexander Block31db9f72012-07-25 23:19:24 +02002723struct recorded_ref {
2724 struct list_head list;
Alexander Block31db9f72012-07-25 23:19:24 +02002725 char *name;
2726 struct fs_path *full_path;
2727 u64 dir;
2728 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002729 int name_len;
2730};
2731
Filipe Mananafdb13882017-06-13 14:13:11 +01002732static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2733{
2734 ref->full_path = path;
2735 ref->name = (char *)kbasename(ref->full_path->start);
2736 ref->name_len = ref->full_path->end - ref->name;
2737}
2738
Alexander Block31db9f72012-07-25 23:19:24 +02002739/*
2740 * We need to process new refs before deleted refs, but compare_tree gives us
2741 * everything mixed. So we first record all refs and later process them.
2742 * This function is a helper to record one ref.
2743 */
Liu Boa4d96d62014-03-03 21:31:03 +08002744static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002745 u64 dir_gen, struct fs_path *path)
2746{
2747 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002748
David Sterbae780b0d2016-01-18 18:42:13 +01002749 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002750 if (!ref)
2751 return -ENOMEM;
2752
2753 ref->dir = dir;
2754 ref->dir_gen = dir_gen;
Filipe Mananafdb13882017-06-13 14:13:11 +01002755 set_ref_path(ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02002756 list_add_tail(&ref->list, head);
2757 return 0;
2758}
2759
Josef Bacikba5e8f22013-08-16 16:52:55 -04002760static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2761{
2762 struct recorded_ref *new;
2763
David Sterbae780b0d2016-01-18 18:42:13 +01002764 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002765 if (!new)
2766 return -ENOMEM;
2767
2768 new->dir = ref->dir;
2769 new->dir_gen = ref->dir_gen;
2770 new->full_path = NULL;
2771 INIT_LIST_HEAD(&new->list);
2772 list_add_tail(&new->list, list);
2773 return 0;
2774}
2775
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002776static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002777{
2778 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002779
Alexander Blocke938c8a2012-07-28 16:33:49 +02002780 while (!list_empty(head)) {
2781 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002782 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002783 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002784 kfree(cur);
2785 }
Alexander Block31db9f72012-07-25 23:19:24 +02002786}
2787
2788static void free_recorded_refs(struct send_ctx *sctx)
2789{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002790 __free_recorded_refs(&sctx->new_refs);
2791 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002792}
2793
2794/*
Alexander Block766702e2012-07-28 14:11:31 +02002795 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002796 * ref of an unprocessed inode gets overwritten and for all non empty
2797 * directories.
2798 */
2799static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2800 struct fs_path *path)
2801{
2802 int ret;
2803 struct fs_path *orphan;
2804
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002805 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002806 if (!orphan)
2807 return -ENOMEM;
2808
2809 ret = gen_unique_name(sctx, ino, gen, orphan);
2810 if (ret < 0)
2811 goto out;
2812
2813 ret = send_rename(sctx, path, orphan);
2814
2815out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002816 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002817 return ret;
2818}
2819
Filipe Manana9dc44212014-02-19 14:31:44 +00002820static struct orphan_dir_info *
2821add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2822{
2823 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2824 struct rb_node *parent = NULL;
2825 struct orphan_dir_info *entry, *odi;
2826
Filipe Manana9dc44212014-02-19 14:31:44 +00002827 while (*p) {
2828 parent = *p;
2829 entry = rb_entry(parent, struct orphan_dir_info, node);
2830 if (dir_ino < entry->ino) {
2831 p = &(*p)->rb_left;
2832 } else if (dir_ino > entry->ino) {
2833 p = &(*p)->rb_right;
2834 } else {
Filipe Manana9dc44212014-02-19 14:31:44 +00002835 return entry;
2836 }
2837 }
2838
Robbie Ko35c8eda2018-05-08 18:11:37 +08002839 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2840 if (!odi)
2841 return ERR_PTR(-ENOMEM);
2842 odi->ino = dir_ino;
2843 odi->gen = 0;
Robbie Ko0f96f512018-05-08 18:11:38 +08002844 odi->last_dir_index_offset = 0;
Robbie Ko35c8eda2018-05-08 18:11:37 +08002845
Filipe Manana9dc44212014-02-19 14:31:44 +00002846 rb_link_node(&odi->node, parent, p);
2847 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2848 return odi;
2849}
2850
2851static struct orphan_dir_info *
2852get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2853{
2854 struct rb_node *n = sctx->orphan_dirs.rb_node;
2855 struct orphan_dir_info *entry;
2856
2857 while (n) {
2858 entry = rb_entry(n, struct orphan_dir_info, node);
2859 if (dir_ino < entry->ino)
2860 n = n->rb_left;
2861 else if (dir_ino > entry->ino)
2862 n = n->rb_right;
2863 else
2864 return entry;
2865 }
2866 return NULL;
2867}
2868
2869static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2870{
2871 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2872
2873 return odi != NULL;
2874}
2875
2876static void free_orphan_dir_info(struct send_ctx *sctx,
2877 struct orphan_dir_info *odi)
2878{
2879 if (!odi)
2880 return;
2881 rb_erase(&odi->node, &sctx->orphan_dirs);
2882 kfree(odi);
2883}
2884
Alexander Block31db9f72012-07-25 23:19:24 +02002885/*
2886 * Returns 1 if a directory can be removed at this point in time.
2887 * We check this by iterating all dir items and checking if the inode behind
2888 * the dir item was already processed.
2889 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002890static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2891 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002892{
2893 int ret = 0;
2894 struct btrfs_root *root = sctx->parent_root;
2895 struct btrfs_path *path;
2896 struct btrfs_key key;
2897 struct btrfs_key found_key;
2898 struct btrfs_key loc;
2899 struct btrfs_dir_item *di;
Robbie Ko0f96f512018-05-08 18:11:38 +08002900 struct orphan_dir_info *odi = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02002901
Alexander Block6d85ed052012-08-01 14:48:59 +02002902 /*
2903 * Don't try to rmdir the top/root subvolume dir.
2904 */
2905 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2906 return 0;
2907
Alexander Block31db9f72012-07-25 23:19:24 +02002908 path = alloc_path_for_send();
2909 if (!path)
2910 return -ENOMEM;
2911
2912 key.objectid = dir;
2913 key.type = BTRFS_DIR_INDEX_KEY;
2914 key.offset = 0;
Robbie Ko0f96f512018-05-08 18:11:38 +08002915
2916 odi = get_orphan_dir_info(sctx, dir);
2917 if (odi)
2918 key.offset = odi->last_dir_index_offset;
2919
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002920 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2921 if (ret < 0)
2922 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002923
2924 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002925 struct waiting_dir_move *dm;
2926
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002927 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2928 ret = btrfs_next_leaf(root, path);
2929 if (ret < 0)
2930 goto out;
2931 else if (ret > 0)
2932 break;
2933 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002934 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002935 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2936 path->slots[0]);
2937 if (found_key.objectid != key.objectid ||
2938 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002939 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002940
2941 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2942 struct btrfs_dir_item);
2943 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2944
Filipe Manana9dc44212014-02-19 14:31:44 +00002945 dm = get_waiting_dir_move(sctx, loc.objectid);
2946 if (dm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002947 odi = add_orphan_dir_info(sctx, dir);
2948 if (IS_ERR(odi)) {
2949 ret = PTR_ERR(odi);
2950 goto out;
2951 }
2952 odi->gen = dir_gen;
Robbie Ko0f96f512018-05-08 18:11:38 +08002953 odi->last_dir_index_offset = found_key.offset;
Filipe Manana9dc44212014-02-19 14:31:44 +00002954 dm->rmdir_ino = dir;
2955 ret = 0;
2956 goto out;
2957 }
2958
Alexander Block31db9f72012-07-25 23:19:24 +02002959 if (loc.objectid > send_progress) {
Robbie Ko0f96f512018-05-08 18:11:38 +08002960 odi = add_orphan_dir_info(sctx, dir);
2961 if (IS_ERR(odi)) {
2962 ret = PTR_ERR(odi);
2963 goto out;
2964 }
2965 odi->gen = dir_gen;
2966 odi->last_dir_index_offset = found_key.offset;
Alexander Block31db9f72012-07-25 23:19:24 +02002967 ret = 0;
2968 goto out;
2969 }
2970
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002971 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002972 }
Robbie Ko0f96f512018-05-08 18:11:38 +08002973 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02002974
2975 ret = 1;
2976
2977out:
2978 btrfs_free_path(path);
2979 return ret;
2980}
2981
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002982static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2983{
Filipe Manana9dc44212014-02-19 14:31:44 +00002984 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002985
Filipe Manana9dc44212014-02-19 14:31:44 +00002986 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002987}
2988
Filipe Manana8b191a62015-04-09 14:09:14 +01002989static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002990{
2991 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2992 struct rb_node *parent = NULL;
2993 struct waiting_dir_move *entry, *dm;
2994
David Sterbae780b0d2016-01-18 18:42:13 +01002995 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002996 if (!dm)
2997 return -ENOMEM;
2998 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00002999 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003000 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003001
3002 while (*p) {
3003 parent = *p;
3004 entry = rb_entry(parent, struct waiting_dir_move, node);
3005 if (ino < entry->ino) {
3006 p = &(*p)->rb_left;
3007 } else if (ino > entry->ino) {
3008 p = &(*p)->rb_right;
3009 } else {
3010 kfree(dm);
3011 return -EEXIST;
3012 }
3013 }
3014
3015 rb_link_node(&dm->node, parent, p);
3016 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3017 return 0;
3018}
3019
Filipe Manana9dc44212014-02-19 14:31:44 +00003020static struct waiting_dir_move *
3021get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003022{
3023 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3024 struct waiting_dir_move *entry;
3025
3026 while (n) {
3027 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003028 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003029 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003030 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003031 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003032 else
3033 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003034 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003035 return NULL;
3036}
3037
3038static void free_waiting_dir_move(struct send_ctx *sctx,
3039 struct waiting_dir_move *dm)
3040{
3041 if (!dm)
3042 return;
3043 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3044 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003045}
3046
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003047static int add_pending_dir_move(struct send_ctx *sctx,
3048 u64 ino,
3049 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003050 u64 parent_ino,
3051 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003052 struct list_head *deleted_refs,
3053 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003054{
3055 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3056 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003057 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003058 struct recorded_ref *cur;
3059 int exists = 0;
3060 int ret;
3061
David Sterbae780b0d2016-01-18 18:42:13 +01003062 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003063 if (!pm)
3064 return -ENOMEM;
3065 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003066 pm->ino = ino;
3067 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003068 INIT_LIST_HEAD(&pm->list);
3069 INIT_LIST_HEAD(&pm->update_refs);
3070 RB_CLEAR_NODE(&pm->node);
3071
3072 while (*p) {
3073 parent = *p;
3074 entry = rb_entry(parent, struct pending_dir_move, node);
3075 if (parent_ino < entry->parent_ino) {
3076 p = &(*p)->rb_left;
3077 } else if (parent_ino > entry->parent_ino) {
3078 p = &(*p)->rb_right;
3079 } else {
3080 exists = 1;
3081 break;
3082 }
3083 }
3084
Filipe Mananaf9594922014-03-27 20:14:01 +00003085 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003086 ret = dup_ref(cur, &pm->update_refs);
3087 if (ret < 0)
3088 goto out;
3089 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003090 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003091 ret = dup_ref(cur, &pm->update_refs);
3092 if (ret < 0)
3093 goto out;
3094 }
3095
Filipe Manana8b191a62015-04-09 14:09:14 +01003096 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003097 if (ret)
3098 goto out;
3099
3100 if (exists) {
3101 list_add_tail(&pm->list, &entry->list);
3102 } else {
3103 rb_link_node(&pm->node, parent, p);
3104 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3105 }
3106 ret = 0;
3107out:
3108 if (ret) {
3109 __free_recorded_refs(&pm->update_refs);
3110 kfree(pm);
3111 }
3112 return ret;
3113}
3114
3115static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3116 u64 parent_ino)
3117{
3118 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3119 struct pending_dir_move *entry;
3120
3121 while (n) {
3122 entry = rb_entry(n, struct pending_dir_move, node);
3123 if (parent_ino < entry->parent_ino)
3124 n = n->rb_left;
3125 else if (parent_ino > entry->parent_ino)
3126 n = n->rb_right;
3127 else
3128 return entry;
3129 }
3130 return NULL;
3131}
3132
Robbie Ko801bec32015-06-23 18:39:46 +08003133static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3134 u64 ino, u64 gen, u64 *ancestor_ino)
3135{
3136 int ret = 0;
3137 u64 parent_inode = 0;
3138 u64 parent_gen = 0;
3139 u64 start_ino = ino;
3140
3141 *ancestor_ino = 0;
3142 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3143 fs_path_reset(name);
3144
3145 if (is_waiting_for_rm(sctx, ino))
3146 break;
3147 if (is_waiting_for_move(sctx, ino)) {
3148 if (*ancestor_ino == 0)
3149 *ancestor_ino = ino;
3150 ret = get_first_ref(sctx->parent_root, ino,
3151 &parent_inode, &parent_gen, name);
3152 } else {
3153 ret = __get_cur_name_and_parent(sctx, ino, gen,
3154 &parent_inode,
3155 &parent_gen, name);
3156 if (ret > 0) {
3157 ret = 0;
3158 break;
3159 }
3160 }
3161 if (ret < 0)
3162 break;
3163 if (parent_inode == start_ino) {
3164 ret = 1;
3165 if (*ancestor_ino == 0)
3166 *ancestor_ino = ino;
3167 break;
3168 }
3169 ino = parent_inode;
3170 gen = parent_gen;
3171 }
3172 return ret;
3173}
3174
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003175static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3176{
3177 struct fs_path *from_path = NULL;
3178 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003179 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003180 u64 orig_progress = sctx->send_progress;
3181 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003182 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003183 struct waiting_dir_move *dm = NULL;
3184 u64 rmdir_ino = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003185 u64 ancestor;
3186 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003187 int ret;
3188
Filipe Manana2b863a12014-02-16 13:43:11 +00003189 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003190 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003191 if (!name || !from_path) {
3192 ret = -ENOMEM;
3193 goto out;
3194 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003195
Filipe Manana9dc44212014-02-19 14:31:44 +00003196 dm = get_waiting_dir_move(sctx, pm->ino);
3197 ASSERT(dm);
3198 rmdir_ino = dm->rmdir_ino;
Robbie Ko801bec32015-06-23 18:39:46 +08003199 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003200 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003201
Robbie Ko801bec32015-06-23 18:39:46 +08003202 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003203 ret = gen_unique_name(sctx, pm->ino,
3204 pm->gen, from_path);
3205 } else {
3206 ret = get_first_ref(sctx->parent_root, pm->ino,
3207 &parent_ino, &parent_gen, name);
3208 if (ret < 0)
3209 goto out;
3210 ret = get_cur_path(sctx, parent_ino, parent_gen,
3211 from_path);
3212 if (ret < 0)
3213 goto out;
3214 ret = fs_path_add_path(from_path, name);
3215 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003216 if (ret < 0)
3217 goto out;
3218
Filipe Mananaf9594922014-03-27 20:14:01 +00003219 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003220 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003221 if (ret < 0)
3222 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003223 if (ret) {
3224 LIST_HEAD(deleted_refs);
3225 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3226 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3227 &pm->update_refs, &deleted_refs,
3228 is_orphan);
3229 if (ret < 0)
3230 goto out;
3231 if (rmdir_ino) {
3232 dm = get_waiting_dir_move(sctx, pm->ino);
3233 ASSERT(dm);
3234 dm->rmdir_ino = rmdir_ino;
3235 }
3236 goto out;
3237 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003238 fs_path_reset(name);
3239 to_path = name;
3240 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003241 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3242 if (ret < 0)
3243 goto out;
3244
3245 ret = send_rename(sctx, from_path, to_path);
3246 if (ret < 0)
3247 goto out;
3248
Filipe Manana9dc44212014-02-19 14:31:44 +00003249 if (rmdir_ino) {
3250 struct orphan_dir_info *odi;
Robbie Ko0f96f512018-05-08 18:11:38 +08003251 u64 gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003252
3253 odi = get_orphan_dir_info(sctx, rmdir_ino);
3254 if (!odi) {
3255 /* already deleted */
3256 goto finish;
3257 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003258 gen = odi->gen;
3259
3260 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003261 if (ret < 0)
3262 goto out;
3263 if (!ret)
3264 goto finish;
3265
3266 name = fs_path_alloc();
3267 if (!name) {
3268 ret = -ENOMEM;
3269 goto out;
3270 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003271 ret = get_cur_path(sctx, rmdir_ino, gen, name);
Filipe Manana9dc44212014-02-19 14:31:44 +00003272 if (ret < 0)
3273 goto out;
3274 ret = send_rmdir(sctx, name);
3275 if (ret < 0)
3276 goto out;
Filipe Manana9dc44212014-02-19 14:31:44 +00003277 }
3278
3279finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003280 ret = send_utimes(sctx, pm->ino, pm->gen);
3281 if (ret < 0)
3282 goto out;
3283
3284 /*
3285 * After rename/move, need to update the utimes of both new parent(s)
3286 * and old parent(s).
3287 */
3288 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003289 /*
3290 * The parent inode might have been deleted in the send snapshot
3291 */
3292 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3293 NULL, NULL, NULL, NULL, NULL);
3294 if (ret == -ENOENT) {
3295 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003296 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003297 }
3298 if (ret < 0)
3299 goto out;
3300
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003301 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3302 if (ret < 0)
3303 goto out;
3304 }
3305
3306out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003307 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003308 fs_path_free(from_path);
3309 fs_path_free(to_path);
3310 sctx->send_progress = orig_progress;
3311
3312 return ret;
3313}
3314
3315static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3316{
3317 if (!list_empty(&m->list))
3318 list_del(&m->list);
3319 if (!RB_EMPTY_NODE(&m->node))
3320 rb_erase(&m->node, &sctx->pending_dir_moves);
3321 __free_recorded_refs(&m->update_refs);
3322 kfree(m);
3323}
3324
Robbie Koa4390ae2018-11-14 18:32:37 +00003325static void tail_append_pending_moves(struct send_ctx *sctx,
3326 struct pending_dir_move *moves,
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003327 struct list_head *stack)
3328{
3329 if (list_empty(&moves->list)) {
3330 list_add_tail(&moves->list, stack);
3331 } else {
3332 LIST_HEAD(list);
3333 list_splice_init(&moves->list, &list);
3334 list_add_tail(&moves->list, stack);
3335 list_splice_tail(&list, stack);
3336 }
Robbie Koa4390ae2018-11-14 18:32:37 +00003337 if (!RB_EMPTY_NODE(&moves->node)) {
3338 rb_erase(&moves->node, &sctx->pending_dir_moves);
3339 RB_CLEAR_NODE(&moves->node);
3340 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003341}
3342
3343static int apply_children_dir_moves(struct send_ctx *sctx)
3344{
3345 struct pending_dir_move *pm;
3346 struct list_head stack;
3347 u64 parent_ino = sctx->cur_ino;
3348 int ret = 0;
3349
3350 pm = get_pending_dir_moves(sctx, parent_ino);
3351 if (!pm)
3352 return 0;
3353
3354 INIT_LIST_HEAD(&stack);
Robbie Koa4390ae2018-11-14 18:32:37 +00003355 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003356
3357 while (!list_empty(&stack)) {
3358 pm = list_first_entry(&stack, struct pending_dir_move, list);
3359 parent_ino = pm->ino;
3360 ret = apply_dir_move(sctx, pm);
3361 free_pending_move(sctx, pm);
3362 if (ret)
3363 goto out;
3364 pm = get_pending_dir_moves(sctx, parent_ino);
3365 if (pm)
Robbie Koa4390ae2018-11-14 18:32:37 +00003366 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003367 }
3368 return 0;
3369
3370out:
3371 while (!list_empty(&stack)) {
3372 pm = list_first_entry(&stack, struct pending_dir_move, list);
3373 free_pending_move(sctx, pm);
3374 }
3375 return ret;
3376}
3377
Filipe Manana84471e22015-02-28 22:29:22 +00003378/*
3379 * We might need to delay a directory rename even when no ancestor directory
3380 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3381 * renamed. This happens when we rename a directory to the old name (the name
3382 * in the parent root) of some other unrelated directory that got its rename
3383 * delayed due to some ancestor with higher number that got renamed.
3384 *
3385 * Example:
3386 *
3387 * Parent snapshot:
3388 * . (ino 256)
3389 * |---- a/ (ino 257)
3390 * | |---- file (ino 260)
3391 * |
3392 * |---- b/ (ino 258)
3393 * |---- c/ (ino 259)
3394 *
3395 * Send snapshot:
3396 * . (ino 256)
3397 * |---- a/ (ino 258)
3398 * |---- x/ (ino 259)
3399 * |---- y/ (ino 257)
3400 * |----- file (ino 260)
3401 *
3402 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3403 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3404 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3405 * must issue is:
3406 *
3407 * 1 - rename 259 from 'c' to 'x'
3408 * 2 - rename 257 from 'a' to 'x/y'
3409 * 3 - rename 258 from 'b' to 'a'
3410 *
3411 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3412 * be done right away and < 0 on error.
3413 */
3414static int wait_for_dest_dir_move(struct send_ctx *sctx,
3415 struct recorded_ref *parent_ref,
3416 const bool is_orphan)
3417{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003418 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003419 struct btrfs_path *path;
3420 struct btrfs_key key;
3421 struct btrfs_key di_key;
3422 struct btrfs_dir_item *di;
3423 u64 left_gen;
3424 u64 right_gen;
3425 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003426 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003427
3428 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3429 return 0;
3430
3431 path = alloc_path_for_send();
3432 if (!path)
3433 return -ENOMEM;
3434
3435 key.objectid = parent_ref->dir;
3436 key.type = BTRFS_DIR_ITEM_KEY;
3437 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3438
3439 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3440 if (ret < 0) {
3441 goto out;
3442 } else if (ret > 0) {
3443 ret = 0;
3444 goto out;
3445 }
3446
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003447 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3448 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003449 if (!di) {
3450 ret = 0;
3451 goto out;
3452 }
3453 /*
3454 * di_key.objectid has the number of the inode that has a dentry in the
3455 * parent directory with the same name that sctx->cur_ino is being
3456 * renamed to. We need to check if that inode is in the send root as
3457 * well and if it is currently marked as an inode with a pending rename,
3458 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3459 * that it happens after that other inode is renamed.
3460 */
3461 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3462 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3463 ret = 0;
3464 goto out;
3465 }
3466
3467 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3468 &left_gen, NULL, NULL, NULL, NULL);
3469 if (ret < 0)
3470 goto out;
3471 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3472 &right_gen, NULL, NULL, NULL, NULL);
3473 if (ret < 0) {
3474 if (ret == -ENOENT)
3475 ret = 0;
3476 goto out;
3477 }
3478
3479 /* Different inode, no need to delay the rename of sctx->cur_ino */
3480 if (right_gen != left_gen) {
3481 ret = 0;
3482 goto out;
3483 }
3484
Robbie Ko801bec32015-06-23 18:39:46 +08003485 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3486 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003487 ret = add_pending_dir_move(sctx,
3488 sctx->cur_ino,
3489 sctx->cur_inode_gen,
3490 di_key.objectid,
3491 &sctx->new_refs,
3492 &sctx->deleted_refs,
3493 is_orphan);
3494 if (!ret)
3495 ret = 1;
3496 }
3497out:
3498 btrfs_free_path(path);
3499 return ret;
3500}
3501
Filipe Manana80aa6022015-03-27 17:50:45 +00003502/*
Filipe Mananaea37d592017-11-17 01:54:00 +00003503 * Check if inode ino2, or any of its ancestors, is inode ino1.
3504 * Return 1 if true, 0 if false and < 0 on error.
3505 */
3506static int check_ino_in_path(struct btrfs_root *root,
3507 const u64 ino1,
3508 const u64 ino1_gen,
3509 const u64 ino2,
3510 const u64 ino2_gen,
3511 struct fs_path *fs_path)
3512{
3513 u64 ino = ino2;
3514
3515 if (ino1 == ino2)
3516 return ino1_gen == ino2_gen;
3517
3518 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3519 u64 parent;
3520 u64 parent_gen;
3521 int ret;
3522
3523 fs_path_reset(fs_path);
3524 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3525 if (ret < 0)
3526 return ret;
3527 if (parent == ino1)
3528 return parent_gen == ino1_gen;
3529 ino = parent;
3530 }
3531 return 0;
3532}
3533
3534/*
3535 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3536 * possible path (in case ino2 is not a directory and has multiple hard links).
Filipe Manana80aa6022015-03-27 17:50:45 +00003537 * Return 1 if true, 0 if false and < 0 on error.
3538 */
3539static int is_ancestor(struct btrfs_root *root,
3540 const u64 ino1,
3541 const u64 ino1_gen,
3542 const u64 ino2,
3543 struct fs_path *fs_path)
3544{
Filipe Mananaea37d592017-11-17 01:54:00 +00003545 bool free_fs_path = false;
Filipe Manana72c36682017-06-07 11:41:29 +01003546 int ret = 0;
Filipe Mananaea37d592017-11-17 01:54:00 +00003547 struct btrfs_path *path = NULL;
3548 struct btrfs_key key;
Filipe Manana72c36682017-06-07 11:41:29 +01003549
3550 if (!fs_path) {
3551 fs_path = fs_path_alloc();
3552 if (!fs_path)
3553 return -ENOMEM;
Filipe Mananaea37d592017-11-17 01:54:00 +00003554 free_fs_path = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003555 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003556
Filipe Mananaea37d592017-11-17 01:54:00 +00003557 path = alloc_path_for_send();
3558 if (!path) {
3559 ret = -ENOMEM;
3560 goto out;
Filipe Manana80aa6022015-03-27 17:50:45 +00003561 }
Filipe Mananaea37d592017-11-17 01:54:00 +00003562
3563 key.objectid = ino2;
3564 key.type = BTRFS_INODE_REF_KEY;
3565 key.offset = 0;
3566
3567 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3568 if (ret < 0)
3569 goto out;
3570
3571 while (true) {
3572 struct extent_buffer *leaf = path->nodes[0];
3573 int slot = path->slots[0];
3574 u32 cur_offset = 0;
3575 u32 item_size;
3576
3577 if (slot >= btrfs_header_nritems(leaf)) {
3578 ret = btrfs_next_leaf(root, path);
3579 if (ret < 0)
3580 goto out;
3581 if (ret > 0)
3582 break;
3583 continue;
3584 }
3585
3586 btrfs_item_key_to_cpu(leaf, &key, slot);
3587 if (key.objectid != ino2)
3588 break;
3589 if (key.type != BTRFS_INODE_REF_KEY &&
3590 key.type != BTRFS_INODE_EXTREF_KEY)
3591 break;
3592
3593 item_size = btrfs_item_size_nr(leaf, slot);
3594 while (cur_offset < item_size) {
3595 u64 parent;
3596 u64 parent_gen;
3597
3598 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3599 unsigned long ptr;
3600 struct btrfs_inode_extref *extref;
3601
3602 ptr = btrfs_item_ptr_offset(leaf, slot);
3603 extref = (struct btrfs_inode_extref *)
3604 (ptr + cur_offset);
3605 parent = btrfs_inode_extref_parent(leaf,
3606 extref);
3607 cur_offset += sizeof(*extref);
3608 cur_offset += btrfs_inode_extref_name_len(leaf,
3609 extref);
3610 } else {
3611 parent = key.offset;
3612 cur_offset = item_size;
3613 }
3614
3615 ret = get_inode_info(root, parent, NULL, &parent_gen,
3616 NULL, NULL, NULL, NULL);
3617 if (ret < 0)
3618 goto out;
3619 ret = check_ino_in_path(root, ino1, ino1_gen,
3620 parent, parent_gen, fs_path);
3621 if (ret)
3622 goto out;
3623 }
3624 path->slots[0]++;
3625 }
3626 ret = 0;
Filipe Manana72c36682017-06-07 11:41:29 +01003627 out:
Filipe Mananaea37d592017-11-17 01:54:00 +00003628 btrfs_free_path(path);
3629 if (free_fs_path)
Filipe Manana72c36682017-06-07 11:41:29 +01003630 fs_path_free(fs_path);
3631 return ret;
Filipe Manana80aa6022015-03-27 17:50:45 +00003632}
3633
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003634static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003635 struct recorded_ref *parent_ref,
3636 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003637{
Filipe Mananaf9594922014-03-27 20:14:01 +00003638 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003639 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003640 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003641 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003642 struct fs_path *path_before = NULL;
3643 struct fs_path *path_after = NULL;
3644 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003645
3646 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003647 path_before = fs_path_alloc();
3648 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003649 ret = -ENOMEM;
3650 goto out;
3651 }
3652
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003653 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003654 * Our current directory inode may not yet be renamed/moved because some
3655 * ancestor (immediate or not) has to be renamed/moved first. So find if
3656 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003657 * that ancestor is processed to avoid path build infinite loops (done
3658 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003659 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003660 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003661 u64 parent_ino_after_gen;
3662
Filipe Mananaf9594922014-03-27 20:14:01 +00003663 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003664 /*
3665 * If the current inode is an ancestor of ino in the
3666 * parent root, we need to delay the rename of the
3667 * current inode, otherwise don't delayed the rename
3668 * because we can end up with a circular dependency
3669 * of renames, resulting in some directories never
3670 * getting the respective rename operations issued in
3671 * the send stream or getting into infinite path build
3672 * loops.
3673 */
3674 ret = is_ancestor(sctx->parent_root,
3675 sctx->cur_ino, sctx->cur_inode_gen,
3676 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003677 if (ret)
3678 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003679 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003680
3681 fs_path_reset(path_before);
3682 fs_path_reset(path_after);
3683
3684 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003685 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003686 if (ret < 0)
3687 goto out;
3688 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3689 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003690 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003691 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003692 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003693 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003694 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003695 }
3696
3697 len1 = fs_path_len(path_before);
3698 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003699 if (ino > sctx->cur_ino &&
3700 (parent_ino_before != parent_ino_after || len1 != len2 ||
3701 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003702 u64 parent_ino_gen;
3703
3704 ret = get_inode_info(sctx->parent_root, ino, NULL,
3705 &parent_ino_gen, NULL, NULL, NULL,
3706 NULL);
3707 if (ret < 0)
3708 goto out;
3709 if (ino_gen == parent_ino_gen) {
3710 ret = 1;
3711 break;
3712 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003713 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003714 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003715 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003716 }
3717
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003718out:
3719 fs_path_free(path_before);
3720 fs_path_free(path_after);
3721
Filipe Mananaf9594922014-03-27 20:14:01 +00003722 if (ret == 1) {
3723 ret = add_pending_dir_move(sctx,
3724 sctx->cur_ino,
3725 sctx->cur_inode_gen,
3726 ino,
3727 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003728 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003729 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003730 if (!ret)
3731 ret = 1;
3732 }
3733
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003734 return ret;
3735}
3736
Filipe Mananaf5962782017-06-22 20:03:51 +01003737static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3738{
3739 int ret;
3740 struct fs_path *new_path;
3741
3742 /*
3743 * Our reference's name member points to its full_path member string, so
3744 * we use here a new path.
3745 */
3746 new_path = fs_path_alloc();
3747 if (!new_path)
3748 return -ENOMEM;
3749
3750 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3751 if (ret < 0) {
3752 fs_path_free(new_path);
3753 return ret;
3754 }
3755 ret = fs_path_add(new_path, ref->name, ref->name_len);
3756 if (ret < 0) {
3757 fs_path_free(new_path);
3758 return ret;
3759 }
3760
3761 fs_path_free(ref->full_path);
3762 set_ref_path(ref, new_path);
3763
3764 return 0;
3765}
3766
Alexander Block31db9f72012-07-25 23:19:24 +02003767/*
3768 * This does all the move/link/unlink/rmdir magic.
3769 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003770static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003771{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003772 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003773 int ret = 0;
3774 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003775 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003776 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003777 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003778 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003779 u64 ow_gen;
Filipe Mananaf5962782017-06-22 20:03:51 +01003780 u64 ow_mode;
Alexander Block31db9f72012-07-25 23:19:24 +02003781 int did_overwrite = 0;
3782 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003783 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003784 bool can_rename = true;
Filipe Mananaf5962782017-06-22 20:03:51 +01003785 bool orphanized_dir = false;
Filipe Mananafdb13882017-06-13 14:13:11 +01003786 bool orphanized_ancestor = false;
Alexander Block31db9f72012-07-25 23:19:24 +02003787
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003788 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003789
Alexander Block6d85ed052012-08-01 14:48:59 +02003790 /*
3791 * This should never happen as the root dir always has the same ref
3792 * which is always '..'
3793 */
3794 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003795 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed052012-08-01 14:48:59 +02003796
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003797 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003798 if (!valid_path) {
3799 ret = -ENOMEM;
3800 goto out;
3801 }
3802
Alexander Block31db9f72012-07-25 23:19:24 +02003803 /*
3804 * First, check if the first ref of the current inode was overwritten
3805 * before. If yes, we know that the current inode was already orphanized
3806 * and thus use the orphan name. If not, we can use get_cur_path to
3807 * get the path of the first ref as it would like while receiving at
3808 * this point in time.
3809 * New inodes are always orphan at the beginning, so force to use the
3810 * orphan name in this case.
3811 * The first ref is stored in valid_path and will be updated if it
3812 * gets moved around.
3813 */
3814 if (!sctx->cur_inode_new) {
3815 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3816 sctx->cur_inode_gen);
3817 if (ret < 0)
3818 goto out;
3819 if (ret)
3820 did_overwrite = 1;
3821 }
3822 if (sctx->cur_inode_new || did_overwrite) {
3823 ret = gen_unique_name(sctx, sctx->cur_ino,
3824 sctx->cur_inode_gen, valid_path);
3825 if (ret < 0)
3826 goto out;
3827 is_orphan = 1;
3828 } else {
3829 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3830 valid_path);
3831 if (ret < 0)
3832 goto out;
3833 }
3834
3835 list_for_each_entry(cur, &sctx->new_refs, list) {
3836 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003837 * We may have refs where the parent directory does not exist
3838 * yet. This happens if the parent directories inum is higher
Andrea Gelmini52042d82018-11-28 12:05:13 +01003839 * than the current inum. To handle this case, we create the
Alexander Block1f4692d2012-07-28 10:42:24 +02003840 * parent directory out of order. But we need to check if this
3841 * did already happen before due to other refs in the same dir.
3842 */
3843 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3844 if (ret < 0)
3845 goto out;
3846 if (ret == inode_state_will_create) {
3847 ret = 0;
3848 /*
3849 * First check if any of the current inodes refs did
3850 * already create the dir.
3851 */
3852 list_for_each_entry(cur2, &sctx->new_refs, list) {
3853 if (cur == cur2)
3854 break;
3855 if (cur2->dir == cur->dir) {
3856 ret = 1;
3857 break;
3858 }
3859 }
3860
3861 /*
3862 * If that did not happen, check if a previous inode
3863 * did already create the dir.
3864 */
3865 if (!ret)
3866 ret = did_create_dir(sctx, cur->dir);
3867 if (ret < 0)
3868 goto out;
3869 if (!ret) {
3870 ret = send_create_inode(sctx, cur->dir);
3871 if (ret < 0)
3872 goto out;
3873 }
3874 }
3875
3876 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003877 * Check if this new ref would overwrite the first ref of
3878 * another unprocessed inode. If yes, orphanize the
3879 * overwritten inode. If we find an overwritten ref that is
3880 * not the first ref, simply unlink it.
3881 */
3882 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3883 cur->name, cur->name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01003884 &ow_inode, &ow_gen, &ow_mode);
Alexander Block31db9f72012-07-25 23:19:24 +02003885 if (ret < 0)
3886 goto out;
3887 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003888 ret = is_first_ref(sctx->parent_root,
3889 ow_inode, cur->dir, cur->name,
3890 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003891 if (ret < 0)
3892 goto out;
3893 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003894 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003895 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003896
Alexander Block31db9f72012-07-25 23:19:24 +02003897 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3898 cur->full_path);
3899 if (ret < 0)
3900 goto out;
Filipe Mananaf5962782017-06-22 20:03:51 +01003901 if (S_ISDIR(ow_mode))
3902 orphanized_dir = true;
Robbie Ko801bec32015-06-23 18:39:46 +08003903
3904 /*
3905 * If ow_inode has its rename operation delayed
3906 * make sure that its orphanized name is used in
3907 * the source path when performing its rename
3908 * operation.
3909 */
3910 if (is_waiting_for_move(sctx, ow_inode)) {
3911 wdm = get_waiting_dir_move(sctx,
3912 ow_inode);
3913 ASSERT(wdm);
3914 wdm->orphanized = true;
3915 }
3916
Filipe Manana8996a482015-03-12 15:16:20 +00003917 /*
3918 * Make sure we clear our orphanized inode's
3919 * name from the name cache. This is because the
3920 * inode ow_inode might be an ancestor of some
3921 * other inode that will be orphanized as well
3922 * later and has an inode number greater than
3923 * sctx->send_progress. We need to prevent
3924 * future name lookups from using the old name
3925 * and get instead the orphan name.
3926 */
3927 nce = name_cache_search(sctx, ow_inode, ow_gen);
3928 if (nce) {
3929 name_cache_delete(sctx, nce);
3930 kfree(nce);
3931 }
Robbie Ko801bec32015-06-23 18:39:46 +08003932
3933 /*
3934 * ow_inode might currently be an ancestor of
3935 * cur_ino, therefore compute valid_path (the
3936 * current path of cur_ino) again because it
3937 * might contain the pre-orphanization name of
3938 * ow_inode, which is no longer valid.
3939 */
Filipe Manana72c36682017-06-07 11:41:29 +01003940 ret = is_ancestor(sctx->parent_root,
3941 ow_inode, ow_gen,
3942 sctx->cur_ino, NULL);
3943 if (ret > 0) {
Filipe Mananafdb13882017-06-13 14:13:11 +01003944 orphanized_ancestor = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003945 fs_path_reset(valid_path);
3946 ret = get_cur_path(sctx, sctx->cur_ino,
3947 sctx->cur_inode_gen,
3948 valid_path);
3949 }
Robbie Ko801bec32015-06-23 18:39:46 +08003950 if (ret < 0)
3951 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003952 } else {
3953 ret = send_unlink(sctx, cur->full_path);
3954 if (ret < 0)
3955 goto out;
3956 }
3957 }
3958
Filipe Manana84471e22015-02-28 22:29:22 +00003959 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3960 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3961 if (ret < 0)
3962 goto out;
3963 if (ret == 1) {
3964 can_rename = false;
3965 *pending_move = 1;
3966 }
3967 }
3968
Filipe Manana8b191a62015-04-09 14:09:14 +01003969 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3970 can_rename) {
3971 ret = wait_for_parent_move(sctx, cur, is_orphan);
3972 if (ret < 0)
3973 goto out;
3974 if (ret == 1) {
3975 can_rename = false;
3976 *pending_move = 1;
3977 }
3978 }
3979
Alexander Block31db9f72012-07-25 23:19:24 +02003980 /*
3981 * link/move the ref to the new place. If we have an orphan
3982 * inode, move it and update valid_path. If not, link or move
3983 * it depending on the inode mode.
3984 */
Filipe Manana84471e22015-02-28 22:29:22 +00003985 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003986 ret = send_rename(sctx, valid_path, cur->full_path);
3987 if (ret < 0)
3988 goto out;
3989 is_orphan = 0;
3990 ret = fs_path_copy(valid_path, cur->full_path);
3991 if (ret < 0)
3992 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00003993 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003994 if (S_ISDIR(sctx->cur_inode_mode)) {
3995 /*
3996 * Dirs can't be linked, so move it. For moved
3997 * dirs, we always have one new and one deleted
3998 * ref. The deleted ref is ignored later.
3999 */
Filipe Manana8b191a62015-04-09 14:09:14 +01004000 ret = send_rename(sctx, valid_path,
4001 cur->full_path);
4002 if (!ret)
4003 ret = fs_path_copy(valid_path,
4004 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004005 if (ret < 0)
4006 goto out;
4007 } else {
Filipe Mananaf5962782017-06-22 20:03:51 +01004008 /*
4009 * We might have previously orphanized an inode
4010 * which is an ancestor of our current inode,
4011 * so our reference's full path, which was
4012 * computed before any such orphanizations, must
4013 * be updated.
4014 */
4015 if (orphanized_dir) {
4016 ret = update_ref_path(sctx, cur);
4017 if (ret < 0)
4018 goto out;
4019 }
Alexander Block31db9f72012-07-25 23:19:24 +02004020 ret = send_link(sctx, cur->full_path,
4021 valid_path);
4022 if (ret < 0)
4023 goto out;
4024 }
4025 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004026 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004027 if (ret < 0)
4028 goto out;
4029 }
4030
4031 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4032 /*
4033 * Check if we can already rmdir the directory. If not,
4034 * orphanize it. For every dir item inside that gets deleted
4035 * later, we do this check again and rmdir it then if possible.
4036 * See the use of check_dirs for more details.
4037 */
Filipe Manana9dc44212014-02-19 14:31:44 +00004038 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4039 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004040 if (ret < 0)
4041 goto out;
4042 if (ret) {
4043 ret = send_rmdir(sctx, valid_path);
4044 if (ret < 0)
4045 goto out;
4046 } else if (!is_orphan) {
4047 ret = orphanize_inode(sctx, sctx->cur_ino,
4048 sctx->cur_inode_gen, valid_path);
4049 if (ret < 0)
4050 goto out;
4051 is_orphan = 1;
4052 }
4053
4054 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004055 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004056 if (ret < 0)
4057 goto out;
4058 }
Alexander Blockccf16262012-07-28 11:46:29 +02004059 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4060 !list_empty(&sctx->deleted_refs)) {
4061 /*
4062 * We have a moved dir. Add the old parent to check_dirs
4063 */
4064 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4065 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04004066 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02004067 if (ret < 0)
4068 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004069 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4070 /*
4071 * We have a non dir inode. Go through all deleted refs and
4072 * unlink them if they were not already overwritten by other
4073 * inodes.
4074 */
4075 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4076 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4077 sctx->cur_ino, sctx->cur_inode_gen,
4078 cur->name, cur->name_len);
4079 if (ret < 0)
4080 goto out;
4081 if (!ret) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004082 /*
4083 * If we orphanized any ancestor before, we need
4084 * to recompute the full path for deleted names,
4085 * since any such path was computed before we
4086 * processed any references and orphanized any
4087 * ancestor inode.
4088 */
4089 if (orphanized_ancestor) {
Filipe Mananaf5962782017-06-22 20:03:51 +01004090 ret = update_ref_path(sctx, cur);
4091 if (ret < 0)
Filipe Mananafdb13882017-06-13 14:13:11 +01004092 goto out;
Filipe Mananafdb13882017-06-13 14:13:11 +01004093 }
Alexander Block1f4692d2012-07-28 10:42:24 +02004094 ret = send_unlink(sctx, cur->full_path);
4095 if (ret < 0)
4096 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004097 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004098 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004099 if (ret < 0)
4100 goto out;
4101 }
Alexander Block31db9f72012-07-25 23:19:24 +02004102 /*
4103 * If the inode is still orphan, unlink the orphan. This may
4104 * happen when a previous inode did overwrite the first ref
4105 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02004106 * inode. Unlinking does not mean that the inode is deleted in
4107 * all cases. There may still be links to this inode in other
4108 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02004109 */
Alexander Block1f4692d2012-07-28 10:42:24 +02004110 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02004111 ret = send_unlink(sctx, valid_path);
4112 if (ret < 0)
4113 goto out;
4114 }
4115 }
4116
4117 /*
4118 * We did collect all parent dirs where cur_inode was once located. We
4119 * now go through all these dirs and check if they are pending for
4120 * deletion and if it's finally possible to perform the rmdir now.
4121 * We also update the inode stats of the parent dirs here.
4122 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004123 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02004124 /*
4125 * In case we had refs into dirs that were not processed yet,
4126 * we don't need to do the utime and rmdir logic for these dirs.
4127 * The dir will be processed later.
4128 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004129 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02004130 continue;
4131
Josef Bacikba5e8f22013-08-16 16:52:55 -04004132 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004133 if (ret < 0)
4134 goto out;
4135
4136 if (ret == inode_state_did_create ||
4137 ret == inode_state_no_change) {
4138 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004139 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004140 if (ret < 0)
4141 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004142 } else if (ret == inode_state_did_delete &&
4143 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004144 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4145 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004146 if (ret < 0)
4147 goto out;
4148 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004149 ret = get_cur_path(sctx, cur->dir,
4150 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004151 if (ret < 0)
4152 goto out;
4153 ret = send_rmdir(sctx, valid_path);
4154 if (ret < 0)
4155 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004156 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004157 }
4158 }
4159 }
4160
Alexander Block31db9f72012-07-25 23:19:24 +02004161 ret = 0;
4162
4163out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004164 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004165 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004166 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004167 return ret;
4168}
4169
Nikolay Borisova0357512017-08-21 12:43:44 +03004170static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4171 void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004172{
4173 int ret = 0;
4174 struct send_ctx *sctx = ctx;
4175 struct fs_path *p;
4176 u64 gen;
4177
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004178 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004179 if (!p)
4180 return -ENOMEM;
4181
Liu Boa4d96d62014-03-03 21:31:03 +08004182 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004183 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004184 if (ret < 0)
4185 goto out;
4186
Alexander Block31db9f72012-07-25 23:19:24 +02004187 ret = get_cur_path(sctx, dir, gen, p);
4188 if (ret < 0)
4189 goto out;
4190 ret = fs_path_add_path(p, name);
4191 if (ret < 0)
4192 goto out;
4193
Liu Boa4d96d62014-03-03 21:31:03 +08004194 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004195
4196out:
4197 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004198 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004199 return ret;
4200}
4201
Liu Boa4d96d62014-03-03 21:31:03 +08004202static int __record_new_ref(int num, u64 dir, int index,
4203 struct fs_path *name,
4204 void *ctx)
4205{
4206 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004207 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
Liu Boa4d96d62014-03-03 21:31:03 +08004208}
4209
4210
Alexander Block31db9f72012-07-25 23:19:24 +02004211static int __record_deleted_ref(int num, u64 dir, int index,
4212 struct fs_path *name,
4213 void *ctx)
4214{
Alexander Block31db9f72012-07-25 23:19:24 +02004215 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004216 return record_ref(sctx->parent_root, dir, name, ctx,
4217 &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004218}
4219
4220static int record_new_ref(struct send_ctx *sctx)
4221{
4222 int ret;
4223
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004224 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4225 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004226 if (ret < 0)
4227 goto out;
4228 ret = 0;
4229
4230out:
4231 return ret;
4232}
4233
4234static int record_deleted_ref(struct send_ctx *sctx)
4235{
4236 int ret;
4237
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004238 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4239 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004240 if (ret < 0)
4241 goto out;
4242 ret = 0;
4243
4244out:
4245 return ret;
4246}
4247
4248struct find_ref_ctx {
4249 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004250 u64 dir_gen;
4251 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004252 struct fs_path *name;
4253 int found_idx;
4254};
4255
4256static int __find_iref(int num, u64 dir, int index,
4257 struct fs_path *name,
4258 void *ctx_)
4259{
4260 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004261 u64 dir_gen;
4262 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004263
4264 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4265 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004266 /*
4267 * To avoid doing extra lookups we'll only do this if everything
4268 * else matches.
4269 */
4270 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4271 NULL, NULL, NULL);
4272 if (ret)
4273 return ret;
4274 if (dir_gen != ctx->dir_gen)
4275 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004276 ctx->found_idx = num;
4277 return 1;
4278 }
4279 return 0;
4280}
4281
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004282static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004283 struct btrfs_path *path,
4284 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004285 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004286{
4287 int ret;
4288 struct find_ref_ctx ctx;
4289
4290 ctx.dir = dir;
4291 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004292 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004293 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004294 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004295
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004296 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004297 if (ret < 0)
4298 return ret;
4299
4300 if (ctx.found_idx == -1)
4301 return -ENOENT;
4302
4303 return ctx.found_idx;
4304}
4305
4306static int __record_changed_new_ref(int num, u64 dir, int index,
4307 struct fs_path *name,
4308 void *ctx)
4309{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004310 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004311 int ret;
4312 struct send_ctx *sctx = ctx;
4313
Josef Bacikba5e8f22013-08-16 16:52:55 -04004314 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4315 NULL, NULL, NULL);
4316 if (ret)
4317 return ret;
4318
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004319 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004320 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004321 if (ret == -ENOENT)
4322 ret = __record_new_ref(num, dir, index, name, sctx);
4323 else if (ret > 0)
4324 ret = 0;
4325
4326 return ret;
4327}
4328
4329static int __record_changed_deleted_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->parent_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->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004343 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004344 if (ret == -ENOENT)
4345 ret = __record_deleted_ref(num, dir, index, name, sctx);
4346 else if (ret > 0)
4347 ret = 0;
4348
4349 return ret;
4350}
4351
4352static int record_changed_ref(struct send_ctx *sctx)
4353{
4354 int ret = 0;
4355
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004356 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004357 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4358 if (ret < 0)
4359 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004360 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004361 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4362 if (ret < 0)
4363 goto out;
4364 ret = 0;
4365
4366out:
4367 return ret;
4368}
4369
4370/*
4371 * Record and process all refs at once. Needed when an inode changes the
4372 * generation number, which means that it was deleted and recreated.
4373 */
4374static int process_all_refs(struct send_ctx *sctx,
4375 enum btrfs_compare_tree_result cmd)
4376{
4377 int ret;
4378 struct btrfs_root *root;
4379 struct btrfs_path *path;
4380 struct btrfs_key key;
4381 struct btrfs_key found_key;
4382 struct extent_buffer *eb;
4383 int slot;
4384 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004385 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004386
4387 path = alloc_path_for_send();
4388 if (!path)
4389 return -ENOMEM;
4390
4391 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4392 root = sctx->send_root;
4393 cb = __record_new_ref;
4394 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4395 root = sctx->parent_root;
4396 cb = __record_deleted_ref;
4397 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004398 btrfs_err(sctx->send_root->fs_info,
4399 "Wrong command %d in process_all_refs", cmd);
4400 ret = -EINVAL;
4401 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004402 }
4403
4404 key.objectid = sctx->cmp_key->objectid;
4405 key.type = BTRFS_INODE_REF_KEY;
4406 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004407 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4408 if (ret < 0)
4409 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004410
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004411 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004412 eb = path->nodes[0];
4413 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004414 if (slot >= btrfs_header_nritems(eb)) {
4415 ret = btrfs_next_leaf(root, path);
4416 if (ret < 0)
4417 goto out;
4418 else if (ret > 0)
4419 break;
4420 continue;
4421 }
4422
Alexander Block31db9f72012-07-25 23:19:24 +02004423 btrfs_item_key_to_cpu(eb, &found_key, slot);
4424
4425 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004426 (found_key.type != BTRFS_INODE_REF_KEY &&
4427 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004428 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004429
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004430 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004431 if (ret < 0)
4432 goto out;
4433
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004434 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004435 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004436 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004437
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004438 /*
4439 * We don't actually care about pending_move as we are simply
4440 * re-creating this inode and will be rename'ing it into place once we
4441 * rename the parent directory.
4442 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004443 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004444out:
4445 btrfs_free_path(path);
4446 return ret;
4447}
4448
4449static int send_set_xattr(struct send_ctx *sctx,
4450 struct fs_path *path,
4451 const char *name, int name_len,
4452 const char *data, int data_len)
4453{
4454 int ret = 0;
4455
4456 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4457 if (ret < 0)
4458 goto out;
4459
4460 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4461 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4462 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4463
4464 ret = send_cmd(sctx);
4465
4466tlv_put_failure:
4467out:
4468 return ret;
4469}
4470
4471static int send_remove_xattr(struct send_ctx *sctx,
4472 struct fs_path *path,
4473 const char *name, int name_len)
4474{
4475 int ret = 0;
4476
4477 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4478 if (ret < 0)
4479 goto out;
4480
4481 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4482 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4483
4484 ret = send_cmd(sctx);
4485
4486tlv_put_failure:
4487out:
4488 return ret;
4489}
4490
4491static int __process_new_xattr(int num, struct btrfs_key *di_key,
4492 const char *name, int name_len,
4493 const char *data, int data_len,
4494 u8 type, void *ctx)
4495{
4496 int ret;
4497 struct send_ctx *sctx = ctx;
4498 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004499 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004500
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004501 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004502 if (!p)
4503 return -ENOMEM;
4504
4505 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004506 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004507 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004508 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004509 * only contains the version number and no entries.
4510 */
4511 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4512 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4513 if (data_len == 0) {
4514 dummy_acl.a_version =
4515 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4516 data = (char *)&dummy_acl;
4517 data_len = sizeof(dummy_acl);
4518 }
4519 }
4520
4521 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4522 if (ret < 0)
4523 goto out;
4524
4525 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4526
4527out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004528 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004529 return ret;
4530}
4531
4532static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4533 const char *name, int name_len,
4534 const char *data, int data_len,
4535 u8 type, void *ctx)
4536{
4537 int ret;
4538 struct send_ctx *sctx = ctx;
4539 struct fs_path *p;
4540
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004541 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004542 if (!p)
4543 return -ENOMEM;
4544
4545 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4546 if (ret < 0)
4547 goto out;
4548
4549 ret = send_remove_xattr(sctx, p, name, name_len);
4550
4551out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004552 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004553 return ret;
4554}
4555
4556static int process_new_xattr(struct send_ctx *sctx)
4557{
4558 int ret = 0;
4559
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004560 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004561 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004562
4563 return ret;
4564}
4565
4566static int process_deleted_xattr(struct send_ctx *sctx)
4567{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004568 return iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004569 __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004570}
4571
4572struct find_xattr_ctx {
4573 const char *name;
4574 int name_len;
4575 int found_idx;
4576 char *found_data;
4577 int found_data_len;
4578};
4579
4580static int __find_xattr(int num, struct btrfs_key *di_key,
4581 const char *name, int name_len,
4582 const char *data, int data_len,
4583 u8 type, void *vctx)
4584{
4585 struct find_xattr_ctx *ctx = vctx;
4586
4587 if (name_len == ctx->name_len &&
4588 strncmp(name, ctx->name, name_len) == 0) {
4589 ctx->found_idx = num;
4590 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004591 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004592 if (!ctx->found_data)
4593 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004594 return 1;
4595 }
4596 return 0;
4597}
4598
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004599static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004600 struct btrfs_path *path,
4601 struct btrfs_key *key,
4602 const char *name, int name_len,
4603 char **data, int *data_len)
4604{
4605 int ret;
4606 struct find_xattr_ctx ctx;
4607
4608 ctx.name = name;
4609 ctx.name_len = name_len;
4610 ctx.found_idx = -1;
4611 ctx.found_data = NULL;
4612 ctx.found_data_len = 0;
4613
Nikolay Borisova0357512017-08-21 12:43:44 +03004614 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004615 if (ret < 0)
4616 return ret;
4617
4618 if (ctx.found_idx == -1)
4619 return -ENOENT;
4620 if (data) {
4621 *data = ctx.found_data;
4622 *data_len = ctx.found_data_len;
4623 } else {
4624 kfree(ctx.found_data);
4625 }
4626 return ctx.found_idx;
4627}
4628
4629
4630static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4631 const char *name, int name_len,
4632 const char *data, int data_len,
4633 u8 type, void *ctx)
4634{
4635 int ret;
4636 struct send_ctx *sctx = ctx;
4637 char *found_data = NULL;
4638 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004639
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004640 ret = find_xattr(sctx->parent_root, sctx->right_path,
4641 sctx->cmp_key, name, name_len, &found_data,
4642 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004643 if (ret == -ENOENT) {
4644 ret = __process_new_xattr(num, di_key, name, name_len, data,
4645 data_len, type, ctx);
4646 } else if (ret >= 0) {
4647 if (data_len != found_data_len ||
4648 memcmp(data, found_data, data_len)) {
4649 ret = __process_new_xattr(num, di_key, name, name_len,
4650 data, data_len, type, ctx);
4651 } else {
4652 ret = 0;
4653 }
4654 }
4655
4656 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004657 return ret;
4658}
4659
4660static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4661 const char *name, int name_len,
4662 const char *data, int data_len,
4663 u8 type, void *ctx)
4664{
4665 int ret;
4666 struct send_ctx *sctx = ctx;
4667
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004668 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4669 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004670 if (ret == -ENOENT)
4671 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4672 data_len, type, ctx);
4673 else if (ret >= 0)
4674 ret = 0;
4675
4676 return ret;
4677}
4678
4679static int process_changed_xattr(struct send_ctx *sctx)
4680{
4681 int ret = 0;
4682
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004683 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004684 __process_changed_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004685 if (ret < 0)
4686 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004687 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004688 __process_changed_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004689
4690out:
4691 return ret;
4692}
4693
4694static int process_all_new_xattrs(struct send_ctx *sctx)
4695{
4696 int ret;
4697 struct btrfs_root *root;
4698 struct btrfs_path *path;
4699 struct btrfs_key key;
4700 struct btrfs_key found_key;
4701 struct extent_buffer *eb;
4702 int slot;
4703
4704 path = alloc_path_for_send();
4705 if (!path)
4706 return -ENOMEM;
4707
4708 root = sctx->send_root;
4709
4710 key.objectid = sctx->cmp_key->objectid;
4711 key.type = BTRFS_XATTR_ITEM_KEY;
4712 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004713 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4714 if (ret < 0)
4715 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004716
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004717 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004718 eb = path->nodes[0];
4719 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004720 if (slot >= btrfs_header_nritems(eb)) {
4721 ret = btrfs_next_leaf(root, path);
4722 if (ret < 0) {
4723 goto out;
4724 } else if (ret > 0) {
4725 ret = 0;
4726 break;
4727 }
4728 continue;
4729 }
Alexander Block31db9f72012-07-25 23:19:24 +02004730
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004731 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004732 if (found_key.objectid != key.objectid ||
4733 found_key.type != key.type) {
4734 ret = 0;
4735 goto out;
4736 }
4737
Nikolay Borisova0357512017-08-21 12:43:44 +03004738 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004739 if (ret < 0)
4740 goto out;
4741
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004742 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004743 }
4744
4745out:
4746 btrfs_free_path(path);
4747 return ret;
4748}
4749
Josef Baciked259092013-10-25 11:36:01 -04004750static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4751{
4752 struct btrfs_root *root = sctx->send_root;
4753 struct btrfs_fs_info *fs_info = root->fs_info;
4754 struct inode *inode;
4755 struct page *page;
4756 char *addr;
4757 struct btrfs_key key;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004758 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004759 pgoff_t last_index;
Johannes Thumshirn70730172018-12-05 15:23:03 +01004760 unsigned pg_offset = offset_in_page(offset);
Josef Baciked259092013-10-25 11:36:01 -04004761 ssize_t ret = 0;
4762
4763 key.objectid = sctx->cur_ino;
4764 key.type = BTRFS_INODE_ITEM_KEY;
4765 key.offset = 0;
4766
4767 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4768 if (IS_ERR(inode))
4769 return PTR_ERR(inode);
4770
4771 if (offset + len > i_size_read(inode)) {
4772 if (offset > i_size_read(inode))
4773 len = 0;
4774 else
4775 len = offset - i_size_read(inode);
4776 }
4777 if (len == 0)
4778 goto out;
4779
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004780 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004781
4782 /* initial readahead */
4783 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4784 file_ra_state_init(&sctx->ra, inode->i_mapping);
Liu Bo2131bcd2014-03-05 10:07:35 +08004785
Josef Baciked259092013-10-25 11:36:01 -04004786 while (index <= last_index) {
4787 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004788 PAGE_SIZE - pg_offset);
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004789
4790 page = find_lock_page(inode->i_mapping, index);
Josef Baciked259092013-10-25 11:36:01 -04004791 if (!page) {
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004792 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4793 NULL, index, last_index + 1 - index);
4794
4795 page = find_or_create_page(inode->i_mapping, index,
4796 GFP_KERNEL);
4797 if (!page) {
4798 ret = -ENOMEM;
4799 break;
4800 }
4801 }
4802
4803 if (PageReadahead(page)) {
4804 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4805 NULL, page, index, last_index + 1 - index);
Josef Baciked259092013-10-25 11:36:01 -04004806 }
4807
4808 if (!PageUptodate(page)) {
4809 btrfs_readpage(NULL, page);
4810 lock_page(page);
4811 if (!PageUptodate(page)) {
4812 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004813 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004814 ret = -EIO;
4815 break;
4816 }
4817 }
4818
4819 addr = kmap(page);
4820 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4821 kunmap(page);
4822 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004823 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004824 index++;
4825 pg_offset = 0;
4826 len -= cur_len;
4827 ret += cur_len;
4828 }
4829out:
4830 iput(inode);
4831 return ret;
4832}
4833
Alexander Block31db9f72012-07-25 23:19:24 +02004834/*
4835 * Read some bytes from the current inode/file and send a write command to
4836 * user space.
4837 */
4838static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4839{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004840 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02004841 int ret = 0;
4842 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004843 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004844
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004845 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004846 if (!p)
4847 return -ENOMEM;
4848
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004849 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004850
Josef Baciked259092013-10-25 11:36:01 -04004851 num_read = fill_read_buf(sctx, offset, len);
4852 if (num_read <= 0) {
4853 if (num_read < 0)
4854 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004855 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004856 }
Alexander Block31db9f72012-07-25 23:19:24 +02004857
4858 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4859 if (ret < 0)
4860 goto out;
4861
4862 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4863 if (ret < 0)
4864 goto out;
4865
4866 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4867 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004868 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004869
4870 ret = send_cmd(sctx);
4871
4872tlv_put_failure:
4873out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004874 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004875 if (ret < 0)
4876 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004877 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004878}
4879
4880/*
4881 * Send a clone command to user space.
4882 */
4883static int send_clone(struct send_ctx *sctx,
4884 u64 offset, u32 len,
4885 struct clone_root *clone_root)
4886{
4887 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004888 struct fs_path *p;
4889 u64 gen;
4890
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004891 btrfs_debug(sctx->send_root->fs_info,
4892 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09004893 offset, len, clone_root->root->root_key.objectid,
4894 clone_root->ino, clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02004895
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004896 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004897 if (!p)
4898 return -ENOMEM;
4899
4900 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4901 if (ret < 0)
4902 goto out;
4903
4904 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4905 if (ret < 0)
4906 goto out;
4907
4908 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4909 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4910 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4911
Alexander Blocke938c8a2012-07-28 16:33:49 +02004912 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004913 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004914 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004915 if (ret < 0)
4916 goto out;
4917 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4918 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004919 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004920 }
4921 if (ret < 0)
4922 goto out;
4923
Josef Bacik37b8d272015-06-04 17:17:25 -04004924 /*
4925 * If the parent we're using has a received_uuid set then use that as
4926 * our clone source as that is what we will look for when doing a
4927 * receive.
4928 *
4929 * This covers the case that we create a snapshot off of a received
4930 * subvolume and then use that as the parent and try to receive on a
4931 * different host.
4932 */
4933 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4934 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4935 clone_root->root->root_item.received_uuid);
4936 else
4937 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4938 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004939 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004940 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004941 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4942 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4943 clone_root->offset);
4944
4945 ret = send_cmd(sctx);
4946
4947tlv_put_failure:
4948out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004949 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004950 return ret;
4951}
4952
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004953/*
4954 * Send an update extent command to user space.
4955 */
4956static int send_update_extent(struct send_ctx *sctx,
4957 u64 offset, u32 len)
4958{
4959 int ret = 0;
4960 struct fs_path *p;
4961
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004962 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004963 if (!p)
4964 return -ENOMEM;
4965
4966 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4967 if (ret < 0)
4968 goto out;
4969
4970 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4971 if (ret < 0)
4972 goto out;
4973
4974 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4975 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4976 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4977
4978 ret = send_cmd(sctx);
4979
4980tlv_put_failure:
4981out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004982 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004983 return ret;
4984}
4985
Josef Bacik16e75492013-10-22 12:18:51 -04004986static int send_hole(struct send_ctx *sctx, u64 end)
4987{
4988 struct fs_path *p = NULL;
4989 u64 offset = sctx->cur_inode_last_extent;
4990 u64 len;
4991 int ret = 0;
4992
Filipe Manana22d31512018-07-30 12:39:58 +01004993 /*
4994 * A hole that starts at EOF or beyond it. Since we do not yet support
4995 * fallocate (for extent preallocation and hole punching), sending a
4996 * write of zeroes starting at EOF or beyond would later require issuing
4997 * a truncate operation which would undo the write and achieve nothing.
4998 */
4999 if (offset >= sctx->cur_inode_size)
5000 return 0;
5001
Filipe Manana6b1f72e52019-05-20 09:55:42 +01005002 /*
5003 * Don't go beyond the inode's i_size due to prealloc extents that start
5004 * after the i_size.
5005 */
5006 end = min_t(u64, end, sctx->cur_inode_size);
5007
Filipe Mananad4dfc0f2018-02-06 20:39:20 +00005008 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5009 return send_update_extent(sctx, offset, end - offset);
5010
Josef Bacik16e75492013-10-22 12:18:51 -04005011 p = fs_path_alloc();
5012 if (!p)
5013 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01005014 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5015 if (ret < 0)
5016 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04005017 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
5018 while (offset < end) {
5019 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
5020
5021 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5022 if (ret < 0)
5023 break;
Josef Bacik16e75492013-10-22 12:18:51 -04005024 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5025 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5026 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
5027 ret = send_cmd(sctx);
5028 if (ret < 0)
5029 break;
5030 offset += len;
5031 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005032 sctx->cur_inode_next_write_offset = offset;
Josef Bacik16e75492013-10-22 12:18:51 -04005033tlv_put_failure:
5034 fs_path_free(p);
5035 return ret;
5036}
5037
Filipe Mananad906d492015-10-02 10:47:34 +01005038static int send_extent_data(struct send_ctx *sctx,
5039 const u64 offset,
5040 const u64 len)
5041{
5042 u64 sent = 0;
5043
5044 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5045 return send_update_extent(sctx, offset, len);
5046
5047 while (sent < len) {
5048 u64 size = len - sent;
5049 int ret;
5050
5051 if (size > BTRFS_SEND_READ_SIZE)
5052 size = BTRFS_SEND_READ_SIZE;
5053 ret = send_write(sctx, offset + sent, size);
5054 if (ret < 0)
5055 return ret;
5056 if (!ret)
5057 break;
5058 sent += ret;
5059 }
5060 return 0;
5061}
5062
5063static int clone_range(struct send_ctx *sctx,
5064 struct clone_root *clone_root,
5065 const u64 disk_byte,
5066 u64 data_offset,
5067 u64 offset,
5068 u64 len)
5069{
5070 struct btrfs_path *path;
5071 struct btrfs_key key;
5072 int ret;
Robbie Ko040ee6122019-03-29 18:03:27 +08005073 u64 clone_src_i_size;
Filipe Mananad906d492015-10-02 10:47:34 +01005074
Filipe Manana72610b12017-08-10 22:54:51 +01005075 /*
5076 * Prevent cloning from a zero offset with a length matching the sector
5077 * size because in some scenarios this will make the receiver fail.
5078 *
5079 * For example, if in the source filesystem the extent at offset 0
5080 * has a length of sectorsize and it was written using direct IO, then
5081 * it can never be an inline extent (even if compression is enabled).
5082 * Then this extent can be cloned in the original filesystem to a non
5083 * zero file offset, but it may not be possible to clone in the
5084 * destination filesystem because it can be inlined due to compression
5085 * on the destination filesystem (as the receiver's write operations are
5086 * always done using buffered IO). The same happens when the original
5087 * filesystem does not have compression enabled but the destination
5088 * filesystem has.
5089 */
5090 if (clone_root->offset == 0 &&
5091 len == sctx->send_root->fs_info->sectorsize)
5092 return send_extent_data(sctx, offset, len);
5093
Filipe Mananad906d492015-10-02 10:47:34 +01005094 path = alloc_path_for_send();
5095 if (!path)
5096 return -ENOMEM;
5097
5098 /*
Robbie Ko040ee6122019-03-29 18:03:27 +08005099 * There are inodes that have extents that lie behind its i_size. Don't
5100 * accept clones from these extents.
5101 */
5102 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5103 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL);
5104 btrfs_release_path(path);
5105 if (ret < 0)
5106 goto out;
5107
5108 /*
Filipe Mananad906d492015-10-02 10:47:34 +01005109 * We can't send a clone operation for the entire range if we find
5110 * extent items in the respective range in the source file that
5111 * refer to different extents or if we find holes.
5112 * So check for that and do a mix of clone and regular write/copy
5113 * operations if needed.
5114 *
5115 * Example:
5116 *
5117 * mkfs.btrfs -f /dev/sda
5118 * mount /dev/sda /mnt
5119 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5120 * cp --reflink=always /mnt/foo /mnt/bar
5121 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5122 * btrfs subvolume snapshot -r /mnt /mnt/snap
5123 *
5124 * If when we send the snapshot and we are processing file bar (which
5125 * has a higher inode number than foo) we blindly send a clone operation
5126 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5127 * a file bar that matches the content of file foo - iow, doesn't match
5128 * the content from bar in the original filesystem.
5129 */
5130 key.objectid = clone_root->ino;
5131 key.type = BTRFS_EXTENT_DATA_KEY;
5132 key.offset = clone_root->offset;
5133 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5134 if (ret < 0)
5135 goto out;
5136 if (ret > 0 && path->slots[0] > 0) {
5137 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5138 if (key.objectid == clone_root->ino &&
5139 key.type == BTRFS_EXTENT_DATA_KEY)
5140 path->slots[0]--;
5141 }
5142
5143 while (true) {
5144 struct extent_buffer *leaf = path->nodes[0];
5145 int slot = path->slots[0];
5146 struct btrfs_file_extent_item *ei;
5147 u8 type;
5148 u64 ext_len;
5149 u64 clone_len;
Robbie Ko040ee6122019-03-29 18:03:27 +08005150 u64 clone_data_offset;
Filipe Mananad906d492015-10-02 10:47:34 +01005151
5152 if (slot >= btrfs_header_nritems(leaf)) {
5153 ret = btrfs_next_leaf(clone_root->root, path);
5154 if (ret < 0)
5155 goto out;
5156 else if (ret > 0)
5157 break;
5158 continue;
5159 }
5160
5161 btrfs_item_key_to_cpu(leaf, &key, slot);
5162
5163 /*
5164 * We might have an implicit trailing hole (NO_HOLES feature
5165 * enabled). We deal with it after leaving this loop.
5166 */
5167 if (key.objectid != clone_root->ino ||
5168 key.type != BTRFS_EXTENT_DATA_KEY)
5169 break;
5170
5171 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5172 type = btrfs_file_extent_type(leaf, ei);
5173 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005174 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005175 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01005176 } else {
5177 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5178 }
5179
5180 if (key.offset + ext_len <= clone_root->offset)
5181 goto next;
5182
5183 if (key.offset > clone_root->offset) {
5184 /* Implicit hole, NO_HOLES feature enabled. */
5185 u64 hole_len = key.offset - clone_root->offset;
5186
5187 if (hole_len > len)
5188 hole_len = len;
5189 ret = send_extent_data(sctx, offset, hole_len);
5190 if (ret < 0)
5191 goto out;
5192
5193 len -= hole_len;
5194 if (len == 0)
5195 break;
5196 offset += hole_len;
5197 clone_root->offset += hole_len;
5198 data_offset += hole_len;
5199 }
5200
5201 if (key.offset >= clone_root->offset + len)
5202 break;
5203
Robbie Ko040ee6122019-03-29 18:03:27 +08005204 if (key.offset >= clone_src_i_size)
5205 break;
5206
5207 if (key.offset + ext_len > clone_src_i_size)
5208 ext_len = clone_src_i_size - key.offset;
5209
5210 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5211 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5212 clone_root->offset = key.offset;
5213 if (clone_data_offset < data_offset &&
5214 clone_data_offset + ext_len > data_offset) {
5215 u64 extent_offset;
5216
5217 extent_offset = data_offset - clone_data_offset;
5218 ext_len -= extent_offset;
5219 clone_data_offset += extent_offset;
5220 clone_root->offset += extent_offset;
5221 }
5222 }
5223
Filipe Mananad906d492015-10-02 10:47:34 +01005224 clone_len = min_t(u64, ext_len, len);
5225
5226 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
Filipe Manana3c850b42019-05-20 09:57:00 +01005227 clone_data_offset == data_offset) {
5228 const u64 src_end = clone_root->offset + clone_len;
5229 const u64 sectorsize = SZ_64K;
5230
5231 /*
5232 * We can't clone the last block, when its size is not
5233 * sector size aligned, into the middle of a file. If we
5234 * do so, the receiver will get a failure (-EINVAL) when
5235 * trying to clone or will silently corrupt the data in
5236 * the destination file if it's on a kernel without the
5237 * fix introduced by commit ac765f83f1397646
5238 * ("Btrfs: fix data corruption due to cloning of eof
5239 * block).
5240 *
5241 * So issue a clone of the aligned down range plus a
5242 * regular write for the eof block, if we hit that case.
5243 *
5244 * Also, we use the maximum possible sector size, 64K,
5245 * because we don't know what's the sector size of the
5246 * filesystem that receives the stream, so we have to
5247 * assume the largest possible sector size.
5248 */
5249 if (src_end == clone_src_i_size &&
5250 !IS_ALIGNED(src_end, sectorsize) &&
5251 offset + clone_len < sctx->cur_inode_size) {
5252 u64 slen;
5253
5254 slen = ALIGN_DOWN(src_end - clone_root->offset,
5255 sectorsize);
5256 if (slen > 0) {
5257 ret = send_clone(sctx, offset, slen,
5258 clone_root);
5259 if (ret < 0)
5260 goto out;
5261 }
5262 ret = send_extent_data(sctx, offset + slen,
5263 clone_len - slen);
5264 } else {
5265 ret = send_clone(sctx, offset, clone_len,
5266 clone_root);
5267 }
5268 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005269 ret = send_extent_data(sctx, offset, clone_len);
Filipe Manana3c850b42019-05-20 09:57:00 +01005270 }
Filipe Mananad906d492015-10-02 10:47:34 +01005271
5272 if (ret < 0)
5273 goto out;
5274
5275 len -= clone_len;
5276 if (len == 0)
5277 break;
5278 offset += clone_len;
5279 clone_root->offset += clone_len;
5280 data_offset += clone_len;
5281next:
5282 path->slots[0]++;
5283 }
5284
5285 if (len > 0)
5286 ret = send_extent_data(sctx, offset, len);
5287 else
5288 ret = 0;
5289out:
5290 btrfs_free_path(path);
5291 return ret;
5292}
5293
Alexander Block31db9f72012-07-25 23:19:24 +02005294static int send_write_or_clone(struct send_ctx *sctx,
5295 struct btrfs_path *path,
5296 struct btrfs_key *key,
5297 struct clone_root *clone_root)
5298{
5299 int ret = 0;
5300 struct btrfs_file_extent_item *ei;
5301 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02005302 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02005303 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005304 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005305
5306 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5307 struct btrfs_file_extent_item);
5308 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005309 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005310 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005311 /*
5312 * it is possible the inline item won't cover the whole page,
5313 * but there may be items after this page. Make
5314 * sure to send the whole thing
5315 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005316 len = PAGE_ALIGN(len);
Chris Mason74dd17f2012-08-07 16:25:13 -04005317 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02005318 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005319 }
Alexander Block31db9f72012-07-25 23:19:24 +02005320
Filipe Mananaa6aa10c72018-04-30 19:05:07 +01005321 if (offset >= sctx->cur_inode_size) {
5322 ret = 0;
5323 goto out;
5324 }
Alexander Block31db9f72012-07-25 23:19:24 +02005325 if (offset + len > sctx->cur_inode_size)
5326 len = sctx->cur_inode_size - offset;
5327 if (len == 0) {
5328 ret = 0;
5329 goto out;
5330 }
5331
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005332 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01005333 u64 disk_byte;
5334 u64 data_offset;
5335
5336 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5337 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5338 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5339 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005340 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005341 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005342 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005343 sctx->cur_inode_next_write_offset = offset + len;
Alexander Block31db9f72012-07-25 23:19:24 +02005344out:
5345 return ret;
5346}
5347
5348static int is_extent_unchanged(struct send_ctx *sctx,
5349 struct btrfs_path *left_path,
5350 struct btrfs_key *ekey)
5351{
5352 int ret = 0;
5353 struct btrfs_key key;
5354 struct btrfs_path *path = NULL;
5355 struct extent_buffer *eb;
5356 int slot;
5357 struct btrfs_key found_key;
5358 struct btrfs_file_extent_item *ei;
5359 u64 left_disknr;
5360 u64 right_disknr;
5361 u64 left_offset;
5362 u64 right_offset;
5363 u64 left_offset_fixed;
5364 u64 left_len;
5365 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005366 u64 left_gen;
5367 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005368 u8 left_type;
5369 u8 right_type;
5370
5371 path = alloc_path_for_send();
5372 if (!path)
5373 return -ENOMEM;
5374
5375 eb = left_path->nodes[0];
5376 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005377 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5378 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005379
5380 if (left_type != BTRFS_FILE_EXTENT_REG) {
5381 ret = 0;
5382 goto out;
5383 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005384 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5385 left_len = btrfs_file_extent_num_bytes(eb, ei);
5386 left_offset = btrfs_file_extent_offset(eb, ei);
5387 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005388
5389 /*
5390 * Following comments will refer to these graphics. L is the left
5391 * extents which we are checking at the moment. 1-8 are the right
5392 * extents that we iterate.
5393 *
5394 * |-----L-----|
5395 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5396 *
5397 * |-----L-----|
5398 * |--1--|-2b-|...(same as above)
5399 *
5400 * Alternative situation. Happens on files where extents got split.
5401 * |-----L-----|
5402 * |-----------7-----------|-6-|
5403 *
5404 * Alternative situation. Happens on files which got larger.
5405 * |-----L-----|
5406 * |-8-|
5407 * Nothing follows after 8.
5408 */
5409
5410 key.objectid = ekey->objectid;
5411 key.type = BTRFS_EXTENT_DATA_KEY;
5412 key.offset = ekey->offset;
5413 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5414 if (ret < 0)
5415 goto out;
5416 if (ret) {
5417 ret = 0;
5418 goto out;
5419 }
5420
5421 /*
5422 * Handle special case where the right side has no extents at all.
5423 */
5424 eb = path->nodes[0];
5425 slot = path->slots[0];
5426 btrfs_item_key_to_cpu(eb, &found_key, slot);
5427 if (found_key.objectid != key.objectid ||
5428 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005429 /* If we're a hole then just pretend nothing changed */
5430 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005431 goto out;
5432 }
5433
5434 /*
5435 * We're now on 2a, 2b or 7.
5436 */
5437 key = found_key;
5438 while (key.offset < ekey->offset + left_len) {
5439 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5440 right_type = btrfs_file_extent_type(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005441 if (right_type != BTRFS_FILE_EXTENT_REG &&
5442 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005443 ret = 0;
5444 goto out;
5445 }
5446
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005447 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005448 right_len = btrfs_file_extent_ram_bytes(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005449 right_len = PAGE_ALIGN(right_len);
5450 } else {
5451 right_len = btrfs_file_extent_num_bytes(eb, ei);
5452 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005453
Alexander Block31db9f72012-07-25 23:19:24 +02005454 /*
5455 * Are we at extent 8? If yes, we know the extent is changed.
5456 * This may only happen on the first iteration.
5457 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005458 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005459 /* If we're a hole just pretend nothing changed */
5460 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005461 goto out;
5462 }
5463
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005464 /*
5465 * We just wanted to see if when we have an inline extent, what
5466 * follows it is a regular extent (wanted to check the above
5467 * condition for inline extents too). This should normally not
5468 * happen but it's possible for example when we have an inline
5469 * compressed extent representing data with a size matching
5470 * the page size (currently the same as sector size).
5471 */
5472 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5473 ret = 0;
5474 goto out;
5475 }
5476
Filipe Manana24e52b12017-07-06 15:31:46 +01005477 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5478 right_offset = btrfs_file_extent_offset(eb, ei);
5479 right_gen = btrfs_file_extent_generation(eb, ei);
5480
Alexander Block31db9f72012-07-25 23:19:24 +02005481 left_offset_fixed = left_offset;
5482 if (key.offset < ekey->offset) {
5483 /* Fix the right offset for 2a and 7. */
5484 right_offset += ekey->offset - key.offset;
5485 } else {
5486 /* Fix the left offset for all behind 2a and 2b */
5487 left_offset_fixed += key.offset - ekey->offset;
5488 }
5489
5490 /*
5491 * Check if we have the same extent.
5492 */
Alexander Block39540962012-08-01 12:46:05 +02005493 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005494 left_offset_fixed != right_offset ||
5495 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005496 ret = 0;
5497 goto out;
5498 }
5499
5500 /*
5501 * Go to the next extent.
5502 */
5503 ret = btrfs_next_item(sctx->parent_root, path);
5504 if (ret < 0)
5505 goto out;
5506 if (!ret) {
5507 eb = path->nodes[0];
5508 slot = path->slots[0];
5509 btrfs_item_key_to_cpu(eb, &found_key, slot);
5510 }
5511 if (ret || found_key.objectid != key.objectid ||
5512 found_key.type != key.type) {
5513 key.offset += right_len;
5514 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005515 }
5516 if (found_key.offset != key.offset + right_len) {
5517 ret = 0;
5518 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005519 }
5520 key = found_key;
5521 }
5522
5523 /*
5524 * We're now behind the left extent (treat as unchanged) or at the end
5525 * of the right side (treat as changed).
5526 */
5527 if (key.offset >= ekey->offset + left_len)
5528 ret = 1;
5529 else
5530 ret = 0;
5531
5532
5533out:
5534 btrfs_free_path(path);
5535 return ret;
5536}
5537
Josef Bacik16e75492013-10-22 12:18:51 -04005538static int get_last_extent(struct send_ctx *sctx, u64 offset)
5539{
5540 struct btrfs_path *path;
5541 struct btrfs_root *root = sctx->send_root;
5542 struct btrfs_file_extent_item *fi;
5543 struct btrfs_key key;
5544 u64 extent_end;
5545 u8 type;
5546 int ret;
5547
5548 path = alloc_path_for_send();
5549 if (!path)
5550 return -ENOMEM;
5551
5552 sctx->cur_inode_last_extent = 0;
5553
5554 key.objectid = sctx->cur_ino;
5555 key.type = BTRFS_EXTENT_DATA_KEY;
5556 key.offset = offset;
5557 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5558 if (ret < 0)
5559 goto out;
5560 ret = 0;
5561 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5562 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5563 goto out;
5564
5565 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5566 struct btrfs_file_extent_item);
5567 type = btrfs_file_extent_type(path->nodes[0], fi);
5568 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005569 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005570 extent_end = ALIGN(key.offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005571 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005572 } else {
5573 extent_end = key.offset +
5574 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5575 }
5576 sctx->cur_inode_last_extent = extent_end;
5577out:
5578 btrfs_free_path(path);
5579 return ret;
5580}
5581
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005582static int range_is_hole_in_parent(struct send_ctx *sctx,
5583 const u64 start,
5584 const u64 end)
5585{
5586 struct btrfs_path *path;
5587 struct btrfs_key key;
5588 struct btrfs_root *root = sctx->parent_root;
5589 u64 search_start = start;
5590 int ret;
5591
5592 path = alloc_path_for_send();
5593 if (!path)
5594 return -ENOMEM;
5595
5596 key.objectid = sctx->cur_ino;
5597 key.type = BTRFS_EXTENT_DATA_KEY;
5598 key.offset = search_start;
5599 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5600 if (ret < 0)
5601 goto out;
5602 if (ret > 0 && path->slots[0] > 0)
5603 path->slots[0]--;
5604
5605 while (search_start < end) {
5606 struct extent_buffer *leaf = path->nodes[0];
5607 int slot = path->slots[0];
5608 struct btrfs_file_extent_item *fi;
5609 u64 extent_end;
5610
5611 if (slot >= btrfs_header_nritems(leaf)) {
5612 ret = btrfs_next_leaf(root, path);
5613 if (ret < 0)
5614 goto out;
5615 else if (ret > 0)
5616 break;
5617 continue;
5618 }
5619
5620 btrfs_item_key_to_cpu(leaf, &key, slot);
5621 if (key.objectid < sctx->cur_ino ||
5622 key.type < BTRFS_EXTENT_DATA_KEY)
5623 goto next;
5624 if (key.objectid > sctx->cur_ino ||
5625 key.type > BTRFS_EXTENT_DATA_KEY ||
5626 key.offset >= end)
5627 break;
5628
5629 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5630 if (btrfs_file_extent_type(leaf, fi) ==
5631 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005632 u64 size = btrfs_file_extent_ram_bytes(leaf, fi);
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005633
5634 extent_end = ALIGN(key.offset + size,
5635 root->fs_info->sectorsize);
5636 } else {
5637 extent_end = key.offset +
5638 btrfs_file_extent_num_bytes(leaf, fi);
5639 }
5640 if (extent_end <= start)
5641 goto next;
5642 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5643 search_start = extent_end;
5644 goto next;
5645 }
5646 ret = 0;
5647 goto out;
5648next:
5649 path->slots[0]++;
5650 }
5651 ret = 1;
5652out:
5653 btrfs_free_path(path);
5654 return ret;
5655}
5656
Josef Bacik16e75492013-10-22 12:18:51 -04005657static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5658 struct btrfs_key *key)
5659{
5660 struct btrfs_file_extent_item *fi;
5661 u64 extent_end;
5662 u8 type;
5663 int ret = 0;
5664
5665 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5666 return 0;
5667
5668 if (sctx->cur_inode_last_extent == (u64)-1) {
5669 ret = get_last_extent(sctx, key->offset - 1);
5670 if (ret)
5671 return ret;
5672 }
5673
5674 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5675 struct btrfs_file_extent_item);
5676 type = btrfs_file_extent_type(path->nodes[0], fi);
5677 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005678 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005679 extent_end = ALIGN(key->offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005680 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005681 } else {
5682 extent_end = key->offset +
5683 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5684 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005685
5686 if (path->slots[0] == 0 &&
5687 sctx->cur_inode_last_extent < key->offset) {
5688 /*
5689 * We might have skipped entire leafs that contained only
5690 * file extent items for our current inode. These leafs have
5691 * a generation number smaller (older) than the one in the
5692 * current leaf and the leaf our last extent came from, and
5693 * are located between these 2 leafs.
5694 */
5695 ret = get_last_extent(sctx, key->offset - 1);
5696 if (ret)
5697 return ret;
5698 }
5699
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005700 if (sctx->cur_inode_last_extent < key->offset) {
5701 ret = range_is_hole_in_parent(sctx,
5702 sctx->cur_inode_last_extent,
5703 key->offset);
5704 if (ret < 0)
5705 return ret;
5706 else if (ret == 0)
5707 ret = send_hole(sctx, key->offset);
5708 else
5709 ret = 0;
5710 }
Josef Bacik16e75492013-10-22 12:18:51 -04005711 sctx->cur_inode_last_extent = extent_end;
5712 return ret;
5713}
5714
Alexander Block31db9f72012-07-25 23:19:24 +02005715static int process_extent(struct send_ctx *sctx,
5716 struct btrfs_path *path,
5717 struct btrfs_key *key)
5718{
Alexander Block31db9f72012-07-25 23:19:24 +02005719 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005720 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005721
5722 if (S_ISLNK(sctx->cur_inode_mode))
5723 return 0;
5724
5725 if (sctx->parent_root && !sctx->cur_inode_new) {
5726 ret = is_extent_unchanged(sctx, path, key);
5727 if (ret < 0)
5728 goto out;
5729 if (ret) {
5730 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005731 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005732 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005733 } else {
5734 struct btrfs_file_extent_item *ei;
5735 u8 type;
5736
5737 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5738 struct btrfs_file_extent_item);
5739 type = btrfs_file_extent_type(path->nodes[0], ei);
5740 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5741 type == BTRFS_FILE_EXTENT_REG) {
5742 /*
5743 * The send spec does not have a prealloc command yet,
5744 * so just leave a hole for prealloc'ed extents until
5745 * we have enough commands queued up to justify rev'ing
5746 * the send spec.
5747 */
5748 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5749 ret = 0;
5750 goto out;
5751 }
5752
5753 /* Have a hole, just skip it. */
5754 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5755 ret = 0;
5756 goto out;
5757 }
5758 }
Alexander Block31db9f72012-07-25 23:19:24 +02005759 }
5760
5761 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5762 sctx->cur_inode_size, &found_clone);
5763 if (ret != -ENOENT && ret < 0)
5764 goto out;
5765
5766 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005767 if (ret)
5768 goto out;
5769out_hole:
5770 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005771out:
5772 return ret;
5773}
5774
5775static int process_all_extents(struct send_ctx *sctx)
5776{
5777 int ret;
5778 struct btrfs_root *root;
5779 struct btrfs_path *path;
5780 struct btrfs_key key;
5781 struct btrfs_key found_key;
5782 struct extent_buffer *eb;
5783 int slot;
5784
5785 root = sctx->send_root;
5786 path = alloc_path_for_send();
5787 if (!path)
5788 return -ENOMEM;
5789
5790 key.objectid = sctx->cmp_key->objectid;
5791 key.type = BTRFS_EXTENT_DATA_KEY;
5792 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005793 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5794 if (ret < 0)
5795 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005796
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005797 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005798 eb = path->nodes[0];
5799 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005800
5801 if (slot >= btrfs_header_nritems(eb)) {
5802 ret = btrfs_next_leaf(root, path);
5803 if (ret < 0) {
5804 goto out;
5805 } else if (ret > 0) {
5806 ret = 0;
5807 break;
5808 }
5809 continue;
5810 }
5811
Alexander Block31db9f72012-07-25 23:19:24 +02005812 btrfs_item_key_to_cpu(eb, &found_key, slot);
5813
5814 if (found_key.objectid != key.objectid ||
5815 found_key.type != key.type) {
5816 ret = 0;
5817 goto out;
5818 }
5819
5820 ret = process_extent(sctx, path, &found_key);
5821 if (ret < 0)
5822 goto out;
5823
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005824 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005825 }
5826
5827out:
5828 btrfs_free_path(path);
5829 return ret;
5830}
5831
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005832static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5833 int *pending_move,
5834 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005835{
5836 int ret = 0;
5837
5838 if (sctx->cur_ino == 0)
5839 goto out;
5840 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005841 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005842 goto out;
5843 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5844 goto out;
5845
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005846 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005847 if (ret < 0)
5848 goto out;
5849
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005850 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005851out:
5852 return ret;
5853}
5854
5855static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5856{
5857 int ret = 0;
5858 u64 left_mode;
5859 u64 left_uid;
5860 u64 left_gid;
5861 u64 right_mode;
5862 u64 right_uid;
5863 u64 right_gid;
5864 int need_chmod = 0;
5865 int need_chown = 0;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005866 int need_truncate = 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005867 int pending_move = 0;
5868 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005869
Filipe Manana46b2f452018-07-24 11:54:04 +01005870 if (sctx->ignore_cur_inode)
5871 return 0;
5872
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005873 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5874 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005875 if (ret < 0)
5876 goto out;
5877
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005878 /*
5879 * We have processed the refs and thus need to advance send_progress.
5880 * Now, calls to get_cur_xxx will take the updated refs of the current
5881 * inode into account.
5882 *
5883 * On the other hand, if our current inode is a directory and couldn't
5884 * be moved/renamed because its parent was renamed/moved too and it has
5885 * a higher inode number, we can only move/rename our current inode
5886 * after we moved/renamed its parent. Therefore in this case operate on
5887 * the old path (pre move/rename) of our current inode, and the
5888 * move/rename will be performed later.
5889 */
5890 if (refs_processed && !pending_move)
5891 sctx->send_progress = sctx->cur_ino + 1;
5892
Alexander Block31db9f72012-07-25 23:19:24 +02005893 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5894 goto out;
5895 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5896 goto out;
5897
5898 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005899 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005900 if (ret < 0)
5901 goto out;
5902
Alex Lyakase2d044f2012-10-17 13:52:47 +00005903 if (!sctx->parent_root || sctx->cur_inode_new) {
5904 need_chown = 1;
5905 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005906 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005907 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
5908 need_truncate = 0;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005909 } else {
Filipe Mananaffa7c422018-02-06 20:40:40 +00005910 u64 old_size;
5911
Alex Lyakase2d044f2012-10-17 13:52:47 +00005912 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
Filipe Mananaffa7c422018-02-06 20:40:40 +00005913 &old_size, NULL, &right_mode, &right_uid,
Alex Lyakase2d044f2012-10-17 13:52:47 +00005914 &right_gid, NULL);
5915 if (ret < 0)
5916 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005917
Alex Lyakase2d044f2012-10-17 13:52:47 +00005918 if (left_uid != right_uid || left_gid != right_gid)
5919 need_chown = 1;
5920 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5921 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005922 if ((old_size == sctx->cur_inode_size) ||
5923 (sctx->cur_inode_size > old_size &&
5924 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
5925 need_truncate = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005926 }
5927
5928 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005929 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005930 if (sctx->cur_inode_last_extent == (u64)-1 ||
5931 sctx->cur_inode_last_extent <
5932 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005933 ret = get_last_extent(sctx, (u64)-1);
5934 if (ret)
5935 goto out;
5936 }
5937 if (sctx->cur_inode_last_extent <
5938 sctx->cur_inode_size) {
5939 ret = send_hole(sctx, sctx->cur_inode_size);
5940 if (ret)
5941 goto out;
5942 }
5943 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005944 if (need_truncate) {
5945 ret = send_truncate(sctx, sctx->cur_ino,
5946 sctx->cur_inode_gen,
5947 sctx->cur_inode_size);
5948 if (ret < 0)
5949 goto out;
5950 }
Alexander Block31db9f72012-07-25 23:19:24 +02005951 }
5952
5953 if (need_chown) {
5954 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5955 left_uid, left_gid);
5956 if (ret < 0)
5957 goto out;
5958 }
5959 if (need_chmod) {
5960 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5961 left_mode);
5962 if (ret < 0)
5963 goto out;
5964 }
5965
5966 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005967 * If other directory inodes depended on our current directory
5968 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005969 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005970 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5971 ret = apply_children_dir_moves(sctx);
5972 if (ret)
5973 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005974 /*
5975 * Need to send that every time, no matter if it actually
5976 * changed between the two trees as we have done changes to
5977 * the inode before. If our inode is a directory and it's
5978 * waiting to be moved/renamed, we will send its utimes when
5979 * it's moved/renamed, therefore we don't need to do it here.
5980 */
5981 sctx->send_progress = sctx->cur_ino + 1;
5982 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5983 if (ret < 0)
5984 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005985 }
5986
Alexander Block31db9f72012-07-25 23:19:24 +02005987out:
5988 return ret;
5989}
5990
Filipe Manana46b2f452018-07-24 11:54:04 +01005991struct parent_paths_ctx {
5992 struct list_head *refs;
5993 struct send_ctx *sctx;
5994};
5995
5996static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
5997 void *ctx)
5998{
5999 struct parent_paths_ctx *ppctx = ctx;
6000
6001 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6002 ppctx->refs);
6003}
6004
6005/*
6006 * Issue unlink operations for all paths of the current inode found in the
6007 * parent snapshot.
6008 */
6009static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6010{
6011 LIST_HEAD(deleted_refs);
6012 struct btrfs_path *path;
6013 struct btrfs_key key;
6014 struct parent_paths_ctx ctx;
6015 int ret;
6016
6017 path = alloc_path_for_send();
6018 if (!path)
6019 return -ENOMEM;
6020
6021 key.objectid = sctx->cur_ino;
6022 key.type = BTRFS_INODE_REF_KEY;
6023 key.offset = 0;
6024 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
6025 if (ret < 0)
6026 goto out;
6027
6028 ctx.refs = &deleted_refs;
6029 ctx.sctx = sctx;
6030
6031 while (true) {
6032 struct extent_buffer *eb = path->nodes[0];
6033 int slot = path->slots[0];
6034
6035 if (slot >= btrfs_header_nritems(eb)) {
6036 ret = btrfs_next_leaf(sctx->parent_root, path);
6037 if (ret < 0)
6038 goto out;
6039 else if (ret > 0)
6040 break;
6041 continue;
6042 }
6043
6044 btrfs_item_key_to_cpu(eb, &key, slot);
6045 if (key.objectid != sctx->cur_ino)
6046 break;
6047 if (key.type != BTRFS_INODE_REF_KEY &&
6048 key.type != BTRFS_INODE_EXTREF_KEY)
6049 break;
6050
6051 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
6052 record_parent_ref, &ctx);
6053 if (ret < 0)
6054 goto out;
6055
6056 path->slots[0]++;
6057 }
6058
6059 while (!list_empty(&deleted_refs)) {
6060 struct recorded_ref *ref;
6061
6062 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6063 ret = send_unlink(sctx, ref->full_path);
6064 if (ret < 0)
6065 goto out;
6066 fs_path_free(ref->full_path);
6067 list_del(&ref->list);
6068 kfree(ref);
6069 }
6070 ret = 0;
6071out:
6072 btrfs_free_path(path);
6073 if (ret)
6074 __free_recorded_refs(&deleted_refs);
6075 return ret;
6076}
6077
Alexander Block31db9f72012-07-25 23:19:24 +02006078static int changed_inode(struct send_ctx *sctx,
6079 enum btrfs_compare_tree_result result)
6080{
6081 int ret = 0;
6082 struct btrfs_key *key = sctx->cmp_key;
6083 struct btrfs_inode_item *left_ii = NULL;
6084 struct btrfs_inode_item *right_ii = NULL;
6085 u64 left_gen = 0;
6086 u64 right_gen = 0;
6087
Alexander Block31db9f72012-07-25 23:19:24 +02006088 sctx->cur_ino = key->objectid;
6089 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006090 sctx->cur_inode_last_extent = (u64)-1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006091 sctx->cur_inode_next_write_offset = 0;
Filipe Manana46b2f452018-07-24 11:54:04 +01006092 sctx->ignore_cur_inode = false;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006093
6094 /*
6095 * Set send_progress to current inode. This will tell all get_cur_xxx
6096 * functions that the current inode's refs are not updated yet. Later,
6097 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6098 */
Alexander Block31db9f72012-07-25 23:19:24 +02006099 sctx->send_progress = sctx->cur_ino;
6100
6101 if (result == BTRFS_COMPARE_TREE_NEW ||
6102 result == BTRFS_COMPARE_TREE_CHANGED) {
6103 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6104 sctx->left_path->slots[0],
6105 struct btrfs_inode_item);
6106 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6107 left_ii);
6108 } else {
6109 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6110 sctx->right_path->slots[0],
6111 struct btrfs_inode_item);
6112 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6113 right_ii);
6114 }
6115 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6116 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6117 sctx->right_path->slots[0],
6118 struct btrfs_inode_item);
6119
6120 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6121 right_ii);
Alexander Block6d85ed052012-08-01 14:48:59 +02006122
6123 /*
6124 * The cur_ino = root dir case is special here. We can't treat
6125 * the inode as deleted+reused because it would generate a
6126 * stream that tries to delete/mkdir the root dir.
6127 */
6128 if (left_gen != right_gen &&
6129 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02006130 sctx->cur_inode_new_gen = 1;
6131 }
6132
Filipe Manana46b2f452018-07-24 11:54:04 +01006133 /*
6134 * Normally we do not find inodes with a link count of zero (orphans)
6135 * because the most common case is to create a snapshot and use it
6136 * for a send operation. However other less common use cases involve
6137 * using a subvolume and send it after turning it to RO mode just
6138 * after deleting all hard links of a file while holding an open
6139 * file descriptor against it or turning a RO snapshot into RW mode,
6140 * keep an open file descriptor against a file, delete it and then
6141 * turn the snapshot back to RO mode before using it for a send
6142 * operation. So if we find such cases, ignore the inode and all its
6143 * items completely if it's a new inode, or if it's a changed inode
6144 * make sure all its previous paths (from the parent snapshot) are all
6145 * unlinked and all other the inode items are ignored.
6146 */
6147 if (result == BTRFS_COMPARE_TREE_NEW ||
6148 result == BTRFS_COMPARE_TREE_CHANGED) {
6149 u32 nlinks;
6150
6151 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6152 if (nlinks == 0) {
6153 sctx->ignore_cur_inode = true;
6154 if (result == BTRFS_COMPARE_TREE_CHANGED)
6155 ret = btrfs_unlink_all_paths(sctx);
6156 goto out;
6157 }
6158 }
6159
Alexander Block31db9f72012-07-25 23:19:24 +02006160 if (result == BTRFS_COMPARE_TREE_NEW) {
6161 sctx->cur_inode_gen = left_gen;
6162 sctx->cur_inode_new = 1;
6163 sctx->cur_inode_deleted = 0;
6164 sctx->cur_inode_size = btrfs_inode_size(
6165 sctx->left_path->nodes[0], left_ii);
6166 sctx->cur_inode_mode = btrfs_inode_mode(
6167 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006168 sctx->cur_inode_rdev = btrfs_inode_rdev(
6169 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02006170 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02006171 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006172 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6173 sctx->cur_inode_gen = right_gen;
6174 sctx->cur_inode_new = 0;
6175 sctx->cur_inode_deleted = 1;
6176 sctx->cur_inode_size = btrfs_inode_size(
6177 sctx->right_path->nodes[0], right_ii);
6178 sctx->cur_inode_mode = btrfs_inode_mode(
6179 sctx->right_path->nodes[0], right_ii);
6180 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02006181 /*
6182 * We need to do some special handling in case the inode was
6183 * reported as changed with a changed generation number. This
6184 * means that the original inode was deleted and new inode
6185 * reused the same inum. So we have to treat the old inode as
6186 * deleted and the new one as new.
6187 */
Alexander Block31db9f72012-07-25 23:19:24 +02006188 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02006189 /*
6190 * First, process the inode as if it was deleted.
6191 */
Alexander Block31db9f72012-07-25 23:19:24 +02006192 sctx->cur_inode_gen = right_gen;
6193 sctx->cur_inode_new = 0;
6194 sctx->cur_inode_deleted = 1;
6195 sctx->cur_inode_size = btrfs_inode_size(
6196 sctx->right_path->nodes[0], right_ii);
6197 sctx->cur_inode_mode = btrfs_inode_mode(
6198 sctx->right_path->nodes[0], right_ii);
6199 ret = process_all_refs(sctx,
6200 BTRFS_COMPARE_TREE_DELETED);
6201 if (ret < 0)
6202 goto out;
6203
Alexander Block766702e2012-07-28 14:11:31 +02006204 /*
6205 * Now process the inode as if it was new.
6206 */
Alexander Block31db9f72012-07-25 23:19:24 +02006207 sctx->cur_inode_gen = left_gen;
6208 sctx->cur_inode_new = 1;
6209 sctx->cur_inode_deleted = 0;
6210 sctx->cur_inode_size = btrfs_inode_size(
6211 sctx->left_path->nodes[0], left_ii);
6212 sctx->cur_inode_mode = btrfs_inode_mode(
6213 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006214 sctx->cur_inode_rdev = btrfs_inode_rdev(
6215 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02006216 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006217 if (ret < 0)
6218 goto out;
6219
6220 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6221 if (ret < 0)
6222 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006223 /*
6224 * Advance send_progress now as we did not get into
6225 * process_recorded_refs_if_needed in the new_gen case.
6226 */
6227 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02006228
6229 /*
6230 * Now process all extents and xattrs of the inode as if
6231 * they were all new.
6232 */
Alexander Block31db9f72012-07-25 23:19:24 +02006233 ret = process_all_extents(sctx);
6234 if (ret < 0)
6235 goto out;
6236 ret = process_all_new_xattrs(sctx);
6237 if (ret < 0)
6238 goto out;
6239 } else {
6240 sctx->cur_inode_gen = left_gen;
6241 sctx->cur_inode_new = 0;
6242 sctx->cur_inode_new_gen = 0;
6243 sctx->cur_inode_deleted = 0;
6244 sctx->cur_inode_size = btrfs_inode_size(
6245 sctx->left_path->nodes[0], left_ii);
6246 sctx->cur_inode_mode = btrfs_inode_mode(
6247 sctx->left_path->nodes[0], left_ii);
6248 }
6249 }
6250
6251out:
6252 return ret;
6253}
6254
Alexander Block766702e2012-07-28 14:11:31 +02006255/*
6256 * We have to process new refs before deleted refs, but compare_trees gives us
6257 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6258 * first and later process them in process_recorded_refs.
6259 * For the cur_inode_new_gen case, we skip recording completely because
6260 * changed_inode did already initiate processing of refs. The reason for this is
6261 * that in this case, compare_tree actually compares the refs of 2 different
6262 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6263 * refs of the right tree as deleted and all refs of the left tree as new.
6264 */
Alexander Block31db9f72012-07-25 23:19:24 +02006265static int changed_ref(struct send_ctx *sctx,
6266 enum btrfs_compare_tree_result result)
6267{
6268 int ret = 0;
6269
Filipe Manana95155582016-08-01 01:50:37 +01006270 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6271 inconsistent_snapshot_error(sctx, result, "reference");
6272 return -EIO;
6273 }
Alexander Block31db9f72012-07-25 23:19:24 +02006274
6275 if (!sctx->cur_inode_new_gen &&
6276 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6277 if (result == BTRFS_COMPARE_TREE_NEW)
6278 ret = record_new_ref(sctx);
6279 else if (result == BTRFS_COMPARE_TREE_DELETED)
6280 ret = record_deleted_ref(sctx);
6281 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6282 ret = record_changed_ref(sctx);
6283 }
6284
6285 return ret;
6286}
6287
Alexander Block766702e2012-07-28 14:11:31 +02006288/*
6289 * Process new/deleted/changed xattrs. We skip processing in the
6290 * cur_inode_new_gen case because changed_inode did already initiate processing
6291 * of xattrs. The reason is the same as in changed_ref
6292 */
Alexander Block31db9f72012-07-25 23:19:24 +02006293static int changed_xattr(struct send_ctx *sctx,
6294 enum btrfs_compare_tree_result result)
6295{
6296 int ret = 0;
6297
Filipe Manana95155582016-08-01 01:50:37 +01006298 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6299 inconsistent_snapshot_error(sctx, result, "xattr");
6300 return -EIO;
6301 }
Alexander Block31db9f72012-07-25 23:19:24 +02006302
6303 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6304 if (result == BTRFS_COMPARE_TREE_NEW)
6305 ret = process_new_xattr(sctx);
6306 else if (result == BTRFS_COMPARE_TREE_DELETED)
6307 ret = process_deleted_xattr(sctx);
6308 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6309 ret = process_changed_xattr(sctx);
6310 }
6311
6312 return ret;
6313}
6314
Alexander Block766702e2012-07-28 14:11:31 +02006315/*
6316 * Process new/deleted/changed extents. We skip processing in the
6317 * cur_inode_new_gen case because changed_inode did already initiate processing
6318 * of extents. The reason is the same as in changed_ref
6319 */
Alexander Block31db9f72012-07-25 23:19:24 +02006320static int changed_extent(struct send_ctx *sctx,
6321 enum btrfs_compare_tree_result result)
6322{
6323 int ret = 0;
6324
Filipe Mananab4f9a1a2019-07-17 13:23:39 +01006325 /*
6326 * We have found an extent item that changed without the inode item
6327 * having changed. This can happen either after relocation (where the
6328 * disk_bytenr of an extent item is replaced at
6329 * relocation.c:replace_file_extents()) or after deduplication into a
6330 * file in both the parent and send snapshots (where an extent item can
6331 * get modified or replaced with a new one). Note that deduplication
6332 * updates the inode item, but it only changes the iversion (sequence
6333 * field in the inode item) of the inode, so if a file is deduplicated
6334 * the same amount of times in both the parent and send snapshots, its
6335 * iversion becames the same in both snapshots, whence the inode item is
6336 * the same on both snapshots.
6337 */
6338 if (sctx->cur_ino != sctx->cmp_key->objectid)
6339 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02006340
6341 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6342 if (result != BTRFS_COMPARE_TREE_DELETED)
6343 ret = process_extent(sctx, sctx->left_path,
6344 sctx->cmp_key);
6345 }
6346
6347 return ret;
6348}
6349
Josef Bacikba5e8f22013-08-16 16:52:55 -04006350static int dir_changed(struct send_ctx *sctx, u64 dir)
6351{
6352 u64 orig_gen, new_gen;
6353 int ret;
6354
6355 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6356 NULL, NULL);
6357 if (ret)
6358 return ret;
6359
6360 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6361 NULL, NULL, NULL);
6362 if (ret)
6363 return ret;
6364
6365 return (orig_gen != new_gen) ? 1 : 0;
6366}
6367
6368static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6369 struct btrfs_key *key)
6370{
6371 struct btrfs_inode_extref *extref;
6372 struct extent_buffer *leaf;
6373 u64 dirid = 0, last_dirid = 0;
6374 unsigned long ptr;
6375 u32 item_size;
6376 u32 cur_offset = 0;
6377 int ref_name_len;
6378 int ret = 0;
6379
6380 /* Easy case, just check this one dirid */
6381 if (key->type == BTRFS_INODE_REF_KEY) {
6382 dirid = key->offset;
6383
6384 ret = dir_changed(sctx, dirid);
6385 goto out;
6386 }
6387
6388 leaf = path->nodes[0];
6389 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6390 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6391 while (cur_offset < item_size) {
6392 extref = (struct btrfs_inode_extref *)(ptr +
6393 cur_offset);
6394 dirid = btrfs_inode_extref_parent(leaf, extref);
6395 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6396 cur_offset += ref_name_len + sizeof(*extref);
6397 if (dirid == last_dirid)
6398 continue;
6399 ret = dir_changed(sctx, dirid);
6400 if (ret)
6401 break;
6402 last_dirid = dirid;
6403 }
6404out:
6405 return ret;
6406}
6407
Alexander Block766702e2012-07-28 14:11:31 +02006408/*
6409 * Updates compare related fields in sctx and simply forwards to the actual
6410 * changed_xxx functions.
6411 */
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006412static int changed_cb(struct btrfs_path *left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02006413 struct btrfs_path *right_path,
6414 struct btrfs_key *key,
6415 enum btrfs_compare_tree_result result,
6416 void *ctx)
6417{
6418 int ret = 0;
6419 struct send_ctx *sctx = ctx;
6420
Josef Bacikba5e8f22013-08-16 16:52:55 -04006421 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04006422 if (key->type == BTRFS_INODE_REF_KEY ||
6423 key->type == BTRFS_INODE_EXTREF_KEY) {
6424 ret = compare_refs(sctx, left_path, key);
6425 if (!ret)
6426 return 0;
6427 if (ret < 0)
6428 return ret;
6429 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6430 return maybe_send_hole(sctx, left_path, key);
6431 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04006432 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006433 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04006434 result = BTRFS_COMPARE_TREE_CHANGED;
6435 ret = 0;
6436 }
6437
Alexander Block31db9f72012-07-25 23:19:24 +02006438 sctx->left_path = left_path;
6439 sctx->right_path = right_path;
6440 sctx->cmp_key = key;
6441
6442 ret = finish_inode_if_needed(sctx, 0);
6443 if (ret < 0)
6444 goto out;
6445
Alexander Block2981e222012-08-01 14:47:03 +02006446 /* Ignore non-FS objects */
6447 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6448 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6449 goto out;
6450
Filipe Manana46b2f452018-07-24 11:54:04 +01006451 if (key->type == BTRFS_INODE_ITEM_KEY) {
Alexander Block31db9f72012-07-25 23:19:24 +02006452 ret = changed_inode(sctx, result);
Filipe Manana46b2f452018-07-24 11:54:04 +01006453 } else if (!sctx->ignore_cur_inode) {
6454 if (key->type == BTRFS_INODE_REF_KEY ||
6455 key->type == BTRFS_INODE_EXTREF_KEY)
6456 ret = changed_ref(sctx, result);
6457 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6458 ret = changed_xattr(sctx, result);
6459 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6460 ret = changed_extent(sctx, result);
6461 }
Alexander Block31db9f72012-07-25 23:19:24 +02006462
6463out:
6464 return ret;
6465}
6466
6467static int full_send_tree(struct send_ctx *sctx)
6468{
6469 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006470 struct btrfs_root *send_root = sctx->send_root;
6471 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006472 struct btrfs_path *path;
6473 struct extent_buffer *eb;
6474 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006475
6476 path = alloc_path_for_send();
6477 if (!path)
6478 return -ENOMEM;
6479
Alexander Block31db9f72012-07-25 23:19:24 +02006480 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6481 key.type = BTRFS_INODE_ITEM_KEY;
6482 key.offset = 0;
6483
Alexander Block31db9f72012-07-25 23:19:24 +02006484 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6485 if (ret < 0)
6486 goto out;
6487 if (ret)
6488 goto out_finish;
6489
6490 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006491 eb = path->nodes[0];
6492 slot = path->slots[0];
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006493 btrfs_item_key_to_cpu(eb, &key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02006494
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006495 ret = changed_cb(path, NULL, &key,
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006496 BTRFS_COMPARE_TREE_NEW, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006497 if (ret < 0)
6498 goto out;
6499
Alexander Block31db9f72012-07-25 23:19:24 +02006500 ret = btrfs_next_item(send_root, path);
6501 if (ret < 0)
6502 goto out;
6503 if (ret) {
6504 ret = 0;
6505 break;
6506 }
6507 }
6508
6509out_finish:
6510 ret = finish_inode_if_needed(sctx, 1);
6511
6512out:
6513 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006514 return ret;
6515}
6516
6517static int send_subvol(struct send_ctx *sctx)
6518{
6519 int ret;
6520
Stefan Behrensc2c71322013-04-10 17:10:52 +00006521 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6522 ret = send_header(sctx);
6523 if (ret < 0)
6524 goto out;
6525 }
Alexander Block31db9f72012-07-25 23:19:24 +02006526
6527 ret = send_subvol_begin(sctx);
6528 if (ret < 0)
6529 goto out;
6530
6531 if (sctx->parent_root) {
6532 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6533 changed_cb, sctx);
6534 if (ret < 0)
6535 goto out;
6536 ret = finish_inode_if_needed(sctx, 1);
6537 if (ret < 0)
6538 goto out;
6539 } else {
6540 ret = full_send_tree(sctx);
6541 if (ret < 0)
6542 goto out;
6543 }
6544
6545out:
Alexander Block31db9f72012-07-25 23:19:24 +02006546 free_recorded_refs(sctx);
6547 return ret;
6548}
6549
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006550/*
6551 * If orphan cleanup did remove any orphans from a root, it means the tree
6552 * was modified and therefore the commit root is not the same as the current
6553 * root anymore. This is a problem, because send uses the commit root and
6554 * therefore can see inode items that don't exist in the current root anymore,
6555 * and for example make calls to btrfs_iget, which will do tree lookups based
6556 * on the current root and not on the commit root. Those lookups will fail,
6557 * returning a -ESTALE error, and making send fail with that error. So make
6558 * sure a send does not see any orphans we have just removed, and that it will
6559 * see the same inodes regardless of whether a transaction commit happened
6560 * before it started (meaning that the commit root will be the same as the
6561 * current root) or not.
6562 */
6563static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6564{
6565 int i;
6566 struct btrfs_trans_handle *trans = NULL;
6567
6568again:
6569 if (sctx->parent_root &&
6570 sctx->parent_root->node != sctx->parent_root->commit_root)
6571 goto commit_trans;
6572
6573 for (i = 0; i < sctx->clone_roots_cnt; i++)
6574 if (sctx->clone_roots[i].root->node !=
6575 sctx->clone_roots[i].root->commit_root)
6576 goto commit_trans;
6577
6578 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006579 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006580
6581 return 0;
6582
6583commit_trans:
6584 /* Use any root, all fs roots will get their commit roots updated. */
6585 if (!trans) {
6586 trans = btrfs_join_transaction(sctx->send_root);
6587 if (IS_ERR(trans))
6588 return PTR_ERR(trans);
6589 goto again;
6590 }
6591
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006592 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006593}
6594
Filipe Manana9f89d5d2019-04-15 09:29:36 +01006595/*
6596 * Make sure any existing dellaloc is flushed for any root used by a send
6597 * operation so that we do not miss any data and we do not race with writeback
6598 * finishing and changing a tree while send is using the tree. This could
6599 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
6600 * a send operation then uses the subvolume.
6601 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
6602 */
6603static int flush_delalloc_roots(struct send_ctx *sctx)
6604{
6605 struct btrfs_root *root = sctx->parent_root;
6606 int ret;
6607 int i;
6608
6609 if (root) {
6610 ret = btrfs_start_delalloc_snapshot(root);
6611 if (ret)
6612 return ret;
6613 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
6614 }
6615
6616 for (i = 0; i < sctx->clone_roots_cnt; i++) {
6617 root = sctx->clone_roots[i].root;
6618 ret = btrfs_start_delalloc_snapshot(root);
6619 if (ret)
6620 return ret;
6621 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
6622 }
6623
6624 return 0;
6625}
6626
David Sterba66ef7d62013-12-17 15:07:20 +01006627static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6628{
6629 spin_lock(&root->root_item_lock);
6630 root->send_in_progress--;
6631 /*
6632 * Not much left to do, we don't know why it's unbalanced and
6633 * can't blindly reset it to 0.
6634 */
6635 if (root->send_in_progress < 0)
6636 btrfs_err(root->fs_info,
Colin Ian Kingf5686e32018-05-04 12:11:12 +01006637 "send_in_progress unbalanced %d root %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006638 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01006639 spin_unlock(&root->root_item_lock);
6640}
6641
Filipe Manana62d54f32019-04-22 16:43:42 +01006642static void dedupe_in_progress_warn(const struct btrfs_root *root)
6643{
6644 btrfs_warn_rl(root->fs_info,
6645"cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
6646 root->root_key.objectid, root->dedupe_in_progress);
6647}
6648
Josef Bacik2351f432017-09-27 10:43:13 -04006649long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
Alexander Block31db9f72012-07-25 23:19:24 +02006650{
6651 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006652 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6653 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02006654 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02006655 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006656 struct send_ctx *sctx = NULL;
6657 u32 i;
6658 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01006659 int clone_sources_to_rollback = 0;
David Sterbae55d1152016-04-11 18:52:02 +02006660 unsigned alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08006661 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08006662 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02006663
6664 if (!capable(CAP_SYS_ADMIN))
6665 return -EPERM;
6666
Josef Bacik139f8072013-05-20 11:26:50 -04006667 /*
David Sterba2c686532013-12-16 17:34:17 +01006668 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02006669 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01006670 */
6671 spin_lock(&send_root->root_item_lock);
Filipe Manana62d54f32019-04-22 16:43:42 +01006672 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
6673 dedupe_in_progress_warn(send_root);
6674 spin_unlock(&send_root->root_item_lock);
6675 return -EAGAIN;
6676 }
David Sterba2c686532013-12-16 17:34:17 +01006677 send_root->send_in_progress++;
6678 spin_unlock(&send_root->root_item_lock);
6679
6680 /*
Josef Bacik139f8072013-05-20 11:26:50 -04006681 * This is done when we lookup the root, it should already be complete
6682 * by the time we get here.
6683 */
6684 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6685
6686 /*
David Sterba2c686532013-12-16 17:34:17 +01006687 * Userspace tools do the checks and warn the user if it's
6688 * not RO.
6689 */
6690 if (!btrfs_root_readonly(send_root)) {
6691 ret = -EPERM;
6692 goto out;
6693 }
6694
Dan Carpenter457ae722017-03-17 23:51:20 +03006695 /*
6696 * Check that we don't overflow at later allocations, we request
6697 * clone_sources_count + 1 items, and compare to unsigned long inside
6698 * access_ok.
6699 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006700 if (arg->clone_sources_count >
Dan Carpenter457ae722017-03-17 23:51:20 +03006701 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006702 ret = -EINVAL;
6703 goto out;
6704 }
6705
Linus Torvalds96d4f262019-01-03 18:57:57 -08006706 if (!access_ok(arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05006707 sizeof(*arg->clone_sources) *
6708 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02006709 ret = -EFAULT;
6710 goto out;
6711 }
6712
Stefan Behrensc2c71322013-04-10 17:10:52 +00006713 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006714 ret = -EINVAL;
6715 goto out;
6716 }
6717
David Sterbae780b0d2016-01-18 18:42:13 +01006718 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006719 if (!sctx) {
6720 ret = -ENOMEM;
6721 goto out;
6722 }
6723
6724 INIT_LIST_HEAD(&sctx->new_refs);
6725 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01006726 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006727 INIT_LIST_HEAD(&sctx->name_cache_list);
6728
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006729 sctx->flags = arg->flags;
6730
Alexander Block31db9f72012-07-25 23:19:24 +02006731 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00006732 if (!sctx->send_filp) {
6733 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02006734 goto out;
6735 }
6736
Alexander Block31db9f72012-07-25 23:19:24 +02006737 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02006738 /*
6739 * Unlikely but possible, if the subvolume is marked for deletion but
6740 * is slow to remove the directory entry, send can still be started
6741 */
6742 if (btrfs_root_dead(sctx->send_root)) {
6743 ret = -EPERM;
6744 goto out;
6745 }
6746
Alexander Block31db9f72012-07-25 23:19:24 +02006747 sctx->clone_roots_cnt = arg->clone_sources_count;
6748
6749 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
Michal Hocko752ade62017-05-08 15:57:27 -07006750 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006751 if (!sctx->send_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006752 ret = -ENOMEM;
6753 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006754 }
6755
Michal Hocko752ade62017-05-08 15:57:27 -07006756 sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006757 if (!sctx->read_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006758 ret = -ENOMEM;
6759 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006760 }
6761
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006762 sctx->pending_dir_moves = RB_ROOT;
6763 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00006764 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006765
David Sterbae55d1152016-04-11 18:52:02 +02006766 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6767
David Sterba818e0102017-05-31 18:40:02 +02006768 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006769 if (!sctx->clone_roots) {
David Sterba818e0102017-05-31 18:40:02 +02006770 ret = -ENOMEM;
6771 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006772 }
6773
David Sterbae55d1152016-04-11 18:52:02 +02006774 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6775
Alexander Block31db9f72012-07-25 23:19:24 +02006776 if (arg->clone_sources_count) {
Michal Hocko752ade62017-05-08 15:57:27 -07006777 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006778 if (!clone_sources_tmp) {
Michal Hocko752ade62017-05-08 15:57:27 -07006779 ret = -ENOMEM;
6780 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006781 }
6782
6783 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02006784 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02006785 if (ret) {
6786 ret = -EFAULT;
6787 goto out;
6788 }
6789
6790 for (i = 0; i < arg->clone_sources_count; i++) {
6791 key.objectid = clone_sources_tmp[i];
6792 key.type = BTRFS_ROOT_ITEM_KEY;
6793 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006794
6795 index = srcu_read_lock(&fs_info->subvol_srcu);
6796
Alexander Block31db9f72012-07-25 23:19:24 +02006797 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02006798 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006799 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006800 ret = PTR_ERR(clone_root);
6801 goto out;
6802 }
David Sterba2c686532013-12-16 17:34:17 +01006803 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00006804 if (!btrfs_root_readonly(clone_root) ||
6805 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006806 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006807 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006808 ret = -EPERM;
6809 goto out;
6810 }
Filipe Manana62d54f32019-04-22 16:43:42 +01006811 if (clone_root->dedupe_in_progress) {
6812 dedupe_in_progress_warn(clone_root);
6813 spin_unlock(&clone_root->root_item_lock);
6814 srcu_read_unlock(&fs_info->subvol_srcu, index);
6815 ret = -EAGAIN;
6816 goto out;
6817 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00006818 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01006819 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006820 srcu_read_unlock(&fs_info->subvol_srcu, index);
6821
Alexander Block31db9f72012-07-25 23:19:24 +02006822 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00006823 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006824 }
David Sterba2f913062016-04-11 18:40:08 +02006825 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006826 clone_sources_tmp = NULL;
6827 }
6828
6829 if (arg->parent_root) {
6830 key.objectid = arg->parent_root;
6831 key.type = BTRFS_ROOT_ITEM_KEY;
6832 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006833
6834 index = srcu_read_lock(&fs_info->subvol_srcu);
6835
Alexander Block31db9f72012-07-25 23:19:24 +02006836 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006837 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006838 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006839 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02006840 goto out;
6841 }
Wang Shilong18f687d2014-01-07 17:25:19 +08006842
David Sterba2c686532013-12-16 17:34:17 +01006843 spin_lock(&sctx->parent_root->root_item_lock);
6844 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02006845 if (!btrfs_root_readonly(sctx->parent_root) ||
6846 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006847 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006848 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006849 ret = -EPERM;
6850 goto out;
6851 }
Filipe Manana62d54f32019-04-22 16:43:42 +01006852 if (sctx->parent_root->dedupe_in_progress) {
6853 dedupe_in_progress_warn(sctx->parent_root);
6854 spin_unlock(&sctx->parent_root->root_item_lock);
6855 srcu_read_unlock(&fs_info->subvol_srcu, index);
6856 ret = -EAGAIN;
6857 goto out;
6858 }
David Sterba2c686532013-12-16 17:34:17 +01006859 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006860
6861 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006862 }
6863
6864 /*
6865 * Clones from send_root are allowed, but only if the clone source
6866 * is behind the current send position. This is checked while searching
6867 * for possible clone sources.
6868 */
6869 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6870
6871 /* We do a bsearch later */
6872 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6873 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6874 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08006875 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006876
Filipe Manana9f89d5d2019-04-15 09:29:36 +01006877 ret = flush_delalloc_roots(sctx);
6878 if (ret)
6879 goto out;
6880
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006881 ret = ensure_commit_roots_uptodate(sctx);
6882 if (ret)
6883 goto out;
6884
Filipe Manana9e967492019-04-22 16:44:09 +01006885 mutex_lock(&fs_info->balance_mutex);
6886 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
6887 mutex_unlock(&fs_info->balance_mutex);
6888 btrfs_warn_rl(fs_info,
6889 "cannot run send because a balance operation is in progress");
6890 ret = -EAGAIN;
6891 goto out;
6892 }
6893 fs_info->send_in_progress++;
6894 mutex_unlock(&fs_info->balance_mutex);
6895
David Sterba2755a0d2014-07-31 00:43:18 +02006896 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02006897 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04006898 current->journal_info = NULL;
Filipe Manana9e967492019-04-22 16:44:09 +01006899 mutex_lock(&fs_info->balance_mutex);
6900 fs_info->send_in_progress--;
6901 mutex_unlock(&fs_info->balance_mutex);
Alexander Block31db9f72012-07-25 23:19:24 +02006902 if (ret < 0)
6903 goto out;
6904
Stefan Behrensc2c71322013-04-10 17:10:52 +00006905 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6906 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6907 if (ret < 0)
6908 goto out;
6909 ret = send_cmd(sctx);
6910 if (ret < 0)
6911 goto out;
6912 }
Alexander Block31db9f72012-07-25 23:19:24 +02006913
6914out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006915 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6916 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6917 struct rb_node *n;
6918 struct pending_dir_move *pm;
6919
6920 n = rb_first(&sctx->pending_dir_moves);
6921 pm = rb_entry(n, struct pending_dir_move, node);
6922 while (!list_empty(&pm->list)) {
6923 struct pending_dir_move *pm2;
6924
6925 pm2 = list_first_entry(&pm->list,
6926 struct pending_dir_move, list);
6927 free_pending_move(sctx, pm2);
6928 }
6929 free_pending_move(sctx, pm);
6930 }
6931
6932 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6933 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6934 struct rb_node *n;
6935 struct waiting_dir_move *dm;
6936
6937 n = rb_first(&sctx->waiting_dir_moves);
6938 dm = rb_entry(n, struct waiting_dir_move, node);
6939 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6940 kfree(dm);
6941 }
6942
Filipe Manana9dc44212014-02-19 14:31:44 +00006943 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6944 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6945 struct rb_node *n;
6946 struct orphan_dir_info *odi;
6947
6948 n = rb_first(&sctx->orphan_dirs);
6949 odi = rb_entry(n, struct orphan_dir_info, node);
6950 free_orphan_dir_info(sctx, odi);
6951 }
6952
Wang Shilong896c14f2014-01-07 17:25:18 +08006953 if (sort_clone_roots) {
6954 for (i = 0; i < sctx->clone_roots_cnt; i++)
6955 btrfs_root_dec_send_in_progress(
6956 sctx->clone_roots[i].root);
6957 } else {
6958 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6959 btrfs_root_dec_send_in_progress(
6960 sctx->clone_roots[i].root);
6961
6962 btrfs_root_dec_send_in_progress(send_root);
6963 }
David Sterba66ef7d62013-12-17 15:07:20 +01006964 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6965 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01006966
David Sterba2f913062016-04-11 18:40:08 +02006967 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006968
6969 if (sctx) {
6970 if (sctx->send_filp)
6971 fput(sctx->send_filp);
6972
David Sterbac03d01f2016-04-11 18:40:08 +02006973 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02006974 kvfree(sctx->send_buf);
David Sterbaeb5b75f2016-04-11 18:40:08 +02006975 kvfree(sctx->read_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02006976
6977 name_cache_free(sctx);
6978
6979 kfree(sctx);
6980 }
6981
6982 return ret;
6983}