blob: 7ea2d6b1f170bc7b20eec66051d69f20986fc827 [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
Nikolay Borisov9678c542018-01-08 11:45:05 +0200689 crc = 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
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001163 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001164 /* number of total found references */
1165 u64 found;
1166
1167 /*
1168 * used for clones found in send_root. clones found behind cur_objectid
1169 * and cur_offset are not considered as allowed clones.
1170 */
1171 u64 cur_objectid;
1172 u64 cur_offset;
1173
1174 /* may be truncated in case it's the last extent in a file */
1175 u64 extent_len;
1176
Filipe Manana619d8c42015-05-03 01:56:00 +01001177 /* data offset in the file extent item */
1178 u64 data_offset;
1179
Alexander Block31db9f72012-07-25 23:19:24 +02001180 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001181 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001182};
1183
1184static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1185{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001186 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001187 struct clone_root *cr = (struct clone_root *)elt;
1188
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001189 if (root < cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001190 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001191 if (root > cr->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001192 return 1;
1193 return 0;
1194}
1195
1196static int __clone_root_cmp_sort(const void *e1, const void *e2)
1197{
1198 struct clone_root *cr1 = (struct clone_root *)e1;
1199 struct clone_root *cr2 = (struct clone_root *)e2;
1200
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001201 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001202 return -1;
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09001203 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001204 return 1;
1205 return 0;
1206}
1207
1208/*
1209 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001210 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001211 */
1212static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1213{
1214 struct backref_ctx *bctx = ctx_;
1215 struct clone_root *found;
1216 int ret;
1217 u64 i_size;
1218
1219 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001220 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001221 bctx->sctx->clone_roots_cnt,
1222 sizeof(struct clone_root),
1223 __clone_root_cmp_bsearch);
1224 if (!found)
1225 return 0;
1226
1227 if (found->root == bctx->sctx->send_root &&
1228 ino == bctx->cur_objectid &&
1229 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001230 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001231 }
1232
1233 /*
Alexander Block766702e2012-07-28 14:11:31 +02001234 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001235 * accept clones from these extents.
1236 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001237 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1238 NULL, NULL, NULL);
1239 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001240 if (ret < 0)
1241 return ret;
1242
Filipe Manana619d8c42015-05-03 01:56:00 +01001243 if (offset + bctx->data_offset + bctx->extent_len > i_size)
Alexander Block31db9f72012-07-25 23:19:24 +02001244 return 0;
1245
1246 /*
1247 * Make sure we don't consider clones from send_root that are
1248 * behind the current inode/offset.
1249 */
1250 if (found->root == bctx->sctx->send_root) {
1251 /*
1252 * TODO for the moment we don't accept clones from the inode
1253 * that is currently send. We may change this when
1254 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1255 * file.
1256 */
1257 if (ino >= bctx->cur_objectid)
1258 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001259 }
1260
1261 bctx->found++;
1262 found->found_refs++;
1263 if (ino < found->ino) {
1264 found->ino = ino;
1265 found->offset = offset;
1266 } else if (found->ino == ino) {
1267 /*
1268 * same extent found more then once in the same file.
1269 */
1270 if (found->offset > offset + bctx->extent_len)
1271 found->offset = offset;
1272 }
1273
1274 return 0;
1275}
1276
1277/*
Alexander Block766702e2012-07-28 14:11:31 +02001278 * Given an inode, offset and extent item, it finds a good clone for a clone
1279 * instruction. Returns -ENOENT when none could be found. The function makes
1280 * sure that the returned clone is usable at the point where sending is at the
1281 * moment. This means, that no clones are accepted which lie behind the current
1282 * inode+offset.
1283 *
Alexander Block31db9f72012-07-25 23:19:24 +02001284 * path must point to the extent item when called.
1285 */
1286static int find_extent_clone(struct send_ctx *sctx,
1287 struct btrfs_path *path,
1288 u64 ino, u64 data_offset,
1289 u64 ino_size,
1290 struct clone_root **found)
1291{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001292 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001293 int ret;
1294 int extent_type;
1295 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001296 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001297 u64 num_bytes;
1298 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001299 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001300 struct btrfs_file_extent_item *fi;
1301 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001302 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001303 struct clone_root *cur_clone_root;
1304 struct btrfs_key found_key;
1305 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001306 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001307 u32 i;
1308
1309 tmp_path = alloc_path_for_send();
1310 if (!tmp_path)
1311 return -ENOMEM;
1312
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001313 /* We only use this path under the commit sem */
1314 tmp_path->need_commit_sem = 0;
1315
David Sterbae780b0d2016-01-18 18:42:13 +01001316 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001317 if (!backref_ctx) {
1318 ret = -ENOMEM;
1319 goto out;
1320 }
1321
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001322 backref_ctx->path = tmp_path;
1323
Alexander Block31db9f72012-07-25 23:19:24 +02001324 if (data_offset >= ino_size) {
1325 /*
1326 * There may be extents that lie behind the file's size.
1327 * I at least had this in combination with snapshotting while
1328 * writing large files.
1329 */
1330 ret = 0;
1331 goto out;
1332 }
1333
1334 fi = btrfs_item_ptr(eb, path->slots[0],
1335 struct btrfs_file_extent_item);
1336 extent_type = btrfs_file_extent_type(eb, fi);
1337 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1338 ret = -ENOENT;
1339 goto out;
1340 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001341 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001342
1343 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001344 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1345 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001346 ret = -ENOENT;
1347 goto out;
1348 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001349 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001350
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001351 down_read(&fs_info->commit_root_sem);
1352 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001353 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001354 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001355 btrfs_release_path(tmp_path);
1356
1357 if (ret < 0)
1358 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001359 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001360 ret = -EIO;
1361 goto out;
1362 }
1363
1364 /*
1365 * Setup the clone roots.
1366 */
1367 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1368 cur_clone_root = sctx->clone_roots + i;
1369 cur_clone_root->ino = (u64)-1;
1370 cur_clone_root->offset = 0;
1371 cur_clone_root->found_refs = 0;
1372 }
1373
Alexander Block35075bb2012-07-28 12:44:34 +02001374 backref_ctx->sctx = sctx;
1375 backref_ctx->found = 0;
1376 backref_ctx->cur_objectid = ino;
1377 backref_ctx->cur_offset = data_offset;
1378 backref_ctx->found_itself = 0;
1379 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001380 /*
1381 * For non-compressed extents iterate_extent_inodes() gives us extent
1382 * offsets that already take into account the data offset, but not for
1383 * compressed extents, since the offset is logical and not relative to
1384 * the physical extent locations. We must take this into account to
1385 * avoid sending clone offsets that go beyond the source file's size,
1386 * which would result in the clone ioctl failing with -EINVAL on the
1387 * receiving end.
1388 */
1389 if (compressed == BTRFS_COMPRESS_NONE)
1390 backref_ctx->data_offset = 0;
1391 else
1392 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001393
1394 /*
1395 * The last extent of a file may be too large due to page alignment.
1396 * We need to adjust extent_len in this case so that the checks in
1397 * __iterate_backrefs work.
1398 */
1399 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001400 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001401
1402 /*
1403 * Now collect all backrefs.
1404 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001405 if (compressed == BTRFS_COMPRESS_NONE)
1406 extent_item_pos = logical - found_key.objectid;
1407 else
1408 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001409 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1410 extent_item_pos, 1, __iterate_backrefs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001411 backref_ctx, false);
Chris Mason74dd17f2012-08-07 16:25:13 -04001412
Alexander Block31db9f72012-07-25 23:19:24 +02001413 if (ret < 0)
1414 goto out;
1415
Alexander Block35075bb2012-07-28 12:44:34 +02001416 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001417 /* found a bug in backref code? */
1418 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001419 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001420 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001421 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001422 goto out;
1423 }
1424
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001425 btrfs_debug(fs_info,
1426 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1427 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001428
Alexander Block35075bb2012-07-28 12:44:34 +02001429 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001430 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001431
1432 cur_clone_root = NULL;
1433 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1434 if (sctx->clone_roots[i].found_refs) {
1435 if (!cur_clone_root)
1436 cur_clone_root = sctx->clone_roots + i;
1437 else if (sctx->clone_roots[i].root == sctx->send_root)
1438 /* prefer clones from send_root over others */
1439 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001440 }
1441
1442 }
1443
1444 if (cur_clone_root) {
1445 *found = cur_clone_root;
1446 ret = 0;
1447 } else {
1448 ret = -ENOENT;
1449 }
1450
1451out:
1452 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001453 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001454 return ret;
1455}
1456
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001457static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001458 u64 ino,
1459 struct fs_path *dest)
1460{
1461 int ret;
1462 struct btrfs_path *path;
1463 struct btrfs_key key;
1464 struct btrfs_file_extent_item *ei;
1465 u8 type;
1466 u8 compression;
1467 unsigned long off;
1468 int len;
1469
1470 path = alloc_path_for_send();
1471 if (!path)
1472 return -ENOMEM;
1473
1474 key.objectid = ino;
1475 key.type = BTRFS_EXTENT_DATA_KEY;
1476 key.offset = 0;
1477 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1478 if (ret < 0)
1479 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001480 if (ret) {
1481 /*
1482 * An empty symlink inode. Can happen in rare error paths when
1483 * creating a symlink (transaction committed before the inode
1484 * eviction handler removed the symlink inode items and a crash
1485 * happened in between or the subvol was snapshoted in between).
1486 * Print an informative message to dmesg/syslog so that the user
1487 * can delete the symlink.
1488 */
1489 btrfs_err(root->fs_info,
1490 "Found empty symlink inode %llu at root %llu",
1491 ino, root->root_key.objectid);
1492 ret = -EIO;
1493 goto out;
1494 }
Alexander Block31db9f72012-07-25 23:19:24 +02001495
1496 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1497 struct btrfs_file_extent_item);
1498 type = btrfs_file_extent_type(path->nodes[0], ei);
1499 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1500 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1501 BUG_ON(compression);
1502
1503 off = btrfs_file_extent_inline_start(ei);
Qu Wenruoe41ca582018-06-06 15:41:49 +08001504 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001505
1506 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001507
1508out:
1509 btrfs_free_path(path);
1510 return ret;
1511}
1512
1513/*
1514 * Helper function to generate a file name that is unique in the root of
1515 * send_root and parent_root. This is used to generate names for orphan inodes.
1516 */
1517static int gen_unique_name(struct send_ctx *sctx,
1518 u64 ino, u64 gen,
1519 struct fs_path *dest)
1520{
1521 int ret = 0;
1522 struct btrfs_path *path;
1523 struct btrfs_dir_item *di;
1524 char tmp[64];
1525 int len;
1526 u64 idx = 0;
1527
1528 path = alloc_path_for_send();
1529 if (!path)
1530 return -ENOMEM;
1531
1532 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001533 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001534 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001535 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001536
1537 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1538 path, BTRFS_FIRST_FREE_OBJECTID,
1539 tmp, strlen(tmp), 0);
1540 btrfs_release_path(path);
1541 if (IS_ERR(di)) {
1542 ret = PTR_ERR(di);
1543 goto out;
1544 }
1545 if (di) {
1546 /* not unique, try again */
1547 idx++;
1548 continue;
1549 }
1550
1551 if (!sctx->parent_root) {
1552 /* unique */
1553 ret = 0;
1554 break;
1555 }
1556
1557 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1558 path, BTRFS_FIRST_FREE_OBJECTID,
1559 tmp, strlen(tmp), 0);
1560 btrfs_release_path(path);
1561 if (IS_ERR(di)) {
1562 ret = PTR_ERR(di);
1563 goto out;
1564 }
1565 if (di) {
1566 /* not unique, try again */
1567 idx++;
1568 continue;
1569 }
1570 /* unique */
1571 break;
1572 }
1573
1574 ret = fs_path_add(dest, tmp, strlen(tmp));
1575
1576out:
1577 btrfs_free_path(path);
1578 return ret;
1579}
1580
1581enum inode_state {
1582 inode_state_no_change,
1583 inode_state_will_create,
1584 inode_state_did_create,
1585 inode_state_will_delete,
1586 inode_state_did_delete,
1587};
1588
1589static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1590{
1591 int ret;
1592 int left_ret;
1593 int right_ret;
1594 u64 left_gen;
1595 u64 right_gen;
1596
1597 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001598 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001599 if (ret < 0 && ret != -ENOENT)
1600 goto out;
1601 left_ret = ret;
1602
1603 if (!sctx->parent_root) {
1604 right_ret = -ENOENT;
1605 } else {
1606 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001607 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001608 if (ret < 0 && ret != -ENOENT)
1609 goto out;
1610 right_ret = ret;
1611 }
1612
1613 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001614 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001615 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001616 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001617 if (ino < sctx->send_progress)
1618 ret = inode_state_did_create;
1619 else
1620 ret = inode_state_will_create;
1621 } else 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 if (!left_ret) {
1630 if (left_gen == gen) {
1631 if (ino < sctx->send_progress)
1632 ret = inode_state_did_create;
1633 else
1634 ret = inode_state_will_create;
1635 } else {
1636 ret = -ENOENT;
1637 }
1638 } else if (!right_ret) {
1639 if (right_gen == gen) {
1640 if (ino < sctx->send_progress)
1641 ret = inode_state_did_delete;
1642 else
1643 ret = inode_state_will_delete;
1644 } else {
1645 ret = -ENOENT;
1646 }
1647 } else {
1648 ret = -ENOENT;
1649 }
1650
1651out:
1652 return ret;
1653}
1654
1655static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1656{
1657 int ret;
1658
Robbie Ko4dd99202017-01-05 16:24:55 +08001659 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1660 return 1;
1661
Alexander Block31db9f72012-07-25 23:19:24 +02001662 ret = get_cur_inode_state(sctx, ino, gen);
1663 if (ret < 0)
1664 goto out;
1665
1666 if (ret == inode_state_no_change ||
1667 ret == inode_state_did_create ||
1668 ret == inode_state_will_delete)
1669 ret = 1;
1670 else
1671 ret = 0;
1672
1673out:
1674 return ret;
1675}
1676
1677/*
1678 * Helper function to lookup a dir item in a dir.
1679 */
1680static int lookup_dir_item_inode(struct btrfs_root *root,
1681 u64 dir, const char *name, int name_len,
1682 u64 *found_inode,
1683 u8 *found_type)
1684{
1685 int ret = 0;
1686 struct btrfs_dir_item *di;
1687 struct btrfs_key key;
1688 struct btrfs_path *path;
1689
1690 path = alloc_path_for_send();
1691 if (!path)
1692 return -ENOMEM;
1693
1694 di = btrfs_lookup_dir_item(NULL, root, path,
1695 dir, name, name_len, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08001696 if (IS_ERR_OR_NULL(di)) {
1697 ret = di ? PTR_ERR(di) : -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02001698 goto out;
1699 }
1700 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001701 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1702 ret = -ENOENT;
1703 goto out;
1704 }
Alexander Block31db9f72012-07-25 23:19:24 +02001705 *found_inode = key.objectid;
1706 *found_type = btrfs_dir_type(path->nodes[0], di);
1707
1708out:
1709 btrfs_free_path(path);
1710 return ret;
1711}
1712
Alexander Block766702e2012-07-28 14:11:31 +02001713/*
1714 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1715 * generation of the parent dir and the name of the dir entry.
1716 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001717static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001718 u64 *dir, u64 *dir_gen, struct fs_path *name)
1719{
1720 int ret;
1721 struct btrfs_key key;
1722 struct btrfs_key found_key;
1723 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001724 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001725 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001726
1727 path = alloc_path_for_send();
1728 if (!path)
1729 return -ENOMEM;
1730
1731 key.objectid = ino;
1732 key.type = BTRFS_INODE_REF_KEY;
1733 key.offset = 0;
1734
1735 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1736 if (ret < 0)
1737 goto out;
1738 if (!ret)
1739 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1740 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001741 if (ret || found_key.objectid != ino ||
1742 (found_key.type != BTRFS_INODE_REF_KEY &&
1743 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001744 ret = -ENOENT;
1745 goto out;
1746 }
1747
Filipe Manana51a60252014-05-13 22:01:02 +01001748 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001749 struct btrfs_inode_ref *iref;
1750 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1751 struct btrfs_inode_ref);
1752 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1753 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1754 (unsigned long)(iref + 1),
1755 len);
1756 parent_dir = found_key.offset;
1757 } else {
1758 struct btrfs_inode_extref *extref;
1759 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1760 struct btrfs_inode_extref);
1761 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1762 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1763 (unsigned long)&extref->name, len);
1764 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1765 }
Alexander Block31db9f72012-07-25 23:19:24 +02001766 if (ret < 0)
1767 goto out;
1768 btrfs_release_path(path);
1769
Filipe Mananab46ab972014-03-21 12:46:54 +00001770 if (dir_gen) {
1771 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1772 NULL, NULL, NULL);
1773 if (ret < 0)
1774 goto out;
1775 }
Alexander Block31db9f72012-07-25 23:19:24 +02001776
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001777 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001778
1779out:
1780 btrfs_free_path(path);
1781 return ret;
1782}
1783
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001784static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001785 u64 ino, u64 dir,
1786 const char *name, int name_len)
1787{
1788 int ret;
1789 struct fs_path *tmp_name;
1790 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001791
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001792 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001793 if (!tmp_name)
1794 return -ENOMEM;
1795
Filipe Mananab46ab972014-03-21 12:46:54 +00001796 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001797 if (ret < 0)
1798 goto out;
1799
Alexander Blockb9291af2012-07-28 11:07:18 +02001800 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001801 ret = 0;
1802 goto out;
1803 }
1804
Alexander Blocke938c8a2012-07-28 16:33:49 +02001805 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001806
1807out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001808 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001809 return ret;
1810}
1811
Alexander Block766702e2012-07-28 14:11:31 +02001812/*
1813 * Used by process_recorded_refs to determine if a new ref would overwrite an
1814 * already existing ref. In case it detects an overwrite, it returns the
1815 * inode/gen in who_ino/who_gen.
1816 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1817 * to make sure later references to the overwritten inode are possible.
1818 * Orphanizing is however only required for the first ref of an inode.
1819 * process_recorded_refs does an additional is_first_ref check to see if
1820 * orphanizing is really required.
1821 */
Alexander Block31db9f72012-07-25 23:19:24 +02001822static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1823 const char *name, int name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01001824 u64 *who_ino, u64 *who_gen, u64 *who_mode)
Alexander Block31db9f72012-07-25 23:19:24 +02001825{
1826 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001827 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001828 u64 other_inode = 0;
1829 u8 other_type = 0;
1830
1831 if (!sctx->parent_root)
1832 goto out;
1833
1834 ret = is_inode_existent(sctx, dir, dir_gen);
1835 if (ret <= 0)
1836 goto out;
1837
Josef Bacikebdad912013-08-06 16:47:48 -04001838 /*
1839 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001840 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001841 * and we can just unlink this entry.
1842 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001843 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001844 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1845 NULL, NULL, NULL);
1846 if (ret < 0 && ret != -ENOENT)
1847 goto out;
1848 if (ret) {
1849 ret = 0;
1850 goto out;
1851 }
1852 if (gen != dir_gen)
1853 goto out;
1854 }
1855
Alexander Block31db9f72012-07-25 23:19:24 +02001856 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1857 &other_inode, &other_type);
1858 if (ret < 0 && ret != -ENOENT)
1859 goto out;
1860 if (ret) {
1861 ret = 0;
1862 goto out;
1863 }
1864
Alexander Block766702e2012-07-28 14:11:31 +02001865 /*
1866 * Check if the overwritten ref was already processed. If yes, the ref
1867 * was already unlinked/moved, so we can safely assume that we will not
1868 * overwrite anything at this point in time.
1869 */
Robbie Ko801bec32015-06-23 18:39:46 +08001870 if (other_inode > sctx->send_progress ||
1871 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001872 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Filipe Mananaf5962782017-06-22 20:03:51 +01001873 who_gen, who_mode, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001874 if (ret < 0)
1875 goto out;
1876
1877 ret = 1;
1878 *who_ino = other_inode;
1879 } else {
1880 ret = 0;
1881 }
1882
1883out:
1884 return ret;
1885}
1886
Alexander Block766702e2012-07-28 14:11:31 +02001887/*
1888 * Checks if the ref was overwritten by an already processed inode. This is
1889 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1890 * thus the orphan name needs be used.
1891 * process_recorded_refs also uses it to avoid unlinking of refs that were
1892 * overwritten.
1893 */
Alexander Block31db9f72012-07-25 23:19:24 +02001894static int did_overwrite_ref(struct send_ctx *sctx,
1895 u64 dir, u64 dir_gen,
1896 u64 ino, u64 ino_gen,
1897 const char *name, int name_len)
1898{
1899 int ret = 0;
1900 u64 gen;
1901 u64 ow_inode;
1902 u8 other_type;
1903
1904 if (!sctx->parent_root)
1905 goto out;
1906
1907 ret = is_inode_existent(sctx, dir, dir_gen);
1908 if (ret <= 0)
1909 goto out;
1910
Robbie Ko01914102017-01-05 16:24:58 +08001911 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1912 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1913 NULL, NULL, NULL);
1914 if (ret < 0 && ret != -ENOENT)
1915 goto out;
1916 if (ret) {
1917 ret = 0;
1918 goto out;
1919 }
1920 if (gen != dir_gen)
1921 goto out;
1922 }
1923
Alexander Block31db9f72012-07-25 23:19:24 +02001924 /* check if the ref was overwritten by another ref */
1925 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1926 &ow_inode, &other_type);
1927 if (ret < 0 && ret != -ENOENT)
1928 goto out;
1929 if (ret) {
1930 /* was never and will never be overwritten */
1931 ret = 0;
1932 goto out;
1933 }
1934
1935 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001936 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001937 if (ret < 0)
1938 goto out;
1939
1940 if (ow_inode == ino && gen == ino_gen) {
1941 ret = 0;
1942 goto out;
1943 }
1944
Filipe Manana8b191a62015-04-09 14:09:14 +01001945 /*
1946 * We know that it is or will be overwritten. Check this now.
1947 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001948 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1949 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001950 */
Filipe Mananab786f162015-09-26 15:30:23 +01001951 if ((ow_inode < sctx->send_progress) ||
1952 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1953 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001954 ret = 1;
1955 else
1956 ret = 0;
1957
1958out:
1959 return ret;
1960}
1961
Alexander Block766702e2012-07-28 14:11:31 +02001962/*
1963 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1964 * that got overwritten. This is used by process_recorded_refs to determine
1965 * if it has to use the path as returned by get_cur_path or the orphan name.
1966 */
Alexander Block31db9f72012-07-25 23:19:24 +02001967static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1968{
1969 int ret = 0;
1970 struct fs_path *name = NULL;
1971 u64 dir;
1972 u64 dir_gen;
1973
1974 if (!sctx->parent_root)
1975 goto out;
1976
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001977 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001978 if (!name)
1979 return -ENOMEM;
1980
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001981 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001982 if (ret < 0)
1983 goto out;
1984
1985 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1986 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001987
1988out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001989 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001990 return ret;
1991}
1992
Alexander Block766702e2012-07-28 14:11:31 +02001993/*
1994 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1995 * so we need to do some special handling in case we have clashes. This function
1996 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001997 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001998 */
Alexander Block31db9f72012-07-25 23:19:24 +02001999static int name_cache_insert(struct send_ctx *sctx,
2000 struct name_cache_entry *nce)
2001{
2002 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002003 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002004
Alexander Block7e0926f2012-07-28 14:20:58 +02002005 nce_head = radix_tree_lookup(&sctx->name_cache,
2006 (unsigned long)nce->ino);
2007 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002008 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002009 if (!nce_head) {
2010 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002011 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002012 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002013 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002014
Alexander Block7e0926f2012-07-28 14:20:58 +02002015 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002016 if (ret < 0) {
2017 kfree(nce_head);
2018 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002019 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002020 }
Alexander Block31db9f72012-07-25 23:19:24 +02002021 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002022 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002023 list_add_tail(&nce->list, &sctx->name_cache_list);
2024 sctx->name_cache_size++;
2025
2026 return ret;
2027}
2028
2029static void name_cache_delete(struct send_ctx *sctx,
2030 struct name_cache_entry *nce)
2031{
Alexander Block7e0926f2012-07-28 14:20:58 +02002032 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002033
Alexander Block7e0926f2012-07-28 14:20:58 +02002034 nce_head = radix_tree_lookup(&sctx->name_cache,
2035 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002036 if (!nce_head) {
2037 btrfs_err(sctx->send_root->fs_info,
2038 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2039 nce->ino, sctx->name_cache_size);
2040 }
Alexander Block31db9f72012-07-25 23:19:24 +02002041
Alexander Block7e0926f2012-07-28 14:20:58 +02002042 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002043 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002044 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002045
David Sterba57fb8912014-02-03 19:24:40 +01002046 /*
2047 * We may not get to the final release of nce_head if the lookup fails
2048 */
2049 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002050 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2051 kfree(nce_head);
2052 }
Alexander Block31db9f72012-07-25 23:19:24 +02002053}
2054
2055static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2056 u64 ino, u64 gen)
2057{
Alexander Block7e0926f2012-07-28 14:20:58 +02002058 struct list_head *nce_head;
2059 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002060
Alexander Block7e0926f2012-07-28 14:20:58 +02002061 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2062 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002063 return NULL;
2064
Alexander Block7e0926f2012-07-28 14:20:58 +02002065 list_for_each_entry(cur, nce_head, radix_list) {
2066 if (cur->ino == ino && cur->gen == gen)
2067 return cur;
2068 }
Alexander Block31db9f72012-07-25 23:19:24 +02002069 return NULL;
2070}
2071
Alexander Block766702e2012-07-28 14:11:31 +02002072/*
2073 * Removes the entry from the list and adds it back to the end. This marks the
2074 * entry as recently used so that name_cache_clean_unused does not remove it.
2075 */
Alexander Block31db9f72012-07-25 23:19:24 +02002076static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2077{
2078 list_del(&nce->list);
2079 list_add_tail(&nce->list, &sctx->name_cache_list);
2080}
2081
Alexander Block766702e2012-07-28 14:11:31 +02002082/*
2083 * Remove some entries from the beginning of name_cache_list.
2084 */
Alexander Block31db9f72012-07-25 23:19:24 +02002085static void name_cache_clean_unused(struct send_ctx *sctx)
2086{
2087 struct name_cache_entry *nce;
2088
2089 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2090 return;
2091
2092 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2093 nce = list_entry(sctx->name_cache_list.next,
2094 struct name_cache_entry, list);
2095 name_cache_delete(sctx, nce);
2096 kfree(nce);
2097 }
2098}
2099
2100static void name_cache_free(struct send_ctx *sctx)
2101{
2102 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002103
Alexander Blocke938c8a2012-07-28 16:33:49 +02002104 while (!list_empty(&sctx->name_cache_list)) {
2105 nce = list_entry(sctx->name_cache_list.next,
2106 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002107 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002108 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002109 }
2110}
2111
Alexander Block766702e2012-07-28 14:11:31 +02002112/*
2113 * Used by get_cur_path for each ref up to the root.
2114 * Returns 0 if it succeeded.
2115 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2116 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2117 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2118 * Returns <0 in case of error.
2119 */
Alexander Block31db9f72012-07-25 23:19:24 +02002120static int __get_cur_name_and_parent(struct send_ctx *sctx,
2121 u64 ino, u64 gen,
2122 u64 *parent_ino,
2123 u64 *parent_gen,
2124 struct fs_path *dest)
2125{
2126 int ret;
2127 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002128 struct name_cache_entry *nce = NULL;
2129
Alexander Block766702e2012-07-28 14:11:31 +02002130 /*
2131 * First check if we already did a call to this function with the same
2132 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2133 * return the cached result.
2134 */
Alexander Block31db9f72012-07-25 23:19:24 +02002135 nce = name_cache_search(sctx, ino, gen);
2136 if (nce) {
2137 if (ino < sctx->send_progress && nce->need_later_update) {
2138 name_cache_delete(sctx, nce);
2139 kfree(nce);
2140 nce = NULL;
2141 } else {
2142 name_cache_used(sctx, nce);
2143 *parent_ino = nce->parent_ino;
2144 *parent_gen = nce->parent_gen;
2145 ret = fs_path_add(dest, nce->name, nce->name_len);
2146 if (ret < 0)
2147 goto out;
2148 ret = nce->ret;
2149 goto out;
2150 }
2151 }
2152
Alexander Block766702e2012-07-28 14:11:31 +02002153 /*
2154 * If the inode is not existent yet, add the orphan name and return 1.
2155 * This should only happen for the parent dir that we determine in
2156 * __record_new_ref
2157 */
Alexander Block31db9f72012-07-25 23:19:24 +02002158 ret = is_inode_existent(sctx, ino, gen);
2159 if (ret < 0)
2160 goto out;
2161
2162 if (!ret) {
2163 ret = gen_unique_name(sctx, ino, gen, dest);
2164 if (ret < 0)
2165 goto out;
2166 ret = 1;
2167 goto out_cache;
2168 }
2169
Alexander Block766702e2012-07-28 14:11:31 +02002170 /*
2171 * Depending on whether the inode was already processed or not, use
2172 * send_root or parent_root for ref lookup.
2173 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002174 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002175 ret = get_first_ref(sctx->send_root, ino,
2176 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002177 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002178 ret = get_first_ref(sctx->parent_root, ino,
2179 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002180 if (ret < 0)
2181 goto out;
2182
Alexander Block766702e2012-07-28 14:11:31 +02002183 /*
2184 * Check if the ref was overwritten by an inode's ref that was processed
2185 * earlier. If yes, treat as orphan and return 1.
2186 */
Alexander Block31db9f72012-07-25 23:19:24 +02002187 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2188 dest->start, dest->end - dest->start);
2189 if (ret < 0)
2190 goto out;
2191 if (ret) {
2192 fs_path_reset(dest);
2193 ret = gen_unique_name(sctx, ino, gen, dest);
2194 if (ret < 0)
2195 goto out;
2196 ret = 1;
2197 }
2198
2199out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002200 /*
2201 * Store the result of the lookup in the name cache.
2202 */
David Sterbae780b0d2016-01-18 18:42:13 +01002203 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002204 if (!nce) {
2205 ret = -ENOMEM;
2206 goto out;
2207 }
2208
2209 nce->ino = ino;
2210 nce->gen = gen;
2211 nce->parent_ino = *parent_ino;
2212 nce->parent_gen = *parent_gen;
2213 nce->name_len = fs_path_len(dest);
2214 nce->ret = ret;
2215 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002216
2217 if (ino < sctx->send_progress)
2218 nce->need_later_update = 0;
2219 else
2220 nce->need_later_update = 1;
2221
2222 nce_ret = name_cache_insert(sctx, nce);
2223 if (nce_ret < 0)
2224 ret = nce_ret;
2225 name_cache_clean_unused(sctx);
2226
2227out:
Alexander Block31db9f72012-07-25 23:19:24 +02002228 return ret;
2229}
2230
2231/*
2232 * Magic happens here. This function returns the first ref to an inode as it
2233 * would look like while receiving the stream at this point in time.
2234 * We walk the path up to the root. For every inode in between, we check if it
2235 * was already processed/sent. If yes, we continue with the parent as found
2236 * in send_root. If not, we continue with the parent as found in parent_root.
2237 * If we encounter an inode that was deleted at this point in time, we use the
2238 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2239 * that were not created yet and overwritten inodes/refs.
2240 *
Andrea Gelmini52042d82018-11-28 12:05:13 +01002241 * When do we have orphan inodes:
Alexander Block31db9f72012-07-25 23:19:24 +02002242 * 1. When an inode is freshly created and thus no valid refs are available yet
2243 * 2. When a directory lost all it's refs (deleted) but still has dir items
2244 * inside which were not processed yet (pending for move/delete). If anyone
2245 * tried to get the path to the dir items, it would get a path inside that
2246 * orphan directory.
2247 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2248 * of an unprocessed inode. If in that case the first ref would be
2249 * overwritten, the overwritten inode gets "orphanized". Later when we
2250 * process this overwritten inode, it is restored at a new place by moving
2251 * the orphan inode.
2252 *
2253 * sctx->send_progress tells this function at which point in time receiving
2254 * would be.
2255 */
2256static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2257 struct fs_path *dest)
2258{
2259 int ret = 0;
2260 struct fs_path *name = NULL;
2261 u64 parent_inode = 0;
2262 u64 parent_gen = 0;
2263 int stop = 0;
2264
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002265 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002266 if (!name) {
2267 ret = -ENOMEM;
2268 goto out;
2269 }
2270
2271 dest->reversed = 1;
2272 fs_path_reset(dest);
2273
2274 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002275 struct waiting_dir_move *wdm;
2276
Alexander Block31db9f72012-07-25 23:19:24 +02002277 fs_path_reset(name);
2278
Filipe Manana9dc44212014-02-19 14:31:44 +00002279 if (is_waiting_for_rm(sctx, ino)) {
2280 ret = gen_unique_name(sctx, ino, gen, name);
2281 if (ret < 0)
2282 goto out;
2283 ret = fs_path_add_path(dest, name);
2284 break;
2285 }
2286
Filipe Manana8b191a62015-04-09 14:09:14 +01002287 wdm = get_waiting_dir_move(sctx, ino);
2288 if (wdm && wdm->orphanized) {
2289 ret = gen_unique_name(sctx, ino, gen, name);
2290 stop = 1;
2291 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002292 ret = get_first_ref(sctx->parent_root, ino,
2293 &parent_inode, &parent_gen, name);
2294 } else {
2295 ret = __get_cur_name_and_parent(sctx, ino, gen,
2296 &parent_inode,
2297 &parent_gen, name);
2298 if (ret)
2299 stop = 1;
2300 }
2301
Alexander Block31db9f72012-07-25 23:19:24 +02002302 if (ret < 0)
2303 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002304
Alexander Block31db9f72012-07-25 23:19:24 +02002305 ret = fs_path_add_path(dest, name);
2306 if (ret < 0)
2307 goto out;
2308
2309 ino = parent_inode;
2310 gen = parent_gen;
2311 }
2312
2313out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002314 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002315 if (!ret)
2316 fs_path_unreverse(dest);
2317 return ret;
2318}
2319
2320/*
Alexander Block31db9f72012-07-25 23:19:24 +02002321 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2322 */
2323static int send_subvol_begin(struct send_ctx *sctx)
2324{
2325 int ret;
2326 struct btrfs_root *send_root = sctx->send_root;
2327 struct btrfs_root *parent_root = sctx->parent_root;
2328 struct btrfs_path *path;
2329 struct btrfs_key key;
2330 struct btrfs_root_ref *ref;
2331 struct extent_buffer *leaf;
2332 char *name = NULL;
2333 int namelen;
2334
Wang Shilongffcfaf82014-01-15 00:26:43 +08002335 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002336 if (!path)
2337 return -ENOMEM;
2338
David Sterbae780b0d2016-01-18 18:42:13 +01002339 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002340 if (!name) {
2341 btrfs_free_path(path);
2342 return -ENOMEM;
2343 }
2344
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002345 key.objectid = send_root->root_key.objectid;
Alexander Block31db9f72012-07-25 23:19:24 +02002346 key.type = BTRFS_ROOT_BACKREF_KEY;
2347 key.offset = 0;
2348
2349 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2350 &key, path, 1, 0);
2351 if (ret < 0)
2352 goto out;
2353 if (ret) {
2354 ret = -ENOENT;
2355 goto out;
2356 }
2357
2358 leaf = path->nodes[0];
2359 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2360 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002361 key.objectid != send_root->root_key.objectid) {
Alexander Block31db9f72012-07-25 23:19:24 +02002362 ret = -ENOENT;
2363 goto out;
2364 }
2365 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2366 namelen = btrfs_root_ref_name_len(leaf, ref);
2367 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2368 btrfs_release_path(path);
2369
Alexander Block31db9f72012-07-25 23:19:24 +02002370 if (parent_root) {
2371 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2372 if (ret < 0)
2373 goto out;
2374 } else {
2375 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2376 if (ret < 0)
2377 goto out;
2378 }
2379
2380 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002381
2382 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2383 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2384 sctx->send_root->root_item.received_uuid);
2385 else
2386 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2387 sctx->send_root->root_item.uuid);
2388
Alexander Block31db9f72012-07-25 23:19:24 +02002389 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002390 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002391 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002392 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2393 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2394 parent_root->root_item.received_uuid);
2395 else
2396 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2397 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002398 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002399 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002400 }
2401
2402 ret = send_cmd(sctx);
2403
2404tlv_put_failure:
2405out:
2406 btrfs_free_path(path);
2407 kfree(name);
2408 return ret;
2409}
2410
2411static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2412{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002413 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002414 int ret = 0;
2415 struct fs_path *p;
2416
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002417 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002418
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002419 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002420 if (!p)
2421 return -ENOMEM;
2422
2423 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2424 if (ret < 0)
2425 goto out;
2426
2427 ret = get_cur_path(sctx, ino, gen, p);
2428 if (ret < 0)
2429 goto out;
2430 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2431 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2432
2433 ret = send_cmd(sctx);
2434
2435tlv_put_failure:
2436out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002437 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002438 return ret;
2439}
2440
2441static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2442{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002443 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002444 int ret = 0;
2445 struct fs_path *p;
2446
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002447 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002448
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002449 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002450 if (!p)
2451 return -ENOMEM;
2452
2453 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2454 if (ret < 0)
2455 goto out;
2456
2457 ret = get_cur_path(sctx, ino, gen, p);
2458 if (ret < 0)
2459 goto out;
2460 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2461 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2462
2463 ret = send_cmd(sctx);
2464
2465tlv_put_failure:
2466out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002467 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002468 return ret;
2469}
2470
2471static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2472{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002473 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002474 int ret = 0;
2475 struct fs_path *p;
2476
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002477 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2478 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002479
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002480 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002481 if (!p)
2482 return -ENOMEM;
2483
2484 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2485 if (ret < 0)
2486 goto out;
2487
2488 ret = get_cur_path(sctx, ino, gen, p);
2489 if (ret < 0)
2490 goto out;
2491 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2492 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2493 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2494
2495 ret = send_cmd(sctx);
2496
2497tlv_put_failure:
2498out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002499 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002500 return ret;
2501}
2502
2503static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2504{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002505 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002506 int ret = 0;
2507 struct fs_path *p = NULL;
2508 struct btrfs_inode_item *ii;
2509 struct btrfs_path *path = NULL;
2510 struct extent_buffer *eb;
2511 struct btrfs_key key;
2512 int slot;
2513
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002514 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002515
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002516 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002517 if (!p)
2518 return -ENOMEM;
2519
2520 path = alloc_path_for_send();
2521 if (!path) {
2522 ret = -ENOMEM;
2523 goto out;
2524 }
2525
2526 key.objectid = ino;
2527 key.type = BTRFS_INODE_ITEM_KEY;
2528 key.offset = 0;
2529 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002530 if (ret > 0)
2531 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002532 if (ret < 0)
2533 goto out;
2534
2535 eb = path->nodes[0];
2536 slot = path->slots[0];
2537 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2538
2539 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2540 if (ret < 0)
2541 goto out;
2542
2543 ret = get_cur_path(sctx, ino, gen, p);
2544 if (ret < 0)
2545 goto out;
2546 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002547 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2548 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2549 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002550 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002551
2552 ret = send_cmd(sctx);
2553
2554tlv_put_failure:
2555out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002556 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002557 btrfs_free_path(path);
2558 return ret;
2559}
2560
2561/*
2562 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2563 * a valid path yet because we did not process the refs yet. So, the inode
2564 * is created as orphan.
2565 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002566static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002567{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002568 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002569 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002570 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002571 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002572 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002573 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002574 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002575
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002576 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002577
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002578 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002579 if (!p)
2580 return -ENOMEM;
2581
Liu Bo644d1942014-02-27 17:29:01 +08002582 if (ino != sctx->cur_ino) {
2583 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2584 NULL, NULL, &rdev);
2585 if (ret < 0)
2586 goto out;
2587 } else {
2588 gen = sctx->cur_inode_gen;
2589 mode = sctx->cur_inode_mode;
2590 rdev = sctx->cur_inode_rdev;
2591 }
Alexander Block31db9f72012-07-25 23:19:24 +02002592
Alexander Blocke938c8a2012-07-28 16:33:49 +02002593 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002594 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002595 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002596 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002597 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002598 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002599 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002600 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002601 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002602 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002603 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002604 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002605 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002606 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002607 (int)(mode & S_IFMT));
Tsutomu Itohca6842b2016-01-22 09:13:25 +09002608 ret = -EOPNOTSUPP;
Alexander Block31db9f72012-07-25 23:19:24 +02002609 goto out;
2610 }
2611
2612 ret = begin_cmd(sctx, cmd);
2613 if (ret < 0)
2614 goto out;
2615
Alexander Block1f4692d2012-07-28 10:42:24 +02002616 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002617 if (ret < 0)
2618 goto out;
2619
2620 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002621 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002622
2623 if (S_ISLNK(mode)) {
2624 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002625 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002626 if (ret < 0)
2627 goto out;
2628 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2629 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2630 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002631 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2632 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002633 }
2634
2635 ret = send_cmd(sctx);
2636 if (ret < 0)
2637 goto out;
2638
2639
2640tlv_put_failure:
2641out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002642 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002643 return ret;
2644}
2645
Alexander Block1f4692d2012-07-28 10:42:24 +02002646/*
2647 * We need some special handling for inodes that get processed before the parent
2648 * directory got created. See process_recorded_refs for details.
2649 * This function does the check if we already created the dir out of order.
2650 */
2651static int did_create_dir(struct send_ctx *sctx, u64 dir)
2652{
2653 int ret = 0;
2654 struct btrfs_path *path = NULL;
2655 struct btrfs_key key;
2656 struct btrfs_key found_key;
2657 struct btrfs_key di_key;
2658 struct extent_buffer *eb;
2659 struct btrfs_dir_item *di;
2660 int slot;
2661
2662 path = alloc_path_for_send();
2663 if (!path) {
2664 ret = -ENOMEM;
2665 goto out;
2666 }
2667
2668 key.objectid = dir;
2669 key.type = BTRFS_DIR_INDEX_KEY;
2670 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002671 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2672 if (ret < 0)
2673 goto out;
2674
Alexander Block1f4692d2012-07-28 10:42:24 +02002675 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002676 eb = path->nodes[0];
2677 slot = path->slots[0];
2678 if (slot >= btrfs_header_nritems(eb)) {
2679 ret = btrfs_next_leaf(sctx->send_root, path);
2680 if (ret < 0) {
2681 goto out;
2682 } else if (ret > 0) {
2683 ret = 0;
2684 break;
2685 }
2686 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002687 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002688
2689 btrfs_item_key_to_cpu(eb, &found_key, slot);
2690 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002691 found_key.type != key.type) {
2692 ret = 0;
2693 goto out;
2694 }
2695
2696 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2697 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2698
Josef Bacika0525412013-08-12 10:56:14 -04002699 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2700 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002701 ret = 1;
2702 goto out;
2703 }
2704
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002705 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002706 }
2707
2708out:
2709 btrfs_free_path(path);
2710 return ret;
2711}
2712
2713/*
2714 * Only creates the inode if it is:
2715 * 1. Not a directory
2716 * 2. Or a directory which was not created already due to out of order
2717 * directories. See did_create_dir and process_recorded_refs for details.
2718 */
2719static int send_create_inode_if_needed(struct send_ctx *sctx)
2720{
2721 int ret;
2722
2723 if (S_ISDIR(sctx->cur_inode_mode)) {
2724 ret = did_create_dir(sctx, sctx->cur_ino);
2725 if (ret < 0)
2726 goto out;
2727 if (ret) {
2728 ret = 0;
2729 goto out;
2730 }
2731 }
2732
2733 ret = send_create_inode(sctx, sctx->cur_ino);
2734 if (ret < 0)
2735 goto out;
2736
2737out:
2738 return ret;
2739}
2740
Alexander Block31db9f72012-07-25 23:19:24 +02002741struct recorded_ref {
2742 struct list_head list;
Alexander Block31db9f72012-07-25 23:19:24 +02002743 char *name;
2744 struct fs_path *full_path;
2745 u64 dir;
2746 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002747 int name_len;
2748};
2749
Filipe Mananafdb13882017-06-13 14:13:11 +01002750static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2751{
2752 ref->full_path = path;
2753 ref->name = (char *)kbasename(ref->full_path->start);
2754 ref->name_len = ref->full_path->end - ref->name;
2755}
2756
Alexander Block31db9f72012-07-25 23:19:24 +02002757/*
2758 * We need to process new refs before deleted refs, but compare_tree gives us
2759 * everything mixed. So we first record all refs and later process them.
2760 * This function is a helper to record one ref.
2761 */
Liu Boa4d96d62014-03-03 21:31:03 +08002762static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002763 u64 dir_gen, struct fs_path *path)
2764{
2765 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002766
David Sterbae780b0d2016-01-18 18:42:13 +01002767 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002768 if (!ref)
2769 return -ENOMEM;
2770
2771 ref->dir = dir;
2772 ref->dir_gen = dir_gen;
Filipe Mananafdb13882017-06-13 14:13:11 +01002773 set_ref_path(ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02002774 list_add_tail(&ref->list, head);
2775 return 0;
2776}
2777
Josef Bacikba5e8f22013-08-16 16:52:55 -04002778static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2779{
2780 struct recorded_ref *new;
2781
David Sterbae780b0d2016-01-18 18:42:13 +01002782 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002783 if (!new)
2784 return -ENOMEM;
2785
2786 new->dir = ref->dir;
2787 new->dir_gen = ref->dir_gen;
2788 new->full_path = NULL;
2789 INIT_LIST_HEAD(&new->list);
2790 list_add_tail(&new->list, list);
2791 return 0;
2792}
2793
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002794static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002795{
2796 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002797
Alexander Blocke938c8a2012-07-28 16:33:49 +02002798 while (!list_empty(head)) {
2799 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002800 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002801 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002802 kfree(cur);
2803 }
Alexander Block31db9f72012-07-25 23:19:24 +02002804}
2805
2806static void free_recorded_refs(struct send_ctx *sctx)
2807{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002808 __free_recorded_refs(&sctx->new_refs);
2809 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002810}
2811
2812/*
Alexander Block766702e2012-07-28 14:11:31 +02002813 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002814 * ref of an unprocessed inode gets overwritten and for all non empty
2815 * directories.
2816 */
2817static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2818 struct fs_path *path)
2819{
2820 int ret;
2821 struct fs_path *orphan;
2822
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002823 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002824 if (!orphan)
2825 return -ENOMEM;
2826
2827 ret = gen_unique_name(sctx, ino, gen, orphan);
2828 if (ret < 0)
2829 goto out;
2830
2831 ret = send_rename(sctx, path, orphan);
2832
2833out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002834 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002835 return ret;
2836}
2837
Filipe Manana9dc44212014-02-19 14:31:44 +00002838static struct orphan_dir_info *
2839add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2840{
2841 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2842 struct rb_node *parent = NULL;
2843 struct orphan_dir_info *entry, *odi;
2844
Filipe Manana9dc44212014-02-19 14:31:44 +00002845 while (*p) {
2846 parent = *p;
2847 entry = rb_entry(parent, struct orphan_dir_info, node);
2848 if (dir_ino < entry->ino) {
2849 p = &(*p)->rb_left;
2850 } else if (dir_ino > entry->ino) {
2851 p = &(*p)->rb_right;
2852 } else {
Filipe Manana9dc44212014-02-19 14:31:44 +00002853 return entry;
2854 }
2855 }
2856
Robbie Ko35c8eda2018-05-08 18:11:37 +08002857 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2858 if (!odi)
2859 return ERR_PTR(-ENOMEM);
2860 odi->ino = dir_ino;
2861 odi->gen = 0;
Robbie Ko0f96f512018-05-08 18:11:38 +08002862 odi->last_dir_index_offset = 0;
Robbie Ko35c8eda2018-05-08 18:11:37 +08002863
Filipe Manana9dc44212014-02-19 14:31:44 +00002864 rb_link_node(&odi->node, parent, p);
2865 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2866 return odi;
2867}
2868
2869static struct orphan_dir_info *
2870get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2871{
2872 struct rb_node *n = sctx->orphan_dirs.rb_node;
2873 struct orphan_dir_info *entry;
2874
2875 while (n) {
2876 entry = rb_entry(n, struct orphan_dir_info, node);
2877 if (dir_ino < entry->ino)
2878 n = n->rb_left;
2879 else if (dir_ino > entry->ino)
2880 n = n->rb_right;
2881 else
2882 return entry;
2883 }
2884 return NULL;
2885}
2886
2887static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2888{
2889 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2890
2891 return odi != NULL;
2892}
2893
2894static void free_orphan_dir_info(struct send_ctx *sctx,
2895 struct orphan_dir_info *odi)
2896{
2897 if (!odi)
2898 return;
2899 rb_erase(&odi->node, &sctx->orphan_dirs);
2900 kfree(odi);
2901}
2902
Alexander Block31db9f72012-07-25 23:19:24 +02002903/*
2904 * Returns 1 if a directory can be removed at this point in time.
2905 * We check this by iterating all dir items and checking if the inode behind
2906 * the dir item was already processed.
2907 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002908static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2909 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002910{
2911 int ret = 0;
2912 struct btrfs_root *root = sctx->parent_root;
2913 struct btrfs_path *path;
2914 struct btrfs_key key;
2915 struct btrfs_key found_key;
2916 struct btrfs_key loc;
2917 struct btrfs_dir_item *di;
Robbie Ko0f96f512018-05-08 18:11:38 +08002918 struct orphan_dir_info *odi = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02002919
Alexander Block6d85ed02012-08-01 14:48:59 +02002920 /*
2921 * Don't try to rmdir the top/root subvolume dir.
2922 */
2923 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2924 return 0;
2925
Alexander Block31db9f72012-07-25 23:19:24 +02002926 path = alloc_path_for_send();
2927 if (!path)
2928 return -ENOMEM;
2929
2930 key.objectid = dir;
2931 key.type = BTRFS_DIR_INDEX_KEY;
2932 key.offset = 0;
Robbie Ko0f96f512018-05-08 18:11:38 +08002933
2934 odi = get_orphan_dir_info(sctx, dir);
2935 if (odi)
2936 key.offset = odi->last_dir_index_offset;
2937
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002938 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2939 if (ret < 0)
2940 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002941
2942 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002943 struct waiting_dir_move *dm;
2944
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002945 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2946 ret = btrfs_next_leaf(root, path);
2947 if (ret < 0)
2948 goto out;
2949 else if (ret > 0)
2950 break;
2951 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002952 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002953 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2954 path->slots[0]);
2955 if (found_key.objectid != key.objectid ||
2956 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002957 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002958
2959 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2960 struct btrfs_dir_item);
2961 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2962
Filipe Manana9dc44212014-02-19 14:31:44 +00002963 dm = get_waiting_dir_move(sctx, loc.objectid);
2964 if (dm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002965 odi = add_orphan_dir_info(sctx, dir);
2966 if (IS_ERR(odi)) {
2967 ret = PTR_ERR(odi);
2968 goto out;
2969 }
2970 odi->gen = dir_gen;
Robbie Ko0f96f512018-05-08 18:11:38 +08002971 odi->last_dir_index_offset = found_key.offset;
Filipe Manana9dc44212014-02-19 14:31:44 +00002972 dm->rmdir_ino = dir;
2973 ret = 0;
2974 goto out;
2975 }
2976
Alexander Block31db9f72012-07-25 23:19:24 +02002977 if (loc.objectid > send_progress) {
Robbie Ko0f96f512018-05-08 18:11:38 +08002978 odi = add_orphan_dir_info(sctx, dir);
2979 if (IS_ERR(odi)) {
2980 ret = PTR_ERR(odi);
2981 goto out;
2982 }
2983 odi->gen = dir_gen;
2984 odi->last_dir_index_offset = found_key.offset;
Alexander Block31db9f72012-07-25 23:19:24 +02002985 ret = 0;
2986 goto out;
2987 }
2988
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002989 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002990 }
Robbie Ko0f96f512018-05-08 18:11:38 +08002991 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02002992
2993 ret = 1;
2994
2995out:
2996 btrfs_free_path(path);
2997 return ret;
2998}
2999
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003000static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3001{
Filipe Manana9dc44212014-02-19 14:31:44 +00003002 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003003
Filipe Manana9dc44212014-02-19 14:31:44 +00003004 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003005}
3006
Filipe Manana8b191a62015-04-09 14:09:14 +01003007static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003008{
3009 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3010 struct rb_node *parent = NULL;
3011 struct waiting_dir_move *entry, *dm;
3012
David Sterbae780b0d2016-01-18 18:42:13 +01003013 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003014 if (!dm)
3015 return -ENOMEM;
3016 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003017 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003018 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003019
3020 while (*p) {
3021 parent = *p;
3022 entry = rb_entry(parent, struct waiting_dir_move, node);
3023 if (ino < entry->ino) {
3024 p = &(*p)->rb_left;
3025 } else if (ino > entry->ino) {
3026 p = &(*p)->rb_right;
3027 } else {
3028 kfree(dm);
3029 return -EEXIST;
3030 }
3031 }
3032
3033 rb_link_node(&dm->node, parent, p);
3034 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3035 return 0;
3036}
3037
Filipe Manana9dc44212014-02-19 14:31:44 +00003038static struct waiting_dir_move *
3039get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003040{
3041 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3042 struct waiting_dir_move *entry;
3043
3044 while (n) {
3045 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003046 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003047 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003048 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003049 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003050 else
3051 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003052 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003053 return NULL;
3054}
3055
3056static void free_waiting_dir_move(struct send_ctx *sctx,
3057 struct waiting_dir_move *dm)
3058{
3059 if (!dm)
3060 return;
3061 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3062 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003063}
3064
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003065static int add_pending_dir_move(struct send_ctx *sctx,
3066 u64 ino,
3067 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003068 u64 parent_ino,
3069 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003070 struct list_head *deleted_refs,
3071 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003072{
3073 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3074 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003075 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003076 struct recorded_ref *cur;
3077 int exists = 0;
3078 int ret;
3079
David Sterbae780b0d2016-01-18 18:42:13 +01003080 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003081 if (!pm)
3082 return -ENOMEM;
3083 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003084 pm->ino = ino;
3085 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003086 INIT_LIST_HEAD(&pm->list);
3087 INIT_LIST_HEAD(&pm->update_refs);
3088 RB_CLEAR_NODE(&pm->node);
3089
3090 while (*p) {
3091 parent = *p;
3092 entry = rb_entry(parent, struct pending_dir_move, node);
3093 if (parent_ino < entry->parent_ino) {
3094 p = &(*p)->rb_left;
3095 } else if (parent_ino > entry->parent_ino) {
3096 p = &(*p)->rb_right;
3097 } else {
3098 exists = 1;
3099 break;
3100 }
3101 }
3102
Filipe Mananaf9594922014-03-27 20:14:01 +00003103 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003104 ret = dup_ref(cur, &pm->update_refs);
3105 if (ret < 0)
3106 goto out;
3107 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003108 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003109 ret = dup_ref(cur, &pm->update_refs);
3110 if (ret < 0)
3111 goto out;
3112 }
3113
Filipe Manana8b191a62015-04-09 14:09:14 +01003114 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003115 if (ret)
3116 goto out;
3117
3118 if (exists) {
3119 list_add_tail(&pm->list, &entry->list);
3120 } else {
3121 rb_link_node(&pm->node, parent, p);
3122 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3123 }
3124 ret = 0;
3125out:
3126 if (ret) {
3127 __free_recorded_refs(&pm->update_refs);
3128 kfree(pm);
3129 }
3130 return ret;
3131}
3132
3133static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3134 u64 parent_ino)
3135{
3136 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3137 struct pending_dir_move *entry;
3138
3139 while (n) {
3140 entry = rb_entry(n, struct pending_dir_move, node);
3141 if (parent_ino < entry->parent_ino)
3142 n = n->rb_left;
3143 else if (parent_ino > entry->parent_ino)
3144 n = n->rb_right;
3145 else
3146 return entry;
3147 }
3148 return NULL;
3149}
3150
Robbie Ko801bec32015-06-23 18:39:46 +08003151static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3152 u64 ino, u64 gen, u64 *ancestor_ino)
3153{
3154 int ret = 0;
3155 u64 parent_inode = 0;
3156 u64 parent_gen = 0;
3157 u64 start_ino = ino;
3158
3159 *ancestor_ino = 0;
3160 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3161 fs_path_reset(name);
3162
3163 if (is_waiting_for_rm(sctx, ino))
3164 break;
3165 if (is_waiting_for_move(sctx, ino)) {
3166 if (*ancestor_ino == 0)
3167 *ancestor_ino = ino;
3168 ret = get_first_ref(sctx->parent_root, ino,
3169 &parent_inode, &parent_gen, name);
3170 } else {
3171 ret = __get_cur_name_and_parent(sctx, ino, gen,
3172 &parent_inode,
3173 &parent_gen, name);
3174 if (ret > 0) {
3175 ret = 0;
3176 break;
3177 }
3178 }
3179 if (ret < 0)
3180 break;
3181 if (parent_inode == start_ino) {
3182 ret = 1;
3183 if (*ancestor_ino == 0)
3184 *ancestor_ino = ino;
3185 break;
3186 }
3187 ino = parent_inode;
3188 gen = parent_gen;
3189 }
3190 return ret;
3191}
3192
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003193static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3194{
3195 struct fs_path *from_path = NULL;
3196 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003197 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003198 u64 orig_progress = sctx->send_progress;
3199 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003200 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003201 struct waiting_dir_move *dm = NULL;
3202 u64 rmdir_ino = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003203 u64 ancestor;
3204 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003205 int ret;
3206
Filipe Manana2b863a12014-02-16 13:43:11 +00003207 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003208 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003209 if (!name || !from_path) {
3210 ret = -ENOMEM;
3211 goto out;
3212 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003213
Filipe Manana9dc44212014-02-19 14:31:44 +00003214 dm = get_waiting_dir_move(sctx, pm->ino);
3215 ASSERT(dm);
3216 rmdir_ino = dm->rmdir_ino;
Robbie Ko801bec32015-06-23 18:39:46 +08003217 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003218 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003219
Robbie Ko801bec32015-06-23 18:39:46 +08003220 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003221 ret = gen_unique_name(sctx, pm->ino,
3222 pm->gen, from_path);
3223 } else {
3224 ret = get_first_ref(sctx->parent_root, pm->ino,
3225 &parent_ino, &parent_gen, name);
3226 if (ret < 0)
3227 goto out;
3228 ret = get_cur_path(sctx, parent_ino, parent_gen,
3229 from_path);
3230 if (ret < 0)
3231 goto out;
3232 ret = fs_path_add_path(from_path, name);
3233 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003234 if (ret < 0)
3235 goto out;
3236
Filipe Mananaf9594922014-03-27 20:14:01 +00003237 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003238 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003239 if (ret < 0)
3240 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003241 if (ret) {
3242 LIST_HEAD(deleted_refs);
3243 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3244 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3245 &pm->update_refs, &deleted_refs,
3246 is_orphan);
3247 if (ret < 0)
3248 goto out;
3249 if (rmdir_ino) {
3250 dm = get_waiting_dir_move(sctx, pm->ino);
3251 ASSERT(dm);
3252 dm->rmdir_ino = rmdir_ino;
3253 }
3254 goto out;
3255 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003256 fs_path_reset(name);
3257 to_path = name;
3258 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003259 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3260 if (ret < 0)
3261 goto out;
3262
3263 ret = send_rename(sctx, from_path, to_path);
3264 if (ret < 0)
3265 goto out;
3266
Filipe Manana9dc44212014-02-19 14:31:44 +00003267 if (rmdir_ino) {
3268 struct orphan_dir_info *odi;
Robbie Ko0f96f512018-05-08 18:11:38 +08003269 u64 gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003270
3271 odi = get_orphan_dir_info(sctx, rmdir_ino);
3272 if (!odi) {
3273 /* already deleted */
3274 goto finish;
3275 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003276 gen = odi->gen;
3277
3278 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003279 if (ret < 0)
3280 goto out;
3281 if (!ret)
3282 goto finish;
3283
3284 name = fs_path_alloc();
3285 if (!name) {
3286 ret = -ENOMEM;
3287 goto out;
3288 }
Robbie Ko0f96f512018-05-08 18:11:38 +08003289 ret = get_cur_path(sctx, rmdir_ino, gen, name);
Filipe Manana9dc44212014-02-19 14:31:44 +00003290 if (ret < 0)
3291 goto out;
3292 ret = send_rmdir(sctx, name);
3293 if (ret < 0)
3294 goto out;
Filipe Manana9dc44212014-02-19 14:31:44 +00003295 }
3296
3297finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003298 ret = send_utimes(sctx, pm->ino, pm->gen);
3299 if (ret < 0)
3300 goto out;
3301
3302 /*
3303 * After rename/move, need to update the utimes of both new parent(s)
3304 * and old parent(s).
3305 */
3306 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003307 /*
3308 * The parent inode might have been deleted in the send snapshot
3309 */
3310 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3311 NULL, NULL, NULL, NULL, NULL);
3312 if (ret == -ENOENT) {
3313 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003314 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003315 }
3316 if (ret < 0)
3317 goto out;
3318
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003319 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3320 if (ret < 0)
3321 goto out;
3322 }
3323
3324out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003325 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003326 fs_path_free(from_path);
3327 fs_path_free(to_path);
3328 sctx->send_progress = orig_progress;
3329
3330 return ret;
3331}
3332
3333static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3334{
3335 if (!list_empty(&m->list))
3336 list_del(&m->list);
3337 if (!RB_EMPTY_NODE(&m->node))
3338 rb_erase(&m->node, &sctx->pending_dir_moves);
3339 __free_recorded_refs(&m->update_refs);
3340 kfree(m);
3341}
3342
Robbie Koa4390ae2018-11-14 18:32:37 +00003343static void tail_append_pending_moves(struct send_ctx *sctx,
3344 struct pending_dir_move *moves,
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003345 struct list_head *stack)
3346{
3347 if (list_empty(&moves->list)) {
3348 list_add_tail(&moves->list, stack);
3349 } else {
3350 LIST_HEAD(list);
3351 list_splice_init(&moves->list, &list);
3352 list_add_tail(&moves->list, stack);
3353 list_splice_tail(&list, stack);
3354 }
Robbie Koa4390ae2018-11-14 18:32:37 +00003355 if (!RB_EMPTY_NODE(&moves->node)) {
3356 rb_erase(&moves->node, &sctx->pending_dir_moves);
3357 RB_CLEAR_NODE(&moves->node);
3358 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003359}
3360
3361static int apply_children_dir_moves(struct send_ctx *sctx)
3362{
3363 struct pending_dir_move *pm;
3364 struct list_head stack;
3365 u64 parent_ino = sctx->cur_ino;
3366 int ret = 0;
3367
3368 pm = get_pending_dir_moves(sctx, parent_ino);
3369 if (!pm)
3370 return 0;
3371
3372 INIT_LIST_HEAD(&stack);
Robbie Koa4390ae2018-11-14 18:32:37 +00003373 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003374
3375 while (!list_empty(&stack)) {
3376 pm = list_first_entry(&stack, struct pending_dir_move, list);
3377 parent_ino = pm->ino;
3378 ret = apply_dir_move(sctx, pm);
3379 free_pending_move(sctx, pm);
3380 if (ret)
3381 goto out;
3382 pm = get_pending_dir_moves(sctx, parent_ino);
3383 if (pm)
Robbie Koa4390ae2018-11-14 18:32:37 +00003384 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003385 }
3386 return 0;
3387
3388out:
3389 while (!list_empty(&stack)) {
3390 pm = list_first_entry(&stack, struct pending_dir_move, list);
3391 free_pending_move(sctx, pm);
3392 }
3393 return ret;
3394}
3395
Filipe Manana84471e22015-02-28 22:29:22 +00003396/*
3397 * We might need to delay a directory rename even when no ancestor directory
3398 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3399 * renamed. This happens when we rename a directory to the old name (the name
3400 * in the parent root) of some other unrelated directory that got its rename
3401 * delayed due to some ancestor with higher number that got renamed.
3402 *
3403 * Example:
3404 *
3405 * Parent snapshot:
3406 * . (ino 256)
3407 * |---- a/ (ino 257)
3408 * | |---- file (ino 260)
3409 * |
3410 * |---- b/ (ino 258)
3411 * |---- c/ (ino 259)
3412 *
3413 * Send snapshot:
3414 * . (ino 256)
3415 * |---- a/ (ino 258)
3416 * |---- x/ (ino 259)
3417 * |---- y/ (ino 257)
3418 * |----- file (ino 260)
3419 *
3420 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3421 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3422 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3423 * must issue is:
3424 *
3425 * 1 - rename 259 from 'c' to 'x'
3426 * 2 - rename 257 from 'a' to 'x/y'
3427 * 3 - rename 258 from 'b' to 'a'
3428 *
3429 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3430 * be done right away and < 0 on error.
3431 */
3432static int wait_for_dest_dir_move(struct send_ctx *sctx,
3433 struct recorded_ref *parent_ref,
3434 const bool is_orphan)
3435{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003436 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003437 struct btrfs_path *path;
3438 struct btrfs_key key;
3439 struct btrfs_key di_key;
3440 struct btrfs_dir_item *di;
3441 u64 left_gen;
3442 u64 right_gen;
3443 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003444 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003445
3446 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3447 return 0;
3448
3449 path = alloc_path_for_send();
3450 if (!path)
3451 return -ENOMEM;
3452
3453 key.objectid = parent_ref->dir;
3454 key.type = BTRFS_DIR_ITEM_KEY;
3455 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3456
3457 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3458 if (ret < 0) {
3459 goto out;
3460 } else if (ret > 0) {
3461 ret = 0;
3462 goto out;
3463 }
3464
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003465 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3466 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003467 if (!di) {
3468 ret = 0;
3469 goto out;
3470 }
3471 /*
3472 * di_key.objectid has the number of the inode that has a dentry in the
3473 * parent directory with the same name that sctx->cur_ino is being
3474 * renamed to. We need to check if that inode is in the send root as
3475 * well and if it is currently marked as an inode with a pending rename,
3476 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3477 * that it happens after that other inode is renamed.
3478 */
3479 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3480 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3481 ret = 0;
3482 goto out;
3483 }
3484
3485 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3486 &left_gen, NULL, NULL, NULL, NULL);
3487 if (ret < 0)
3488 goto out;
3489 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3490 &right_gen, NULL, NULL, NULL, NULL);
3491 if (ret < 0) {
3492 if (ret == -ENOENT)
3493 ret = 0;
3494 goto out;
3495 }
3496
3497 /* Different inode, no need to delay the rename of sctx->cur_ino */
3498 if (right_gen != left_gen) {
3499 ret = 0;
3500 goto out;
3501 }
3502
Robbie Ko801bec32015-06-23 18:39:46 +08003503 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3504 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003505 ret = add_pending_dir_move(sctx,
3506 sctx->cur_ino,
3507 sctx->cur_inode_gen,
3508 di_key.objectid,
3509 &sctx->new_refs,
3510 &sctx->deleted_refs,
3511 is_orphan);
3512 if (!ret)
3513 ret = 1;
3514 }
3515out:
3516 btrfs_free_path(path);
3517 return ret;
3518}
3519
Filipe Manana80aa6022015-03-27 17:50:45 +00003520/*
Filipe Mananaea37d592017-11-17 01:54:00 +00003521 * Check if inode ino2, or any of its ancestors, is inode ino1.
3522 * Return 1 if true, 0 if false and < 0 on error.
3523 */
3524static int check_ino_in_path(struct btrfs_root *root,
3525 const u64 ino1,
3526 const u64 ino1_gen,
3527 const u64 ino2,
3528 const u64 ino2_gen,
3529 struct fs_path *fs_path)
3530{
3531 u64 ino = ino2;
3532
3533 if (ino1 == ino2)
3534 return ino1_gen == ino2_gen;
3535
3536 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3537 u64 parent;
3538 u64 parent_gen;
3539 int ret;
3540
3541 fs_path_reset(fs_path);
3542 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3543 if (ret < 0)
3544 return ret;
3545 if (parent == ino1)
3546 return parent_gen == ino1_gen;
3547 ino = parent;
3548 }
3549 return 0;
3550}
3551
3552/*
3553 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3554 * possible path (in case ino2 is not a directory and has multiple hard links).
Filipe Manana80aa6022015-03-27 17:50:45 +00003555 * Return 1 if true, 0 if false and < 0 on error.
3556 */
3557static int is_ancestor(struct btrfs_root *root,
3558 const u64 ino1,
3559 const u64 ino1_gen,
3560 const u64 ino2,
3561 struct fs_path *fs_path)
3562{
Filipe Mananaea37d592017-11-17 01:54:00 +00003563 bool free_fs_path = false;
Filipe Manana72c36682017-06-07 11:41:29 +01003564 int ret = 0;
Filipe Mananaea37d592017-11-17 01:54:00 +00003565 struct btrfs_path *path = NULL;
3566 struct btrfs_key key;
Filipe Manana72c36682017-06-07 11:41:29 +01003567
3568 if (!fs_path) {
3569 fs_path = fs_path_alloc();
3570 if (!fs_path)
3571 return -ENOMEM;
Filipe Mananaea37d592017-11-17 01:54:00 +00003572 free_fs_path = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003573 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003574
Filipe Mananaea37d592017-11-17 01:54:00 +00003575 path = alloc_path_for_send();
3576 if (!path) {
3577 ret = -ENOMEM;
3578 goto out;
Filipe Manana80aa6022015-03-27 17:50:45 +00003579 }
Filipe Mananaea37d592017-11-17 01:54:00 +00003580
3581 key.objectid = ino2;
3582 key.type = BTRFS_INODE_REF_KEY;
3583 key.offset = 0;
3584
3585 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3586 if (ret < 0)
3587 goto out;
3588
3589 while (true) {
3590 struct extent_buffer *leaf = path->nodes[0];
3591 int slot = path->slots[0];
3592 u32 cur_offset = 0;
3593 u32 item_size;
3594
3595 if (slot >= btrfs_header_nritems(leaf)) {
3596 ret = btrfs_next_leaf(root, path);
3597 if (ret < 0)
3598 goto out;
3599 if (ret > 0)
3600 break;
3601 continue;
3602 }
3603
3604 btrfs_item_key_to_cpu(leaf, &key, slot);
3605 if (key.objectid != ino2)
3606 break;
3607 if (key.type != BTRFS_INODE_REF_KEY &&
3608 key.type != BTRFS_INODE_EXTREF_KEY)
3609 break;
3610
3611 item_size = btrfs_item_size_nr(leaf, slot);
3612 while (cur_offset < item_size) {
3613 u64 parent;
3614 u64 parent_gen;
3615
3616 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3617 unsigned long ptr;
3618 struct btrfs_inode_extref *extref;
3619
3620 ptr = btrfs_item_ptr_offset(leaf, slot);
3621 extref = (struct btrfs_inode_extref *)
3622 (ptr + cur_offset);
3623 parent = btrfs_inode_extref_parent(leaf,
3624 extref);
3625 cur_offset += sizeof(*extref);
3626 cur_offset += btrfs_inode_extref_name_len(leaf,
3627 extref);
3628 } else {
3629 parent = key.offset;
3630 cur_offset = item_size;
3631 }
3632
3633 ret = get_inode_info(root, parent, NULL, &parent_gen,
3634 NULL, NULL, NULL, NULL);
3635 if (ret < 0)
3636 goto out;
3637 ret = check_ino_in_path(root, ino1, ino1_gen,
3638 parent, parent_gen, fs_path);
3639 if (ret)
3640 goto out;
3641 }
3642 path->slots[0]++;
3643 }
3644 ret = 0;
Filipe Manana72c36682017-06-07 11:41:29 +01003645 out:
Filipe Mananaea37d592017-11-17 01:54:00 +00003646 btrfs_free_path(path);
3647 if (free_fs_path)
Filipe Manana72c36682017-06-07 11:41:29 +01003648 fs_path_free(fs_path);
3649 return ret;
Filipe Manana80aa6022015-03-27 17:50:45 +00003650}
3651
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003652static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003653 struct recorded_ref *parent_ref,
3654 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003655{
Filipe Mananaf9594922014-03-27 20:14:01 +00003656 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003657 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003658 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003659 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003660 struct fs_path *path_before = NULL;
3661 struct fs_path *path_after = NULL;
3662 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003663
3664 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003665 path_before = fs_path_alloc();
3666 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003667 ret = -ENOMEM;
3668 goto out;
3669 }
3670
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003671 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003672 * Our current directory inode may not yet be renamed/moved because some
3673 * ancestor (immediate or not) has to be renamed/moved first. So find if
3674 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003675 * that ancestor is processed to avoid path build infinite loops (done
3676 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003677 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003678 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003679 u64 parent_ino_after_gen;
3680
Filipe Mananaf9594922014-03-27 20:14:01 +00003681 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003682 /*
3683 * If the current inode is an ancestor of ino in the
3684 * parent root, we need to delay the rename of the
3685 * current inode, otherwise don't delayed the rename
3686 * because we can end up with a circular dependency
3687 * of renames, resulting in some directories never
3688 * getting the respective rename operations issued in
3689 * the send stream or getting into infinite path build
3690 * loops.
3691 */
3692 ret = is_ancestor(sctx->parent_root,
3693 sctx->cur_ino, sctx->cur_inode_gen,
3694 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003695 if (ret)
3696 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003697 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003698
3699 fs_path_reset(path_before);
3700 fs_path_reset(path_after);
3701
3702 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003703 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003704 if (ret < 0)
3705 goto out;
3706 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3707 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003708 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003709 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003710 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003711 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003712 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003713 }
3714
3715 len1 = fs_path_len(path_before);
3716 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003717 if (ino > sctx->cur_ino &&
3718 (parent_ino_before != parent_ino_after || len1 != len2 ||
3719 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003720 u64 parent_ino_gen;
3721
3722 ret = get_inode_info(sctx->parent_root, ino, NULL,
3723 &parent_ino_gen, NULL, NULL, NULL,
3724 NULL);
3725 if (ret < 0)
3726 goto out;
3727 if (ino_gen == parent_ino_gen) {
3728 ret = 1;
3729 break;
3730 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003731 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003732 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003733 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003734 }
3735
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003736out:
3737 fs_path_free(path_before);
3738 fs_path_free(path_after);
3739
Filipe Mananaf9594922014-03-27 20:14:01 +00003740 if (ret == 1) {
3741 ret = add_pending_dir_move(sctx,
3742 sctx->cur_ino,
3743 sctx->cur_inode_gen,
3744 ino,
3745 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003746 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003747 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003748 if (!ret)
3749 ret = 1;
3750 }
3751
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003752 return ret;
3753}
3754
Filipe Mananaf5962782017-06-22 20:03:51 +01003755static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3756{
3757 int ret;
3758 struct fs_path *new_path;
3759
3760 /*
3761 * Our reference's name member points to its full_path member string, so
3762 * we use here a new path.
3763 */
3764 new_path = fs_path_alloc();
3765 if (!new_path)
3766 return -ENOMEM;
3767
3768 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3769 if (ret < 0) {
3770 fs_path_free(new_path);
3771 return ret;
3772 }
3773 ret = fs_path_add(new_path, ref->name, ref->name_len);
3774 if (ret < 0) {
3775 fs_path_free(new_path);
3776 return ret;
3777 }
3778
3779 fs_path_free(ref->full_path);
3780 set_ref_path(ref, new_path);
3781
3782 return 0;
3783}
3784
Alexander Block31db9f72012-07-25 23:19:24 +02003785/*
3786 * This does all the move/link/unlink/rmdir magic.
3787 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003788static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003789{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003790 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003791 int ret = 0;
3792 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003793 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003794 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003795 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003796 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003797 u64 ow_gen;
Filipe Mananaf5962782017-06-22 20:03:51 +01003798 u64 ow_mode;
Alexander Block31db9f72012-07-25 23:19:24 +02003799 int did_overwrite = 0;
3800 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003801 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003802 bool can_rename = true;
Filipe Mananaf5962782017-06-22 20:03:51 +01003803 bool orphanized_dir = false;
Filipe Mananafdb13882017-06-13 14:13:11 +01003804 bool orphanized_ancestor = false;
Alexander Block31db9f72012-07-25 23:19:24 +02003805
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003806 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003807
Alexander Block6d85ed02012-08-01 14:48:59 +02003808 /*
3809 * This should never happen as the root dir always has the same ref
3810 * which is always '..'
3811 */
3812 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003813 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003814
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003815 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003816 if (!valid_path) {
3817 ret = -ENOMEM;
3818 goto out;
3819 }
3820
Alexander Block31db9f72012-07-25 23:19:24 +02003821 /*
3822 * First, check if the first ref of the current inode was overwritten
3823 * before. If yes, we know that the current inode was already orphanized
3824 * and thus use the orphan name. If not, we can use get_cur_path to
3825 * get the path of the first ref as it would like while receiving at
3826 * this point in time.
3827 * New inodes are always orphan at the beginning, so force to use the
3828 * orphan name in this case.
3829 * The first ref is stored in valid_path and will be updated if it
3830 * gets moved around.
3831 */
3832 if (!sctx->cur_inode_new) {
3833 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3834 sctx->cur_inode_gen);
3835 if (ret < 0)
3836 goto out;
3837 if (ret)
3838 did_overwrite = 1;
3839 }
3840 if (sctx->cur_inode_new || did_overwrite) {
3841 ret = gen_unique_name(sctx, sctx->cur_ino,
3842 sctx->cur_inode_gen, valid_path);
3843 if (ret < 0)
3844 goto out;
3845 is_orphan = 1;
3846 } else {
3847 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3848 valid_path);
3849 if (ret < 0)
3850 goto out;
3851 }
3852
3853 list_for_each_entry(cur, &sctx->new_refs, list) {
3854 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003855 * We may have refs where the parent directory does not exist
3856 * yet. This happens if the parent directories inum is higher
Andrea Gelmini52042d82018-11-28 12:05:13 +01003857 * than the current inum. To handle this case, we create the
Alexander Block1f4692d2012-07-28 10:42:24 +02003858 * parent directory out of order. But we need to check if this
3859 * did already happen before due to other refs in the same dir.
3860 */
3861 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3862 if (ret < 0)
3863 goto out;
3864 if (ret == inode_state_will_create) {
3865 ret = 0;
3866 /*
3867 * First check if any of the current inodes refs did
3868 * already create the dir.
3869 */
3870 list_for_each_entry(cur2, &sctx->new_refs, list) {
3871 if (cur == cur2)
3872 break;
3873 if (cur2->dir == cur->dir) {
3874 ret = 1;
3875 break;
3876 }
3877 }
3878
3879 /*
3880 * If that did not happen, check if a previous inode
3881 * did already create the dir.
3882 */
3883 if (!ret)
3884 ret = did_create_dir(sctx, cur->dir);
3885 if (ret < 0)
3886 goto out;
3887 if (!ret) {
3888 ret = send_create_inode(sctx, cur->dir);
3889 if (ret < 0)
3890 goto out;
3891 }
3892 }
3893
3894 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003895 * Check if this new ref would overwrite the first ref of
3896 * another unprocessed inode. If yes, orphanize the
3897 * overwritten inode. If we find an overwritten ref that is
3898 * not the first ref, simply unlink it.
3899 */
3900 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3901 cur->name, cur->name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01003902 &ow_inode, &ow_gen, &ow_mode);
Alexander Block31db9f72012-07-25 23:19:24 +02003903 if (ret < 0)
3904 goto out;
3905 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003906 ret = is_first_ref(sctx->parent_root,
3907 ow_inode, cur->dir, cur->name,
3908 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003909 if (ret < 0)
3910 goto out;
3911 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003912 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003913 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003914
Alexander Block31db9f72012-07-25 23:19:24 +02003915 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3916 cur->full_path);
3917 if (ret < 0)
3918 goto out;
Filipe Mananaf5962782017-06-22 20:03:51 +01003919 if (S_ISDIR(ow_mode))
3920 orphanized_dir = true;
Robbie Ko801bec32015-06-23 18:39:46 +08003921
3922 /*
3923 * If ow_inode has its rename operation delayed
3924 * make sure that its orphanized name is used in
3925 * the source path when performing its rename
3926 * operation.
3927 */
3928 if (is_waiting_for_move(sctx, ow_inode)) {
3929 wdm = get_waiting_dir_move(sctx,
3930 ow_inode);
3931 ASSERT(wdm);
3932 wdm->orphanized = true;
3933 }
3934
Filipe Manana8996a482015-03-12 15:16:20 +00003935 /*
3936 * Make sure we clear our orphanized inode's
3937 * name from the name cache. This is because the
3938 * inode ow_inode might be an ancestor of some
3939 * other inode that will be orphanized as well
3940 * later and has an inode number greater than
3941 * sctx->send_progress. We need to prevent
3942 * future name lookups from using the old name
3943 * and get instead the orphan name.
3944 */
3945 nce = name_cache_search(sctx, ow_inode, ow_gen);
3946 if (nce) {
3947 name_cache_delete(sctx, nce);
3948 kfree(nce);
3949 }
Robbie Ko801bec32015-06-23 18:39:46 +08003950
3951 /*
3952 * ow_inode might currently be an ancestor of
3953 * cur_ino, therefore compute valid_path (the
3954 * current path of cur_ino) again because it
3955 * might contain the pre-orphanization name of
3956 * ow_inode, which is no longer valid.
3957 */
Filipe Manana72c36682017-06-07 11:41:29 +01003958 ret = is_ancestor(sctx->parent_root,
3959 ow_inode, ow_gen,
3960 sctx->cur_ino, NULL);
3961 if (ret > 0) {
Filipe Mananafdb13882017-06-13 14:13:11 +01003962 orphanized_ancestor = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003963 fs_path_reset(valid_path);
3964 ret = get_cur_path(sctx, sctx->cur_ino,
3965 sctx->cur_inode_gen,
3966 valid_path);
3967 }
Robbie Ko801bec32015-06-23 18:39:46 +08003968 if (ret < 0)
3969 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003970 } else {
3971 ret = send_unlink(sctx, cur->full_path);
3972 if (ret < 0)
3973 goto out;
3974 }
3975 }
3976
Filipe Manana84471e22015-02-28 22:29:22 +00003977 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3978 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3979 if (ret < 0)
3980 goto out;
3981 if (ret == 1) {
3982 can_rename = false;
3983 *pending_move = 1;
3984 }
3985 }
3986
Filipe Manana8b191a62015-04-09 14:09:14 +01003987 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3988 can_rename) {
3989 ret = wait_for_parent_move(sctx, cur, is_orphan);
3990 if (ret < 0)
3991 goto out;
3992 if (ret == 1) {
3993 can_rename = false;
3994 *pending_move = 1;
3995 }
3996 }
3997
Alexander Block31db9f72012-07-25 23:19:24 +02003998 /*
3999 * link/move the ref to the new place. If we have an orphan
4000 * inode, move it and update valid_path. If not, link or move
4001 * it depending on the inode mode.
4002 */
Filipe Manana84471e22015-02-28 22:29:22 +00004003 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004004 ret = send_rename(sctx, valid_path, cur->full_path);
4005 if (ret < 0)
4006 goto out;
4007 is_orphan = 0;
4008 ret = fs_path_copy(valid_path, cur->full_path);
4009 if (ret < 0)
4010 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00004011 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004012 if (S_ISDIR(sctx->cur_inode_mode)) {
4013 /*
4014 * Dirs can't be linked, so move it. For moved
4015 * dirs, we always have one new and one deleted
4016 * ref. The deleted ref is ignored later.
4017 */
Filipe Manana8b191a62015-04-09 14:09:14 +01004018 ret = send_rename(sctx, valid_path,
4019 cur->full_path);
4020 if (!ret)
4021 ret = fs_path_copy(valid_path,
4022 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004023 if (ret < 0)
4024 goto out;
4025 } else {
Filipe Mananaf5962782017-06-22 20:03:51 +01004026 /*
4027 * We might have previously orphanized an inode
4028 * which is an ancestor of our current inode,
4029 * so our reference's full path, which was
4030 * computed before any such orphanizations, must
4031 * be updated.
4032 */
4033 if (orphanized_dir) {
4034 ret = update_ref_path(sctx, cur);
4035 if (ret < 0)
4036 goto out;
4037 }
Alexander Block31db9f72012-07-25 23:19:24 +02004038 ret = send_link(sctx, cur->full_path,
4039 valid_path);
4040 if (ret < 0)
4041 goto out;
4042 }
4043 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004044 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004045 if (ret < 0)
4046 goto out;
4047 }
4048
4049 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4050 /*
4051 * Check if we can already rmdir the directory. If not,
4052 * orphanize it. For every dir item inside that gets deleted
4053 * later, we do this check again and rmdir it then if possible.
4054 * See the use of check_dirs for more details.
4055 */
Filipe Manana9dc44212014-02-19 14:31:44 +00004056 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4057 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004058 if (ret < 0)
4059 goto out;
4060 if (ret) {
4061 ret = send_rmdir(sctx, valid_path);
4062 if (ret < 0)
4063 goto out;
4064 } else if (!is_orphan) {
4065 ret = orphanize_inode(sctx, sctx->cur_ino,
4066 sctx->cur_inode_gen, valid_path);
4067 if (ret < 0)
4068 goto out;
4069 is_orphan = 1;
4070 }
4071
4072 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004073 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004074 if (ret < 0)
4075 goto out;
4076 }
Alexander Blockccf16262012-07-28 11:46:29 +02004077 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4078 !list_empty(&sctx->deleted_refs)) {
4079 /*
4080 * We have a moved dir. Add the old parent to check_dirs
4081 */
4082 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4083 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04004084 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02004085 if (ret < 0)
4086 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004087 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4088 /*
4089 * We have a non dir inode. Go through all deleted refs and
4090 * unlink them if they were not already overwritten by other
4091 * inodes.
4092 */
4093 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4094 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4095 sctx->cur_ino, sctx->cur_inode_gen,
4096 cur->name, cur->name_len);
4097 if (ret < 0)
4098 goto out;
4099 if (!ret) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004100 /*
4101 * If we orphanized any ancestor before, we need
4102 * to recompute the full path for deleted names,
4103 * since any such path was computed before we
4104 * processed any references and orphanized any
4105 * ancestor inode.
4106 */
4107 if (orphanized_ancestor) {
Filipe Mananaf5962782017-06-22 20:03:51 +01004108 ret = update_ref_path(sctx, cur);
4109 if (ret < 0)
Filipe Mananafdb13882017-06-13 14:13:11 +01004110 goto out;
Filipe Mananafdb13882017-06-13 14:13:11 +01004111 }
Alexander Block1f4692d2012-07-28 10:42:24 +02004112 ret = send_unlink(sctx, cur->full_path);
4113 if (ret < 0)
4114 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004115 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004116 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004117 if (ret < 0)
4118 goto out;
4119 }
Alexander Block31db9f72012-07-25 23:19:24 +02004120 /*
4121 * If the inode is still orphan, unlink the orphan. This may
4122 * happen when a previous inode did overwrite the first ref
4123 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02004124 * inode. Unlinking does not mean that the inode is deleted in
4125 * all cases. There may still be links to this inode in other
4126 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02004127 */
Alexander Block1f4692d2012-07-28 10:42:24 +02004128 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02004129 ret = send_unlink(sctx, valid_path);
4130 if (ret < 0)
4131 goto out;
4132 }
4133 }
4134
4135 /*
4136 * We did collect all parent dirs where cur_inode was once located. We
4137 * now go through all these dirs and check if they are pending for
4138 * deletion and if it's finally possible to perform the rmdir now.
4139 * We also update the inode stats of the parent dirs here.
4140 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004141 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02004142 /*
4143 * In case we had refs into dirs that were not processed yet,
4144 * we don't need to do the utime and rmdir logic for these dirs.
4145 * The dir will be processed later.
4146 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004147 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02004148 continue;
4149
Josef Bacikba5e8f22013-08-16 16:52:55 -04004150 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004151 if (ret < 0)
4152 goto out;
4153
4154 if (ret == inode_state_did_create ||
4155 ret == inode_state_no_change) {
4156 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004157 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004158 if (ret < 0)
4159 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004160 } else if (ret == inode_state_did_delete &&
4161 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004162 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4163 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004164 if (ret < 0)
4165 goto out;
4166 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004167 ret = get_cur_path(sctx, cur->dir,
4168 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004169 if (ret < 0)
4170 goto out;
4171 ret = send_rmdir(sctx, valid_path);
4172 if (ret < 0)
4173 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004174 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004175 }
4176 }
4177 }
4178
Alexander Block31db9f72012-07-25 23:19:24 +02004179 ret = 0;
4180
4181out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004182 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004183 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004184 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004185 return ret;
4186}
4187
Nikolay Borisova0357512017-08-21 12:43:44 +03004188static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4189 void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004190{
4191 int ret = 0;
4192 struct send_ctx *sctx = ctx;
4193 struct fs_path *p;
4194 u64 gen;
4195
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004196 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004197 if (!p)
4198 return -ENOMEM;
4199
Liu Boa4d96d62014-03-03 21:31:03 +08004200 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004201 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004202 if (ret < 0)
4203 goto out;
4204
Alexander Block31db9f72012-07-25 23:19:24 +02004205 ret = get_cur_path(sctx, dir, gen, p);
4206 if (ret < 0)
4207 goto out;
4208 ret = fs_path_add_path(p, name);
4209 if (ret < 0)
4210 goto out;
4211
Liu Boa4d96d62014-03-03 21:31:03 +08004212 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004213
4214out:
4215 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004216 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004217 return ret;
4218}
4219
Liu Boa4d96d62014-03-03 21:31:03 +08004220static int __record_new_ref(int num, u64 dir, int index,
4221 struct fs_path *name,
4222 void *ctx)
4223{
4224 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004225 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
Liu Boa4d96d62014-03-03 21:31:03 +08004226}
4227
4228
Alexander Block31db9f72012-07-25 23:19:24 +02004229static int __record_deleted_ref(int num, u64 dir, int index,
4230 struct fs_path *name,
4231 void *ctx)
4232{
Alexander Block31db9f72012-07-25 23:19:24 +02004233 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004234 return record_ref(sctx->parent_root, dir, name, ctx,
4235 &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004236}
4237
4238static int record_new_ref(struct send_ctx *sctx)
4239{
4240 int ret;
4241
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004242 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4243 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004244 if (ret < 0)
4245 goto out;
4246 ret = 0;
4247
4248out:
4249 return ret;
4250}
4251
4252static int record_deleted_ref(struct send_ctx *sctx)
4253{
4254 int ret;
4255
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004256 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4257 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004258 if (ret < 0)
4259 goto out;
4260 ret = 0;
4261
4262out:
4263 return ret;
4264}
4265
4266struct find_ref_ctx {
4267 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004268 u64 dir_gen;
4269 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004270 struct fs_path *name;
4271 int found_idx;
4272};
4273
4274static int __find_iref(int num, u64 dir, int index,
4275 struct fs_path *name,
4276 void *ctx_)
4277{
4278 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004279 u64 dir_gen;
4280 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004281
4282 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4283 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004284 /*
4285 * To avoid doing extra lookups we'll only do this if everything
4286 * else matches.
4287 */
4288 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4289 NULL, NULL, NULL);
4290 if (ret)
4291 return ret;
4292 if (dir_gen != ctx->dir_gen)
4293 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004294 ctx->found_idx = num;
4295 return 1;
4296 }
4297 return 0;
4298}
4299
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004300static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004301 struct btrfs_path *path,
4302 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004303 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004304{
4305 int ret;
4306 struct find_ref_ctx ctx;
4307
4308 ctx.dir = dir;
4309 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004310 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004311 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004312 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004313
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004314 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004315 if (ret < 0)
4316 return ret;
4317
4318 if (ctx.found_idx == -1)
4319 return -ENOENT;
4320
4321 return ctx.found_idx;
4322}
4323
4324static int __record_changed_new_ref(int num, u64 dir, int index,
4325 struct fs_path *name,
4326 void *ctx)
4327{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004328 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004329 int ret;
4330 struct send_ctx *sctx = ctx;
4331
Josef Bacikba5e8f22013-08-16 16:52:55 -04004332 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4333 NULL, NULL, NULL);
4334 if (ret)
4335 return ret;
4336
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004337 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004338 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004339 if (ret == -ENOENT)
4340 ret = __record_new_ref(num, dir, index, name, sctx);
4341 else if (ret > 0)
4342 ret = 0;
4343
4344 return ret;
4345}
4346
4347static int __record_changed_deleted_ref(int num, u64 dir, int index,
4348 struct fs_path *name,
4349 void *ctx)
4350{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004351 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004352 int ret;
4353 struct send_ctx *sctx = ctx;
4354
Josef Bacikba5e8f22013-08-16 16:52:55 -04004355 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4356 NULL, NULL, NULL);
4357 if (ret)
4358 return ret;
4359
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004360 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004361 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004362 if (ret == -ENOENT)
4363 ret = __record_deleted_ref(num, dir, index, name, sctx);
4364 else if (ret > 0)
4365 ret = 0;
4366
4367 return ret;
4368}
4369
4370static int record_changed_ref(struct send_ctx *sctx)
4371{
4372 int ret = 0;
4373
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004374 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004375 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4376 if (ret < 0)
4377 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004378 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004379 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4380 if (ret < 0)
4381 goto out;
4382 ret = 0;
4383
4384out:
4385 return ret;
4386}
4387
4388/*
4389 * Record and process all refs at once. Needed when an inode changes the
4390 * generation number, which means that it was deleted and recreated.
4391 */
4392static int process_all_refs(struct send_ctx *sctx,
4393 enum btrfs_compare_tree_result cmd)
4394{
4395 int ret;
4396 struct btrfs_root *root;
4397 struct btrfs_path *path;
4398 struct btrfs_key key;
4399 struct btrfs_key found_key;
4400 struct extent_buffer *eb;
4401 int slot;
4402 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004403 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004404
4405 path = alloc_path_for_send();
4406 if (!path)
4407 return -ENOMEM;
4408
4409 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4410 root = sctx->send_root;
4411 cb = __record_new_ref;
4412 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4413 root = sctx->parent_root;
4414 cb = __record_deleted_ref;
4415 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004416 btrfs_err(sctx->send_root->fs_info,
4417 "Wrong command %d in process_all_refs", cmd);
4418 ret = -EINVAL;
4419 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004420 }
4421
4422 key.objectid = sctx->cmp_key->objectid;
4423 key.type = BTRFS_INODE_REF_KEY;
4424 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004425 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4426 if (ret < 0)
4427 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004428
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004429 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004430 eb = path->nodes[0];
4431 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004432 if (slot >= btrfs_header_nritems(eb)) {
4433 ret = btrfs_next_leaf(root, path);
4434 if (ret < 0)
4435 goto out;
4436 else if (ret > 0)
4437 break;
4438 continue;
4439 }
4440
Alexander Block31db9f72012-07-25 23:19:24 +02004441 btrfs_item_key_to_cpu(eb, &found_key, slot);
4442
4443 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004444 (found_key.type != BTRFS_INODE_REF_KEY &&
4445 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004446 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004447
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004448 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004449 if (ret < 0)
4450 goto out;
4451
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004452 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004453 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004454 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004455
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004456 /*
4457 * We don't actually care about pending_move as we are simply
4458 * re-creating this inode and will be rename'ing it into place once we
4459 * rename the parent directory.
4460 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004461 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004462out:
4463 btrfs_free_path(path);
4464 return ret;
4465}
4466
4467static int send_set_xattr(struct send_ctx *sctx,
4468 struct fs_path *path,
4469 const char *name, int name_len,
4470 const char *data, int data_len)
4471{
4472 int ret = 0;
4473
4474 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4475 if (ret < 0)
4476 goto out;
4477
4478 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4479 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4480 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4481
4482 ret = send_cmd(sctx);
4483
4484tlv_put_failure:
4485out:
4486 return ret;
4487}
4488
4489static int send_remove_xattr(struct send_ctx *sctx,
4490 struct fs_path *path,
4491 const char *name, int name_len)
4492{
4493 int ret = 0;
4494
4495 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4496 if (ret < 0)
4497 goto out;
4498
4499 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4500 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4501
4502 ret = send_cmd(sctx);
4503
4504tlv_put_failure:
4505out:
4506 return ret;
4507}
4508
4509static int __process_new_xattr(int num, struct btrfs_key *di_key,
4510 const char *name, int name_len,
4511 const char *data, int data_len,
4512 u8 type, void *ctx)
4513{
4514 int ret;
4515 struct send_ctx *sctx = ctx;
4516 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004517 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004518
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004519 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004520 if (!p)
4521 return -ENOMEM;
4522
4523 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004524 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004525 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004526 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004527 * only contains the version number and no entries.
4528 */
4529 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4530 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4531 if (data_len == 0) {
4532 dummy_acl.a_version =
4533 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4534 data = (char *)&dummy_acl;
4535 data_len = sizeof(dummy_acl);
4536 }
4537 }
4538
4539 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4540 if (ret < 0)
4541 goto out;
4542
4543 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4544
4545out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004546 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004547 return ret;
4548}
4549
4550static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4551 const char *name, int name_len,
4552 const char *data, int data_len,
4553 u8 type, void *ctx)
4554{
4555 int ret;
4556 struct send_ctx *sctx = ctx;
4557 struct fs_path *p;
4558
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004559 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004560 if (!p)
4561 return -ENOMEM;
4562
4563 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4564 if (ret < 0)
4565 goto out;
4566
4567 ret = send_remove_xattr(sctx, p, name, name_len);
4568
4569out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004570 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004571 return ret;
4572}
4573
4574static int process_new_xattr(struct send_ctx *sctx)
4575{
4576 int ret = 0;
4577
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004578 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004579 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004580
4581 return ret;
4582}
4583
4584static int process_deleted_xattr(struct send_ctx *sctx)
4585{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004586 return iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004587 __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004588}
4589
4590struct find_xattr_ctx {
4591 const char *name;
4592 int name_len;
4593 int found_idx;
4594 char *found_data;
4595 int found_data_len;
4596};
4597
4598static int __find_xattr(int num, struct btrfs_key *di_key,
4599 const char *name, int name_len,
4600 const char *data, int data_len,
4601 u8 type, void *vctx)
4602{
4603 struct find_xattr_ctx *ctx = vctx;
4604
4605 if (name_len == ctx->name_len &&
4606 strncmp(name, ctx->name, name_len) == 0) {
4607 ctx->found_idx = num;
4608 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004609 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004610 if (!ctx->found_data)
4611 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004612 return 1;
4613 }
4614 return 0;
4615}
4616
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004617static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004618 struct btrfs_path *path,
4619 struct btrfs_key *key,
4620 const char *name, int name_len,
4621 char **data, int *data_len)
4622{
4623 int ret;
4624 struct find_xattr_ctx ctx;
4625
4626 ctx.name = name;
4627 ctx.name_len = name_len;
4628 ctx.found_idx = -1;
4629 ctx.found_data = NULL;
4630 ctx.found_data_len = 0;
4631
Nikolay Borisova0357512017-08-21 12:43:44 +03004632 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004633 if (ret < 0)
4634 return ret;
4635
4636 if (ctx.found_idx == -1)
4637 return -ENOENT;
4638 if (data) {
4639 *data = ctx.found_data;
4640 *data_len = ctx.found_data_len;
4641 } else {
4642 kfree(ctx.found_data);
4643 }
4644 return ctx.found_idx;
4645}
4646
4647
4648static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4649 const char *name, int name_len,
4650 const char *data, int data_len,
4651 u8 type, void *ctx)
4652{
4653 int ret;
4654 struct send_ctx *sctx = ctx;
4655 char *found_data = NULL;
4656 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004657
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004658 ret = find_xattr(sctx->parent_root, sctx->right_path,
4659 sctx->cmp_key, name, name_len, &found_data,
4660 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004661 if (ret == -ENOENT) {
4662 ret = __process_new_xattr(num, di_key, name, name_len, data,
4663 data_len, type, ctx);
4664 } else if (ret >= 0) {
4665 if (data_len != found_data_len ||
4666 memcmp(data, found_data, data_len)) {
4667 ret = __process_new_xattr(num, di_key, name, name_len,
4668 data, data_len, type, ctx);
4669 } else {
4670 ret = 0;
4671 }
4672 }
4673
4674 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004675 return ret;
4676}
4677
4678static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4679 const char *name, int name_len,
4680 const char *data, int data_len,
4681 u8 type, void *ctx)
4682{
4683 int ret;
4684 struct send_ctx *sctx = ctx;
4685
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004686 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4687 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004688 if (ret == -ENOENT)
4689 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4690 data_len, type, ctx);
4691 else if (ret >= 0)
4692 ret = 0;
4693
4694 return ret;
4695}
4696
4697static int process_changed_xattr(struct send_ctx *sctx)
4698{
4699 int ret = 0;
4700
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004701 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004702 __process_changed_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004703 if (ret < 0)
4704 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004705 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004706 __process_changed_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004707
4708out:
4709 return ret;
4710}
4711
4712static int process_all_new_xattrs(struct send_ctx *sctx)
4713{
4714 int ret;
4715 struct btrfs_root *root;
4716 struct btrfs_path *path;
4717 struct btrfs_key key;
4718 struct btrfs_key found_key;
4719 struct extent_buffer *eb;
4720 int slot;
4721
4722 path = alloc_path_for_send();
4723 if (!path)
4724 return -ENOMEM;
4725
4726 root = sctx->send_root;
4727
4728 key.objectid = sctx->cmp_key->objectid;
4729 key.type = BTRFS_XATTR_ITEM_KEY;
4730 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004731 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4732 if (ret < 0)
4733 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004734
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004735 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004736 eb = path->nodes[0];
4737 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004738 if (slot >= btrfs_header_nritems(eb)) {
4739 ret = btrfs_next_leaf(root, path);
4740 if (ret < 0) {
4741 goto out;
4742 } else if (ret > 0) {
4743 ret = 0;
4744 break;
4745 }
4746 continue;
4747 }
Alexander Block31db9f72012-07-25 23:19:24 +02004748
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004749 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004750 if (found_key.objectid != key.objectid ||
4751 found_key.type != key.type) {
4752 ret = 0;
4753 goto out;
4754 }
4755
Nikolay Borisova0357512017-08-21 12:43:44 +03004756 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004757 if (ret < 0)
4758 goto out;
4759
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004760 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004761 }
4762
4763out:
4764 btrfs_free_path(path);
4765 return ret;
4766}
4767
Josef Baciked259092013-10-25 11:36:01 -04004768static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4769{
4770 struct btrfs_root *root = sctx->send_root;
4771 struct btrfs_fs_info *fs_info = root->fs_info;
4772 struct inode *inode;
4773 struct page *page;
4774 char *addr;
4775 struct btrfs_key key;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004776 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004777 pgoff_t last_index;
Johannes Thumshirn70730172018-12-05 15:23:03 +01004778 unsigned pg_offset = offset_in_page(offset);
Josef Baciked259092013-10-25 11:36:01 -04004779 ssize_t ret = 0;
4780
4781 key.objectid = sctx->cur_ino;
4782 key.type = BTRFS_INODE_ITEM_KEY;
4783 key.offset = 0;
4784
4785 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4786 if (IS_ERR(inode))
4787 return PTR_ERR(inode);
4788
4789 if (offset + len > i_size_read(inode)) {
4790 if (offset > i_size_read(inode))
4791 len = 0;
4792 else
4793 len = offset - i_size_read(inode);
4794 }
4795 if (len == 0)
4796 goto out;
4797
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004798 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004799
4800 /* initial readahead */
4801 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4802 file_ra_state_init(&sctx->ra, inode->i_mapping);
Liu Bo2131bcd2014-03-05 10:07:35 +08004803
Josef Baciked259092013-10-25 11:36:01 -04004804 while (index <= last_index) {
4805 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004806 PAGE_SIZE - pg_offset);
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004807
4808 page = find_lock_page(inode->i_mapping, index);
Josef Baciked259092013-10-25 11:36:01 -04004809 if (!page) {
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004810 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4811 NULL, index, last_index + 1 - index);
4812
4813 page = find_or_create_page(inode->i_mapping, index,
4814 GFP_KERNEL);
4815 if (!page) {
4816 ret = -ENOMEM;
4817 break;
4818 }
4819 }
4820
4821 if (PageReadahead(page)) {
4822 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4823 NULL, page, index, last_index + 1 - index);
Josef Baciked259092013-10-25 11:36:01 -04004824 }
4825
4826 if (!PageUptodate(page)) {
4827 btrfs_readpage(NULL, page);
4828 lock_page(page);
4829 if (!PageUptodate(page)) {
4830 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004831 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004832 ret = -EIO;
4833 break;
4834 }
4835 }
4836
4837 addr = kmap(page);
4838 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4839 kunmap(page);
4840 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004841 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004842 index++;
4843 pg_offset = 0;
4844 len -= cur_len;
4845 ret += cur_len;
4846 }
4847out:
4848 iput(inode);
4849 return ret;
4850}
4851
Alexander Block31db9f72012-07-25 23:19:24 +02004852/*
4853 * Read some bytes from the current inode/file and send a write command to
4854 * user space.
4855 */
4856static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4857{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004858 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02004859 int ret = 0;
4860 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004861 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004862
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004863 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004864 if (!p)
4865 return -ENOMEM;
4866
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004867 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004868
Josef Baciked259092013-10-25 11:36:01 -04004869 num_read = fill_read_buf(sctx, offset, len);
4870 if (num_read <= 0) {
4871 if (num_read < 0)
4872 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004873 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004874 }
Alexander Block31db9f72012-07-25 23:19:24 +02004875
4876 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4877 if (ret < 0)
4878 goto out;
4879
4880 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4881 if (ret < 0)
4882 goto out;
4883
4884 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4885 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004886 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004887
4888 ret = send_cmd(sctx);
4889
4890tlv_put_failure:
4891out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004892 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004893 if (ret < 0)
4894 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004895 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004896}
4897
4898/*
4899 * Send a clone command to user space.
4900 */
4901static int send_clone(struct send_ctx *sctx,
4902 u64 offset, u32 len,
4903 struct clone_root *clone_root)
4904{
4905 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004906 struct fs_path *p;
4907 u64 gen;
4908
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004909 btrfs_debug(sctx->send_root->fs_info,
4910 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09004911 offset, len, clone_root->root->root_key.objectid,
4912 clone_root->ino, clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02004913
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004914 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004915 if (!p)
4916 return -ENOMEM;
4917
4918 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4919 if (ret < 0)
4920 goto out;
4921
4922 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4923 if (ret < 0)
4924 goto out;
4925
4926 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4927 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4928 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4929
Alexander Blocke938c8a2012-07-28 16:33:49 +02004930 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004931 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004932 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004933 if (ret < 0)
4934 goto out;
4935 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4936 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004937 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004938 }
4939 if (ret < 0)
4940 goto out;
4941
Josef Bacik37b8d272015-06-04 17:17:25 -04004942 /*
4943 * If the parent we're using has a received_uuid set then use that as
4944 * our clone source as that is what we will look for when doing a
4945 * receive.
4946 *
4947 * This covers the case that we create a snapshot off of a received
4948 * subvolume and then use that as the parent and try to receive on a
4949 * different host.
4950 */
4951 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4952 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4953 clone_root->root->root_item.received_uuid);
4954 else
4955 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4956 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004957 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004958 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004959 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4960 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4961 clone_root->offset);
4962
4963 ret = send_cmd(sctx);
4964
4965tlv_put_failure:
4966out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004967 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004968 return ret;
4969}
4970
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004971/*
4972 * Send an update extent command to user space.
4973 */
4974static int send_update_extent(struct send_ctx *sctx,
4975 u64 offset, u32 len)
4976{
4977 int ret = 0;
4978 struct fs_path *p;
4979
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004980 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004981 if (!p)
4982 return -ENOMEM;
4983
4984 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4985 if (ret < 0)
4986 goto out;
4987
4988 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4989 if (ret < 0)
4990 goto out;
4991
4992 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4993 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4994 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4995
4996 ret = send_cmd(sctx);
4997
4998tlv_put_failure:
4999out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00005000 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005001 return ret;
5002}
5003
Josef Bacik16e75492013-10-22 12:18:51 -04005004static int send_hole(struct send_ctx *sctx, u64 end)
5005{
5006 struct fs_path *p = NULL;
5007 u64 offset = sctx->cur_inode_last_extent;
5008 u64 len;
5009 int ret = 0;
5010
Filipe Manana22d31512018-07-30 12:39:58 +01005011 /*
5012 * A hole that starts at EOF or beyond it. Since we do not yet support
5013 * fallocate (for extent preallocation and hole punching), sending a
5014 * write of zeroes starting at EOF or beyond would later require issuing
5015 * a truncate operation which would undo the write and achieve nothing.
5016 */
5017 if (offset >= sctx->cur_inode_size)
5018 return 0;
5019
Filipe Mananad4dfc0f2018-02-06 20:39:20 +00005020 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5021 return send_update_extent(sctx, offset, end - offset);
5022
Josef Bacik16e75492013-10-22 12:18:51 -04005023 p = fs_path_alloc();
5024 if (!p)
5025 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01005026 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5027 if (ret < 0)
5028 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04005029 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
5030 while (offset < end) {
5031 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
5032
5033 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5034 if (ret < 0)
5035 break;
Josef Bacik16e75492013-10-22 12:18:51 -04005036 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5037 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5038 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
5039 ret = send_cmd(sctx);
5040 if (ret < 0)
5041 break;
5042 offset += len;
5043 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005044 sctx->cur_inode_next_write_offset = offset;
Josef Bacik16e75492013-10-22 12:18:51 -04005045tlv_put_failure:
5046 fs_path_free(p);
5047 return ret;
5048}
5049
Filipe Mananad906d492015-10-02 10:47:34 +01005050static int send_extent_data(struct send_ctx *sctx,
5051 const u64 offset,
5052 const u64 len)
5053{
5054 u64 sent = 0;
5055
5056 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5057 return send_update_extent(sctx, offset, len);
5058
5059 while (sent < len) {
5060 u64 size = len - sent;
5061 int ret;
5062
5063 if (size > BTRFS_SEND_READ_SIZE)
5064 size = BTRFS_SEND_READ_SIZE;
5065 ret = send_write(sctx, offset + sent, size);
5066 if (ret < 0)
5067 return ret;
5068 if (!ret)
5069 break;
5070 sent += ret;
5071 }
5072 return 0;
5073}
5074
5075static int clone_range(struct send_ctx *sctx,
5076 struct clone_root *clone_root,
5077 const u64 disk_byte,
5078 u64 data_offset,
5079 u64 offset,
5080 u64 len)
5081{
5082 struct btrfs_path *path;
5083 struct btrfs_key key;
5084 int ret;
5085
Filipe Manana72610b12017-08-10 22:54:51 +01005086 /*
5087 * Prevent cloning from a zero offset with a length matching the sector
5088 * size because in some scenarios this will make the receiver fail.
5089 *
5090 * For example, if in the source filesystem the extent at offset 0
5091 * has a length of sectorsize and it was written using direct IO, then
5092 * it can never be an inline extent (even if compression is enabled).
5093 * Then this extent can be cloned in the original filesystem to a non
5094 * zero file offset, but it may not be possible to clone in the
5095 * destination filesystem because it can be inlined due to compression
5096 * on the destination filesystem (as the receiver's write operations are
5097 * always done using buffered IO). The same happens when the original
5098 * filesystem does not have compression enabled but the destination
5099 * filesystem has.
5100 */
5101 if (clone_root->offset == 0 &&
5102 len == sctx->send_root->fs_info->sectorsize)
5103 return send_extent_data(sctx, offset, len);
5104
Filipe Mananad906d492015-10-02 10:47:34 +01005105 path = alloc_path_for_send();
5106 if (!path)
5107 return -ENOMEM;
5108
5109 /*
5110 * We can't send a clone operation for the entire range if we find
5111 * extent items in the respective range in the source file that
5112 * refer to different extents or if we find holes.
5113 * So check for that and do a mix of clone and regular write/copy
5114 * operations if needed.
5115 *
5116 * Example:
5117 *
5118 * mkfs.btrfs -f /dev/sda
5119 * mount /dev/sda /mnt
5120 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5121 * cp --reflink=always /mnt/foo /mnt/bar
5122 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5123 * btrfs subvolume snapshot -r /mnt /mnt/snap
5124 *
5125 * If when we send the snapshot and we are processing file bar (which
5126 * has a higher inode number than foo) we blindly send a clone operation
5127 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5128 * a file bar that matches the content of file foo - iow, doesn't match
5129 * the content from bar in the original filesystem.
5130 */
5131 key.objectid = clone_root->ino;
5132 key.type = BTRFS_EXTENT_DATA_KEY;
5133 key.offset = clone_root->offset;
5134 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5135 if (ret < 0)
5136 goto out;
5137 if (ret > 0 && path->slots[0] > 0) {
5138 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5139 if (key.objectid == clone_root->ino &&
5140 key.type == BTRFS_EXTENT_DATA_KEY)
5141 path->slots[0]--;
5142 }
5143
5144 while (true) {
5145 struct extent_buffer *leaf = path->nodes[0];
5146 int slot = path->slots[0];
5147 struct btrfs_file_extent_item *ei;
5148 u8 type;
5149 u64 ext_len;
5150 u64 clone_len;
5151
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
5204 clone_len = min_t(u64, ext_len, len);
5205
5206 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5207 btrfs_file_extent_offset(leaf, ei) == data_offset)
5208 ret = send_clone(sctx, offset, clone_len, clone_root);
5209 else
5210 ret = send_extent_data(sctx, offset, clone_len);
5211
5212 if (ret < 0)
5213 goto out;
5214
5215 len -= clone_len;
5216 if (len == 0)
5217 break;
5218 offset += clone_len;
5219 clone_root->offset += clone_len;
5220 data_offset += clone_len;
5221next:
5222 path->slots[0]++;
5223 }
5224
5225 if (len > 0)
5226 ret = send_extent_data(sctx, offset, len);
5227 else
5228 ret = 0;
5229out:
5230 btrfs_free_path(path);
5231 return ret;
5232}
5233
Alexander Block31db9f72012-07-25 23:19:24 +02005234static int send_write_or_clone(struct send_ctx *sctx,
5235 struct btrfs_path *path,
5236 struct btrfs_key *key,
5237 struct clone_root *clone_root)
5238{
5239 int ret = 0;
5240 struct btrfs_file_extent_item *ei;
5241 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02005242 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02005243 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005244 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005245
5246 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5247 struct btrfs_file_extent_item);
5248 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005249 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005250 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005251 /*
5252 * it is possible the inline item won't cover the whole page,
5253 * but there may be items after this page. Make
5254 * sure to send the whole thing
5255 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005256 len = PAGE_ALIGN(len);
Chris Mason74dd17f2012-08-07 16:25:13 -04005257 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02005258 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005259 }
Alexander Block31db9f72012-07-25 23:19:24 +02005260
Filipe Mananaa6aa10c72018-04-30 19:05:07 +01005261 if (offset >= sctx->cur_inode_size) {
5262 ret = 0;
5263 goto out;
5264 }
Alexander Block31db9f72012-07-25 23:19:24 +02005265 if (offset + len > sctx->cur_inode_size)
5266 len = sctx->cur_inode_size - offset;
5267 if (len == 0) {
5268 ret = 0;
5269 goto out;
5270 }
5271
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005272 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01005273 u64 disk_byte;
5274 u64 data_offset;
5275
5276 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5277 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5278 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5279 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005280 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005281 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005282 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005283 sctx->cur_inode_next_write_offset = offset + len;
Alexander Block31db9f72012-07-25 23:19:24 +02005284out:
5285 return ret;
5286}
5287
5288static int is_extent_unchanged(struct send_ctx *sctx,
5289 struct btrfs_path *left_path,
5290 struct btrfs_key *ekey)
5291{
5292 int ret = 0;
5293 struct btrfs_key key;
5294 struct btrfs_path *path = NULL;
5295 struct extent_buffer *eb;
5296 int slot;
5297 struct btrfs_key found_key;
5298 struct btrfs_file_extent_item *ei;
5299 u64 left_disknr;
5300 u64 right_disknr;
5301 u64 left_offset;
5302 u64 right_offset;
5303 u64 left_offset_fixed;
5304 u64 left_len;
5305 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005306 u64 left_gen;
5307 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005308 u8 left_type;
5309 u8 right_type;
5310
5311 path = alloc_path_for_send();
5312 if (!path)
5313 return -ENOMEM;
5314
5315 eb = left_path->nodes[0];
5316 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005317 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5318 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005319
5320 if (left_type != BTRFS_FILE_EXTENT_REG) {
5321 ret = 0;
5322 goto out;
5323 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005324 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5325 left_len = btrfs_file_extent_num_bytes(eb, ei);
5326 left_offset = btrfs_file_extent_offset(eb, ei);
5327 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005328
5329 /*
5330 * Following comments will refer to these graphics. L is the left
5331 * extents which we are checking at the moment. 1-8 are the right
5332 * extents that we iterate.
5333 *
5334 * |-----L-----|
5335 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5336 *
5337 * |-----L-----|
5338 * |--1--|-2b-|...(same as above)
5339 *
5340 * Alternative situation. Happens on files where extents got split.
5341 * |-----L-----|
5342 * |-----------7-----------|-6-|
5343 *
5344 * Alternative situation. Happens on files which got larger.
5345 * |-----L-----|
5346 * |-8-|
5347 * Nothing follows after 8.
5348 */
5349
5350 key.objectid = ekey->objectid;
5351 key.type = BTRFS_EXTENT_DATA_KEY;
5352 key.offset = ekey->offset;
5353 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5354 if (ret < 0)
5355 goto out;
5356 if (ret) {
5357 ret = 0;
5358 goto out;
5359 }
5360
5361 /*
5362 * Handle special case where the right side has no extents at all.
5363 */
5364 eb = path->nodes[0];
5365 slot = path->slots[0];
5366 btrfs_item_key_to_cpu(eb, &found_key, slot);
5367 if (found_key.objectid != key.objectid ||
5368 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005369 /* If we're a hole then just pretend nothing changed */
5370 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005371 goto out;
5372 }
5373
5374 /*
5375 * We're now on 2a, 2b or 7.
5376 */
5377 key = found_key;
5378 while (key.offset < ekey->offset + left_len) {
5379 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5380 right_type = btrfs_file_extent_type(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005381 if (right_type != BTRFS_FILE_EXTENT_REG &&
5382 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005383 ret = 0;
5384 goto out;
5385 }
5386
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005387 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005388 right_len = btrfs_file_extent_ram_bytes(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005389 right_len = PAGE_ALIGN(right_len);
5390 } else {
5391 right_len = btrfs_file_extent_num_bytes(eb, ei);
5392 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005393
Alexander Block31db9f72012-07-25 23:19:24 +02005394 /*
5395 * Are we at extent 8? If yes, we know the extent is changed.
5396 * This may only happen on the first iteration.
5397 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005398 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005399 /* If we're a hole just pretend nothing changed */
5400 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005401 goto out;
5402 }
5403
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005404 /*
5405 * We just wanted to see if when we have an inline extent, what
5406 * follows it is a regular extent (wanted to check the above
5407 * condition for inline extents too). This should normally not
5408 * happen but it's possible for example when we have an inline
5409 * compressed extent representing data with a size matching
5410 * the page size (currently the same as sector size).
5411 */
5412 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5413 ret = 0;
5414 goto out;
5415 }
5416
Filipe Manana24e52b12017-07-06 15:31:46 +01005417 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5418 right_offset = btrfs_file_extent_offset(eb, ei);
5419 right_gen = btrfs_file_extent_generation(eb, ei);
5420
Alexander Block31db9f72012-07-25 23:19:24 +02005421 left_offset_fixed = left_offset;
5422 if (key.offset < ekey->offset) {
5423 /* Fix the right offset for 2a and 7. */
5424 right_offset += ekey->offset - key.offset;
5425 } else {
5426 /* Fix the left offset for all behind 2a and 2b */
5427 left_offset_fixed += key.offset - ekey->offset;
5428 }
5429
5430 /*
5431 * Check if we have the same extent.
5432 */
Alexander Block39540962012-08-01 12:46:05 +02005433 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005434 left_offset_fixed != right_offset ||
5435 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005436 ret = 0;
5437 goto out;
5438 }
5439
5440 /*
5441 * Go to the next extent.
5442 */
5443 ret = btrfs_next_item(sctx->parent_root, path);
5444 if (ret < 0)
5445 goto out;
5446 if (!ret) {
5447 eb = path->nodes[0];
5448 slot = path->slots[0];
5449 btrfs_item_key_to_cpu(eb, &found_key, slot);
5450 }
5451 if (ret || found_key.objectid != key.objectid ||
5452 found_key.type != key.type) {
5453 key.offset += right_len;
5454 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005455 }
5456 if (found_key.offset != key.offset + right_len) {
5457 ret = 0;
5458 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005459 }
5460 key = found_key;
5461 }
5462
5463 /*
5464 * We're now behind the left extent (treat as unchanged) or at the end
5465 * of the right side (treat as changed).
5466 */
5467 if (key.offset >= ekey->offset + left_len)
5468 ret = 1;
5469 else
5470 ret = 0;
5471
5472
5473out:
5474 btrfs_free_path(path);
5475 return ret;
5476}
5477
Josef Bacik16e75492013-10-22 12:18:51 -04005478static int get_last_extent(struct send_ctx *sctx, u64 offset)
5479{
5480 struct btrfs_path *path;
5481 struct btrfs_root *root = sctx->send_root;
5482 struct btrfs_file_extent_item *fi;
5483 struct btrfs_key key;
5484 u64 extent_end;
5485 u8 type;
5486 int ret;
5487
5488 path = alloc_path_for_send();
5489 if (!path)
5490 return -ENOMEM;
5491
5492 sctx->cur_inode_last_extent = 0;
5493
5494 key.objectid = sctx->cur_ino;
5495 key.type = BTRFS_EXTENT_DATA_KEY;
5496 key.offset = offset;
5497 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5498 if (ret < 0)
5499 goto out;
5500 ret = 0;
5501 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5502 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5503 goto out;
5504
5505 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5506 struct btrfs_file_extent_item);
5507 type = btrfs_file_extent_type(path->nodes[0], fi);
5508 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005509 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005510 extent_end = ALIGN(key.offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005511 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005512 } else {
5513 extent_end = key.offset +
5514 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5515 }
5516 sctx->cur_inode_last_extent = extent_end;
5517out:
5518 btrfs_free_path(path);
5519 return ret;
5520}
5521
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005522static int range_is_hole_in_parent(struct send_ctx *sctx,
5523 const u64 start,
5524 const u64 end)
5525{
5526 struct btrfs_path *path;
5527 struct btrfs_key key;
5528 struct btrfs_root *root = sctx->parent_root;
5529 u64 search_start = start;
5530 int ret;
5531
5532 path = alloc_path_for_send();
5533 if (!path)
5534 return -ENOMEM;
5535
5536 key.objectid = sctx->cur_ino;
5537 key.type = BTRFS_EXTENT_DATA_KEY;
5538 key.offset = search_start;
5539 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5540 if (ret < 0)
5541 goto out;
5542 if (ret > 0 && path->slots[0] > 0)
5543 path->slots[0]--;
5544
5545 while (search_start < end) {
5546 struct extent_buffer *leaf = path->nodes[0];
5547 int slot = path->slots[0];
5548 struct btrfs_file_extent_item *fi;
5549 u64 extent_end;
5550
5551 if (slot >= btrfs_header_nritems(leaf)) {
5552 ret = btrfs_next_leaf(root, path);
5553 if (ret < 0)
5554 goto out;
5555 else if (ret > 0)
5556 break;
5557 continue;
5558 }
5559
5560 btrfs_item_key_to_cpu(leaf, &key, slot);
5561 if (key.objectid < sctx->cur_ino ||
5562 key.type < BTRFS_EXTENT_DATA_KEY)
5563 goto next;
5564 if (key.objectid > sctx->cur_ino ||
5565 key.type > BTRFS_EXTENT_DATA_KEY ||
5566 key.offset >= end)
5567 break;
5568
5569 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5570 if (btrfs_file_extent_type(leaf, fi) ==
5571 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005572 u64 size = btrfs_file_extent_ram_bytes(leaf, fi);
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005573
5574 extent_end = ALIGN(key.offset + size,
5575 root->fs_info->sectorsize);
5576 } else {
5577 extent_end = key.offset +
5578 btrfs_file_extent_num_bytes(leaf, fi);
5579 }
5580 if (extent_end <= start)
5581 goto next;
5582 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5583 search_start = extent_end;
5584 goto next;
5585 }
5586 ret = 0;
5587 goto out;
5588next:
5589 path->slots[0]++;
5590 }
5591 ret = 1;
5592out:
5593 btrfs_free_path(path);
5594 return ret;
5595}
5596
Josef Bacik16e75492013-10-22 12:18:51 -04005597static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5598 struct btrfs_key *key)
5599{
5600 struct btrfs_file_extent_item *fi;
5601 u64 extent_end;
5602 u8 type;
5603 int ret = 0;
5604
5605 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5606 return 0;
5607
5608 if (sctx->cur_inode_last_extent == (u64)-1) {
5609 ret = get_last_extent(sctx, key->offset - 1);
5610 if (ret)
5611 return ret;
5612 }
5613
5614 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5615 struct btrfs_file_extent_item);
5616 type = btrfs_file_extent_type(path->nodes[0], fi);
5617 if (type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08005618 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005619 extent_end = ALIGN(key->offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005620 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005621 } else {
5622 extent_end = key->offset +
5623 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5624 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005625
5626 if (path->slots[0] == 0 &&
5627 sctx->cur_inode_last_extent < key->offset) {
5628 /*
5629 * We might have skipped entire leafs that contained only
5630 * file extent items for our current inode. These leafs have
5631 * a generation number smaller (older) than the one in the
5632 * current leaf and the leaf our last extent came from, and
5633 * are located between these 2 leafs.
5634 */
5635 ret = get_last_extent(sctx, key->offset - 1);
5636 if (ret)
5637 return ret;
5638 }
5639
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005640 if (sctx->cur_inode_last_extent < key->offset) {
5641 ret = range_is_hole_in_parent(sctx,
5642 sctx->cur_inode_last_extent,
5643 key->offset);
5644 if (ret < 0)
5645 return ret;
5646 else if (ret == 0)
5647 ret = send_hole(sctx, key->offset);
5648 else
5649 ret = 0;
5650 }
Josef Bacik16e75492013-10-22 12:18:51 -04005651 sctx->cur_inode_last_extent = extent_end;
5652 return ret;
5653}
5654
Alexander Block31db9f72012-07-25 23:19:24 +02005655static int process_extent(struct send_ctx *sctx,
5656 struct btrfs_path *path,
5657 struct btrfs_key *key)
5658{
Alexander Block31db9f72012-07-25 23:19:24 +02005659 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005660 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005661
5662 if (S_ISLNK(sctx->cur_inode_mode))
5663 return 0;
5664
5665 if (sctx->parent_root && !sctx->cur_inode_new) {
5666 ret = is_extent_unchanged(sctx, path, key);
5667 if (ret < 0)
5668 goto out;
5669 if (ret) {
5670 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005671 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005672 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005673 } else {
5674 struct btrfs_file_extent_item *ei;
5675 u8 type;
5676
5677 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5678 struct btrfs_file_extent_item);
5679 type = btrfs_file_extent_type(path->nodes[0], ei);
5680 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5681 type == BTRFS_FILE_EXTENT_REG) {
5682 /*
5683 * The send spec does not have a prealloc command yet,
5684 * so just leave a hole for prealloc'ed extents until
5685 * we have enough commands queued up to justify rev'ing
5686 * the send spec.
5687 */
5688 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5689 ret = 0;
5690 goto out;
5691 }
5692
5693 /* Have a hole, just skip it. */
5694 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5695 ret = 0;
5696 goto out;
5697 }
5698 }
Alexander Block31db9f72012-07-25 23:19:24 +02005699 }
5700
5701 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5702 sctx->cur_inode_size, &found_clone);
5703 if (ret != -ENOENT && ret < 0)
5704 goto out;
5705
5706 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005707 if (ret)
5708 goto out;
5709out_hole:
5710 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005711out:
5712 return ret;
5713}
5714
5715static int process_all_extents(struct send_ctx *sctx)
5716{
5717 int ret;
5718 struct btrfs_root *root;
5719 struct btrfs_path *path;
5720 struct btrfs_key key;
5721 struct btrfs_key found_key;
5722 struct extent_buffer *eb;
5723 int slot;
5724
5725 root = sctx->send_root;
5726 path = alloc_path_for_send();
5727 if (!path)
5728 return -ENOMEM;
5729
5730 key.objectid = sctx->cmp_key->objectid;
5731 key.type = BTRFS_EXTENT_DATA_KEY;
5732 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005733 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5734 if (ret < 0)
5735 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005736
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005737 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005738 eb = path->nodes[0];
5739 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005740
5741 if (slot >= btrfs_header_nritems(eb)) {
5742 ret = btrfs_next_leaf(root, path);
5743 if (ret < 0) {
5744 goto out;
5745 } else if (ret > 0) {
5746 ret = 0;
5747 break;
5748 }
5749 continue;
5750 }
5751
Alexander Block31db9f72012-07-25 23:19:24 +02005752 btrfs_item_key_to_cpu(eb, &found_key, slot);
5753
5754 if (found_key.objectid != key.objectid ||
5755 found_key.type != key.type) {
5756 ret = 0;
5757 goto out;
5758 }
5759
5760 ret = process_extent(sctx, path, &found_key);
5761 if (ret < 0)
5762 goto out;
5763
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005764 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005765 }
5766
5767out:
5768 btrfs_free_path(path);
5769 return ret;
5770}
5771
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005772static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5773 int *pending_move,
5774 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005775{
5776 int ret = 0;
5777
5778 if (sctx->cur_ino == 0)
5779 goto out;
5780 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005781 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005782 goto out;
5783 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5784 goto out;
5785
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005786 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005787 if (ret < 0)
5788 goto out;
5789
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005790 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005791out:
5792 return ret;
5793}
5794
5795static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5796{
5797 int ret = 0;
5798 u64 left_mode;
5799 u64 left_uid;
5800 u64 left_gid;
5801 u64 right_mode;
5802 u64 right_uid;
5803 u64 right_gid;
5804 int need_chmod = 0;
5805 int need_chown = 0;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005806 int need_truncate = 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005807 int pending_move = 0;
5808 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005809
Filipe Manana46b2f452018-07-24 11:54:04 +01005810 if (sctx->ignore_cur_inode)
5811 return 0;
5812
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005813 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5814 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005815 if (ret < 0)
5816 goto out;
5817
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005818 /*
5819 * We have processed the refs and thus need to advance send_progress.
5820 * Now, calls to get_cur_xxx will take the updated refs of the current
5821 * inode into account.
5822 *
5823 * On the other hand, if our current inode is a directory and couldn't
5824 * be moved/renamed because its parent was renamed/moved too and it has
5825 * a higher inode number, we can only move/rename our current inode
5826 * after we moved/renamed its parent. Therefore in this case operate on
5827 * the old path (pre move/rename) of our current inode, and the
5828 * move/rename will be performed later.
5829 */
5830 if (refs_processed && !pending_move)
5831 sctx->send_progress = sctx->cur_ino + 1;
5832
Alexander Block31db9f72012-07-25 23:19:24 +02005833 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5834 goto out;
5835 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5836 goto out;
5837
5838 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005839 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005840 if (ret < 0)
5841 goto out;
5842
Alex Lyakase2d044f2012-10-17 13:52:47 +00005843 if (!sctx->parent_root || sctx->cur_inode_new) {
5844 need_chown = 1;
5845 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005846 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005847 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
5848 need_truncate = 0;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005849 } else {
Filipe Mananaffa7c422018-02-06 20:40:40 +00005850 u64 old_size;
5851
Alex Lyakase2d044f2012-10-17 13:52:47 +00005852 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
Filipe Mananaffa7c422018-02-06 20:40:40 +00005853 &old_size, NULL, &right_mode, &right_uid,
Alex Lyakase2d044f2012-10-17 13:52:47 +00005854 &right_gid, NULL);
5855 if (ret < 0)
5856 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005857
Alex Lyakase2d044f2012-10-17 13:52:47 +00005858 if (left_uid != right_uid || left_gid != right_gid)
5859 need_chown = 1;
5860 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5861 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005862 if ((old_size == sctx->cur_inode_size) ||
5863 (sctx->cur_inode_size > old_size &&
5864 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
5865 need_truncate = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005866 }
5867
5868 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005869 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005870 if (sctx->cur_inode_last_extent == (u64)-1 ||
5871 sctx->cur_inode_last_extent <
5872 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005873 ret = get_last_extent(sctx, (u64)-1);
5874 if (ret)
5875 goto out;
5876 }
5877 if (sctx->cur_inode_last_extent <
5878 sctx->cur_inode_size) {
5879 ret = send_hole(sctx, sctx->cur_inode_size);
5880 if (ret)
5881 goto out;
5882 }
5883 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005884 if (need_truncate) {
5885 ret = send_truncate(sctx, sctx->cur_ino,
5886 sctx->cur_inode_gen,
5887 sctx->cur_inode_size);
5888 if (ret < 0)
5889 goto out;
5890 }
Alexander Block31db9f72012-07-25 23:19:24 +02005891 }
5892
5893 if (need_chown) {
5894 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5895 left_uid, left_gid);
5896 if (ret < 0)
5897 goto out;
5898 }
5899 if (need_chmod) {
5900 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5901 left_mode);
5902 if (ret < 0)
5903 goto out;
5904 }
5905
5906 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005907 * If other directory inodes depended on our current directory
5908 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005909 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005910 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5911 ret = apply_children_dir_moves(sctx);
5912 if (ret)
5913 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005914 /*
5915 * Need to send that every time, no matter if it actually
5916 * changed between the two trees as we have done changes to
5917 * the inode before. If our inode is a directory and it's
5918 * waiting to be moved/renamed, we will send its utimes when
5919 * it's moved/renamed, therefore we don't need to do it here.
5920 */
5921 sctx->send_progress = sctx->cur_ino + 1;
5922 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5923 if (ret < 0)
5924 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005925 }
5926
Alexander Block31db9f72012-07-25 23:19:24 +02005927out:
5928 return ret;
5929}
5930
Filipe Manana46b2f452018-07-24 11:54:04 +01005931struct parent_paths_ctx {
5932 struct list_head *refs;
5933 struct send_ctx *sctx;
5934};
5935
5936static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
5937 void *ctx)
5938{
5939 struct parent_paths_ctx *ppctx = ctx;
5940
5941 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
5942 ppctx->refs);
5943}
5944
5945/*
5946 * Issue unlink operations for all paths of the current inode found in the
5947 * parent snapshot.
5948 */
5949static int btrfs_unlink_all_paths(struct send_ctx *sctx)
5950{
5951 LIST_HEAD(deleted_refs);
5952 struct btrfs_path *path;
5953 struct btrfs_key key;
5954 struct parent_paths_ctx ctx;
5955 int ret;
5956
5957 path = alloc_path_for_send();
5958 if (!path)
5959 return -ENOMEM;
5960
5961 key.objectid = sctx->cur_ino;
5962 key.type = BTRFS_INODE_REF_KEY;
5963 key.offset = 0;
5964 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
5965 if (ret < 0)
5966 goto out;
5967
5968 ctx.refs = &deleted_refs;
5969 ctx.sctx = sctx;
5970
5971 while (true) {
5972 struct extent_buffer *eb = path->nodes[0];
5973 int slot = path->slots[0];
5974
5975 if (slot >= btrfs_header_nritems(eb)) {
5976 ret = btrfs_next_leaf(sctx->parent_root, path);
5977 if (ret < 0)
5978 goto out;
5979 else if (ret > 0)
5980 break;
5981 continue;
5982 }
5983
5984 btrfs_item_key_to_cpu(eb, &key, slot);
5985 if (key.objectid != sctx->cur_ino)
5986 break;
5987 if (key.type != BTRFS_INODE_REF_KEY &&
5988 key.type != BTRFS_INODE_EXTREF_KEY)
5989 break;
5990
5991 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
5992 record_parent_ref, &ctx);
5993 if (ret < 0)
5994 goto out;
5995
5996 path->slots[0]++;
5997 }
5998
5999 while (!list_empty(&deleted_refs)) {
6000 struct recorded_ref *ref;
6001
6002 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6003 ret = send_unlink(sctx, ref->full_path);
6004 if (ret < 0)
6005 goto out;
6006 fs_path_free(ref->full_path);
6007 list_del(&ref->list);
6008 kfree(ref);
6009 }
6010 ret = 0;
6011out:
6012 btrfs_free_path(path);
6013 if (ret)
6014 __free_recorded_refs(&deleted_refs);
6015 return ret;
6016}
6017
Alexander Block31db9f72012-07-25 23:19:24 +02006018static int changed_inode(struct send_ctx *sctx,
6019 enum btrfs_compare_tree_result result)
6020{
6021 int ret = 0;
6022 struct btrfs_key *key = sctx->cmp_key;
6023 struct btrfs_inode_item *left_ii = NULL;
6024 struct btrfs_inode_item *right_ii = NULL;
6025 u64 left_gen = 0;
6026 u64 right_gen = 0;
6027
Alexander Block31db9f72012-07-25 23:19:24 +02006028 sctx->cur_ino = key->objectid;
6029 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006030 sctx->cur_inode_last_extent = (u64)-1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00006031 sctx->cur_inode_next_write_offset = 0;
Filipe Manana46b2f452018-07-24 11:54:04 +01006032 sctx->ignore_cur_inode = false;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006033
6034 /*
6035 * Set send_progress to current inode. This will tell all get_cur_xxx
6036 * functions that the current inode's refs are not updated yet. Later,
6037 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6038 */
Alexander Block31db9f72012-07-25 23:19:24 +02006039 sctx->send_progress = sctx->cur_ino;
6040
6041 if (result == BTRFS_COMPARE_TREE_NEW ||
6042 result == BTRFS_COMPARE_TREE_CHANGED) {
6043 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6044 sctx->left_path->slots[0],
6045 struct btrfs_inode_item);
6046 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6047 left_ii);
6048 } else {
6049 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6050 sctx->right_path->slots[0],
6051 struct btrfs_inode_item);
6052 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6053 right_ii);
6054 }
6055 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6056 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6057 sctx->right_path->slots[0],
6058 struct btrfs_inode_item);
6059
6060 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6061 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02006062
6063 /*
6064 * The cur_ino = root dir case is special here. We can't treat
6065 * the inode as deleted+reused because it would generate a
6066 * stream that tries to delete/mkdir the root dir.
6067 */
6068 if (left_gen != right_gen &&
6069 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02006070 sctx->cur_inode_new_gen = 1;
6071 }
6072
Filipe Manana46b2f452018-07-24 11:54:04 +01006073 /*
6074 * Normally we do not find inodes with a link count of zero (orphans)
6075 * because the most common case is to create a snapshot and use it
6076 * for a send operation. However other less common use cases involve
6077 * using a subvolume and send it after turning it to RO mode just
6078 * after deleting all hard links of a file while holding an open
6079 * file descriptor against it or turning a RO snapshot into RW mode,
6080 * keep an open file descriptor against a file, delete it and then
6081 * turn the snapshot back to RO mode before using it for a send
6082 * operation. So if we find such cases, ignore the inode and all its
6083 * items completely if it's a new inode, or if it's a changed inode
6084 * make sure all its previous paths (from the parent snapshot) are all
6085 * unlinked and all other the inode items are ignored.
6086 */
6087 if (result == BTRFS_COMPARE_TREE_NEW ||
6088 result == BTRFS_COMPARE_TREE_CHANGED) {
6089 u32 nlinks;
6090
6091 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6092 if (nlinks == 0) {
6093 sctx->ignore_cur_inode = true;
6094 if (result == BTRFS_COMPARE_TREE_CHANGED)
6095 ret = btrfs_unlink_all_paths(sctx);
6096 goto out;
6097 }
6098 }
6099
Alexander Block31db9f72012-07-25 23:19:24 +02006100 if (result == BTRFS_COMPARE_TREE_NEW) {
6101 sctx->cur_inode_gen = left_gen;
6102 sctx->cur_inode_new = 1;
6103 sctx->cur_inode_deleted = 0;
6104 sctx->cur_inode_size = btrfs_inode_size(
6105 sctx->left_path->nodes[0], left_ii);
6106 sctx->cur_inode_mode = btrfs_inode_mode(
6107 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006108 sctx->cur_inode_rdev = btrfs_inode_rdev(
6109 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02006110 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02006111 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006112 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6113 sctx->cur_inode_gen = right_gen;
6114 sctx->cur_inode_new = 0;
6115 sctx->cur_inode_deleted = 1;
6116 sctx->cur_inode_size = btrfs_inode_size(
6117 sctx->right_path->nodes[0], right_ii);
6118 sctx->cur_inode_mode = btrfs_inode_mode(
6119 sctx->right_path->nodes[0], right_ii);
6120 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02006121 /*
6122 * We need to do some special handling in case the inode was
6123 * reported as changed with a changed generation number. This
6124 * means that the original inode was deleted and new inode
6125 * reused the same inum. So we have to treat the old inode as
6126 * deleted and the new one as new.
6127 */
Alexander Block31db9f72012-07-25 23:19:24 +02006128 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02006129 /*
6130 * First, process the inode as if it was deleted.
6131 */
Alexander Block31db9f72012-07-25 23:19:24 +02006132 sctx->cur_inode_gen = right_gen;
6133 sctx->cur_inode_new = 0;
6134 sctx->cur_inode_deleted = 1;
6135 sctx->cur_inode_size = btrfs_inode_size(
6136 sctx->right_path->nodes[0], right_ii);
6137 sctx->cur_inode_mode = btrfs_inode_mode(
6138 sctx->right_path->nodes[0], right_ii);
6139 ret = process_all_refs(sctx,
6140 BTRFS_COMPARE_TREE_DELETED);
6141 if (ret < 0)
6142 goto out;
6143
Alexander Block766702e2012-07-28 14:11:31 +02006144 /*
6145 * Now process the inode as if it was new.
6146 */
Alexander Block31db9f72012-07-25 23:19:24 +02006147 sctx->cur_inode_gen = left_gen;
6148 sctx->cur_inode_new = 1;
6149 sctx->cur_inode_deleted = 0;
6150 sctx->cur_inode_size = btrfs_inode_size(
6151 sctx->left_path->nodes[0], left_ii);
6152 sctx->cur_inode_mode = btrfs_inode_mode(
6153 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006154 sctx->cur_inode_rdev = btrfs_inode_rdev(
6155 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02006156 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006157 if (ret < 0)
6158 goto out;
6159
6160 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6161 if (ret < 0)
6162 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006163 /*
6164 * Advance send_progress now as we did not get into
6165 * process_recorded_refs_if_needed in the new_gen case.
6166 */
6167 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02006168
6169 /*
6170 * Now process all extents and xattrs of the inode as if
6171 * they were all new.
6172 */
Alexander Block31db9f72012-07-25 23:19:24 +02006173 ret = process_all_extents(sctx);
6174 if (ret < 0)
6175 goto out;
6176 ret = process_all_new_xattrs(sctx);
6177 if (ret < 0)
6178 goto out;
6179 } else {
6180 sctx->cur_inode_gen = left_gen;
6181 sctx->cur_inode_new = 0;
6182 sctx->cur_inode_new_gen = 0;
6183 sctx->cur_inode_deleted = 0;
6184 sctx->cur_inode_size = btrfs_inode_size(
6185 sctx->left_path->nodes[0], left_ii);
6186 sctx->cur_inode_mode = btrfs_inode_mode(
6187 sctx->left_path->nodes[0], left_ii);
6188 }
6189 }
6190
6191out:
6192 return ret;
6193}
6194
Alexander Block766702e2012-07-28 14:11:31 +02006195/*
6196 * We have to process new refs before deleted refs, but compare_trees gives us
6197 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6198 * first and later process them in process_recorded_refs.
6199 * For the cur_inode_new_gen case, we skip recording completely because
6200 * changed_inode did already initiate processing of refs. The reason for this is
6201 * that in this case, compare_tree actually compares the refs of 2 different
6202 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6203 * refs of the right tree as deleted and all refs of the left tree as new.
6204 */
Alexander Block31db9f72012-07-25 23:19:24 +02006205static int changed_ref(struct send_ctx *sctx,
6206 enum btrfs_compare_tree_result result)
6207{
6208 int ret = 0;
6209
Filipe Manana95155582016-08-01 01:50:37 +01006210 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6211 inconsistent_snapshot_error(sctx, result, "reference");
6212 return -EIO;
6213 }
Alexander Block31db9f72012-07-25 23:19:24 +02006214
6215 if (!sctx->cur_inode_new_gen &&
6216 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6217 if (result == BTRFS_COMPARE_TREE_NEW)
6218 ret = record_new_ref(sctx);
6219 else if (result == BTRFS_COMPARE_TREE_DELETED)
6220 ret = record_deleted_ref(sctx);
6221 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6222 ret = record_changed_ref(sctx);
6223 }
6224
6225 return ret;
6226}
6227
Alexander Block766702e2012-07-28 14:11:31 +02006228/*
6229 * Process new/deleted/changed xattrs. We skip processing in the
6230 * cur_inode_new_gen case because changed_inode did already initiate processing
6231 * of xattrs. The reason is the same as in changed_ref
6232 */
Alexander Block31db9f72012-07-25 23:19:24 +02006233static int changed_xattr(struct send_ctx *sctx,
6234 enum btrfs_compare_tree_result result)
6235{
6236 int ret = 0;
6237
Filipe Manana95155582016-08-01 01:50:37 +01006238 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6239 inconsistent_snapshot_error(sctx, result, "xattr");
6240 return -EIO;
6241 }
Alexander Block31db9f72012-07-25 23:19:24 +02006242
6243 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6244 if (result == BTRFS_COMPARE_TREE_NEW)
6245 ret = process_new_xattr(sctx);
6246 else if (result == BTRFS_COMPARE_TREE_DELETED)
6247 ret = process_deleted_xattr(sctx);
6248 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6249 ret = process_changed_xattr(sctx);
6250 }
6251
6252 return ret;
6253}
6254
Alexander Block766702e2012-07-28 14:11:31 +02006255/*
6256 * Process new/deleted/changed extents. We skip processing in the
6257 * cur_inode_new_gen case because changed_inode did already initiate processing
6258 * of extents. The reason is the same as in changed_ref
6259 */
Alexander Block31db9f72012-07-25 23:19:24 +02006260static int changed_extent(struct send_ctx *sctx,
6261 enum btrfs_compare_tree_result result)
6262{
6263 int ret = 0;
6264
Filipe Manana95155582016-08-01 01:50:37 +01006265 if (sctx->cur_ino != sctx->cmp_key->objectid) {
Filipe Mananad5e84fd2016-09-19 10:57:40 +01006266
6267 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6268 struct extent_buffer *leaf_l;
6269 struct extent_buffer *leaf_r;
6270 struct btrfs_file_extent_item *ei_l;
6271 struct btrfs_file_extent_item *ei_r;
6272
6273 leaf_l = sctx->left_path->nodes[0];
6274 leaf_r = sctx->right_path->nodes[0];
6275 ei_l = btrfs_item_ptr(leaf_l,
6276 sctx->left_path->slots[0],
6277 struct btrfs_file_extent_item);
6278 ei_r = btrfs_item_ptr(leaf_r,
6279 sctx->right_path->slots[0],
6280 struct btrfs_file_extent_item);
6281
6282 /*
6283 * We may have found an extent item that has changed
6284 * only its disk_bytenr field and the corresponding
6285 * inode item was not updated. This case happens due to
6286 * very specific timings during relocation when a leaf
6287 * that contains file extent items is COWed while
6288 * relocation is ongoing and its in the stage where it
6289 * updates data pointers. So when this happens we can
6290 * safely ignore it since we know it's the same extent,
6291 * but just at different logical and physical locations
6292 * (when an extent is fully replaced with a new one, we
6293 * know the generation number must have changed too,
6294 * since snapshot creation implies committing the current
6295 * transaction, and the inode item must have been updated
6296 * as well).
6297 * This replacement of the disk_bytenr happens at
6298 * relocation.c:replace_file_extents() through
6299 * relocation.c:btrfs_reloc_cow_block().
6300 */
6301 if (btrfs_file_extent_generation(leaf_l, ei_l) ==
6302 btrfs_file_extent_generation(leaf_r, ei_r) &&
6303 btrfs_file_extent_ram_bytes(leaf_l, ei_l) ==
6304 btrfs_file_extent_ram_bytes(leaf_r, ei_r) &&
6305 btrfs_file_extent_compression(leaf_l, ei_l) ==
6306 btrfs_file_extent_compression(leaf_r, ei_r) &&
6307 btrfs_file_extent_encryption(leaf_l, ei_l) ==
6308 btrfs_file_extent_encryption(leaf_r, ei_r) &&
6309 btrfs_file_extent_other_encoding(leaf_l, ei_l) ==
6310 btrfs_file_extent_other_encoding(leaf_r, ei_r) &&
6311 btrfs_file_extent_type(leaf_l, ei_l) ==
6312 btrfs_file_extent_type(leaf_r, ei_r) &&
6313 btrfs_file_extent_disk_bytenr(leaf_l, ei_l) !=
6314 btrfs_file_extent_disk_bytenr(leaf_r, ei_r) &&
6315 btrfs_file_extent_disk_num_bytes(leaf_l, ei_l) ==
6316 btrfs_file_extent_disk_num_bytes(leaf_r, ei_r) &&
6317 btrfs_file_extent_offset(leaf_l, ei_l) ==
6318 btrfs_file_extent_offset(leaf_r, ei_r) &&
6319 btrfs_file_extent_num_bytes(leaf_l, ei_l) ==
6320 btrfs_file_extent_num_bytes(leaf_r, ei_r))
6321 return 0;
6322 }
6323
Filipe Manana95155582016-08-01 01:50:37 +01006324 inconsistent_snapshot_error(sctx, result, "extent");
6325 return -EIO;
6326 }
Alexander Block31db9f72012-07-25 23:19:24 +02006327
6328 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6329 if (result != BTRFS_COMPARE_TREE_DELETED)
6330 ret = process_extent(sctx, sctx->left_path,
6331 sctx->cmp_key);
6332 }
6333
6334 return ret;
6335}
6336
Josef Bacikba5e8f22013-08-16 16:52:55 -04006337static int dir_changed(struct send_ctx *sctx, u64 dir)
6338{
6339 u64 orig_gen, new_gen;
6340 int ret;
6341
6342 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6343 NULL, NULL);
6344 if (ret)
6345 return ret;
6346
6347 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6348 NULL, NULL, NULL);
6349 if (ret)
6350 return ret;
6351
6352 return (orig_gen != new_gen) ? 1 : 0;
6353}
6354
6355static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6356 struct btrfs_key *key)
6357{
6358 struct btrfs_inode_extref *extref;
6359 struct extent_buffer *leaf;
6360 u64 dirid = 0, last_dirid = 0;
6361 unsigned long ptr;
6362 u32 item_size;
6363 u32 cur_offset = 0;
6364 int ref_name_len;
6365 int ret = 0;
6366
6367 /* Easy case, just check this one dirid */
6368 if (key->type == BTRFS_INODE_REF_KEY) {
6369 dirid = key->offset;
6370
6371 ret = dir_changed(sctx, dirid);
6372 goto out;
6373 }
6374
6375 leaf = path->nodes[0];
6376 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6377 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6378 while (cur_offset < item_size) {
6379 extref = (struct btrfs_inode_extref *)(ptr +
6380 cur_offset);
6381 dirid = btrfs_inode_extref_parent(leaf, extref);
6382 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6383 cur_offset += ref_name_len + sizeof(*extref);
6384 if (dirid == last_dirid)
6385 continue;
6386 ret = dir_changed(sctx, dirid);
6387 if (ret)
6388 break;
6389 last_dirid = dirid;
6390 }
6391out:
6392 return ret;
6393}
6394
Alexander Block766702e2012-07-28 14:11:31 +02006395/*
6396 * Updates compare related fields in sctx and simply forwards to the actual
6397 * changed_xxx functions.
6398 */
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006399static int changed_cb(struct btrfs_path *left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02006400 struct btrfs_path *right_path,
6401 struct btrfs_key *key,
6402 enum btrfs_compare_tree_result result,
6403 void *ctx)
6404{
6405 int ret = 0;
6406 struct send_ctx *sctx = ctx;
6407
Josef Bacikba5e8f22013-08-16 16:52:55 -04006408 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04006409 if (key->type == BTRFS_INODE_REF_KEY ||
6410 key->type == BTRFS_INODE_EXTREF_KEY) {
6411 ret = compare_refs(sctx, left_path, key);
6412 if (!ret)
6413 return 0;
6414 if (ret < 0)
6415 return ret;
6416 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6417 return maybe_send_hole(sctx, left_path, key);
6418 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04006419 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006420 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04006421 result = BTRFS_COMPARE_TREE_CHANGED;
6422 ret = 0;
6423 }
6424
Alexander Block31db9f72012-07-25 23:19:24 +02006425 sctx->left_path = left_path;
6426 sctx->right_path = right_path;
6427 sctx->cmp_key = key;
6428
6429 ret = finish_inode_if_needed(sctx, 0);
6430 if (ret < 0)
6431 goto out;
6432
Alexander Block2981e222012-08-01 14:47:03 +02006433 /* Ignore non-FS objects */
6434 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6435 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6436 goto out;
6437
Filipe Manana46b2f452018-07-24 11:54:04 +01006438 if (key->type == BTRFS_INODE_ITEM_KEY) {
Alexander Block31db9f72012-07-25 23:19:24 +02006439 ret = changed_inode(sctx, result);
Filipe Manana46b2f452018-07-24 11:54:04 +01006440 } else if (!sctx->ignore_cur_inode) {
6441 if (key->type == BTRFS_INODE_REF_KEY ||
6442 key->type == BTRFS_INODE_EXTREF_KEY)
6443 ret = changed_ref(sctx, result);
6444 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6445 ret = changed_xattr(sctx, result);
6446 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6447 ret = changed_extent(sctx, result);
6448 }
Alexander Block31db9f72012-07-25 23:19:24 +02006449
6450out:
6451 return ret;
6452}
6453
6454static int full_send_tree(struct send_ctx *sctx)
6455{
6456 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006457 struct btrfs_root *send_root = sctx->send_root;
6458 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006459 struct btrfs_path *path;
6460 struct extent_buffer *eb;
6461 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006462
6463 path = alloc_path_for_send();
6464 if (!path)
6465 return -ENOMEM;
6466
Alexander Block31db9f72012-07-25 23:19:24 +02006467 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6468 key.type = BTRFS_INODE_ITEM_KEY;
6469 key.offset = 0;
6470
Alexander Block31db9f72012-07-25 23:19:24 +02006471 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6472 if (ret < 0)
6473 goto out;
6474 if (ret)
6475 goto out_finish;
6476
6477 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006478 eb = path->nodes[0];
6479 slot = path->slots[0];
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006480 btrfs_item_key_to_cpu(eb, &key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02006481
Filipe Mananaca5d2ba2018-07-23 09:10:09 +01006482 ret = changed_cb(path, NULL, &key,
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006483 BTRFS_COMPARE_TREE_NEW, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006484 if (ret < 0)
6485 goto out;
6486
Alexander Block31db9f72012-07-25 23:19:24 +02006487 ret = btrfs_next_item(send_root, path);
6488 if (ret < 0)
6489 goto out;
6490 if (ret) {
6491 ret = 0;
6492 break;
6493 }
6494 }
6495
6496out_finish:
6497 ret = finish_inode_if_needed(sctx, 1);
6498
6499out:
6500 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006501 return ret;
6502}
6503
6504static int send_subvol(struct send_ctx *sctx)
6505{
6506 int ret;
6507
Stefan Behrensc2c71322013-04-10 17:10:52 +00006508 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6509 ret = send_header(sctx);
6510 if (ret < 0)
6511 goto out;
6512 }
Alexander Block31db9f72012-07-25 23:19:24 +02006513
6514 ret = send_subvol_begin(sctx);
6515 if (ret < 0)
6516 goto out;
6517
6518 if (sctx->parent_root) {
6519 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6520 changed_cb, sctx);
6521 if (ret < 0)
6522 goto out;
6523 ret = finish_inode_if_needed(sctx, 1);
6524 if (ret < 0)
6525 goto out;
6526 } else {
6527 ret = full_send_tree(sctx);
6528 if (ret < 0)
6529 goto out;
6530 }
6531
6532out:
Alexander Block31db9f72012-07-25 23:19:24 +02006533 free_recorded_refs(sctx);
6534 return ret;
6535}
6536
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006537/*
6538 * If orphan cleanup did remove any orphans from a root, it means the tree
6539 * was modified and therefore the commit root is not the same as the current
6540 * root anymore. This is a problem, because send uses the commit root and
6541 * therefore can see inode items that don't exist in the current root anymore,
6542 * and for example make calls to btrfs_iget, which will do tree lookups based
6543 * on the current root and not on the commit root. Those lookups will fail,
6544 * returning a -ESTALE error, and making send fail with that error. So make
6545 * sure a send does not see any orphans we have just removed, and that it will
6546 * see the same inodes regardless of whether a transaction commit happened
6547 * before it started (meaning that the commit root will be the same as the
6548 * current root) or not.
6549 */
6550static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6551{
6552 int i;
6553 struct btrfs_trans_handle *trans = NULL;
6554
6555again:
6556 if (sctx->parent_root &&
6557 sctx->parent_root->node != sctx->parent_root->commit_root)
6558 goto commit_trans;
6559
6560 for (i = 0; i < sctx->clone_roots_cnt; i++)
6561 if (sctx->clone_roots[i].root->node !=
6562 sctx->clone_roots[i].root->commit_root)
6563 goto commit_trans;
6564
6565 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006566 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006567
6568 return 0;
6569
6570commit_trans:
6571 /* Use any root, all fs roots will get their commit roots updated. */
6572 if (!trans) {
6573 trans = btrfs_join_transaction(sctx->send_root);
6574 if (IS_ERR(trans))
6575 return PTR_ERR(trans);
6576 goto again;
6577 }
6578
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006579 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006580}
6581
David Sterba66ef7d62013-12-17 15:07:20 +01006582static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6583{
6584 spin_lock(&root->root_item_lock);
6585 root->send_in_progress--;
6586 /*
6587 * Not much left to do, we don't know why it's unbalanced and
6588 * can't blindly reset it to 0.
6589 */
6590 if (root->send_in_progress < 0)
6591 btrfs_err(root->fs_info,
Colin Ian Kingf5686e32018-05-04 12:11:12 +01006592 "send_in_progress unbalanced %d root %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006593 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01006594 spin_unlock(&root->root_item_lock);
6595}
6596
Josef Bacik2351f432017-09-27 10:43:13 -04006597long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
Alexander Block31db9f72012-07-25 23:19:24 +02006598{
6599 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006600 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6601 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02006602 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02006603 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006604 struct send_ctx *sctx = NULL;
6605 u32 i;
6606 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01006607 int clone_sources_to_rollback = 0;
David Sterbae55d1152016-04-11 18:52:02 +02006608 unsigned alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08006609 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08006610 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02006611
6612 if (!capable(CAP_SYS_ADMIN))
6613 return -EPERM;
6614
Josef Bacik139f8072013-05-20 11:26:50 -04006615 /*
David Sterba2c686532013-12-16 17:34:17 +01006616 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02006617 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01006618 */
6619 spin_lock(&send_root->root_item_lock);
6620 send_root->send_in_progress++;
6621 spin_unlock(&send_root->root_item_lock);
6622
6623 /*
Josef Bacik139f8072013-05-20 11:26:50 -04006624 * This is done when we lookup the root, it should already be complete
6625 * by the time we get here.
6626 */
6627 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6628
6629 /*
David Sterba2c686532013-12-16 17:34:17 +01006630 * Userspace tools do the checks and warn the user if it's
6631 * not RO.
6632 */
6633 if (!btrfs_root_readonly(send_root)) {
6634 ret = -EPERM;
6635 goto out;
6636 }
6637
Dan Carpenter457ae722017-03-17 23:51:20 +03006638 /*
6639 * Check that we don't overflow at later allocations, we request
6640 * clone_sources_count + 1 items, and compare to unsigned long inside
6641 * access_ok.
6642 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006643 if (arg->clone_sources_count >
Dan Carpenter457ae722017-03-17 23:51:20 +03006644 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006645 ret = -EINVAL;
6646 goto out;
6647 }
6648
Linus Torvalds96d4f262019-01-03 18:57:57 -08006649 if (!access_ok(arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05006650 sizeof(*arg->clone_sources) *
6651 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02006652 ret = -EFAULT;
6653 goto out;
6654 }
6655
Stefan Behrensc2c71322013-04-10 17:10:52 +00006656 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006657 ret = -EINVAL;
6658 goto out;
6659 }
6660
David Sterbae780b0d2016-01-18 18:42:13 +01006661 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006662 if (!sctx) {
6663 ret = -ENOMEM;
6664 goto out;
6665 }
6666
6667 INIT_LIST_HEAD(&sctx->new_refs);
6668 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01006669 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006670 INIT_LIST_HEAD(&sctx->name_cache_list);
6671
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006672 sctx->flags = arg->flags;
6673
Alexander Block31db9f72012-07-25 23:19:24 +02006674 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00006675 if (!sctx->send_filp) {
6676 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02006677 goto out;
6678 }
6679
Alexander Block31db9f72012-07-25 23:19:24 +02006680 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02006681 /*
6682 * Unlikely but possible, if the subvolume is marked for deletion but
6683 * is slow to remove the directory entry, send can still be started
6684 */
6685 if (btrfs_root_dead(sctx->send_root)) {
6686 ret = -EPERM;
6687 goto out;
6688 }
6689
Alexander Block31db9f72012-07-25 23:19:24 +02006690 sctx->clone_roots_cnt = arg->clone_sources_count;
6691
6692 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
Michal Hocko752ade62017-05-08 15:57:27 -07006693 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006694 if (!sctx->send_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006695 ret = -ENOMEM;
6696 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006697 }
6698
Michal Hocko752ade62017-05-08 15:57:27 -07006699 sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006700 if (!sctx->read_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006701 ret = -ENOMEM;
6702 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006703 }
6704
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006705 sctx->pending_dir_moves = RB_ROOT;
6706 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00006707 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006708
David Sterbae55d1152016-04-11 18:52:02 +02006709 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6710
David Sterba818e0102017-05-31 18:40:02 +02006711 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006712 if (!sctx->clone_roots) {
David Sterba818e0102017-05-31 18:40:02 +02006713 ret = -ENOMEM;
6714 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006715 }
6716
David Sterbae55d1152016-04-11 18:52:02 +02006717 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6718
Alexander Block31db9f72012-07-25 23:19:24 +02006719 if (arg->clone_sources_count) {
Michal Hocko752ade62017-05-08 15:57:27 -07006720 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006721 if (!clone_sources_tmp) {
Michal Hocko752ade62017-05-08 15:57:27 -07006722 ret = -ENOMEM;
6723 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006724 }
6725
6726 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02006727 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02006728 if (ret) {
6729 ret = -EFAULT;
6730 goto out;
6731 }
6732
6733 for (i = 0; i < arg->clone_sources_count; i++) {
6734 key.objectid = clone_sources_tmp[i];
6735 key.type = BTRFS_ROOT_ITEM_KEY;
6736 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006737
6738 index = srcu_read_lock(&fs_info->subvol_srcu);
6739
Alexander Block31db9f72012-07-25 23:19:24 +02006740 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02006741 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006742 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006743 ret = PTR_ERR(clone_root);
6744 goto out;
6745 }
David Sterba2c686532013-12-16 17:34:17 +01006746 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00006747 if (!btrfs_root_readonly(clone_root) ||
6748 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006749 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006750 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006751 ret = -EPERM;
6752 goto out;
6753 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00006754 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01006755 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006756 srcu_read_unlock(&fs_info->subvol_srcu, index);
6757
Alexander Block31db9f72012-07-25 23:19:24 +02006758 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00006759 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006760 }
David Sterba2f913062016-04-11 18:40:08 +02006761 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006762 clone_sources_tmp = NULL;
6763 }
6764
6765 if (arg->parent_root) {
6766 key.objectid = arg->parent_root;
6767 key.type = BTRFS_ROOT_ITEM_KEY;
6768 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006769
6770 index = srcu_read_lock(&fs_info->subvol_srcu);
6771
Alexander Block31db9f72012-07-25 23:19:24 +02006772 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006773 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006774 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006775 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02006776 goto out;
6777 }
Wang Shilong18f687d2014-01-07 17:25:19 +08006778
David Sterba2c686532013-12-16 17:34:17 +01006779 spin_lock(&sctx->parent_root->root_item_lock);
6780 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02006781 if (!btrfs_root_readonly(sctx->parent_root) ||
6782 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006783 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006784 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006785 ret = -EPERM;
6786 goto out;
6787 }
6788 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006789
6790 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006791 }
6792
6793 /*
6794 * Clones from send_root are allowed, but only if the clone source
6795 * is behind the current send position. This is checked while searching
6796 * for possible clone sources.
6797 */
6798 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6799
6800 /* We do a bsearch later */
6801 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6802 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6803 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08006804 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006805
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006806 ret = ensure_commit_roots_uptodate(sctx);
6807 if (ret)
6808 goto out;
6809
David Sterba2755a0d2014-07-31 00:43:18 +02006810 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02006811 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04006812 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02006813 if (ret < 0)
6814 goto out;
6815
Stefan Behrensc2c71322013-04-10 17:10:52 +00006816 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6817 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6818 if (ret < 0)
6819 goto out;
6820 ret = send_cmd(sctx);
6821 if (ret < 0)
6822 goto out;
6823 }
Alexander Block31db9f72012-07-25 23:19:24 +02006824
6825out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006826 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6827 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6828 struct rb_node *n;
6829 struct pending_dir_move *pm;
6830
6831 n = rb_first(&sctx->pending_dir_moves);
6832 pm = rb_entry(n, struct pending_dir_move, node);
6833 while (!list_empty(&pm->list)) {
6834 struct pending_dir_move *pm2;
6835
6836 pm2 = list_first_entry(&pm->list,
6837 struct pending_dir_move, list);
6838 free_pending_move(sctx, pm2);
6839 }
6840 free_pending_move(sctx, pm);
6841 }
6842
6843 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6844 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6845 struct rb_node *n;
6846 struct waiting_dir_move *dm;
6847
6848 n = rb_first(&sctx->waiting_dir_moves);
6849 dm = rb_entry(n, struct waiting_dir_move, node);
6850 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6851 kfree(dm);
6852 }
6853
Filipe Manana9dc44212014-02-19 14:31:44 +00006854 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6855 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6856 struct rb_node *n;
6857 struct orphan_dir_info *odi;
6858
6859 n = rb_first(&sctx->orphan_dirs);
6860 odi = rb_entry(n, struct orphan_dir_info, node);
6861 free_orphan_dir_info(sctx, odi);
6862 }
6863
Wang Shilong896c14f2014-01-07 17:25:18 +08006864 if (sort_clone_roots) {
6865 for (i = 0; i < sctx->clone_roots_cnt; i++)
6866 btrfs_root_dec_send_in_progress(
6867 sctx->clone_roots[i].root);
6868 } else {
6869 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6870 btrfs_root_dec_send_in_progress(
6871 sctx->clone_roots[i].root);
6872
6873 btrfs_root_dec_send_in_progress(send_root);
6874 }
David Sterba66ef7d62013-12-17 15:07:20 +01006875 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6876 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01006877
David Sterba2f913062016-04-11 18:40:08 +02006878 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006879
6880 if (sctx) {
6881 if (sctx->send_filp)
6882 fput(sctx->send_filp);
6883
David Sterbac03d01f2016-04-11 18:40:08 +02006884 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02006885 kvfree(sctx->send_buf);
David Sterbaeb5b75f2016-04-11 18:40:08 +02006886 kvfree(sctx->read_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02006887
6888 name_cache_free(sctx);
6889
6890 kfree(sctx);
6891 }
6892
6893 return ret;
6894}