blob: e4ad2fa82acf568c1f81b2f12dba4a646e606e1d [file] [log] [blame]
Alexander Block31db9f72012-07-25 23:19:24 +02001/*
2 * Copyright (C) 2012 Alexander Block. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/bsearch.h>
20#include <linux/fs.h>
21#include <linux/file.h>
22#include <linux/sort.h>
23#include <linux/mount.h>
24#include <linux/xattr.h>
25#include <linux/posix_acl_xattr.h>
26#include <linux/radix-tree.h>
Stephen Rothwella1857eb2012-07-27 10:11:13 +100027#include <linux/vmalloc.h>
Andy Shevchenkoed848852013-08-21 10:32:13 +030028#include <linux/string.h>
Alexander Block31db9f72012-07-25 23:19:24 +020029
30#include "send.h"
31#include "backref.h"
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +000032#include "hash.h"
Alexander Block31db9f72012-07-25 23:19:24 +020033#include "locking.h"
34#include "disk-io.h"
35#include "btrfs_inode.h"
36#include "transaction.h"
Anand Jainebb87652016-03-10 17:26:59 +080037#include "compression.h"
Alexander Block31db9f72012-07-25 23:19:24 +020038
Alexander Block31db9f72012-07-25 23:19:24 +020039/*
40 * A fs_path is a helper to dynamically build path names with unknown size.
41 * It reallocates the internal buffer on demand.
42 * It allows fast adding of path elements on the right side (normal path) and
43 * fast adding to the left side (reversed path). A reversed path can also be
44 * unreversed if needed.
45 */
46struct fs_path {
47 union {
48 struct {
49 char *start;
50 char *end;
Alexander Block31db9f72012-07-25 23:19:24 +020051
52 char *buf;
David Sterba1f5a7ff2014-02-03 19:23:47 +010053 unsigned short buf_len:15;
54 unsigned short reversed:1;
Alexander Block31db9f72012-07-25 23:19:24 +020055 char inline_buf[];
56 };
David Sterbaace01052014-02-05 16:17:34 +010057 /*
58 * Average path length does not exceed 200 bytes, we'll have
59 * better packing in the slab and higher chance to satisfy
60 * a allocation later during send.
61 */
62 char pad[256];
Alexander Block31db9f72012-07-25 23:19:24 +020063 };
64};
65#define FS_PATH_INLINE_SIZE \
66 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
67
68
69/* reused for each extent */
70struct clone_root {
71 struct btrfs_root *root;
72 u64 ino;
73 u64 offset;
74
75 u64 found_refs;
76};
77
78#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
79#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
80
81struct send_ctx {
82 struct file *send_filp;
83 loff_t send_off;
84 char *send_buf;
85 u32 send_size;
86 u32 send_max_size;
87 u64 total_send_size;
88 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000089 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020090
Alexander Block31db9f72012-07-25 23:19:24 +020091 struct btrfs_root *send_root;
92 struct btrfs_root *parent_root;
93 struct clone_root *clone_roots;
94 int clone_roots_cnt;
95
96 /* current state of the compare_tree call */
97 struct btrfs_path *left_path;
98 struct btrfs_path *right_path;
99 struct btrfs_key *cmp_key;
100
101 /*
102 * infos of the currently processed inode. In case of deleted inodes,
103 * these are the values from the deleted inode.
104 */
105 u64 cur_ino;
106 u64 cur_inode_gen;
107 int cur_inode_new;
108 int cur_inode_new_gen;
109 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200110 u64 cur_inode_size;
111 u64 cur_inode_mode;
Liu Bo644d1942014-02-27 17:29:01 +0800112 u64 cur_inode_rdev;
Josef Bacik16e75492013-10-22 12:18:51 -0400113 u64 cur_inode_last_extent;
Alexander Block31db9f72012-07-25 23:19:24 +0200114
115 u64 send_progress;
116
117 struct list_head new_refs;
118 struct list_head deleted_refs;
119
120 struct radix_tree_root name_cache;
121 struct list_head name_cache_list;
122 int name_cache_size;
123
Liu Bo2131bcd2014-03-05 10:07:35 +0800124 struct file_ra_state ra;
125
Alexander Block31db9f72012-07-25 23:19:24 +0200126 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000127
128 /*
129 * We process inodes by their increasing order, so if before an
130 * incremental send we reverse the parent/child relationship of
131 * directories such that a directory with a lower inode number was
132 * the parent of a directory with a higher inode number, and the one
133 * becoming the new parent got renamed too, we can't rename/move the
134 * directory with lower inode number when we finish processing it - we
135 * must process the directory with higher inode number first, then
136 * rename/move it and then rename/move the directory with lower inode
137 * number. Example follows.
138 *
139 * Tree state when the first send was performed:
140 *
141 * .
142 * |-- a (ino 257)
143 * |-- b (ino 258)
144 * |
145 * |
146 * |-- c (ino 259)
147 * | |-- d (ino 260)
148 * |
149 * |-- c2 (ino 261)
150 *
151 * Tree state when the second (incremental) send is performed:
152 *
153 * .
154 * |-- a (ino 257)
155 * |-- b (ino 258)
156 * |-- c2 (ino 261)
157 * |-- d2 (ino 260)
158 * |-- cc (ino 259)
159 *
160 * The sequence of steps that lead to the second state was:
161 *
162 * mv /a/b/c/d /a/b/c2/d2
163 * mv /a/b/c /a/b/c2/d2/cc
164 *
165 * "c" has lower inode number, but we can't move it (2nd mv operation)
166 * before we move "d", which has higher inode number.
167 *
168 * So we just memorize which move/rename operations must be performed
169 * later when their respective parent is processed and moved/renamed.
170 */
171
172 /* Indexed by parent directory inode number. */
173 struct rb_root pending_dir_moves;
174
175 /*
176 * Reverse index, indexed by the inode number of a directory that
177 * is waiting for the move/rename of its immediate parent before its
178 * own move/rename can be performed.
179 */
180 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000181
182 /*
183 * A directory that is going to be rm'ed might have a child directory
184 * which is in the pending directory moves index above. In this case,
185 * the directory can only be removed after the move/rename of its child
186 * is performed. Example:
187 *
188 * Parent snapshot:
189 *
190 * . (ino 256)
191 * |-- a/ (ino 257)
192 * |-- b/ (ino 258)
193 * |-- c/ (ino 259)
194 * | |-- x/ (ino 260)
195 * |
196 * |-- y/ (ino 261)
197 *
198 * Send snapshot:
199 *
200 * . (ino 256)
201 * |-- a/ (ino 257)
202 * |-- b/ (ino 258)
203 * |-- YY/ (ino 261)
204 * |-- x/ (ino 260)
205 *
206 * Sequence of steps that lead to the send snapshot:
207 * rm -f /a/b/c/foo.txt
208 * mv /a/b/y /a/b/YY
209 * mv /a/b/c/x /a/b/YY
210 * rmdir /a/b/c
211 *
212 * When the child is processed, its move/rename is delayed until its
213 * parent is processed (as explained above), but all other operations
214 * like update utimes, chown, chgrp, etc, are performed and the paths
215 * that it uses for those operations must use the orphanized name of
216 * its parent (the directory we're going to rm later), so we need to
217 * memorize that name.
218 *
219 * Indexed by the inode number of the directory to be deleted.
220 */
221 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000222};
223
224struct pending_dir_move {
225 struct rb_node node;
226 struct list_head list;
227 u64 parent_ino;
228 u64 ino;
229 u64 gen;
230 struct list_head update_refs;
231};
232
233struct waiting_dir_move {
234 struct rb_node node;
235 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000236 /*
237 * There might be some directory that could not be removed because it
238 * was waiting for this directory inode to be moved first. Therefore
239 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
240 */
241 u64 rmdir_ino;
Filipe Manana8b191a62015-04-09 14:09:14 +0100242 bool orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +0000243};
244
245struct orphan_dir_info {
246 struct rb_node node;
247 u64 ino;
248 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +0200249};
250
251struct name_cache_entry {
252 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200253 /*
254 * radix_tree has only 32bit entries but we need to handle 64bit inums.
255 * We use the lower 32bit of the 64bit inum to store it in the tree. If
256 * more then one inum would fall into the same entry, we use radix_list
257 * to store the additional entries. radix_list is also used to store
258 * entries where two entries have the same inum but different
259 * generations.
260 */
261 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200262 u64 ino;
263 u64 gen;
264 u64 parent_ino;
265 u64 parent_gen;
266 int ret;
267 int need_later_update;
268 int name_len;
269 char name[];
270};
271
Filipe Manana95155582016-08-01 01:50:37 +0100272static void inconsistent_snapshot_error(struct send_ctx *sctx,
273 enum btrfs_compare_tree_result result,
274 const char *what)
275{
276 const char *result_string;
277
278 switch (result) {
279 case BTRFS_COMPARE_TREE_NEW:
280 result_string = "new";
281 break;
282 case BTRFS_COMPARE_TREE_DELETED:
283 result_string = "deleted";
284 break;
285 case BTRFS_COMPARE_TREE_CHANGED:
286 result_string = "updated";
287 break;
288 case BTRFS_COMPARE_TREE_SAME:
289 ASSERT(0);
290 result_string = "unchanged";
291 break;
292 default:
293 ASSERT(0);
294 result_string = "unexpected";
295 }
296
297 btrfs_err(sctx->send_root->fs_info,
298 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
299 result_string, what, sctx->cmp_key->objectid,
300 sctx->send_root->root_key.objectid,
301 (sctx->parent_root ?
302 sctx->parent_root->root_key.objectid : 0));
303}
304
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000305static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
306
Filipe Manana9dc44212014-02-19 14:31:44 +0000307static struct waiting_dir_move *
308get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
309
310static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
311
Josef Bacik16e75492013-10-22 12:18:51 -0400312static int need_send_hole(struct send_ctx *sctx)
313{
314 return (sctx->parent_root && !sctx->cur_inode_new &&
315 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
316 S_ISREG(sctx->cur_inode_mode));
317}
318
Alexander Block31db9f72012-07-25 23:19:24 +0200319static void fs_path_reset(struct fs_path *p)
320{
321 if (p->reversed) {
322 p->start = p->buf + p->buf_len - 1;
323 p->end = p->start;
324 *p->start = 0;
325 } else {
326 p->start = p->buf;
327 p->end = p->start;
328 *p->start = 0;
329 }
330}
331
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000332static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200333{
334 struct fs_path *p;
335
David Sterbae780b0d2016-01-18 18:42:13 +0100336 p = kmalloc(sizeof(*p), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +0200337 if (!p)
338 return NULL;
339 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200340 p->buf = p->inline_buf;
341 p->buf_len = FS_PATH_INLINE_SIZE;
342 fs_path_reset(p);
343 return p;
344}
345
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000346static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200347{
348 struct fs_path *p;
349
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000350 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200351 if (!p)
352 return NULL;
353 p->reversed = 1;
354 fs_path_reset(p);
355 return p;
356}
357
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000358static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200359{
360 if (!p)
361 return;
David Sterbaace01052014-02-05 16:17:34 +0100362 if (p->buf != p->inline_buf)
363 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200364 kfree(p);
365}
366
367static int fs_path_len(struct fs_path *p)
368{
369 return p->end - p->start;
370}
371
372static int fs_path_ensure_buf(struct fs_path *p, int len)
373{
374 char *tmp_buf;
375 int path_len;
376 int old_buf_len;
377
378 len++;
379
380 if (p->buf_len >= len)
381 return 0;
382
Chris Masoncfd4a532014-04-26 05:02:03 -0700383 if (len > PATH_MAX) {
384 WARN_ON(1);
385 return -ENOMEM;
386 }
387
David Sterba1b2782c2014-02-25 19:32:59 +0100388 path_len = p->end - p->start;
389 old_buf_len = p->buf_len;
390
David Sterbaace01052014-02-05 16:17:34 +0100391 /*
392 * First time the inline_buf does not suffice
393 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100394 if (p->buf == p->inline_buf) {
David Sterbae780b0d2016-01-18 18:42:13 +0100395 tmp_buf = kmalloc(len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100396 if (tmp_buf)
397 memcpy(tmp_buf, p->buf, old_buf_len);
398 } else {
David Sterbae780b0d2016-01-18 18:42:13 +0100399 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100400 }
David Sterba9c9ca002014-02-25 19:33:08 +0100401 if (!tmp_buf)
402 return -ENOMEM;
403 p->buf = tmp_buf;
404 /*
405 * The real size of the buffer is bigger, this will let the fast path
406 * happen most of the time
407 */
408 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100409
Alexander Block31db9f72012-07-25 23:19:24 +0200410 if (p->reversed) {
411 tmp_buf = p->buf + old_buf_len - path_len - 1;
412 p->end = p->buf + p->buf_len - 1;
413 p->start = p->end - path_len;
414 memmove(p->start, tmp_buf, path_len + 1);
415 } else {
416 p->start = p->buf;
417 p->end = p->start + path_len;
418 }
419 return 0;
420}
421
David Sterbab23ab572014-02-03 19:23:19 +0100422static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
423 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200424{
425 int ret;
426 int new_len;
427
428 new_len = p->end - p->start + name_len;
429 if (p->start != p->end)
430 new_len++;
431 ret = fs_path_ensure_buf(p, new_len);
432 if (ret < 0)
433 goto out;
434
435 if (p->reversed) {
436 if (p->start != p->end)
437 *--p->start = '/';
438 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100439 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200440 } else {
441 if (p->start != p->end)
442 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100443 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200444 p->end += name_len;
445 *p->end = 0;
446 }
447
448out:
449 return ret;
450}
451
452static int fs_path_add(struct fs_path *p, const char *name, int name_len)
453{
454 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100455 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200456
David Sterbab23ab572014-02-03 19:23:19 +0100457 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200458 if (ret < 0)
459 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100460 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200461
462out:
463 return ret;
464}
465
466static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
467{
468 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100469 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200470
David Sterbab23ab572014-02-03 19:23:19 +0100471 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200472 if (ret < 0)
473 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100474 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200475
476out:
477 return ret;
478}
479
480static int fs_path_add_from_extent_buffer(struct fs_path *p,
481 struct extent_buffer *eb,
482 unsigned long off, int len)
483{
484 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100485 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200486
David Sterbab23ab572014-02-03 19:23:19 +0100487 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200488 if (ret < 0)
489 goto out;
490
David Sterbab23ab572014-02-03 19:23:19 +0100491 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200492
493out:
494 return ret;
495}
496
Alexander Block31db9f72012-07-25 23:19:24 +0200497static int fs_path_copy(struct fs_path *p, struct fs_path *from)
498{
499 int ret;
500
501 p->reversed = from->reversed;
502 fs_path_reset(p);
503
504 ret = fs_path_add_path(p, from);
505
506 return ret;
507}
508
509
510static void fs_path_unreverse(struct fs_path *p)
511{
512 char *tmp;
513 int len;
514
515 if (!p->reversed)
516 return;
517
518 tmp = p->start;
519 len = p->end - p->start;
520 p->start = p->buf;
521 p->end = p->start + len;
522 memmove(p->start, tmp, len + 1);
523 p->reversed = 0;
524}
525
526static struct btrfs_path *alloc_path_for_send(void)
527{
528 struct btrfs_path *path;
529
530 path = btrfs_alloc_path();
531 if (!path)
532 return NULL;
533 path->search_commit_root = 1;
534 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400535 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200536 return path;
537}
538
Eric Sandeen48a3b632013-04-25 20:41:01 +0000539static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200540{
541 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200542 u32 pos = 0;
543
Alexander Block31db9f72012-07-25 23:19:24 +0200544 while (pos < len) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200545 ret = kernel_write(filp, buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200546 /* TODO handle that correctly */
547 /*if (ret == -ERESTARTSYS) {
548 continue;
549 }*/
550 if (ret < 0)
Christoph Hellwig8e931572017-09-01 17:39:19 +0200551 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200552 if (ret == 0) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200553 return -EIO;
Alexander Block31db9f72012-07-25 23:19:24 +0200554 }
555 pos += ret;
556 }
557
Christoph Hellwig8e931572017-09-01 17:39:19 +0200558 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200559}
560
561static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
562{
563 struct btrfs_tlv_header *hdr;
564 int total_len = sizeof(*hdr) + len;
565 int left = sctx->send_max_size - sctx->send_size;
566
567 if (unlikely(left < total_len))
568 return -EOVERFLOW;
569
570 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
571 hdr->tlv_type = cpu_to_le16(attr);
572 hdr->tlv_len = cpu_to_le16(len);
573 memcpy(hdr + 1, data, len);
574 sctx->send_size += total_len;
575
576 return 0;
577}
578
David Sterba95bc79d2013-12-16 17:34:10 +0100579#define TLV_PUT_DEFINE_INT(bits) \
580 static int tlv_put_u##bits(struct send_ctx *sctx, \
581 u##bits attr, u##bits value) \
582 { \
583 __le##bits __tmp = cpu_to_le##bits(value); \
584 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
585 }
Alexander Block31db9f72012-07-25 23:19:24 +0200586
David Sterba95bc79d2013-12-16 17:34:10 +0100587TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200588
589static int tlv_put_string(struct send_ctx *sctx, u16 attr,
590 const char *str, int len)
591{
592 if (len == -1)
593 len = strlen(str);
594 return tlv_put(sctx, attr, str, len);
595}
596
597static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
598 const u8 *uuid)
599{
600 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
601}
602
Alexander Block31db9f72012-07-25 23:19:24 +0200603static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
604 struct extent_buffer *eb,
605 struct btrfs_timespec *ts)
606{
607 struct btrfs_timespec bts;
608 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
609 return tlv_put(sctx, attr, &bts, sizeof(bts));
610}
611
612
613#define TLV_PUT(sctx, attrtype, attrlen, data) \
614 do { \
615 ret = tlv_put(sctx, attrtype, attrlen, data); \
616 if (ret < 0) \
617 goto tlv_put_failure; \
618 } while (0)
619
620#define TLV_PUT_INT(sctx, attrtype, bits, value) \
621 do { \
622 ret = tlv_put_u##bits(sctx, attrtype, value); \
623 if (ret < 0) \
624 goto tlv_put_failure; \
625 } while (0)
626
627#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
628#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
629#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
630#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
631#define TLV_PUT_STRING(sctx, attrtype, str, len) \
632 do { \
633 ret = tlv_put_string(sctx, attrtype, str, len); \
634 if (ret < 0) \
635 goto tlv_put_failure; \
636 } while (0)
637#define TLV_PUT_PATH(sctx, attrtype, p) \
638 do { \
639 ret = tlv_put_string(sctx, attrtype, p->start, \
640 p->end - p->start); \
641 if (ret < 0) \
642 goto tlv_put_failure; \
643 } while(0)
644#define TLV_PUT_UUID(sctx, attrtype, uuid) \
645 do { \
646 ret = tlv_put_uuid(sctx, attrtype, uuid); \
647 if (ret < 0) \
648 goto tlv_put_failure; \
649 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200650#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
651 do { \
652 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
653 if (ret < 0) \
654 goto tlv_put_failure; \
655 } while (0)
656
657static int send_header(struct send_ctx *sctx)
658{
659 struct btrfs_stream_header hdr;
660
661 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
662 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
663
Anand Jain1bcea352012-09-14 00:04:21 -0600664 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
665 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200666}
667
668/*
669 * For each command/item we want to send to userspace, we call this function.
670 */
671static int begin_cmd(struct send_ctx *sctx, int cmd)
672{
673 struct btrfs_cmd_header *hdr;
674
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530675 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200676 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200677
678 BUG_ON(sctx->send_size);
679
680 sctx->send_size += sizeof(*hdr);
681 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
682 hdr->cmd = cpu_to_le16(cmd);
683
684 return 0;
685}
686
687static int send_cmd(struct send_ctx *sctx)
688{
689 int ret;
690 struct btrfs_cmd_header *hdr;
691 u32 crc;
692
693 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
694 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
695 hdr->crc = 0;
696
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000697 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200698 hdr->crc = cpu_to_le32(crc);
699
Anand Jain1bcea352012-09-14 00:04:21 -0600700 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
701 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200702
703 sctx->total_send_size += sctx->send_size;
704 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
705 sctx->send_size = 0;
706
707 return ret;
708}
709
710/*
711 * Sends a move instruction to user space
712 */
713static int send_rename(struct send_ctx *sctx,
714 struct fs_path *from, struct fs_path *to)
715{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400716 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200717 int ret;
718
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400719 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200720
721 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
722 if (ret < 0)
723 goto out;
724
725 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
726 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
727
728 ret = send_cmd(sctx);
729
730tlv_put_failure:
731out:
732 return ret;
733}
734
735/*
736 * Sends a link instruction to user space
737 */
738static int send_link(struct send_ctx *sctx,
739 struct fs_path *path, struct fs_path *lnk)
740{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400741 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200742 int ret;
743
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400744 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200745
746 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
747 if (ret < 0)
748 goto out;
749
750 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
751 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
752
753 ret = send_cmd(sctx);
754
755tlv_put_failure:
756out:
757 return ret;
758}
759
760/*
761 * Sends an unlink instruction to user space
762 */
763static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
764{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400765 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200766 int ret;
767
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400768 btrfs_debug(fs_info, "send_unlink %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200769
770 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
771 if (ret < 0)
772 goto out;
773
774 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
775
776 ret = send_cmd(sctx);
777
778tlv_put_failure:
779out:
780 return ret;
781}
782
783/*
784 * Sends a rmdir instruction to user space
785 */
786static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
787{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400788 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200789 int ret;
790
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400791 btrfs_debug(fs_info, "send_rmdir %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200792
793 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
794 if (ret < 0)
795 goto out;
796
797 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
798
799 ret = send_cmd(sctx);
800
801tlv_put_failure:
802out:
803 return ret;
804}
805
806/*
807 * Helper function to retrieve some fields from an inode item.
808 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400809static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
810 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
811 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200812{
813 int ret;
814 struct btrfs_inode_item *ii;
815 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200816
817 key.objectid = ino;
818 key.type = BTRFS_INODE_ITEM_KEY;
819 key.offset = 0;
820 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200821 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400822 if (ret > 0)
823 ret = -ENOENT;
824 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200825 }
826
827 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
828 struct btrfs_inode_item);
829 if (size)
830 *size = btrfs_inode_size(path->nodes[0], ii);
831 if (gen)
832 *gen = btrfs_inode_generation(path->nodes[0], ii);
833 if (mode)
834 *mode = btrfs_inode_mode(path->nodes[0], ii);
835 if (uid)
836 *uid = btrfs_inode_uid(path->nodes[0], ii);
837 if (gid)
838 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200839 if (rdev)
840 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200841
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400842 return ret;
843}
844
845static int get_inode_info(struct btrfs_root *root,
846 u64 ino, u64 *size, u64 *gen,
847 u64 *mode, u64 *uid, u64 *gid,
848 u64 *rdev)
849{
850 struct btrfs_path *path;
851 int ret;
852
853 path = alloc_path_for_send();
854 if (!path)
855 return -ENOMEM;
856 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
857 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200858 btrfs_free_path(path);
859 return ret;
860}
861
862typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
863 struct fs_path *p,
864 void *ctx);
865
866/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000867 * Helper function to iterate the entries in ONE btrfs_inode_ref or
868 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200869 * The iterate callback may return a non zero value to stop iteration. This can
870 * be a negative value for error codes or 1 to simply stop it.
871 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000872 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200873 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000874static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200875 struct btrfs_key *found_key, int resolve,
876 iterate_inode_ref_t iterate, void *ctx)
877{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000878 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200879 struct btrfs_item *item;
880 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000881 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200882 struct btrfs_path *tmp_path;
883 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000884 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200885 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000886 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200887 u32 name_len;
888 char *start;
889 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000890 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200891 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000892 u64 dir;
893 unsigned long name_off;
894 unsigned long elem_size;
895 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200896
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000897 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200898 if (!p)
899 return -ENOMEM;
900
901 tmp_path = alloc_path_for_send();
902 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000903 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200904 return -ENOMEM;
905 }
906
Alexander Block31db9f72012-07-25 23:19:24 +0200907
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000908 if (found_key->type == BTRFS_INODE_REF_KEY) {
909 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
910 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100911 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000912 total = btrfs_item_size(eb, item);
913 elem_size = sizeof(*iref);
914 } else {
915 ptr = btrfs_item_ptr_offset(eb, slot);
916 total = btrfs_item_size_nr(eb, slot);
917 elem_size = sizeof(*extref);
918 }
919
Alexander Block31db9f72012-07-25 23:19:24 +0200920 while (cur < total) {
921 fs_path_reset(p);
922
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000923 if (found_key->type == BTRFS_INODE_REF_KEY) {
924 iref = (struct btrfs_inode_ref *)(ptr + cur);
925 name_len = btrfs_inode_ref_name_len(eb, iref);
926 name_off = (unsigned long)(iref + 1);
927 index = btrfs_inode_ref_index(eb, iref);
928 dir = found_key->offset;
929 } else {
930 extref = (struct btrfs_inode_extref *)(ptr + cur);
931 name_len = btrfs_inode_extref_name_len(eb, extref);
932 name_off = (unsigned long)&extref->name;
933 index = btrfs_inode_extref_index(eb, extref);
934 dir = btrfs_inode_extref_parent(eb, extref);
935 }
936
Alexander Block31db9f72012-07-25 23:19:24 +0200937 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000938 start = btrfs_ref_to_path(root, tmp_path, name_len,
939 name_off, eb, dir,
940 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200941 if (IS_ERR(start)) {
942 ret = PTR_ERR(start);
943 goto out;
944 }
945 if (start < p->buf) {
946 /* overflow , try again with larger buffer */
947 ret = fs_path_ensure_buf(p,
948 p->buf_len + p->buf - start);
949 if (ret < 0)
950 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000951 start = btrfs_ref_to_path(root, tmp_path,
952 name_len, name_off,
953 eb, dir,
954 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200955 if (IS_ERR(start)) {
956 ret = PTR_ERR(start);
957 goto out;
958 }
959 BUG_ON(start < p->buf);
960 }
961 p->start = start;
962 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000963 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
964 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200965 if (ret < 0)
966 goto out;
967 }
968
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000969 cur += elem_size + name_len;
970 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200971 if (ret)
972 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200973 num++;
974 }
975
976out:
977 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000978 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200979 return ret;
980}
981
982typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
983 const char *name, int name_len,
984 const char *data, int data_len,
985 u8 type, void *ctx);
986
987/*
988 * Helper function to iterate the entries in ONE btrfs_dir_item.
989 * The iterate callback may return a non zero value to stop iteration. This can
990 * be a negative value for error codes or 1 to simply stop it.
991 *
992 * path must point to the dir item when called.
993 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000994static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200995 iterate_dir_item_t iterate, void *ctx)
996{
997 int ret = 0;
998 struct extent_buffer *eb;
999 struct btrfs_item *item;
1000 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +02001001 struct btrfs_key di_key;
1002 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +01001003 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +02001004 u32 name_len;
1005 u32 data_len;
1006 u32 cur;
1007 u32 len;
1008 u32 total;
1009 int slot;
1010 int num;
1011 u8 type;
1012
Filipe Manana4395e0c2014-08-20 10:45:45 +01001013 /*
1014 * Start with a small buffer (1 page). If later we end up needing more
1015 * space, which can happen for xattrs on a fs with a leaf size greater
1016 * then the page size, attempt to increase the buffer. Typically xattr
1017 * values are small.
1018 */
1019 buf_len = PATH_MAX;
David Sterbae780b0d2016-01-18 18:42:13 +01001020 buf = kmalloc(buf_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02001021 if (!buf) {
1022 ret = -ENOMEM;
1023 goto out;
1024 }
1025
Alexander Block31db9f72012-07-25 23:19:24 +02001026 eb = path->nodes[0];
1027 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001028 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001029 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1030 cur = 0;
1031 len = 0;
1032 total = btrfs_item_size(eb, item);
1033
1034 num = 0;
1035 while (cur < total) {
1036 name_len = btrfs_dir_name_len(eb, di);
1037 data_len = btrfs_dir_data_len(eb, di);
1038 type = btrfs_dir_type(eb, di);
1039 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1040
Filipe Manana7e3ae332014-05-23 20:15:16 +01001041 if (type == BTRFS_FT_XATTR) {
1042 if (name_len > XATTR_NAME_MAX) {
1043 ret = -ENAMETOOLONG;
1044 goto out;
1045 }
Jeff Mahoneyda170662016-06-15 09:22:56 -04001046 if (name_len + data_len >
1047 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001048 ret = -E2BIG;
1049 goto out;
1050 }
1051 } else {
1052 /*
1053 * Path too long
1054 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001055 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001056 ret = -ENAMETOOLONG;
1057 goto out;
1058 }
Alexander Block31db9f72012-07-25 23:19:24 +02001059 }
1060
Su Yue59b0a7f2017-06-06 17:57:05 +08001061 ret = btrfs_is_name_len_valid(eb, path->slots[0],
1062 (unsigned long)(di + 1), name_len + data_len);
1063 if (!ret) {
1064 ret = -EIO;
1065 goto out;
1066 }
Filipe Manana4395e0c2014-08-20 10:45:45 +01001067 if (name_len + data_len > buf_len) {
1068 buf_len = name_len + data_len;
1069 if (is_vmalloc_addr(buf)) {
1070 vfree(buf);
1071 buf = NULL;
1072 } else {
1073 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001074 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001075
1076 if (!tmp)
1077 kfree(buf);
1078 buf = tmp;
1079 }
1080 if (!buf) {
David Sterbaf11f7442017-05-31 18:40:02 +02001081 buf = kvmalloc(buf_len, GFP_KERNEL);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001082 if (!buf) {
1083 ret = -ENOMEM;
1084 goto out;
1085 }
1086 }
1087 }
1088
Alexander Block31db9f72012-07-25 23:19:24 +02001089 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1090 name_len + data_len);
1091
1092 len = sizeof(*di) + name_len + data_len;
1093 di = (struct btrfs_dir_item *)((char *)di + len);
1094 cur += len;
1095
1096 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1097 data_len, type, ctx);
1098 if (ret < 0)
1099 goto out;
1100 if (ret) {
1101 ret = 0;
1102 goto out;
1103 }
1104
1105 num++;
1106 }
1107
1108out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001109 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001110 return ret;
1111}
1112
1113static int __copy_first_ref(int num, u64 dir, int index,
1114 struct fs_path *p, void *ctx)
1115{
1116 int ret;
1117 struct fs_path *pt = ctx;
1118
1119 ret = fs_path_copy(pt, p);
1120 if (ret < 0)
1121 return ret;
1122
1123 /* we want the first only */
1124 return 1;
1125}
1126
1127/*
1128 * Retrieve the first path of an inode. If an inode has more then one
1129 * ref/hardlink, this is ignored.
1130 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001131static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001132 u64 ino, struct fs_path *path)
1133{
1134 int ret;
1135 struct btrfs_key key, found_key;
1136 struct btrfs_path *p;
1137
1138 p = alloc_path_for_send();
1139 if (!p)
1140 return -ENOMEM;
1141
1142 fs_path_reset(path);
1143
1144 key.objectid = ino;
1145 key.type = BTRFS_INODE_REF_KEY;
1146 key.offset = 0;
1147
1148 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1149 if (ret < 0)
1150 goto out;
1151 if (ret) {
1152 ret = 1;
1153 goto out;
1154 }
1155 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1156 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001157 (found_key.type != BTRFS_INODE_REF_KEY &&
1158 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001159 ret = -ENOENT;
1160 goto out;
1161 }
1162
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001163 ret = iterate_inode_ref(root, p, &found_key, 1,
1164 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001165 if (ret < 0)
1166 goto out;
1167 ret = 0;
1168
1169out:
1170 btrfs_free_path(p);
1171 return ret;
1172}
1173
1174struct backref_ctx {
1175 struct send_ctx *sctx;
1176
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001177 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001178 /* number of total found references */
1179 u64 found;
1180
1181 /*
1182 * used for clones found in send_root. clones found behind cur_objectid
1183 * and cur_offset are not considered as allowed clones.
1184 */
1185 u64 cur_objectid;
1186 u64 cur_offset;
1187
1188 /* may be truncated in case it's the last extent in a file */
1189 u64 extent_len;
1190
Filipe Manana619d8c42015-05-03 01:56:00 +01001191 /* data offset in the file extent item */
1192 u64 data_offset;
1193
Alexander Block31db9f72012-07-25 23:19:24 +02001194 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001195 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001196};
1197
1198static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1199{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001200 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001201 struct clone_root *cr = (struct clone_root *)elt;
1202
1203 if (root < cr->root->objectid)
1204 return -1;
1205 if (root > cr->root->objectid)
1206 return 1;
1207 return 0;
1208}
1209
1210static int __clone_root_cmp_sort(const void *e1, const void *e2)
1211{
1212 struct clone_root *cr1 = (struct clone_root *)e1;
1213 struct clone_root *cr2 = (struct clone_root *)e2;
1214
1215 if (cr1->root->objectid < cr2->root->objectid)
1216 return -1;
1217 if (cr1->root->objectid > cr2->root->objectid)
1218 return 1;
1219 return 0;
1220}
1221
1222/*
1223 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001224 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001225 */
1226static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1227{
1228 struct backref_ctx *bctx = ctx_;
1229 struct clone_root *found;
1230 int ret;
1231 u64 i_size;
1232
1233 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001234 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001235 bctx->sctx->clone_roots_cnt,
1236 sizeof(struct clone_root),
1237 __clone_root_cmp_bsearch);
1238 if (!found)
1239 return 0;
1240
1241 if (found->root == bctx->sctx->send_root &&
1242 ino == bctx->cur_objectid &&
1243 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001244 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001245 }
1246
1247 /*
Alexander Block766702e2012-07-28 14:11:31 +02001248 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001249 * accept clones from these extents.
1250 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001251 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1252 NULL, NULL, NULL);
1253 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001254 if (ret < 0)
1255 return ret;
1256
Filipe Manana619d8c42015-05-03 01:56:00 +01001257 if (offset + bctx->data_offset + bctx->extent_len > i_size)
Alexander Block31db9f72012-07-25 23:19:24 +02001258 return 0;
1259
1260 /*
1261 * Make sure we don't consider clones from send_root that are
1262 * behind the current inode/offset.
1263 */
1264 if (found->root == bctx->sctx->send_root) {
1265 /*
1266 * TODO for the moment we don't accept clones from the inode
1267 * that is currently send. We may change this when
1268 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1269 * file.
1270 */
1271 if (ino >= bctx->cur_objectid)
1272 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001273#if 0
1274 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001275 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001276 if (offset + bctx->extent_len > bctx->cur_offset)
1277 return 0;
1278#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001279 }
1280
1281 bctx->found++;
1282 found->found_refs++;
1283 if (ino < found->ino) {
1284 found->ino = ino;
1285 found->offset = offset;
1286 } else if (found->ino == ino) {
1287 /*
1288 * same extent found more then once in the same file.
1289 */
1290 if (found->offset > offset + bctx->extent_len)
1291 found->offset = offset;
1292 }
1293
1294 return 0;
1295}
1296
1297/*
Alexander Block766702e2012-07-28 14:11:31 +02001298 * Given an inode, offset and extent item, it finds a good clone for a clone
1299 * instruction. Returns -ENOENT when none could be found. The function makes
1300 * sure that the returned clone is usable at the point where sending is at the
1301 * moment. This means, that no clones are accepted which lie behind the current
1302 * inode+offset.
1303 *
Alexander Block31db9f72012-07-25 23:19:24 +02001304 * path must point to the extent item when called.
1305 */
1306static int find_extent_clone(struct send_ctx *sctx,
1307 struct btrfs_path *path,
1308 u64 ino, u64 data_offset,
1309 u64 ino_size,
1310 struct clone_root **found)
1311{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001312 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001313 int ret;
1314 int extent_type;
1315 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001316 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001317 u64 num_bytes;
1318 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001319 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001320 struct btrfs_file_extent_item *fi;
1321 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001322 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001323 struct clone_root *cur_clone_root;
1324 struct btrfs_key found_key;
1325 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001326 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001327 u32 i;
1328
1329 tmp_path = alloc_path_for_send();
1330 if (!tmp_path)
1331 return -ENOMEM;
1332
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001333 /* We only use this path under the commit sem */
1334 tmp_path->need_commit_sem = 0;
1335
David Sterbae780b0d2016-01-18 18:42:13 +01001336 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001337 if (!backref_ctx) {
1338 ret = -ENOMEM;
1339 goto out;
1340 }
1341
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001342 backref_ctx->path = tmp_path;
1343
Alexander Block31db9f72012-07-25 23:19:24 +02001344 if (data_offset >= ino_size) {
1345 /*
1346 * There may be extents that lie behind the file's size.
1347 * I at least had this in combination with snapshotting while
1348 * writing large files.
1349 */
1350 ret = 0;
1351 goto out;
1352 }
1353
1354 fi = btrfs_item_ptr(eb, path->slots[0],
1355 struct btrfs_file_extent_item);
1356 extent_type = btrfs_file_extent_type(eb, fi);
1357 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1358 ret = -ENOENT;
1359 goto out;
1360 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001361 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001362
1363 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001364 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1365 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001366 ret = -ENOENT;
1367 goto out;
1368 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001369 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001370
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001371 down_read(&fs_info->commit_root_sem);
1372 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001373 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001374 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001375 btrfs_release_path(tmp_path);
1376
1377 if (ret < 0)
1378 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001379 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001380 ret = -EIO;
1381 goto out;
1382 }
1383
1384 /*
1385 * Setup the clone roots.
1386 */
1387 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1388 cur_clone_root = sctx->clone_roots + i;
1389 cur_clone_root->ino = (u64)-1;
1390 cur_clone_root->offset = 0;
1391 cur_clone_root->found_refs = 0;
1392 }
1393
Alexander Block35075bb2012-07-28 12:44:34 +02001394 backref_ctx->sctx = sctx;
1395 backref_ctx->found = 0;
1396 backref_ctx->cur_objectid = ino;
1397 backref_ctx->cur_offset = data_offset;
1398 backref_ctx->found_itself = 0;
1399 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001400 /*
1401 * For non-compressed extents iterate_extent_inodes() gives us extent
1402 * offsets that already take into account the data offset, but not for
1403 * compressed extents, since the offset is logical and not relative to
1404 * the physical extent locations. We must take this into account to
1405 * avoid sending clone offsets that go beyond the source file's size,
1406 * which would result in the clone ioctl failing with -EINVAL on the
1407 * receiving end.
1408 */
1409 if (compressed == BTRFS_COMPRESS_NONE)
1410 backref_ctx->data_offset = 0;
1411 else
1412 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001413
1414 /*
1415 * The last extent of a file may be too large due to page alignment.
1416 * We need to adjust extent_len in this case so that the checks in
1417 * __iterate_backrefs work.
1418 */
1419 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001420 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001421
1422 /*
1423 * Now collect all backrefs.
1424 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001425 if (compressed == BTRFS_COMPRESS_NONE)
1426 extent_item_pos = logical - found_key.objectid;
1427 else
1428 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001429 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1430 extent_item_pos, 1, __iterate_backrefs,
1431 backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001432
Alexander Block31db9f72012-07-25 23:19:24 +02001433 if (ret < 0)
1434 goto out;
1435
Alexander Block35075bb2012-07-28 12:44:34 +02001436 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001437 /* found a bug in backref code? */
1438 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001439 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001440 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001441 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001442 goto out;
1443 }
1444
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001445 btrfs_debug(fs_info,
1446 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1447 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001448
Alexander Block35075bb2012-07-28 12:44:34 +02001449 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001450 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001451
1452 cur_clone_root = NULL;
1453 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1454 if (sctx->clone_roots[i].found_refs) {
1455 if (!cur_clone_root)
1456 cur_clone_root = sctx->clone_roots + i;
1457 else if (sctx->clone_roots[i].root == sctx->send_root)
1458 /* prefer clones from send_root over others */
1459 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001460 }
1461
1462 }
1463
1464 if (cur_clone_root) {
1465 *found = cur_clone_root;
1466 ret = 0;
1467 } else {
1468 ret = -ENOENT;
1469 }
1470
1471out:
1472 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001473 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001474 return ret;
1475}
1476
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001477static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001478 u64 ino,
1479 struct fs_path *dest)
1480{
1481 int ret;
1482 struct btrfs_path *path;
1483 struct btrfs_key key;
1484 struct btrfs_file_extent_item *ei;
1485 u8 type;
1486 u8 compression;
1487 unsigned long off;
1488 int len;
1489
1490 path = alloc_path_for_send();
1491 if (!path)
1492 return -ENOMEM;
1493
1494 key.objectid = ino;
1495 key.type = BTRFS_EXTENT_DATA_KEY;
1496 key.offset = 0;
1497 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1498 if (ret < 0)
1499 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001500 if (ret) {
1501 /*
1502 * An empty symlink inode. Can happen in rare error paths when
1503 * creating a symlink (transaction committed before the inode
1504 * eviction handler removed the symlink inode items and a crash
1505 * happened in between or the subvol was snapshoted in between).
1506 * Print an informative message to dmesg/syslog so that the user
1507 * can delete the symlink.
1508 */
1509 btrfs_err(root->fs_info,
1510 "Found empty symlink inode %llu at root %llu",
1511 ino, root->root_key.objectid);
1512 ret = -EIO;
1513 goto out;
1514 }
Alexander Block31db9f72012-07-25 23:19:24 +02001515
1516 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1517 struct btrfs_file_extent_item);
1518 type = btrfs_file_extent_type(path->nodes[0], ei);
1519 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1520 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1521 BUG_ON(compression);
1522
1523 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001524 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001525
1526 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001527
1528out:
1529 btrfs_free_path(path);
1530 return ret;
1531}
1532
1533/*
1534 * Helper function to generate a file name that is unique in the root of
1535 * send_root and parent_root. This is used to generate names for orphan inodes.
1536 */
1537static int gen_unique_name(struct send_ctx *sctx,
1538 u64 ino, u64 gen,
1539 struct fs_path *dest)
1540{
1541 int ret = 0;
1542 struct btrfs_path *path;
1543 struct btrfs_dir_item *di;
1544 char tmp[64];
1545 int len;
1546 u64 idx = 0;
1547
1548 path = alloc_path_for_send();
1549 if (!path)
1550 return -ENOMEM;
1551
1552 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001553 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001554 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001555 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001556
1557 di = btrfs_lookup_dir_item(NULL, sctx->send_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
1571 if (!sctx->parent_root) {
1572 /* unique */
1573 ret = 0;
1574 break;
1575 }
1576
1577 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1578 path, BTRFS_FIRST_FREE_OBJECTID,
1579 tmp, strlen(tmp), 0);
1580 btrfs_release_path(path);
1581 if (IS_ERR(di)) {
1582 ret = PTR_ERR(di);
1583 goto out;
1584 }
1585 if (di) {
1586 /* not unique, try again */
1587 idx++;
1588 continue;
1589 }
1590 /* unique */
1591 break;
1592 }
1593
1594 ret = fs_path_add(dest, tmp, strlen(tmp));
1595
1596out:
1597 btrfs_free_path(path);
1598 return ret;
1599}
1600
1601enum inode_state {
1602 inode_state_no_change,
1603 inode_state_will_create,
1604 inode_state_did_create,
1605 inode_state_will_delete,
1606 inode_state_did_delete,
1607};
1608
1609static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1610{
1611 int ret;
1612 int left_ret;
1613 int right_ret;
1614 u64 left_gen;
1615 u64 right_gen;
1616
1617 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001618 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001619 if (ret < 0 && ret != -ENOENT)
1620 goto out;
1621 left_ret = ret;
1622
1623 if (!sctx->parent_root) {
1624 right_ret = -ENOENT;
1625 } else {
1626 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001627 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001628 if (ret < 0 && ret != -ENOENT)
1629 goto out;
1630 right_ret = ret;
1631 }
1632
1633 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001634 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001635 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001636 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001637 if (ino < sctx->send_progress)
1638 ret = inode_state_did_create;
1639 else
1640 ret = inode_state_will_create;
1641 } else if (right_gen == gen) {
1642 if (ino < sctx->send_progress)
1643 ret = inode_state_did_delete;
1644 else
1645 ret = inode_state_will_delete;
1646 } else {
1647 ret = -ENOENT;
1648 }
1649 } else if (!left_ret) {
1650 if (left_gen == gen) {
1651 if (ino < sctx->send_progress)
1652 ret = inode_state_did_create;
1653 else
1654 ret = inode_state_will_create;
1655 } else {
1656 ret = -ENOENT;
1657 }
1658 } else if (!right_ret) {
1659 if (right_gen == gen) {
1660 if (ino < sctx->send_progress)
1661 ret = inode_state_did_delete;
1662 else
1663 ret = inode_state_will_delete;
1664 } else {
1665 ret = -ENOENT;
1666 }
1667 } else {
1668 ret = -ENOENT;
1669 }
1670
1671out:
1672 return ret;
1673}
1674
1675static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1676{
1677 int ret;
1678
Robbie Ko4dd99202017-01-05 16:24:55 +08001679 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1680 return 1;
1681
Alexander Block31db9f72012-07-25 23:19:24 +02001682 ret = get_cur_inode_state(sctx, ino, gen);
1683 if (ret < 0)
1684 goto out;
1685
1686 if (ret == inode_state_no_change ||
1687 ret == inode_state_did_create ||
1688 ret == inode_state_will_delete)
1689 ret = 1;
1690 else
1691 ret = 0;
1692
1693out:
1694 return ret;
1695}
1696
1697/*
1698 * Helper function to lookup a dir item in a dir.
1699 */
1700static int lookup_dir_item_inode(struct btrfs_root *root,
1701 u64 dir, const char *name, int name_len,
1702 u64 *found_inode,
1703 u8 *found_type)
1704{
1705 int ret = 0;
1706 struct btrfs_dir_item *di;
1707 struct btrfs_key key;
1708 struct btrfs_path *path;
1709
1710 path = alloc_path_for_send();
1711 if (!path)
1712 return -ENOMEM;
1713
1714 di = btrfs_lookup_dir_item(NULL, root, path,
1715 dir, name, name_len, 0);
1716 if (!di) {
1717 ret = -ENOENT;
1718 goto out;
1719 }
1720 if (IS_ERR(di)) {
1721 ret = PTR_ERR(di);
1722 goto out;
1723 }
1724 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001725 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1726 ret = -ENOENT;
1727 goto out;
1728 }
Alexander Block31db9f72012-07-25 23:19:24 +02001729 *found_inode = key.objectid;
1730 *found_type = btrfs_dir_type(path->nodes[0], di);
1731
1732out:
1733 btrfs_free_path(path);
1734 return ret;
1735}
1736
Alexander Block766702e2012-07-28 14:11:31 +02001737/*
1738 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1739 * generation of the parent dir and the name of the dir entry.
1740 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001741static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001742 u64 *dir, u64 *dir_gen, struct fs_path *name)
1743{
1744 int ret;
1745 struct btrfs_key key;
1746 struct btrfs_key found_key;
1747 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001748 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001749 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001750
1751 path = alloc_path_for_send();
1752 if (!path)
1753 return -ENOMEM;
1754
1755 key.objectid = ino;
1756 key.type = BTRFS_INODE_REF_KEY;
1757 key.offset = 0;
1758
1759 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1760 if (ret < 0)
1761 goto out;
1762 if (!ret)
1763 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1764 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001765 if (ret || found_key.objectid != ino ||
1766 (found_key.type != BTRFS_INODE_REF_KEY &&
1767 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001768 ret = -ENOENT;
1769 goto out;
1770 }
1771
Filipe Manana51a60252014-05-13 22:01:02 +01001772 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001773 struct btrfs_inode_ref *iref;
1774 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1775 struct btrfs_inode_ref);
1776 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1777 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1778 (unsigned long)(iref + 1),
1779 len);
1780 parent_dir = found_key.offset;
1781 } else {
1782 struct btrfs_inode_extref *extref;
1783 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1784 struct btrfs_inode_extref);
1785 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1786 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1787 (unsigned long)&extref->name, len);
1788 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1789 }
Alexander Block31db9f72012-07-25 23:19:24 +02001790 if (ret < 0)
1791 goto out;
1792 btrfs_release_path(path);
1793
Filipe Mananab46ab972014-03-21 12:46:54 +00001794 if (dir_gen) {
1795 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1796 NULL, NULL, NULL);
1797 if (ret < 0)
1798 goto out;
1799 }
Alexander Block31db9f72012-07-25 23:19:24 +02001800
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001801 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001802
1803out:
1804 btrfs_free_path(path);
1805 return ret;
1806}
1807
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001808static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001809 u64 ino, u64 dir,
1810 const char *name, int name_len)
1811{
1812 int ret;
1813 struct fs_path *tmp_name;
1814 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001815
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001816 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001817 if (!tmp_name)
1818 return -ENOMEM;
1819
Filipe Mananab46ab972014-03-21 12:46:54 +00001820 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001821 if (ret < 0)
1822 goto out;
1823
Alexander Blockb9291af2012-07-28 11:07:18 +02001824 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001825 ret = 0;
1826 goto out;
1827 }
1828
Alexander Blocke938c8a2012-07-28 16:33:49 +02001829 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001830
1831out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001832 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001833 return ret;
1834}
1835
Alexander Block766702e2012-07-28 14:11:31 +02001836/*
1837 * Used by process_recorded_refs to determine if a new ref would overwrite an
1838 * already existing ref. In case it detects an overwrite, it returns the
1839 * inode/gen in who_ino/who_gen.
1840 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1841 * to make sure later references to the overwritten inode are possible.
1842 * Orphanizing is however only required for the first ref of an inode.
1843 * process_recorded_refs does an additional is_first_ref check to see if
1844 * orphanizing is really required.
1845 */
Alexander Block31db9f72012-07-25 23:19:24 +02001846static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1847 const char *name, int name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01001848 u64 *who_ino, u64 *who_gen, u64 *who_mode)
Alexander Block31db9f72012-07-25 23:19:24 +02001849{
1850 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001851 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001852 u64 other_inode = 0;
1853 u8 other_type = 0;
1854
1855 if (!sctx->parent_root)
1856 goto out;
1857
1858 ret = is_inode_existent(sctx, dir, dir_gen);
1859 if (ret <= 0)
1860 goto out;
1861
Josef Bacikebdad912013-08-06 16:47:48 -04001862 /*
1863 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001864 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001865 * and we can just unlink this entry.
1866 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001867 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001868 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1869 NULL, NULL, NULL);
1870 if (ret < 0 && ret != -ENOENT)
1871 goto out;
1872 if (ret) {
1873 ret = 0;
1874 goto out;
1875 }
1876 if (gen != dir_gen)
1877 goto out;
1878 }
1879
Alexander Block31db9f72012-07-25 23:19:24 +02001880 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1881 &other_inode, &other_type);
1882 if (ret < 0 && ret != -ENOENT)
1883 goto out;
1884 if (ret) {
1885 ret = 0;
1886 goto out;
1887 }
1888
Alexander Block766702e2012-07-28 14:11:31 +02001889 /*
1890 * Check if the overwritten ref was already processed. If yes, the ref
1891 * was already unlinked/moved, so we can safely assume that we will not
1892 * overwrite anything at this point in time.
1893 */
Robbie Ko801bec32015-06-23 18:39:46 +08001894 if (other_inode > sctx->send_progress ||
1895 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001896 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Filipe Mananaf5962782017-06-22 20:03:51 +01001897 who_gen, who_mode, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001898 if (ret < 0)
1899 goto out;
1900
1901 ret = 1;
1902 *who_ino = other_inode;
1903 } else {
1904 ret = 0;
1905 }
1906
1907out:
1908 return ret;
1909}
1910
Alexander Block766702e2012-07-28 14:11:31 +02001911/*
1912 * Checks if the ref was overwritten by an already processed inode. This is
1913 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1914 * thus the orphan name needs be used.
1915 * process_recorded_refs also uses it to avoid unlinking of refs that were
1916 * overwritten.
1917 */
Alexander Block31db9f72012-07-25 23:19:24 +02001918static int did_overwrite_ref(struct send_ctx *sctx,
1919 u64 dir, u64 dir_gen,
1920 u64 ino, u64 ino_gen,
1921 const char *name, int name_len)
1922{
1923 int ret = 0;
1924 u64 gen;
1925 u64 ow_inode;
1926 u8 other_type;
1927
1928 if (!sctx->parent_root)
1929 goto out;
1930
1931 ret = is_inode_existent(sctx, dir, dir_gen);
1932 if (ret <= 0)
1933 goto out;
1934
Robbie Ko01914102017-01-05 16:24:58 +08001935 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1936 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1937 NULL, NULL, NULL);
1938 if (ret < 0 && ret != -ENOENT)
1939 goto out;
1940 if (ret) {
1941 ret = 0;
1942 goto out;
1943 }
1944 if (gen != dir_gen)
1945 goto out;
1946 }
1947
Alexander Block31db9f72012-07-25 23:19:24 +02001948 /* check if the ref was overwritten by another ref */
1949 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1950 &ow_inode, &other_type);
1951 if (ret < 0 && ret != -ENOENT)
1952 goto out;
1953 if (ret) {
1954 /* was never and will never be overwritten */
1955 ret = 0;
1956 goto out;
1957 }
1958
1959 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001960 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001961 if (ret < 0)
1962 goto out;
1963
1964 if (ow_inode == ino && gen == ino_gen) {
1965 ret = 0;
1966 goto out;
1967 }
1968
Filipe Manana8b191a62015-04-09 14:09:14 +01001969 /*
1970 * We know that it is or will be overwritten. Check this now.
1971 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001972 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1973 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001974 */
Filipe Mananab786f162015-09-26 15:30:23 +01001975 if ((ow_inode < sctx->send_progress) ||
1976 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1977 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001978 ret = 1;
1979 else
1980 ret = 0;
1981
1982out:
1983 return ret;
1984}
1985
Alexander Block766702e2012-07-28 14:11:31 +02001986/*
1987 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1988 * that got overwritten. This is used by process_recorded_refs to determine
1989 * if it has to use the path as returned by get_cur_path or the orphan name.
1990 */
Alexander Block31db9f72012-07-25 23:19:24 +02001991static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1992{
1993 int ret = 0;
1994 struct fs_path *name = NULL;
1995 u64 dir;
1996 u64 dir_gen;
1997
1998 if (!sctx->parent_root)
1999 goto out;
2000
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002001 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002002 if (!name)
2003 return -ENOMEM;
2004
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002005 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02002006 if (ret < 0)
2007 goto out;
2008
2009 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2010 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02002011
2012out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002013 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002014 return ret;
2015}
2016
Alexander Block766702e2012-07-28 14:11:31 +02002017/*
2018 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2019 * so we need to do some special handling in case we have clashes. This function
2020 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002021 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002022 */
Alexander Block31db9f72012-07-25 23:19:24 +02002023static int name_cache_insert(struct send_ctx *sctx,
2024 struct name_cache_entry *nce)
2025{
2026 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002027 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002028
Alexander Block7e0926f2012-07-28 14:20:58 +02002029 nce_head = radix_tree_lookup(&sctx->name_cache,
2030 (unsigned long)nce->ino);
2031 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002032 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002033 if (!nce_head) {
2034 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002035 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002036 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002037 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002038
Alexander Block7e0926f2012-07-28 14:20:58 +02002039 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002040 if (ret < 0) {
2041 kfree(nce_head);
2042 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002043 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002044 }
Alexander Block31db9f72012-07-25 23:19:24 +02002045 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002046 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002047 list_add_tail(&nce->list, &sctx->name_cache_list);
2048 sctx->name_cache_size++;
2049
2050 return ret;
2051}
2052
2053static void name_cache_delete(struct send_ctx *sctx,
2054 struct name_cache_entry *nce)
2055{
Alexander Block7e0926f2012-07-28 14:20:58 +02002056 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002057
Alexander Block7e0926f2012-07-28 14:20:58 +02002058 nce_head = radix_tree_lookup(&sctx->name_cache,
2059 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002060 if (!nce_head) {
2061 btrfs_err(sctx->send_root->fs_info,
2062 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2063 nce->ino, sctx->name_cache_size);
2064 }
Alexander Block31db9f72012-07-25 23:19:24 +02002065
Alexander Block7e0926f2012-07-28 14:20:58 +02002066 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002067 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002068 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002069
David Sterba57fb8912014-02-03 19:24:40 +01002070 /*
2071 * We may not get to the final release of nce_head if the lookup fails
2072 */
2073 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002074 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2075 kfree(nce_head);
2076 }
Alexander Block31db9f72012-07-25 23:19:24 +02002077}
2078
2079static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2080 u64 ino, u64 gen)
2081{
Alexander Block7e0926f2012-07-28 14:20:58 +02002082 struct list_head *nce_head;
2083 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002084
Alexander Block7e0926f2012-07-28 14:20:58 +02002085 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2086 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002087 return NULL;
2088
Alexander Block7e0926f2012-07-28 14:20:58 +02002089 list_for_each_entry(cur, nce_head, radix_list) {
2090 if (cur->ino == ino && cur->gen == gen)
2091 return cur;
2092 }
Alexander Block31db9f72012-07-25 23:19:24 +02002093 return NULL;
2094}
2095
Alexander Block766702e2012-07-28 14:11:31 +02002096/*
2097 * Removes the entry from the list and adds it back to the end. This marks the
2098 * entry as recently used so that name_cache_clean_unused does not remove it.
2099 */
Alexander Block31db9f72012-07-25 23:19:24 +02002100static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2101{
2102 list_del(&nce->list);
2103 list_add_tail(&nce->list, &sctx->name_cache_list);
2104}
2105
Alexander Block766702e2012-07-28 14:11:31 +02002106/*
2107 * Remove some entries from the beginning of name_cache_list.
2108 */
Alexander Block31db9f72012-07-25 23:19:24 +02002109static void name_cache_clean_unused(struct send_ctx *sctx)
2110{
2111 struct name_cache_entry *nce;
2112
2113 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2114 return;
2115
2116 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2117 nce = list_entry(sctx->name_cache_list.next,
2118 struct name_cache_entry, list);
2119 name_cache_delete(sctx, nce);
2120 kfree(nce);
2121 }
2122}
2123
2124static void name_cache_free(struct send_ctx *sctx)
2125{
2126 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002127
Alexander Blocke938c8a2012-07-28 16:33:49 +02002128 while (!list_empty(&sctx->name_cache_list)) {
2129 nce = list_entry(sctx->name_cache_list.next,
2130 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002131 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002132 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002133 }
2134}
2135
Alexander Block766702e2012-07-28 14:11:31 +02002136/*
2137 * Used by get_cur_path for each ref up to the root.
2138 * Returns 0 if it succeeded.
2139 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2140 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2141 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2142 * Returns <0 in case of error.
2143 */
Alexander Block31db9f72012-07-25 23:19:24 +02002144static int __get_cur_name_and_parent(struct send_ctx *sctx,
2145 u64 ino, u64 gen,
2146 u64 *parent_ino,
2147 u64 *parent_gen,
2148 struct fs_path *dest)
2149{
2150 int ret;
2151 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002152 struct name_cache_entry *nce = NULL;
2153
Alexander Block766702e2012-07-28 14:11:31 +02002154 /*
2155 * First check if we already did a call to this function with the same
2156 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2157 * return the cached result.
2158 */
Alexander Block31db9f72012-07-25 23:19:24 +02002159 nce = name_cache_search(sctx, ino, gen);
2160 if (nce) {
2161 if (ino < sctx->send_progress && nce->need_later_update) {
2162 name_cache_delete(sctx, nce);
2163 kfree(nce);
2164 nce = NULL;
2165 } else {
2166 name_cache_used(sctx, nce);
2167 *parent_ino = nce->parent_ino;
2168 *parent_gen = nce->parent_gen;
2169 ret = fs_path_add(dest, nce->name, nce->name_len);
2170 if (ret < 0)
2171 goto out;
2172 ret = nce->ret;
2173 goto out;
2174 }
2175 }
2176
Alexander Block766702e2012-07-28 14:11:31 +02002177 /*
2178 * If the inode is not existent yet, add the orphan name and return 1.
2179 * This should only happen for the parent dir that we determine in
2180 * __record_new_ref
2181 */
Alexander Block31db9f72012-07-25 23:19:24 +02002182 ret = is_inode_existent(sctx, ino, gen);
2183 if (ret < 0)
2184 goto out;
2185
2186 if (!ret) {
2187 ret = gen_unique_name(sctx, ino, gen, dest);
2188 if (ret < 0)
2189 goto out;
2190 ret = 1;
2191 goto out_cache;
2192 }
2193
Alexander Block766702e2012-07-28 14:11:31 +02002194 /*
2195 * Depending on whether the inode was already processed or not, use
2196 * send_root or parent_root for ref lookup.
2197 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002198 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002199 ret = get_first_ref(sctx->send_root, ino,
2200 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002201 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002202 ret = get_first_ref(sctx->parent_root, ino,
2203 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002204 if (ret < 0)
2205 goto out;
2206
Alexander Block766702e2012-07-28 14:11:31 +02002207 /*
2208 * Check if the ref was overwritten by an inode's ref that was processed
2209 * earlier. If yes, treat as orphan and return 1.
2210 */
Alexander Block31db9f72012-07-25 23:19:24 +02002211 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2212 dest->start, dest->end - dest->start);
2213 if (ret < 0)
2214 goto out;
2215 if (ret) {
2216 fs_path_reset(dest);
2217 ret = gen_unique_name(sctx, ino, gen, dest);
2218 if (ret < 0)
2219 goto out;
2220 ret = 1;
2221 }
2222
2223out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002224 /*
2225 * Store the result of the lookup in the name cache.
2226 */
David Sterbae780b0d2016-01-18 18:42:13 +01002227 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002228 if (!nce) {
2229 ret = -ENOMEM;
2230 goto out;
2231 }
2232
2233 nce->ino = ino;
2234 nce->gen = gen;
2235 nce->parent_ino = *parent_ino;
2236 nce->parent_gen = *parent_gen;
2237 nce->name_len = fs_path_len(dest);
2238 nce->ret = ret;
2239 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002240
2241 if (ino < sctx->send_progress)
2242 nce->need_later_update = 0;
2243 else
2244 nce->need_later_update = 1;
2245
2246 nce_ret = name_cache_insert(sctx, nce);
2247 if (nce_ret < 0)
2248 ret = nce_ret;
2249 name_cache_clean_unused(sctx);
2250
2251out:
Alexander Block31db9f72012-07-25 23:19:24 +02002252 return ret;
2253}
2254
2255/*
2256 * Magic happens here. This function returns the first ref to an inode as it
2257 * would look like while receiving the stream at this point in time.
2258 * We walk the path up to the root. For every inode in between, we check if it
2259 * was already processed/sent. If yes, we continue with the parent as found
2260 * in send_root. If not, we continue with the parent as found in parent_root.
2261 * If we encounter an inode that was deleted at this point in time, we use the
2262 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2263 * that were not created yet and overwritten inodes/refs.
2264 *
2265 * When do we have have orphan inodes:
2266 * 1. When an inode is freshly created and thus no valid refs are available yet
2267 * 2. When a directory lost all it's refs (deleted) but still has dir items
2268 * inside which were not processed yet (pending for move/delete). If anyone
2269 * tried to get the path to the dir items, it would get a path inside that
2270 * orphan directory.
2271 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2272 * of an unprocessed inode. If in that case the first ref would be
2273 * overwritten, the overwritten inode gets "orphanized". Later when we
2274 * process this overwritten inode, it is restored at a new place by moving
2275 * the orphan inode.
2276 *
2277 * sctx->send_progress tells this function at which point in time receiving
2278 * would be.
2279 */
2280static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2281 struct fs_path *dest)
2282{
2283 int ret = 0;
2284 struct fs_path *name = NULL;
2285 u64 parent_inode = 0;
2286 u64 parent_gen = 0;
2287 int stop = 0;
2288
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002289 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002290 if (!name) {
2291 ret = -ENOMEM;
2292 goto out;
2293 }
2294
2295 dest->reversed = 1;
2296 fs_path_reset(dest);
2297
2298 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002299 struct waiting_dir_move *wdm;
2300
Alexander Block31db9f72012-07-25 23:19:24 +02002301 fs_path_reset(name);
2302
Filipe Manana9dc44212014-02-19 14:31:44 +00002303 if (is_waiting_for_rm(sctx, ino)) {
2304 ret = gen_unique_name(sctx, ino, gen, name);
2305 if (ret < 0)
2306 goto out;
2307 ret = fs_path_add_path(dest, name);
2308 break;
2309 }
2310
Filipe Manana8b191a62015-04-09 14:09:14 +01002311 wdm = get_waiting_dir_move(sctx, ino);
2312 if (wdm && wdm->orphanized) {
2313 ret = gen_unique_name(sctx, ino, gen, name);
2314 stop = 1;
2315 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002316 ret = get_first_ref(sctx->parent_root, ino,
2317 &parent_inode, &parent_gen, name);
2318 } else {
2319 ret = __get_cur_name_and_parent(sctx, ino, gen,
2320 &parent_inode,
2321 &parent_gen, name);
2322 if (ret)
2323 stop = 1;
2324 }
2325
Alexander Block31db9f72012-07-25 23:19:24 +02002326 if (ret < 0)
2327 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002328
Alexander Block31db9f72012-07-25 23:19:24 +02002329 ret = fs_path_add_path(dest, name);
2330 if (ret < 0)
2331 goto out;
2332
2333 ino = parent_inode;
2334 gen = parent_gen;
2335 }
2336
2337out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002338 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002339 if (!ret)
2340 fs_path_unreverse(dest);
2341 return ret;
2342}
2343
2344/*
Alexander Block31db9f72012-07-25 23:19:24 +02002345 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2346 */
2347static int send_subvol_begin(struct send_ctx *sctx)
2348{
2349 int ret;
2350 struct btrfs_root *send_root = sctx->send_root;
2351 struct btrfs_root *parent_root = sctx->parent_root;
2352 struct btrfs_path *path;
2353 struct btrfs_key key;
2354 struct btrfs_root_ref *ref;
2355 struct extent_buffer *leaf;
2356 char *name = NULL;
2357 int namelen;
2358
Wang Shilongffcfaf82014-01-15 00:26:43 +08002359 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002360 if (!path)
2361 return -ENOMEM;
2362
David Sterbae780b0d2016-01-18 18:42:13 +01002363 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002364 if (!name) {
2365 btrfs_free_path(path);
2366 return -ENOMEM;
2367 }
2368
2369 key.objectid = send_root->objectid;
2370 key.type = BTRFS_ROOT_BACKREF_KEY;
2371 key.offset = 0;
2372
2373 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2374 &key, path, 1, 0);
2375 if (ret < 0)
2376 goto out;
2377 if (ret) {
2378 ret = -ENOENT;
2379 goto out;
2380 }
2381
2382 leaf = path->nodes[0];
2383 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2384 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2385 key.objectid != send_root->objectid) {
2386 ret = -ENOENT;
2387 goto out;
2388 }
2389 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2390 namelen = btrfs_root_ref_name_len(leaf, ref);
2391 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2392 btrfs_release_path(path);
2393
Alexander Block31db9f72012-07-25 23:19:24 +02002394 if (parent_root) {
2395 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2396 if (ret < 0)
2397 goto out;
2398 } else {
2399 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2400 if (ret < 0)
2401 goto out;
2402 }
2403
2404 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002405
2406 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2407 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2408 sctx->send_root->root_item.received_uuid);
2409 else
2410 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2411 sctx->send_root->root_item.uuid);
2412
Alexander Block31db9f72012-07-25 23:19:24 +02002413 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002414 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002415 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002416 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2417 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2418 parent_root->root_item.received_uuid);
2419 else
2420 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2421 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002422 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002423 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002424 }
2425
2426 ret = send_cmd(sctx);
2427
2428tlv_put_failure:
2429out:
2430 btrfs_free_path(path);
2431 kfree(name);
2432 return ret;
2433}
2434
2435static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2436{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002437 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002438 int ret = 0;
2439 struct fs_path *p;
2440
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002441 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002442
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002443 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002444 if (!p)
2445 return -ENOMEM;
2446
2447 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2448 if (ret < 0)
2449 goto out;
2450
2451 ret = get_cur_path(sctx, ino, gen, p);
2452 if (ret < 0)
2453 goto out;
2454 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2455 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2456
2457 ret = send_cmd(sctx);
2458
2459tlv_put_failure:
2460out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002461 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002462 return ret;
2463}
2464
2465static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2466{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002467 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002468 int ret = 0;
2469 struct fs_path *p;
2470
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002471 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002472
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002473 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002474 if (!p)
2475 return -ENOMEM;
2476
2477 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2478 if (ret < 0)
2479 goto out;
2480
2481 ret = get_cur_path(sctx, ino, gen, p);
2482 if (ret < 0)
2483 goto out;
2484 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2485 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2486
2487 ret = send_cmd(sctx);
2488
2489tlv_put_failure:
2490out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002491 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002492 return ret;
2493}
2494
2495static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2496{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002497 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002498 int ret = 0;
2499 struct fs_path *p;
2500
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002501 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2502 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002503
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002504 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002505 if (!p)
2506 return -ENOMEM;
2507
2508 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2509 if (ret < 0)
2510 goto out;
2511
2512 ret = get_cur_path(sctx, ino, gen, p);
2513 if (ret < 0)
2514 goto out;
2515 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2516 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2517 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2518
2519 ret = send_cmd(sctx);
2520
2521tlv_put_failure:
2522out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002523 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002524 return ret;
2525}
2526
2527static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2528{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002529 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002530 int ret = 0;
2531 struct fs_path *p = NULL;
2532 struct btrfs_inode_item *ii;
2533 struct btrfs_path *path = NULL;
2534 struct extent_buffer *eb;
2535 struct btrfs_key key;
2536 int slot;
2537
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002538 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002539
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002540 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002541 if (!p)
2542 return -ENOMEM;
2543
2544 path = alloc_path_for_send();
2545 if (!path) {
2546 ret = -ENOMEM;
2547 goto out;
2548 }
2549
2550 key.objectid = ino;
2551 key.type = BTRFS_INODE_ITEM_KEY;
2552 key.offset = 0;
2553 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002554 if (ret > 0)
2555 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002556 if (ret < 0)
2557 goto out;
2558
2559 eb = path->nodes[0];
2560 slot = path->slots[0];
2561 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2562
2563 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2564 if (ret < 0)
2565 goto out;
2566
2567 ret = get_cur_path(sctx, ino, gen, p);
2568 if (ret < 0)
2569 goto out;
2570 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002571 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2572 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2573 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002574 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002575
2576 ret = send_cmd(sctx);
2577
2578tlv_put_failure:
2579out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002580 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002581 btrfs_free_path(path);
2582 return ret;
2583}
2584
2585/*
2586 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2587 * a valid path yet because we did not process the refs yet. So, the inode
2588 * is created as orphan.
2589 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002590static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002591{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002592 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002593 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002594 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002595 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002596 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002597 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002598 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002599
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002600 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002601
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002602 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002603 if (!p)
2604 return -ENOMEM;
2605
Liu Bo644d1942014-02-27 17:29:01 +08002606 if (ino != sctx->cur_ino) {
2607 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2608 NULL, NULL, &rdev);
2609 if (ret < 0)
2610 goto out;
2611 } else {
2612 gen = sctx->cur_inode_gen;
2613 mode = sctx->cur_inode_mode;
2614 rdev = sctx->cur_inode_rdev;
2615 }
Alexander Block31db9f72012-07-25 23:19:24 +02002616
Alexander Blocke938c8a2012-07-28 16:33:49 +02002617 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002618 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002619 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002620 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002621 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002622 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002623 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002624 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002625 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002626 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002627 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002628 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002629 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002630 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002631 (int)(mode & S_IFMT));
Tsutomu Itohca6842b2016-01-22 09:13:25 +09002632 ret = -EOPNOTSUPP;
Alexander Block31db9f72012-07-25 23:19:24 +02002633 goto out;
2634 }
2635
2636 ret = begin_cmd(sctx, cmd);
2637 if (ret < 0)
2638 goto out;
2639
Alexander Block1f4692d2012-07-28 10:42:24 +02002640 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002641 if (ret < 0)
2642 goto out;
2643
2644 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002645 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002646
2647 if (S_ISLNK(mode)) {
2648 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002649 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002650 if (ret < 0)
2651 goto out;
2652 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2653 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2654 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002655 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2656 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002657 }
2658
2659 ret = send_cmd(sctx);
2660 if (ret < 0)
2661 goto out;
2662
2663
2664tlv_put_failure:
2665out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002666 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002667 return ret;
2668}
2669
Alexander Block1f4692d2012-07-28 10:42:24 +02002670/*
2671 * We need some special handling for inodes that get processed before the parent
2672 * directory got created. See process_recorded_refs for details.
2673 * This function does the check if we already created the dir out of order.
2674 */
2675static int did_create_dir(struct send_ctx *sctx, u64 dir)
2676{
2677 int ret = 0;
2678 struct btrfs_path *path = NULL;
2679 struct btrfs_key key;
2680 struct btrfs_key found_key;
2681 struct btrfs_key di_key;
2682 struct extent_buffer *eb;
2683 struct btrfs_dir_item *di;
2684 int slot;
2685
2686 path = alloc_path_for_send();
2687 if (!path) {
2688 ret = -ENOMEM;
2689 goto out;
2690 }
2691
2692 key.objectid = dir;
2693 key.type = BTRFS_DIR_INDEX_KEY;
2694 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002695 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2696 if (ret < 0)
2697 goto out;
2698
Alexander Block1f4692d2012-07-28 10:42:24 +02002699 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002700 eb = path->nodes[0];
2701 slot = path->slots[0];
2702 if (slot >= btrfs_header_nritems(eb)) {
2703 ret = btrfs_next_leaf(sctx->send_root, path);
2704 if (ret < 0) {
2705 goto out;
2706 } else if (ret > 0) {
2707 ret = 0;
2708 break;
2709 }
2710 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002711 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002712
2713 btrfs_item_key_to_cpu(eb, &found_key, slot);
2714 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002715 found_key.type != key.type) {
2716 ret = 0;
2717 goto out;
2718 }
2719
2720 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2721 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2722
Josef Bacika0525412013-08-12 10:56:14 -04002723 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2724 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002725 ret = 1;
2726 goto out;
2727 }
2728
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002729 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002730 }
2731
2732out:
2733 btrfs_free_path(path);
2734 return ret;
2735}
2736
2737/*
2738 * Only creates the inode if it is:
2739 * 1. Not a directory
2740 * 2. Or a directory which was not created already due to out of order
2741 * directories. See did_create_dir and process_recorded_refs for details.
2742 */
2743static int send_create_inode_if_needed(struct send_ctx *sctx)
2744{
2745 int ret;
2746
2747 if (S_ISDIR(sctx->cur_inode_mode)) {
2748 ret = did_create_dir(sctx, sctx->cur_ino);
2749 if (ret < 0)
2750 goto out;
2751 if (ret) {
2752 ret = 0;
2753 goto out;
2754 }
2755 }
2756
2757 ret = send_create_inode(sctx, sctx->cur_ino);
2758 if (ret < 0)
2759 goto out;
2760
2761out:
2762 return ret;
2763}
2764
Alexander Block31db9f72012-07-25 23:19:24 +02002765struct recorded_ref {
2766 struct list_head list;
Alexander Block31db9f72012-07-25 23:19:24 +02002767 char *name;
2768 struct fs_path *full_path;
2769 u64 dir;
2770 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002771 int name_len;
2772};
2773
Filipe Mananafdb13882017-06-13 14:13:11 +01002774static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2775{
2776 ref->full_path = path;
2777 ref->name = (char *)kbasename(ref->full_path->start);
2778 ref->name_len = ref->full_path->end - ref->name;
2779}
2780
Alexander Block31db9f72012-07-25 23:19:24 +02002781/*
2782 * We need to process new refs before deleted refs, but compare_tree gives us
2783 * everything mixed. So we first record all refs and later process them.
2784 * This function is a helper to record one ref.
2785 */
Liu Boa4d96d62014-03-03 21:31:03 +08002786static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002787 u64 dir_gen, struct fs_path *path)
2788{
2789 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002790
David Sterbae780b0d2016-01-18 18:42:13 +01002791 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002792 if (!ref)
2793 return -ENOMEM;
2794
2795 ref->dir = dir;
2796 ref->dir_gen = dir_gen;
Filipe Mananafdb13882017-06-13 14:13:11 +01002797 set_ref_path(ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02002798 list_add_tail(&ref->list, head);
2799 return 0;
2800}
2801
Josef Bacikba5e8f22013-08-16 16:52:55 -04002802static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2803{
2804 struct recorded_ref *new;
2805
David Sterbae780b0d2016-01-18 18:42:13 +01002806 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002807 if (!new)
2808 return -ENOMEM;
2809
2810 new->dir = ref->dir;
2811 new->dir_gen = ref->dir_gen;
2812 new->full_path = NULL;
2813 INIT_LIST_HEAD(&new->list);
2814 list_add_tail(&new->list, list);
2815 return 0;
2816}
2817
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002818static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002819{
2820 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002821
Alexander Blocke938c8a2012-07-28 16:33:49 +02002822 while (!list_empty(head)) {
2823 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002824 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002825 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002826 kfree(cur);
2827 }
Alexander Block31db9f72012-07-25 23:19:24 +02002828}
2829
2830static void free_recorded_refs(struct send_ctx *sctx)
2831{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002832 __free_recorded_refs(&sctx->new_refs);
2833 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002834}
2835
2836/*
Alexander Block766702e2012-07-28 14:11:31 +02002837 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002838 * ref of an unprocessed inode gets overwritten and for all non empty
2839 * directories.
2840 */
2841static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2842 struct fs_path *path)
2843{
2844 int ret;
2845 struct fs_path *orphan;
2846
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002847 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002848 if (!orphan)
2849 return -ENOMEM;
2850
2851 ret = gen_unique_name(sctx, ino, gen, orphan);
2852 if (ret < 0)
2853 goto out;
2854
2855 ret = send_rename(sctx, path, orphan);
2856
2857out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002858 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002859 return ret;
2860}
2861
Filipe Manana9dc44212014-02-19 14:31:44 +00002862static struct orphan_dir_info *
2863add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2864{
2865 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2866 struct rb_node *parent = NULL;
2867 struct orphan_dir_info *entry, *odi;
2868
David Sterbae780b0d2016-01-18 18:42:13 +01002869 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
Filipe Manana9dc44212014-02-19 14:31:44 +00002870 if (!odi)
2871 return ERR_PTR(-ENOMEM);
2872 odi->ino = dir_ino;
2873 odi->gen = 0;
2874
2875 while (*p) {
2876 parent = *p;
2877 entry = rb_entry(parent, struct orphan_dir_info, node);
2878 if (dir_ino < entry->ino) {
2879 p = &(*p)->rb_left;
2880 } else if (dir_ino > entry->ino) {
2881 p = &(*p)->rb_right;
2882 } else {
2883 kfree(odi);
2884 return entry;
2885 }
2886 }
2887
2888 rb_link_node(&odi->node, parent, p);
2889 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2890 return odi;
2891}
2892
2893static struct orphan_dir_info *
2894get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2895{
2896 struct rb_node *n = sctx->orphan_dirs.rb_node;
2897 struct orphan_dir_info *entry;
2898
2899 while (n) {
2900 entry = rb_entry(n, struct orphan_dir_info, node);
2901 if (dir_ino < entry->ino)
2902 n = n->rb_left;
2903 else if (dir_ino > entry->ino)
2904 n = n->rb_right;
2905 else
2906 return entry;
2907 }
2908 return NULL;
2909}
2910
2911static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2912{
2913 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2914
2915 return odi != NULL;
2916}
2917
2918static void free_orphan_dir_info(struct send_ctx *sctx,
2919 struct orphan_dir_info *odi)
2920{
2921 if (!odi)
2922 return;
2923 rb_erase(&odi->node, &sctx->orphan_dirs);
2924 kfree(odi);
2925}
2926
Alexander Block31db9f72012-07-25 23:19:24 +02002927/*
2928 * Returns 1 if a directory can be removed at this point in time.
2929 * We check this by iterating all dir items and checking if the inode behind
2930 * the dir item was already processed.
2931 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002932static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2933 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002934{
2935 int ret = 0;
2936 struct btrfs_root *root = sctx->parent_root;
2937 struct btrfs_path *path;
2938 struct btrfs_key key;
2939 struct btrfs_key found_key;
2940 struct btrfs_key loc;
2941 struct btrfs_dir_item *di;
2942
Alexander Block6d85ed052012-08-01 14:48:59 +02002943 /*
2944 * Don't try to rmdir the top/root subvolume dir.
2945 */
2946 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2947 return 0;
2948
Alexander Block31db9f72012-07-25 23:19:24 +02002949 path = alloc_path_for_send();
2950 if (!path)
2951 return -ENOMEM;
2952
2953 key.objectid = dir;
2954 key.type = BTRFS_DIR_INDEX_KEY;
2955 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002956 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2957 if (ret < 0)
2958 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002959
2960 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002961 struct waiting_dir_move *dm;
2962
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002963 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2964 ret = btrfs_next_leaf(root, path);
2965 if (ret < 0)
2966 goto out;
2967 else if (ret > 0)
2968 break;
2969 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002970 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002971 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2972 path->slots[0]);
2973 if (found_key.objectid != key.objectid ||
2974 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002975 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002976
2977 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2978 struct btrfs_dir_item);
2979 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2980
Filipe Manana9dc44212014-02-19 14:31:44 +00002981 dm = get_waiting_dir_move(sctx, loc.objectid);
2982 if (dm) {
2983 struct orphan_dir_info *odi;
2984
2985 odi = add_orphan_dir_info(sctx, dir);
2986 if (IS_ERR(odi)) {
2987 ret = PTR_ERR(odi);
2988 goto out;
2989 }
2990 odi->gen = dir_gen;
2991 dm->rmdir_ino = dir;
2992 ret = 0;
2993 goto out;
2994 }
2995
Alexander Block31db9f72012-07-25 23:19:24 +02002996 if (loc.objectid > send_progress) {
Robbie Ko443f9d22015-06-22 17:08:45 +08002997 struct orphan_dir_info *odi;
2998
2999 odi = get_orphan_dir_info(sctx, dir);
3000 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02003001 ret = 0;
3002 goto out;
3003 }
3004
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003005 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003006 }
3007
3008 ret = 1;
3009
3010out:
3011 btrfs_free_path(path);
3012 return ret;
3013}
3014
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003015static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3016{
Filipe Manana9dc44212014-02-19 14:31:44 +00003017 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003018
Filipe Manana9dc44212014-02-19 14:31:44 +00003019 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003020}
3021
Filipe Manana8b191a62015-04-09 14:09:14 +01003022static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003023{
3024 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3025 struct rb_node *parent = NULL;
3026 struct waiting_dir_move *entry, *dm;
3027
David Sterbae780b0d2016-01-18 18:42:13 +01003028 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003029 if (!dm)
3030 return -ENOMEM;
3031 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003032 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003033 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003034
3035 while (*p) {
3036 parent = *p;
3037 entry = rb_entry(parent, struct waiting_dir_move, node);
3038 if (ino < entry->ino) {
3039 p = &(*p)->rb_left;
3040 } else if (ino > entry->ino) {
3041 p = &(*p)->rb_right;
3042 } else {
3043 kfree(dm);
3044 return -EEXIST;
3045 }
3046 }
3047
3048 rb_link_node(&dm->node, parent, p);
3049 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3050 return 0;
3051}
3052
Filipe Manana9dc44212014-02-19 14:31:44 +00003053static struct waiting_dir_move *
3054get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003055{
3056 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3057 struct waiting_dir_move *entry;
3058
3059 while (n) {
3060 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003061 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003062 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003063 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003064 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003065 else
3066 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003067 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003068 return NULL;
3069}
3070
3071static void free_waiting_dir_move(struct send_ctx *sctx,
3072 struct waiting_dir_move *dm)
3073{
3074 if (!dm)
3075 return;
3076 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3077 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003078}
3079
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003080static int add_pending_dir_move(struct send_ctx *sctx,
3081 u64 ino,
3082 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003083 u64 parent_ino,
3084 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003085 struct list_head *deleted_refs,
3086 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003087{
3088 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3089 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003090 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003091 struct recorded_ref *cur;
3092 int exists = 0;
3093 int ret;
3094
David Sterbae780b0d2016-01-18 18:42:13 +01003095 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003096 if (!pm)
3097 return -ENOMEM;
3098 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003099 pm->ino = ino;
3100 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003101 INIT_LIST_HEAD(&pm->list);
3102 INIT_LIST_HEAD(&pm->update_refs);
3103 RB_CLEAR_NODE(&pm->node);
3104
3105 while (*p) {
3106 parent = *p;
3107 entry = rb_entry(parent, struct pending_dir_move, node);
3108 if (parent_ino < entry->parent_ino) {
3109 p = &(*p)->rb_left;
3110 } else if (parent_ino > entry->parent_ino) {
3111 p = &(*p)->rb_right;
3112 } else {
3113 exists = 1;
3114 break;
3115 }
3116 }
3117
Filipe Mananaf9594922014-03-27 20:14:01 +00003118 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003119 ret = dup_ref(cur, &pm->update_refs);
3120 if (ret < 0)
3121 goto out;
3122 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003123 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003124 ret = dup_ref(cur, &pm->update_refs);
3125 if (ret < 0)
3126 goto out;
3127 }
3128
Filipe Manana8b191a62015-04-09 14:09:14 +01003129 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003130 if (ret)
3131 goto out;
3132
3133 if (exists) {
3134 list_add_tail(&pm->list, &entry->list);
3135 } else {
3136 rb_link_node(&pm->node, parent, p);
3137 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3138 }
3139 ret = 0;
3140out:
3141 if (ret) {
3142 __free_recorded_refs(&pm->update_refs);
3143 kfree(pm);
3144 }
3145 return ret;
3146}
3147
3148static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3149 u64 parent_ino)
3150{
3151 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3152 struct pending_dir_move *entry;
3153
3154 while (n) {
3155 entry = rb_entry(n, struct pending_dir_move, node);
3156 if (parent_ino < entry->parent_ino)
3157 n = n->rb_left;
3158 else if (parent_ino > entry->parent_ino)
3159 n = n->rb_right;
3160 else
3161 return entry;
3162 }
3163 return NULL;
3164}
3165
Robbie Ko801bec32015-06-23 18:39:46 +08003166static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3167 u64 ino, u64 gen, u64 *ancestor_ino)
3168{
3169 int ret = 0;
3170 u64 parent_inode = 0;
3171 u64 parent_gen = 0;
3172 u64 start_ino = ino;
3173
3174 *ancestor_ino = 0;
3175 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3176 fs_path_reset(name);
3177
3178 if (is_waiting_for_rm(sctx, ino))
3179 break;
3180 if (is_waiting_for_move(sctx, ino)) {
3181 if (*ancestor_ino == 0)
3182 *ancestor_ino = ino;
3183 ret = get_first_ref(sctx->parent_root, ino,
3184 &parent_inode, &parent_gen, name);
3185 } else {
3186 ret = __get_cur_name_and_parent(sctx, ino, gen,
3187 &parent_inode,
3188 &parent_gen, name);
3189 if (ret > 0) {
3190 ret = 0;
3191 break;
3192 }
3193 }
3194 if (ret < 0)
3195 break;
3196 if (parent_inode == start_ino) {
3197 ret = 1;
3198 if (*ancestor_ino == 0)
3199 *ancestor_ino = ino;
3200 break;
3201 }
3202 ino = parent_inode;
3203 gen = parent_gen;
3204 }
3205 return ret;
3206}
3207
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003208static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3209{
3210 struct fs_path *from_path = NULL;
3211 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003212 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003213 u64 orig_progress = sctx->send_progress;
3214 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003215 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003216 struct waiting_dir_move *dm = NULL;
3217 u64 rmdir_ino = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003218 u64 ancestor;
3219 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003220 int ret;
3221
Filipe Manana2b863a12014-02-16 13:43:11 +00003222 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003223 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003224 if (!name || !from_path) {
3225 ret = -ENOMEM;
3226 goto out;
3227 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003228
Filipe Manana9dc44212014-02-19 14:31:44 +00003229 dm = get_waiting_dir_move(sctx, pm->ino);
3230 ASSERT(dm);
3231 rmdir_ino = dm->rmdir_ino;
Robbie Ko801bec32015-06-23 18:39:46 +08003232 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003233 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003234
Robbie Ko801bec32015-06-23 18:39:46 +08003235 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003236 ret = gen_unique_name(sctx, pm->ino,
3237 pm->gen, from_path);
3238 } else {
3239 ret = get_first_ref(sctx->parent_root, pm->ino,
3240 &parent_ino, &parent_gen, name);
3241 if (ret < 0)
3242 goto out;
3243 ret = get_cur_path(sctx, parent_ino, parent_gen,
3244 from_path);
3245 if (ret < 0)
3246 goto out;
3247 ret = fs_path_add_path(from_path, name);
3248 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003249 if (ret < 0)
3250 goto out;
3251
Filipe Mananaf9594922014-03-27 20:14:01 +00003252 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003253 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003254 if (ret < 0)
3255 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003256 if (ret) {
3257 LIST_HEAD(deleted_refs);
3258 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3259 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3260 &pm->update_refs, &deleted_refs,
3261 is_orphan);
3262 if (ret < 0)
3263 goto out;
3264 if (rmdir_ino) {
3265 dm = get_waiting_dir_move(sctx, pm->ino);
3266 ASSERT(dm);
3267 dm->rmdir_ino = rmdir_ino;
3268 }
3269 goto out;
3270 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003271 fs_path_reset(name);
3272 to_path = name;
3273 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003274 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3275 if (ret < 0)
3276 goto out;
3277
3278 ret = send_rename(sctx, from_path, to_path);
3279 if (ret < 0)
3280 goto out;
3281
Filipe Manana9dc44212014-02-19 14:31:44 +00003282 if (rmdir_ino) {
3283 struct orphan_dir_info *odi;
3284
3285 odi = get_orphan_dir_info(sctx, rmdir_ino);
3286 if (!odi) {
3287 /* already deleted */
3288 goto finish;
3289 }
Robbie Ko99ea42d2015-06-23 18:39:49 +08003290 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003291 if (ret < 0)
3292 goto out;
3293 if (!ret)
3294 goto finish;
3295
3296 name = fs_path_alloc();
3297 if (!name) {
3298 ret = -ENOMEM;
3299 goto out;
3300 }
3301 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3302 if (ret < 0)
3303 goto out;
3304 ret = send_rmdir(sctx, name);
3305 if (ret < 0)
3306 goto out;
3307 free_orphan_dir_info(sctx, odi);
3308 }
3309
3310finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003311 ret = send_utimes(sctx, pm->ino, pm->gen);
3312 if (ret < 0)
3313 goto out;
3314
3315 /*
3316 * After rename/move, need to update the utimes of both new parent(s)
3317 * and old parent(s).
3318 */
3319 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003320 /*
3321 * The parent inode might have been deleted in the send snapshot
3322 */
3323 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3324 NULL, NULL, NULL, NULL, NULL);
3325 if (ret == -ENOENT) {
3326 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003327 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003328 }
3329 if (ret < 0)
3330 goto out;
3331
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003332 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3333 if (ret < 0)
3334 goto out;
3335 }
3336
3337out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003338 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003339 fs_path_free(from_path);
3340 fs_path_free(to_path);
3341 sctx->send_progress = orig_progress;
3342
3343 return ret;
3344}
3345
3346static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3347{
3348 if (!list_empty(&m->list))
3349 list_del(&m->list);
3350 if (!RB_EMPTY_NODE(&m->node))
3351 rb_erase(&m->node, &sctx->pending_dir_moves);
3352 __free_recorded_refs(&m->update_refs);
3353 kfree(m);
3354}
3355
3356static void tail_append_pending_moves(struct pending_dir_move *moves,
3357 struct list_head *stack)
3358{
3359 if (list_empty(&moves->list)) {
3360 list_add_tail(&moves->list, stack);
3361 } else {
3362 LIST_HEAD(list);
3363 list_splice_init(&moves->list, &list);
3364 list_add_tail(&moves->list, stack);
3365 list_splice_tail(&list, stack);
3366 }
3367}
3368
3369static int apply_children_dir_moves(struct send_ctx *sctx)
3370{
3371 struct pending_dir_move *pm;
3372 struct list_head stack;
3373 u64 parent_ino = sctx->cur_ino;
3374 int ret = 0;
3375
3376 pm = get_pending_dir_moves(sctx, parent_ino);
3377 if (!pm)
3378 return 0;
3379
3380 INIT_LIST_HEAD(&stack);
3381 tail_append_pending_moves(pm, &stack);
3382
3383 while (!list_empty(&stack)) {
3384 pm = list_first_entry(&stack, struct pending_dir_move, list);
3385 parent_ino = pm->ino;
3386 ret = apply_dir_move(sctx, pm);
3387 free_pending_move(sctx, pm);
3388 if (ret)
3389 goto out;
3390 pm = get_pending_dir_moves(sctx, parent_ino);
3391 if (pm)
3392 tail_append_pending_moves(pm, &stack);
3393 }
3394 return 0;
3395
3396out:
3397 while (!list_empty(&stack)) {
3398 pm = list_first_entry(&stack, struct pending_dir_move, list);
3399 free_pending_move(sctx, pm);
3400 }
3401 return ret;
3402}
3403
Filipe Manana84471e22015-02-28 22:29:22 +00003404/*
3405 * We might need to delay a directory rename even when no ancestor directory
3406 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3407 * renamed. This happens when we rename a directory to the old name (the name
3408 * in the parent root) of some other unrelated directory that got its rename
3409 * delayed due to some ancestor with higher number that got renamed.
3410 *
3411 * Example:
3412 *
3413 * Parent snapshot:
3414 * . (ino 256)
3415 * |---- a/ (ino 257)
3416 * | |---- file (ino 260)
3417 * |
3418 * |---- b/ (ino 258)
3419 * |---- c/ (ino 259)
3420 *
3421 * Send snapshot:
3422 * . (ino 256)
3423 * |---- a/ (ino 258)
3424 * |---- x/ (ino 259)
3425 * |---- y/ (ino 257)
3426 * |----- file (ino 260)
3427 *
3428 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3429 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3430 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3431 * must issue is:
3432 *
3433 * 1 - rename 259 from 'c' to 'x'
3434 * 2 - rename 257 from 'a' to 'x/y'
3435 * 3 - rename 258 from 'b' to 'a'
3436 *
3437 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3438 * be done right away and < 0 on error.
3439 */
3440static int wait_for_dest_dir_move(struct send_ctx *sctx,
3441 struct recorded_ref *parent_ref,
3442 const bool is_orphan)
3443{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003444 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003445 struct btrfs_path *path;
3446 struct btrfs_key key;
3447 struct btrfs_key di_key;
3448 struct btrfs_dir_item *di;
3449 u64 left_gen;
3450 u64 right_gen;
3451 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003452 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003453
3454 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3455 return 0;
3456
3457 path = alloc_path_for_send();
3458 if (!path)
3459 return -ENOMEM;
3460
3461 key.objectid = parent_ref->dir;
3462 key.type = BTRFS_DIR_ITEM_KEY;
3463 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3464
3465 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3466 if (ret < 0) {
3467 goto out;
3468 } else if (ret > 0) {
3469 ret = 0;
3470 goto out;
3471 }
3472
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003473 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3474 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003475 if (!di) {
3476 ret = 0;
3477 goto out;
3478 }
3479 /*
3480 * di_key.objectid has the number of the inode that has a dentry in the
3481 * parent directory with the same name that sctx->cur_ino is being
3482 * renamed to. We need to check if that inode is in the send root as
3483 * well and if it is currently marked as an inode with a pending rename,
3484 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3485 * that it happens after that other inode is renamed.
3486 */
3487 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3488 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3489 ret = 0;
3490 goto out;
3491 }
3492
3493 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3494 &left_gen, NULL, NULL, NULL, NULL);
3495 if (ret < 0)
3496 goto out;
3497 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3498 &right_gen, NULL, NULL, NULL, NULL);
3499 if (ret < 0) {
3500 if (ret == -ENOENT)
3501 ret = 0;
3502 goto out;
3503 }
3504
3505 /* Different inode, no need to delay the rename of sctx->cur_ino */
3506 if (right_gen != left_gen) {
3507 ret = 0;
3508 goto out;
3509 }
3510
Robbie Ko801bec32015-06-23 18:39:46 +08003511 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3512 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003513 ret = add_pending_dir_move(sctx,
3514 sctx->cur_ino,
3515 sctx->cur_inode_gen,
3516 di_key.objectid,
3517 &sctx->new_refs,
3518 &sctx->deleted_refs,
3519 is_orphan);
3520 if (!ret)
3521 ret = 1;
3522 }
3523out:
3524 btrfs_free_path(path);
3525 return ret;
3526}
3527
Filipe Manana80aa6022015-03-27 17:50:45 +00003528/*
3529 * Check if ino ino1 is an ancestor of inode ino2 in the given root.
3530 * Return 1 if true, 0 if false and < 0 on error.
3531 */
3532static int is_ancestor(struct btrfs_root *root,
3533 const u64 ino1,
3534 const u64 ino1_gen,
3535 const u64 ino2,
3536 struct fs_path *fs_path)
3537{
3538 u64 ino = ino2;
Filipe Manana72c36682017-06-07 11:41:29 +01003539 bool free_path = false;
3540 int ret = 0;
3541
3542 if (!fs_path) {
3543 fs_path = fs_path_alloc();
3544 if (!fs_path)
3545 return -ENOMEM;
3546 free_path = true;
3547 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003548
3549 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003550 u64 parent;
3551 u64 parent_gen;
3552
3553 fs_path_reset(fs_path);
3554 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3555 if (ret < 0) {
3556 if (ret == -ENOENT && ino == ino2)
3557 ret = 0;
Filipe Manana72c36682017-06-07 11:41:29 +01003558 goto out;
Filipe Manana80aa6022015-03-27 17:50:45 +00003559 }
Filipe Manana72c36682017-06-07 11:41:29 +01003560 if (parent == ino1) {
3561 ret = parent_gen == ino1_gen ? 1 : 0;
3562 goto out;
3563 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003564 ino = parent;
3565 }
Filipe Manana72c36682017-06-07 11:41:29 +01003566 out:
3567 if (free_path)
3568 fs_path_free(fs_path);
3569 return ret;
Filipe Manana80aa6022015-03-27 17:50:45 +00003570}
3571
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003572static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003573 struct recorded_ref *parent_ref,
3574 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003575{
Filipe Mananaf9594922014-03-27 20:14:01 +00003576 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003577 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003578 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003579 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003580 struct fs_path *path_before = NULL;
3581 struct fs_path *path_after = NULL;
3582 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003583
3584 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003585 path_before = fs_path_alloc();
3586 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003587 ret = -ENOMEM;
3588 goto out;
3589 }
3590
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003591 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003592 * Our current directory inode may not yet be renamed/moved because some
3593 * ancestor (immediate or not) has to be renamed/moved first. So find if
3594 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003595 * that ancestor is processed to avoid path build infinite loops (done
3596 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003597 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003598 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003599 u64 parent_ino_after_gen;
3600
Filipe Mananaf9594922014-03-27 20:14:01 +00003601 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003602 /*
3603 * If the current inode is an ancestor of ino in the
3604 * parent root, we need to delay the rename of the
3605 * current inode, otherwise don't delayed the rename
3606 * because we can end up with a circular dependency
3607 * of renames, resulting in some directories never
3608 * getting the respective rename operations issued in
3609 * the send stream or getting into infinite path build
3610 * loops.
3611 */
3612 ret = is_ancestor(sctx->parent_root,
3613 sctx->cur_ino, sctx->cur_inode_gen,
3614 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003615 if (ret)
3616 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003617 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003618
3619 fs_path_reset(path_before);
3620 fs_path_reset(path_after);
3621
3622 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003623 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003624 if (ret < 0)
3625 goto out;
3626 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3627 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003628 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003629 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003630 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003631 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003632 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003633 }
3634
3635 len1 = fs_path_len(path_before);
3636 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003637 if (ino > sctx->cur_ino &&
3638 (parent_ino_before != parent_ino_after || len1 != len2 ||
3639 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003640 u64 parent_ino_gen;
3641
3642 ret = get_inode_info(sctx->parent_root, ino, NULL,
3643 &parent_ino_gen, NULL, NULL, NULL,
3644 NULL);
3645 if (ret < 0)
3646 goto out;
3647 if (ino_gen == parent_ino_gen) {
3648 ret = 1;
3649 break;
3650 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003651 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003652 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003653 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003654 }
3655
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003656out:
3657 fs_path_free(path_before);
3658 fs_path_free(path_after);
3659
Filipe Mananaf9594922014-03-27 20:14:01 +00003660 if (ret == 1) {
3661 ret = add_pending_dir_move(sctx,
3662 sctx->cur_ino,
3663 sctx->cur_inode_gen,
3664 ino,
3665 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003666 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003667 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003668 if (!ret)
3669 ret = 1;
3670 }
3671
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003672 return ret;
3673}
3674
Filipe Mananaf5962782017-06-22 20:03:51 +01003675static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3676{
3677 int ret;
3678 struct fs_path *new_path;
3679
3680 /*
3681 * Our reference's name member points to its full_path member string, so
3682 * we use here a new path.
3683 */
3684 new_path = fs_path_alloc();
3685 if (!new_path)
3686 return -ENOMEM;
3687
3688 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3689 if (ret < 0) {
3690 fs_path_free(new_path);
3691 return ret;
3692 }
3693 ret = fs_path_add(new_path, ref->name, ref->name_len);
3694 if (ret < 0) {
3695 fs_path_free(new_path);
3696 return ret;
3697 }
3698
3699 fs_path_free(ref->full_path);
3700 set_ref_path(ref, new_path);
3701
3702 return 0;
3703}
3704
Alexander Block31db9f72012-07-25 23:19:24 +02003705/*
3706 * This does all the move/link/unlink/rmdir magic.
3707 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003708static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003709{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003710 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003711 int ret = 0;
3712 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003713 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003714 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003715 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003716 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003717 u64 ow_gen;
Filipe Mananaf5962782017-06-22 20:03:51 +01003718 u64 ow_mode;
Alexander Block31db9f72012-07-25 23:19:24 +02003719 int did_overwrite = 0;
3720 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003721 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003722 bool can_rename = true;
Filipe Mananaf5962782017-06-22 20:03:51 +01003723 bool orphanized_dir = false;
Filipe Mananafdb13882017-06-13 14:13:11 +01003724 bool orphanized_ancestor = false;
Alexander Block31db9f72012-07-25 23:19:24 +02003725
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003726 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003727
Alexander Block6d85ed052012-08-01 14:48:59 +02003728 /*
3729 * This should never happen as the root dir always has the same ref
3730 * which is always '..'
3731 */
3732 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003733 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed052012-08-01 14:48:59 +02003734
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003735 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003736 if (!valid_path) {
3737 ret = -ENOMEM;
3738 goto out;
3739 }
3740
Alexander Block31db9f72012-07-25 23:19:24 +02003741 /*
3742 * First, check if the first ref of the current inode was overwritten
3743 * before. If yes, we know that the current inode was already orphanized
3744 * and thus use the orphan name. If not, we can use get_cur_path to
3745 * get the path of the first ref as it would like while receiving at
3746 * this point in time.
3747 * New inodes are always orphan at the beginning, so force to use the
3748 * orphan name in this case.
3749 * The first ref is stored in valid_path and will be updated if it
3750 * gets moved around.
3751 */
3752 if (!sctx->cur_inode_new) {
3753 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3754 sctx->cur_inode_gen);
3755 if (ret < 0)
3756 goto out;
3757 if (ret)
3758 did_overwrite = 1;
3759 }
3760 if (sctx->cur_inode_new || did_overwrite) {
3761 ret = gen_unique_name(sctx, sctx->cur_ino,
3762 sctx->cur_inode_gen, valid_path);
3763 if (ret < 0)
3764 goto out;
3765 is_orphan = 1;
3766 } else {
3767 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3768 valid_path);
3769 if (ret < 0)
3770 goto out;
3771 }
3772
3773 list_for_each_entry(cur, &sctx->new_refs, list) {
3774 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003775 * We may have refs where the parent directory does not exist
3776 * yet. This happens if the parent directories inum is higher
3777 * the the current inum. To handle this case, we create the
3778 * parent directory out of order. But we need to check if this
3779 * did already happen before due to other refs in the same dir.
3780 */
3781 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3782 if (ret < 0)
3783 goto out;
3784 if (ret == inode_state_will_create) {
3785 ret = 0;
3786 /*
3787 * First check if any of the current inodes refs did
3788 * already create the dir.
3789 */
3790 list_for_each_entry(cur2, &sctx->new_refs, list) {
3791 if (cur == cur2)
3792 break;
3793 if (cur2->dir == cur->dir) {
3794 ret = 1;
3795 break;
3796 }
3797 }
3798
3799 /*
3800 * If that did not happen, check if a previous inode
3801 * did already create the dir.
3802 */
3803 if (!ret)
3804 ret = did_create_dir(sctx, cur->dir);
3805 if (ret < 0)
3806 goto out;
3807 if (!ret) {
3808 ret = send_create_inode(sctx, cur->dir);
3809 if (ret < 0)
3810 goto out;
3811 }
3812 }
3813
3814 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003815 * Check if this new ref would overwrite the first ref of
3816 * another unprocessed inode. If yes, orphanize the
3817 * overwritten inode. If we find an overwritten ref that is
3818 * not the first ref, simply unlink it.
3819 */
3820 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3821 cur->name, cur->name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01003822 &ow_inode, &ow_gen, &ow_mode);
Alexander Block31db9f72012-07-25 23:19:24 +02003823 if (ret < 0)
3824 goto out;
3825 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003826 ret = is_first_ref(sctx->parent_root,
3827 ow_inode, cur->dir, cur->name,
3828 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003829 if (ret < 0)
3830 goto out;
3831 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003832 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003833 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003834
Alexander Block31db9f72012-07-25 23:19:24 +02003835 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3836 cur->full_path);
3837 if (ret < 0)
3838 goto out;
Filipe Mananaf5962782017-06-22 20:03:51 +01003839 if (S_ISDIR(ow_mode))
3840 orphanized_dir = true;
Robbie Ko801bec32015-06-23 18:39:46 +08003841
3842 /*
3843 * If ow_inode has its rename operation delayed
3844 * make sure that its orphanized name is used in
3845 * the source path when performing its rename
3846 * operation.
3847 */
3848 if (is_waiting_for_move(sctx, ow_inode)) {
3849 wdm = get_waiting_dir_move(sctx,
3850 ow_inode);
3851 ASSERT(wdm);
3852 wdm->orphanized = true;
3853 }
3854
Filipe Manana8996a482015-03-12 15:16:20 +00003855 /*
3856 * Make sure we clear our orphanized inode's
3857 * name from the name cache. This is because the
3858 * inode ow_inode might be an ancestor of some
3859 * other inode that will be orphanized as well
3860 * later and has an inode number greater than
3861 * sctx->send_progress. We need to prevent
3862 * future name lookups from using the old name
3863 * and get instead the orphan name.
3864 */
3865 nce = name_cache_search(sctx, ow_inode, ow_gen);
3866 if (nce) {
3867 name_cache_delete(sctx, nce);
3868 kfree(nce);
3869 }
Robbie Ko801bec32015-06-23 18:39:46 +08003870
3871 /*
3872 * ow_inode might currently be an ancestor of
3873 * cur_ino, therefore compute valid_path (the
3874 * current path of cur_ino) again because it
3875 * might contain the pre-orphanization name of
3876 * ow_inode, which is no longer valid.
3877 */
Filipe Manana72c36682017-06-07 11:41:29 +01003878 ret = is_ancestor(sctx->parent_root,
3879 ow_inode, ow_gen,
3880 sctx->cur_ino, NULL);
3881 if (ret > 0) {
Filipe Mananafdb13882017-06-13 14:13:11 +01003882 orphanized_ancestor = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003883 fs_path_reset(valid_path);
3884 ret = get_cur_path(sctx, sctx->cur_ino,
3885 sctx->cur_inode_gen,
3886 valid_path);
3887 }
Robbie Ko801bec32015-06-23 18:39:46 +08003888 if (ret < 0)
3889 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003890 } else {
3891 ret = send_unlink(sctx, cur->full_path);
3892 if (ret < 0)
3893 goto out;
3894 }
3895 }
3896
Filipe Manana84471e22015-02-28 22:29:22 +00003897 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3898 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3899 if (ret < 0)
3900 goto out;
3901 if (ret == 1) {
3902 can_rename = false;
3903 *pending_move = 1;
3904 }
3905 }
3906
Filipe Manana8b191a62015-04-09 14:09:14 +01003907 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3908 can_rename) {
3909 ret = wait_for_parent_move(sctx, cur, is_orphan);
3910 if (ret < 0)
3911 goto out;
3912 if (ret == 1) {
3913 can_rename = false;
3914 *pending_move = 1;
3915 }
3916 }
3917
Alexander Block31db9f72012-07-25 23:19:24 +02003918 /*
3919 * link/move the ref to the new place. If we have an orphan
3920 * inode, move it and update valid_path. If not, link or move
3921 * it depending on the inode mode.
3922 */
Filipe Manana84471e22015-02-28 22:29:22 +00003923 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003924 ret = send_rename(sctx, valid_path, cur->full_path);
3925 if (ret < 0)
3926 goto out;
3927 is_orphan = 0;
3928 ret = fs_path_copy(valid_path, cur->full_path);
3929 if (ret < 0)
3930 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00003931 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003932 if (S_ISDIR(sctx->cur_inode_mode)) {
3933 /*
3934 * Dirs can't be linked, so move it. For moved
3935 * dirs, we always have one new and one deleted
3936 * ref. The deleted ref is ignored later.
3937 */
Filipe Manana8b191a62015-04-09 14:09:14 +01003938 ret = send_rename(sctx, valid_path,
3939 cur->full_path);
3940 if (!ret)
3941 ret = fs_path_copy(valid_path,
3942 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003943 if (ret < 0)
3944 goto out;
3945 } else {
Filipe Mananaf5962782017-06-22 20:03:51 +01003946 /*
3947 * We might have previously orphanized an inode
3948 * which is an ancestor of our current inode,
3949 * so our reference's full path, which was
3950 * computed before any such orphanizations, must
3951 * be updated.
3952 */
3953 if (orphanized_dir) {
3954 ret = update_ref_path(sctx, cur);
3955 if (ret < 0)
3956 goto out;
3957 }
Alexander Block31db9f72012-07-25 23:19:24 +02003958 ret = send_link(sctx, cur->full_path,
3959 valid_path);
3960 if (ret < 0)
3961 goto out;
3962 }
3963 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003964 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003965 if (ret < 0)
3966 goto out;
3967 }
3968
3969 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3970 /*
3971 * Check if we can already rmdir the directory. If not,
3972 * orphanize it. For every dir item inside that gets deleted
3973 * later, we do this check again and rmdir it then if possible.
3974 * See the use of check_dirs for more details.
3975 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003976 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3977 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003978 if (ret < 0)
3979 goto out;
3980 if (ret) {
3981 ret = send_rmdir(sctx, valid_path);
3982 if (ret < 0)
3983 goto out;
3984 } else if (!is_orphan) {
3985 ret = orphanize_inode(sctx, sctx->cur_ino,
3986 sctx->cur_inode_gen, valid_path);
3987 if (ret < 0)
3988 goto out;
3989 is_orphan = 1;
3990 }
3991
3992 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003993 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003994 if (ret < 0)
3995 goto out;
3996 }
Alexander Blockccf16262012-07-28 11:46:29 +02003997 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3998 !list_empty(&sctx->deleted_refs)) {
3999 /*
4000 * We have a moved dir. Add the old parent to check_dirs
4001 */
4002 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4003 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04004004 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02004005 if (ret < 0)
4006 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004007 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4008 /*
4009 * We have a non dir inode. Go through all deleted refs and
4010 * unlink them if they were not already overwritten by other
4011 * inodes.
4012 */
4013 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4014 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4015 sctx->cur_ino, sctx->cur_inode_gen,
4016 cur->name, cur->name_len);
4017 if (ret < 0)
4018 goto out;
4019 if (!ret) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004020 /*
4021 * If we orphanized any ancestor before, we need
4022 * to recompute the full path for deleted names,
4023 * since any such path was computed before we
4024 * processed any references and orphanized any
4025 * ancestor inode.
4026 */
4027 if (orphanized_ancestor) {
Filipe Mananaf5962782017-06-22 20:03:51 +01004028 ret = update_ref_path(sctx, cur);
4029 if (ret < 0)
Filipe Mananafdb13882017-06-13 14:13:11 +01004030 goto out;
Filipe Mananafdb13882017-06-13 14:13:11 +01004031 }
Alexander Block1f4692d2012-07-28 10:42:24 +02004032 ret = send_unlink(sctx, cur->full_path);
4033 if (ret < 0)
4034 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004035 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004036 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004037 if (ret < 0)
4038 goto out;
4039 }
Alexander Block31db9f72012-07-25 23:19:24 +02004040 /*
4041 * If the inode is still orphan, unlink the orphan. This may
4042 * happen when a previous inode did overwrite the first ref
4043 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02004044 * inode. Unlinking does not mean that the inode is deleted in
4045 * all cases. There may still be links to this inode in other
4046 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02004047 */
Alexander Block1f4692d2012-07-28 10:42:24 +02004048 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02004049 ret = send_unlink(sctx, valid_path);
4050 if (ret < 0)
4051 goto out;
4052 }
4053 }
4054
4055 /*
4056 * We did collect all parent dirs where cur_inode was once located. We
4057 * now go through all these dirs and check if they are pending for
4058 * deletion and if it's finally possible to perform the rmdir now.
4059 * We also update the inode stats of the parent dirs here.
4060 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004061 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02004062 /*
4063 * In case we had refs into dirs that were not processed yet,
4064 * we don't need to do the utime and rmdir logic for these dirs.
4065 * The dir will be processed later.
4066 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004067 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02004068 continue;
4069
Josef Bacikba5e8f22013-08-16 16:52:55 -04004070 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004071 if (ret < 0)
4072 goto out;
4073
4074 if (ret == inode_state_did_create ||
4075 ret == inode_state_no_change) {
4076 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004077 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004078 if (ret < 0)
4079 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004080 } else if (ret == inode_state_did_delete &&
4081 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004082 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4083 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004084 if (ret < 0)
4085 goto out;
4086 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004087 ret = get_cur_path(sctx, cur->dir,
4088 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004089 if (ret < 0)
4090 goto out;
4091 ret = send_rmdir(sctx, valid_path);
4092 if (ret < 0)
4093 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004094 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004095 }
4096 }
4097 }
4098
Alexander Block31db9f72012-07-25 23:19:24 +02004099 ret = 0;
4100
4101out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004102 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004103 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004104 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004105 return ret;
4106}
4107
Nikolay Borisova0357512017-08-21 12:43:44 +03004108static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4109 void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004110{
4111 int ret = 0;
4112 struct send_ctx *sctx = ctx;
4113 struct fs_path *p;
4114 u64 gen;
4115
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004116 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004117 if (!p)
4118 return -ENOMEM;
4119
Liu Boa4d96d62014-03-03 21:31:03 +08004120 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004121 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004122 if (ret < 0)
4123 goto out;
4124
Alexander Block31db9f72012-07-25 23:19:24 +02004125 ret = get_cur_path(sctx, dir, gen, p);
4126 if (ret < 0)
4127 goto out;
4128 ret = fs_path_add_path(p, name);
4129 if (ret < 0)
4130 goto out;
4131
Liu Boa4d96d62014-03-03 21:31:03 +08004132 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004133
4134out:
4135 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004136 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004137 return ret;
4138}
4139
Liu Boa4d96d62014-03-03 21:31:03 +08004140static int __record_new_ref(int num, u64 dir, int index,
4141 struct fs_path *name,
4142 void *ctx)
4143{
4144 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004145 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
Liu Boa4d96d62014-03-03 21:31:03 +08004146}
4147
4148
Alexander Block31db9f72012-07-25 23:19:24 +02004149static int __record_deleted_ref(int num, u64 dir, int index,
4150 struct fs_path *name,
4151 void *ctx)
4152{
Alexander Block31db9f72012-07-25 23:19:24 +02004153 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004154 return record_ref(sctx->parent_root, dir, name, ctx,
4155 &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004156}
4157
4158static int record_new_ref(struct send_ctx *sctx)
4159{
4160 int ret;
4161
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004162 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4163 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004164 if (ret < 0)
4165 goto out;
4166 ret = 0;
4167
4168out:
4169 return ret;
4170}
4171
4172static int record_deleted_ref(struct send_ctx *sctx)
4173{
4174 int ret;
4175
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004176 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4177 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004178 if (ret < 0)
4179 goto out;
4180 ret = 0;
4181
4182out:
4183 return ret;
4184}
4185
4186struct find_ref_ctx {
4187 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004188 u64 dir_gen;
4189 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004190 struct fs_path *name;
4191 int found_idx;
4192};
4193
4194static int __find_iref(int num, u64 dir, int index,
4195 struct fs_path *name,
4196 void *ctx_)
4197{
4198 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004199 u64 dir_gen;
4200 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004201
4202 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4203 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004204 /*
4205 * To avoid doing extra lookups we'll only do this if everything
4206 * else matches.
4207 */
4208 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4209 NULL, NULL, NULL);
4210 if (ret)
4211 return ret;
4212 if (dir_gen != ctx->dir_gen)
4213 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004214 ctx->found_idx = num;
4215 return 1;
4216 }
4217 return 0;
4218}
4219
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004220static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004221 struct btrfs_path *path,
4222 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004223 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004224{
4225 int ret;
4226 struct find_ref_ctx ctx;
4227
4228 ctx.dir = dir;
4229 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004230 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004231 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004232 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004233
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004234 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004235 if (ret < 0)
4236 return ret;
4237
4238 if (ctx.found_idx == -1)
4239 return -ENOENT;
4240
4241 return ctx.found_idx;
4242}
4243
4244static int __record_changed_new_ref(int num, u64 dir, int index,
4245 struct fs_path *name,
4246 void *ctx)
4247{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004248 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004249 int ret;
4250 struct send_ctx *sctx = ctx;
4251
Josef Bacikba5e8f22013-08-16 16:52:55 -04004252 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4253 NULL, NULL, NULL);
4254 if (ret)
4255 return ret;
4256
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004257 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004258 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004259 if (ret == -ENOENT)
4260 ret = __record_new_ref(num, dir, index, name, sctx);
4261 else if (ret > 0)
4262 ret = 0;
4263
4264 return ret;
4265}
4266
4267static int __record_changed_deleted_ref(int num, u64 dir, int index,
4268 struct fs_path *name,
4269 void *ctx)
4270{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004271 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004272 int ret;
4273 struct send_ctx *sctx = ctx;
4274
Josef Bacikba5e8f22013-08-16 16:52:55 -04004275 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4276 NULL, NULL, NULL);
4277 if (ret)
4278 return ret;
4279
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004280 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004281 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004282 if (ret == -ENOENT)
4283 ret = __record_deleted_ref(num, dir, index, name, sctx);
4284 else if (ret > 0)
4285 ret = 0;
4286
4287 return ret;
4288}
4289
4290static int record_changed_ref(struct send_ctx *sctx)
4291{
4292 int ret = 0;
4293
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004294 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004295 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4296 if (ret < 0)
4297 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004298 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004299 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4300 if (ret < 0)
4301 goto out;
4302 ret = 0;
4303
4304out:
4305 return ret;
4306}
4307
4308/*
4309 * Record and process all refs at once. Needed when an inode changes the
4310 * generation number, which means that it was deleted and recreated.
4311 */
4312static int process_all_refs(struct send_ctx *sctx,
4313 enum btrfs_compare_tree_result cmd)
4314{
4315 int ret;
4316 struct btrfs_root *root;
4317 struct btrfs_path *path;
4318 struct btrfs_key key;
4319 struct btrfs_key found_key;
4320 struct extent_buffer *eb;
4321 int slot;
4322 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004323 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004324
4325 path = alloc_path_for_send();
4326 if (!path)
4327 return -ENOMEM;
4328
4329 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4330 root = sctx->send_root;
4331 cb = __record_new_ref;
4332 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4333 root = sctx->parent_root;
4334 cb = __record_deleted_ref;
4335 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004336 btrfs_err(sctx->send_root->fs_info,
4337 "Wrong command %d in process_all_refs", cmd);
4338 ret = -EINVAL;
4339 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004340 }
4341
4342 key.objectid = sctx->cmp_key->objectid;
4343 key.type = BTRFS_INODE_REF_KEY;
4344 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004345 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4346 if (ret < 0)
4347 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004348
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004349 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004350 eb = path->nodes[0];
4351 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004352 if (slot >= btrfs_header_nritems(eb)) {
4353 ret = btrfs_next_leaf(root, path);
4354 if (ret < 0)
4355 goto out;
4356 else if (ret > 0)
4357 break;
4358 continue;
4359 }
4360
Alexander Block31db9f72012-07-25 23:19:24 +02004361 btrfs_item_key_to_cpu(eb, &found_key, slot);
4362
4363 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004364 (found_key.type != BTRFS_INODE_REF_KEY &&
4365 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004366 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004367
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004368 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004369 if (ret < 0)
4370 goto out;
4371
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004372 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004373 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004374 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004375
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004376 /*
4377 * We don't actually care about pending_move as we are simply
4378 * re-creating this inode and will be rename'ing it into place once we
4379 * rename the parent directory.
4380 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004381 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004382out:
4383 btrfs_free_path(path);
4384 return ret;
4385}
4386
4387static int send_set_xattr(struct send_ctx *sctx,
4388 struct fs_path *path,
4389 const char *name, int name_len,
4390 const char *data, int data_len)
4391{
4392 int ret = 0;
4393
4394 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4395 if (ret < 0)
4396 goto out;
4397
4398 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4399 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4400 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4401
4402 ret = send_cmd(sctx);
4403
4404tlv_put_failure:
4405out:
4406 return ret;
4407}
4408
4409static int send_remove_xattr(struct send_ctx *sctx,
4410 struct fs_path *path,
4411 const char *name, int name_len)
4412{
4413 int ret = 0;
4414
4415 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4416 if (ret < 0)
4417 goto out;
4418
4419 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4420 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4421
4422 ret = send_cmd(sctx);
4423
4424tlv_put_failure:
4425out:
4426 return ret;
4427}
4428
4429static int __process_new_xattr(int num, struct btrfs_key *di_key,
4430 const char *name, int name_len,
4431 const char *data, int data_len,
4432 u8 type, void *ctx)
4433{
4434 int ret;
4435 struct send_ctx *sctx = ctx;
4436 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004437 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004438
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004439 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004440 if (!p)
4441 return -ENOMEM;
4442
4443 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004444 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004445 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004446 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004447 * only contains the version number and no entries.
4448 */
4449 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4450 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4451 if (data_len == 0) {
4452 dummy_acl.a_version =
4453 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4454 data = (char *)&dummy_acl;
4455 data_len = sizeof(dummy_acl);
4456 }
4457 }
4458
4459 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4460 if (ret < 0)
4461 goto out;
4462
4463 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4464
4465out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004466 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004467 return ret;
4468}
4469
4470static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4471 const char *name, int name_len,
4472 const char *data, int data_len,
4473 u8 type, void *ctx)
4474{
4475 int ret;
4476 struct send_ctx *sctx = ctx;
4477 struct fs_path *p;
4478
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004479 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004480 if (!p)
4481 return -ENOMEM;
4482
4483 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4484 if (ret < 0)
4485 goto out;
4486
4487 ret = send_remove_xattr(sctx, p, name, name_len);
4488
4489out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004490 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004491 return ret;
4492}
4493
4494static int process_new_xattr(struct send_ctx *sctx)
4495{
4496 int ret = 0;
4497
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004498 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004499 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004500
4501 return ret;
4502}
4503
4504static int process_deleted_xattr(struct send_ctx *sctx)
4505{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004506 return iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004507 __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004508}
4509
4510struct find_xattr_ctx {
4511 const char *name;
4512 int name_len;
4513 int found_idx;
4514 char *found_data;
4515 int found_data_len;
4516};
4517
4518static int __find_xattr(int num, struct btrfs_key *di_key,
4519 const char *name, int name_len,
4520 const char *data, int data_len,
4521 u8 type, void *vctx)
4522{
4523 struct find_xattr_ctx *ctx = vctx;
4524
4525 if (name_len == ctx->name_len &&
4526 strncmp(name, ctx->name, name_len) == 0) {
4527 ctx->found_idx = num;
4528 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004529 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004530 if (!ctx->found_data)
4531 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004532 return 1;
4533 }
4534 return 0;
4535}
4536
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004537static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004538 struct btrfs_path *path,
4539 struct btrfs_key *key,
4540 const char *name, int name_len,
4541 char **data, int *data_len)
4542{
4543 int ret;
4544 struct find_xattr_ctx ctx;
4545
4546 ctx.name = name;
4547 ctx.name_len = name_len;
4548 ctx.found_idx = -1;
4549 ctx.found_data = NULL;
4550 ctx.found_data_len = 0;
4551
Nikolay Borisova0357512017-08-21 12:43:44 +03004552 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004553 if (ret < 0)
4554 return ret;
4555
4556 if (ctx.found_idx == -1)
4557 return -ENOENT;
4558 if (data) {
4559 *data = ctx.found_data;
4560 *data_len = ctx.found_data_len;
4561 } else {
4562 kfree(ctx.found_data);
4563 }
4564 return ctx.found_idx;
4565}
4566
4567
4568static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4569 const char *name, int name_len,
4570 const char *data, int data_len,
4571 u8 type, void *ctx)
4572{
4573 int ret;
4574 struct send_ctx *sctx = ctx;
4575 char *found_data = NULL;
4576 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004577
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004578 ret = find_xattr(sctx->parent_root, sctx->right_path,
4579 sctx->cmp_key, name, name_len, &found_data,
4580 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004581 if (ret == -ENOENT) {
4582 ret = __process_new_xattr(num, di_key, name, name_len, data,
4583 data_len, type, ctx);
4584 } else if (ret >= 0) {
4585 if (data_len != found_data_len ||
4586 memcmp(data, found_data, data_len)) {
4587 ret = __process_new_xattr(num, di_key, name, name_len,
4588 data, data_len, type, ctx);
4589 } else {
4590 ret = 0;
4591 }
4592 }
4593
4594 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004595 return ret;
4596}
4597
4598static int __process_changed_deleted_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 *ctx)
4602{
4603 int ret;
4604 struct send_ctx *sctx = ctx;
4605
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004606 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4607 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004608 if (ret == -ENOENT)
4609 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4610 data_len, type, ctx);
4611 else if (ret >= 0)
4612 ret = 0;
4613
4614 return ret;
4615}
4616
4617static int process_changed_xattr(struct send_ctx *sctx)
4618{
4619 int ret = 0;
4620
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004621 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004622 __process_changed_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004623 if (ret < 0)
4624 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004625 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004626 __process_changed_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004627
4628out:
4629 return ret;
4630}
4631
4632static int process_all_new_xattrs(struct send_ctx *sctx)
4633{
4634 int ret;
4635 struct btrfs_root *root;
4636 struct btrfs_path *path;
4637 struct btrfs_key key;
4638 struct btrfs_key found_key;
4639 struct extent_buffer *eb;
4640 int slot;
4641
4642 path = alloc_path_for_send();
4643 if (!path)
4644 return -ENOMEM;
4645
4646 root = sctx->send_root;
4647
4648 key.objectid = sctx->cmp_key->objectid;
4649 key.type = BTRFS_XATTR_ITEM_KEY;
4650 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004651 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4652 if (ret < 0)
4653 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004654
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004655 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004656 eb = path->nodes[0];
4657 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004658 if (slot >= btrfs_header_nritems(eb)) {
4659 ret = btrfs_next_leaf(root, path);
4660 if (ret < 0) {
4661 goto out;
4662 } else if (ret > 0) {
4663 ret = 0;
4664 break;
4665 }
4666 continue;
4667 }
Alexander Block31db9f72012-07-25 23:19:24 +02004668
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004669 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004670 if (found_key.objectid != key.objectid ||
4671 found_key.type != key.type) {
4672 ret = 0;
4673 goto out;
4674 }
4675
Nikolay Borisova0357512017-08-21 12:43:44 +03004676 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004677 if (ret < 0)
4678 goto out;
4679
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004680 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004681 }
4682
4683out:
4684 btrfs_free_path(path);
4685 return ret;
4686}
4687
Josef Baciked259092013-10-25 11:36:01 -04004688static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4689{
4690 struct btrfs_root *root = sctx->send_root;
4691 struct btrfs_fs_info *fs_info = root->fs_info;
4692 struct inode *inode;
4693 struct page *page;
4694 char *addr;
4695 struct btrfs_key key;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004696 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004697 pgoff_t last_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004698 unsigned pg_offset = offset & ~PAGE_MASK;
Josef Baciked259092013-10-25 11:36:01 -04004699 ssize_t ret = 0;
4700
4701 key.objectid = sctx->cur_ino;
4702 key.type = BTRFS_INODE_ITEM_KEY;
4703 key.offset = 0;
4704
4705 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4706 if (IS_ERR(inode))
4707 return PTR_ERR(inode);
4708
4709 if (offset + len > i_size_read(inode)) {
4710 if (offset > i_size_read(inode))
4711 len = 0;
4712 else
4713 len = offset - i_size_read(inode);
4714 }
4715 if (len == 0)
4716 goto out;
4717
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004718 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004719
4720 /* initial readahead */
4721 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4722 file_ra_state_init(&sctx->ra, inode->i_mapping);
David Sterbad3c0bab2017-06-22 03:35:28 +02004723 page_cache_sync_readahead(inode->i_mapping, &sctx->ra, NULL, index,
Liu Bo2131bcd2014-03-05 10:07:35 +08004724 last_index - index + 1);
4725
Josef Baciked259092013-10-25 11:36:01 -04004726 while (index <= last_index) {
4727 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004728 PAGE_SIZE - pg_offset);
David Sterbae780b0d2016-01-18 18:42:13 +01004729 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
Josef Baciked259092013-10-25 11:36:01 -04004730 if (!page) {
4731 ret = -ENOMEM;
4732 break;
4733 }
4734
4735 if (!PageUptodate(page)) {
4736 btrfs_readpage(NULL, page);
4737 lock_page(page);
4738 if (!PageUptodate(page)) {
4739 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004740 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004741 ret = -EIO;
4742 break;
4743 }
4744 }
4745
4746 addr = kmap(page);
4747 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4748 kunmap(page);
4749 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004750 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004751 index++;
4752 pg_offset = 0;
4753 len -= cur_len;
4754 ret += cur_len;
4755 }
4756out:
4757 iput(inode);
4758 return ret;
4759}
4760
Alexander Block31db9f72012-07-25 23:19:24 +02004761/*
4762 * Read some bytes from the current inode/file and send a write command to
4763 * user space.
4764 */
4765static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4766{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004767 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02004768 int ret = 0;
4769 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004770 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004771
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004772 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004773 if (!p)
4774 return -ENOMEM;
4775
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004776 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004777
Josef Baciked259092013-10-25 11:36:01 -04004778 num_read = fill_read_buf(sctx, offset, len);
4779 if (num_read <= 0) {
4780 if (num_read < 0)
4781 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004782 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004783 }
Alexander Block31db9f72012-07-25 23:19:24 +02004784
4785 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4786 if (ret < 0)
4787 goto out;
4788
4789 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4790 if (ret < 0)
4791 goto out;
4792
4793 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4794 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004795 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004796
4797 ret = send_cmd(sctx);
4798
4799tlv_put_failure:
4800out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004801 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004802 if (ret < 0)
4803 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004804 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004805}
4806
4807/*
4808 * Send a clone command to user space.
4809 */
4810static int send_clone(struct send_ctx *sctx,
4811 u64 offset, u32 len,
4812 struct clone_root *clone_root)
4813{
4814 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004815 struct fs_path *p;
4816 u64 gen;
4817
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004818 btrfs_debug(sctx->send_root->fs_info,
4819 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4820 offset, len, clone_root->root->objectid, clone_root->ino,
4821 clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02004822
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004823 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004824 if (!p)
4825 return -ENOMEM;
4826
4827 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4828 if (ret < 0)
4829 goto out;
4830
4831 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4832 if (ret < 0)
4833 goto out;
4834
4835 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4836 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4837 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4838
Alexander Blocke938c8a2012-07-28 16:33:49 +02004839 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004840 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004841 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004842 if (ret < 0)
4843 goto out;
4844 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4845 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004846 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004847 }
4848 if (ret < 0)
4849 goto out;
4850
Josef Bacik37b8d272015-06-04 17:17:25 -04004851 /*
4852 * If the parent we're using has a received_uuid set then use that as
4853 * our clone source as that is what we will look for when doing a
4854 * receive.
4855 *
4856 * This covers the case that we create a snapshot off of a received
4857 * subvolume and then use that as the parent and try to receive on a
4858 * different host.
4859 */
4860 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4861 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4862 clone_root->root->root_item.received_uuid);
4863 else
4864 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4865 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004866 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004867 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004868 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4869 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4870 clone_root->offset);
4871
4872 ret = send_cmd(sctx);
4873
4874tlv_put_failure:
4875out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004876 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004877 return ret;
4878}
4879
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004880/*
4881 * Send an update extent command to user space.
4882 */
4883static int send_update_extent(struct send_ctx *sctx,
4884 u64 offset, u32 len)
4885{
4886 int ret = 0;
4887 struct fs_path *p;
4888
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004889 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004890 if (!p)
4891 return -ENOMEM;
4892
4893 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4894 if (ret < 0)
4895 goto out;
4896
4897 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4898 if (ret < 0)
4899 goto out;
4900
4901 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4902 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4903 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4904
4905 ret = send_cmd(sctx);
4906
4907tlv_put_failure:
4908out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004909 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004910 return ret;
4911}
4912
Josef Bacik16e75492013-10-22 12:18:51 -04004913static int send_hole(struct send_ctx *sctx, u64 end)
4914{
4915 struct fs_path *p = NULL;
4916 u64 offset = sctx->cur_inode_last_extent;
4917 u64 len;
4918 int ret = 0;
4919
4920 p = fs_path_alloc();
4921 if (!p)
4922 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01004923 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4924 if (ret < 0)
4925 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04004926 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4927 while (offset < end) {
4928 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4929
4930 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4931 if (ret < 0)
4932 break;
Josef Bacik16e75492013-10-22 12:18:51 -04004933 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4934 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4935 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4936 ret = send_cmd(sctx);
4937 if (ret < 0)
4938 break;
4939 offset += len;
4940 }
4941tlv_put_failure:
4942 fs_path_free(p);
4943 return ret;
4944}
4945
Filipe Mananad906d492015-10-02 10:47:34 +01004946static int send_extent_data(struct send_ctx *sctx,
4947 const u64 offset,
4948 const u64 len)
4949{
4950 u64 sent = 0;
4951
4952 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
4953 return send_update_extent(sctx, offset, len);
4954
4955 while (sent < len) {
4956 u64 size = len - sent;
4957 int ret;
4958
4959 if (size > BTRFS_SEND_READ_SIZE)
4960 size = BTRFS_SEND_READ_SIZE;
4961 ret = send_write(sctx, offset + sent, size);
4962 if (ret < 0)
4963 return ret;
4964 if (!ret)
4965 break;
4966 sent += ret;
4967 }
4968 return 0;
4969}
4970
4971static int clone_range(struct send_ctx *sctx,
4972 struct clone_root *clone_root,
4973 const u64 disk_byte,
4974 u64 data_offset,
4975 u64 offset,
4976 u64 len)
4977{
4978 struct btrfs_path *path;
4979 struct btrfs_key key;
4980 int ret;
4981
Filipe Manana72610b12017-08-10 22:54:51 +01004982 /*
4983 * Prevent cloning from a zero offset with a length matching the sector
4984 * size because in some scenarios this will make the receiver fail.
4985 *
4986 * For example, if in the source filesystem the extent at offset 0
4987 * has a length of sectorsize and it was written using direct IO, then
4988 * it can never be an inline extent (even if compression is enabled).
4989 * Then this extent can be cloned in the original filesystem to a non
4990 * zero file offset, but it may not be possible to clone in the
4991 * destination filesystem because it can be inlined due to compression
4992 * on the destination filesystem (as the receiver's write operations are
4993 * always done using buffered IO). The same happens when the original
4994 * filesystem does not have compression enabled but the destination
4995 * filesystem has.
4996 */
4997 if (clone_root->offset == 0 &&
4998 len == sctx->send_root->fs_info->sectorsize)
4999 return send_extent_data(sctx, offset, len);
5000
Filipe Mananad906d492015-10-02 10:47:34 +01005001 path = alloc_path_for_send();
5002 if (!path)
5003 return -ENOMEM;
5004
5005 /*
5006 * We can't send a clone operation for the entire range if we find
5007 * extent items in the respective range in the source file that
5008 * refer to different extents or if we find holes.
5009 * So check for that and do a mix of clone and regular write/copy
5010 * operations if needed.
5011 *
5012 * Example:
5013 *
5014 * mkfs.btrfs -f /dev/sda
5015 * mount /dev/sda /mnt
5016 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5017 * cp --reflink=always /mnt/foo /mnt/bar
5018 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5019 * btrfs subvolume snapshot -r /mnt /mnt/snap
5020 *
5021 * If when we send the snapshot and we are processing file bar (which
5022 * has a higher inode number than foo) we blindly send a clone operation
5023 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5024 * a file bar that matches the content of file foo - iow, doesn't match
5025 * the content from bar in the original filesystem.
5026 */
5027 key.objectid = clone_root->ino;
5028 key.type = BTRFS_EXTENT_DATA_KEY;
5029 key.offset = clone_root->offset;
5030 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5031 if (ret < 0)
5032 goto out;
5033 if (ret > 0 && path->slots[0] > 0) {
5034 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5035 if (key.objectid == clone_root->ino &&
5036 key.type == BTRFS_EXTENT_DATA_KEY)
5037 path->slots[0]--;
5038 }
5039
5040 while (true) {
5041 struct extent_buffer *leaf = path->nodes[0];
5042 int slot = path->slots[0];
5043 struct btrfs_file_extent_item *ei;
5044 u8 type;
5045 u64 ext_len;
5046 u64 clone_len;
5047
5048 if (slot >= btrfs_header_nritems(leaf)) {
5049 ret = btrfs_next_leaf(clone_root->root, path);
5050 if (ret < 0)
5051 goto out;
5052 else if (ret > 0)
5053 break;
5054 continue;
5055 }
5056
5057 btrfs_item_key_to_cpu(leaf, &key, slot);
5058
5059 /*
5060 * We might have an implicit trailing hole (NO_HOLES feature
5061 * enabled). We deal with it after leaving this loop.
5062 */
5063 if (key.objectid != clone_root->ino ||
5064 key.type != BTRFS_EXTENT_DATA_KEY)
5065 break;
5066
5067 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5068 type = btrfs_file_extent_type(leaf, ei);
5069 if (type == BTRFS_FILE_EXTENT_INLINE) {
5070 ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005071 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01005072 } else {
5073 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5074 }
5075
5076 if (key.offset + ext_len <= clone_root->offset)
5077 goto next;
5078
5079 if (key.offset > clone_root->offset) {
5080 /* Implicit hole, NO_HOLES feature enabled. */
5081 u64 hole_len = key.offset - clone_root->offset;
5082
5083 if (hole_len > len)
5084 hole_len = len;
5085 ret = send_extent_data(sctx, offset, hole_len);
5086 if (ret < 0)
5087 goto out;
5088
5089 len -= hole_len;
5090 if (len == 0)
5091 break;
5092 offset += hole_len;
5093 clone_root->offset += hole_len;
5094 data_offset += hole_len;
5095 }
5096
5097 if (key.offset >= clone_root->offset + len)
5098 break;
5099
5100 clone_len = min_t(u64, ext_len, len);
5101
5102 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5103 btrfs_file_extent_offset(leaf, ei) == data_offset)
5104 ret = send_clone(sctx, offset, clone_len, clone_root);
5105 else
5106 ret = send_extent_data(sctx, offset, clone_len);
5107
5108 if (ret < 0)
5109 goto out;
5110
5111 len -= clone_len;
5112 if (len == 0)
5113 break;
5114 offset += clone_len;
5115 clone_root->offset += clone_len;
5116 data_offset += clone_len;
5117next:
5118 path->slots[0]++;
5119 }
5120
5121 if (len > 0)
5122 ret = send_extent_data(sctx, offset, len);
5123 else
5124 ret = 0;
5125out:
5126 btrfs_free_path(path);
5127 return ret;
5128}
5129
Alexander Block31db9f72012-07-25 23:19:24 +02005130static int send_write_or_clone(struct send_ctx *sctx,
5131 struct btrfs_path *path,
5132 struct btrfs_key *key,
5133 struct clone_root *clone_root)
5134{
5135 int ret = 0;
5136 struct btrfs_file_extent_item *ei;
5137 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02005138 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02005139 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005140 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005141
5142 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5143 struct btrfs_file_extent_item);
5144 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005145 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005146 len = btrfs_file_extent_inline_len(path->nodes[0],
5147 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005148 /*
5149 * it is possible the inline item won't cover the whole page,
5150 * but there may be items after this page. Make
5151 * sure to send the whole thing
5152 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005153 len = PAGE_ALIGN(len);
Chris Mason74dd17f2012-08-07 16:25:13 -04005154 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02005155 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005156 }
Alexander Block31db9f72012-07-25 23:19:24 +02005157
5158 if (offset + len > sctx->cur_inode_size)
5159 len = sctx->cur_inode_size - offset;
5160 if (len == 0) {
5161 ret = 0;
5162 goto out;
5163 }
5164
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005165 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01005166 u64 disk_byte;
5167 u64 data_offset;
5168
5169 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5170 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5171 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5172 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005173 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005174 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005175 }
Alexander Block31db9f72012-07-25 23:19:24 +02005176out:
5177 return ret;
5178}
5179
5180static int is_extent_unchanged(struct send_ctx *sctx,
5181 struct btrfs_path *left_path,
5182 struct btrfs_key *ekey)
5183{
5184 int ret = 0;
5185 struct btrfs_key key;
5186 struct btrfs_path *path = NULL;
5187 struct extent_buffer *eb;
5188 int slot;
5189 struct btrfs_key found_key;
5190 struct btrfs_file_extent_item *ei;
5191 u64 left_disknr;
5192 u64 right_disknr;
5193 u64 left_offset;
5194 u64 right_offset;
5195 u64 left_offset_fixed;
5196 u64 left_len;
5197 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005198 u64 left_gen;
5199 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005200 u8 left_type;
5201 u8 right_type;
5202
5203 path = alloc_path_for_send();
5204 if (!path)
5205 return -ENOMEM;
5206
5207 eb = left_path->nodes[0];
5208 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005209 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5210 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005211
5212 if (left_type != BTRFS_FILE_EXTENT_REG) {
5213 ret = 0;
5214 goto out;
5215 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005216 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5217 left_len = btrfs_file_extent_num_bytes(eb, ei);
5218 left_offset = btrfs_file_extent_offset(eb, ei);
5219 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005220
5221 /*
5222 * Following comments will refer to these graphics. L is the left
5223 * extents which we are checking at the moment. 1-8 are the right
5224 * extents that we iterate.
5225 *
5226 * |-----L-----|
5227 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5228 *
5229 * |-----L-----|
5230 * |--1--|-2b-|...(same as above)
5231 *
5232 * Alternative situation. Happens on files where extents got split.
5233 * |-----L-----|
5234 * |-----------7-----------|-6-|
5235 *
5236 * Alternative situation. Happens on files which got larger.
5237 * |-----L-----|
5238 * |-8-|
5239 * Nothing follows after 8.
5240 */
5241
5242 key.objectid = ekey->objectid;
5243 key.type = BTRFS_EXTENT_DATA_KEY;
5244 key.offset = ekey->offset;
5245 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5246 if (ret < 0)
5247 goto out;
5248 if (ret) {
5249 ret = 0;
5250 goto out;
5251 }
5252
5253 /*
5254 * Handle special case where the right side has no extents at all.
5255 */
5256 eb = path->nodes[0];
5257 slot = path->slots[0];
5258 btrfs_item_key_to_cpu(eb, &found_key, slot);
5259 if (found_key.objectid != key.objectid ||
5260 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005261 /* If we're a hole then just pretend nothing changed */
5262 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005263 goto out;
5264 }
5265
5266 /*
5267 * We're now on 2a, 2b or 7.
5268 */
5269 key = found_key;
5270 while (key.offset < ekey->offset + left_len) {
5271 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5272 right_type = btrfs_file_extent_type(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005273 if (right_type != BTRFS_FILE_EXTENT_REG &&
5274 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005275 ret = 0;
5276 goto out;
5277 }
5278
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005279 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5280 right_len = btrfs_file_extent_inline_len(eb, slot, ei);
5281 right_len = PAGE_ALIGN(right_len);
5282 } else {
5283 right_len = btrfs_file_extent_num_bytes(eb, ei);
5284 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005285
Alexander Block31db9f72012-07-25 23:19:24 +02005286 /*
5287 * Are we at extent 8? If yes, we know the extent is changed.
5288 * This may only happen on the first iteration.
5289 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005290 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005291 /* If we're a hole just pretend nothing changed */
5292 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005293 goto out;
5294 }
5295
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005296 /*
5297 * We just wanted to see if when we have an inline extent, what
5298 * follows it is a regular extent (wanted to check the above
5299 * condition for inline extents too). This should normally not
5300 * happen but it's possible for example when we have an inline
5301 * compressed extent representing data with a size matching
5302 * the page size (currently the same as sector size).
5303 */
5304 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5305 ret = 0;
5306 goto out;
5307 }
5308
Filipe Manana24e52b12017-07-06 15:31:46 +01005309 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5310 right_offset = btrfs_file_extent_offset(eb, ei);
5311 right_gen = btrfs_file_extent_generation(eb, ei);
5312
Alexander Block31db9f72012-07-25 23:19:24 +02005313 left_offset_fixed = left_offset;
5314 if (key.offset < ekey->offset) {
5315 /* Fix the right offset for 2a and 7. */
5316 right_offset += ekey->offset - key.offset;
5317 } else {
5318 /* Fix the left offset for all behind 2a and 2b */
5319 left_offset_fixed += key.offset - ekey->offset;
5320 }
5321
5322 /*
5323 * Check if we have the same extent.
5324 */
Alexander Block39540962012-08-01 12:46:05 +02005325 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005326 left_offset_fixed != right_offset ||
5327 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005328 ret = 0;
5329 goto out;
5330 }
5331
5332 /*
5333 * Go to the next extent.
5334 */
5335 ret = btrfs_next_item(sctx->parent_root, path);
5336 if (ret < 0)
5337 goto out;
5338 if (!ret) {
5339 eb = path->nodes[0];
5340 slot = path->slots[0];
5341 btrfs_item_key_to_cpu(eb, &found_key, slot);
5342 }
5343 if (ret || found_key.objectid != key.objectid ||
5344 found_key.type != key.type) {
5345 key.offset += right_len;
5346 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005347 }
5348 if (found_key.offset != key.offset + right_len) {
5349 ret = 0;
5350 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005351 }
5352 key = found_key;
5353 }
5354
5355 /*
5356 * We're now behind the left extent (treat as unchanged) or at the end
5357 * of the right side (treat as changed).
5358 */
5359 if (key.offset >= ekey->offset + left_len)
5360 ret = 1;
5361 else
5362 ret = 0;
5363
5364
5365out:
5366 btrfs_free_path(path);
5367 return ret;
5368}
5369
Josef Bacik16e75492013-10-22 12:18:51 -04005370static int get_last_extent(struct send_ctx *sctx, u64 offset)
5371{
5372 struct btrfs_path *path;
5373 struct btrfs_root *root = sctx->send_root;
5374 struct btrfs_file_extent_item *fi;
5375 struct btrfs_key key;
5376 u64 extent_end;
5377 u8 type;
5378 int ret;
5379
5380 path = alloc_path_for_send();
5381 if (!path)
5382 return -ENOMEM;
5383
5384 sctx->cur_inode_last_extent = 0;
5385
5386 key.objectid = sctx->cur_ino;
5387 key.type = BTRFS_EXTENT_DATA_KEY;
5388 key.offset = offset;
5389 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5390 if (ret < 0)
5391 goto out;
5392 ret = 0;
5393 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5394 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5395 goto out;
5396
5397 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5398 struct btrfs_file_extent_item);
5399 type = btrfs_file_extent_type(path->nodes[0], fi);
5400 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005401 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5402 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005403 extent_end = ALIGN(key.offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005404 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005405 } else {
5406 extent_end = key.offset +
5407 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5408 }
5409 sctx->cur_inode_last_extent = extent_end;
5410out:
5411 btrfs_free_path(path);
5412 return ret;
5413}
5414
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005415static int range_is_hole_in_parent(struct send_ctx *sctx,
5416 const u64 start,
5417 const u64 end)
5418{
5419 struct btrfs_path *path;
5420 struct btrfs_key key;
5421 struct btrfs_root *root = sctx->parent_root;
5422 u64 search_start = start;
5423 int ret;
5424
5425 path = alloc_path_for_send();
5426 if (!path)
5427 return -ENOMEM;
5428
5429 key.objectid = sctx->cur_ino;
5430 key.type = BTRFS_EXTENT_DATA_KEY;
5431 key.offset = search_start;
5432 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5433 if (ret < 0)
5434 goto out;
5435 if (ret > 0 && path->slots[0] > 0)
5436 path->slots[0]--;
5437
5438 while (search_start < end) {
5439 struct extent_buffer *leaf = path->nodes[0];
5440 int slot = path->slots[0];
5441 struct btrfs_file_extent_item *fi;
5442 u64 extent_end;
5443
5444 if (slot >= btrfs_header_nritems(leaf)) {
5445 ret = btrfs_next_leaf(root, path);
5446 if (ret < 0)
5447 goto out;
5448 else if (ret > 0)
5449 break;
5450 continue;
5451 }
5452
5453 btrfs_item_key_to_cpu(leaf, &key, slot);
5454 if (key.objectid < sctx->cur_ino ||
5455 key.type < BTRFS_EXTENT_DATA_KEY)
5456 goto next;
5457 if (key.objectid > sctx->cur_ino ||
5458 key.type > BTRFS_EXTENT_DATA_KEY ||
5459 key.offset >= end)
5460 break;
5461
5462 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5463 if (btrfs_file_extent_type(leaf, fi) ==
5464 BTRFS_FILE_EXTENT_INLINE) {
5465 u64 size = btrfs_file_extent_inline_len(leaf, slot, fi);
5466
5467 extent_end = ALIGN(key.offset + size,
5468 root->fs_info->sectorsize);
5469 } else {
5470 extent_end = key.offset +
5471 btrfs_file_extent_num_bytes(leaf, fi);
5472 }
5473 if (extent_end <= start)
5474 goto next;
5475 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5476 search_start = extent_end;
5477 goto next;
5478 }
5479 ret = 0;
5480 goto out;
5481next:
5482 path->slots[0]++;
5483 }
5484 ret = 1;
5485out:
5486 btrfs_free_path(path);
5487 return ret;
5488}
5489
Josef Bacik16e75492013-10-22 12:18:51 -04005490static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5491 struct btrfs_key *key)
5492{
5493 struct btrfs_file_extent_item *fi;
5494 u64 extent_end;
5495 u8 type;
5496 int ret = 0;
5497
5498 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5499 return 0;
5500
5501 if (sctx->cur_inode_last_extent == (u64)-1) {
5502 ret = get_last_extent(sctx, key->offset - 1);
5503 if (ret)
5504 return ret;
5505 }
5506
5507 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5508 struct btrfs_file_extent_item);
5509 type = btrfs_file_extent_type(path->nodes[0], fi);
5510 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005511 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5512 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005513 extent_end = ALIGN(key->offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005514 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005515 } else {
5516 extent_end = key->offset +
5517 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5518 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005519
5520 if (path->slots[0] == 0 &&
5521 sctx->cur_inode_last_extent < key->offset) {
5522 /*
5523 * We might have skipped entire leafs that contained only
5524 * file extent items for our current inode. These leafs have
5525 * a generation number smaller (older) than the one in the
5526 * current leaf and the leaf our last extent came from, and
5527 * are located between these 2 leafs.
5528 */
5529 ret = get_last_extent(sctx, key->offset - 1);
5530 if (ret)
5531 return ret;
5532 }
5533
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005534 if (sctx->cur_inode_last_extent < key->offset) {
5535 ret = range_is_hole_in_parent(sctx,
5536 sctx->cur_inode_last_extent,
5537 key->offset);
5538 if (ret < 0)
5539 return ret;
5540 else if (ret == 0)
5541 ret = send_hole(sctx, key->offset);
5542 else
5543 ret = 0;
5544 }
Josef Bacik16e75492013-10-22 12:18:51 -04005545 sctx->cur_inode_last_extent = extent_end;
5546 return ret;
5547}
5548
Alexander Block31db9f72012-07-25 23:19:24 +02005549static int process_extent(struct send_ctx *sctx,
5550 struct btrfs_path *path,
5551 struct btrfs_key *key)
5552{
Alexander Block31db9f72012-07-25 23:19:24 +02005553 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005554 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005555
5556 if (S_ISLNK(sctx->cur_inode_mode))
5557 return 0;
5558
5559 if (sctx->parent_root && !sctx->cur_inode_new) {
5560 ret = is_extent_unchanged(sctx, path, key);
5561 if (ret < 0)
5562 goto out;
5563 if (ret) {
5564 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005565 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005566 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005567 } else {
5568 struct btrfs_file_extent_item *ei;
5569 u8 type;
5570
5571 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5572 struct btrfs_file_extent_item);
5573 type = btrfs_file_extent_type(path->nodes[0], ei);
5574 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5575 type == BTRFS_FILE_EXTENT_REG) {
5576 /*
5577 * The send spec does not have a prealloc command yet,
5578 * so just leave a hole for prealloc'ed extents until
5579 * we have enough commands queued up to justify rev'ing
5580 * the send spec.
5581 */
5582 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5583 ret = 0;
5584 goto out;
5585 }
5586
5587 /* Have a hole, just skip it. */
5588 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5589 ret = 0;
5590 goto out;
5591 }
5592 }
Alexander Block31db9f72012-07-25 23:19:24 +02005593 }
5594
5595 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5596 sctx->cur_inode_size, &found_clone);
5597 if (ret != -ENOENT && ret < 0)
5598 goto out;
5599
5600 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005601 if (ret)
5602 goto out;
5603out_hole:
5604 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005605out:
5606 return ret;
5607}
5608
5609static int process_all_extents(struct send_ctx *sctx)
5610{
5611 int ret;
5612 struct btrfs_root *root;
5613 struct btrfs_path *path;
5614 struct btrfs_key key;
5615 struct btrfs_key found_key;
5616 struct extent_buffer *eb;
5617 int slot;
5618
5619 root = sctx->send_root;
5620 path = alloc_path_for_send();
5621 if (!path)
5622 return -ENOMEM;
5623
5624 key.objectid = sctx->cmp_key->objectid;
5625 key.type = BTRFS_EXTENT_DATA_KEY;
5626 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005627 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5628 if (ret < 0)
5629 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005630
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005631 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005632 eb = path->nodes[0];
5633 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005634
5635 if (slot >= btrfs_header_nritems(eb)) {
5636 ret = btrfs_next_leaf(root, path);
5637 if (ret < 0) {
5638 goto out;
5639 } else if (ret > 0) {
5640 ret = 0;
5641 break;
5642 }
5643 continue;
5644 }
5645
Alexander Block31db9f72012-07-25 23:19:24 +02005646 btrfs_item_key_to_cpu(eb, &found_key, slot);
5647
5648 if (found_key.objectid != key.objectid ||
5649 found_key.type != key.type) {
5650 ret = 0;
5651 goto out;
5652 }
5653
5654 ret = process_extent(sctx, path, &found_key);
5655 if (ret < 0)
5656 goto out;
5657
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005658 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005659 }
5660
5661out:
5662 btrfs_free_path(path);
5663 return ret;
5664}
5665
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005666static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5667 int *pending_move,
5668 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005669{
5670 int ret = 0;
5671
5672 if (sctx->cur_ino == 0)
5673 goto out;
5674 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005675 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005676 goto out;
5677 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5678 goto out;
5679
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005680 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005681 if (ret < 0)
5682 goto out;
5683
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005684 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005685out:
5686 return ret;
5687}
5688
5689static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5690{
5691 int ret = 0;
5692 u64 left_mode;
5693 u64 left_uid;
5694 u64 left_gid;
5695 u64 right_mode;
5696 u64 right_uid;
5697 u64 right_gid;
5698 int need_chmod = 0;
5699 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005700 int pending_move = 0;
5701 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005702
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005703 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5704 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005705 if (ret < 0)
5706 goto out;
5707
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005708 /*
5709 * We have processed the refs and thus need to advance send_progress.
5710 * Now, calls to get_cur_xxx will take the updated refs of the current
5711 * inode into account.
5712 *
5713 * On the other hand, if our current inode is a directory and couldn't
5714 * be moved/renamed because its parent was renamed/moved too and it has
5715 * a higher inode number, we can only move/rename our current inode
5716 * after we moved/renamed its parent. Therefore in this case operate on
5717 * the old path (pre move/rename) of our current inode, and the
5718 * move/rename will be performed later.
5719 */
5720 if (refs_processed && !pending_move)
5721 sctx->send_progress = sctx->cur_ino + 1;
5722
Alexander Block31db9f72012-07-25 23:19:24 +02005723 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5724 goto out;
5725 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5726 goto out;
5727
5728 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005729 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005730 if (ret < 0)
5731 goto out;
5732
Alex Lyakase2d044f2012-10-17 13:52:47 +00005733 if (!sctx->parent_root || sctx->cur_inode_new) {
5734 need_chown = 1;
5735 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005736 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005737 } else {
5738 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5739 NULL, NULL, &right_mode, &right_uid,
5740 &right_gid, NULL);
5741 if (ret < 0)
5742 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005743
Alex Lyakase2d044f2012-10-17 13:52:47 +00005744 if (left_uid != right_uid || left_gid != right_gid)
5745 need_chown = 1;
5746 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5747 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005748 }
5749
5750 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005751 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005752 if (sctx->cur_inode_last_extent == (u64)-1 ||
5753 sctx->cur_inode_last_extent <
5754 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005755 ret = get_last_extent(sctx, (u64)-1);
5756 if (ret)
5757 goto out;
5758 }
5759 if (sctx->cur_inode_last_extent <
5760 sctx->cur_inode_size) {
5761 ret = send_hole(sctx, sctx->cur_inode_size);
5762 if (ret)
5763 goto out;
5764 }
5765 }
Alexander Block31db9f72012-07-25 23:19:24 +02005766 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5767 sctx->cur_inode_size);
5768 if (ret < 0)
5769 goto out;
5770 }
5771
5772 if (need_chown) {
5773 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5774 left_uid, left_gid);
5775 if (ret < 0)
5776 goto out;
5777 }
5778 if (need_chmod) {
5779 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5780 left_mode);
5781 if (ret < 0)
5782 goto out;
5783 }
5784
5785 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005786 * If other directory inodes depended on our current directory
5787 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005788 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005789 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5790 ret = apply_children_dir_moves(sctx);
5791 if (ret)
5792 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005793 /*
5794 * Need to send that every time, no matter if it actually
5795 * changed between the two trees as we have done changes to
5796 * the inode before. If our inode is a directory and it's
5797 * waiting to be moved/renamed, we will send its utimes when
5798 * it's moved/renamed, therefore we don't need to do it here.
5799 */
5800 sctx->send_progress = sctx->cur_ino + 1;
5801 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5802 if (ret < 0)
5803 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005804 }
5805
Alexander Block31db9f72012-07-25 23:19:24 +02005806out:
5807 return ret;
5808}
5809
5810static int changed_inode(struct send_ctx *sctx,
5811 enum btrfs_compare_tree_result result)
5812{
5813 int ret = 0;
5814 struct btrfs_key *key = sctx->cmp_key;
5815 struct btrfs_inode_item *left_ii = NULL;
5816 struct btrfs_inode_item *right_ii = NULL;
5817 u64 left_gen = 0;
5818 u64 right_gen = 0;
5819
Alexander Block31db9f72012-07-25 23:19:24 +02005820 sctx->cur_ino = key->objectid;
5821 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005822 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005823
5824 /*
5825 * Set send_progress to current inode. This will tell all get_cur_xxx
5826 * functions that the current inode's refs are not updated yet. Later,
5827 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5828 */
Alexander Block31db9f72012-07-25 23:19:24 +02005829 sctx->send_progress = sctx->cur_ino;
5830
5831 if (result == BTRFS_COMPARE_TREE_NEW ||
5832 result == BTRFS_COMPARE_TREE_CHANGED) {
5833 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5834 sctx->left_path->slots[0],
5835 struct btrfs_inode_item);
5836 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5837 left_ii);
5838 } else {
5839 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5840 sctx->right_path->slots[0],
5841 struct btrfs_inode_item);
5842 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5843 right_ii);
5844 }
5845 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5846 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5847 sctx->right_path->slots[0],
5848 struct btrfs_inode_item);
5849
5850 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5851 right_ii);
Alexander Block6d85ed052012-08-01 14:48:59 +02005852
5853 /*
5854 * The cur_ino = root dir case is special here. We can't treat
5855 * the inode as deleted+reused because it would generate a
5856 * stream that tries to delete/mkdir the root dir.
5857 */
5858 if (left_gen != right_gen &&
5859 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005860 sctx->cur_inode_new_gen = 1;
5861 }
5862
5863 if (result == BTRFS_COMPARE_TREE_NEW) {
5864 sctx->cur_inode_gen = left_gen;
5865 sctx->cur_inode_new = 1;
5866 sctx->cur_inode_deleted = 0;
5867 sctx->cur_inode_size = btrfs_inode_size(
5868 sctx->left_path->nodes[0], left_ii);
5869 sctx->cur_inode_mode = btrfs_inode_mode(
5870 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005871 sctx->cur_inode_rdev = btrfs_inode_rdev(
5872 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005873 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005874 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005875 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5876 sctx->cur_inode_gen = right_gen;
5877 sctx->cur_inode_new = 0;
5878 sctx->cur_inode_deleted = 1;
5879 sctx->cur_inode_size = btrfs_inode_size(
5880 sctx->right_path->nodes[0], right_ii);
5881 sctx->cur_inode_mode = btrfs_inode_mode(
5882 sctx->right_path->nodes[0], right_ii);
5883 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005884 /*
5885 * We need to do some special handling in case the inode was
5886 * reported as changed with a changed generation number. This
5887 * means that the original inode was deleted and new inode
5888 * reused the same inum. So we have to treat the old inode as
5889 * deleted and the new one as new.
5890 */
Alexander Block31db9f72012-07-25 23:19:24 +02005891 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005892 /*
5893 * First, process the inode as if it was deleted.
5894 */
Alexander Block31db9f72012-07-25 23:19:24 +02005895 sctx->cur_inode_gen = right_gen;
5896 sctx->cur_inode_new = 0;
5897 sctx->cur_inode_deleted = 1;
5898 sctx->cur_inode_size = btrfs_inode_size(
5899 sctx->right_path->nodes[0], right_ii);
5900 sctx->cur_inode_mode = btrfs_inode_mode(
5901 sctx->right_path->nodes[0], right_ii);
5902 ret = process_all_refs(sctx,
5903 BTRFS_COMPARE_TREE_DELETED);
5904 if (ret < 0)
5905 goto out;
5906
Alexander Block766702e2012-07-28 14:11:31 +02005907 /*
5908 * Now process the inode as if it was new.
5909 */
Alexander Block31db9f72012-07-25 23:19:24 +02005910 sctx->cur_inode_gen = left_gen;
5911 sctx->cur_inode_new = 1;
5912 sctx->cur_inode_deleted = 0;
5913 sctx->cur_inode_size = btrfs_inode_size(
5914 sctx->left_path->nodes[0], left_ii);
5915 sctx->cur_inode_mode = btrfs_inode_mode(
5916 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005917 sctx->cur_inode_rdev = btrfs_inode_rdev(
5918 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005919 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005920 if (ret < 0)
5921 goto out;
5922
5923 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5924 if (ret < 0)
5925 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005926 /*
5927 * Advance send_progress now as we did not get into
5928 * process_recorded_refs_if_needed in the new_gen case.
5929 */
5930 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005931
5932 /*
5933 * Now process all extents and xattrs of the inode as if
5934 * they were all new.
5935 */
Alexander Block31db9f72012-07-25 23:19:24 +02005936 ret = process_all_extents(sctx);
5937 if (ret < 0)
5938 goto out;
5939 ret = process_all_new_xattrs(sctx);
5940 if (ret < 0)
5941 goto out;
5942 } else {
5943 sctx->cur_inode_gen = left_gen;
5944 sctx->cur_inode_new = 0;
5945 sctx->cur_inode_new_gen = 0;
5946 sctx->cur_inode_deleted = 0;
5947 sctx->cur_inode_size = btrfs_inode_size(
5948 sctx->left_path->nodes[0], left_ii);
5949 sctx->cur_inode_mode = btrfs_inode_mode(
5950 sctx->left_path->nodes[0], left_ii);
5951 }
5952 }
5953
5954out:
5955 return ret;
5956}
5957
Alexander Block766702e2012-07-28 14:11:31 +02005958/*
5959 * We have to process new refs before deleted refs, but compare_trees gives us
5960 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5961 * first and later process them in process_recorded_refs.
5962 * For the cur_inode_new_gen case, we skip recording completely because
5963 * changed_inode did already initiate processing of refs. The reason for this is
5964 * that in this case, compare_tree actually compares the refs of 2 different
5965 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5966 * refs of the right tree as deleted and all refs of the left tree as new.
5967 */
Alexander Block31db9f72012-07-25 23:19:24 +02005968static int changed_ref(struct send_ctx *sctx,
5969 enum btrfs_compare_tree_result result)
5970{
5971 int ret = 0;
5972
Filipe Manana95155582016-08-01 01:50:37 +01005973 if (sctx->cur_ino != sctx->cmp_key->objectid) {
5974 inconsistent_snapshot_error(sctx, result, "reference");
5975 return -EIO;
5976 }
Alexander Block31db9f72012-07-25 23:19:24 +02005977
5978 if (!sctx->cur_inode_new_gen &&
5979 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5980 if (result == BTRFS_COMPARE_TREE_NEW)
5981 ret = record_new_ref(sctx);
5982 else if (result == BTRFS_COMPARE_TREE_DELETED)
5983 ret = record_deleted_ref(sctx);
5984 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5985 ret = record_changed_ref(sctx);
5986 }
5987
5988 return ret;
5989}
5990
Alexander Block766702e2012-07-28 14:11:31 +02005991/*
5992 * Process new/deleted/changed xattrs. We skip processing in the
5993 * cur_inode_new_gen case because changed_inode did already initiate processing
5994 * of xattrs. The reason is the same as in changed_ref
5995 */
Alexander Block31db9f72012-07-25 23:19:24 +02005996static int changed_xattr(struct send_ctx *sctx,
5997 enum btrfs_compare_tree_result result)
5998{
5999 int ret = 0;
6000
Filipe Manana95155582016-08-01 01:50:37 +01006001 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6002 inconsistent_snapshot_error(sctx, result, "xattr");
6003 return -EIO;
6004 }
Alexander Block31db9f72012-07-25 23:19:24 +02006005
6006 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6007 if (result == BTRFS_COMPARE_TREE_NEW)
6008 ret = process_new_xattr(sctx);
6009 else if (result == BTRFS_COMPARE_TREE_DELETED)
6010 ret = process_deleted_xattr(sctx);
6011 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6012 ret = process_changed_xattr(sctx);
6013 }
6014
6015 return ret;
6016}
6017
Alexander Block766702e2012-07-28 14:11:31 +02006018/*
6019 * Process new/deleted/changed extents. We skip processing in the
6020 * cur_inode_new_gen case because changed_inode did already initiate processing
6021 * of extents. The reason is the same as in changed_ref
6022 */
Alexander Block31db9f72012-07-25 23:19:24 +02006023static int changed_extent(struct send_ctx *sctx,
6024 enum btrfs_compare_tree_result result)
6025{
6026 int ret = 0;
6027
Filipe Manana95155582016-08-01 01:50:37 +01006028 if (sctx->cur_ino != sctx->cmp_key->objectid) {
Filipe Mananad5e84fd2016-09-19 10:57:40 +01006029
6030 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6031 struct extent_buffer *leaf_l;
6032 struct extent_buffer *leaf_r;
6033 struct btrfs_file_extent_item *ei_l;
6034 struct btrfs_file_extent_item *ei_r;
6035
6036 leaf_l = sctx->left_path->nodes[0];
6037 leaf_r = sctx->right_path->nodes[0];
6038 ei_l = btrfs_item_ptr(leaf_l,
6039 sctx->left_path->slots[0],
6040 struct btrfs_file_extent_item);
6041 ei_r = btrfs_item_ptr(leaf_r,
6042 sctx->right_path->slots[0],
6043 struct btrfs_file_extent_item);
6044
6045 /*
6046 * We may have found an extent item that has changed
6047 * only its disk_bytenr field and the corresponding
6048 * inode item was not updated. This case happens due to
6049 * very specific timings during relocation when a leaf
6050 * that contains file extent items is COWed while
6051 * relocation is ongoing and its in the stage where it
6052 * updates data pointers. So when this happens we can
6053 * safely ignore it since we know it's the same extent,
6054 * but just at different logical and physical locations
6055 * (when an extent is fully replaced with a new one, we
6056 * know the generation number must have changed too,
6057 * since snapshot creation implies committing the current
6058 * transaction, and the inode item must have been updated
6059 * as well).
6060 * This replacement of the disk_bytenr happens at
6061 * relocation.c:replace_file_extents() through
6062 * relocation.c:btrfs_reloc_cow_block().
6063 */
6064 if (btrfs_file_extent_generation(leaf_l, ei_l) ==
6065 btrfs_file_extent_generation(leaf_r, ei_r) &&
6066 btrfs_file_extent_ram_bytes(leaf_l, ei_l) ==
6067 btrfs_file_extent_ram_bytes(leaf_r, ei_r) &&
6068 btrfs_file_extent_compression(leaf_l, ei_l) ==
6069 btrfs_file_extent_compression(leaf_r, ei_r) &&
6070 btrfs_file_extent_encryption(leaf_l, ei_l) ==
6071 btrfs_file_extent_encryption(leaf_r, ei_r) &&
6072 btrfs_file_extent_other_encoding(leaf_l, ei_l) ==
6073 btrfs_file_extent_other_encoding(leaf_r, ei_r) &&
6074 btrfs_file_extent_type(leaf_l, ei_l) ==
6075 btrfs_file_extent_type(leaf_r, ei_r) &&
6076 btrfs_file_extent_disk_bytenr(leaf_l, ei_l) !=
6077 btrfs_file_extent_disk_bytenr(leaf_r, ei_r) &&
6078 btrfs_file_extent_disk_num_bytes(leaf_l, ei_l) ==
6079 btrfs_file_extent_disk_num_bytes(leaf_r, ei_r) &&
6080 btrfs_file_extent_offset(leaf_l, ei_l) ==
6081 btrfs_file_extent_offset(leaf_r, ei_r) &&
6082 btrfs_file_extent_num_bytes(leaf_l, ei_l) ==
6083 btrfs_file_extent_num_bytes(leaf_r, ei_r))
6084 return 0;
6085 }
6086
Filipe Manana95155582016-08-01 01:50:37 +01006087 inconsistent_snapshot_error(sctx, result, "extent");
6088 return -EIO;
6089 }
Alexander Block31db9f72012-07-25 23:19:24 +02006090
6091 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6092 if (result != BTRFS_COMPARE_TREE_DELETED)
6093 ret = process_extent(sctx, sctx->left_path,
6094 sctx->cmp_key);
6095 }
6096
6097 return ret;
6098}
6099
Josef Bacikba5e8f22013-08-16 16:52:55 -04006100static int dir_changed(struct send_ctx *sctx, u64 dir)
6101{
6102 u64 orig_gen, new_gen;
6103 int ret;
6104
6105 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6106 NULL, NULL);
6107 if (ret)
6108 return ret;
6109
6110 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6111 NULL, NULL, NULL);
6112 if (ret)
6113 return ret;
6114
6115 return (orig_gen != new_gen) ? 1 : 0;
6116}
6117
6118static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6119 struct btrfs_key *key)
6120{
6121 struct btrfs_inode_extref *extref;
6122 struct extent_buffer *leaf;
6123 u64 dirid = 0, last_dirid = 0;
6124 unsigned long ptr;
6125 u32 item_size;
6126 u32 cur_offset = 0;
6127 int ref_name_len;
6128 int ret = 0;
6129
6130 /* Easy case, just check this one dirid */
6131 if (key->type == BTRFS_INODE_REF_KEY) {
6132 dirid = key->offset;
6133
6134 ret = dir_changed(sctx, dirid);
6135 goto out;
6136 }
6137
6138 leaf = path->nodes[0];
6139 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6140 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6141 while (cur_offset < item_size) {
6142 extref = (struct btrfs_inode_extref *)(ptr +
6143 cur_offset);
6144 dirid = btrfs_inode_extref_parent(leaf, extref);
6145 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6146 cur_offset += ref_name_len + sizeof(*extref);
6147 if (dirid == last_dirid)
6148 continue;
6149 ret = dir_changed(sctx, dirid);
6150 if (ret)
6151 break;
6152 last_dirid = dirid;
6153 }
6154out:
6155 return ret;
6156}
6157
Alexander Block766702e2012-07-28 14:11:31 +02006158/*
6159 * Updates compare related fields in sctx and simply forwards to the actual
6160 * changed_xxx functions.
6161 */
Alexander Block31db9f72012-07-25 23:19:24 +02006162static int changed_cb(struct btrfs_root *left_root,
6163 struct btrfs_root *right_root,
6164 struct btrfs_path *left_path,
6165 struct btrfs_path *right_path,
6166 struct btrfs_key *key,
6167 enum btrfs_compare_tree_result result,
6168 void *ctx)
6169{
6170 int ret = 0;
6171 struct send_ctx *sctx = ctx;
6172
Josef Bacikba5e8f22013-08-16 16:52:55 -04006173 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04006174 if (key->type == BTRFS_INODE_REF_KEY ||
6175 key->type == BTRFS_INODE_EXTREF_KEY) {
6176 ret = compare_refs(sctx, left_path, key);
6177 if (!ret)
6178 return 0;
6179 if (ret < 0)
6180 return ret;
6181 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6182 return maybe_send_hole(sctx, left_path, key);
6183 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04006184 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006185 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04006186 result = BTRFS_COMPARE_TREE_CHANGED;
6187 ret = 0;
6188 }
6189
Alexander Block31db9f72012-07-25 23:19:24 +02006190 sctx->left_path = left_path;
6191 sctx->right_path = right_path;
6192 sctx->cmp_key = key;
6193
6194 ret = finish_inode_if_needed(sctx, 0);
6195 if (ret < 0)
6196 goto out;
6197
Alexander Block2981e222012-08-01 14:47:03 +02006198 /* Ignore non-FS objects */
6199 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6200 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6201 goto out;
6202
Alexander Block31db9f72012-07-25 23:19:24 +02006203 if (key->type == BTRFS_INODE_ITEM_KEY)
6204 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00006205 else if (key->type == BTRFS_INODE_REF_KEY ||
6206 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02006207 ret = changed_ref(sctx, result);
6208 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6209 ret = changed_xattr(sctx, result);
6210 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6211 ret = changed_extent(sctx, result);
6212
6213out:
6214 return ret;
6215}
6216
6217static int full_send_tree(struct send_ctx *sctx)
6218{
6219 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006220 struct btrfs_root *send_root = sctx->send_root;
6221 struct btrfs_key key;
6222 struct btrfs_key found_key;
6223 struct btrfs_path *path;
6224 struct extent_buffer *eb;
6225 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006226
6227 path = alloc_path_for_send();
6228 if (!path)
6229 return -ENOMEM;
6230
Alexander Block31db9f72012-07-25 23:19:24 +02006231 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6232 key.type = BTRFS_INODE_ITEM_KEY;
6233 key.offset = 0;
6234
Alexander Block31db9f72012-07-25 23:19:24 +02006235 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6236 if (ret < 0)
6237 goto out;
6238 if (ret)
6239 goto out_finish;
6240
6241 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006242 eb = path->nodes[0];
6243 slot = path->slots[0];
6244 btrfs_item_key_to_cpu(eb, &found_key, slot);
6245
6246 ret = changed_cb(send_root, NULL, path, NULL,
6247 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
6248 if (ret < 0)
6249 goto out;
6250
6251 key.objectid = found_key.objectid;
6252 key.type = found_key.type;
6253 key.offset = found_key.offset + 1;
6254
6255 ret = btrfs_next_item(send_root, path);
6256 if (ret < 0)
6257 goto out;
6258 if (ret) {
6259 ret = 0;
6260 break;
6261 }
6262 }
6263
6264out_finish:
6265 ret = finish_inode_if_needed(sctx, 1);
6266
6267out:
6268 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006269 return ret;
6270}
6271
6272static int send_subvol(struct send_ctx *sctx)
6273{
6274 int ret;
6275
Stefan Behrensc2c71322013-04-10 17:10:52 +00006276 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6277 ret = send_header(sctx);
6278 if (ret < 0)
6279 goto out;
6280 }
Alexander Block31db9f72012-07-25 23:19:24 +02006281
6282 ret = send_subvol_begin(sctx);
6283 if (ret < 0)
6284 goto out;
6285
6286 if (sctx->parent_root) {
6287 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6288 changed_cb, sctx);
6289 if (ret < 0)
6290 goto out;
6291 ret = finish_inode_if_needed(sctx, 1);
6292 if (ret < 0)
6293 goto out;
6294 } else {
6295 ret = full_send_tree(sctx);
6296 if (ret < 0)
6297 goto out;
6298 }
6299
6300out:
Alexander Block31db9f72012-07-25 23:19:24 +02006301 free_recorded_refs(sctx);
6302 return ret;
6303}
6304
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006305/*
6306 * If orphan cleanup did remove any orphans from a root, it means the tree
6307 * was modified and therefore the commit root is not the same as the current
6308 * root anymore. This is a problem, because send uses the commit root and
6309 * therefore can see inode items that don't exist in the current root anymore,
6310 * and for example make calls to btrfs_iget, which will do tree lookups based
6311 * on the current root and not on the commit root. Those lookups will fail,
6312 * returning a -ESTALE error, and making send fail with that error. So make
6313 * sure a send does not see any orphans we have just removed, and that it will
6314 * see the same inodes regardless of whether a transaction commit happened
6315 * before it started (meaning that the commit root will be the same as the
6316 * current root) or not.
6317 */
6318static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6319{
6320 int i;
6321 struct btrfs_trans_handle *trans = NULL;
6322
6323again:
6324 if (sctx->parent_root &&
6325 sctx->parent_root->node != sctx->parent_root->commit_root)
6326 goto commit_trans;
6327
6328 for (i = 0; i < sctx->clone_roots_cnt; i++)
6329 if (sctx->clone_roots[i].root->node !=
6330 sctx->clone_roots[i].root->commit_root)
6331 goto commit_trans;
6332
6333 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006334 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006335
6336 return 0;
6337
6338commit_trans:
6339 /* Use any root, all fs roots will get their commit roots updated. */
6340 if (!trans) {
6341 trans = btrfs_join_transaction(sctx->send_root);
6342 if (IS_ERR(trans))
6343 return PTR_ERR(trans);
6344 goto again;
6345 }
6346
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006347 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006348}
6349
David Sterba66ef7d62013-12-17 15:07:20 +01006350static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6351{
6352 spin_lock(&root->root_item_lock);
6353 root->send_in_progress--;
6354 /*
6355 * Not much left to do, we don't know why it's unbalanced and
6356 * can't blindly reset it to 0.
6357 */
6358 if (root->send_in_progress < 0)
6359 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006360 "send_in_progres unbalanced %d root %llu",
6361 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01006362 spin_unlock(&root->root_item_lock);
6363}
6364
Alexander Block31db9f72012-07-25 23:19:24 +02006365long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
6366{
6367 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006368 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6369 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02006370 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02006371 struct btrfs_ioctl_send_args *arg = NULL;
6372 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006373 struct send_ctx *sctx = NULL;
6374 u32 i;
6375 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01006376 int clone_sources_to_rollback = 0;
David Sterbae55d1152016-04-11 18:52:02 +02006377 unsigned alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08006378 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08006379 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02006380
6381 if (!capable(CAP_SYS_ADMIN))
6382 return -EPERM;
6383
Josef Bacik139f8072013-05-20 11:26:50 -04006384 /*
David Sterba2c686532013-12-16 17:34:17 +01006385 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02006386 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01006387 */
6388 spin_lock(&send_root->root_item_lock);
6389 send_root->send_in_progress++;
6390 spin_unlock(&send_root->root_item_lock);
6391
6392 /*
Josef Bacik139f8072013-05-20 11:26:50 -04006393 * This is done when we lookup the root, it should already be complete
6394 * by the time we get here.
6395 */
6396 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6397
6398 /*
David Sterba2c686532013-12-16 17:34:17 +01006399 * Userspace tools do the checks and warn the user if it's
6400 * not RO.
6401 */
6402 if (!btrfs_root_readonly(send_root)) {
6403 ret = -EPERM;
6404 goto out;
6405 }
6406
Alexander Block31db9f72012-07-25 23:19:24 +02006407 arg = memdup_user(arg_, sizeof(*arg));
6408 if (IS_ERR(arg)) {
6409 ret = PTR_ERR(arg);
6410 arg = NULL;
6411 goto out;
6412 }
6413
Dan Carpenter457ae722017-03-17 23:51:20 +03006414 /*
6415 * Check that we don't overflow at later allocations, we request
6416 * clone_sources_count + 1 items, and compare to unsigned long inside
6417 * access_ok.
6418 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006419 if (arg->clone_sources_count >
Dan Carpenter457ae722017-03-17 23:51:20 +03006420 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006421 ret = -EINVAL;
6422 goto out;
6423 }
6424
Alexander Block31db9f72012-07-25 23:19:24 +02006425 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05006426 sizeof(*arg->clone_sources) *
6427 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02006428 ret = -EFAULT;
6429 goto out;
6430 }
6431
Stefan Behrensc2c71322013-04-10 17:10:52 +00006432 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006433 ret = -EINVAL;
6434 goto out;
6435 }
6436
David Sterbae780b0d2016-01-18 18:42:13 +01006437 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006438 if (!sctx) {
6439 ret = -ENOMEM;
6440 goto out;
6441 }
6442
6443 INIT_LIST_HEAD(&sctx->new_refs);
6444 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01006445 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006446 INIT_LIST_HEAD(&sctx->name_cache_list);
6447
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006448 sctx->flags = arg->flags;
6449
Alexander Block31db9f72012-07-25 23:19:24 +02006450 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00006451 if (!sctx->send_filp) {
6452 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02006453 goto out;
6454 }
6455
Alexander Block31db9f72012-07-25 23:19:24 +02006456 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02006457 /*
6458 * Unlikely but possible, if the subvolume is marked for deletion but
6459 * is slow to remove the directory entry, send can still be started
6460 */
6461 if (btrfs_root_dead(sctx->send_root)) {
6462 ret = -EPERM;
6463 goto out;
6464 }
6465
Alexander Block31db9f72012-07-25 23:19:24 +02006466 sctx->clone_roots_cnt = arg->clone_sources_count;
6467
6468 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
Michal Hocko752ade62017-05-08 15:57:27 -07006469 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006470 if (!sctx->send_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006471 ret = -ENOMEM;
6472 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006473 }
6474
Michal Hocko752ade62017-05-08 15:57:27 -07006475 sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006476 if (!sctx->read_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006477 ret = -ENOMEM;
6478 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006479 }
6480
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006481 sctx->pending_dir_moves = RB_ROOT;
6482 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00006483 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006484
David Sterbae55d1152016-04-11 18:52:02 +02006485 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6486
David Sterba818e0102017-05-31 18:40:02 +02006487 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006488 if (!sctx->clone_roots) {
David Sterba818e0102017-05-31 18:40:02 +02006489 ret = -ENOMEM;
6490 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006491 }
6492
David Sterbae55d1152016-04-11 18:52:02 +02006493 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6494
Alexander Block31db9f72012-07-25 23:19:24 +02006495 if (arg->clone_sources_count) {
Michal Hocko752ade62017-05-08 15:57:27 -07006496 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006497 if (!clone_sources_tmp) {
Michal Hocko752ade62017-05-08 15:57:27 -07006498 ret = -ENOMEM;
6499 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006500 }
6501
6502 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02006503 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02006504 if (ret) {
6505 ret = -EFAULT;
6506 goto out;
6507 }
6508
6509 for (i = 0; i < arg->clone_sources_count; i++) {
6510 key.objectid = clone_sources_tmp[i];
6511 key.type = BTRFS_ROOT_ITEM_KEY;
6512 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006513
6514 index = srcu_read_lock(&fs_info->subvol_srcu);
6515
Alexander Block31db9f72012-07-25 23:19:24 +02006516 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02006517 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006518 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006519 ret = PTR_ERR(clone_root);
6520 goto out;
6521 }
David Sterba2c686532013-12-16 17:34:17 +01006522 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00006523 if (!btrfs_root_readonly(clone_root) ||
6524 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006525 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006526 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006527 ret = -EPERM;
6528 goto out;
6529 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00006530 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01006531 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006532 srcu_read_unlock(&fs_info->subvol_srcu, index);
6533
Alexander Block31db9f72012-07-25 23:19:24 +02006534 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00006535 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006536 }
David Sterba2f913062016-04-11 18:40:08 +02006537 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006538 clone_sources_tmp = NULL;
6539 }
6540
6541 if (arg->parent_root) {
6542 key.objectid = arg->parent_root;
6543 key.type = BTRFS_ROOT_ITEM_KEY;
6544 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006545
6546 index = srcu_read_lock(&fs_info->subvol_srcu);
6547
Alexander Block31db9f72012-07-25 23:19:24 +02006548 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006549 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006550 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006551 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02006552 goto out;
6553 }
Wang Shilong18f687d2014-01-07 17:25:19 +08006554
David Sterba2c686532013-12-16 17:34:17 +01006555 spin_lock(&sctx->parent_root->root_item_lock);
6556 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02006557 if (!btrfs_root_readonly(sctx->parent_root) ||
6558 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006559 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006560 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006561 ret = -EPERM;
6562 goto out;
6563 }
6564 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006565
6566 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006567 }
6568
6569 /*
6570 * Clones from send_root are allowed, but only if the clone source
6571 * is behind the current send position. This is checked while searching
6572 * for possible clone sources.
6573 */
6574 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6575
6576 /* We do a bsearch later */
6577 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6578 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6579 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08006580 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006581
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006582 ret = ensure_commit_roots_uptodate(sctx);
6583 if (ret)
6584 goto out;
6585
David Sterba2755a0d2014-07-31 00:43:18 +02006586 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02006587 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04006588 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02006589 if (ret < 0)
6590 goto out;
6591
Stefan Behrensc2c71322013-04-10 17:10:52 +00006592 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6593 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6594 if (ret < 0)
6595 goto out;
6596 ret = send_cmd(sctx);
6597 if (ret < 0)
6598 goto out;
6599 }
Alexander Block31db9f72012-07-25 23:19:24 +02006600
6601out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006602 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6603 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6604 struct rb_node *n;
6605 struct pending_dir_move *pm;
6606
6607 n = rb_first(&sctx->pending_dir_moves);
6608 pm = rb_entry(n, struct pending_dir_move, node);
6609 while (!list_empty(&pm->list)) {
6610 struct pending_dir_move *pm2;
6611
6612 pm2 = list_first_entry(&pm->list,
6613 struct pending_dir_move, list);
6614 free_pending_move(sctx, pm2);
6615 }
6616 free_pending_move(sctx, pm);
6617 }
6618
6619 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6620 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6621 struct rb_node *n;
6622 struct waiting_dir_move *dm;
6623
6624 n = rb_first(&sctx->waiting_dir_moves);
6625 dm = rb_entry(n, struct waiting_dir_move, node);
6626 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6627 kfree(dm);
6628 }
6629
Filipe Manana9dc44212014-02-19 14:31:44 +00006630 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6631 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6632 struct rb_node *n;
6633 struct orphan_dir_info *odi;
6634
6635 n = rb_first(&sctx->orphan_dirs);
6636 odi = rb_entry(n, struct orphan_dir_info, node);
6637 free_orphan_dir_info(sctx, odi);
6638 }
6639
Wang Shilong896c14f2014-01-07 17:25:18 +08006640 if (sort_clone_roots) {
6641 for (i = 0; i < sctx->clone_roots_cnt; i++)
6642 btrfs_root_dec_send_in_progress(
6643 sctx->clone_roots[i].root);
6644 } else {
6645 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6646 btrfs_root_dec_send_in_progress(
6647 sctx->clone_roots[i].root);
6648
6649 btrfs_root_dec_send_in_progress(send_root);
6650 }
David Sterba66ef7d62013-12-17 15:07:20 +01006651 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6652 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01006653
Alexander Block31db9f72012-07-25 23:19:24 +02006654 kfree(arg);
David Sterba2f913062016-04-11 18:40:08 +02006655 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006656
6657 if (sctx) {
6658 if (sctx->send_filp)
6659 fput(sctx->send_filp);
6660
David Sterbac03d01f2016-04-11 18:40:08 +02006661 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02006662 kvfree(sctx->send_buf);
David Sterbaeb5b75f2016-04-11 18:40:08 +02006663 kvfree(sctx->read_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02006664
6665 name_cache_free(sctx);
6666
6667 kfree(sctx);
6668 }
6669
6670 return ret;
6671}