blob: 2742324514e4fd284e546b7b01af69b5be0992cb [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;
542 mm_segment_t old_fs;
543 u32 pos = 0;
544
545 old_fs = get_fs();
546 set_fs(KERNEL_DS);
547
548 while (pos < len) {
Fabian Frederickd447d0d2014-07-15 21:17:17 +0200549 ret = vfs_write(filp, (__force const char __user *)buf + pos,
550 len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200551 /* TODO handle that correctly */
552 /*if (ret == -ERESTARTSYS) {
553 continue;
554 }*/
555 if (ret < 0)
556 goto out;
557 if (ret == 0) {
558 ret = -EIO;
559 goto out;
560 }
561 pos += ret;
562 }
563
564 ret = 0;
565
566out:
567 set_fs(old_fs);
568 return ret;
569}
570
571static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
572{
573 struct btrfs_tlv_header *hdr;
574 int total_len = sizeof(*hdr) + len;
575 int left = sctx->send_max_size - sctx->send_size;
576
577 if (unlikely(left < total_len))
578 return -EOVERFLOW;
579
580 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
581 hdr->tlv_type = cpu_to_le16(attr);
582 hdr->tlv_len = cpu_to_le16(len);
583 memcpy(hdr + 1, data, len);
584 sctx->send_size += total_len;
585
586 return 0;
587}
588
David Sterba95bc79d2013-12-16 17:34:10 +0100589#define TLV_PUT_DEFINE_INT(bits) \
590 static int tlv_put_u##bits(struct send_ctx *sctx, \
591 u##bits attr, u##bits value) \
592 { \
593 __le##bits __tmp = cpu_to_le##bits(value); \
594 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
595 }
Alexander Block31db9f72012-07-25 23:19:24 +0200596
David Sterba95bc79d2013-12-16 17:34:10 +0100597TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200598
599static int tlv_put_string(struct send_ctx *sctx, u16 attr,
600 const char *str, int len)
601{
602 if (len == -1)
603 len = strlen(str);
604 return tlv_put(sctx, attr, str, len);
605}
606
607static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
608 const u8 *uuid)
609{
610 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
611}
612
Alexander Block31db9f72012-07-25 23:19:24 +0200613static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
614 struct extent_buffer *eb,
615 struct btrfs_timespec *ts)
616{
617 struct btrfs_timespec bts;
618 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
619 return tlv_put(sctx, attr, &bts, sizeof(bts));
620}
621
622
623#define TLV_PUT(sctx, attrtype, attrlen, data) \
624 do { \
625 ret = tlv_put(sctx, attrtype, attrlen, data); \
626 if (ret < 0) \
627 goto tlv_put_failure; \
628 } while (0)
629
630#define TLV_PUT_INT(sctx, attrtype, bits, value) \
631 do { \
632 ret = tlv_put_u##bits(sctx, attrtype, value); \
633 if (ret < 0) \
634 goto tlv_put_failure; \
635 } while (0)
636
637#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
638#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
639#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
640#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
641#define TLV_PUT_STRING(sctx, attrtype, str, len) \
642 do { \
643 ret = tlv_put_string(sctx, attrtype, str, len); \
644 if (ret < 0) \
645 goto tlv_put_failure; \
646 } while (0)
647#define TLV_PUT_PATH(sctx, attrtype, p) \
648 do { \
649 ret = tlv_put_string(sctx, attrtype, p->start, \
650 p->end - p->start); \
651 if (ret < 0) \
652 goto tlv_put_failure; \
653 } while(0)
654#define TLV_PUT_UUID(sctx, attrtype, uuid) \
655 do { \
656 ret = tlv_put_uuid(sctx, attrtype, uuid); \
657 if (ret < 0) \
658 goto tlv_put_failure; \
659 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200660#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
661 do { \
662 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
663 if (ret < 0) \
664 goto tlv_put_failure; \
665 } while (0)
666
667static int send_header(struct send_ctx *sctx)
668{
669 struct btrfs_stream_header hdr;
670
671 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
672 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
673
Anand Jain1bcea352012-09-14 00:04:21 -0600674 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
675 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200676}
677
678/*
679 * For each command/item we want to send to userspace, we call this function.
680 */
681static int begin_cmd(struct send_ctx *sctx, int cmd)
682{
683 struct btrfs_cmd_header *hdr;
684
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530685 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200686 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200687
688 BUG_ON(sctx->send_size);
689
690 sctx->send_size += sizeof(*hdr);
691 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
692 hdr->cmd = cpu_to_le16(cmd);
693
694 return 0;
695}
696
697static int send_cmd(struct send_ctx *sctx)
698{
699 int ret;
700 struct btrfs_cmd_header *hdr;
701 u32 crc;
702
703 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
704 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
705 hdr->crc = 0;
706
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000707 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200708 hdr->crc = cpu_to_le32(crc);
709
Anand Jain1bcea352012-09-14 00:04:21 -0600710 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
711 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200712
713 sctx->total_send_size += sctx->send_size;
714 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
715 sctx->send_size = 0;
716
717 return ret;
718}
719
720/*
721 * Sends a move instruction to user space
722 */
723static int send_rename(struct send_ctx *sctx,
724 struct fs_path *from, struct fs_path *to)
725{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400726 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200727 int ret;
728
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400729 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200730
731 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
732 if (ret < 0)
733 goto out;
734
735 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
736 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
737
738 ret = send_cmd(sctx);
739
740tlv_put_failure:
741out:
742 return ret;
743}
744
745/*
746 * Sends a link instruction to user space
747 */
748static int send_link(struct send_ctx *sctx,
749 struct fs_path *path, struct fs_path *lnk)
750{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400751 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200752 int ret;
753
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400754 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200755
756 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
757 if (ret < 0)
758 goto out;
759
760 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
761 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
762
763 ret = send_cmd(sctx);
764
765tlv_put_failure:
766out:
767 return ret;
768}
769
770/*
771 * Sends an unlink instruction to user space
772 */
773static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
774{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400775 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200776 int ret;
777
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400778 btrfs_debug(fs_info, "send_unlink %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200779
780 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
781 if (ret < 0)
782 goto out;
783
784 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
785
786 ret = send_cmd(sctx);
787
788tlv_put_failure:
789out:
790 return ret;
791}
792
793/*
794 * Sends a rmdir instruction to user space
795 */
796static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
797{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400798 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200799 int ret;
800
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400801 btrfs_debug(fs_info, "send_rmdir %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200802
803 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
804 if (ret < 0)
805 goto out;
806
807 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
808
809 ret = send_cmd(sctx);
810
811tlv_put_failure:
812out:
813 return ret;
814}
815
816/*
817 * Helper function to retrieve some fields from an inode item.
818 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400819static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
820 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
821 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200822{
823 int ret;
824 struct btrfs_inode_item *ii;
825 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200826
827 key.objectid = ino;
828 key.type = BTRFS_INODE_ITEM_KEY;
829 key.offset = 0;
830 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200831 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400832 if (ret > 0)
833 ret = -ENOENT;
834 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200835 }
836
837 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
838 struct btrfs_inode_item);
839 if (size)
840 *size = btrfs_inode_size(path->nodes[0], ii);
841 if (gen)
842 *gen = btrfs_inode_generation(path->nodes[0], ii);
843 if (mode)
844 *mode = btrfs_inode_mode(path->nodes[0], ii);
845 if (uid)
846 *uid = btrfs_inode_uid(path->nodes[0], ii);
847 if (gid)
848 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200849 if (rdev)
850 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200851
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400852 return ret;
853}
854
855static int get_inode_info(struct btrfs_root *root,
856 u64 ino, u64 *size, u64 *gen,
857 u64 *mode, u64 *uid, u64 *gid,
858 u64 *rdev)
859{
860 struct btrfs_path *path;
861 int ret;
862
863 path = alloc_path_for_send();
864 if (!path)
865 return -ENOMEM;
866 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
867 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200868 btrfs_free_path(path);
869 return ret;
870}
871
872typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
873 struct fs_path *p,
874 void *ctx);
875
876/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000877 * Helper function to iterate the entries in ONE btrfs_inode_ref or
878 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200879 * The iterate callback may return a non zero value to stop iteration. This can
880 * be a negative value for error codes or 1 to simply stop it.
881 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000882 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200883 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000884static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200885 struct btrfs_key *found_key, int resolve,
886 iterate_inode_ref_t iterate, void *ctx)
887{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000888 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200889 struct btrfs_item *item;
890 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000891 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200892 struct btrfs_path *tmp_path;
893 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000894 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200895 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000896 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200897 u32 name_len;
898 char *start;
899 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000900 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200901 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000902 u64 dir;
903 unsigned long name_off;
904 unsigned long elem_size;
905 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200906
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000907 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200908 if (!p)
909 return -ENOMEM;
910
911 tmp_path = alloc_path_for_send();
912 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000913 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200914 return -ENOMEM;
915 }
916
Alexander Block31db9f72012-07-25 23:19:24 +0200917
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000918 if (found_key->type == BTRFS_INODE_REF_KEY) {
919 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
920 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100921 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000922 total = btrfs_item_size(eb, item);
923 elem_size = sizeof(*iref);
924 } else {
925 ptr = btrfs_item_ptr_offset(eb, slot);
926 total = btrfs_item_size_nr(eb, slot);
927 elem_size = sizeof(*extref);
928 }
929
Alexander Block31db9f72012-07-25 23:19:24 +0200930 while (cur < total) {
931 fs_path_reset(p);
932
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000933 if (found_key->type == BTRFS_INODE_REF_KEY) {
934 iref = (struct btrfs_inode_ref *)(ptr + cur);
935 name_len = btrfs_inode_ref_name_len(eb, iref);
936 name_off = (unsigned long)(iref + 1);
937 index = btrfs_inode_ref_index(eb, iref);
938 dir = found_key->offset;
939 } else {
940 extref = (struct btrfs_inode_extref *)(ptr + cur);
941 name_len = btrfs_inode_extref_name_len(eb, extref);
942 name_off = (unsigned long)&extref->name;
943 index = btrfs_inode_extref_index(eb, extref);
944 dir = btrfs_inode_extref_parent(eb, extref);
945 }
946
Alexander Block31db9f72012-07-25 23:19:24 +0200947 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000948 start = btrfs_ref_to_path(root, tmp_path, name_len,
949 name_off, eb, dir,
950 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200951 if (IS_ERR(start)) {
952 ret = PTR_ERR(start);
953 goto out;
954 }
955 if (start < p->buf) {
956 /* overflow , try again with larger buffer */
957 ret = fs_path_ensure_buf(p,
958 p->buf_len + p->buf - start);
959 if (ret < 0)
960 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000961 start = btrfs_ref_to_path(root, tmp_path,
962 name_len, name_off,
963 eb, dir,
964 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200965 if (IS_ERR(start)) {
966 ret = PTR_ERR(start);
967 goto out;
968 }
969 BUG_ON(start < p->buf);
970 }
971 p->start = start;
972 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000973 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
974 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200975 if (ret < 0)
976 goto out;
977 }
978
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000979 cur += elem_size + name_len;
980 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200981 if (ret)
982 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200983 num++;
984 }
985
986out:
987 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000988 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200989 return ret;
990}
991
992typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
993 const char *name, int name_len,
994 const char *data, int data_len,
995 u8 type, void *ctx);
996
997/*
998 * Helper function to iterate the entries in ONE btrfs_dir_item.
999 * The iterate callback may return a non zero value to stop iteration. This can
1000 * be a negative value for error codes or 1 to simply stop it.
1001 *
1002 * path must point to the dir item when called.
1003 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001004static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +02001005 struct btrfs_key *found_key,
1006 iterate_dir_item_t iterate, void *ctx)
1007{
1008 int ret = 0;
1009 struct extent_buffer *eb;
1010 struct btrfs_item *item;
1011 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +02001012 struct btrfs_key di_key;
1013 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +01001014 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +02001015 u32 name_len;
1016 u32 data_len;
1017 u32 cur;
1018 u32 len;
1019 u32 total;
1020 int slot;
1021 int num;
1022 u8 type;
1023
Filipe Manana4395e0c2014-08-20 10:45:45 +01001024 /*
1025 * Start with a small buffer (1 page). If later we end up needing more
1026 * space, which can happen for xattrs on a fs with a leaf size greater
1027 * then the page size, attempt to increase the buffer. Typically xattr
1028 * values are small.
1029 */
1030 buf_len = PATH_MAX;
David Sterbae780b0d2016-01-18 18:42:13 +01001031 buf = kmalloc(buf_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02001032 if (!buf) {
1033 ret = -ENOMEM;
1034 goto out;
1035 }
1036
Alexander Block31db9f72012-07-25 23:19:24 +02001037 eb = path->nodes[0];
1038 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001039 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001040 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1041 cur = 0;
1042 len = 0;
1043 total = btrfs_item_size(eb, item);
1044
1045 num = 0;
1046 while (cur < total) {
1047 name_len = btrfs_dir_name_len(eb, di);
1048 data_len = btrfs_dir_data_len(eb, di);
1049 type = btrfs_dir_type(eb, di);
1050 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1051
Filipe Manana7e3ae332014-05-23 20:15:16 +01001052 if (type == BTRFS_FT_XATTR) {
1053 if (name_len > XATTR_NAME_MAX) {
1054 ret = -ENAMETOOLONG;
1055 goto out;
1056 }
Jeff Mahoneyda170662016-06-15 09:22:56 -04001057 if (name_len + data_len >
1058 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001059 ret = -E2BIG;
1060 goto out;
1061 }
1062 } else {
1063 /*
1064 * Path too long
1065 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001066 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001067 ret = -ENAMETOOLONG;
1068 goto out;
1069 }
Alexander Block31db9f72012-07-25 23:19:24 +02001070 }
1071
Filipe Manana4395e0c2014-08-20 10:45:45 +01001072 if (name_len + data_len > buf_len) {
1073 buf_len = name_len + data_len;
1074 if (is_vmalloc_addr(buf)) {
1075 vfree(buf);
1076 buf = NULL;
1077 } else {
1078 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001079 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001080
1081 if (!tmp)
1082 kfree(buf);
1083 buf = tmp;
1084 }
1085 if (!buf) {
1086 buf = vmalloc(buf_len);
1087 if (!buf) {
1088 ret = -ENOMEM;
1089 goto out;
1090 }
1091 }
1092 }
1093
Alexander Block31db9f72012-07-25 23:19:24 +02001094 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1095 name_len + data_len);
1096
1097 len = sizeof(*di) + name_len + data_len;
1098 di = (struct btrfs_dir_item *)((char *)di + len);
1099 cur += len;
1100
1101 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1102 data_len, type, ctx);
1103 if (ret < 0)
1104 goto out;
1105 if (ret) {
1106 ret = 0;
1107 goto out;
1108 }
1109
1110 num++;
1111 }
1112
1113out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001114 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001115 return ret;
1116}
1117
1118static int __copy_first_ref(int num, u64 dir, int index,
1119 struct fs_path *p, void *ctx)
1120{
1121 int ret;
1122 struct fs_path *pt = ctx;
1123
1124 ret = fs_path_copy(pt, p);
1125 if (ret < 0)
1126 return ret;
1127
1128 /* we want the first only */
1129 return 1;
1130}
1131
1132/*
1133 * Retrieve the first path of an inode. If an inode has more then one
1134 * ref/hardlink, this is ignored.
1135 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001136static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001137 u64 ino, struct fs_path *path)
1138{
1139 int ret;
1140 struct btrfs_key key, found_key;
1141 struct btrfs_path *p;
1142
1143 p = alloc_path_for_send();
1144 if (!p)
1145 return -ENOMEM;
1146
1147 fs_path_reset(path);
1148
1149 key.objectid = ino;
1150 key.type = BTRFS_INODE_REF_KEY;
1151 key.offset = 0;
1152
1153 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1154 if (ret < 0)
1155 goto out;
1156 if (ret) {
1157 ret = 1;
1158 goto out;
1159 }
1160 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1161 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001162 (found_key.type != BTRFS_INODE_REF_KEY &&
1163 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001164 ret = -ENOENT;
1165 goto out;
1166 }
1167
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001168 ret = iterate_inode_ref(root, p, &found_key, 1,
1169 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001170 if (ret < 0)
1171 goto out;
1172 ret = 0;
1173
1174out:
1175 btrfs_free_path(p);
1176 return ret;
1177}
1178
1179struct backref_ctx {
1180 struct send_ctx *sctx;
1181
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001182 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001183 /* number of total found references */
1184 u64 found;
1185
1186 /*
1187 * used for clones found in send_root. clones found behind cur_objectid
1188 * and cur_offset are not considered as allowed clones.
1189 */
1190 u64 cur_objectid;
1191 u64 cur_offset;
1192
1193 /* may be truncated in case it's the last extent in a file */
1194 u64 extent_len;
1195
Filipe Manana619d8c42015-05-03 01:56:00 +01001196 /* data offset in the file extent item */
1197 u64 data_offset;
1198
Alexander Block31db9f72012-07-25 23:19:24 +02001199 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001200 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001201};
1202
1203static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1204{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001205 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001206 struct clone_root *cr = (struct clone_root *)elt;
1207
1208 if (root < cr->root->objectid)
1209 return -1;
1210 if (root > cr->root->objectid)
1211 return 1;
1212 return 0;
1213}
1214
1215static int __clone_root_cmp_sort(const void *e1, const void *e2)
1216{
1217 struct clone_root *cr1 = (struct clone_root *)e1;
1218 struct clone_root *cr2 = (struct clone_root *)e2;
1219
1220 if (cr1->root->objectid < cr2->root->objectid)
1221 return -1;
1222 if (cr1->root->objectid > cr2->root->objectid)
1223 return 1;
1224 return 0;
1225}
1226
1227/*
1228 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001229 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001230 */
1231static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1232{
1233 struct backref_ctx *bctx = ctx_;
1234 struct clone_root *found;
1235 int ret;
1236 u64 i_size;
1237
1238 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001239 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001240 bctx->sctx->clone_roots_cnt,
1241 sizeof(struct clone_root),
1242 __clone_root_cmp_bsearch);
1243 if (!found)
1244 return 0;
1245
1246 if (found->root == bctx->sctx->send_root &&
1247 ino == bctx->cur_objectid &&
1248 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001249 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001250 }
1251
1252 /*
Alexander Block766702e2012-07-28 14:11:31 +02001253 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001254 * accept clones from these extents.
1255 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001256 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1257 NULL, NULL, NULL);
1258 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001259 if (ret < 0)
1260 return ret;
1261
Filipe Manana619d8c42015-05-03 01:56:00 +01001262 if (offset + bctx->data_offset + bctx->extent_len > i_size)
Alexander Block31db9f72012-07-25 23:19:24 +02001263 return 0;
1264
1265 /*
1266 * Make sure we don't consider clones from send_root that are
1267 * behind the current inode/offset.
1268 */
1269 if (found->root == bctx->sctx->send_root) {
1270 /*
1271 * TODO for the moment we don't accept clones from the inode
1272 * that is currently send. We may change this when
1273 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1274 * file.
1275 */
1276 if (ino >= bctx->cur_objectid)
1277 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001278#if 0
1279 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001280 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001281 if (offset + bctx->extent_len > bctx->cur_offset)
1282 return 0;
1283#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001284 }
1285
1286 bctx->found++;
1287 found->found_refs++;
1288 if (ino < found->ino) {
1289 found->ino = ino;
1290 found->offset = offset;
1291 } else if (found->ino == ino) {
1292 /*
1293 * same extent found more then once in the same file.
1294 */
1295 if (found->offset > offset + bctx->extent_len)
1296 found->offset = offset;
1297 }
1298
1299 return 0;
1300}
1301
1302/*
Alexander Block766702e2012-07-28 14:11:31 +02001303 * Given an inode, offset and extent item, it finds a good clone for a clone
1304 * instruction. Returns -ENOENT when none could be found. The function makes
1305 * sure that the returned clone is usable at the point where sending is at the
1306 * moment. This means, that no clones are accepted which lie behind the current
1307 * inode+offset.
1308 *
Alexander Block31db9f72012-07-25 23:19:24 +02001309 * path must point to the extent item when called.
1310 */
1311static int find_extent_clone(struct send_ctx *sctx,
1312 struct btrfs_path *path,
1313 u64 ino, u64 data_offset,
1314 u64 ino_size,
1315 struct clone_root **found)
1316{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001317 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001318 int ret;
1319 int extent_type;
1320 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001321 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001322 u64 num_bytes;
1323 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001324 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001325 struct btrfs_file_extent_item *fi;
1326 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001327 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001328 struct clone_root *cur_clone_root;
1329 struct btrfs_key found_key;
1330 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001331 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001332 u32 i;
1333
1334 tmp_path = alloc_path_for_send();
1335 if (!tmp_path)
1336 return -ENOMEM;
1337
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001338 /* We only use this path under the commit sem */
1339 tmp_path->need_commit_sem = 0;
1340
David Sterbae780b0d2016-01-18 18:42:13 +01001341 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001342 if (!backref_ctx) {
1343 ret = -ENOMEM;
1344 goto out;
1345 }
1346
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001347 backref_ctx->path = tmp_path;
1348
Alexander Block31db9f72012-07-25 23:19:24 +02001349 if (data_offset >= ino_size) {
1350 /*
1351 * There may be extents that lie behind the file's size.
1352 * I at least had this in combination with snapshotting while
1353 * writing large files.
1354 */
1355 ret = 0;
1356 goto out;
1357 }
1358
1359 fi = btrfs_item_ptr(eb, path->slots[0],
1360 struct btrfs_file_extent_item);
1361 extent_type = btrfs_file_extent_type(eb, fi);
1362 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1363 ret = -ENOENT;
1364 goto out;
1365 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001366 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001367
1368 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001369 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1370 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001371 ret = -ENOENT;
1372 goto out;
1373 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001374 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001375
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001376 down_read(&fs_info->commit_root_sem);
1377 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001378 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001379 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001380 btrfs_release_path(tmp_path);
1381
1382 if (ret < 0)
1383 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001384 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001385 ret = -EIO;
1386 goto out;
1387 }
1388
1389 /*
1390 * Setup the clone roots.
1391 */
1392 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1393 cur_clone_root = sctx->clone_roots + i;
1394 cur_clone_root->ino = (u64)-1;
1395 cur_clone_root->offset = 0;
1396 cur_clone_root->found_refs = 0;
1397 }
1398
Alexander Block35075bb2012-07-28 12:44:34 +02001399 backref_ctx->sctx = sctx;
1400 backref_ctx->found = 0;
1401 backref_ctx->cur_objectid = ino;
1402 backref_ctx->cur_offset = data_offset;
1403 backref_ctx->found_itself = 0;
1404 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001405 /*
1406 * For non-compressed extents iterate_extent_inodes() gives us extent
1407 * offsets that already take into account the data offset, but not for
1408 * compressed extents, since the offset is logical and not relative to
1409 * the physical extent locations. We must take this into account to
1410 * avoid sending clone offsets that go beyond the source file's size,
1411 * which would result in the clone ioctl failing with -EINVAL on the
1412 * receiving end.
1413 */
1414 if (compressed == BTRFS_COMPRESS_NONE)
1415 backref_ctx->data_offset = 0;
1416 else
1417 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001418
1419 /*
1420 * The last extent of a file may be too large due to page alignment.
1421 * We need to adjust extent_len in this case so that the checks in
1422 * __iterate_backrefs work.
1423 */
1424 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001425 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001426
1427 /*
1428 * Now collect all backrefs.
1429 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001430 if (compressed == BTRFS_COMPRESS_NONE)
1431 extent_item_pos = logical - found_key.objectid;
1432 else
1433 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001434 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1435 extent_item_pos, 1, __iterate_backrefs,
1436 backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001437
Alexander Block31db9f72012-07-25 23:19:24 +02001438 if (ret < 0)
1439 goto out;
1440
Alexander Block35075bb2012-07-28 12:44:34 +02001441 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001442 /* found a bug in backref code? */
1443 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001444 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001445 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001446 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001447 goto out;
1448 }
1449
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001450 btrfs_debug(fs_info,
1451 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1452 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001453
Alexander Block35075bb2012-07-28 12:44:34 +02001454 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001455 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001456
1457 cur_clone_root = NULL;
1458 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1459 if (sctx->clone_roots[i].found_refs) {
1460 if (!cur_clone_root)
1461 cur_clone_root = sctx->clone_roots + i;
1462 else if (sctx->clone_roots[i].root == sctx->send_root)
1463 /* prefer clones from send_root over others */
1464 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001465 }
1466
1467 }
1468
1469 if (cur_clone_root) {
1470 *found = cur_clone_root;
1471 ret = 0;
1472 } else {
1473 ret = -ENOENT;
1474 }
1475
1476out:
1477 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001478 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001479 return ret;
1480}
1481
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001482static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001483 u64 ino,
1484 struct fs_path *dest)
1485{
1486 int ret;
1487 struct btrfs_path *path;
1488 struct btrfs_key key;
1489 struct btrfs_file_extent_item *ei;
1490 u8 type;
1491 u8 compression;
1492 unsigned long off;
1493 int len;
1494
1495 path = alloc_path_for_send();
1496 if (!path)
1497 return -ENOMEM;
1498
1499 key.objectid = ino;
1500 key.type = BTRFS_EXTENT_DATA_KEY;
1501 key.offset = 0;
1502 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1503 if (ret < 0)
1504 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001505 if (ret) {
1506 /*
1507 * An empty symlink inode. Can happen in rare error paths when
1508 * creating a symlink (transaction committed before the inode
1509 * eviction handler removed the symlink inode items and a crash
1510 * happened in between or the subvol was snapshoted in between).
1511 * Print an informative message to dmesg/syslog so that the user
1512 * can delete the symlink.
1513 */
1514 btrfs_err(root->fs_info,
1515 "Found empty symlink inode %llu at root %llu",
1516 ino, root->root_key.objectid);
1517 ret = -EIO;
1518 goto out;
1519 }
Alexander Block31db9f72012-07-25 23:19:24 +02001520
1521 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1522 struct btrfs_file_extent_item);
1523 type = btrfs_file_extent_type(path->nodes[0], ei);
1524 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1525 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1526 BUG_ON(compression);
1527
1528 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001529 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001530
1531 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001532
1533out:
1534 btrfs_free_path(path);
1535 return ret;
1536}
1537
1538/*
1539 * Helper function to generate a file name that is unique in the root of
1540 * send_root and parent_root. This is used to generate names for orphan inodes.
1541 */
1542static int gen_unique_name(struct send_ctx *sctx,
1543 u64 ino, u64 gen,
1544 struct fs_path *dest)
1545{
1546 int ret = 0;
1547 struct btrfs_path *path;
1548 struct btrfs_dir_item *di;
1549 char tmp[64];
1550 int len;
1551 u64 idx = 0;
1552
1553 path = alloc_path_for_send();
1554 if (!path)
1555 return -ENOMEM;
1556
1557 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001558 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001559 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001560 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001561
1562 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1563 path, BTRFS_FIRST_FREE_OBJECTID,
1564 tmp, strlen(tmp), 0);
1565 btrfs_release_path(path);
1566 if (IS_ERR(di)) {
1567 ret = PTR_ERR(di);
1568 goto out;
1569 }
1570 if (di) {
1571 /* not unique, try again */
1572 idx++;
1573 continue;
1574 }
1575
1576 if (!sctx->parent_root) {
1577 /* unique */
1578 ret = 0;
1579 break;
1580 }
1581
1582 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1583 path, BTRFS_FIRST_FREE_OBJECTID,
1584 tmp, strlen(tmp), 0);
1585 btrfs_release_path(path);
1586 if (IS_ERR(di)) {
1587 ret = PTR_ERR(di);
1588 goto out;
1589 }
1590 if (di) {
1591 /* not unique, try again */
1592 idx++;
1593 continue;
1594 }
1595 /* unique */
1596 break;
1597 }
1598
1599 ret = fs_path_add(dest, tmp, strlen(tmp));
1600
1601out:
1602 btrfs_free_path(path);
1603 return ret;
1604}
1605
1606enum inode_state {
1607 inode_state_no_change,
1608 inode_state_will_create,
1609 inode_state_did_create,
1610 inode_state_will_delete,
1611 inode_state_did_delete,
1612};
1613
1614static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1615{
1616 int ret;
1617 int left_ret;
1618 int right_ret;
1619 u64 left_gen;
1620 u64 right_gen;
1621
1622 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001623 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001624 if (ret < 0 && ret != -ENOENT)
1625 goto out;
1626 left_ret = ret;
1627
1628 if (!sctx->parent_root) {
1629 right_ret = -ENOENT;
1630 } else {
1631 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001632 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001633 if (ret < 0 && ret != -ENOENT)
1634 goto out;
1635 right_ret = ret;
1636 }
1637
1638 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001639 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001640 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001641 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001642 if (ino < sctx->send_progress)
1643 ret = inode_state_did_create;
1644 else
1645 ret = inode_state_will_create;
1646 } else if (right_gen == gen) {
1647 if (ino < sctx->send_progress)
1648 ret = inode_state_did_delete;
1649 else
1650 ret = inode_state_will_delete;
1651 } else {
1652 ret = -ENOENT;
1653 }
1654 } else if (!left_ret) {
1655 if (left_gen == gen) {
1656 if (ino < sctx->send_progress)
1657 ret = inode_state_did_create;
1658 else
1659 ret = inode_state_will_create;
1660 } else {
1661 ret = -ENOENT;
1662 }
1663 } else if (!right_ret) {
1664 if (right_gen == gen) {
1665 if (ino < sctx->send_progress)
1666 ret = inode_state_did_delete;
1667 else
1668 ret = inode_state_will_delete;
1669 } else {
1670 ret = -ENOENT;
1671 }
1672 } else {
1673 ret = -ENOENT;
1674 }
1675
1676out:
1677 return ret;
1678}
1679
1680static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1681{
1682 int ret;
1683
Robbie Ko4dd99202017-01-05 16:24:55 +08001684 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1685 return 1;
1686
Alexander Block31db9f72012-07-25 23:19:24 +02001687 ret = get_cur_inode_state(sctx, ino, gen);
1688 if (ret < 0)
1689 goto out;
1690
1691 if (ret == inode_state_no_change ||
1692 ret == inode_state_did_create ||
1693 ret == inode_state_will_delete)
1694 ret = 1;
1695 else
1696 ret = 0;
1697
1698out:
1699 return ret;
1700}
1701
1702/*
1703 * Helper function to lookup a dir item in a dir.
1704 */
1705static int lookup_dir_item_inode(struct btrfs_root *root,
1706 u64 dir, const char *name, int name_len,
1707 u64 *found_inode,
1708 u8 *found_type)
1709{
1710 int ret = 0;
1711 struct btrfs_dir_item *di;
1712 struct btrfs_key key;
1713 struct btrfs_path *path;
1714
1715 path = alloc_path_for_send();
1716 if (!path)
1717 return -ENOMEM;
1718
1719 di = btrfs_lookup_dir_item(NULL, root, path,
1720 dir, name, name_len, 0);
1721 if (!di) {
1722 ret = -ENOENT;
1723 goto out;
1724 }
1725 if (IS_ERR(di)) {
1726 ret = PTR_ERR(di);
1727 goto out;
1728 }
1729 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001730 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1731 ret = -ENOENT;
1732 goto out;
1733 }
Alexander Block31db9f72012-07-25 23:19:24 +02001734 *found_inode = key.objectid;
1735 *found_type = btrfs_dir_type(path->nodes[0], di);
1736
1737out:
1738 btrfs_free_path(path);
1739 return ret;
1740}
1741
Alexander Block766702e2012-07-28 14:11:31 +02001742/*
1743 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1744 * generation of the parent dir and the name of the dir entry.
1745 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001746static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001747 u64 *dir, u64 *dir_gen, struct fs_path *name)
1748{
1749 int ret;
1750 struct btrfs_key key;
1751 struct btrfs_key found_key;
1752 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001753 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001754 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001755
1756 path = alloc_path_for_send();
1757 if (!path)
1758 return -ENOMEM;
1759
1760 key.objectid = ino;
1761 key.type = BTRFS_INODE_REF_KEY;
1762 key.offset = 0;
1763
1764 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1765 if (ret < 0)
1766 goto out;
1767 if (!ret)
1768 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1769 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001770 if (ret || found_key.objectid != ino ||
1771 (found_key.type != BTRFS_INODE_REF_KEY &&
1772 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001773 ret = -ENOENT;
1774 goto out;
1775 }
1776
Filipe Manana51a60252014-05-13 22:01:02 +01001777 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001778 struct btrfs_inode_ref *iref;
1779 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1780 struct btrfs_inode_ref);
1781 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1782 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1783 (unsigned long)(iref + 1),
1784 len);
1785 parent_dir = found_key.offset;
1786 } else {
1787 struct btrfs_inode_extref *extref;
1788 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1789 struct btrfs_inode_extref);
1790 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1791 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1792 (unsigned long)&extref->name, len);
1793 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1794 }
Alexander Block31db9f72012-07-25 23:19:24 +02001795 if (ret < 0)
1796 goto out;
1797 btrfs_release_path(path);
1798
Filipe Mananab46ab972014-03-21 12:46:54 +00001799 if (dir_gen) {
1800 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1801 NULL, NULL, NULL);
1802 if (ret < 0)
1803 goto out;
1804 }
Alexander Block31db9f72012-07-25 23:19:24 +02001805
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001806 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001807
1808out:
1809 btrfs_free_path(path);
1810 return ret;
1811}
1812
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001813static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001814 u64 ino, u64 dir,
1815 const char *name, int name_len)
1816{
1817 int ret;
1818 struct fs_path *tmp_name;
1819 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001820
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001821 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001822 if (!tmp_name)
1823 return -ENOMEM;
1824
Filipe Mananab46ab972014-03-21 12:46:54 +00001825 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001826 if (ret < 0)
1827 goto out;
1828
Alexander Blockb9291af2012-07-28 11:07:18 +02001829 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001830 ret = 0;
1831 goto out;
1832 }
1833
Alexander Blocke938c8a2012-07-28 16:33:49 +02001834 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001835
1836out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001837 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001838 return ret;
1839}
1840
Alexander Block766702e2012-07-28 14:11:31 +02001841/*
1842 * Used by process_recorded_refs to determine if a new ref would overwrite an
1843 * already existing ref. In case it detects an overwrite, it returns the
1844 * inode/gen in who_ino/who_gen.
1845 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1846 * to make sure later references to the overwritten inode are possible.
1847 * Orphanizing is however only required for the first ref of an inode.
1848 * process_recorded_refs does an additional is_first_ref check to see if
1849 * orphanizing is really required.
1850 */
Alexander Block31db9f72012-07-25 23:19:24 +02001851static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1852 const char *name, int name_len,
1853 u64 *who_ino, u64 *who_gen)
1854{
1855 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001856 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001857 u64 other_inode = 0;
1858 u8 other_type = 0;
1859
1860 if (!sctx->parent_root)
1861 goto out;
1862
1863 ret = is_inode_existent(sctx, dir, dir_gen);
1864 if (ret <= 0)
1865 goto out;
1866
Josef Bacikebdad912013-08-06 16:47:48 -04001867 /*
1868 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001869 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001870 * and we can just unlink this entry.
1871 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001872 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001873 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1874 NULL, NULL, NULL);
1875 if (ret < 0 && ret != -ENOENT)
1876 goto out;
1877 if (ret) {
1878 ret = 0;
1879 goto out;
1880 }
1881 if (gen != dir_gen)
1882 goto out;
1883 }
1884
Alexander Block31db9f72012-07-25 23:19:24 +02001885 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1886 &other_inode, &other_type);
1887 if (ret < 0 && ret != -ENOENT)
1888 goto out;
1889 if (ret) {
1890 ret = 0;
1891 goto out;
1892 }
1893
Alexander Block766702e2012-07-28 14:11:31 +02001894 /*
1895 * Check if the overwritten ref was already processed. If yes, the ref
1896 * was already unlinked/moved, so we can safely assume that we will not
1897 * overwrite anything at this point in time.
1898 */
Robbie Ko801bec32015-06-23 18:39:46 +08001899 if (other_inode > sctx->send_progress ||
1900 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001901 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001902 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001903 if (ret < 0)
1904 goto out;
1905
1906 ret = 1;
1907 *who_ino = other_inode;
1908 } else {
1909 ret = 0;
1910 }
1911
1912out:
1913 return ret;
1914}
1915
Alexander Block766702e2012-07-28 14:11:31 +02001916/*
1917 * Checks if the ref was overwritten by an already processed inode. This is
1918 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1919 * thus the orphan name needs be used.
1920 * process_recorded_refs also uses it to avoid unlinking of refs that were
1921 * overwritten.
1922 */
Alexander Block31db9f72012-07-25 23:19:24 +02001923static int did_overwrite_ref(struct send_ctx *sctx,
1924 u64 dir, u64 dir_gen,
1925 u64 ino, u64 ino_gen,
1926 const char *name, int name_len)
1927{
1928 int ret = 0;
1929 u64 gen;
1930 u64 ow_inode;
1931 u8 other_type;
1932
1933 if (!sctx->parent_root)
1934 goto out;
1935
1936 ret = is_inode_existent(sctx, dir, dir_gen);
1937 if (ret <= 0)
1938 goto out;
1939
1940 /* check if the ref was overwritten by another ref */
1941 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1942 &ow_inode, &other_type);
1943 if (ret < 0 && ret != -ENOENT)
1944 goto out;
1945 if (ret) {
1946 /* was never and will never be overwritten */
1947 ret = 0;
1948 goto out;
1949 }
1950
1951 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001952 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001953 if (ret < 0)
1954 goto out;
1955
1956 if (ow_inode == ino && gen == ino_gen) {
1957 ret = 0;
1958 goto out;
1959 }
1960
Filipe Manana8b191a62015-04-09 14:09:14 +01001961 /*
1962 * We know that it is or will be overwritten. Check this now.
1963 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001964 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1965 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001966 */
Filipe Mananab786f162015-09-26 15:30:23 +01001967 if ((ow_inode < sctx->send_progress) ||
1968 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1969 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001970 ret = 1;
1971 else
1972 ret = 0;
1973
1974out:
1975 return ret;
1976}
1977
Alexander Block766702e2012-07-28 14:11:31 +02001978/*
1979 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1980 * that got overwritten. This is used by process_recorded_refs to determine
1981 * if it has to use the path as returned by get_cur_path or the orphan name.
1982 */
Alexander Block31db9f72012-07-25 23:19:24 +02001983static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1984{
1985 int ret = 0;
1986 struct fs_path *name = NULL;
1987 u64 dir;
1988 u64 dir_gen;
1989
1990 if (!sctx->parent_root)
1991 goto out;
1992
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001993 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001994 if (!name)
1995 return -ENOMEM;
1996
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001997 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001998 if (ret < 0)
1999 goto out;
2000
2001 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2002 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02002003
2004out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002005 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002006 return ret;
2007}
2008
Alexander Block766702e2012-07-28 14:11:31 +02002009/*
2010 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2011 * so we need to do some special handling in case we have clashes. This function
2012 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002013 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002014 */
Alexander Block31db9f72012-07-25 23:19:24 +02002015static int name_cache_insert(struct send_ctx *sctx,
2016 struct name_cache_entry *nce)
2017{
2018 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002019 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002020
Alexander Block7e0926f2012-07-28 14:20:58 +02002021 nce_head = radix_tree_lookup(&sctx->name_cache,
2022 (unsigned long)nce->ino);
2023 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002024 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002025 if (!nce_head) {
2026 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002027 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002028 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002029 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002030
Alexander Block7e0926f2012-07-28 14:20:58 +02002031 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002032 if (ret < 0) {
2033 kfree(nce_head);
2034 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002035 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002036 }
Alexander Block31db9f72012-07-25 23:19:24 +02002037 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002038 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002039 list_add_tail(&nce->list, &sctx->name_cache_list);
2040 sctx->name_cache_size++;
2041
2042 return ret;
2043}
2044
2045static void name_cache_delete(struct send_ctx *sctx,
2046 struct name_cache_entry *nce)
2047{
Alexander Block7e0926f2012-07-28 14:20:58 +02002048 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002049
Alexander Block7e0926f2012-07-28 14:20:58 +02002050 nce_head = radix_tree_lookup(&sctx->name_cache,
2051 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002052 if (!nce_head) {
2053 btrfs_err(sctx->send_root->fs_info,
2054 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2055 nce->ino, sctx->name_cache_size);
2056 }
Alexander Block31db9f72012-07-25 23:19:24 +02002057
Alexander Block7e0926f2012-07-28 14:20:58 +02002058 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002059 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002060 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002061
David Sterba57fb8912014-02-03 19:24:40 +01002062 /*
2063 * We may not get to the final release of nce_head if the lookup fails
2064 */
2065 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002066 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2067 kfree(nce_head);
2068 }
Alexander Block31db9f72012-07-25 23:19:24 +02002069}
2070
2071static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2072 u64 ino, u64 gen)
2073{
Alexander Block7e0926f2012-07-28 14:20:58 +02002074 struct list_head *nce_head;
2075 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002076
Alexander Block7e0926f2012-07-28 14:20:58 +02002077 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2078 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002079 return NULL;
2080
Alexander Block7e0926f2012-07-28 14:20:58 +02002081 list_for_each_entry(cur, nce_head, radix_list) {
2082 if (cur->ino == ino && cur->gen == gen)
2083 return cur;
2084 }
Alexander Block31db9f72012-07-25 23:19:24 +02002085 return NULL;
2086}
2087
Alexander Block766702e2012-07-28 14:11:31 +02002088/*
2089 * Removes the entry from the list and adds it back to the end. This marks the
2090 * entry as recently used so that name_cache_clean_unused does not remove it.
2091 */
Alexander Block31db9f72012-07-25 23:19:24 +02002092static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2093{
2094 list_del(&nce->list);
2095 list_add_tail(&nce->list, &sctx->name_cache_list);
2096}
2097
Alexander Block766702e2012-07-28 14:11:31 +02002098/*
2099 * Remove some entries from the beginning of name_cache_list.
2100 */
Alexander Block31db9f72012-07-25 23:19:24 +02002101static void name_cache_clean_unused(struct send_ctx *sctx)
2102{
2103 struct name_cache_entry *nce;
2104
2105 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2106 return;
2107
2108 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2109 nce = list_entry(sctx->name_cache_list.next,
2110 struct name_cache_entry, list);
2111 name_cache_delete(sctx, nce);
2112 kfree(nce);
2113 }
2114}
2115
2116static void name_cache_free(struct send_ctx *sctx)
2117{
2118 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002119
Alexander Blocke938c8a2012-07-28 16:33:49 +02002120 while (!list_empty(&sctx->name_cache_list)) {
2121 nce = list_entry(sctx->name_cache_list.next,
2122 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002123 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002124 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002125 }
2126}
2127
Alexander Block766702e2012-07-28 14:11:31 +02002128/*
2129 * Used by get_cur_path for each ref up to the root.
2130 * Returns 0 if it succeeded.
2131 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2132 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2133 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2134 * Returns <0 in case of error.
2135 */
Alexander Block31db9f72012-07-25 23:19:24 +02002136static int __get_cur_name_and_parent(struct send_ctx *sctx,
2137 u64 ino, u64 gen,
2138 u64 *parent_ino,
2139 u64 *parent_gen,
2140 struct fs_path *dest)
2141{
2142 int ret;
2143 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002144 struct name_cache_entry *nce = NULL;
2145
Alexander Block766702e2012-07-28 14:11:31 +02002146 /*
2147 * First check if we already did a call to this function with the same
2148 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2149 * return the cached result.
2150 */
Alexander Block31db9f72012-07-25 23:19:24 +02002151 nce = name_cache_search(sctx, ino, gen);
2152 if (nce) {
2153 if (ino < sctx->send_progress && nce->need_later_update) {
2154 name_cache_delete(sctx, nce);
2155 kfree(nce);
2156 nce = NULL;
2157 } else {
2158 name_cache_used(sctx, nce);
2159 *parent_ino = nce->parent_ino;
2160 *parent_gen = nce->parent_gen;
2161 ret = fs_path_add(dest, nce->name, nce->name_len);
2162 if (ret < 0)
2163 goto out;
2164 ret = nce->ret;
2165 goto out;
2166 }
2167 }
2168
Alexander Block766702e2012-07-28 14:11:31 +02002169 /*
2170 * If the inode is not existent yet, add the orphan name and return 1.
2171 * This should only happen for the parent dir that we determine in
2172 * __record_new_ref
2173 */
Alexander Block31db9f72012-07-25 23:19:24 +02002174 ret = is_inode_existent(sctx, ino, gen);
2175 if (ret < 0)
2176 goto out;
2177
2178 if (!ret) {
2179 ret = gen_unique_name(sctx, ino, gen, dest);
2180 if (ret < 0)
2181 goto out;
2182 ret = 1;
2183 goto out_cache;
2184 }
2185
Alexander Block766702e2012-07-28 14:11:31 +02002186 /*
2187 * Depending on whether the inode was already processed or not, use
2188 * send_root or parent_root for ref lookup.
2189 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002190 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002191 ret = get_first_ref(sctx->send_root, ino,
2192 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002193 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002194 ret = get_first_ref(sctx->parent_root, ino,
2195 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002196 if (ret < 0)
2197 goto out;
2198
Alexander Block766702e2012-07-28 14:11:31 +02002199 /*
2200 * Check if the ref was overwritten by an inode's ref that was processed
2201 * earlier. If yes, treat as orphan and return 1.
2202 */
Alexander Block31db9f72012-07-25 23:19:24 +02002203 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2204 dest->start, dest->end - dest->start);
2205 if (ret < 0)
2206 goto out;
2207 if (ret) {
2208 fs_path_reset(dest);
2209 ret = gen_unique_name(sctx, ino, gen, dest);
2210 if (ret < 0)
2211 goto out;
2212 ret = 1;
2213 }
2214
2215out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002216 /*
2217 * Store the result of the lookup in the name cache.
2218 */
David Sterbae780b0d2016-01-18 18:42:13 +01002219 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002220 if (!nce) {
2221 ret = -ENOMEM;
2222 goto out;
2223 }
2224
2225 nce->ino = ino;
2226 nce->gen = gen;
2227 nce->parent_ino = *parent_ino;
2228 nce->parent_gen = *parent_gen;
2229 nce->name_len = fs_path_len(dest);
2230 nce->ret = ret;
2231 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002232
2233 if (ino < sctx->send_progress)
2234 nce->need_later_update = 0;
2235 else
2236 nce->need_later_update = 1;
2237
2238 nce_ret = name_cache_insert(sctx, nce);
2239 if (nce_ret < 0)
2240 ret = nce_ret;
2241 name_cache_clean_unused(sctx);
2242
2243out:
Alexander Block31db9f72012-07-25 23:19:24 +02002244 return ret;
2245}
2246
2247/*
2248 * Magic happens here. This function returns the first ref to an inode as it
2249 * would look like while receiving the stream at this point in time.
2250 * We walk the path up to the root. For every inode in between, we check if it
2251 * was already processed/sent. If yes, we continue with the parent as found
2252 * in send_root. If not, we continue with the parent as found in parent_root.
2253 * If we encounter an inode that was deleted at this point in time, we use the
2254 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2255 * that were not created yet and overwritten inodes/refs.
2256 *
2257 * When do we have have orphan inodes:
2258 * 1. When an inode is freshly created and thus no valid refs are available yet
2259 * 2. When a directory lost all it's refs (deleted) but still has dir items
2260 * inside which were not processed yet (pending for move/delete). If anyone
2261 * tried to get the path to the dir items, it would get a path inside that
2262 * orphan directory.
2263 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2264 * of an unprocessed inode. If in that case the first ref would be
2265 * overwritten, the overwritten inode gets "orphanized". Later when we
2266 * process this overwritten inode, it is restored at a new place by moving
2267 * the orphan inode.
2268 *
2269 * sctx->send_progress tells this function at which point in time receiving
2270 * would be.
2271 */
2272static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2273 struct fs_path *dest)
2274{
2275 int ret = 0;
2276 struct fs_path *name = NULL;
2277 u64 parent_inode = 0;
2278 u64 parent_gen = 0;
2279 int stop = 0;
2280
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002281 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002282 if (!name) {
2283 ret = -ENOMEM;
2284 goto out;
2285 }
2286
2287 dest->reversed = 1;
2288 fs_path_reset(dest);
2289
2290 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002291 struct waiting_dir_move *wdm;
2292
Alexander Block31db9f72012-07-25 23:19:24 +02002293 fs_path_reset(name);
2294
Filipe Manana9dc44212014-02-19 14:31:44 +00002295 if (is_waiting_for_rm(sctx, ino)) {
2296 ret = gen_unique_name(sctx, ino, gen, name);
2297 if (ret < 0)
2298 goto out;
2299 ret = fs_path_add_path(dest, name);
2300 break;
2301 }
2302
Filipe Manana8b191a62015-04-09 14:09:14 +01002303 wdm = get_waiting_dir_move(sctx, ino);
2304 if (wdm && wdm->orphanized) {
2305 ret = gen_unique_name(sctx, ino, gen, name);
2306 stop = 1;
2307 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002308 ret = get_first_ref(sctx->parent_root, ino,
2309 &parent_inode, &parent_gen, name);
2310 } else {
2311 ret = __get_cur_name_and_parent(sctx, ino, gen,
2312 &parent_inode,
2313 &parent_gen, name);
2314 if (ret)
2315 stop = 1;
2316 }
2317
Alexander Block31db9f72012-07-25 23:19:24 +02002318 if (ret < 0)
2319 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002320
Alexander Block31db9f72012-07-25 23:19:24 +02002321 ret = fs_path_add_path(dest, name);
2322 if (ret < 0)
2323 goto out;
2324
2325 ino = parent_inode;
2326 gen = parent_gen;
2327 }
2328
2329out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002330 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002331 if (!ret)
2332 fs_path_unreverse(dest);
2333 return ret;
2334}
2335
2336/*
Alexander Block31db9f72012-07-25 23:19:24 +02002337 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2338 */
2339static int send_subvol_begin(struct send_ctx *sctx)
2340{
2341 int ret;
2342 struct btrfs_root *send_root = sctx->send_root;
2343 struct btrfs_root *parent_root = sctx->parent_root;
2344 struct btrfs_path *path;
2345 struct btrfs_key key;
2346 struct btrfs_root_ref *ref;
2347 struct extent_buffer *leaf;
2348 char *name = NULL;
2349 int namelen;
2350
Wang Shilongffcfaf82014-01-15 00:26:43 +08002351 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002352 if (!path)
2353 return -ENOMEM;
2354
David Sterbae780b0d2016-01-18 18:42:13 +01002355 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002356 if (!name) {
2357 btrfs_free_path(path);
2358 return -ENOMEM;
2359 }
2360
2361 key.objectid = send_root->objectid;
2362 key.type = BTRFS_ROOT_BACKREF_KEY;
2363 key.offset = 0;
2364
2365 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2366 &key, path, 1, 0);
2367 if (ret < 0)
2368 goto out;
2369 if (ret) {
2370 ret = -ENOENT;
2371 goto out;
2372 }
2373
2374 leaf = path->nodes[0];
2375 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2376 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2377 key.objectid != send_root->objectid) {
2378 ret = -ENOENT;
2379 goto out;
2380 }
2381 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2382 namelen = btrfs_root_ref_name_len(leaf, ref);
2383 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2384 btrfs_release_path(path);
2385
Alexander Block31db9f72012-07-25 23:19:24 +02002386 if (parent_root) {
2387 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2388 if (ret < 0)
2389 goto out;
2390 } else {
2391 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2392 if (ret < 0)
2393 goto out;
2394 }
2395
2396 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002397
2398 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2399 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2400 sctx->send_root->root_item.received_uuid);
2401 else
2402 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2403 sctx->send_root->root_item.uuid);
2404
Alexander Block31db9f72012-07-25 23:19:24 +02002405 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002406 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002407 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002408 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2409 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2410 parent_root->root_item.received_uuid);
2411 else
2412 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2413 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002414 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002415 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002416 }
2417
2418 ret = send_cmd(sctx);
2419
2420tlv_put_failure:
2421out:
2422 btrfs_free_path(path);
2423 kfree(name);
2424 return ret;
2425}
2426
2427static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2428{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002429 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002430 int ret = 0;
2431 struct fs_path *p;
2432
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002433 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002434
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002435 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002436 if (!p)
2437 return -ENOMEM;
2438
2439 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2440 if (ret < 0)
2441 goto out;
2442
2443 ret = get_cur_path(sctx, ino, gen, p);
2444 if (ret < 0)
2445 goto out;
2446 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2447 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2448
2449 ret = send_cmd(sctx);
2450
2451tlv_put_failure:
2452out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002453 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002454 return ret;
2455}
2456
2457static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2458{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002459 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002460 int ret = 0;
2461 struct fs_path *p;
2462
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002463 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002464
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002465 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002466 if (!p)
2467 return -ENOMEM;
2468
2469 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2470 if (ret < 0)
2471 goto out;
2472
2473 ret = get_cur_path(sctx, ino, gen, p);
2474 if (ret < 0)
2475 goto out;
2476 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2477 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2478
2479 ret = send_cmd(sctx);
2480
2481tlv_put_failure:
2482out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002483 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002484 return ret;
2485}
2486
2487static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2488{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002489 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002490 int ret = 0;
2491 struct fs_path *p;
2492
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002493 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2494 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002495
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002496 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002497 if (!p)
2498 return -ENOMEM;
2499
2500 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2501 if (ret < 0)
2502 goto out;
2503
2504 ret = get_cur_path(sctx, ino, gen, p);
2505 if (ret < 0)
2506 goto out;
2507 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2508 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2509 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2510
2511 ret = send_cmd(sctx);
2512
2513tlv_put_failure:
2514out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002515 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002516 return ret;
2517}
2518
2519static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2520{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002521 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002522 int ret = 0;
2523 struct fs_path *p = NULL;
2524 struct btrfs_inode_item *ii;
2525 struct btrfs_path *path = NULL;
2526 struct extent_buffer *eb;
2527 struct btrfs_key key;
2528 int slot;
2529
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002530 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002531
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002532 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002533 if (!p)
2534 return -ENOMEM;
2535
2536 path = alloc_path_for_send();
2537 if (!path) {
2538 ret = -ENOMEM;
2539 goto out;
2540 }
2541
2542 key.objectid = ino;
2543 key.type = BTRFS_INODE_ITEM_KEY;
2544 key.offset = 0;
2545 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002546 if (ret > 0)
2547 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002548 if (ret < 0)
2549 goto out;
2550
2551 eb = path->nodes[0];
2552 slot = path->slots[0];
2553 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2554
2555 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2556 if (ret < 0)
2557 goto out;
2558
2559 ret = get_cur_path(sctx, ino, gen, p);
2560 if (ret < 0)
2561 goto out;
2562 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002563 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2564 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2565 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002566 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002567
2568 ret = send_cmd(sctx);
2569
2570tlv_put_failure:
2571out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002572 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002573 btrfs_free_path(path);
2574 return ret;
2575}
2576
2577/*
2578 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2579 * a valid path yet because we did not process the refs yet. So, the inode
2580 * is created as orphan.
2581 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002582static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002583{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002584 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002585 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002586 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002587 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002588 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002589 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002590 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002591
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002592 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002593
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002594 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002595 if (!p)
2596 return -ENOMEM;
2597
Liu Bo644d1942014-02-27 17:29:01 +08002598 if (ino != sctx->cur_ino) {
2599 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2600 NULL, NULL, &rdev);
2601 if (ret < 0)
2602 goto out;
2603 } else {
2604 gen = sctx->cur_inode_gen;
2605 mode = sctx->cur_inode_mode;
2606 rdev = sctx->cur_inode_rdev;
2607 }
Alexander Block31db9f72012-07-25 23:19:24 +02002608
Alexander Blocke938c8a2012-07-28 16:33:49 +02002609 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002610 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002611 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002612 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002613 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002614 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002615 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002616 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002617 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002618 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002619 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002620 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002621 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002622 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002623 (int)(mode & S_IFMT));
2624 ret = -ENOTSUPP;
2625 goto out;
2626 }
2627
2628 ret = begin_cmd(sctx, cmd);
2629 if (ret < 0)
2630 goto out;
2631
Alexander Block1f4692d2012-07-28 10:42:24 +02002632 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002633 if (ret < 0)
2634 goto out;
2635
2636 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002637 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002638
2639 if (S_ISLNK(mode)) {
2640 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002641 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002642 if (ret < 0)
2643 goto out;
2644 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2645 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2646 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002647 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2648 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002649 }
2650
2651 ret = send_cmd(sctx);
2652 if (ret < 0)
2653 goto out;
2654
2655
2656tlv_put_failure:
2657out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002658 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002659 return ret;
2660}
2661
Alexander Block1f4692d2012-07-28 10:42:24 +02002662/*
2663 * We need some special handling for inodes that get processed before the parent
2664 * directory got created. See process_recorded_refs for details.
2665 * This function does the check if we already created the dir out of order.
2666 */
2667static int did_create_dir(struct send_ctx *sctx, u64 dir)
2668{
2669 int ret = 0;
2670 struct btrfs_path *path = NULL;
2671 struct btrfs_key key;
2672 struct btrfs_key found_key;
2673 struct btrfs_key di_key;
2674 struct extent_buffer *eb;
2675 struct btrfs_dir_item *di;
2676 int slot;
2677
2678 path = alloc_path_for_send();
2679 if (!path) {
2680 ret = -ENOMEM;
2681 goto out;
2682 }
2683
2684 key.objectid = dir;
2685 key.type = BTRFS_DIR_INDEX_KEY;
2686 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002687 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2688 if (ret < 0)
2689 goto out;
2690
Alexander Block1f4692d2012-07-28 10:42:24 +02002691 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002692 eb = path->nodes[0];
2693 slot = path->slots[0];
2694 if (slot >= btrfs_header_nritems(eb)) {
2695 ret = btrfs_next_leaf(sctx->send_root, path);
2696 if (ret < 0) {
2697 goto out;
2698 } else if (ret > 0) {
2699 ret = 0;
2700 break;
2701 }
2702 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002703 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002704
2705 btrfs_item_key_to_cpu(eb, &found_key, slot);
2706 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002707 found_key.type != key.type) {
2708 ret = 0;
2709 goto out;
2710 }
2711
2712 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2713 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2714
Josef Bacika0525412013-08-12 10:56:14 -04002715 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2716 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002717 ret = 1;
2718 goto out;
2719 }
2720
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002721 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002722 }
2723
2724out:
2725 btrfs_free_path(path);
2726 return ret;
2727}
2728
2729/*
2730 * Only creates the inode if it is:
2731 * 1. Not a directory
2732 * 2. Or a directory which was not created already due to out of order
2733 * directories. See did_create_dir and process_recorded_refs for details.
2734 */
2735static int send_create_inode_if_needed(struct send_ctx *sctx)
2736{
2737 int ret;
2738
2739 if (S_ISDIR(sctx->cur_inode_mode)) {
2740 ret = did_create_dir(sctx, sctx->cur_ino);
2741 if (ret < 0)
2742 goto out;
2743 if (ret) {
2744 ret = 0;
2745 goto out;
2746 }
2747 }
2748
2749 ret = send_create_inode(sctx, sctx->cur_ino);
2750 if (ret < 0)
2751 goto out;
2752
2753out:
2754 return ret;
2755}
2756
Alexander Block31db9f72012-07-25 23:19:24 +02002757struct recorded_ref {
2758 struct list_head list;
2759 char *dir_path;
2760 char *name;
2761 struct fs_path *full_path;
2762 u64 dir;
2763 u64 dir_gen;
2764 int dir_path_len;
2765 int name_len;
2766};
2767
2768/*
2769 * We need to process new refs before deleted refs, but compare_tree gives us
2770 * everything mixed. So we first record all refs and later process them.
2771 * This function is a helper to record one ref.
2772 */
Liu Boa4d96d62014-03-03 21:31:03 +08002773static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002774 u64 dir_gen, struct fs_path *path)
2775{
2776 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002777
David Sterbae780b0d2016-01-18 18:42:13 +01002778 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002779 if (!ref)
2780 return -ENOMEM;
2781
2782 ref->dir = dir;
2783 ref->dir_gen = dir_gen;
2784 ref->full_path = path;
2785
Andy Shevchenkoed848852013-08-21 10:32:13 +03002786 ref->name = (char *)kbasename(ref->full_path->start);
2787 ref->name_len = ref->full_path->end - ref->name;
2788 ref->dir_path = ref->full_path->start;
2789 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002790 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002791 else
Alexander Block31db9f72012-07-25 23:19:24 +02002792 ref->dir_path_len = ref->full_path->end -
2793 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002794
2795 list_add_tail(&ref->list, head);
2796 return 0;
2797}
2798
Josef Bacikba5e8f22013-08-16 16:52:55 -04002799static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2800{
2801 struct recorded_ref *new;
2802
David Sterbae780b0d2016-01-18 18:42:13 +01002803 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002804 if (!new)
2805 return -ENOMEM;
2806
2807 new->dir = ref->dir;
2808 new->dir_gen = ref->dir_gen;
2809 new->full_path = NULL;
2810 INIT_LIST_HEAD(&new->list);
2811 list_add_tail(&new->list, list);
2812 return 0;
2813}
2814
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002815static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002816{
2817 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002818
Alexander Blocke938c8a2012-07-28 16:33:49 +02002819 while (!list_empty(head)) {
2820 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002821 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002822 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002823 kfree(cur);
2824 }
Alexander Block31db9f72012-07-25 23:19:24 +02002825}
2826
2827static void free_recorded_refs(struct send_ctx *sctx)
2828{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002829 __free_recorded_refs(&sctx->new_refs);
2830 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002831}
2832
2833/*
Alexander Block766702e2012-07-28 14:11:31 +02002834 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002835 * ref of an unprocessed inode gets overwritten and for all non empty
2836 * directories.
2837 */
2838static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2839 struct fs_path *path)
2840{
2841 int ret;
2842 struct fs_path *orphan;
2843
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002844 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002845 if (!orphan)
2846 return -ENOMEM;
2847
2848 ret = gen_unique_name(sctx, ino, gen, orphan);
2849 if (ret < 0)
2850 goto out;
2851
2852 ret = send_rename(sctx, path, orphan);
2853
2854out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002855 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002856 return ret;
2857}
2858
Filipe Manana9dc44212014-02-19 14:31:44 +00002859static struct orphan_dir_info *
2860add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2861{
2862 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2863 struct rb_node *parent = NULL;
2864 struct orphan_dir_info *entry, *odi;
2865
David Sterbae780b0d2016-01-18 18:42:13 +01002866 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
Filipe Manana9dc44212014-02-19 14:31:44 +00002867 if (!odi)
2868 return ERR_PTR(-ENOMEM);
2869 odi->ino = dir_ino;
2870 odi->gen = 0;
2871
2872 while (*p) {
2873 parent = *p;
2874 entry = rb_entry(parent, struct orphan_dir_info, node);
2875 if (dir_ino < entry->ino) {
2876 p = &(*p)->rb_left;
2877 } else if (dir_ino > entry->ino) {
2878 p = &(*p)->rb_right;
2879 } else {
2880 kfree(odi);
2881 return entry;
2882 }
2883 }
2884
2885 rb_link_node(&odi->node, parent, p);
2886 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2887 return odi;
2888}
2889
2890static struct orphan_dir_info *
2891get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2892{
2893 struct rb_node *n = sctx->orphan_dirs.rb_node;
2894 struct orphan_dir_info *entry;
2895
2896 while (n) {
2897 entry = rb_entry(n, struct orphan_dir_info, node);
2898 if (dir_ino < entry->ino)
2899 n = n->rb_left;
2900 else if (dir_ino > entry->ino)
2901 n = n->rb_right;
2902 else
2903 return entry;
2904 }
2905 return NULL;
2906}
2907
2908static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2909{
2910 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2911
2912 return odi != NULL;
2913}
2914
2915static void free_orphan_dir_info(struct send_ctx *sctx,
2916 struct orphan_dir_info *odi)
2917{
2918 if (!odi)
2919 return;
2920 rb_erase(&odi->node, &sctx->orphan_dirs);
2921 kfree(odi);
2922}
2923
Alexander Block31db9f72012-07-25 23:19:24 +02002924/*
2925 * Returns 1 if a directory can be removed at this point in time.
2926 * We check this by iterating all dir items and checking if the inode behind
2927 * the dir item was already processed.
2928 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002929static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2930 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002931{
2932 int ret = 0;
2933 struct btrfs_root *root = sctx->parent_root;
2934 struct btrfs_path *path;
2935 struct btrfs_key key;
2936 struct btrfs_key found_key;
2937 struct btrfs_key loc;
2938 struct btrfs_dir_item *di;
2939
Alexander Block6d85ed052012-08-01 14:48:59 +02002940 /*
2941 * Don't try to rmdir the top/root subvolume dir.
2942 */
2943 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2944 return 0;
2945
Alexander Block31db9f72012-07-25 23:19:24 +02002946 path = alloc_path_for_send();
2947 if (!path)
2948 return -ENOMEM;
2949
2950 key.objectid = dir;
2951 key.type = BTRFS_DIR_INDEX_KEY;
2952 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002953 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2954 if (ret < 0)
2955 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002956
2957 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002958 struct waiting_dir_move *dm;
2959
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002960 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2961 ret = btrfs_next_leaf(root, path);
2962 if (ret < 0)
2963 goto out;
2964 else if (ret > 0)
2965 break;
2966 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002967 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002968 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2969 path->slots[0]);
2970 if (found_key.objectid != key.objectid ||
2971 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002972 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002973
2974 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2975 struct btrfs_dir_item);
2976 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2977
Filipe Manana9dc44212014-02-19 14:31:44 +00002978 dm = get_waiting_dir_move(sctx, loc.objectid);
2979 if (dm) {
2980 struct orphan_dir_info *odi;
2981
2982 odi = add_orphan_dir_info(sctx, dir);
2983 if (IS_ERR(odi)) {
2984 ret = PTR_ERR(odi);
2985 goto out;
2986 }
2987 odi->gen = dir_gen;
2988 dm->rmdir_ino = dir;
2989 ret = 0;
2990 goto out;
2991 }
2992
Alexander Block31db9f72012-07-25 23:19:24 +02002993 if (loc.objectid > send_progress) {
Robbie Ko443f9d22015-06-22 17:08:45 +08002994 struct orphan_dir_info *odi;
2995
2996 odi = get_orphan_dir_info(sctx, dir);
2997 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02002998 ret = 0;
2999 goto out;
3000 }
3001
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003002 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003003 }
3004
3005 ret = 1;
3006
3007out:
3008 btrfs_free_path(path);
3009 return ret;
3010}
3011
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003012static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3013{
Filipe Manana9dc44212014-02-19 14:31:44 +00003014 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003015
Filipe Manana9dc44212014-02-19 14:31:44 +00003016 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003017}
3018
Filipe Manana8b191a62015-04-09 14:09:14 +01003019static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003020{
3021 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3022 struct rb_node *parent = NULL;
3023 struct waiting_dir_move *entry, *dm;
3024
David Sterbae780b0d2016-01-18 18:42:13 +01003025 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003026 if (!dm)
3027 return -ENOMEM;
3028 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003029 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003030 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003031
3032 while (*p) {
3033 parent = *p;
3034 entry = rb_entry(parent, struct waiting_dir_move, node);
3035 if (ino < entry->ino) {
3036 p = &(*p)->rb_left;
3037 } else if (ino > entry->ino) {
3038 p = &(*p)->rb_right;
3039 } else {
3040 kfree(dm);
3041 return -EEXIST;
3042 }
3043 }
3044
3045 rb_link_node(&dm->node, parent, p);
3046 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3047 return 0;
3048}
3049
Filipe Manana9dc44212014-02-19 14:31:44 +00003050static struct waiting_dir_move *
3051get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003052{
3053 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3054 struct waiting_dir_move *entry;
3055
3056 while (n) {
3057 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003058 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003059 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003060 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003061 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003062 else
3063 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003064 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003065 return NULL;
3066}
3067
3068static void free_waiting_dir_move(struct send_ctx *sctx,
3069 struct waiting_dir_move *dm)
3070{
3071 if (!dm)
3072 return;
3073 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3074 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003075}
3076
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003077static int add_pending_dir_move(struct send_ctx *sctx,
3078 u64 ino,
3079 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003080 u64 parent_ino,
3081 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003082 struct list_head *deleted_refs,
3083 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003084{
3085 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3086 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003087 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003088 struct recorded_ref *cur;
3089 int exists = 0;
3090 int ret;
3091
David Sterbae780b0d2016-01-18 18:42:13 +01003092 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003093 if (!pm)
3094 return -ENOMEM;
3095 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003096 pm->ino = ino;
3097 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003098 INIT_LIST_HEAD(&pm->list);
3099 INIT_LIST_HEAD(&pm->update_refs);
3100 RB_CLEAR_NODE(&pm->node);
3101
3102 while (*p) {
3103 parent = *p;
3104 entry = rb_entry(parent, struct pending_dir_move, node);
3105 if (parent_ino < entry->parent_ino) {
3106 p = &(*p)->rb_left;
3107 } else if (parent_ino > entry->parent_ino) {
3108 p = &(*p)->rb_right;
3109 } else {
3110 exists = 1;
3111 break;
3112 }
3113 }
3114
Filipe Mananaf9594922014-03-27 20:14:01 +00003115 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003116 ret = dup_ref(cur, &pm->update_refs);
3117 if (ret < 0)
3118 goto out;
3119 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003120 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003121 ret = dup_ref(cur, &pm->update_refs);
3122 if (ret < 0)
3123 goto out;
3124 }
3125
Filipe Manana8b191a62015-04-09 14:09:14 +01003126 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003127 if (ret)
3128 goto out;
3129
3130 if (exists) {
3131 list_add_tail(&pm->list, &entry->list);
3132 } else {
3133 rb_link_node(&pm->node, parent, p);
3134 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3135 }
3136 ret = 0;
3137out:
3138 if (ret) {
3139 __free_recorded_refs(&pm->update_refs);
3140 kfree(pm);
3141 }
3142 return ret;
3143}
3144
3145static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3146 u64 parent_ino)
3147{
3148 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3149 struct pending_dir_move *entry;
3150
3151 while (n) {
3152 entry = rb_entry(n, struct pending_dir_move, node);
3153 if (parent_ino < entry->parent_ino)
3154 n = n->rb_left;
3155 else if (parent_ino > entry->parent_ino)
3156 n = n->rb_right;
3157 else
3158 return entry;
3159 }
3160 return NULL;
3161}
3162
Robbie Ko801bec32015-06-23 18:39:46 +08003163static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3164 u64 ino, u64 gen, u64 *ancestor_ino)
3165{
3166 int ret = 0;
3167 u64 parent_inode = 0;
3168 u64 parent_gen = 0;
3169 u64 start_ino = ino;
3170
3171 *ancestor_ino = 0;
3172 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3173 fs_path_reset(name);
3174
3175 if (is_waiting_for_rm(sctx, ino))
3176 break;
3177 if (is_waiting_for_move(sctx, ino)) {
3178 if (*ancestor_ino == 0)
3179 *ancestor_ino = ino;
3180 ret = get_first_ref(sctx->parent_root, ino,
3181 &parent_inode, &parent_gen, name);
3182 } else {
3183 ret = __get_cur_name_and_parent(sctx, ino, gen,
3184 &parent_inode,
3185 &parent_gen, name);
3186 if (ret > 0) {
3187 ret = 0;
3188 break;
3189 }
3190 }
3191 if (ret < 0)
3192 break;
3193 if (parent_inode == start_ino) {
3194 ret = 1;
3195 if (*ancestor_ino == 0)
3196 *ancestor_ino = ino;
3197 break;
3198 }
3199 ino = parent_inode;
3200 gen = parent_gen;
3201 }
3202 return ret;
3203}
3204
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003205static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3206{
3207 struct fs_path *from_path = NULL;
3208 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003209 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003210 u64 orig_progress = sctx->send_progress;
3211 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003212 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003213 struct waiting_dir_move *dm = NULL;
3214 u64 rmdir_ino = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003215 u64 ancestor;
3216 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003217 int ret;
3218
Filipe Manana2b863a12014-02-16 13:43:11 +00003219 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003220 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003221 if (!name || !from_path) {
3222 ret = -ENOMEM;
3223 goto out;
3224 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003225
Filipe Manana9dc44212014-02-19 14:31:44 +00003226 dm = get_waiting_dir_move(sctx, pm->ino);
3227 ASSERT(dm);
3228 rmdir_ino = dm->rmdir_ino;
Robbie Ko801bec32015-06-23 18:39:46 +08003229 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003230 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003231
Robbie Ko801bec32015-06-23 18:39:46 +08003232 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003233 ret = gen_unique_name(sctx, pm->ino,
3234 pm->gen, from_path);
3235 } else {
3236 ret = get_first_ref(sctx->parent_root, pm->ino,
3237 &parent_ino, &parent_gen, name);
3238 if (ret < 0)
3239 goto out;
3240 ret = get_cur_path(sctx, parent_ino, parent_gen,
3241 from_path);
3242 if (ret < 0)
3243 goto out;
3244 ret = fs_path_add_path(from_path, name);
3245 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003246 if (ret < 0)
3247 goto out;
3248
Filipe Mananaf9594922014-03-27 20:14:01 +00003249 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003250 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003251 if (ret < 0)
3252 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003253 if (ret) {
3254 LIST_HEAD(deleted_refs);
3255 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3256 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3257 &pm->update_refs, &deleted_refs,
3258 is_orphan);
3259 if (ret < 0)
3260 goto out;
3261 if (rmdir_ino) {
3262 dm = get_waiting_dir_move(sctx, pm->ino);
3263 ASSERT(dm);
3264 dm->rmdir_ino = rmdir_ino;
3265 }
3266 goto out;
3267 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003268 fs_path_reset(name);
3269 to_path = name;
3270 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003271 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3272 if (ret < 0)
3273 goto out;
3274
3275 ret = send_rename(sctx, from_path, to_path);
3276 if (ret < 0)
3277 goto out;
3278
Filipe Manana9dc44212014-02-19 14:31:44 +00003279 if (rmdir_ino) {
3280 struct orphan_dir_info *odi;
3281
3282 odi = get_orphan_dir_info(sctx, rmdir_ino);
3283 if (!odi) {
3284 /* already deleted */
3285 goto finish;
3286 }
Robbie Ko99ea42d2015-06-23 18:39:49 +08003287 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003288 if (ret < 0)
3289 goto out;
3290 if (!ret)
3291 goto finish;
3292
3293 name = fs_path_alloc();
3294 if (!name) {
3295 ret = -ENOMEM;
3296 goto out;
3297 }
3298 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3299 if (ret < 0)
3300 goto out;
3301 ret = send_rmdir(sctx, name);
3302 if (ret < 0)
3303 goto out;
3304 free_orphan_dir_info(sctx, odi);
3305 }
3306
3307finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003308 ret = send_utimes(sctx, pm->ino, pm->gen);
3309 if (ret < 0)
3310 goto out;
3311
3312 /*
3313 * After rename/move, need to update the utimes of both new parent(s)
3314 * and old parent(s).
3315 */
3316 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003317 /*
3318 * The parent inode might have been deleted in the send snapshot
3319 */
3320 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3321 NULL, NULL, NULL, NULL, NULL);
3322 if (ret == -ENOENT) {
3323 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003324 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003325 }
3326 if (ret < 0)
3327 goto out;
3328
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003329 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3330 if (ret < 0)
3331 goto out;
3332 }
3333
3334out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003335 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003336 fs_path_free(from_path);
3337 fs_path_free(to_path);
3338 sctx->send_progress = orig_progress;
3339
3340 return ret;
3341}
3342
3343static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3344{
3345 if (!list_empty(&m->list))
3346 list_del(&m->list);
3347 if (!RB_EMPTY_NODE(&m->node))
3348 rb_erase(&m->node, &sctx->pending_dir_moves);
3349 __free_recorded_refs(&m->update_refs);
3350 kfree(m);
3351}
3352
3353static void tail_append_pending_moves(struct pending_dir_move *moves,
3354 struct list_head *stack)
3355{
3356 if (list_empty(&moves->list)) {
3357 list_add_tail(&moves->list, stack);
3358 } else {
3359 LIST_HEAD(list);
3360 list_splice_init(&moves->list, &list);
3361 list_add_tail(&moves->list, stack);
3362 list_splice_tail(&list, stack);
3363 }
3364}
3365
3366static int apply_children_dir_moves(struct send_ctx *sctx)
3367{
3368 struct pending_dir_move *pm;
3369 struct list_head stack;
3370 u64 parent_ino = sctx->cur_ino;
3371 int ret = 0;
3372
3373 pm = get_pending_dir_moves(sctx, parent_ino);
3374 if (!pm)
3375 return 0;
3376
3377 INIT_LIST_HEAD(&stack);
3378 tail_append_pending_moves(pm, &stack);
3379
3380 while (!list_empty(&stack)) {
3381 pm = list_first_entry(&stack, struct pending_dir_move, list);
3382 parent_ino = pm->ino;
3383 ret = apply_dir_move(sctx, pm);
3384 free_pending_move(sctx, pm);
3385 if (ret)
3386 goto out;
3387 pm = get_pending_dir_moves(sctx, parent_ino);
3388 if (pm)
3389 tail_append_pending_moves(pm, &stack);
3390 }
3391 return 0;
3392
3393out:
3394 while (!list_empty(&stack)) {
3395 pm = list_first_entry(&stack, struct pending_dir_move, list);
3396 free_pending_move(sctx, pm);
3397 }
3398 return ret;
3399}
3400
Filipe Manana84471e22015-02-28 22:29:22 +00003401/*
3402 * We might need to delay a directory rename even when no ancestor directory
3403 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3404 * renamed. This happens when we rename a directory to the old name (the name
3405 * in the parent root) of some other unrelated directory that got its rename
3406 * delayed due to some ancestor with higher number that got renamed.
3407 *
3408 * Example:
3409 *
3410 * Parent snapshot:
3411 * . (ino 256)
3412 * |---- a/ (ino 257)
3413 * | |---- file (ino 260)
3414 * |
3415 * |---- b/ (ino 258)
3416 * |---- c/ (ino 259)
3417 *
3418 * Send snapshot:
3419 * . (ino 256)
3420 * |---- a/ (ino 258)
3421 * |---- x/ (ino 259)
3422 * |---- y/ (ino 257)
3423 * |----- file (ino 260)
3424 *
3425 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3426 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3427 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3428 * must issue is:
3429 *
3430 * 1 - rename 259 from 'c' to 'x'
3431 * 2 - rename 257 from 'a' to 'x/y'
3432 * 3 - rename 258 from 'b' to 'a'
3433 *
3434 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3435 * be done right away and < 0 on error.
3436 */
3437static int wait_for_dest_dir_move(struct send_ctx *sctx,
3438 struct recorded_ref *parent_ref,
3439 const bool is_orphan)
3440{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003441 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003442 struct btrfs_path *path;
3443 struct btrfs_key key;
3444 struct btrfs_key di_key;
3445 struct btrfs_dir_item *di;
3446 u64 left_gen;
3447 u64 right_gen;
3448 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003449 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003450
3451 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3452 return 0;
3453
3454 path = alloc_path_for_send();
3455 if (!path)
3456 return -ENOMEM;
3457
3458 key.objectid = parent_ref->dir;
3459 key.type = BTRFS_DIR_ITEM_KEY;
3460 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3461
3462 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3463 if (ret < 0) {
3464 goto out;
3465 } else if (ret > 0) {
3466 ret = 0;
3467 goto out;
3468 }
3469
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003470 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3471 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003472 if (!di) {
3473 ret = 0;
3474 goto out;
3475 }
3476 /*
3477 * di_key.objectid has the number of the inode that has a dentry in the
3478 * parent directory with the same name that sctx->cur_ino is being
3479 * renamed to. We need to check if that inode is in the send root as
3480 * well and if it is currently marked as an inode with a pending rename,
3481 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3482 * that it happens after that other inode is renamed.
3483 */
3484 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3485 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3486 ret = 0;
3487 goto out;
3488 }
3489
3490 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3491 &left_gen, NULL, NULL, NULL, NULL);
3492 if (ret < 0)
3493 goto out;
3494 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3495 &right_gen, NULL, NULL, NULL, NULL);
3496 if (ret < 0) {
3497 if (ret == -ENOENT)
3498 ret = 0;
3499 goto out;
3500 }
3501
3502 /* Different inode, no need to delay the rename of sctx->cur_ino */
3503 if (right_gen != left_gen) {
3504 ret = 0;
3505 goto out;
3506 }
3507
Robbie Ko801bec32015-06-23 18:39:46 +08003508 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3509 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003510 ret = add_pending_dir_move(sctx,
3511 sctx->cur_ino,
3512 sctx->cur_inode_gen,
3513 di_key.objectid,
3514 &sctx->new_refs,
3515 &sctx->deleted_refs,
3516 is_orphan);
3517 if (!ret)
3518 ret = 1;
3519 }
3520out:
3521 btrfs_free_path(path);
3522 return ret;
3523}
3524
Filipe Manana80aa6022015-03-27 17:50:45 +00003525/*
3526 * Check if ino ino1 is an ancestor of inode ino2 in the given root.
3527 * Return 1 if true, 0 if false and < 0 on error.
3528 */
3529static int is_ancestor(struct btrfs_root *root,
3530 const u64 ino1,
3531 const u64 ino1_gen,
3532 const u64 ino2,
3533 struct fs_path *fs_path)
3534{
3535 u64 ino = ino2;
3536
3537 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3538 int ret;
3539 u64 parent;
3540 u64 parent_gen;
3541
3542 fs_path_reset(fs_path);
3543 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3544 if (ret < 0) {
3545 if (ret == -ENOENT && ino == ino2)
3546 ret = 0;
3547 return ret;
3548 }
3549 if (parent == ino1)
3550 return parent_gen == ino1_gen ? 1 : 0;
3551 ino = parent;
3552 }
3553 return 0;
3554}
3555
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003556static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003557 struct recorded_ref *parent_ref,
3558 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003559{
Filipe Mananaf9594922014-03-27 20:14:01 +00003560 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003561 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003562 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003563 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003564 struct fs_path *path_before = NULL;
3565 struct fs_path *path_after = NULL;
3566 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003567
3568 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003569 path_before = fs_path_alloc();
3570 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003571 ret = -ENOMEM;
3572 goto out;
3573 }
3574
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003575 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003576 * Our current directory inode may not yet be renamed/moved because some
3577 * ancestor (immediate or not) has to be renamed/moved first. So find if
3578 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003579 * that ancestor is processed to avoid path build infinite loops (done
3580 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003581 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003582 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003583 u64 parent_ino_after_gen;
3584
Filipe Mananaf9594922014-03-27 20:14:01 +00003585 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003586 /*
3587 * If the current inode is an ancestor of ino in the
3588 * parent root, we need to delay the rename of the
3589 * current inode, otherwise don't delayed the rename
3590 * because we can end up with a circular dependency
3591 * of renames, resulting in some directories never
3592 * getting the respective rename operations issued in
3593 * the send stream or getting into infinite path build
3594 * loops.
3595 */
3596 ret = is_ancestor(sctx->parent_root,
3597 sctx->cur_ino, sctx->cur_inode_gen,
3598 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003599 if (ret)
3600 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003601 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003602
3603 fs_path_reset(path_before);
3604 fs_path_reset(path_after);
3605
3606 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003607 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003608 if (ret < 0)
3609 goto out;
3610 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3611 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003612 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003613 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003614 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003615 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003616 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003617 }
3618
3619 len1 = fs_path_len(path_before);
3620 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003621 if (ino > sctx->cur_ino &&
3622 (parent_ino_before != parent_ino_after || len1 != len2 ||
3623 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003624 u64 parent_ino_gen;
3625
3626 ret = get_inode_info(sctx->parent_root, ino, NULL,
3627 &parent_ino_gen, NULL, NULL, NULL,
3628 NULL);
3629 if (ret < 0)
3630 goto out;
3631 if (ino_gen == parent_ino_gen) {
3632 ret = 1;
3633 break;
3634 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003635 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003636 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003637 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003638 }
3639
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003640out:
3641 fs_path_free(path_before);
3642 fs_path_free(path_after);
3643
Filipe Mananaf9594922014-03-27 20:14:01 +00003644 if (ret == 1) {
3645 ret = add_pending_dir_move(sctx,
3646 sctx->cur_ino,
3647 sctx->cur_inode_gen,
3648 ino,
3649 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003650 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003651 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003652 if (!ret)
3653 ret = 1;
3654 }
3655
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003656 return ret;
3657}
3658
Alexander Block31db9f72012-07-25 23:19:24 +02003659/*
3660 * This does all the move/link/unlink/rmdir magic.
3661 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003662static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003663{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003664 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003665 int ret = 0;
3666 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003667 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003668 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003669 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003670 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003671 u64 ow_gen;
3672 int did_overwrite = 0;
3673 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003674 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003675 bool can_rename = true;
Alexander Block31db9f72012-07-25 23:19:24 +02003676
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003677 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003678
Alexander Block6d85ed052012-08-01 14:48:59 +02003679 /*
3680 * This should never happen as the root dir always has the same ref
3681 * which is always '..'
3682 */
3683 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003684 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed052012-08-01 14:48:59 +02003685
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003686 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003687 if (!valid_path) {
3688 ret = -ENOMEM;
3689 goto out;
3690 }
3691
Alexander Block31db9f72012-07-25 23:19:24 +02003692 /*
3693 * First, check if the first ref of the current inode was overwritten
3694 * before. If yes, we know that the current inode was already orphanized
3695 * and thus use the orphan name. If not, we can use get_cur_path to
3696 * get the path of the first ref as it would like while receiving at
3697 * this point in time.
3698 * New inodes are always orphan at the beginning, so force to use the
3699 * orphan name in this case.
3700 * The first ref is stored in valid_path and will be updated if it
3701 * gets moved around.
3702 */
3703 if (!sctx->cur_inode_new) {
3704 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3705 sctx->cur_inode_gen);
3706 if (ret < 0)
3707 goto out;
3708 if (ret)
3709 did_overwrite = 1;
3710 }
3711 if (sctx->cur_inode_new || did_overwrite) {
3712 ret = gen_unique_name(sctx, sctx->cur_ino,
3713 sctx->cur_inode_gen, valid_path);
3714 if (ret < 0)
3715 goto out;
3716 is_orphan = 1;
3717 } else {
3718 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3719 valid_path);
3720 if (ret < 0)
3721 goto out;
3722 }
3723
3724 list_for_each_entry(cur, &sctx->new_refs, list) {
3725 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003726 * We may have refs where the parent directory does not exist
3727 * yet. This happens if the parent directories inum is higher
3728 * the the current inum. To handle this case, we create the
3729 * parent directory out of order. But we need to check if this
3730 * did already happen before due to other refs in the same dir.
3731 */
3732 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3733 if (ret < 0)
3734 goto out;
3735 if (ret == inode_state_will_create) {
3736 ret = 0;
3737 /*
3738 * First check if any of the current inodes refs did
3739 * already create the dir.
3740 */
3741 list_for_each_entry(cur2, &sctx->new_refs, list) {
3742 if (cur == cur2)
3743 break;
3744 if (cur2->dir == cur->dir) {
3745 ret = 1;
3746 break;
3747 }
3748 }
3749
3750 /*
3751 * If that did not happen, check if a previous inode
3752 * did already create the dir.
3753 */
3754 if (!ret)
3755 ret = did_create_dir(sctx, cur->dir);
3756 if (ret < 0)
3757 goto out;
3758 if (!ret) {
3759 ret = send_create_inode(sctx, cur->dir);
3760 if (ret < 0)
3761 goto out;
3762 }
3763 }
3764
3765 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003766 * Check if this new ref would overwrite the first ref of
3767 * another unprocessed inode. If yes, orphanize the
3768 * overwritten inode. If we find an overwritten ref that is
3769 * not the first ref, simply unlink it.
3770 */
3771 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3772 cur->name, cur->name_len,
3773 &ow_inode, &ow_gen);
3774 if (ret < 0)
3775 goto out;
3776 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003777 ret = is_first_ref(sctx->parent_root,
3778 ow_inode, cur->dir, cur->name,
3779 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003780 if (ret < 0)
3781 goto out;
3782 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003783 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003784 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003785
Alexander Block31db9f72012-07-25 23:19:24 +02003786 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3787 cur->full_path);
3788 if (ret < 0)
3789 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003790
3791 /*
3792 * If ow_inode has its rename operation delayed
3793 * make sure that its orphanized name is used in
3794 * the source path when performing its rename
3795 * operation.
3796 */
3797 if (is_waiting_for_move(sctx, ow_inode)) {
3798 wdm = get_waiting_dir_move(sctx,
3799 ow_inode);
3800 ASSERT(wdm);
3801 wdm->orphanized = true;
3802 }
3803
Filipe Manana8996a482015-03-12 15:16:20 +00003804 /*
3805 * Make sure we clear our orphanized inode's
3806 * name from the name cache. This is because the
3807 * inode ow_inode might be an ancestor of some
3808 * other inode that will be orphanized as well
3809 * later and has an inode number greater than
3810 * sctx->send_progress. We need to prevent
3811 * future name lookups from using the old name
3812 * and get instead the orphan name.
3813 */
3814 nce = name_cache_search(sctx, ow_inode, ow_gen);
3815 if (nce) {
3816 name_cache_delete(sctx, nce);
3817 kfree(nce);
3818 }
Robbie Ko801bec32015-06-23 18:39:46 +08003819
3820 /*
3821 * ow_inode might currently be an ancestor of
3822 * cur_ino, therefore compute valid_path (the
3823 * current path of cur_ino) again because it
3824 * might contain the pre-orphanization name of
3825 * ow_inode, which is no longer valid.
3826 */
3827 fs_path_reset(valid_path);
3828 ret = get_cur_path(sctx, sctx->cur_ino,
3829 sctx->cur_inode_gen, valid_path);
3830 if (ret < 0)
3831 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003832 } else {
3833 ret = send_unlink(sctx, cur->full_path);
3834 if (ret < 0)
3835 goto out;
3836 }
3837 }
3838
Filipe Manana84471e22015-02-28 22:29:22 +00003839 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3840 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3841 if (ret < 0)
3842 goto out;
3843 if (ret == 1) {
3844 can_rename = false;
3845 *pending_move = 1;
3846 }
3847 }
3848
Filipe Manana8b191a62015-04-09 14:09:14 +01003849 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3850 can_rename) {
3851 ret = wait_for_parent_move(sctx, cur, is_orphan);
3852 if (ret < 0)
3853 goto out;
3854 if (ret == 1) {
3855 can_rename = false;
3856 *pending_move = 1;
3857 }
3858 }
3859
Alexander Block31db9f72012-07-25 23:19:24 +02003860 /*
3861 * link/move the ref to the new place. If we have an orphan
3862 * inode, move it and update valid_path. If not, link or move
3863 * it depending on the inode mode.
3864 */
Filipe Manana84471e22015-02-28 22:29:22 +00003865 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003866 ret = send_rename(sctx, valid_path, cur->full_path);
3867 if (ret < 0)
3868 goto out;
3869 is_orphan = 0;
3870 ret = fs_path_copy(valid_path, cur->full_path);
3871 if (ret < 0)
3872 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00003873 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003874 if (S_ISDIR(sctx->cur_inode_mode)) {
3875 /*
3876 * Dirs can't be linked, so move it. For moved
3877 * dirs, we always have one new and one deleted
3878 * ref. The deleted ref is ignored later.
3879 */
Filipe Manana8b191a62015-04-09 14:09:14 +01003880 ret = send_rename(sctx, valid_path,
3881 cur->full_path);
3882 if (!ret)
3883 ret = fs_path_copy(valid_path,
3884 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003885 if (ret < 0)
3886 goto out;
3887 } else {
3888 ret = send_link(sctx, cur->full_path,
3889 valid_path);
3890 if (ret < 0)
3891 goto out;
3892 }
3893 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003894 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003895 if (ret < 0)
3896 goto out;
3897 }
3898
3899 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3900 /*
3901 * Check if we can already rmdir the directory. If not,
3902 * orphanize it. For every dir item inside that gets deleted
3903 * later, we do this check again and rmdir it then if possible.
3904 * See the use of check_dirs for more details.
3905 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003906 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3907 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003908 if (ret < 0)
3909 goto out;
3910 if (ret) {
3911 ret = send_rmdir(sctx, valid_path);
3912 if (ret < 0)
3913 goto out;
3914 } else if (!is_orphan) {
3915 ret = orphanize_inode(sctx, sctx->cur_ino,
3916 sctx->cur_inode_gen, valid_path);
3917 if (ret < 0)
3918 goto out;
3919 is_orphan = 1;
3920 }
3921
3922 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003923 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003924 if (ret < 0)
3925 goto out;
3926 }
Alexander Blockccf16262012-07-28 11:46:29 +02003927 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3928 !list_empty(&sctx->deleted_refs)) {
3929 /*
3930 * We have a moved dir. Add the old parent to check_dirs
3931 */
3932 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3933 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003934 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003935 if (ret < 0)
3936 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003937 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3938 /*
3939 * We have a non dir inode. Go through all deleted refs and
3940 * unlink them if they were not already overwritten by other
3941 * inodes.
3942 */
3943 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3944 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3945 sctx->cur_ino, sctx->cur_inode_gen,
3946 cur->name, cur->name_len);
3947 if (ret < 0)
3948 goto out;
3949 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003950 ret = send_unlink(sctx, cur->full_path);
3951 if (ret < 0)
3952 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003953 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003954 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003955 if (ret < 0)
3956 goto out;
3957 }
Alexander Block31db9f72012-07-25 23:19:24 +02003958 /*
3959 * If the inode is still orphan, unlink the orphan. This may
3960 * happen when a previous inode did overwrite the first ref
3961 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003962 * inode. Unlinking does not mean that the inode is deleted in
3963 * all cases. There may still be links to this inode in other
3964 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003965 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003966 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003967 ret = send_unlink(sctx, valid_path);
3968 if (ret < 0)
3969 goto out;
3970 }
3971 }
3972
3973 /*
3974 * We did collect all parent dirs where cur_inode was once located. We
3975 * now go through all these dirs and check if they are pending for
3976 * deletion and if it's finally possible to perform the rmdir now.
3977 * We also update the inode stats of the parent dirs here.
3978 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003979 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003980 /*
3981 * In case we had refs into dirs that were not processed yet,
3982 * we don't need to do the utime and rmdir logic for these dirs.
3983 * The dir will be processed later.
3984 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003985 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003986 continue;
3987
Josef Bacikba5e8f22013-08-16 16:52:55 -04003988 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003989 if (ret < 0)
3990 goto out;
3991
3992 if (ret == inode_state_did_create ||
3993 ret == inode_state_no_change) {
3994 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003995 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003996 if (ret < 0)
3997 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003998 } else if (ret == inode_state_did_delete &&
3999 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004000 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4001 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004002 if (ret < 0)
4003 goto out;
4004 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004005 ret = get_cur_path(sctx, cur->dir,
4006 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004007 if (ret < 0)
4008 goto out;
4009 ret = send_rmdir(sctx, valid_path);
4010 if (ret < 0)
4011 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004012 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004013 }
4014 }
4015 }
4016
Alexander Block31db9f72012-07-25 23:19:24 +02004017 ret = 0;
4018
4019out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004020 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004021 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004022 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004023 return ret;
4024}
4025
Liu Boa4d96d62014-03-03 21:31:03 +08004026static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
4027 struct fs_path *name, void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004028{
4029 int ret = 0;
4030 struct send_ctx *sctx = ctx;
4031 struct fs_path *p;
4032 u64 gen;
4033
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004034 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004035 if (!p)
4036 return -ENOMEM;
4037
Liu Boa4d96d62014-03-03 21:31:03 +08004038 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004039 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004040 if (ret < 0)
4041 goto out;
4042
Alexander Block31db9f72012-07-25 23:19:24 +02004043 ret = get_cur_path(sctx, dir, gen, p);
4044 if (ret < 0)
4045 goto out;
4046 ret = fs_path_add_path(p, name);
4047 if (ret < 0)
4048 goto out;
4049
Liu Boa4d96d62014-03-03 21:31:03 +08004050 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004051
4052out:
4053 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004054 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004055 return ret;
4056}
4057
Liu Boa4d96d62014-03-03 21:31:03 +08004058static int __record_new_ref(int num, u64 dir, int index,
4059 struct fs_path *name,
4060 void *ctx)
4061{
4062 struct send_ctx *sctx = ctx;
4063 return record_ref(sctx->send_root, num, dir, index, name,
4064 ctx, &sctx->new_refs);
4065}
4066
4067
Alexander Block31db9f72012-07-25 23:19:24 +02004068static int __record_deleted_ref(int num, u64 dir, int index,
4069 struct fs_path *name,
4070 void *ctx)
4071{
Alexander Block31db9f72012-07-25 23:19:24 +02004072 struct send_ctx *sctx = ctx;
Liu Boa4d96d62014-03-03 21:31:03 +08004073 return record_ref(sctx->parent_root, num, dir, index, name,
4074 ctx, &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004075}
4076
4077static int record_new_ref(struct send_ctx *sctx)
4078{
4079 int ret;
4080
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004081 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4082 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004083 if (ret < 0)
4084 goto out;
4085 ret = 0;
4086
4087out:
4088 return ret;
4089}
4090
4091static int record_deleted_ref(struct send_ctx *sctx)
4092{
4093 int ret;
4094
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004095 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4096 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004097 if (ret < 0)
4098 goto out;
4099 ret = 0;
4100
4101out:
4102 return ret;
4103}
4104
4105struct find_ref_ctx {
4106 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004107 u64 dir_gen;
4108 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004109 struct fs_path *name;
4110 int found_idx;
4111};
4112
4113static int __find_iref(int num, u64 dir, int index,
4114 struct fs_path *name,
4115 void *ctx_)
4116{
4117 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004118 u64 dir_gen;
4119 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004120
4121 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4122 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004123 /*
4124 * To avoid doing extra lookups we'll only do this if everything
4125 * else matches.
4126 */
4127 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4128 NULL, NULL, NULL);
4129 if (ret)
4130 return ret;
4131 if (dir_gen != ctx->dir_gen)
4132 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004133 ctx->found_idx = num;
4134 return 1;
4135 }
4136 return 0;
4137}
4138
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004139static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004140 struct btrfs_path *path,
4141 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004142 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004143{
4144 int ret;
4145 struct find_ref_ctx ctx;
4146
4147 ctx.dir = dir;
4148 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004149 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004150 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004151 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004152
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004153 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004154 if (ret < 0)
4155 return ret;
4156
4157 if (ctx.found_idx == -1)
4158 return -ENOENT;
4159
4160 return ctx.found_idx;
4161}
4162
4163static int __record_changed_new_ref(int num, u64 dir, int index,
4164 struct fs_path *name,
4165 void *ctx)
4166{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004167 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004168 int ret;
4169 struct send_ctx *sctx = ctx;
4170
Josef Bacikba5e8f22013-08-16 16:52:55 -04004171 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4172 NULL, NULL, NULL);
4173 if (ret)
4174 return ret;
4175
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004176 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004177 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004178 if (ret == -ENOENT)
4179 ret = __record_new_ref(num, dir, index, name, sctx);
4180 else if (ret > 0)
4181 ret = 0;
4182
4183 return ret;
4184}
4185
4186static int __record_changed_deleted_ref(int num, u64 dir, int index,
4187 struct fs_path *name,
4188 void *ctx)
4189{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004190 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004191 int ret;
4192 struct send_ctx *sctx = ctx;
4193
Josef Bacikba5e8f22013-08-16 16:52:55 -04004194 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4195 NULL, NULL, NULL);
4196 if (ret)
4197 return ret;
4198
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004199 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004200 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004201 if (ret == -ENOENT)
4202 ret = __record_deleted_ref(num, dir, index, name, sctx);
4203 else if (ret > 0)
4204 ret = 0;
4205
4206 return ret;
4207}
4208
4209static int record_changed_ref(struct send_ctx *sctx)
4210{
4211 int ret = 0;
4212
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004213 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004214 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4215 if (ret < 0)
4216 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004217 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004218 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4219 if (ret < 0)
4220 goto out;
4221 ret = 0;
4222
4223out:
4224 return ret;
4225}
4226
4227/*
4228 * Record and process all refs at once. Needed when an inode changes the
4229 * generation number, which means that it was deleted and recreated.
4230 */
4231static int process_all_refs(struct send_ctx *sctx,
4232 enum btrfs_compare_tree_result cmd)
4233{
4234 int ret;
4235 struct btrfs_root *root;
4236 struct btrfs_path *path;
4237 struct btrfs_key key;
4238 struct btrfs_key found_key;
4239 struct extent_buffer *eb;
4240 int slot;
4241 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004242 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004243
4244 path = alloc_path_for_send();
4245 if (!path)
4246 return -ENOMEM;
4247
4248 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4249 root = sctx->send_root;
4250 cb = __record_new_ref;
4251 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4252 root = sctx->parent_root;
4253 cb = __record_deleted_ref;
4254 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004255 btrfs_err(sctx->send_root->fs_info,
4256 "Wrong command %d in process_all_refs", cmd);
4257 ret = -EINVAL;
4258 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004259 }
4260
4261 key.objectid = sctx->cmp_key->objectid;
4262 key.type = BTRFS_INODE_REF_KEY;
4263 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004264 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4265 if (ret < 0)
4266 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004267
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004268 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004269 eb = path->nodes[0];
4270 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004271 if (slot >= btrfs_header_nritems(eb)) {
4272 ret = btrfs_next_leaf(root, path);
4273 if (ret < 0)
4274 goto out;
4275 else if (ret > 0)
4276 break;
4277 continue;
4278 }
4279
Alexander Block31db9f72012-07-25 23:19:24 +02004280 btrfs_item_key_to_cpu(eb, &found_key, slot);
4281
4282 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004283 (found_key.type != BTRFS_INODE_REF_KEY &&
4284 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004285 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004286
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004287 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004288 if (ret < 0)
4289 goto out;
4290
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004291 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004292 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004293 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004294
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004295 /*
4296 * We don't actually care about pending_move as we are simply
4297 * re-creating this inode and will be rename'ing it into place once we
4298 * rename the parent directory.
4299 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004300 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004301out:
4302 btrfs_free_path(path);
4303 return ret;
4304}
4305
4306static int send_set_xattr(struct send_ctx *sctx,
4307 struct fs_path *path,
4308 const char *name, int name_len,
4309 const char *data, int data_len)
4310{
4311 int ret = 0;
4312
4313 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4314 if (ret < 0)
4315 goto out;
4316
4317 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4318 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4319 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4320
4321 ret = send_cmd(sctx);
4322
4323tlv_put_failure:
4324out:
4325 return ret;
4326}
4327
4328static int send_remove_xattr(struct send_ctx *sctx,
4329 struct fs_path *path,
4330 const char *name, int name_len)
4331{
4332 int ret = 0;
4333
4334 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4335 if (ret < 0)
4336 goto out;
4337
4338 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4339 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4340
4341 ret = send_cmd(sctx);
4342
4343tlv_put_failure:
4344out:
4345 return ret;
4346}
4347
4348static int __process_new_xattr(int num, struct btrfs_key *di_key,
4349 const char *name, int name_len,
4350 const char *data, int data_len,
4351 u8 type, void *ctx)
4352{
4353 int ret;
4354 struct send_ctx *sctx = ctx;
4355 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004356 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004357
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004358 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004359 if (!p)
4360 return -ENOMEM;
4361
4362 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004363 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004364 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004365 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004366 * only contains the version number and no entries.
4367 */
4368 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4369 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4370 if (data_len == 0) {
4371 dummy_acl.a_version =
4372 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4373 data = (char *)&dummy_acl;
4374 data_len = sizeof(dummy_acl);
4375 }
4376 }
4377
4378 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4379 if (ret < 0)
4380 goto out;
4381
4382 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4383
4384out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004385 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004386 return ret;
4387}
4388
4389static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4390 const char *name, int name_len,
4391 const char *data, int data_len,
4392 u8 type, void *ctx)
4393{
4394 int ret;
4395 struct send_ctx *sctx = ctx;
4396 struct fs_path *p;
4397
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004398 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004399 if (!p)
4400 return -ENOMEM;
4401
4402 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4403 if (ret < 0)
4404 goto out;
4405
4406 ret = send_remove_xattr(sctx, p, name, name_len);
4407
4408out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004409 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004410 return ret;
4411}
4412
4413static int process_new_xattr(struct send_ctx *sctx)
4414{
4415 int ret = 0;
4416
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004417 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4418 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004419
4420 return ret;
4421}
4422
4423static int process_deleted_xattr(struct send_ctx *sctx)
4424{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004425 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4426 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004427}
4428
4429struct find_xattr_ctx {
4430 const char *name;
4431 int name_len;
4432 int found_idx;
4433 char *found_data;
4434 int found_data_len;
4435};
4436
4437static int __find_xattr(int num, struct btrfs_key *di_key,
4438 const char *name, int name_len,
4439 const char *data, int data_len,
4440 u8 type, void *vctx)
4441{
4442 struct find_xattr_ctx *ctx = vctx;
4443
4444 if (name_len == ctx->name_len &&
4445 strncmp(name, ctx->name, name_len) == 0) {
4446 ctx->found_idx = num;
4447 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004448 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004449 if (!ctx->found_data)
4450 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004451 return 1;
4452 }
4453 return 0;
4454}
4455
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004456static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004457 struct btrfs_path *path,
4458 struct btrfs_key *key,
4459 const char *name, int name_len,
4460 char **data, int *data_len)
4461{
4462 int ret;
4463 struct find_xattr_ctx ctx;
4464
4465 ctx.name = name;
4466 ctx.name_len = name_len;
4467 ctx.found_idx = -1;
4468 ctx.found_data = NULL;
4469 ctx.found_data_len = 0;
4470
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004471 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004472 if (ret < 0)
4473 return ret;
4474
4475 if (ctx.found_idx == -1)
4476 return -ENOENT;
4477 if (data) {
4478 *data = ctx.found_data;
4479 *data_len = ctx.found_data_len;
4480 } else {
4481 kfree(ctx.found_data);
4482 }
4483 return ctx.found_idx;
4484}
4485
4486
4487static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4488 const char *name, int name_len,
4489 const char *data, int data_len,
4490 u8 type, void *ctx)
4491{
4492 int ret;
4493 struct send_ctx *sctx = ctx;
4494 char *found_data = NULL;
4495 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004496
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004497 ret = find_xattr(sctx->parent_root, sctx->right_path,
4498 sctx->cmp_key, name, name_len, &found_data,
4499 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004500 if (ret == -ENOENT) {
4501 ret = __process_new_xattr(num, di_key, name, name_len, data,
4502 data_len, type, ctx);
4503 } else if (ret >= 0) {
4504 if (data_len != found_data_len ||
4505 memcmp(data, found_data, data_len)) {
4506 ret = __process_new_xattr(num, di_key, name, name_len,
4507 data, data_len, type, ctx);
4508 } else {
4509 ret = 0;
4510 }
4511 }
4512
4513 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004514 return ret;
4515}
4516
4517static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4518 const char *name, int name_len,
4519 const char *data, int data_len,
4520 u8 type, void *ctx)
4521{
4522 int ret;
4523 struct send_ctx *sctx = ctx;
4524
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004525 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4526 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004527 if (ret == -ENOENT)
4528 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4529 data_len, type, ctx);
4530 else if (ret >= 0)
4531 ret = 0;
4532
4533 return ret;
4534}
4535
4536static int process_changed_xattr(struct send_ctx *sctx)
4537{
4538 int ret = 0;
4539
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004540 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004541 sctx->cmp_key, __process_changed_new_xattr, sctx);
4542 if (ret < 0)
4543 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004544 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004545 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4546
4547out:
4548 return ret;
4549}
4550
4551static int process_all_new_xattrs(struct send_ctx *sctx)
4552{
4553 int ret;
4554 struct btrfs_root *root;
4555 struct btrfs_path *path;
4556 struct btrfs_key key;
4557 struct btrfs_key found_key;
4558 struct extent_buffer *eb;
4559 int slot;
4560
4561 path = alloc_path_for_send();
4562 if (!path)
4563 return -ENOMEM;
4564
4565 root = sctx->send_root;
4566
4567 key.objectid = sctx->cmp_key->objectid;
4568 key.type = BTRFS_XATTR_ITEM_KEY;
4569 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004570 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4571 if (ret < 0)
4572 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004573
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004574 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004575 eb = path->nodes[0];
4576 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004577 if (slot >= btrfs_header_nritems(eb)) {
4578 ret = btrfs_next_leaf(root, path);
4579 if (ret < 0) {
4580 goto out;
4581 } else if (ret > 0) {
4582 ret = 0;
4583 break;
4584 }
4585 continue;
4586 }
Alexander Block31db9f72012-07-25 23:19:24 +02004587
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004588 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004589 if (found_key.objectid != key.objectid ||
4590 found_key.type != key.type) {
4591 ret = 0;
4592 goto out;
4593 }
4594
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004595 ret = iterate_dir_item(root, path, &found_key,
4596 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004597 if (ret < 0)
4598 goto out;
4599
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004600 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004601 }
4602
4603out:
4604 btrfs_free_path(path);
4605 return ret;
4606}
4607
Josef Baciked259092013-10-25 11:36:01 -04004608static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4609{
4610 struct btrfs_root *root = sctx->send_root;
4611 struct btrfs_fs_info *fs_info = root->fs_info;
4612 struct inode *inode;
4613 struct page *page;
4614 char *addr;
4615 struct btrfs_key key;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004616 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004617 pgoff_t last_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004618 unsigned pg_offset = offset & ~PAGE_MASK;
Josef Baciked259092013-10-25 11:36:01 -04004619 ssize_t ret = 0;
4620
4621 key.objectid = sctx->cur_ino;
4622 key.type = BTRFS_INODE_ITEM_KEY;
4623 key.offset = 0;
4624
4625 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4626 if (IS_ERR(inode))
4627 return PTR_ERR(inode);
4628
4629 if (offset + len > i_size_read(inode)) {
4630 if (offset > i_size_read(inode))
4631 len = 0;
4632 else
4633 len = offset - i_size_read(inode);
4634 }
4635 if (len == 0)
4636 goto out;
4637
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004638 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004639
4640 /* initial readahead */
4641 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4642 file_ra_state_init(&sctx->ra, inode->i_mapping);
4643 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4644 last_index - index + 1);
4645
Josef Baciked259092013-10-25 11:36:01 -04004646 while (index <= last_index) {
4647 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004648 PAGE_SIZE - pg_offset);
David Sterbae780b0d2016-01-18 18:42:13 +01004649 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
Josef Baciked259092013-10-25 11:36:01 -04004650 if (!page) {
4651 ret = -ENOMEM;
4652 break;
4653 }
4654
4655 if (!PageUptodate(page)) {
4656 btrfs_readpage(NULL, page);
4657 lock_page(page);
4658 if (!PageUptodate(page)) {
4659 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004660 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004661 ret = -EIO;
4662 break;
4663 }
4664 }
4665
4666 addr = kmap(page);
4667 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4668 kunmap(page);
4669 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004670 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004671 index++;
4672 pg_offset = 0;
4673 len -= cur_len;
4674 ret += cur_len;
4675 }
4676out:
4677 iput(inode);
4678 return ret;
4679}
4680
Alexander Block31db9f72012-07-25 23:19:24 +02004681/*
4682 * Read some bytes from the current inode/file and send a write command to
4683 * user space.
4684 */
4685static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4686{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004687 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02004688 int ret = 0;
4689 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004690 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004691
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004692 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004693 if (!p)
4694 return -ENOMEM;
4695
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004696 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004697
Josef Baciked259092013-10-25 11:36:01 -04004698 num_read = fill_read_buf(sctx, offset, len);
4699 if (num_read <= 0) {
4700 if (num_read < 0)
4701 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004702 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004703 }
Alexander Block31db9f72012-07-25 23:19:24 +02004704
4705 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4706 if (ret < 0)
4707 goto out;
4708
4709 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4710 if (ret < 0)
4711 goto out;
4712
4713 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4714 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004715 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004716
4717 ret = send_cmd(sctx);
4718
4719tlv_put_failure:
4720out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004721 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004722 if (ret < 0)
4723 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004724 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004725}
4726
4727/*
4728 * Send a clone command to user space.
4729 */
4730static int send_clone(struct send_ctx *sctx,
4731 u64 offset, u32 len,
4732 struct clone_root *clone_root)
4733{
4734 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004735 struct fs_path *p;
4736 u64 gen;
4737
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004738 btrfs_debug(sctx->send_root->fs_info,
4739 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4740 offset, len, clone_root->root->objectid, clone_root->ino,
4741 clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02004742
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004743 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004744 if (!p)
4745 return -ENOMEM;
4746
4747 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4748 if (ret < 0)
4749 goto out;
4750
4751 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4752 if (ret < 0)
4753 goto out;
4754
4755 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4756 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4757 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4758
Alexander Blocke938c8a2012-07-28 16:33:49 +02004759 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004760 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004761 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004762 if (ret < 0)
4763 goto out;
4764 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4765 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004766 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004767 }
4768 if (ret < 0)
4769 goto out;
4770
Josef Bacik37b8d272015-06-04 17:17:25 -04004771 /*
4772 * If the parent we're using has a received_uuid set then use that as
4773 * our clone source as that is what we will look for when doing a
4774 * receive.
4775 *
4776 * This covers the case that we create a snapshot off of a received
4777 * subvolume and then use that as the parent and try to receive on a
4778 * different host.
4779 */
4780 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4781 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4782 clone_root->root->root_item.received_uuid);
4783 else
4784 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4785 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004786 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004787 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004788 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4789 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4790 clone_root->offset);
4791
4792 ret = send_cmd(sctx);
4793
4794tlv_put_failure:
4795out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004796 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004797 return ret;
4798}
4799
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004800/*
4801 * Send an update extent command to user space.
4802 */
4803static int send_update_extent(struct send_ctx *sctx,
4804 u64 offset, u32 len)
4805{
4806 int ret = 0;
4807 struct fs_path *p;
4808
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004809 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004810 if (!p)
4811 return -ENOMEM;
4812
4813 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4814 if (ret < 0)
4815 goto out;
4816
4817 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4818 if (ret < 0)
4819 goto out;
4820
4821 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4822 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4823 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4824
4825 ret = send_cmd(sctx);
4826
4827tlv_put_failure:
4828out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004829 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004830 return ret;
4831}
4832
Josef Bacik16e75492013-10-22 12:18:51 -04004833static int send_hole(struct send_ctx *sctx, u64 end)
4834{
4835 struct fs_path *p = NULL;
4836 u64 offset = sctx->cur_inode_last_extent;
4837 u64 len;
4838 int ret = 0;
4839
4840 p = fs_path_alloc();
4841 if (!p)
4842 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01004843 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4844 if (ret < 0)
4845 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04004846 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4847 while (offset < end) {
4848 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4849
4850 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4851 if (ret < 0)
4852 break;
Josef Bacik16e75492013-10-22 12:18:51 -04004853 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4854 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4855 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4856 ret = send_cmd(sctx);
4857 if (ret < 0)
4858 break;
4859 offset += len;
4860 }
4861tlv_put_failure:
4862 fs_path_free(p);
4863 return ret;
4864}
4865
Filipe Mananad906d492015-10-02 10:47:34 +01004866static int send_extent_data(struct send_ctx *sctx,
4867 const u64 offset,
4868 const u64 len)
4869{
4870 u64 sent = 0;
4871
4872 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
4873 return send_update_extent(sctx, offset, len);
4874
4875 while (sent < len) {
4876 u64 size = len - sent;
4877 int ret;
4878
4879 if (size > BTRFS_SEND_READ_SIZE)
4880 size = BTRFS_SEND_READ_SIZE;
4881 ret = send_write(sctx, offset + sent, size);
4882 if (ret < 0)
4883 return ret;
4884 if (!ret)
4885 break;
4886 sent += ret;
4887 }
4888 return 0;
4889}
4890
4891static int clone_range(struct send_ctx *sctx,
4892 struct clone_root *clone_root,
4893 const u64 disk_byte,
4894 u64 data_offset,
4895 u64 offset,
4896 u64 len)
4897{
4898 struct btrfs_path *path;
4899 struct btrfs_key key;
4900 int ret;
4901
4902 path = alloc_path_for_send();
4903 if (!path)
4904 return -ENOMEM;
4905
4906 /*
4907 * We can't send a clone operation for the entire range if we find
4908 * extent items in the respective range in the source file that
4909 * refer to different extents or if we find holes.
4910 * So check for that and do a mix of clone and regular write/copy
4911 * operations if needed.
4912 *
4913 * Example:
4914 *
4915 * mkfs.btrfs -f /dev/sda
4916 * mount /dev/sda /mnt
4917 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
4918 * cp --reflink=always /mnt/foo /mnt/bar
4919 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
4920 * btrfs subvolume snapshot -r /mnt /mnt/snap
4921 *
4922 * If when we send the snapshot and we are processing file bar (which
4923 * has a higher inode number than foo) we blindly send a clone operation
4924 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
4925 * a file bar that matches the content of file foo - iow, doesn't match
4926 * the content from bar in the original filesystem.
4927 */
4928 key.objectid = clone_root->ino;
4929 key.type = BTRFS_EXTENT_DATA_KEY;
4930 key.offset = clone_root->offset;
4931 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
4932 if (ret < 0)
4933 goto out;
4934 if (ret > 0 && path->slots[0] > 0) {
4935 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
4936 if (key.objectid == clone_root->ino &&
4937 key.type == BTRFS_EXTENT_DATA_KEY)
4938 path->slots[0]--;
4939 }
4940
4941 while (true) {
4942 struct extent_buffer *leaf = path->nodes[0];
4943 int slot = path->slots[0];
4944 struct btrfs_file_extent_item *ei;
4945 u8 type;
4946 u64 ext_len;
4947 u64 clone_len;
4948
4949 if (slot >= btrfs_header_nritems(leaf)) {
4950 ret = btrfs_next_leaf(clone_root->root, path);
4951 if (ret < 0)
4952 goto out;
4953 else if (ret > 0)
4954 break;
4955 continue;
4956 }
4957
4958 btrfs_item_key_to_cpu(leaf, &key, slot);
4959
4960 /*
4961 * We might have an implicit trailing hole (NO_HOLES feature
4962 * enabled). We deal with it after leaving this loop.
4963 */
4964 if (key.objectid != clone_root->ino ||
4965 key.type != BTRFS_EXTENT_DATA_KEY)
4966 break;
4967
4968 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4969 type = btrfs_file_extent_type(leaf, ei);
4970 if (type == BTRFS_FILE_EXTENT_INLINE) {
4971 ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004972 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01004973 } else {
4974 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
4975 }
4976
4977 if (key.offset + ext_len <= clone_root->offset)
4978 goto next;
4979
4980 if (key.offset > clone_root->offset) {
4981 /* Implicit hole, NO_HOLES feature enabled. */
4982 u64 hole_len = key.offset - clone_root->offset;
4983
4984 if (hole_len > len)
4985 hole_len = len;
4986 ret = send_extent_data(sctx, offset, hole_len);
4987 if (ret < 0)
4988 goto out;
4989
4990 len -= hole_len;
4991 if (len == 0)
4992 break;
4993 offset += hole_len;
4994 clone_root->offset += hole_len;
4995 data_offset += hole_len;
4996 }
4997
4998 if (key.offset >= clone_root->offset + len)
4999 break;
5000
5001 clone_len = min_t(u64, ext_len, len);
5002
5003 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5004 btrfs_file_extent_offset(leaf, ei) == data_offset)
5005 ret = send_clone(sctx, offset, clone_len, clone_root);
5006 else
5007 ret = send_extent_data(sctx, offset, clone_len);
5008
5009 if (ret < 0)
5010 goto out;
5011
5012 len -= clone_len;
5013 if (len == 0)
5014 break;
5015 offset += clone_len;
5016 clone_root->offset += clone_len;
5017 data_offset += clone_len;
5018next:
5019 path->slots[0]++;
5020 }
5021
5022 if (len > 0)
5023 ret = send_extent_data(sctx, offset, len);
5024 else
5025 ret = 0;
5026out:
5027 btrfs_free_path(path);
5028 return ret;
5029}
5030
Alexander Block31db9f72012-07-25 23:19:24 +02005031static int send_write_or_clone(struct send_ctx *sctx,
5032 struct btrfs_path *path,
5033 struct btrfs_key *key,
5034 struct clone_root *clone_root)
5035{
5036 int ret = 0;
5037 struct btrfs_file_extent_item *ei;
5038 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02005039 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02005040 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005041 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005042
5043 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5044 struct btrfs_file_extent_item);
5045 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005046 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005047 len = btrfs_file_extent_inline_len(path->nodes[0],
5048 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005049 /*
5050 * it is possible the inline item won't cover the whole page,
5051 * but there may be items after this page. Make
5052 * sure to send the whole thing
5053 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005054 len = PAGE_ALIGN(len);
Chris Mason74dd17f2012-08-07 16:25:13 -04005055 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02005056 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005057 }
Alexander Block31db9f72012-07-25 23:19:24 +02005058
5059 if (offset + len > sctx->cur_inode_size)
5060 len = sctx->cur_inode_size - offset;
5061 if (len == 0) {
5062 ret = 0;
5063 goto out;
5064 }
5065
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005066 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01005067 u64 disk_byte;
5068 u64 data_offset;
5069
5070 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5071 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5072 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5073 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005074 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005075 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005076 }
Alexander Block31db9f72012-07-25 23:19:24 +02005077out:
5078 return ret;
5079}
5080
5081static int is_extent_unchanged(struct send_ctx *sctx,
5082 struct btrfs_path *left_path,
5083 struct btrfs_key *ekey)
5084{
5085 int ret = 0;
5086 struct btrfs_key key;
5087 struct btrfs_path *path = NULL;
5088 struct extent_buffer *eb;
5089 int slot;
5090 struct btrfs_key found_key;
5091 struct btrfs_file_extent_item *ei;
5092 u64 left_disknr;
5093 u64 right_disknr;
5094 u64 left_offset;
5095 u64 right_offset;
5096 u64 left_offset_fixed;
5097 u64 left_len;
5098 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005099 u64 left_gen;
5100 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005101 u8 left_type;
5102 u8 right_type;
5103
5104 path = alloc_path_for_send();
5105 if (!path)
5106 return -ENOMEM;
5107
5108 eb = left_path->nodes[0];
5109 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005110 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5111 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005112
5113 if (left_type != BTRFS_FILE_EXTENT_REG) {
5114 ret = 0;
5115 goto out;
5116 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005117 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5118 left_len = btrfs_file_extent_num_bytes(eb, ei);
5119 left_offset = btrfs_file_extent_offset(eb, ei);
5120 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005121
5122 /*
5123 * Following comments will refer to these graphics. L is the left
5124 * extents which we are checking at the moment. 1-8 are the right
5125 * extents that we iterate.
5126 *
5127 * |-----L-----|
5128 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5129 *
5130 * |-----L-----|
5131 * |--1--|-2b-|...(same as above)
5132 *
5133 * Alternative situation. Happens on files where extents got split.
5134 * |-----L-----|
5135 * |-----------7-----------|-6-|
5136 *
5137 * Alternative situation. Happens on files which got larger.
5138 * |-----L-----|
5139 * |-8-|
5140 * Nothing follows after 8.
5141 */
5142
5143 key.objectid = ekey->objectid;
5144 key.type = BTRFS_EXTENT_DATA_KEY;
5145 key.offset = ekey->offset;
5146 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5147 if (ret < 0)
5148 goto out;
5149 if (ret) {
5150 ret = 0;
5151 goto out;
5152 }
5153
5154 /*
5155 * Handle special case where the right side has no extents at all.
5156 */
5157 eb = path->nodes[0];
5158 slot = path->slots[0];
5159 btrfs_item_key_to_cpu(eb, &found_key, slot);
5160 if (found_key.objectid != key.objectid ||
5161 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005162 /* If we're a hole then just pretend nothing changed */
5163 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005164 goto out;
5165 }
5166
5167 /*
5168 * We're now on 2a, 2b or 7.
5169 */
5170 key = found_key;
5171 while (key.offset < ekey->offset + left_len) {
5172 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5173 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005174 if (right_type != BTRFS_FILE_EXTENT_REG) {
5175 ret = 0;
5176 goto out;
5177 }
5178
Josef Bacik007d31f2013-10-31 16:49:02 -04005179 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5180 right_len = btrfs_file_extent_num_bytes(eb, ei);
5181 right_offset = btrfs_file_extent_offset(eb, ei);
5182 right_gen = btrfs_file_extent_generation(eb, ei);
5183
Alexander Block31db9f72012-07-25 23:19:24 +02005184 /*
5185 * Are we at extent 8? If yes, we know the extent is changed.
5186 * This may only happen on the first iteration.
5187 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005188 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005189 /* If we're a hole just pretend nothing changed */
5190 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005191 goto out;
5192 }
5193
5194 left_offset_fixed = left_offset;
5195 if (key.offset < ekey->offset) {
5196 /* Fix the right offset for 2a and 7. */
5197 right_offset += ekey->offset - key.offset;
5198 } else {
5199 /* Fix the left offset for all behind 2a and 2b */
5200 left_offset_fixed += key.offset - ekey->offset;
5201 }
5202
5203 /*
5204 * Check if we have the same extent.
5205 */
Alexander Block39540962012-08-01 12:46:05 +02005206 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005207 left_offset_fixed != right_offset ||
5208 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005209 ret = 0;
5210 goto out;
5211 }
5212
5213 /*
5214 * Go to the next extent.
5215 */
5216 ret = btrfs_next_item(sctx->parent_root, path);
5217 if (ret < 0)
5218 goto out;
5219 if (!ret) {
5220 eb = path->nodes[0];
5221 slot = path->slots[0];
5222 btrfs_item_key_to_cpu(eb, &found_key, slot);
5223 }
5224 if (ret || found_key.objectid != key.objectid ||
5225 found_key.type != key.type) {
5226 key.offset += right_len;
5227 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005228 }
5229 if (found_key.offset != key.offset + right_len) {
5230 ret = 0;
5231 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005232 }
5233 key = found_key;
5234 }
5235
5236 /*
5237 * We're now behind the left extent (treat as unchanged) or at the end
5238 * of the right side (treat as changed).
5239 */
5240 if (key.offset >= ekey->offset + left_len)
5241 ret = 1;
5242 else
5243 ret = 0;
5244
5245
5246out:
5247 btrfs_free_path(path);
5248 return ret;
5249}
5250
Josef Bacik16e75492013-10-22 12:18:51 -04005251static int get_last_extent(struct send_ctx *sctx, u64 offset)
5252{
5253 struct btrfs_path *path;
5254 struct btrfs_root *root = sctx->send_root;
5255 struct btrfs_file_extent_item *fi;
5256 struct btrfs_key key;
5257 u64 extent_end;
5258 u8 type;
5259 int ret;
5260
5261 path = alloc_path_for_send();
5262 if (!path)
5263 return -ENOMEM;
5264
5265 sctx->cur_inode_last_extent = 0;
5266
5267 key.objectid = sctx->cur_ino;
5268 key.type = BTRFS_EXTENT_DATA_KEY;
5269 key.offset = offset;
5270 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5271 if (ret < 0)
5272 goto out;
5273 ret = 0;
5274 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5275 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5276 goto out;
5277
5278 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5279 struct btrfs_file_extent_item);
5280 type = btrfs_file_extent_type(path->nodes[0], fi);
5281 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005282 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5283 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005284 extent_end = ALIGN(key.offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005285 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005286 } else {
5287 extent_end = key.offset +
5288 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5289 }
5290 sctx->cur_inode_last_extent = extent_end;
5291out:
5292 btrfs_free_path(path);
5293 return ret;
5294}
5295
5296static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5297 struct btrfs_key *key)
5298{
5299 struct btrfs_file_extent_item *fi;
5300 u64 extent_end;
5301 u8 type;
5302 int ret = 0;
5303
5304 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5305 return 0;
5306
5307 if (sctx->cur_inode_last_extent == (u64)-1) {
5308 ret = get_last_extent(sctx, key->offset - 1);
5309 if (ret)
5310 return ret;
5311 }
5312
5313 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5314 struct btrfs_file_extent_item);
5315 type = btrfs_file_extent_type(path->nodes[0], fi);
5316 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005317 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5318 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005319 extent_end = ALIGN(key->offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005320 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005321 } else {
5322 extent_end = key->offset +
5323 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5324 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005325
5326 if (path->slots[0] == 0 &&
5327 sctx->cur_inode_last_extent < key->offset) {
5328 /*
5329 * We might have skipped entire leafs that contained only
5330 * file extent items for our current inode. These leafs have
5331 * a generation number smaller (older) than the one in the
5332 * current leaf and the leaf our last extent came from, and
5333 * are located between these 2 leafs.
5334 */
5335 ret = get_last_extent(sctx, key->offset - 1);
5336 if (ret)
5337 return ret;
5338 }
5339
Josef Bacik16e75492013-10-22 12:18:51 -04005340 if (sctx->cur_inode_last_extent < key->offset)
5341 ret = send_hole(sctx, key->offset);
5342 sctx->cur_inode_last_extent = extent_end;
5343 return ret;
5344}
5345
Alexander Block31db9f72012-07-25 23:19:24 +02005346static int process_extent(struct send_ctx *sctx,
5347 struct btrfs_path *path,
5348 struct btrfs_key *key)
5349{
Alexander Block31db9f72012-07-25 23:19:24 +02005350 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005351 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005352
5353 if (S_ISLNK(sctx->cur_inode_mode))
5354 return 0;
5355
5356 if (sctx->parent_root && !sctx->cur_inode_new) {
5357 ret = is_extent_unchanged(sctx, path, key);
5358 if (ret < 0)
5359 goto out;
5360 if (ret) {
5361 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005362 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005363 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005364 } else {
5365 struct btrfs_file_extent_item *ei;
5366 u8 type;
5367
5368 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5369 struct btrfs_file_extent_item);
5370 type = btrfs_file_extent_type(path->nodes[0], ei);
5371 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5372 type == BTRFS_FILE_EXTENT_REG) {
5373 /*
5374 * The send spec does not have a prealloc command yet,
5375 * so just leave a hole for prealloc'ed extents until
5376 * we have enough commands queued up to justify rev'ing
5377 * the send spec.
5378 */
5379 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5380 ret = 0;
5381 goto out;
5382 }
5383
5384 /* Have a hole, just skip it. */
5385 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5386 ret = 0;
5387 goto out;
5388 }
5389 }
Alexander Block31db9f72012-07-25 23:19:24 +02005390 }
5391
5392 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5393 sctx->cur_inode_size, &found_clone);
5394 if (ret != -ENOENT && ret < 0)
5395 goto out;
5396
5397 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005398 if (ret)
5399 goto out;
5400out_hole:
5401 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005402out:
5403 return ret;
5404}
5405
5406static int process_all_extents(struct send_ctx *sctx)
5407{
5408 int ret;
5409 struct btrfs_root *root;
5410 struct btrfs_path *path;
5411 struct btrfs_key key;
5412 struct btrfs_key found_key;
5413 struct extent_buffer *eb;
5414 int slot;
5415
5416 root = sctx->send_root;
5417 path = alloc_path_for_send();
5418 if (!path)
5419 return -ENOMEM;
5420
5421 key.objectid = sctx->cmp_key->objectid;
5422 key.type = BTRFS_EXTENT_DATA_KEY;
5423 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5425 if (ret < 0)
5426 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005427
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005428 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005429 eb = path->nodes[0];
5430 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005431
5432 if (slot >= btrfs_header_nritems(eb)) {
5433 ret = btrfs_next_leaf(root, path);
5434 if (ret < 0) {
5435 goto out;
5436 } else if (ret > 0) {
5437 ret = 0;
5438 break;
5439 }
5440 continue;
5441 }
5442
Alexander Block31db9f72012-07-25 23:19:24 +02005443 btrfs_item_key_to_cpu(eb, &found_key, slot);
5444
5445 if (found_key.objectid != key.objectid ||
5446 found_key.type != key.type) {
5447 ret = 0;
5448 goto out;
5449 }
5450
5451 ret = process_extent(sctx, path, &found_key);
5452 if (ret < 0)
5453 goto out;
5454
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005455 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005456 }
5457
5458out:
5459 btrfs_free_path(path);
5460 return ret;
5461}
5462
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005463static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5464 int *pending_move,
5465 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005466{
5467 int ret = 0;
5468
5469 if (sctx->cur_ino == 0)
5470 goto out;
5471 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005472 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005473 goto out;
5474 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5475 goto out;
5476
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005477 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005478 if (ret < 0)
5479 goto out;
5480
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005481 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005482out:
5483 return ret;
5484}
5485
5486static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5487{
5488 int ret = 0;
5489 u64 left_mode;
5490 u64 left_uid;
5491 u64 left_gid;
5492 u64 right_mode;
5493 u64 right_uid;
5494 u64 right_gid;
5495 int need_chmod = 0;
5496 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005497 int pending_move = 0;
5498 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005499
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005500 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5501 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005502 if (ret < 0)
5503 goto out;
5504
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005505 /*
5506 * We have processed the refs and thus need to advance send_progress.
5507 * Now, calls to get_cur_xxx will take the updated refs of the current
5508 * inode into account.
5509 *
5510 * On the other hand, if our current inode is a directory and couldn't
5511 * be moved/renamed because its parent was renamed/moved too and it has
5512 * a higher inode number, we can only move/rename our current inode
5513 * after we moved/renamed its parent. Therefore in this case operate on
5514 * the old path (pre move/rename) of our current inode, and the
5515 * move/rename will be performed later.
5516 */
5517 if (refs_processed && !pending_move)
5518 sctx->send_progress = sctx->cur_ino + 1;
5519
Alexander Block31db9f72012-07-25 23:19:24 +02005520 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5521 goto out;
5522 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5523 goto out;
5524
5525 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005526 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005527 if (ret < 0)
5528 goto out;
5529
Alex Lyakase2d044f2012-10-17 13:52:47 +00005530 if (!sctx->parent_root || sctx->cur_inode_new) {
5531 need_chown = 1;
5532 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005533 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005534 } else {
5535 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5536 NULL, NULL, &right_mode, &right_uid,
5537 &right_gid, NULL);
5538 if (ret < 0)
5539 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005540
Alex Lyakase2d044f2012-10-17 13:52:47 +00005541 if (left_uid != right_uid || left_gid != right_gid)
5542 need_chown = 1;
5543 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5544 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005545 }
5546
5547 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005548 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005549 if (sctx->cur_inode_last_extent == (u64)-1 ||
5550 sctx->cur_inode_last_extent <
5551 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005552 ret = get_last_extent(sctx, (u64)-1);
5553 if (ret)
5554 goto out;
5555 }
5556 if (sctx->cur_inode_last_extent <
5557 sctx->cur_inode_size) {
5558 ret = send_hole(sctx, sctx->cur_inode_size);
5559 if (ret)
5560 goto out;
5561 }
5562 }
Alexander Block31db9f72012-07-25 23:19:24 +02005563 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5564 sctx->cur_inode_size);
5565 if (ret < 0)
5566 goto out;
5567 }
5568
5569 if (need_chown) {
5570 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5571 left_uid, left_gid);
5572 if (ret < 0)
5573 goto out;
5574 }
5575 if (need_chmod) {
5576 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5577 left_mode);
5578 if (ret < 0)
5579 goto out;
5580 }
5581
5582 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005583 * If other directory inodes depended on our current directory
5584 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005585 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005586 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5587 ret = apply_children_dir_moves(sctx);
5588 if (ret)
5589 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005590 /*
5591 * Need to send that every time, no matter if it actually
5592 * changed between the two trees as we have done changes to
5593 * the inode before. If our inode is a directory and it's
5594 * waiting to be moved/renamed, we will send its utimes when
5595 * it's moved/renamed, therefore we don't need to do it here.
5596 */
5597 sctx->send_progress = sctx->cur_ino + 1;
5598 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5599 if (ret < 0)
5600 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005601 }
5602
Alexander Block31db9f72012-07-25 23:19:24 +02005603out:
5604 return ret;
5605}
5606
5607static int changed_inode(struct send_ctx *sctx,
5608 enum btrfs_compare_tree_result result)
5609{
5610 int ret = 0;
5611 struct btrfs_key *key = sctx->cmp_key;
5612 struct btrfs_inode_item *left_ii = NULL;
5613 struct btrfs_inode_item *right_ii = NULL;
5614 u64 left_gen = 0;
5615 u64 right_gen = 0;
5616
Alexander Block31db9f72012-07-25 23:19:24 +02005617 sctx->cur_ino = key->objectid;
5618 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005619 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005620
5621 /*
5622 * Set send_progress to current inode. This will tell all get_cur_xxx
5623 * functions that the current inode's refs are not updated yet. Later,
5624 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5625 */
Alexander Block31db9f72012-07-25 23:19:24 +02005626 sctx->send_progress = sctx->cur_ino;
5627
5628 if (result == BTRFS_COMPARE_TREE_NEW ||
5629 result == BTRFS_COMPARE_TREE_CHANGED) {
5630 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5631 sctx->left_path->slots[0],
5632 struct btrfs_inode_item);
5633 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5634 left_ii);
5635 } else {
5636 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5637 sctx->right_path->slots[0],
5638 struct btrfs_inode_item);
5639 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5640 right_ii);
5641 }
5642 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5643 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5644 sctx->right_path->slots[0],
5645 struct btrfs_inode_item);
5646
5647 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5648 right_ii);
Alexander Block6d85ed052012-08-01 14:48:59 +02005649
5650 /*
5651 * The cur_ino = root dir case is special here. We can't treat
5652 * the inode as deleted+reused because it would generate a
5653 * stream that tries to delete/mkdir the root dir.
5654 */
5655 if (left_gen != right_gen &&
5656 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005657 sctx->cur_inode_new_gen = 1;
5658 }
5659
5660 if (result == BTRFS_COMPARE_TREE_NEW) {
5661 sctx->cur_inode_gen = left_gen;
5662 sctx->cur_inode_new = 1;
5663 sctx->cur_inode_deleted = 0;
5664 sctx->cur_inode_size = btrfs_inode_size(
5665 sctx->left_path->nodes[0], left_ii);
5666 sctx->cur_inode_mode = btrfs_inode_mode(
5667 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005668 sctx->cur_inode_rdev = btrfs_inode_rdev(
5669 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005670 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005671 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005672 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5673 sctx->cur_inode_gen = right_gen;
5674 sctx->cur_inode_new = 0;
5675 sctx->cur_inode_deleted = 1;
5676 sctx->cur_inode_size = btrfs_inode_size(
5677 sctx->right_path->nodes[0], right_ii);
5678 sctx->cur_inode_mode = btrfs_inode_mode(
5679 sctx->right_path->nodes[0], right_ii);
5680 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005681 /*
5682 * We need to do some special handling in case the inode was
5683 * reported as changed with a changed generation number. This
5684 * means that the original inode was deleted and new inode
5685 * reused the same inum. So we have to treat the old inode as
5686 * deleted and the new one as new.
5687 */
Alexander Block31db9f72012-07-25 23:19:24 +02005688 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005689 /*
5690 * First, process the inode as if it was deleted.
5691 */
Alexander Block31db9f72012-07-25 23:19:24 +02005692 sctx->cur_inode_gen = right_gen;
5693 sctx->cur_inode_new = 0;
5694 sctx->cur_inode_deleted = 1;
5695 sctx->cur_inode_size = btrfs_inode_size(
5696 sctx->right_path->nodes[0], right_ii);
5697 sctx->cur_inode_mode = btrfs_inode_mode(
5698 sctx->right_path->nodes[0], right_ii);
5699 ret = process_all_refs(sctx,
5700 BTRFS_COMPARE_TREE_DELETED);
5701 if (ret < 0)
5702 goto out;
5703
Alexander Block766702e2012-07-28 14:11:31 +02005704 /*
5705 * Now process the inode as if it was new.
5706 */
Alexander Block31db9f72012-07-25 23:19:24 +02005707 sctx->cur_inode_gen = left_gen;
5708 sctx->cur_inode_new = 1;
5709 sctx->cur_inode_deleted = 0;
5710 sctx->cur_inode_size = btrfs_inode_size(
5711 sctx->left_path->nodes[0], left_ii);
5712 sctx->cur_inode_mode = btrfs_inode_mode(
5713 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005714 sctx->cur_inode_rdev = btrfs_inode_rdev(
5715 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005716 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005717 if (ret < 0)
5718 goto out;
5719
5720 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5721 if (ret < 0)
5722 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005723 /*
5724 * Advance send_progress now as we did not get into
5725 * process_recorded_refs_if_needed in the new_gen case.
5726 */
5727 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005728
5729 /*
5730 * Now process all extents and xattrs of the inode as if
5731 * they were all new.
5732 */
Alexander Block31db9f72012-07-25 23:19:24 +02005733 ret = process_all_extents(sctx);
5734 if (ret < 0)
5735 goto out;
5736 ret = process_all_new_xattrs(sctx);
5737 if (ret < 0)
5738 goto out;
5739 } else {
5740 sctx->cur_inode_gen = left_gen;
5741 sctx->cur_inode_new = 0;
5742 sctx->cur_inode_new_gen = 0;
5743 sctx->cur_inode_deleted = 0;
5744 sctx->cur_inode_size = btrfs_inode_size(
5745 sctx->left_path->nodes[0], left_ii);
5746 sctx->cur_inode_mode = btrfs_inode_mode(
5747 sctx->left_path->nodes[0], left_ii);
5748 }
5749 }
5750
5751out:
5752 return ret;
5753}
5754
Alexander Block766702e2012-07-28 14:11:31 +02005755/*
5756 * We have to process new refs before deleted refs, but compare_trees gives us
5757 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5758 * first and later process them in process_recorded_refs.
5759 * For the cur_inode_new_gen case, we skip recording completely because
5760 * changed_inode did already initiate processing of refs. The reason for this is
5761 * that in this case, compare_tree actually compares the refs of 2 different
5762 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5763 * refs of the right tree as deleted and all refs of the left tree as new.
5764 */
Alexander Block31db9f72012-07-25 23:19:24 +02005765static int changed_ref(struct send_ctx *sctx,
5766 enum btrfs_compare_tree_result result)
5767{
5768 int ret = 0;
5769
Filipe Manana95155582016-08-01 01:50:37 +01005770 if (sctx->cur_ino != sctx->cmp_key->objectid) {
5771 inconsistent_snapshot_error(sctx, result, "reference");
5772 return -EIO;
5773 }
Alexander Block31db9f72012-07-25 23:19:24 +02005774
5775 if (!sctx->cur_inode_new_gen &&
5776 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5777 if (result == BTRFS_COMPARE_TREE_NEW)
5778 ret = record_new_ref(sctx);
5779 else if (result == BTRFS_COMPARE_TREE_DELETED)
5780 ret = record_deleted_ref(sctx);
5781 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5782 ret = record_changed_ref(sctx);
5783 }
5784
5785 return ret;
5786}
5787
Alexander Block766702e2012-07-28 14:11:31 +02005788/*
5789 * Process new/deleted/changed xattrs. We skip processing in the
5790 * cur_inode_new_gen case because changed_inode did already initiate processing
5791 * of xattrs. The reason is the same as in changed_ref
5792 */
Alexander Block31db9f72012-07-25 23:19:24 +02005793static int changed_xattr(struct send_ctx *sctx,
5794 enum btrfs_compare_tree_result result)
5795{
5796 int ret = 0;
5797
Filipe Manana95155582016-08-01 01:50:37 +01005798 if (sctx->cur_ino != sctx->cmp_key->objectid) {
5799 inconsistent_snapshot_error(sctx, result, "xattr");
5800 return -EIO;
5801 }
Alexander Block31db9f72012-07-25 23:19:24 +02005802
5803 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5804 if (result == BTRFS_COMPARE_TREE_NEW)
5805 ret = process_new_xattr(sctx);
5806 else if (result == BTRFS_COMPARE_TREE_DELETED)
5807 ret = process_deleted_xattr(sctx);
5808 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5809 ret = process_changed_xattr(sctx);
5810 }
5811
5812 return ret;
5813}
5814
Alexander Block766702e2012-07-28 14:11:31 +02005815/*
5816 * Process new/deleted/changed extents. We skip processing in the
5817 * cur_inode_new_gen case because changed_inode did already initiate processing
5818 * of extents. The reason is the same as in changed_ref
5819 */
Alexander Block31db9f72012-07-25 23:19:24 +02005820static int changed_extent(struct send_ctx *sctx,
5821 enum btrfs_compare_tree_result result)
5822{
5823 int ret = 0;
5824
Filipe Manana95155582016-08-01 01:50:37 +01005825 if (sctx->cur_ino != sctx->cmp_key->objectid) {
Filipe Mananad5e84fd2016-09-19 10:57:40 +01005826
5827 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5828 struct extent_buffer *leaf_l;
5829 struct extent_buffer *leaf_r;
5830 struct btrfs_file_extent_item *ei_l;
5831 struct btrfs_file_extent_item *ei_r;
5832
5833 leaf_l = sctx->left_path->nodes[0];
5834 leaf_r = sctx->right_path->nodes[0];
5835 ei_l = btrfs_item_ptr(leaf_l,
5836 sctx->left_path->slots[0],
5837 struct btrfs_file_extent_item);
5838 ei_r = btrfs_item_ptr(leaf_r,
5839 sctx->right_path->slots[0],
5840 struct btrfs_file_extent_item);
5841
5842 /*
5843 * We may have found an extent item that has changed
5844 * only its disk_bytenr field and the corresponding
5845 * inode item was not updated. This case happens due to
5846 * very specific timings during relocation when a leaf
5847 * that contains file extent items is COWed while
5848 * relocation is ongoing and its in the stage where it
5849 * updates data pointers. So when this happens we can
5850 * safely ignore it since we know it's the same extent,
5851 * but just at different logical and physical locations
5852 * (when an extent is fully replaced with a new one, we
5853 * know the generation number must have changed too,
5854 * since snapshot creation implies committing the current
5855 * transaction, and the inode item must have been updated
5856 * as well).
5857 * This replacement of the disk_bytenr happens at
5858 * relocation.c:replace_file_extents() through
5859 * relocation.c:btrfs_reloc_cow_block().
5860 */
5861 if (btrfs_file_extent_generation(leaf_l, ei_l) ==
5862 btrfs_file_extent_generation(leaf_r, ei_r) &&
5863 btrfs_file_extent_ram_bytes(leaf_l, ei_l) ==
5864 btrfs_file_extent_ram_bytes(leaf_r, ei_r) &&
5865 btrfs_file_extent_compression(leaf_l, ei_l) ==
5866 btrfs_file_extent_compression(leaf_r, ei_r) &&
5867 btrfs_file_extent_encryption(leaf_l, ei_l) ==
5868 btrfs_file_extent_encryption(leaf_r, ei_r) &&
5869 btrfs_file_extent_other_encoding(leaf_l, ei_l) ==
5870 btrfs_file_extent_other_encoding(leaf_r, ei_r) &&
5871 btrfs_file_extent_type(leaf_l, ei_l) ==
5872 btrfs_file_extent_type(leaf_r, ei_r) &&
5873 btrfs_file_extent_disk_bytenr(leaf_l, ei_l) !=
5874 btrfs_file_extent_disk_bytenr(leaf_r, ei_r) &&
5875 btrfs_file_extent_disk_num_bytes(leaf_l, ei_l) ==
5876 btrfs_file_extent_disk_num_bytes(leaf_r, ei_r) &&
5877 btrfs_file_extent_offset(leaf_l, ei_l) ==
5878 btrfs_file_extent_offset(leaf_r, ei_r) &&
5879 btrfs_file_extent_num_bytes(leaf_l, ei_l) ==
5880 btrfs_file_extent_num_bytes(leaf_r, ei_r))
5881 return 0;
5882 }
5883
Filipe Manana95155582016-08-01 01:50:37 +01005884 inconsistent_snapshot_error(sctx, result, "extent");
5885 return -EIO;
5886 }
Alexander Block31db9f72012-07-25 23:19:24 +02005887
5888 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5889 if (result != BTRFS_COMPARE_TREE_DELETED)
5890 ret = process_extent(sctx, sctx->left_path,
5891 sctx->cmp_key);
5892 }
5893
5894 return ret;
5895}
5896
Josef Bacikba5e8f22013-08-16 16:52:55 -04005897static int dir_changed(struct send_ctx *sctx, u64 dir)
5898{
5899 u64 orig_gen, new_gen;
5900 int ret;
5901
5902 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5903 NULL, NULL);
5904 if (ret)
5905 return ret;
5906
5907 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5908 NULL, NULL, NULL);
5909 if (ret)
5910 return ret;
5911
5912 return (orig_gen != new_gen) ? 1 : 0;
5913}
5914
5915static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5916 struct btrfs_key *key)
5917{
5918 struct btrfs_inode_extref *extref;
5919 struct extent_buffer *leaf;
5920 u64 dirid = 0, last_dirid = 0;
5921 unsigned long ptr;
5922 u32 item_size;
5923 u32 cur_offset = 0;
5924 int ref_name_len;
5925 int ret = 0;
5926
5927 /* Easy case, just check this one dirid */
5928 if (key->type == BTRFS_INODE_REF_KEY) {
5929 dirid = key->offset;
5930
5931 ret = dir_changed(sctx, dirid);
5932 goto out;
5933 }
5934
5935 leaf = path->nodes[0];
5936 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5937 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5938 while (cur_offset < item_size) {
5939 extref = (struct btrfs_inode_extref *)(ptr +
5940 cur_offset);
5941 dirid = btrfs_inode_extref_parent(leaf, extref);
5942 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5943 cur_offset += ref_name_len + sizeof(*extref);
5944 if (dirid == last_dirid)
5945 continue;
5946 ret = dir_changed(sctx, dirid);
5947 if (ret)
5948 break;
5949 last_dirid = dirid;
5950 }
5951out:
5952 return ret;
5953}
5954
Alexander Block766702e2012-07-28 14:11:31 +02005955/*
5956 * Updates compare related fields in sctx and simply forwards to the actual
5957 * changed_xxx functions.
5958 */
Alexander Block31db9f72012-07-25 23:19:24 +02005959static int changed_cb(struct btrfs_root *left_root,
5960 struct btrfs_root *right_root,
5961 struct btrfs_path *left_path,
5962 struct btrfs_path *right_path,
5963 struct btrfs_key *key,
5964 enum btrfs_compare_tree_result result,
5965 void *ctx)
5966{
5967 int ret = 0;
5968 struct send_ctx *sctx = ctx;
5969
Josef Bacikba5e8f22013-08-16 16:52:55 -04005970 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005971 if (key->type == BTRFS_INODE_REF_KEY ||
5972 key->type == BTRFS_INODE_EXTREF_KEY) {
5973 ret = compare_refs(sctx, left_path, key);
5974 if (!ret)
5975 return 0;
5976 if (ret < 0)
5977 return ret;
5978 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5979 return maybe_send_hole(sctx, left_path, key);
5980 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005981 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005982 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005983 result = BTRFS_COMPARE_TREE_CHANGED;
5984 ret = 0;
5985 }
5986
Alexander Block31db9f72012-07-25 23:19:24 +02005987 sctx->left_path = left_path;
5988 sctx->right_path = right_path;
5989 sctx->cmp_key = key;
5990
5991 ret = finish_inode_if_needed(sctx, 0);
5992 if (ret < 0)
5993 goto out;
5994
Alexander Block2981e222012-08-01 14:47:03 +02005995 /* Ignore non-FS objects */
5996 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5997 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5998 goto out;
5999
Alexander Block31db9f72012-07-25 23:19:24 +02006000 if (key->type == BTRFS_INODE_ITEM_KEY)
6001 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00006002 else if (key->type == BTRFS_INODE_REF_KEY ||
6003 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02006004 ret = changed_ref(sctx, result);
6005 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6006 ret = changed_xattr(sctx, result);
6007 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6008 ret = changed_extent(sctx, result);
6009
6010out:
6011 return ret;
6012}
6013
6014static int full_send_tree(struct send_ctx *sctx)
6015{
6016 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006017 struct btrfs_root *send_root = sctx->send_root;
6018 struct btrfs_key key;
6019 struct btrfs_key found_key;
6020 struct btrfs_path *path;
6021 struct extent_buffer *eb;
6022 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006023
6024 path = alloc_path_for_send();
6025 if (!path)
6026 return -ENOMEM;
6027
Alexander Block31db9f72012-07-25 23:19:24 +02006028 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6029 key.type = BTRFS_INODE_ITEM_KEY;
6030 key.offset = 0;
6031
Alexander Block31db9f72012-07-25 23:19:24 +02006032 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6033 if (ret < 0)
6034 goto out;
6035 if (ret)
6036 goto out_finish;
6037
6038 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006039 eb = path->nodes[0];
6040 slot = path->slots[0];
6041 btrfs_item_key_to_cpu(eb, &found_key, slot);
6042
6043 ret = changed_cb(send_root, NULL, path, NULL,
6044 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
6045 if (ret < 0)
6046 goto out;
6047
6048 key.objectid = found_key.objectid;
6049 key.type = found_key.type;
6050 key.offset = found_key.offset + 1;
6051
6052 ret = btrfs_next_item(send_root, path);
6053 if (ret < 0)
6054 goto out;
6055 if (ret) {
6056 ret = 0;
6057 break;
6058 }
6059 }
6060
6061out_finish:
6062 ret = finish_inode_if_needed(sctx, 1);
6063
6064out:
6065 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006066 return ret;
6067}
6068
6069static int send_subvol(struct send_ctx *sctx)
6070{
6071 int ret;
6072
Stefan Behrensc2c71322013-04-10 17:10:52 +00006073 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6074 ret = send_header(sctx);
6075 if (ret < 0)
6076 goto out;
6077 }
Alexander Block31db9f72012-07-25 23:19:24 +02006078
6079 ret = send_subvol_begin(sctx);
6080 if (ret < 0)
6081 goto out;
6082
6083 if (sctx->parent_root) {
6084 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6085 changed_cb, sctx);
6086 if (ret < 0)
6087 goto out;
6088 ret = finish_inode_if_needed(sctx, 1);
6089 if (ret < 0)
6090 goto out;
6091 } else {
6092 ret = full_send_tree(sctx);
6093 if (ret < 0)
6094 goto out;
6095 }
6096
6097out:
Alexander Block31db9f72012-07-25 23:19:24 +02006098 free_recorded_refs(sctx);
6099 return ret;
6100}
6101
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006102/*
6103 * If orphan cleanup did remove any orphans from a root, it means the tree
6104 * was modified and therefore the commit root is not the same as the current
6105 * root anymore. This is a problem, because send uses the commit root and
6106 * therefore can see inode items that don't exist in the current root anymore,
6107 * and for example make calls to btrfs_iget, which will do tree lookups based
6108 * on the current root and not on the commit root. Those lookups will fail,
6109 * returning a -ESTALE error, and making send fail with that error. So make
6110 * sure a send does not see any orphans we have just removed, and that it will
6111 * see the same inodes regardless of whether a transaction commit happened
6112 * before it started (meaning that the commit root will be the same as the
6113 * current root) or not.
6114 */
6115static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6116{
6117 int i;
6118 struct btrfs_trans_handle *trans = NULL;
6119
6120again:
6121 if (sctx->parent_root &&
6122 sctx->parent_root->node != sctx->parent_root->commit_root)
6123 goto commit_trans;
6124
6125 for (i = 0; i < sctx->clone_roots_cnt; i++)
6126 if (sctx->clone_roots[i].root->node !=
6127 sctx->clone_roots[i].root->commit_root)
6128 goto commit_trans;
6129
6130 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006131 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006132
6133 return 0;
6134
6135commit_trans:
6136 /* Use any root, all fs roots will get their commit roots updated. */
6137 if (!trans) {
6138 trans = btrfs_join_transaction(sctx->send_root);
6139 if (IS_ERR(trans))
6140 return PTR_ERR(trans);
6141 goto again;
6142 }
6143
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006144 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006145}
6146
David Sterba66ef7d62013-12-17 15:07:20 +01006147static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6148{
6149 spin_lock(&root->root_item_lock);
6150 root->send_in_progress--;
6151 /*
6152 * Not much left to do, we don't know why it's unbalanced and
6153 * can't blindly reset it to 0.
6154 */
6155 if (root->send_in_progress < 0)
6156 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006157 "send_in_progres unbalanced %d root %llu",
6158 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01006159 spin_unlock(&root->root_item_lock);
6160}
6161
Alexander Block31db9f72012-07-25 23:19:24 +02006162long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
6163{
6164 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006165 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6166 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02006167 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02006168 struct btrfs_ioctl_send_args *arg = NULL;
6169 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006170 struct send_ctx *sctx = NULL;
6171 u32 i;
6172 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01006173 int clone_sources_to_rollback = 0;
David Sterbae55d1152016-04-11 18:52:02 +02006174 unsigned alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08006175 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08006176 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02006177
6178 if (!capable(CAP_SYS_ADMIN))
6179 return -EPERM;
6180
Josef Bacik139f8072013-05-20 11:26:50 -04006181 /*
David Sterba2c686532013-12-16 17:34:17 +01006182 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02006183 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01006184 */
6185 spin_lock(&send_root->root_item_lock);
6186 send_root->send_in_progress++;
6187 spin_unlock(&send_root->root_item_lock);
6188
6189 /*
Josef Bacik139f8072013-05-20 11:26:50 -04006190 * This is done when we lookup the root, it should already be complete
6191 * by the time we get here.
6192 */
6193 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6194
6195 /*
David Sterba2c686532013-12-16 17:34:17 +01006196 * Userspace tools do the checks and warn the user if it's
6197 * not RO.
6198 */
6199 if (!btrfs_root_readonly(send_root)) {
6200 ret = -EPERM;
6201 goto out;
6202 }
6203
Alexander Block31db9f72012-07-25 23:19:24 +02006204 arg = memdup_user(arg_, sizeof(*arg));
6205 if (IS_ERR(arg)) {
6206 ret = PTR_ERR(arg);
6207 arg = NULL;
6208 goto out;
6209 }
6210
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006211 if (arg->clone_sources_count >
6212 ULLONG_MAX / sizeof(*arg->clone_sources)) {
6213 ret = -EINVAL;
6214 goto out;
6215 }
6216
Alexander Block31db9f72012-07-25 23:19:24 +02006217 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05006218 sizeof(*arg->clone_sources) *
6219 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02006220 ret = -EFAULT;
6221 goto out;
6222 }
6223
Stefan Behrensc2c71322013-04-10 17:10:52 +00006224 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006225 ret = -EINVAL;
6226 goto out;
6227 }
6228
David Sterbae780b0d2016-01-18 18:42:13 +01006229 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006230 if (!sctx) {
6231 ret = -ENOMEM;
6232 goto out;
6233 }
6234
6235 INIT_LIST_HEAD(&sctx->new_refs);
6236 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01006237 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006238 INIT_LIST_HEAD(&sctx->name_cache_list);
6239
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006240 sctx->flags = arg->flags;
6241
Alexander Block31db9f72012-07-25 23:19:24 +02006242 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00006243 if (!sctx->send_filp) {
6244 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02006245 goto out;
6246 }
6247
Alexander Block31db9f72012-07-25 23:19:24 +02006248 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02006249 /*
6250 * Unlikely but possible, if the subvolume is marked for deletion but
6251 * is slow to remove the directory entry, send can still be started
6252 */
6253 if (btrfs_root_dead(sctx->send_root)) {
6254 ret = -EPERM;
6255 goto out;
6256 }
6257
Alexander Block31db9f72012-07-25 23:19:24 +02006258 sctx->clone_roots_cnt = arg->clone_sources_count;
6259
6260 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
David Sterba6ff48ce2016-04-11 18:40:08 +02006261 sctx->send_buf = kmalloc(sctx->send_max_size, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006262 if (!sctx->send_buf) {
David Sterba6ff48ce2016-04-11 18:40:08 +02006263 sctx->send_buf = vmalloc(sctx->send_max_size);
6264 if (!sctx->send_buf) {
6265 ret = -ENOMEM;
6266 goto out;
6267 }
Alexander Block31db9f72012-07-25 23:19:24 +02006268 }
6269
David Sterbaeb5b75f2016-04-11 18:40:08 +02006270 sctx->read_buf = kmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006271 if (!sctx->read_buf) {
David Sterbaeb5b75f2016-04-11 18:40:08 +02006272 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
6273 if (!sctx->read_buf) {
6274 ret = -ENOMEM;
6275 goto out;
6276 }
Alexander Block31db9f72012-07-25 23:19:24 +02006277 }
6278
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006279 sctx->pending_dir_moves = RB_ROOT;
6280 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00006281 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006282
David Sterbae55d1152016-04-11 18:52:02 +02006283 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6284
David Sterbac03d01f2016-04-11 18:40:08 +02006285 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006286 if (!sctx->clone_roots) {
David Sterbac03d01f2016-04-11 18:40:08 +02006287 sctx->clone_roots = vzalloc(alloc_size);
6288 if (!sctx->clone_roots) {
6289 ret = -ENOMEM;
6290 goto out;
6291 }
Alexander Block31db9f72012-07-25 23:19:24 +02006292 }
6293
David Sterbae55d1152016-04-11 18:52:02 +02006294 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6295
Alexander Block31db9f72012-07-25 23:19:24 +02006296 if (arg->clone_sources_count) {
David Sterba2f913062016-04-11 18:40:08 +02006297 clone_sources_tmp = kmalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006298 if (!clone_sources_tmp) {
David Sterba2f913062016-04-11 18:40:08 +02006299 clone_sources_tmp = vmalloc(alloc_size);
6300 if (!clone_sources_tmp) {
6301 ret = -ENOMEM;
6302 goto out;
6303 }
Alexander Block31db9f72012-07-25 23:19:24 +02006304 }
6305
6306 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02006307 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02006308 if (ret) {
6309 ret = -EFAULT;
6310 goto out;
6311 }
6312
6313 for (i = 0; i < arg->clone_sources_count; i++) {
6314 key.objectid = clone_sources_tmp[i];
6315 key.type = BTRFS_ROOT_ITEM_KEY;
6316 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006317
6318 index = srcu_read_lock(&fs_info->subvol_srcu);
6319
Alexander Block31db9f72012-07-25 23:19:24 +02006320 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02006321 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006322 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006323 ret = PTR_ERR(clone_root);
6324 goto out;
6325 }
David Sterba2c686532013-12-16 17:34:17 +01006326 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00006327 if (!btrfs_root_readonly(clone_root) ||
6328 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006329 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006330 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006331 ret = -EPERM;
6332 goto out;
6333 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00006334 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01006335 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006336 srcu_read_unlock(&fs_info->subvol_srcu, index);
6337
Alexander Block31db9f72012-07-25 23:19:24 +02006338 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00006339 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006340 }
David Sterba2f913062016-04-11 18:40:08 +02006341 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006342 clone_sources_tmp = NULL;
6343 }
6344
6345 if (arg->parent_root) {
6346 key.objectid = arg->parent_root;
6347 key.type = BTRFS_ROOT_ITEM_KEY;
6348 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006349
6350 index = srcu_read_lock(&fs_info->subvol_srcu);
6351
Alexander Block31db9f72012-07-25 23:19:24 +02006352 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006353 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006354 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006355 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02006356 goto out;
6357 }
Wang Shilong18f687d2014-01-07 17:25:19 +08006358
David Sterba2c686532013-12-16 17:34:17 +01006359 spin_lock(&sctx->parent_root->root_item_lock);
6360 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02006361 if (!btrfs_root_readonly(sctx->parent_root) ||
6362 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006363 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006364 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006365 ret = -EPERM;
6366 goto out;
6367 }
6368 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006369
6370 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006371 }
6372
6373 /*
6374 * Clones from send_root are allowed, but only if the clone source
6375 * is behind the current send position. This is checked while searching
6376 * for possible clone sources.
6377 */
6378 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6379
6380 /* We do a bsearch later */
6381 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6382 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6383 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08006384 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006385
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006386 ret = ensure_commit_roots_uptodate(sctx);
6387 if (ret)
6388 goto out;
6389
David Sterba2755a0d2014-07-31 00:43:18 +02006390 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02006391 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04006392 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02006393 if (ret < 0)
6394 goto out;
6395
Stefan Behrensc2c71322013-04-10 17:10:52 +00006396 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6397 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6398 if (ret < 0)
6399 goto out;
6400 ret = send_cmd(sctx);
6401 if (ret < 0)
6402 goto out;
6403 }
Alexander Block31db9f72012-07-25 23:19:24 +02006404
6405out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006406 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6407 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6408 struct rb_node *n;
6409 struct pending_dir_move *pm;
6410
6411 n = rb_first(&sctx->pending_dir_moves);
6412 pm = rb_entry(n, struct pending_dir_move, node);
6413 while (!list_empty(&pm->list)) {
6414 struct pending_dir_move *pm2;
6415
6416 pm2 = list_first_entry(&pm->list,
6417 struct pending_dir_move, list);
6418 free_pending_move(sctx, pm2);
6419 }
6420 free_pending_move(sctx, pm);
6421 }
6422
6423 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6424 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6425 struct rb_node *n;
6426 struct waiting_dir_move *dm;
6427
6428 n = rb_first(&sctx->waiting_dir_moves);
6429 dm = rb_entry(n, struct waiting_dir_move, node);
6430 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6431 kfree(dm);
6432 }
6433
Filipe Manana9dc44212014-02-19 14:31:44 +00006434 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6435 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6436 struct rb_node *n;
6437 struct orphan_dir_info *odi;
6438
6439 n = rb_first(&sctx->orphan_dirs);
6440 odi = rb_entry(n, struct orphan_dir_info, node);
6441 free_orphan_dir_info(sctx, odi);
6442 }
6443
Wang Shilong896c14f2014-01-07 17:25:18 +08006444 if (sort_clone_roots) {
6445 for (i = 0; i < sctx->clone_roots_cnt; i++)
6446 btrfs_root_dec_send_in_progress(
6447 sctx->clone_roots[i].root);
6448 } else {
6449 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6450 btrfs_root_dec_send_in_progress(
6451 sctx->clone_roots[i].root);
6452
6453 btrfs_root_dec_send_in_progress(send_root);
6454 }
David Sterba66ef7d62013-12-17 15:07:20 +01006455 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6456 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01006457
Alexander Block31db9f72012-07-25 23:19:24 +02006458 kfree(arg);
David Sterba2f913062016-04-11 18:40:08 +02006459 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006460
6461 if (sctx) {
6462 if (sctx->send_filp)
6463 fput(sctx->send_filp);
6464
David Sterbac03d01f2016-04-11 18:40:08 +02006465 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02006466 kvfree(sctx->send_buf);
David Sterbaeb5b75f2016-04-11 18:40:08 +02006467 kvfree(sctx->read_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02006468
6469 name_cache_free(sctx);
6470
6471 kfree(sctx);
6472 }
6473
6474 return ret;
6475}