blob: 79dc3ee1de58a1741a6e43329dae5c247c0acf4a [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 Bo2131bcd32014-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 }
Filipe Manana4395e0c2014-08-20 10:45:45 +01001057 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001058 ret = -E2BIG;
1059 goto out;
1060 }
1061 } else {
1062 /*
1063 * Path too long
1064 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001065 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001066 ret = -ENAMETOOLONG;
1067 goto out;
1068 }
Alexander Block31db9f72012-07-25 23:19:24 +02001069 }
1070
Filipe Manana4395e0c2014-08-20 10:45:45 +01001071 if (name_len + data_len > buf_len) {
1072 buf_len = name_len + data_len;
1073 if (is_vmalloc_addr(buf)) {
1074 vfree(buf);
1075 buf = NULL;
1076 } else {
1077 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001078 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001079
1080 if (!tmp)
1081 kfree(buf);
1082 buf = tmp;
1083 }
1084 if (!buf) {
1085 buf = vmalloc(buf_len);
1086 if (!buf) {
1087 ret = -ENOMEM;
1088 goto out;
1089 }
1090 }
1091 }
1092
Alexander Block31db9f72012-07-25 23:19:24 +02001093 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1094 name_len + data_len);
1095
1096 len = sizeof(*di) + name_len + data_len;
1097 di = (struct btrfs_dir_item *)((char *)di + len);
1098 cur += len;
1099
1100 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1101 data_len, type, ctx);
1102 if (ret < 0)
1103 goto out;
1104 if (ret) {
1105 ret = 0;
1106 goto out;
1107 }
1108
1109 num++;
1110 }
1111
1112out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001113 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001114 return ret;
1115}
1116
1117static int __copy_first_ref(int num, u64 dir, int index,
1118 struct fs_path *p, void *ctx)
1119{
1120 int ret;
1121 struct fs_path *pt = ctx;
1122
1123 ret = fs_path_copy(pt, p);
1124 if (ret < 0)
1125 return ret;
1126
1127 /* we want the first only */
1128 return 1;
1129}
1130
1131/*
1132 * Retrieve the first path of an inode. If an inode has more then one
1133 * ref/hardlink, this is ignored.
1134 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001135static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001136 u64 ino, struct fs_path *path)
1137{
1138 int ret;
1139 struct btrfs_key key, found_key;
1140 struct btrfs_path *p;
1141
1142 p = alloc_path_for_send();
1143 if (!p)
1144 return -ENOMEM;
1145
1146 fs_path_reset(path);
1147
1148 key.objectid = ino;
1149 key.type = BTRFS_INODE_REF_KEY;
1150 key.offset = 0;
1151
1152 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1153 if (ret < 0)
1154 goto out;
1155 if (ret) {
1156 ret = 1;
1157 goto out;
1158 }
1159 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1160 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001161 (found_key.type != BTRFS_INODE_REF_KEY &&
1162 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001163 ret = -ENOENT;
1164 goto out;
1165 }
1166
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001167 ret = iterate_inode_ref(root, p, &found_key, 1,
1168 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001169 if (ret < 0)
1170 goto out;
1171 ret = 0;
1172
1173out:
1174 btrfs_free_path(p);
1175 return ret;
1176}
1177
1178struct backref_ctx {
1179 struct send_ctx *sctx;
1180
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001181 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001182 /* number of total found references */
1183 u64 found;
1184
1185 /*
1186 * used for clones found in send_root. clones found behind cur_objectid
1187 * and cur_offset are not considered as allowed clones.
1188 */
1189 u64 cur_objectid;
1190 u64 cur_offset;
1191
1192 /* may be truncated in case it's the last extent in a file */
1193 u64 extent_len;
1194
Filipe Manana619d8c42015-05-03 01:56:00 +01001195 /* data offset in the file extent item */
1196 u64 data_offset;
1197
Alexander Block31db9f72012-07-25 23:19:24 +02001198 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001199 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001200};
1201
1202static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1203{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001204 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001205 struct clone_root *cr = (struct clone_root *)elt;
1206
1207 if (root < cr->root->objectid)
1208 return -1;
1209 if (root > cr->root->objectid)
1210 return 1;
1211 return 0;
1212}
1213
1214static int __clone_root_cmp_sort(const void *e1, const void *e2)
1215{
1216 struct clone_root *cr1 = (struct clone_root *)e1;
1217 struct clone_root *cr2 = (struct clone_root *)e2;
1218
1219 if (cr1->root->objectid < cr2->root->objectid)
1220 return -1;
1221 if (cr1->root->objectid > cr2->root->objectid)
1222 return 1;
1223 return 0;
1224}
1225
1226/*
1227 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001228 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001229 */
1230static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1231{
1232 struct backref_ctx *bctx = ctx_;
1233 struct clone_root *found;
1234 int ret;
1235 u64 i_size;
1236
1237 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001238 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001239 bctx->sctx->clone_roots_cnt,
1240 sizeof(struct clone_root),
1241 __clone_root_cmp_bsearch);
1242 if (!found)
1243 return 0;
1244
1245 if (found->root == bctx->sctx->send_root &&
1246 ino == bctx->cur_objectid &&
1247 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001248 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001249 }
1250
1251 /*
Alexander Block766702e2012-07-28 14:11:31 +02001252 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001253 * accept clones from these extents.
1254 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001255 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1256 NULL, NULL, NULL);
1257 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001258 if (ret < 0)
1259 return ret;
1260
Filipe Manana619d8c42015-05-03 01:56:00 +01001261 if (offset + bctx->data_offset + bctx->extent_len > i_size)
Alexander Block31db9f72012-07-25 23:19:24 +02001262 return 0;
1263
1264 /*
1265 * Make sure we don't consider clones from send_root that are
1266 * behind the current inode/offset.
1267 */
1268 if (found->root == bctx->sctx->send_root) {
1269 /*
1270 * TODO for the moment we don't accept clones from the inode
1271 * that is currently send. We may change this when
1272 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1273 * file.
1274 */
1275 if (ino >= bctx->cur_objectid)
1276 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001277#if 0
1278 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001279 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001280 if (offset + bctx->extent_len > bctx->cur_offset)
1281 return 0;
1282#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001283 }
1284
1285 bctx->found++;
1286 found->found_refs++;
1287 if (ino < found->ino) {
1288 found->ino = ino;
1289 found->offset = offset;
1290 } else if (found->ino == ino) {
1291 /*
1292 * same extent found more then once in the same file.
1293 */
1294 if (found->offset > offset + bctx->extent_len)
1295 found->offset = offset;
1296 }
1297
1298 return 0;
1299}
1300
1301/*
Alexander Block766702e2012-07-28 14:11:31 +02001302 * Given an inode, offset and extent item, it finds a good clone for a clone
1303 * instruction. Returns -ENOENT when none could be found. The function makes
1304 * sure that the returned clone is usable at the point where sending is at the
1305 * moment. This means, that no clones are accepted which lie behind the current
1306 * inode+offset.
1307 *
Alexander Block31db9f72012-07-25 23:19:24 +02001308 * path must point to the extent item when called.
1309 */
1310static int find_extent_clone(struct send_ctx *sctx,
1311 struct btrfs_path *path,
1312 u64 ino, u64 data_offset,
1313 u64 ino_size,
1314 struct clone_root **found)
1315{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001316 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001317 int ret;
1318 int extent_type;
1319 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001320 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001321 u64 num_bytes;
1322 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001323 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001324 struct btrfs_file_extent_item *fi;
1325 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001326 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001327 struct clone_root *cur_clone_root;
1328 struct btrfs_key found_key;
1329 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001330 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001331 u32 i;
1332
1333 tmp_path = alloc_path_for_send();
1334 if (!tmp_path)
1335 return -ENOMEM;
1336
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001337 /* We only use this path under the commit sem */
1338 tmp_path->need_commit_sem = 0;
1339
David Sterbae780b0d2016-01-18 18:42:13 +01001340 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001341 if (!backref_ctx) {
1342 ret = -ENOMEM;
1343 goto out;
1344 }
1345
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001346 backref_ctx->path = tmp_path;
1347
Alexander Block31db9f72012-07-25 23:19:24 +02001348 if (data_offset >= ino_size) {
1349 /*
1350 * There may be extents that lie behind the file's size.
1351 * I at least had this in combination with snapshotting while
1352 * writing large files.
1353 */
1354 ret = 0;
1355 goto out;
1356 }
1357
1358 fi = btrfs_item_ptr(eb, path->slots[0],
1359 struct btrfs_file_extent_item);
1360 extent_type = btrfs_file_extent_type(eb, fi);
1361 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1362 ret = -ENOENT;
1363 goto out;
1364 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001365 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001366
1367 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001368 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1369 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001370 ret = -ENOENT;
1371 goto out;
1372 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001373 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001374
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001375 down_read(&fs_info->commit_root_sem);
1376 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001377 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001378 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001379 btrfs_release_path(tmp_path);
1380
1381 if (ret < 0)
1382 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001383 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001384 ret = -EIO;
1385 goto out;
1386 }
1387
1388 /*
1389 * Setup the clone roots.
1390 */
1391 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1392 cur_clone_root = sctx->clone_roots + i;
1393 cur_clone_root->ino = (u64)-1;
1394 cur_clone_root->offset = 0;
1395 cur_clone_root->found_refs = 0;
1396 }
1397
Alexander Block35075bb2012-07-28 12:44:34 +02001398 backref_ctx->sctx = sctx;
1399 backref_ctx->found = 0;
1400 backref_ctx->cur_objectid = ino;
1401 backref_ctx->cur_offset = data_offset;
1402 backref_ctx->found_itself = 0;
1403 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001404 /*
1405 * For non-compressed extents iterate_extent_inodes() gives us extent
1406 * offsets that already take into account the data offset, but not for
1407 * compressed extents, since the offset is logical and not relative to
1408 * the physical extent locations. We must take this into account to
1409 * avoid sending clone offsets that go beyond the source file's size,
1410 * which would result in the clone ioctl failing with -EINVAL on the
1411 * receiving end.
1412 */
1413 if (compressed == BTRFS_COMPRESS_NONE)
1414 backref_ctx->data_offset = 0;
1415 else
1416 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001417
1418 /*
1419 * The last extent of a file may be too large due to page alignment.
1420 * We need to adjust extent_len in this case so that the checks in
1421 * __iterate_backrefs work.
1422 */
1423 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001424 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001425
1426 /*
1427 * Now collect all backrefs.
1428 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001429 if (compressed == BTRFS_COMPRESS_NONE)
1430 extent_item_pos = logical - found_key.objectid;
1431 else
1432 extent_item_pos = 0;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001433 ret = iterate_extent_inodes(fs_info,
Alexander Block31db9f72012-07-25 23:19:24 +02001434 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001435 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001436
Alexander Block31db9f72012-07-25 23:19:24 +02001437 if (ret < 0)
1438 goto out;
1439
Alexander Block35075bb2012-07-28 12:44:34 +02001440 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001441 /* found a bug in backref code? */
1442 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001443 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001444 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001445 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001446 goto out;
1447 }
1448
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001449 btrfs_debug(fs_info,
1450 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1451 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001452
Alexander Block35075bb2012-07-28 12:44:34 +02001453 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001454 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001455
1456 cur_clone_root = NULL;
1457 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1458 if (sctx->clone_roots[i].found_refs) {
1459 if (!cur_clone_root)
1460 cur_clone_root = sctx->clone_roots + i;
1461 else if (sctx->clone_roots[i].root == sctx->send_root)
1462 /* prefer clones from send_root over others */
1463 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001464 }
1465
1466 }
1467
1468 if (cur_clone_root) {
1469 *found = cur_clone_root;
1470 ret = 0;
1471 } else {
1472 ret = -ENOENT;
1473 }
1474
1475out:
1476 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001477 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001478 return ret;
1479}
1480
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001481static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001482 u64 ino,
1483 struct fs_path *dest)
1484{
1485 int ret;
1486 struct btrfs_path *path;
1487 struct btrfs_key key;
1488 struct btrfs_file_extent_item *ei;
1489 u8 type;
1490 u8 compression;
1491 unsigned long off;
1492 int len;
1493
1494 path = alloc_path_for_send();
1495 if (!path)
1496 return -ENOMEM;
1497
1498 key.objectid = ino;
1499 key.type = BTRFS_EXTENT_DATA_KEY;
1500 key.offset = 0;
1501 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1502 if (ret < 0)
1503 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001504 if (ret) {
1505 /*
1506 * An empty symlink inode. Can happen in rare error paths when
1507 * creating a symlink (transaction committed before the inode
1508 * eviction handler removed the symlink inode items and a crash
1509 * happened in between or the subvol was snapshoted in between).
1510 * Print an informative message to dmesg/syslog so that the user
1511 * can delete the symlink.
1512 */
1513 btrfs_err(root->fs_info,
1514 "Found empty symlink inode %llu at root %llu",
1515 ino, root->root_key.objectid);
1516 ret = -EIO;
1517 goto out;
1518 }
Alexander Block31db9f72012-07-25 23:19:24 +02001519
1520 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1521 struct btrfs_file_extent_item);
1522 type = btrfs_file_extent_type(path->nodes[0], ei);
1523 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1524 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1525 BUG_ON(compression);
1526
1527 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001528 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001529
1530 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001531
1532out:
1533 btrfs_free_path(path);
1534 return ret;
1535}
1536
1537/*
1538 * Helper function to generate a file name that is unique in the root of
1539 * send_root and parent_root. This is used to generate names for orphan inodes.
1540 */
1541static int gen_unique_name(struct send_ctx *sctx,
1542 u64 ino, u64 gen,
1543 struct fs_path *dest)
1544{
1545 int ret = 0;
1546 struct btrfs_path *path;
1547 struct btrfs_dir_item *di;
1548 char tmp[64];
1549 int len;
1550 u64 idx = 0;
1551
1552 path = alloc_path_for_send();
1553 if (!path)
1554 return -ENOMEM;
1555
1556 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001557 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001558 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001559 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001560
1561 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1562 path, BTRFS_FIRST_FREE_OBJECTID,
1563 tmp, strlen(tmp), 0);
1564 btrfs_release_path(path);
1565 if (IS_ERR(di)) {
1566 ret = PTR_ERR(di);
1567 goto out;
1568 }
1569 if (di) {
1570 /* not unique, try again */
1571 idx++;
1572 continue;
1573 }
1574
1575 if (!sctx->parent_root) {
1576 /* unique */
1577 ret = 0;
1578 break;
1579 }
1580
1581 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1582 path, BTRFS_FIRST_FREE_OBJECTID,
1583 tmp, strlen(tmp), 0);
1584 btrfs_release_path(path);
1585 if (IS_ERR(di)) {
1586 ret = PTR_ERR(di);
1587 goto out;
1588 }
1589 if (di) {
1590 /* not unique, try again */
1591 idx++;
1592 continue;
1593 }
1594 /* unique */
1595 break;
1596 }
1597
1598 ret = fs_path_add(dest, tmp, strlen(tmp));
1599
1600out:
1601 btrfs_free_path(path);
1602 return ret;
1603}
1604
1605enum inode_state {
1606 inode_state_no_change,
1607 inode_state_will_create,
1608 inode_state_did_create,
1609 inode_state_will_delete,
1610 inode_state_did_delete,
1611};
1612
1613static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1614{
1615 int ret;
1616 int left_ret;
1617 int right_ret;
1618 u64 left_gen;
1619 u64 right_gen;
1620
1621 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001622 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001623 if (ret < 0 && ret != -ENOENT)
1624 goto out;
1625 left_ret = ret;
1626
1627 if (!sctx->parent_root) {
1628 right_ret = -ENOENT;
1629 } else {
1630 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001631 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001632 if (ret < 0 && ret != -ENOENT)
1633 goto out;
1634 right_ret = ret;
1635 }
1636
1637 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001638 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001639 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001640 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001641 if (ino < sctx->send_progress)
1642 ret = inode_state_did_create;
1643 else
1644 ret = inode_state_will_create;
1645 } else if (right_gen == gen) {
1646 if (ino < sctx->send_progress)
1647 ret = inode_state_did_delete;
1648 else
1649 ret = inode_state_will_delete;
1650 } else {
1651 ret = -ENOENT;
1652 }
1653 } else if (!left_ret) {
1654 if (left_gen == gen) {
1655 if (ino < sctx->send_progress)
1656 ret = inode_state_did_create;
1657 else
1658 ret = inode_state_will_create;
1659 } else {
1660 ret = -ENOENT;
1661 }
1662 } else if (!right_ret) {
1663 if (right_gen == gen) {
1664 if (ino < sctx->send_progress)
1665 ret = inode_state_did_delete;
1666 else
1667 ret = inode_state_will_delete;
1668 } else {
1669 ret = -ENOENT;
1670 }
1671 } else {
1672 ret = -ENOENT;
1673 }
1674
1675out:
1676 return ret;
1677}
1678
1679static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1680{
1681 int ret;
1682
Robbie Koe215b6b2017-01-05 16:24:55 +08001683 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1684 return 1;
1685
Alexander Block31db9f72012-07-25 23:19:24 +02001686 ret = get_cur_inode_state(sctx, ino, gen);
1687 if (ret < 0)
1688 goto out;
1689
1690 if (ret == inode_state_no_change ||
1691 ret == inode_state_did_create ||
1692 ret == inode_state_will_delete)
1693 ret = 1;
1694 else
1695 ret = 0;
1696
1697out:
1698 return ret;
1699}
1700
1701/*
1702 * Helper function to lookup a dir item in a dir.
1703 */
1704static int lookup_dir_item_inode(struct btrfs_root *root,
1705 u64 dir, const char *name, int name_len,
1706 u64 *found_inode,
1707 u8 *found_type)
1708{
1709 int ret = 0;
1710 struct btrfs_dir_item *di;
1711 struct btrfs_key key;
1712 struct btrfs_path *path;
1713
1714 path = alloc_path_for_send();
1715 if (!path)
1716 return -ENOMEM;
1717
1718 di = btrfs_lookup_dir_item(NULL, root, path,
1719 dir, name, name_len, 0);
1720 if (!di) {
1721 ret = -ENOENT;
1722 goto out;
1723 }
1724 if (IS_ERR(di)) {
1725 ret = PTR_ERR(di);
1726 goto out;
1727 }
1728 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001729 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1730 ret = -ENOENT;
1731 goto out;
1732 }
Alexander Block31db9f72012-07-25 23:19:24 +02001733 *found_inode = key.objectid;
1734 *found_type = btrfs_dir_type(path->nodes[0], di);
1735
1736out:
1737 btrfs_free_path(path);
1738 return ret;
1739}
1740
Alexander Block766702e2012-07-28 14:11:31 +02001741/*
1742 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1743 * generation of the parent dir and the name of the dir entry.
1744 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001745static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001746 u64 *dir, u64 *dir_gen, struct fs_path *name)
1747{
1748 int ret;
1749 struct btrfs_key key;
1750 struct btrfs_key found_key;
1751 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001752 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001753 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001754
1755 path = alloc_path_for_send();
1756 if (!path)
1757 return -ENOMEM;
1758
1759 key.objectid = ino;
1760 key.type = BTRFS_INODE_REF_KEY;
1761 key.offset = 0;
1762
1763 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1764 if (ret < 0)
1765 goto out;
1766 if (!ret)
1767 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1768 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001769 if (ret || found_key.objectid != ino ||
1770 (found_key.type != BTRFS_INODE_REF_KEY &&
1771 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001772 ret = -ENOENT;
1773 goto out;
1774 }
1775
Filipe Manana51a60252014-05-13 22:01:02 +01001776 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001777 struct btrfs_inode_ref *iref;
1778 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1779 struct btrfs_inode_ref);
1780 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1781 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1782 (unsigned long)(iref + 1),
1783 len);
1784 parent_dir = found_key.offset;
1785 } else {
1786 struct btrfs_inode_extref *extref;
1787 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1788 struct btrfs_inode_extref);
1789 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1790 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1791 (unsigned long)&extref->name, len);
1792 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1793 }
Alexander Block31db9f72012-07-25 23:19:24 +02001794 if (ret < 0)
1795 goto out;
1796 btrfs_release_path(path);
1797
Filipe Mananab46ab972014-03-21 12:46:54 +00001798 if (dir_gen) {
1799 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1800 NULL, NULL, NULL);
1801 if (ret < 0)
1802 goto out;
1803 }
Alexander Block31db9f72012-07-25 23:19:24 +02001804
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001805 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001806
1807out:
1808 btrfs_free_path(path);
1809 return ret;
1810}
1811
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001812static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001813 u64 ino, u64 dir,
1814 const char *name, int name_len)
1815{
1816 int ret;
1817 struct fs_path *tmp_name;
1818 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001819
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001820 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001821 if (!tmp_name)
1822 return -ENOMEM;
1823
Filipe Mananab46ab972014-03-21 12:46:54 +00001824 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001825 if (ret < 0)
1826 goto out;
1827
Alexander Blockb9291af2012-07-28 11:07:18 +02001828 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001829 ret = 0;
1830 goto out;
1831 }
1832
Alexander Blocke938c8a2012-07-28 16:33:49 +02001833 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001834
1835out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001836 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001837 return ret;
1838}
1839
Alexander Block766702e2012-07-28 14:11:31 +02001840/*
1841 * Used by process_recorded_refs to determine if a new ref would overwrite an
1842 * already existing ref. In case it detects an overwrite, it returns the
1843 * inode/gen in who_ino/who_gen.
1844 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1845 * to make sure later references to the overwritten inode are possible.
1846 * Orphanizing is however only required for the first ref of an inode.
1847 * process_recorded_refs does an additional is_first_ref check to see if
1848 * orphanizing is really required.
1849 */
Alexander Block31db9f72012-07-25 23:19:24 +02001850static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1851 const char *name, int name_len,
1852 u64 *who_ino, u64 *who_gen)
1853{
1854 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001855 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001856 u64 other_inode = 0;
1857 u8 other_type = 0;
1858
1859 if (!sctx->parent_root)
1860 goto out;
1861
1862 ret = is_inode_existent(sctx, dir, dir_gen);
1863 if (ret <= 0)
1864 goto out;
1865
Josef Bacikebdad912013-08-06 16:47:48 -04001866 /*
1867 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001868 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001869 * and we can just unlink this entry.
1870 */
Robbie Koe215b6b2017-01-05 16:24:55 +08001871 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001872 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1873 NULL, NULL, NULL);
1874 if (ret < 0 && ret != -ENOENT)
1875 goto out;
1876 if (ret) {
1877 ret = 0;
1878 goto out;
1879 }
1880 if (gen != dir_gen)
1881 goto out;
1882 }
1883
Alexander Block31db9f72012-07-25 23:19:24 +02001884 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1885 &other_inode, &other_type);
1886 if (ret < 0 && ret != -ENOENT)
1887 goto out;
1888 if (ret) {
1889 ret = 0;
1890 goto out;
1891 }
1892
Alexander Block766702e2012-07-28 14:11:31 +02001893 /*
1894 * Check if the overwritten ref was already processed. If yes, the ref
1895 * was already unlinked/moved, so we can safely assume that we will not
1896 * overwrite anything at this point in time.
1897 */
Robbie Ko801bec32015-06-23 18:39:46 +08001898 if (other_inode > sctx->send_progress ||
1899 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001900 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001901 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001902 if (ret < 0)
1903 goto out;
1904
1905 ret = 1;
1906 *who_ino = other_inode;
1907 } else {
1908 ret = 0;
1909 }
1910
1911out:
1912 return ret;
1913}
1914
Alexander Block766702e2012-07-28 14:11:31 +02001915/*
1916 * Checks if the ref was overwritten by an already processed inode. This is
1917 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1918 * thus the orphan name needs be used.
1919 * process_recorded_refs also uses it to avoid unlinking of refs that were
1920 * overwritten.
1921 */
Alexander Block31db9f72012-07-25 23:19:24 +02001922static int did_overwrite_ref(struct send_ctx *sctx,
1923 u64 dir, u64 dir_gen,
1924 u64 ino, u64 ino_gen,
1925 const char *name, int name_len)
1926{
1927 int ret = 0;
1928 u64 gen;
1929 u64 ow_inode;
1930 u8 other_type;
1931
1932 if (!sctx->parent_root)
1933 goto out;
1934
1935 ret = is_inode_existent(sctx, dir, dir_gen);
1936 if (ret <= 0)
1937 goto out;
1938
1939 /* check if the ref was overwritten by another ref */
1940 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1941 &ow_inode, &other_type);
1942 if (ret < 0 && ret != -ENOENT)
1943 goto out;
1944 if (ret) {
1945 /* was never and will never be overwritten */
1946 ret = 0;
1947 goto out;
1948 }
1949
1950 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001951 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001952 if (ret < 0)
1953 goto out;
1954
1955 if (ow_inode == ino && gen == ino_gen) {
1956 ret = 0;
1957 goto out;
1958 }
1959
Filipe Manana8b191a62015-04-09 14:09:14 +01001960 /*
1961 * We know that it is or will be overwritten. Check this now.
1962 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001963 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1964 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001965 */
Filipe Mananab786f162015-09-26 15:30:23 +01001966 if ((ow_inode < sctx->send_progress) ||
1967 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1968 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001969 ret = 1;
1970 else
1971 ret = 0;
1972
1973out:
1974 return ret;
1975}
1976
Alexander Block766702e2012-07-28 14:11:31 +02001977/*
1978 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1979 * that got overwritten. This is used by process_recorded_refs to determine
1980 * if it has to use the path as returned by get_cur_path or the orphan name.
1981 */
Alexander Block31db9f72012-07-25 23:19:24 +02001982static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1983{
1984 int ret = 0;
1985 struct fs_path *name = NULL;
1986 u64 dir;
1987 u64 dir_gen;
1988
1989 if (!sctx->parent_root)
1990 goto out;
1991
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001992 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001993 if (!name)
1994 return -ENOMEM;
1995
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001996 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001997 if (ret < 0)
1998 goto out;
1999
2000 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2001 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02002002
2003out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002004 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002005 return ret;
2006}
2007
Alexander Block766702e2012-07-28 14:11:31 +02002008/*
2009 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2010 * so we need to do some special handling in case we have clashes. This function
2011 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002012 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002013 */
Alexander Block31db9f72012-07-25 23:19:24 +02002014static int name_cache_insert(struct send_ctx *sctx,
2015 struct name_cache_entry *nce)
2016{
2017 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002018 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002019
Alexander Block7e0926f2012-07-28 14:20:58 +02002020 nce_head = radix_tree_lookup(&sctx->name_cache,
2021 (unsigned long)nce->ino);
2022 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002023 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002024 if (!nce_head) {
2025 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002026 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002027 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002028 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002029
Alexander Block7e0926f2012-07-28 14:20:58 +02002030 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002031 if (ret < 0) {
2032 kfree(nce_head);
2033 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002034 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002035 }
Alexander Block31db9f72012-07-25 23:19:24 +02002036 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002037 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002038 list_add_tail(&nce->list, &sctx->name_cache_list);
2039 sctx->name_cache_size++;
2040
2041 return ret;
2042}
2043
2044static void name_cache_delete(struct send_ctx *sctx,
2045 struct name_cache_entry *nce)
2046{
Alexander Block7e0926f2012-07-28 14:20:58 +02002047 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002048
Alexander Block7e0926f2012-07-28 14:20:58 +02002049 nce_head = radix_tree_lookup(&sctx->name_cache,
2050 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002051 if (!nce_head) {
2052 btrfs_err(sctx->send_root->fs_info,
2053 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2054 nce->ino, sctx->name_cache_size);
2055 }
Alexander Block31db9f72012-07-25 23:19:24 +02002056
Alexander Block7e0926f2012-07-28 14:20:58 +02002057 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002058 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002059 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002060
David Sterba57fb8912014-02-03 19:24:40 +01002061 /*
2062 * We may not get to the final release of nce_head if the lookup fails
2063 */
2064 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002065 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2066 kfree(nce_head);
2067 }
Alexander Block31db9f72012-07-25 23:19:24 +02002068}
2069
2070static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2071 u64 ino, u64 gen)
2072{
Alexander Block7e0926f2012-07-28 14:20:58 +02002073 struct list_head *nce_head;
2074 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002075
Alexander Block7e0926f2012-07-28 14:20:58 +02002076 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2077 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002078 return NULL;
2079
Alexander Block7e0926f2012-07-28 14:20:58 +02002080 list_for_each_entry(cur, nce_head, radix_list) {
2081 if (cur->ino == ino && cur->gen == gen)
2082 return cur;
2083 }
Alexander Block31db9f72012-07-25 23:19:24 +02002084 return NULL;
2085}
2086
Alexander Block766702e2012-07-28 14:11:31 +02002087/*
2088 * Removes the entry from the list and adds it back to the end. This marks the
2089 * entry as recently used so that name_cache_clean_unused does not remove it.
2090 */
Alexander Block31db9f72012-07-25 23:19:24 +02002091static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2092{
2093 list_del(&nce->list);
2094 list_add_tail(&nce->list, &sctx->name_cache_list);
2095}
2096
Alexander Block766702e2012-07-28 14:11:31 +02002097/*
2098 * Remove some entries from the beginning of name_cache_list.
2099 */
Alexander Block31db9f72012-07-25 23:19:24 +02002100static void name_cache_clean_unused(struct send_ctx *sctx)
2101{
2102 struct name_cache_entry *nce;
2103
2104 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2105 return;
2106
2107 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2108 nce = list_entry(sctx->name_cache_list.next,
2109 struct name_cache_entry, list);
2110 name_cache_delete(sctx, nce);
2111 kfree(nce);
2112 }
2113}
2114
2115static void name_cache_free(struct send_ctx *sctx)
2116{
2117 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002118
Alexander Blocke938c8a2012-07-28 16:33:49 +02002119 while (!list_empty(&sctx->name_cache_list)) {
2120 nce = list_entry(sctx->name_cache_list.next,
2121 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002122 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002123 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002124 }
2125}
2126
Alexander Block766702e2012-07-28 14:11:31 +02002127/*
2128 * Used by get_cur_path for each ref up to the root.
2129 * Returns 0 if it succeeded.
2130 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2131 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2132 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2133 * Returns <0 in case of error.
2134 */
Alexander Block31db9f72012-07-25 23:19:24 +02002135static int __get_cur_name_and_parent(struct send_ctx *sctx,
2136 u64 ino, u64 gen,
2137 u64 *parent_ino,
2138 u64 *parent_gen,
2139 struct fs_path *dest)
2140{
2141 int ret;
2142 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002143 struct name_cache_entry *nce = NULL;
2144
Alexander Block766702e2012-07-28 14:11:31 +02002145 /*
2146 * First check if we already did a call to this function with the same
2147 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2148 * return the cached result.
2149 */
Alexander Block31db9f72012-07-25 23:19:24 +02002150 nce = name_cache_search(sctx, ino, gen);
2151 if (nce) {
2152 if (ino < sctx->send_progress && nce->need_later_update) {
2153 name_cache_delete(sctx, nce);
2154 kfree(nce);
2155 nce = NULL;
2156 } else {
2157 name_cache_used(sctx, nce);
2158 *parent_ino = nce->parent_ino;
2159 *parent_gen = nce->parent_gen;
2160 ret = fs_path_add(dest, nce->name, nce->name_len);
2161 if (ret < 0)
2162 goto out;
2163 ret = nce->ret;
2164 goto out;
2165 }
2166 }
2167
Alexander Block766702e2012-07-28 14:11:31 +02002168 /*
2169 * If the inode is not existent yet, add the orphan name and return 1.
2170 * This should only happen for the parent dir that we determine in
2171 * __record_new_ref
2172 */
Alexander Block31db9f72012-07-25 23:19:24 +02002173 ret = is_inode_existent(sctx, ino, gen);
2174 if (ret < 0)
2175 goto out;
2176
2177 if (!ret) {
2178 ret = gen_unique_name(sctx, ino, gen, dest);
2179 if (ret < 0)
2180 goto out;
2181 ret = 1;
2182 goto out_cache;
2183 }
2184
Alexander Block766702e2012-07-28 14:11:31 +02002185 /*
2186 * Depending on whether the inode was already processed or not, use
2187 * send_root or parent_root for ref lookup.
2188 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002189 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002190 ret = get_first_ref(sctx->send_root, ino,
2191 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002192 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002193 ret = get_first_ref(sctx->parent_root, ino,
2194 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002195 if (ret < 0)
2196 goto out;
2197
Alexander Block766702e2012-07-28 14:11:31 +02002198 /*
2199 * Check if the ref was overwritten by an inode's ref that was processed
2200 * earlier. If yes, treat as orphan and return 1.
2201 */
Alexander Block31db9f72012-07-25 23:19:24 +02002202 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2203 dest->start, dest->end - dest->start);
2204 if (ret < 0)
2205 goto out;
2206 if (ret) {
2207 fs_path_reset(dest);
2208 ret = gen_unique_name(sctx, ino, gen, dest);
2209 if (ret < 0)
2210 goto out;
2211 ret = 1;
2212 }
2213
2214out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002215 /*
2216 * Store the result of the lookup in the name cache.
2217 */
David Sterbae780b0d2016-01-18 18:42:13 +01002218 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002219 if (!nce) {
2220 ret = -ENOMEM;
2221 goto out;
2222 }
2223
2224 nce->ino = ino;
2225 nce->gen = gen;
2226 nce->parent_ino = *parent_ino;
2227 nce->parent_gen = *parent_gen;
2228 nce->name_len = fs_path_len(dest);
2229 nce->ret = ret;
2230 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002231
2232 if (ino < sctx->send_progress)
2233 nce->need_later_update = 0;
2234 else
2235 nce->need_later_update = 1;
2236
2237 nce_ret = name_cache_insert(sctx, nce);
2238 if (nce_ret < 0)
2239 ret = nce_ret;
2240 name_cache_clean_unused(sctx);
2241
2242out:
Alexander Block31db9f72012-07-25 23:19:24 +02002243 return ret;
2244}
2245
2246/*
2247 * Magic happens here. This function returns the first ref to an inode as it
2248 * would look like while receiving the stream at this point in time.
2249 * We walk the path up to the root. For every inode in between, we check if it
2250 * was already processed/sent. If yes, we continue with the parent as found
2251 * in send_root. If not, we continue with the parent as found in parent_root.
2252 * If we encounter an inode that was deleted at this point in time, we use the
2253 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2254 * that were not created yet and overwritten inodes/refs.
2255 *
2256 * When do we have have orphan inodes:
2257 * 1. When an inode is freshly created and thus no valid refs are available yet
2258 * 2. When a directory lost all it's refs (deleted) but still has dir items
2259 * inside which were not processed yet (pending for move/delete). If anyone
2260 * tried to get the path to the dir items, it would get a path inside that
2261 * orphan directory.
2262 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2263 * of an unprocessed inode. If in that case the first ref would be
2264 * overwritten, the overwritten inode gets "orphanized". Later when we
2265 * process this overwritten inode, it is restored at a new place by moving
2266 * the orphan inode.
2267 *
2268 * sctx->send_progress tells this function at which point in time receiving
2269 * would be.
2270 */
2271static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2272 struct fs_path *dest)
2273{
2274 int ret = 0;
2275 struct fs_path *name = NULL;
2276 u64 parent_inode = 0;
2277 u64 parent_gen = 0;
2278 int stop = 0;
2279
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002280 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002281 if (!name) {
2282 ret = -ENOMEM;
2283 goto out;
2284 }
2285
2286 dest->reversed = 1;
2287 fs_path_reset(dest);
2288
2289 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002290 struct waiting_dir_move *wdm;
2291
Alexander Block31db9f72012-07-25 23:19:24 +02002292 fs_path_reset(name);
2293
Filipe Manana9dc44212014-02-19 14:31:44 +00002294 if (is_waiting_for_rm(sctx, ino)) {
2295 ret = gen_unique_name(sctx, ino, gen, name);
2296 if (ret < 0)
2297 goto out;
2298 ret = fs_path_add_path(dest, name);
2299 break;
2300 }
2301
Filipe Manana8b191a62015-04-09 14:09:14 +01002302 wdm = get_waiting_dir_move(sctx, ino);
2303 if (wdm && wdm->orphanized) {
2304 ret = gen_unique_name(sctx, ino, gen, name);
2305 stop = 1;
2306 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002307 ret = get_first_ref(sctx->parent_root, ino,
2308 &parent_inode, &parent_gen, name);
2309 } else {
2310 ret = __get_cur_name_and_parent(sctx, ino, gen,
2311 &parent_inode,
2312 &parent_gen, name);
2313 if (ret)
2314 stop = 1;
2315 }
2316
Alexander Block31db9f72012-07-25 23:19:24 +02002317 if (ret < 0)
2318 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002319
Alexander Block31db9f72012-07-25 23:19:24 +02002320 ret = fs_path_add_path(dest, name);
2321 if (ret < 0)
2322 goto out;
2323
2324 ino = parent_inode;
2325 gen = parent_gen;
2326 }
2327
2328out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002329 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002330 if (!ret)
2331 fs_path_unreverse(dest);
2332 return ret;
2333}
2334
2335/*
Alexander Block31db9f72012-07-25 23:19:24 +02002336 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2337 */
2338static int send_subvol_begin(struct send_ctx *sctx)
2339{
2340 int ret;
2341 struct btrfs_root *send_root = sctx->send_root;
2342 struct btrfs_root *parent_root = sctx->parent_root;
2343 struct btrfs_path *path;
2344 struct btrfs_key key;
2345 struct btrfs_root_ref *ref;
2346 struct extent_buffer *leaf;
2347 char *name = NULL;
2348 int namelen;
2349
Wang Shilongffcfaf82014-01-15 00:26:43 +08002350 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002351 if (!path)
2352 return -ENOMEM;
2353
David Sterbae780b0d2016-01-18 18:42:13 +01002354 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002355 if (!name) {
2356 btrfs_free_path(path);
2357 return -ENOMEM;
2358 }
2359
2360 key.objectid = send_root->objectid;
2361 key.type = BTRFS_ROOT_BACKREF_KEY;
2362 key.offset = 0;
2363
2364 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2365 &key, path, 1, 0);
2366 if (ret < 0)
2367 goto out;
2368 if (ret) {
2369 ret = -ENOENT;
2370 goto out;
2371 }
2372
2373 leaf = path->nodes[0];
2374 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2375 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2376 key.objectid != send_root->objectid) {
2377 ret = -ENOENT;
2378 goto out;
2379 }
2380 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2381 namelen = btrfs_root_ref_name_len(leaf, ref);
2382 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2383 btrfs_release_path(path);
2384
Alexander Block31db9f72012-07-25 23:19:24 +02002385 if (parent_root) {
2386 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2387 if (ret < 0)
2388 goto out;
2389 } else {
2390 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2391 if (ret < 0)
2392 goto out;
2393 }
2394
2395 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002396
2397 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2398 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2399 sctx->send_root->root_item.received_uuid);
2400 else
2401 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2402 sctx->send_root->root_item.uuid);
2403
Alexander Block31db9f72012-07-25 23:19:24 +02002404 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002405 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002406 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002407 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2408 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2409 parent_root->root_item.received_uuid);
2410 else
2411 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2412 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002413 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002414 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002415 }
2416
2417 ret = send_cmd(sctx);
2418
2419tlv_put_failure:
2420out:
2421 btrfs_free_path(path);
2422 kfree(name);
2423 return ret;
2424}
2425
2426static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2427{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002428 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002429 int ret = 0;
2430 struct fs_path *p;
2431
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002432 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002433
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002434 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002435 if (!p)
2436 return -ENOMEM;
2437
2438 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2439 if (ret < 0)
2440 goto out;
2441
2442 ret = get_cur_path(sctx, ino, gen, p);
2443 if (ret < 0)
2444 goto out;
2445 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2446 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2447
2448 ret = send_cmd(sctx);
2449
2450tlv_put_failure:
2451out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002452 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002453 return ret;
2454}
2455
2456static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2457{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002458 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002459 int ret = 0;
2460 struct fs_path *p;
2461
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002462 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002463
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002464 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002465 if (!p)
2466 return -ENOMEM;
2467
2468 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2469 if (ret < 0)
2470 goto out;
2471
2472 ret = get_cur_path(sctx, ino, gen, p);
2473 if (ret < 0)
2474 goto out;
2475 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2476 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2477
2478 ret = send_cmd(sctx);
2479
2480tlv_put_failure:
2481out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002482 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002483 return ret;
2484}
2485
2486static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2487{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002488 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002489 int ret = 0;
2490 struct fs_path *p;
2491
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002492 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2493 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002494
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002495 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002496 if (!p)
2497 return -ENOMEM;
2498
2499 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2500 if (ret < 0)
2501 goto out;
2502
2503 ret = get_cur_path(sctx, ino, gen, p);
2504 if (ret < 0)
2505 goto out;
2506 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2507 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2508 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2509
2510 ret = send_cmd(sctx);
2511
2512tlv_put_failure:
2513out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002514 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002515 return ret;
2516}
2517
2518static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2519{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002520 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002521 int ret = 0;
2522 struct fs_path *p = NULL;
2523 struct btrfs_inode_item *ii;
2524 struct btrfs_path *path = NULL;
2525 struct extent_buffer *eb;
2526 struct btrfs_key key;
2527 int slot;
2528
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002529 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002530
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002531 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002532 if (!p)
2533 return -ENOMEM;
2534
2535 path = alloc_path_for_send();
2536 if (!path) {
2537 ret = -ENOMEM;
2538 goto out;
2539 }
2540
2541 key.objectid = ino;
2542 key.type = BTRFS_INODE_ITEM_KEY;
2543 key.offset = 0;
2544 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002545 if (ret > 0)
2546 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002547 if (ret < 0)
2548 goto out;
2549
2550 eb = path->nodes[0];
2551 slot = path->slots[0];
2552 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2553
2554 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2555 if (ret < 0)
2556 goto out;
2557
2558 ret = get_cur_path(sctx, ino, gen, p);
2559 if (ret < 0)
2560 goto out;
2561 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002562 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2563 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2564 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002565 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002566
2567 ret = send_cmd(sctx);
2568
2569tlv_put_failure:
2570out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002571 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002572 btrfs_free_path(path);
2573 return ret;
2574}
2575
2576/*
2577 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2578 * a valid path yet because we did not process the refs yet. So, the inode
2579 * is created as orphan.
2580 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002581static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002582{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002583 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002584 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002585 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002586 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002587 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002588 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002589 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002590
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002591 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002592
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002593 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002594 if (!p)
2595 return -ENOMEM;
2596
Liu Bo644d1942014-02-27 17:29:01 +08002597 if (ino != sctx->cur_ino) {
2598 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2599 NULL, NULL, &rdev);
2600 if (ret < 0)
2601 goto out;
2602 } else {
2603 gen = sctx->cur_inode_gen;
2604 mode = sctx->cur_inode_mode;
2605 rdev = sctx->cur_inode_rdev;
2606 }
Alexander Block31db9f72012-07-25 23:19:24 +02002607
Alexander Blocke938c8a2012-07-28 16:33:49 +02002608 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002609 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002610 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002611 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002612 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002613 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002614 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002615 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002616 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002617 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002618 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002619 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002620 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002621 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002622 (int)(mode & S_IFMT));
2623 ret = -ENOTSUPP;
2624 goto out;
2625 }
2626
2627 ret = begin_cmd(sctx, cmd);
2628 if (ret < 0)
2629 goto out;
2630
Alexander Block1f4692d2012-07-28 10:42:24 +02002631 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002632 if (ret < 0)
2633 goto out;
2634
2635 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002636 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002637
2638 if (S_ISLNK(mode)) {
2639 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002640 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002641 if (ret < 0)
2642 goto out;
2643 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2644 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2645 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002646 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2647 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002648 }
2649
2650 ret = send_cmd(sctx);
2651 if (ret < 0)
2652 goto out;
2653
2654
2655tlv_put_failure:
2656out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002657 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002658 return ret;
2659}
2660
Alexander Block1f4692d2012-07-28 10:42:24 +02002661/*
2662 * We need some special handling for inodes that get processed before the parent
2663 * directory got created. See process_recorded_refs for details.
2664 * This function does the check if we already created the dir out of order.
2665 */
2666static int did_create_dir(struct send_ctx *sctx, u64 dir)
2667{
2668 int ret = 0;
2669 struct btrfs_path *path = NULL;
2670 struct btrfs_key key;
2671 struct btrfs_key found_key;
2672 struct btrfs_key di_key;
2673 struct extent_buffer *eb;
2674 struct btrfs_dir_item *di;
2675 int slot;
2676
2677 path = alloc_path_for_send();
2678 if (!path) {
2679 ret = -ENOMEM;
2680 goto out;
2681 }
2682
2683 key.objectid = dir;
2684 key.type = BTRFS_DIR_INDEX_KEY;
2685 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002686 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2687 if (ret < 0)
2688 goto out;
2689
Alexander Block1f4692d2012-07-28 10:42:24 +02002690 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002691 eb = path->nodes[0];
2692 slot = path->slots[0];
2693 if (slot >= btrfs_header_nritems(eb)) {
2694 ret = btrfs_next_leaf(sctx->send_root, path);
2695 if (ret < 0) {
2696 goto out;
2697 } else if (ret > 0) {
2698 ret = 0;
2699 break;
2700 }
2701 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002702 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002703
2704 btrfs_item_key_to_cpu(eb, &found_key, slot);
2705 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002706 found_key.type != key.type) {
2707 ret = 0;
2708 goto out;
2709 }
2710
2711 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2712 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2713
Josef Bacika0525412013-08-12 10:56:14 -04002714 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2715 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002716 ret = 1;
2717 goto out;
2718 }
2719
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002720 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002721 }
2722
2723out:
2724 btrfs_free_path(path);
2725 return ret;
2726}
2727
2728/*
2729 * Only creates the inode if it is:
2730 * 1. Not a directory
2731 * 2. Or a directory which was not created already due to out of order
2732 * directories. See did_create_dir and process_recorded_refs for details.
2733 */
2734static int send_create_inode_if_needed(struct send_ctx *sctx)
2735{
2736 int ret;
2737
2738 if (S_ISDIR(sctx->cur_inode_mode)) {
2739 ret = did_create_dir(sctx, sctx->cur_ino);
2740 if (ret < 0)
2741 goto out;
2742 if (ret) {
2743 ret = 0;
2744 goto out;
2745 }
2746 }
2747
2748 ret = send_create_inode(sctx, sctx->cur_ino);
2749 if (ret < 0)
2750 goto out;
2751
2752out:
2753 return ret;
2754}
2755
Alexander Block31db9f72012-07-25 23:19:24 +02002756struct recorded_ref {
2757 struct list_head list;
2758 char *dir_path;
2759 char *name;
2760 struct fs_path *full_path;
2761 u64 dir;
2762 u64 dir_gen;
2763 int dir_path_len;
2764 int name_len;
2765};
2766
2767/*
2768 * We need to process new refs before deleted refs, but compare_tree gives us
2769 * everything mixed. So we first record all refs and later process them.
2770 * This function is a helper to record one ref.
2771 */
Liu Boa4d96d62014-03-03 21:31:03 +08002772static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002773 u64 dir_gen, struct fs_path *path)
2774{
2775 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002776
David Sterbae780b0d2016-01-18 18:42:13 +01002777 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002778 if (!ref)
2779 return -ENOMEM;
2780
2781 ref->dir = dir;
2782 ref->dir_gen = dir_gen;
2783 ref->full_path = path;
2784
Andy Shevchenkoed848852013-08-21 10:32:13 +03002785 ref->name = (char *)kbasename(ref->full_path->start);
2786 ref->name_len = ref->full_path->end - ref->name;
2787 ref->dir_path = ref->full_path->start;
2788 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002789 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002790 else
Alexander Block31db9f72012-07-25 23:19:24 +02002791 ref->dir_path_len = ref->full_path->end -
2792 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002793
2794 list_add_tail(&ref->list, head);
2795 return 0;
2796}
2797
Josef Bacikba5e8f22013-08-16 16:52:55 -04002798static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2799{
2800 struct recorded_ref *new;
2801
David Sterbae780b0d2016-01-18 18:42:13 +01002802 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002803 if (!new)
2804 return -ENOMEM;
2805
2806 new->dir = ref->dir;
2807 new->dir_gen = ref->dir_gen;
2808 new->full_path = NULL;
2809 INIT_LIST_HEAD(&new->list);
2810 list_add_tail(&new->list, list);
2811 return 0;
2812}
2813
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002814static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002815{
2816 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002817
Alexander Blocke938c8a2012-07-28 16:33:49 +02002818 while (!list_empty(head)) {
2819 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002820 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002821 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002822 kfree(cur);
2823 }
Alexander Block31db9f72012-07-25 23:19:24 +02002824}
2825
2826static void free_recorded_refs(struct send_ctx *sctx)
2827{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002828 __free_recorded_refs(&sctx->new_refs);
2829 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002830}
2831
2832/*
Alexander Block766702e2012-07-28 14:11:31 +02002833 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002834 * ref of an unprocessed inode gets overwritten and for all non empty
2835 * directories.
2836 */
2837static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2838 struct fs_path *path)
2839{
2840 int ret;
2841 struct fs_path *orphan;
2842
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002843 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002844 if (!orphan)
2845 return -ENOMEM;
2846
2847 ret = gen_unique_name(sctx, ino, gen, orphan);
2848 if (ret < 0)
2849 goto out;
2850
2851 ret = send_rename(sctx, path, orphan);
2852
2853out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002854 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002855 return ret;
2856}
2857
Filipe Manana9dc44212014-02-19 14:31:44 +00002858static struct orphan_dir_info *
2859add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2860{
2861 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2862 struct rb_node *parent = NULL;
2863 struct orphan_dir_info *entry, *odi;
2864
David Sterbae780b0d2016-01-18 18:42:13 +01002865 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
Filipe Manana9dc44212014-02-19 14:31:44 +00002866 if (!odi)
2867 return ERR_PTR(-ENOMEM);
2868 odi->ino = dir_ino;
2869 odi->gen = 0;
2870
2871 while (*p) {
2872 parent = *p;
2873 entry = rb_entry(parent, struct orphan_dir_info, node);
2874 if (dir_ino < entry->ino) {
2875 p = &(*p)->rb_left;
2876 } else if (dir_ino > entry->ino) {
2877 p = &(*p)->rb_right;
2878 } else {
2879 kfree(odi);
2880 return entry;
2881 }
2882 }
2883
2884 rb_link_node(&odi->node, parent, p);
2885 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2886 return odi;
2887}
2888
2889static struct orphan_dir_info *
2890get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2891{
2892 struct rb_node *n = sctx->orphan_dirs.rb_node;
2893 struct orphan_dir_info *entry;
2894
2895 while (n) {
2896 entry = rb_entry(n, struct orphan_dir_info, node);
2897 if (dir_ino < entry->ino)
2898 n = n->rb_left;
2899 else if (dir_ino > entry->ino)
2900 n = n->rb_right;
2901 else
2902 return entry;
2903 }
2904 return NULL;
2905}
2906
2907static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2908{
2909 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2910
2911 return odi != NULL;
2912}
2913
2914static void free_orphan_dir_info(struct send_ctx *sctx,
2915 struct orphan_dir_info *odi)
2916{
2917 if (!odi)
2918 return;
2919 rb_erase(&odi->node, &sctx->orphan_dirs);
2920 kfree(odi);
2921}
2922
Alexander Block31db9f72012-07-25 23:19:24 +02002923/*
2924 * Returns 1 if a directory can be removed at this point in time.
2925 * We check this by iterating all dir items and checking if the inode behind
2926 * the dir item was already processed.
2927 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002928static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2929 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002930{
2931 int ret = 0;
2932 struct btrfs_root *root = sctx->parent_root;
2933 struct btrfs_path *path;
2934 struct btrfs_key key;
2935 struct btrfs_key found_key;
2936 struct btrfs_key loc;
2937 struct btrfs_dir_item *di;
2938
Alexander Block6d85ed02012-08-01 14:48:59 +02002939 /*
2940 * Don't try to rmdir the top/root subvolume dir.
2941 */
2942 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2943 return 0;
2944
Alexander Block31db9f72012-07-25 23:19:24 +02002945 path = alloc_path_for_send();
2946 if (!path)
2947 return -ENOMEM;
2948
2949 key.objectid = dir;
2950 key.type = BTRFS_DIR_INDEX_KEY;
2951 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002952 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2953 if (ret < 0)
2954 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002955
2956 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002957 struct waiting_dir_move *dm;
2958
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002959 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2960 ret = btrfs_next_leaf(root, path);
2961 if (ret < 0)
2962 goto out;
2963 else if (ret > 0)
2964 break;
2965 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002966 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002967 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2968 path->slots[0]);
2969 if (found_key.objectid != key.objectid ||
2970 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002971 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002972
2973 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2974 struct btrfs_dir_item);
2975 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2976
Filipe Manana9dc44212014-02-19 14:31:44 +00002977 dm = get_waiting_dir_move(sctx, loc.objectid);
2978 if (dm) {
2979 struct orphan_dir_info *odi;
2980
2981 odi = add_orphan_dir_info(sctx, dir);
2982 if (IS_ERR(odi)) {
2983 ret = PTR_ERR(odi);
2984 goto out;
2985 }
2986 odi->gen = dir_gen;
2987 dm->rmdir_ino = dir;
2988 ret = 0;
2989 goto out;
2990 }
2991
Alexander Block31db9f72012-07-25 23:19:24 +02002992 if (loc.objectid > send_progress) {
Robbie Ko443f9d22015-06-22 17:08:45 +08002993 struct orphan_dir_info *odi;
2994
2995 odi = get_orphan_dir_info(sctx, dir);
2996 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02002997 ret = 0;
2998 goto out;
2999 }
3000
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003001 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003002 }
3003
3004 ret = 1;
3005
3006out:
3007 btrfs_free_path(path);
3008 return ret;
3009}
3010
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003011static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3012{
Filipe Manana9dc44212014-02-19 14:31:44 +00003013 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003014
Filipe Manana9dc44212014-02-19 14:31:44 +00003015 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003016}
3017
Filipe Manana8b191a62015-04-09 14:09:14 +01003018static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003019{
3020 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3021 struct rb_node *parent = NULL;
3022 struct waiting_dir_move *entry, *dm;
3023
David Sterbae780b0d2016-01-18 18:42:13 +01003024 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003025 if (!dm)
3026 return -ENOMEM;
3027 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003028 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003029 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003030
3031 while (*p) {
3032 parent = *p;
3033 entry = rb_entry(parent, struct waiting_dir_move, node);
3034 if (ino < entry->ino) {
3035 p = &(*p)->rb_left;
3036 } else if (ino > entry->ino) {
3037 p = &(*p)->rb_right;
3038 } else {
3039 kfree(dm);
3040 return -EEXIST;
3041 }
3042 }
3043
3044 rb_link_node(&dm->node, parent, p);
3045 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3046 return 0;
3047}
3048
Filipe Manana9dc44212014-02-19 14:31:44 +00003049static struct waiting_dir_move *
3050get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003051{
3052 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3053 struct waiting_dir_move *entry;
3054
3055 while (n) {
3056 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003057 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003058 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003059 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003060 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003061 else
3062 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003063 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003064 return NULL;
3065}
3066
3067static void free_waiting_dir_move(struct send_ctx *sctx,
3068 struct waiting_dir_move *dm)
3069{
3070 if (!dm)
3071 return;
3072 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3073 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003074}
3075
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003076static int add_pending_dir_move(struct send_ctx *sctx,
3077 u64 ino,
3078 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003079 u64 parent_ino,
3080 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003081 struct list_head *deleted_refs,
3082 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003083{
3084 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3085 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003086 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003087 struct recorded_ref *cur;
3088 int exists = 0;
3089 int ret;
3090
David Sterbae780b0d2016-01-18 18:42:13 +01003091 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003092 if (!pm)
3093 return -ENOMEM;
3094 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003095 pm->ino = ino;
3096 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003097 INIT_LIST_HEAD(&pm->list);
3098 INIT_LIST_HEAD(&pm->update_refs);
3099 RB_CLEAR_NODE(&pm->node);
3100
3101 while (*p) {
3102 parent = *p;
3103 entry = rb_entry(parent, struct pending_dir_move, node);
3104 if (parent_ino < entry->parent_ino) {
3105 p = &(*p)->rb_left;
3106 } else if (parent_ino > entry->parent_ino) {
3107 p = &(*p)->rb_right;
3108 } else {
3109 exists = 1;
3110 break;
3111 }
3112 }
3113
Filipe Mananaf9594922014-03-27 20:14:01 +00003114 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003115 ret = dup_ref(cur, &pm->update_refs);
3116 if (ret < 0)
3117 goto out;
3118 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003119 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003120 ret = dup_ref(cur, &pm->update_refs);
3121 if (ret < 0)
3122 goto out;
3123 }
3124
Filipe Manana8b191a62015-04-09 14:09:14 +01003125 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003126 if (ret)
3127 goto out;
3128
3129 if (exists) {
3130 list_add_tail(&pm->list, &entry->list);
3131 } else {
3132 rb_link_node(&pm->node, parent, p);
3133 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3134 }
3135 ret = 0;
3136out:
3137 if (ret) {
3138 __free_recorded_refs(&pm->update_refs);
3139 kfree(pm);
3140 }
3141 return ret;
3142}
3143
3144static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3145 u64 parent_ino)
3146{
3147 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3148 struct pending_dir_move *entry;
3149
3150 while (n) {
3151 entry = rb_entry(n, struct pending_dir_move, node);
3152 if (parent_ino < entry->parent_ino)
3153 n = n->rb_left;
3154 else if (parent_ino > entry->parent_ino)
3155 n = n->rb_right;
3156 else
3157 return entry;
3158 }
3159 return NULL;
3160}
3161
Robbie Ko801bec32015-06-23 18:39:46 +08003162static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3163 u64 ino, u64 gen, u64 *ancestor_ino)
3164{
3165 int ret = 0;
3166 u64 parent_inode = 0;
3167 u64 parent_gen = 0;
3168 u64 start_ino = ino;
3169
3170 *ancestor_ino = 0;
3171 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3172 fs_path_reset(name);
3173
3174 if (is_waiting_for_rm(sctx, ino))
3175 break;
3176 if (is_waiting_for_move(sctx, ino)) {
3177 if (*ancestor_ino == 0)
3178 *ancestor_ino = ino;
3179 ret = get_first_ref(sctx->parent_root, ino,
3180 &parent_inode, &parent_gen, name);
3181 } else {
3182 ret = __get_cur_name_and_parent(sctx, ino, gen,
3183 &parent_inode,
3184 &parent_gen, name);
3185 if (ret > 0) {
3186 ret = 0;
3187 break;
3188 }
3189 }
3190 if (ret < 0)
3191 break;
3192 if (parent_inode == start_ino) {
3193 ret = 1;
3194 if (*ancestor_ino == 0)
3195 *ancestor_ino = ino;
3196 break;
3197 }
3198 ino = parent_inode;
3199 gen = parent_gen;
3200 }
3201 return ret;
3202}
3203
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003204static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3205{
3206 struct fs_path *from_path = NULL;
3207 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003208 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003209 u64 orig_progress = sctx->send_progress;
3210 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003211 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003212 struct waiting_dir_move *dm = NULL;
3213 u64 rmdir_ino = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003214 u64 ancestor;
3215 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003216 int ret;
3217
Filipe Manana2b863a12014-02-16 13:43:11 +00003218 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003219 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003220 if (!name || !from_path) {
3221 ret = -ENOMEM;
3222 goto out;
3223 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003224
Filipe Manana9dc44212014-02-19 14:31:44 +00003225 dm = get_waiting_dir_move(sctx, pm->ino);
3226 ASSERT(dm);
3227 rmdir_ino = dm->rmdir_ino;
Robbie Ko801bec32015-06-23 18:39:46 +08003228 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003229 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003230
Robbie Ko801bec32015-06-23 18:39:46 +08003231 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003232 ret = gen_unique_name(sctx, pm->ino,
3233 pm->gen, from_path);
3234 } else {
3235 ret = get_first_ref(sctx->parent_root, pm->ino,
3236 &parent_ino, &parent_gen, name);
3237 if (ret < 0)
3238 goto out;
3239 ret = get_cur_path(sctx, parent_ino, parent_gen,
3240 from_path);
3241 if (ret < 0)
3242 goto out;
3243 ret = fs_path_add_path(from_path, name);
3244 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003245 if (ret < 0)
3246 goto out;
3247
Filipe Mananaf9594922014-03-27 20:14:01 +00003248 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003249 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003250 if (ret < 0)
3251 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003252 if (ret) {
3253 LIST_HEAD(deleted_refs);
3254 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3255 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3256 &pm->update_refs, &deleted_refs,
3257 is_orphan);
3258 if (ret < 0)
3259 goto out;
3260 if (rmdir_ino) {
3261 dm = get_waiting_dir_move(sctx, pm->ino);
3262 ASSERT(dm);
3263 dm->rmdir_ino = rmdir_ino;
3264 }
3265 goto out;
3266 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003267 fs_path_reset(name);
3268 to_path = name;
3269 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003270 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3271 if (ret < 0)
3272 goto out;
3273
3274 ret = send_rename(sctx, from_path, to_path);
3275 if (ret < 0)
3276 goto out;
3277
Filipe Manana9dc44212014-02-19 14:31:44 +00003278 if (rmdir_ino) {
3279 struct orphan_dir_info *odi;
3280
3281 odi = get_orphan_dir_info(sctx, rmdir_ino);
3282 if (!odi) {
3283 /* already deleted */
3284 goto finish;
3285 }
Robbie Ko99ea42d2015-06-23 18:39:49 +08003286 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003287 if (ret < 0)
3288 goto out;
3289 if (!ret)
3290 goto finish;
3291
3292 name = fs_path_alloc();
3293 if (!name) {
3294 ret = -ENOMEM;
3295 goto out;
3296 }
3297 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3298 if (ret < 0)
3299 goto out;
3300 ret = send_rmdir(sctx, name);
3301 if (ret < 0)
3302 goto out;
3303 free_orphan_dir_info(sctx, odi);
3304 }
3305
3306finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003307 ret = send_utimes(sctx, pm->ino, pm->gen);
3308 if (ret < 0)
3309 goto out;
3310
3311 /*
3312 * After rename/move, need to update the utimes of both new parent(s)
3313 * and old parent(s).
3314 */
3315 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003316 /*
3317 * The parent inode might have been deleted in the send snapshot
3318 */
3319 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3320 NULL, NULL, NULL, NULL, NULL);
3321 if (ret == -ENOENT) {
3322 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003323 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003324 }
3325 if (ret < 0)
3326 goto out;
3327
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003328 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3329 if (ret < 0)
3330 goto out;
3331 }
3332
3333out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003334 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003335 fs_path_free(from_path);
3336 fs_path_free(to_path);
3337 sctx->send_progress = orig_progress;
3338
3339 return ret;
3340}
3341
3342static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3343{
3344 if (!list_empty(&m->list))
3345 list_del(&m->list);
3346 if (!RB_EMPTY_NODE(&m->node))
3347 rb_erase(&m->node, &sctx->pending_dir_moves);
3348 __free_recorded_refs(&m->update_refs);
3349 kfree(m);
3350}
3351
3352static void tail_append_pending_moves(struct pending_dir_move *moves,
3353 struct list_head *stack)
3354{
3355 if (list_empty(&moves->list)) {
3356 list_add_tail(&moves->list, stack);
3357 } else {
3358 LIST_HEAD(list);
3359 list_splice_init(&moves->list, &list);
3360 list_add_tail(&moves->list, stack);
3361 list_splice_tail(&list, stack);
3362 }
3363}
3364
3365static int apply_children_dir_moves(struct send_ctx *sctx)
3366{
3367 struct pending_dir_move *pm;
3368 struct list_head stack;
3369 u64 parent_ino = sctx->cur_ino;
3370 int ret = 0;
3371
3372 pm = get_pending_dir_moves(sctx, parent_ino);
3373 if (!pm)
3374 return 0;
3375
3376 INIT_LIST_HEAD(&stack);
3377 tail_append_pending_moves(pm, &stack);
3378
3379 while (!list_empty(&stack)) {
3380 pm = list_first_entry(&stack, struct pending_dir_move, list);
3381 parent_ino = pm->ino;
3382 ret = apply_dir_move(sctx, pm);
3383 free_pending_move(sctx, pm);
3384 if (ret)
3385 goto out;
3386 pm = get_pending_dir_moves(sctx, parent_ino);
3387 if (pm)
3388 tail_append_pending_moves(pm, &stack);
3389 }
3390 return 0;
3391
3392out:
3393 while (!list_empty(&stack)) {
3394 pm = list_first_entry(&stack, struct pending_dir_move, list);
3395 free_pending_move(sctx, pm);
3396 }
3397 return ret;
3398}
3399
Filipe Manana84471e22015-02-28 22:29:22 +00003400/*
3401 * We might need to delay a directory rename even when no ancestor directory
3402 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3403 * renamed. This happens when we rename a directory to the old name (the name
3404 * in the parent root) of some other unrelated directory that got its rename
3405 * delayed due to some ancestor with higher number that got renamed.
3406 *
3407 * Example:
3408 *
3409 * Parent snapshot:
3410 * . (ino 256)
3411 * |---- a/ (ino 257)
3412 * | |---- file (ino 260)
3413 * |
3414 * |---- b/ (ino 258)
3415 * |---- c/ (ino 259)
3416 *
3417 * Send snapshot:
3418 * . (ino 256)
3419 * |---- a/ (ino 258)
3420 * |---- x/ (ino 259)
3421 * |---- y/ (ino 257)
3422 * |----- file (ino 260)
3423 *
3424 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3425 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3426 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3427 * must issue is:
3428 *
3429 * 1 - rename 259 from 'c' to 'x'
3430 * 2 - rename 257 from 'a' to 'x/y'
3431 * 3 - rename 258 from 'b' to 'a'
3432 *
3433 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3434 * be done right away and < 0 on error.
3435 */
3436static int wait_for_dest_dir_move(struct send_ctx *sctx,
3437 struct recorded_ref *parent_ref,
3438 const bool is_orphan)
3439{
3440 struct btrfs_path *path;
3441 struct btrfs_key key;
3442 struct btrfs_key di_key;
3443 struct btrfs_dir_item *di;
3444 u64 left_gen;
3445 u64 right_gen;
3446 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003447 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003448
3449 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3450 return 0;
3451
3452 path = alloc_path_for_send();
3453 if (!path)
3454 return -ENOMEM;
3455
3456 key.objectid = parent_ref->dir;
3457 key.type = BTRFS_DIR_ITEM_KEY;
3458 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3459
3460 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3461 if (ret < 0) {
3462 goto out;
3463 } else if (ret > 0) {
3464 ret = 0;
3465 goto out;
3466 }
3467
3468 di = btrfs_match_dir_item_name(sctx->parent_root, path,
3469 parent_ref->name, parent_ref->name_len);
3470 if (!di) {
3471 ret = 0;
3472 goto out;
3473 }
3474 /*
3475 * di_key.objectid has the number of the inode that has a dentry in the
3476 * parent directory with the same name that sctx->cur_ino is being
3477 * renamed to. We need to check if that inode is in the send root as
3478 * well and if it is currently marked as an inode with a pending rename,
3479 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3480 * that it happens after that other inode is renamed.
3481 */
3482 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3483 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3484 ret = 0;
3485 goto out;
3486 }
3487
3488 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3489 &left_gen, NULL, NULL, NULL, NULL);
3490 if (ret < 0)
3491 goto out;
3492 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3493 &right_gen, NULL, NULL, NULL, NULL);
3494 if (ret < 0) {
3495 if (ret == -ENOENT)
3496 ret = 0;
3497 goto out;
3498 }
3499
3500 /* Different inode, no need to delay the rename of sctx->cur_ino */
3501 if (right_gen != left_gen) {
3502 ret = 0;
3503 goto out;
3504 }
3505
Robbie Ko801bec32015-06-23 18:39:46 +08003506 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3507 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003508 ret = add_pending_dir_move(sctx,
3509 sctx->cur_ino,
3510 sctx->cur_inode_gen,
3511 di_key.objectid,
3512 &sctx->new_refs,
3513 &sctx->deleted_refs,
3514 is_orphan);
3515 if (!ret)
3516 ret = 1;
3517 }
3518out:
3519 btrfs_free_path(path);
3520 return ret;
3521}
3522
Filipe Manana80aa6022015-03-27 17:50:45 +00003523/*
3524 * Check if ino ino1 is an ancestor of inode ino2 in the given root.
3525 * Return 1 if true, 0 if false and < 0 on error.
3526 */
3527static int is_ancestor(struct btrfs_root *root,
3528 const u64 ino1,
3529 const u64 ino1_gen,
3530 const u64 ino2,
3531 struct fs_path *fs_path)
3532{
3533 u64 ino = ino2;
3534
3535 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3536 int ret;
3537 u64 parent;
3538 u64 parent_gen;
3539
3540 fs_path_reset(fs_path);
3541 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3542 if (ret < 0) {
3543 if (ret == -ENOENT && ino == ino2)
3544 ret = 0;
3545 return ret;
3546 }
3547 if (parent == ino1)
3548 return parent_gen == ino1_gen ? 1 : 0;
3549 ino = parent;
3550 }
3551 return 0;
3552}
3553
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003554static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003555 struct recorded_ref *parent_ref,
3556 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003557{
Filipe Mananaf9594922014-03-27 20:14:01 +00003558 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003559 u64 ino = parent_ref->dir;
3560 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003561 struct fs_path *path_before = NULL;
3562 struct fs_path *path_after = NULL;
3563 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003564
3565 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003566 path_before = fs_path_alloc();
3567 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003568 ret = -ENOMEM;
3569 goto out;
3570 }
3571
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003572 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003573 * Our current directory inode may not yet be renamed/moved because some
3574 * ancestor (immediate or not) has to be renamed/moved first. So find if
3575 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003576 * that ancestor is processed to avoid path build infinite loops (done
3577 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003578 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003579 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3580 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003581 /*
3582 * If the current inode is an ancestor of ino in the
3583 * parent root, we need to delay the rename of the
3584 * current inode, otherwise don't delayed the rename
3585 * because we can end up with a circular dependency
3586 * of renames, resulting in some directories never
3587 * getting the respective rename operations issued in
3588 * the send stream or getting into infinite path build
3589 * loops.
3590 */
3591 ret = is_ancestor(sctx->parent_root,
3592 sctx->cur_ino, sctx->cur_inode_gen,
3593 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003594 if (ret)
3595 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003596 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003597
3598 fs_path_reset(path_before);
3599 fs_path_reset(path_after);
3600
3601 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananaf9594922014-03-27 20:14:01 +00003602 NULL, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003603 if (ret < 0)
3604 goto out;
3605 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3606 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003607 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003608 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003609 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003610 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003611 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003612 }
3613
3614 len1 = fs_path_len(path_before);
3615 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003616 if (ino > sctx->cur_ino &&
3617 (parent_ino_before != parent_ino_after || len1 != len2 ||
3618 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003619 ret = 1;
Filipe Mananaf9594922014-03-27 20:14:01 +00003620 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003621 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003622 ino = parent_ino_after;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003623 }
3624
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003625out:
3626 fs_path_free(path_before);
3627 fs_path_free(path_after);
3628
Filipe Mananaf9594922014-03-27 20:14:01 +00003629 if (ret == 1) {
3630 ret = add_pending_dir_move(sctx,
3631 sctx->cur_ino,
3632 sctx->cur_inode_gen,
3633 ino,
3634 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003635 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003636 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003637 if (!ret)
3638 ret = 1;
3639 }
3640
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003641 return ret;
3642}
3643
Alexander Block31db9f72012-07-25 23:19:24 +02003644/*
3645 * This does all the move/link/unlink/rmdir magic.
3646 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003647static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003648{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003649 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003650 int ret = 0;
3651 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003652 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003653 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003654 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003655 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003656 u64 ow_gen;
3657 int did_overwrite = 0;
3658 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003659 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003660 bool can_rename = true;
Alexander Block31db9f72012-07-25 23:19:24 +02003661
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003662 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003663
Alexander Block6d85ed02012-08-01 14:48:59 +02003664 /*
3665 * This should never happen as the root dir always has the same ref
3666 * which is always '..'
3667 */
3668 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003669 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003670
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003671 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003672 if (!valid_path) {
3673 ret = -ENOMEM;
3674 goto out;
3675 }
3676
Alexander Block31db9f72012-07-25 23:19:24 +02003677 /*
3678 * First, check if the first ref of the current inode was overwritten
3679 * before. If yes, we know that the current inode was already orphanized
3680 * and thus use the orphan name. If not, we can use get_cur_path to
3681 * get the path of the first ref as it would like while receiving at
3682 * this point in time.
3683 * New inodes are always orphan at the beginning, so force to use the
3684 * orphan name in this case.
3685 * The first ref is stored in valid_path and will be updated if it
3686 * gets moved around.
3687 */
3688 if (!sctx->cur_inode_new) {
3689 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3690 sctx->cur_inode_gen);
3691 if (ret < 0)
3692 goto out;
3693 if (ret)
3694 did_overwrite = 1;
3695 }
3696 if (sctx->cur_inode_new || did_overwrite) {
3697 ret = gen_unique_name(sctx, sctx->cur_ino,
3698 sctx->cur_inode_gen, valid_path);
3699 if (ret < 0)
3700 goto out;
3701 is_orphan = 1;
3702 } else {
3703 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3704 valid_path);
3705 if (ret < 0)
3706 goto out;
3707 }
3708
3709 list_for_each_entry(cur, &sctx->new_refs, list) {
3710 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003711 * We may have refs where the parent directory does not exist
3712 * yet. This happens if the parent directories inum is higher
3713 * the the current inum. To handle this case, we create the
3714 * parent directory out of order. But we need to check if this
3715 * did already happen before due to other refs in the same dir.
3716 */
3717 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3718 if (ret < 0)
3719 goto out;
3720 if (ret == inode_state_will_create) {
3721 ret = 0;
3722 /*
3723 * First check if any of the current inodes refs did
3724 * already create the dir.
3725 */
3726 list_for_each_entry(cur2, &sctx->new_refs, list) {
3727 if (cur == cur2)
3728 break;
3729 if (cur2->dir == cur->dir) {
3730 ret = 1;
3731 break;
3732 }
3733 }
3734
3735 /*
3736 * If that did not happen, check if a previous inode
3737 * did already create the dir.
3738 */
3739 if (!ret)
3740 ret = did_create_dir(sctx, cur->dir);
3741 if (ret < 0)
3742 goto out;
3743 if (!ret) {
3744 ret = send_create_inode(sctx, cur->dir);
3745 if (ret < 0)
3746 goto out;
3747 }
3748 }
3749
3750 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003751 * Check if this new ref would overwrite the first ref of
3752 * another unprocessed inode. If yes, orphanize the
3753 * overwritten inode. If we find an overwritten ref that is
3754 * not the first ref, simply unlink it.
3755 */
3756 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3757 cur->name, cur->name_len,
3758 &ow_inode, &ow_gen);
3759 if (ret < 0)
3760 goto out;
3761 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003762 ret = is_first_ref(sctx->parent_root,
3763 ow_inode, cur->dir, cur->name,
3764 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003765 if (ret < 0)
3766 goto out;
3767 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003768 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003769 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003770
Alexander Block31db9f72012-07-25 23:19:24 +02003771 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3772 cur->full_path);
3773 if (ret < 0)
3774 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003775
3776 /*
3777 * If ow_inode has its rename operation delayed
3778 * make sure that its orphanized name is used in
3779 * the source path when performing its rename
3780 * operation.
3781 */
3782 if (is_waiting_for_move(sctx, ow_inode)) {
3783 wdm = get_waiting_dir_move(sctx,
3784 ow_inode);
3785 ASSERT(wdm);
3786 wdm->orphanized = true;
3787 }
3788
Filipe Manana8996a482015-03-12 15:16:20 +00003789 /*
3790 * Make sure we clear our orphanized inode's
3791 * name from the name cache. This is because the
3792 * inode ow_inode might be an ancestor of some
3793 * other inode that will be orphanized as well
3794 * later and has an inode number greater than
3795 * sctx->send_progress. We need to prevent
3796 * future name lookups from using the old name
3797 * and get instead the orphan name.
3798 */
3799 nce = name_cache_search(sctx, ow_inode, ow_gen);
3800 if (nce) {
3801 name_cache_delete(sctx, nce);
3802 kfree(nce);
3803 }
Robbie Ko801bec32015-06-23 18:39:46 +08003804
3805 /*
3806 * ow_inode might currently be an ancestor of
3807 * cur_ino, therefore compute valid_path (the
3808 * current path of cur_ino) again because it
3809 * might contain the pre-orphanization name of
3810 * ow_inode, which is no longer valid.
3811 */
3812 fs_path_reset(valid_path);
3813 ret = get_cur_path(sctx, sctx->cur_ino,
3814 sctx->cur_inode_gen, valid_path);
3815 if (ret < 0)
3816 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003817 } else {
3818 ret = send_unlink(sctx, cur->full_path);
3819 if (ret < 0)
3820 goto out;
3821 }
3822 }
3823
Filipe Manana84471e22015-02-28 22:29:22 +00003824 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3825 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3826 if (ret < 0)
3827 goto out;
3828 if (ret == 1) {
3829 can_rename = false;
3830 *pending_move = 1;
3831 }
3832 }
3833
Filipe Manana8b191a62015-04-09 14:09:14 +01003834 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3835 can_rename) {
3836 ret = wait_for_parent_move(sctx, cur, is_orphan);
3837 if (ret < 0)
3838 goto out;
3839 if (ret == 1) {
3840 can_rename = false;
3841 *pending_move = 1;
3842 }
3843 }
3844
Alexander Block31db9f72012-07-25 23:19:24 +02003845 /*
3846 * link/move the ref to the new place. If we have an orphan
3847 * inode, move it and update valid_path. If not, link or move
3848 * it depending on the inode mode.
3849 */
Filipe Manana84471e22015-02-28 22:29:22 +00003850 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003851 ret = send_rename(sctx, valid_path, cur->full_path);
3852 if (ret < 0)
3853 goto out;
3854 is_orphan = 0;
3855 ret = fs_path_copy(valid_path, cur->full_path);
3856 if (ret < 0)
3857 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00003858 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003859 if (S_ISDIR(sctx->cur_inode_mode)) {
3860 /*
3861 * Dirs can't be linked, so move it. For moved
3862 * dirs, we always have one new and one deleted
3863 * ref. The deleted ref is ignored later.
3864 */
Filipe Manana8b191a62015-04-09 14:09:14 +01003865 ret = send_rename(sctx, valid_path,
3866 cur->full_path);
3867 if (!ret)
3868 ret = fs_path_copy(valid_path,
3869 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003870 if (ret < 0)
3871 goto out;
3872 } else {
3873 ret = send_link(sctx, cur->full_path,
3874 valid_path);
3875 if (ret < 0)
3876 goto out;
3877 }
3878 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003879 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003880 if (ret < 0)
3881 goto out;
3882 }
3883
3884 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3885 /*
3886 * Check if we can already rmdir the directory. If not,
3887 * orphanize it. For every dir item inside that gets deleted
3888 * later, we do this check again and rmdir it then if possible.
3889 * See the use of check_dirs for more details.
3890 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003891 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3892 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003893 if (ret < 0)
3894 goto out;
3895 if (ret) {
3896 ret = send_rmdir(sctx, valid_path);
3897 if (ret < 0)
3898 goto out;
3899 } else if (!is_orphan) {
3900 ret = orphanize_inode(sctx, sctx->cur_ino,
3901 sctx->cur_inode_gen, valid_path);
3902 if (ret < 0)
3903 goto out;
3904 is_orphan = 1;
3905 }
3906
3907 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003908 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003909 if (ret < 0)
3910 goto out;
3911 }
Alexander Blockccf16262012-07-28 11:46:29 +02003912 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3913 !list_empty(&sctx->deleted_refs)) {
3914 /*
3915 * We have a moved dir. Add the old parent to check_dirs
3916 */
3917 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3918 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003919 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003920 if (ret < 0)
3921 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003922 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3923 /*
3924 * We have a non dir inode. Go through all deleted refs and
3925 * unlink them if they were not already overwritten by other
3926 * inodes.
3927 */
3928 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3929 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3930 sctx->cur_ino, sctx->cur_inode_gen,
3931 cur->name, cur->name_len);
3932 if (ret < 0)
3933 goto out;
3934 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003935 ret = send_unlink(sctx, cur->full_path);
3936 if (ret < 0)
3937 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003938 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003939 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003940 if (ret < 0)
3941 goto out;
3942 }
Alexander Block31db9f72012-07-25 23:19:24 +02003943 /*
3944 * If the inode is still orphan, unlink the orphan. This may
3945 * happen when a previous inode did overwrite the first ref
3946 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003947 * inode. Unlinking does not mean that the inode is deleted in
3948 * all cases. There may still be links to this inode in other
3949 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003950 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003951 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003952 ret = send_unlink(sctx, valid_path);
3953 if (ret < 0)
3954 goto out;
3955 }
3956 }
3957
3958 /*
3959 * We did collect all parent dirs where cur_inode was once located. We
3960 * now go through all these dirs and check if they are pending for
3961 * deletion and if it's finally possible to perform the rmdir now.
3962 * We also update the inode stats of the parent dirs here.
3963 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003964 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003965 /*
3966 * In case we had refs into dirs that were not processed yet,
3967 * we don't need to do the utime and rmdir logic for these dirs.
3968 * The dir will be processed later.
3969 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003970 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003971 continue;
3972
Josef Bacikba5e8f22013-08-16 16:52:55 -04003973 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003974 if (ret < 0)
3975 goto out;
3976
3977 if (ret == inode_state_did_create ||
3978 ret == inode_state_no_change) {
3979 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003980 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003981 if (ret < 0)
3982 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003983 } else if (ret == inode_state_did_delete &&
3984 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003985 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3986 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003987 if (ret < 0)
3988 goto out;
3989 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003990 ret = get_cur_path(sctx, cur->dir,
3991 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003992 if (ret < 0)
3993 goto out;
3994 ret = send_rmdir(sctx, valid_path);
3995 if (ret < 0)
3996 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003997 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02003998 }
3999 }
4000 }
4001
Alexander Block31db9f72012-07-25 23:19:24 +02004002 ret = 0;
4003
4004out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004005 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004006 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004007 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004008 return ret;
4009}
4010
Liu Boa4d96d62014-03-03 21:31:03 +08004011static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
4012 struct fs_path *name, void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004013{
4014 int ret = 0;
4015 struct send_ctx *sctx = ctx;
4016 struct fs_path *p;
4017 u64 gen;
4018
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004019 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004020 if (!p)
4021 return -ENOMEM;
4022
Liu Boa4d96d62014-03-03 21:31:03 +08004023 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004024 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004025 if (ret < 0)
4026 goto out;
4027
Alexander Block31db9f72012-07-25 23:19:24 +02004028 ret = get_cur_path(sctx, dir, gen, p);
4029 if (ret < 0)
4030 goto out;
4031 ret = fs_path_add_path(p, name);
4032 if (ret < 0)
4033 goto out;
4034
Liu Boa4d96d62014-03-03 21:31:03 +08004035 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004036
4037out:
4038 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004039 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004040 return ret;
4041}
4042
Liu Boa4d96d62014-03-03 21:31:03 +08004043static int __record_new_ref(int num, u64 dir, int index,
4044 struct fs_path *name,
4045 void *ctx)
4046{
4047 struct send_ctx *sctx = ctx;
4048 return record_ref(sctx->send_root, num, dir, index, name,
4049 ctx, &sctx->new_refs);
4050}
4051
4052
Alexander Block31db9f72012-07-25 23:19:24 +02004053static int __record_deleted_ref(int num, u64 dir, int index,
4054 struct fs_path *name,
4055 void *ctx)
4056{
Alexander Block31db9f72012-07-25 23:19:24 +02004057 struct send_ctx *sctx = ctx;
Liu Boa4d96d62014-03-03 21:31:03 +08004058 return record_ref(sctx->parent_root, num, dir, index, name,
4059 ctx, &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004060}
4061
4062static int record_new_ref(struct send_ctx *sctx)
4063{
4064 int ret;
4065
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004066 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4067 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004068 if (ret < 0)
4069 goto out;
4070 ret = 0;
4071
4072out:
4073 return ret;
4074}
4075
4076static int record_deleted_ref(struct send_ctx *sctx)
4077{
4078 int ret;
4079
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004080 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4081 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004082 if (ret < 0)
4083 goto out;
4084 ret = 0;
4085
4086out:
4087 return ret;
4088}
4089
4090struct find_ref_ctx {
4091 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004092 u64 dir_gen;
4093 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004094 struct fs_path *name;
4095 int found_idx;
4096};
4097
4098static int __find_iref(int num, u64 dir, int index,
4099 struct fs_path *name,
4100 void *ctx_)
4101{
4102 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004103 u64 dir_gen;
4104 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004105
4106 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4107 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004108 /*
4109 * To avoid doing extra lookups we'll only do this if everything
4110 * else matches.
4111 */
4112 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4113 NULL, NULL, NULL);
4114 if (ret)
4115 return ret;
4116 if (dir_gen != ctx->dir_gen)
4117 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004118 ctx->found_idx = num;
4119 return 1;
4120 }
4121 return 0;
4122}
4123
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004124static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004125 struct btrfs_path *path,
4126 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004127 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004128{
4129 int ret;
4130 struct find_ref_ctx ctx;
4131
4132 ctx.dir = dir;
4133 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004134 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004135 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004136 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004137
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004138 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004139 if (ret < 0)
4140 return ret;
4141
4142 if (ctx.found_idx == -1)
4143 return -ENOENT;
4144
4145 return ctx.found_idx;
4146}
4147
4148static int __record_changed_new_ref(int num, u64 dir, int index,
4149 struct fs_path *name,
4150 void *ctx)
4151{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004152 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004153 int ret;
4154 struct send_ctx *sctx = ctx;
4155
Josef Bacikba5e8f22013-08-16 16:52:55 -04004156 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4157 NULL, NULL, NULL);
4158 if (ret)
4159 return ret;
4160
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004161 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004162 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004163 if (ret == -ENOENT)
4164 ret = __record_new_ref(num, dir, index, name, sctx);
4165 else if (ret > 0)
4166 ret = 0;
4167
4168 return ret;
4169}
4170
4171static int __record_changed_deleted_ref(int num, u64 dir, int index,
4172 struct fs_path *name,
4173 void *ctx)
4174{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004175 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004176 int ret;
4177 struct send_ctx *sctx = ctx;
4178
Josef Bacikba5e8f22013-08-16 16:52:55 -04004179 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4180 NULL, NULL, NULL);
4181 if (ret)
4182 return ret;
4183
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004184 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004185 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004186 if (ret == -ENOENT)
4187 ret = __record_deleted_ref(num, dir, index, name, sctx);
4188 else if (ret > 0)
4189 ret = 0;
4190
4191 return ret;
4192}
4193
4194static int record_changed_ref(struct send_ctx *sctx)
4195{
4196 int ret = 0;
4197
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004198 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004199 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4200 if (ret < 0)
4201 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004202 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004203 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4204 if (ret < 0)
4205 goto out;
4206 ret = 0;
4207
4208out:
4209 return ret;
4210}
4211
4212/*
4213 * Record and process all refs at once. Needed when an inode changes the
4214 * generation number, which means that it was deleted and recreated.
4215 */
4216static int process_all_refs(struct send_ctx *sctx,
4217 enum btrfs_compare_tree_result cmd)
4218{
4219 int ret;
4220 struct btrfs_root *root;
4221 struct btrfs_path *path;
4222 struct btrfs_key key;
4223 struct btrfs_key found_key;
4224 struct extent_buffer *eb;
4225 int slot;
4226 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004227 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004228
4229 path = alloc_path_for_send();
4230 if (!path)
4231 return -ENOMEM;
4232
4233 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4234 root = sctx->send_root;
4235 cb = __record_new_ref;
4236 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4237 root = sctx->parent_root;
4238 cb = __record_deleted_ref;
4239 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004240 btrfs_err(sctx->send_root->fs_info,
4241 "Wrong command %d in process_all_refs", cmd);
4242 ret = -EINVAL;
4243 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004244 }
4245
4246 key.objectid = sctx->cmp_key->objectid;
4247 key.type = BTRFS_INODE_REF_KEY;
4248 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004249 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4250 if (ret < 0)
4251 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004252
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004253 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004254 eb = path->nodes[0];
4255 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004256 if (slot >= btrfs_header_nritems(eb)) {
4257 ret = btrfs_next_leaf(root, path);
4258 if (ret < 0)
4259 goto out;
4260 else if (ret > 0)
4261 break;
4262 continue;
4263 }
4264
Alexander Block31db9f72012-07-25 23:19:24 +02004265 btrfs_item_key_to_cpu(eb, &found_key, slot);
4266
4267 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004268 (found_key.type != BTRFS_INODE_REF_KEY &&
4269 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004270 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004271
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004272 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004273 if (ret < 0)
4274 goto out;
4275
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004276 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004277 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004278 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004279
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004280 /*
4281 * We don't actually care about pending_move as we are simply
4282 * re-creating this inode and will be rename'ing it into place once we
4283 * rename the parent directory.
4284 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004285 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004286out:
4287 btrfs_free_path(path);
4288 return ret;
4289}
4290
4291static int send_set_xattr(struct send_ctx *sctx,
4292 struct fs_path *path,
4293 const char *name, int name_len,
4294 const char *data, int data_len)
4295{
4296 int ret = 0;
4297
4298 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4299 if (ret < 0)
4300 goto out;
4301
4302 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4303 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4304 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4305
4306 ret = send_cmd(sctx);
4307
4308tlv_put_failure:
4309out:
4310 return ret;
4311}
4312
4313static int send_remove_xattr(struct send_ctx *sctx,
4314 struct fs_path *path,
4315 const char *name, int name_len)
4316{
4317 int ret = 0;
4318
4319 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4320 if (ret < 0)
4321 goto out;
4322
4323 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4324 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4325
4326 ret = send_cmd(sctx);
4327
4328tlv_put_failure:
4329out:
4330 return ret;
4331}
4332
4333static int __process_new_xattr(int num, struct btrfs_key *di_key,
4334 const char *name, int name_len,
4335 const char *data, int data_len,
4336 u8 type, void *ctx)
4337{
4338 int ret;
4339 struct send_ctx *sctx = ctx;
4340 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004341 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004342
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004343 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004344 if (!p)
4345 return -ENOMEM;
4346
4347 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004348 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004349 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004350 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004351 * only contains the version number and no entries.
4352 */
4353 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4354 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4355 if (data_len == 0) {
4356 dummy_acl.a_version =
4357 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4358 data = (char *)&dummy_acl;
4359 data_len = sizeof(dummy_acl);
4360 }
4361 }
4362
4363 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4364 if (ret < 0)
4365 goto out;
4366
4367 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4368
4369out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004370 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004371 return ret;
4372}
4373
4374static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4375 const char *name, int name_len,
4376 const char *data, int data_len,
4377 u8 type, void *ctx)
4378{
4379 int ret;
4380 struct send_ctx *sctx = ctx;
4381 struct fs_path *p;
4382
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004383 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004384 if (!p)
4385 return -ENOMEM;
4386
4387 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4388 if (ret < 0)
4389 goto out;
4390
4391 ret = send_remove_xattr(sctx, p, name, name_len);
4392
4393out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004394 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004395 return ret;
4396}
4397
4398static int process_new_xattr(struct send_ctx *sctx)
4399{
4400 int ret = 0;
4401
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004402 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4403 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004404
4405 return ret;
4406}
4407
4408static int process_deleted_xattr(struct send_ctx *sctx)
4409{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004410 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4411 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004412}
4413
4414struct find_xattr_ctx {
4415 const char *name;
4416 int name_len;
4417 int found_idx;
4418 char *found_data;
4419 int found_data_len;
4420};
4421
4422static int __find_xattr(int num, struct btrfs_key *di_key,
4423 const char *name, int name_len,
4424 const char *data, int data_len,
4425 u8 type, void *vctx)
4426{
4427 struct find_xattr_ctx *ctx = vctx;
4428
4429 if (name_len == ctx->name_len &&
4430 strncmp(name, ctx->name, name_len) == 0) {
4431 ctx->found_idx = num;
4432 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004433 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004434 if (!ctx->found_data)
4435 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004436 return 1;
4437 }
4438 return 0;
4439}
4440
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004441static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004442 struct btrfs_path *path,
4443 struct btrfs_key *key,
4444 const char *name, int name_len,
4445 char **data, int *data_len)
4446{
4447 int ret;
4448 struct find_xattr_ctx ctx;
4449
4450 ctx.name = name;
4451 ctx.name_len = name_len;
4452 ctx.found_idx = -1;
4453 ctx.found_data = NULL;
4454 ctx.found_data_len = 0;
4455
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004456 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004457 if (ret < 0)
4458 return ret;
4459
4460 if (ctx.found_idx == -1)
4461 return -ENOENT;
4462 if (data) {
4463 *data = ctx.found_data;
4464 *data_len = ctx.found_data_len;
4465 } else {
4466 kfree(ctx.found_data);
4467 }
4468 return ctx.found_idx;
4469}
4470
4471
4472static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4473 const char *name, int name_len,
4474 const char *data, int data_len,
4475 u8 type, void *ctx)
4476{
4477 int ret;
4478 struct send_ctx *sctx = ctx;
4479 char *found_data = NULL;
4480 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004481
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004482 ret = find_xattr(sctx->parent_root, sctx->right_path,
4483 sctx->cmp_key, name, name_len, &found_data,
4484 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004485 if (ret == -ENOENT) {
4486 ret = __process_new_xattr(num, di_key, name, name_len, data,
4487 data_len, type, ctx);
4488 } else if (ret >= 0) {
4489 if (data_len != found_data_len ||
4490 memcmp(data, found_data, data_len)) {
4491 ret = __process_new_xattr(num, di_key, name, name_len,
4492 data, data_len, type, ctx);
4493 } else {
4494 ret = 0;
4495 }
4496 }
4497
4498 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004499 return ret;
4500}
4501
4502static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4503 const char *name, int name_len,
4504 const char *data, int data_len,
4505 u8 type, void *ctx)
4506{
4507 int ret;
4508 struct send_ctx *sctx = ctx;
4509
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004510 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4511 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004512 if (ret == -ENOENT)
4513 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4514 data_len, type, ctx);
4515 else if (ret >= 0)
4516 ret = 0;
4517
4518 return ret;
4519}
4520
4521static int process_changed_xattr(struct send_ctx *sctx)
4522{
4523 int ret = 0;
4524
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004525 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004526 sctx->cmp_key, __process_changed_new_xattr, sctx);
4527 if (ret < 0)
4528 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004529 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004530 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4531
4532out:
4533 return ret;
4534}
4535
4536static int process_all_new_xattrs(struct send_ctx *sctx)
4537{
4538 int ret;
4539 struct btrfs_root *root;
4540 struct btrfs_path *path;
4541 struct btrfs_key key;
4542 struct btrfs_key found_key;
4543 struct extent_buffer *eb;
4544 int slot;
4545
4546 path = alloc_path_for_send();
4547 if (!path)
4548 return -ENOMEM;
4549
4550 root = sctx->send_root;
4551
4552 key.objectid = sctx->cmp_key->objectid;
4553 key.type = BTRFS_XATTR_ITEM_KEY;
4554 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004555 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4556 if (ret < 0)
4557 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004558
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004559 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004560 eb = path->nodes[0];
4561 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004562 if (slot >= btrfs_header_nritems(eb)) {
4563 ret = btrfs_next_leaf(root, path);
4564 if (ret < 0) {
4565 goto out;
4566 } else if (ret > 0) {
4567 ret = 0;
4568 break;
4569 }
4570 continue;
4571 }
Alexander Block31db9f72012-07-25 23:19:24 +02004572
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004573 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004574 if (found_key.objectid != key.objectid ||
4575 found_key.type != key.type) {
4576 ret = 0;
4577 goto out;
4578 }
4579
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004580 ret = iterate_dir_item(root, path, &found_key,
4581 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004582 if (ret < 0)
4583 goto out;
4584
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004585 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004586 }
4587
4588out:
4589 btrfs_free_path(path);
4590 return ret;
4591}
4592
Josef Baciked259092013-10-25 11:36:01 -04004593static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4594{
4595 struct btrfs_root *root = sctx->send_root;
4596 struct btrfs_fs_info *fs_info = root->fs_info;
4597 struct inode *inode;
4598 struct page *page;
4599 char *addr;
4600 struct btrfs_key key;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004601 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004602 pgoff_t last_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004603 unsigned pg_offset = offset & ~PAGE_MASK;
Josef Baciked259092013-10-25 11:36:01 -04004604 ssize_t ret = 0;
4605
4606 key.objectid = sctx->cur_ino;
4607 key.type = BTRFS_INODE_ITEM_KEY;
4608 key.offset = 0;
4609
4610 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4611 if (IS_ERR(inode))
4612 return PTR_ERR(inode);
4613
4614 if (offset + len > i_size_read(inode)) {
4615 if (offset > i_size_read(inode))
4616 len = 0;
4617 else
4618 len = offset - i_size_read(inode);
4619 }
4620 if (len == 0)
4621 goto out;
4622
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004623 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd32014-03-05 10:07:35 +08004624
4625 /* initial readahead */
4626 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4627 file_ra_state_init(&sctx->ra, inode->i_mapping);
4628 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4629 last_index - index + 1);
4630
Josef Baciked259092013-10-25 11:36:01 -04004631 while (index <= last_index) {
4632 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004633 PAGE_SIZE - pg_offset);
David Sterbae780b0d2016-01-18 18:42:13 +01004634 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
Josef Baciked259092013-10-25 11:36:01 -04004635 if (!page) {
4636 ret = -ENOMEM;
4637 break;
4638 }
4639
4640 if (!PageUptodate(page)) {
4641 btrfs_readpage(NULL, page);
4642 lock_page(page);
4643 if (!PageUptodate(page)) {
4644 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004645 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004646 ret = -EIO;
4647 break;
4648 }
4649 }
4650
4651 addr = kmap(page);
4652 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4653 kunmap(page);
4654 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004655 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004656 index++;
4657 pg_offset = 0;
4658 len -= cur_len;
4659 ret += cur_len;
4660 }
4661out:
4662 iput(inode);
4663 return ret;
4664}
4665
Alexander Block31db9f72012-07-25 23:19:24 +02004666/*
4667 * Read some bytes from the current inode/file and send a write command to
4668 * user space.
4669 */
4670static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4671{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004672 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02004673 int ret = 0;
4674 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004675 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004676
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004677 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004678 if (!p)
4679 return -ENOMEM;
4680
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004681 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004682
Josef Baciked259092013-10-25 11:36:01 -04004683 num_read = fill_read_buf(sctx, offset, len);
4684 if (num_read <= 0) {
4685 if (num_read < 0)
4686 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004687 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004688 }
Alexander Block31db9f72012-07-25 23:19:24 +02004689
4690 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4691 if (ret < 0)
4692 goto out;
4693
4694 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4695 if (ret < 0)
4696 goto out;
4697
4698 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4699 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004700 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004701
4702 ret = send_cmd(sctx);
4703
4704tlv_put_failure:
4705out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004706 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004707 if (ret < 0)
4708 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004709 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004710}
4711
4712/*
4713 * Send a clone command to user space.
4714 */
4715static int send_clone(struct send_ctx *sctx,
4716 u64 offset, u32 len,
4717 struct clone_root *clone_root)
4718{
4719 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004720 struct fs_path *p;
4721 u64 gen;
4722
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004723 btrfs_debug(sctx->send_root->fs_info,
4724 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4725 offset, len, clone_root->root->objectid, clone_root->ino,
4726 clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02004727
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004728 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004729 if (!p)
4730 return -ENOMEM;
4731
4732 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4733 if (ret < 0)
4734 goto out;
4735
4736 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4737 if (ret < 0)
4738 goto out;
4739
4740 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4741 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4742 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4743
Alexander Blocke938c8a2012-07-28 16:33:49 +02004744 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004745 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004746 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004747 if (ret < 0)
4748 goto out;
4749 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4750 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004751 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004752 }
4753 if (ret < 0)
4754 goto out;
4755
Josef Bacik37b8d272015-06-04 17:17:25 -04004756 /*
4757 * If the parent we're using has a received_uuid set then use that as
4758 * our clone source as that is what we will look for when doing a
4759 * receive.
4760 *
4761 * This covers the case that we create a snapshot off of a received
4762 * subvolume and then use that as the parent and try to receive on a
4763 * different host.
4764 */
4765 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4766 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4767 clone_root->root->root_item.received_uuid);
4768 else
4769 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4770 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004771 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004772 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004773 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4774 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4775 clone_root->offset);
4776
4777 ret = send_cmd(sctx);
4778
4779tlv_put_failure:
4780out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004781 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004782 return ret;
4783}
4784
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004785/*
4786 * Send an update extent command to user space.
4787 */
4788static int send_update_extent(struct send_ctx *sctx,
4789 u64 offset, u32 len)
4790{
4791 int ret = 0;
4792 struct fs_path *p;
4793
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004794 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004795 if (!p)
4796 return -ENOMEM;
4797
4798 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4799 if (ret < 0)
4800 goto out;
4801
4802 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4803 if (ret < 0)
4804 goto out;
4805
4806 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4807 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4808 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4809
4810 ret = send_cmd(sctx);
4811
4812tlv_put_failure:
4813out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004814 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004815 return ret;
4816}
4817
Josef Bacik16e75492013-10-22 12:18:51 -04004818static int send_hole(struct send_ctx *sctx, u64 end)
4819{
4820 struct fs_path *p = NULL;
4821 u64 offset = sctx->cur_inode_last_extent;
4822 u64 len;
4823 int ret = 0;
4824
Filipe Mananab672f4b2018-02-06 20:39:20 +00004825 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
4826 return send_update_extent(sctx, offset, end - offset);
4827
Josef Bacik16e75492013-10-22 12:18:51 -04004828 p = fs_path_alloc();
4829 if (!p)
4830 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01004831 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4832 if (ret < 0)
4833 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04004834 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4835 while (offset < end) {
4836 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4837
4838 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4839 if (ret < 0)
4840 break;
Josef Bacik16e75492013-10-22 12:18:51 -04004841 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4842 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4843 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4844 ret = send_cmd(sctx);
4845 if (ret < 0)
4846 break;
4847 offset += len;
4848 }
4849tlv_put_failure:
4850 fs_path_free(p);
4851 return ret;
4852}
4853
Filipe Mananad906d492015-10-02 10:47:34 +01004854static int send_extent_data(struct send_ctx *sctx,
4855 const u64 offset,
4856 const u64 len)
4857{
4858 u64 sent = 0;
4859
4860 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
4861 return send_update_extent(sctx, offset, len);
4862
4863 while (sent < len) {
4864 u64 size = len - sent;
4865 int ret;
4866
4867 if (size > BTRFS_SEND_READ_SIZE)
4868 size = BTRFS_SEND_READ_SIZE;
4869 ret = send_write(sctx, offset + sent, size);
4870 if (ret < 0)
4871 return ret;
4872 if (!ret)
4873 break;
4874 sent += ret;
4875 }
4876 return 0;
4877}
4878
4879static int clone_range(struct send_ctx *sctx,
4880 struct clone_root *clone_root,
4881 const u64 disk_byte,
4882 u64 data_offset,
4883 u64 offset,
4884 u64 len)
4885{
4886 struct btrfs_path *path;
4887 struct btrfs_key key;
4888 int ret;
4889
4890 path = alloc_path_for_send();
4891 if (!path)
4892 return -ENOMEM;
4893
4894 /*
4895 * We can't send a clone operation for the entire range if we find
4896 * extent items in the respective range in the source file that
4897 * refer to different extents or if we find holes.
4898 * So check for that and do a mix of clone and regular write/copy
4899 * operations if needed.
4900 *
4901 * Example:
4902 *
4903 * mkfs.btrfs -f /dev/sda
4904 * mount /dev/sda /mnt
4905 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
4906 * cp --reflink=always /mnt/foo /mnt/bar
4907 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
4908 * btrfs subvolume snapshot -r /mnt /mnt/snap
4909 *
4910 * If when we send the snapshot and we are processing file bar (which
4911 * has a higher inode number than foo) we blindly send a clone operation
4912 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
4913 * a file bar that matches the content of file foo - iow, doesn't match
4914 * the content from bar in the original filesystem.
4915 */
4916 key.objectid = clone_root->ino;
4917 key.type = BTRFS_EXTENT_DATA_KEY;
4918 key.offset = clone_root->offset;
4919 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
4920 if (ret < 0)
4921 goto out;
4922 if (ret > 0 && path->slots[0] > 0) {
4923 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
4924 if (key.objectid == clone_root->ino &&
4925 key.type == BTRFS_EXTENT_DATA_KEY)
4926 path->slots[0]--;
4927 }
4928
4929 while (true) {
4930 struct extent_buffer *leaf = path->nodes[0];
4931 int slot = path->slots[0];
4932 struct btrfs_file_extent_item *ei;
4933 u8 type;
4934 u64 ext_len;
4935 u64 clone_len;
4936
4937 if (slot >= btrfs_header_nritems(leaf)) {
4938 ret = btrfs_next_leaf(clone_root->root, path);
4939 if (ret < 0)
4940 goto out;
4941 else if (ret > 0)
4942 break;
4943 continue;
4944 }
4945
4946 btrfs_item_key_to_cpu(leaf, &key, slot);
4947
4948 /*
4949 * We might have an implicit trailing hole (NO_HOLES feature
4950 * enabled). We deal with it after leaving this loop.
4951 */
4952 if (key.objectid != clone_root->ino ||
4953 key.type != BTRFS_EXTENT_DATA_KEY)
4954 break;
4955
4956 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4957 type = btrfs_file_extent_type(leaf, ei);
4958 if (type == BTRFS_FILE_EXTENT_INLINE) {
4959 ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004960 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01004961 } else {
4962 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
4963 }
4964
4965 if (key.offset + ext_len <= clone_root->offset)
4966 goto next;
4967
4968 if (key.offset > clone_root->offset) {
4969 /* Implicit hole, NO_HOLES feature enabled. */
4970 u64 hole_len = key.offset - clone_root->offset;
4971
4972 if (hole_len > len)
4973 hole_len = len;
4974 ret = send_extent_data(sctx, offset, hole_len);
4975 if (ret < 0)
4976 goto out;
4977
4978 len -= hole_len;
4979 if (len == 0)
4980 break;
4981 offset += hole_len;
4982 clone_root->offset += hole_len;
4983 data_offset += hole_len;
4984 }
4985
4986 if (key.offset >= clone_root->offset + len)
4987 break;
4988
4989 clone_len = min_t(u64, ext_len, len);
4990
4991 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
4992 btrfs_file_extent_offset(leaf, ei) == data_offset)
4993 ret = send_clone(sctx, offset, clone_len, clone_root);
4994 else
4995 ret = send_extent_data(sctx, offset, clone_len);
4996
4997 if (ret < 0)
4998 goto out;
4999
5000 len -= clone_len;
5001 if (len == 0)
5002 break;
5003 offset += clone_len;
5004 clone_root->offset += clone_len;
5005 data_offset += clone_len;
5006next:
5007 path->slots[0]++;
5008 }
5009
5010 if (len > 0)
5011 ret = send_extent_data(sctx, offset, len);
5012 else
5013 ret = 0;
5014out:
5015 btrfs_free_path(path);
5016 return ret;
5017}
5018
Alexander Block31db9f72012-07-25 23:19:24 +02005019static int send_write_or_clone(struct send_ctx *sctx,
5020 struct btrfs_path *path,
5021 struct btrfs_key *key,
5022 struct clone_root *clone_root)
5023{
5024 int ret = 0;
5025 struct btrfs_file_extent_item *ei;
5026 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02005027 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02005028 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005029 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005030
5031 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5032 struct btrfs_file_extent_item);
5033 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005034 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005035 len = btrfs_file_extent_inline_len(path->nodes[0],
5036 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005037 /*
5038 * it is possible the inline item won't cover the whole page,
5039 * but there may be items after this page. Make
5040 * sure to send the whole thing
5041 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005042 len = PAGE_ALIGN(len);
Chris Mason74dd17f2012-08-07 16:25:13 -04005043 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02005044 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005045 }
Alexander Block31db9f72012-07-25 23:19:24 +02005046
5047 if (offset + len > sctx->cur_inode_size)
5048 len = sctx->cur_inode_size - offset;
5049 if (len == 0) {
5050 ret = 0;
5051 goto out;
5052 }
5053
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005054 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01005055 u64 disk_byte;
5056 u64 data_offset;
5057
5058 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5059 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5060 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5061 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005062 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005063 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005064 }
Alexander Block31db9f72012-07-25 23:19:24 +02005065out:
5066 return ret;
5067}
5068
5069static int is_extent_unchanged(struct send_ctx *sctx,
5070 struct btrfs_path *left_path,
5071 struct btrfs_key *ekey)
5072{
5073 int ret = 0;
5074 struct btrfs_key key;
5075 struct btrfs_path *path = NULL;
5076 struct extent_buffer *eb;
5077 int slot;
5078 struct btrfs_key found_key;
5079 struct btrfs_file_extent_item *ei;
5080 u64 left_disknr;
5081 u64 right_disknr;
5082 u64 left_offset;
5083 u64 right_offset;
5084 u64 left_offset_fixed;
5085 u64 left_len;
5086 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005087 u64 left_gen;
5088 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005089 u8 left_type;
5090 u8 right_type;
5091
5092 path = alloc_path_for_send();
5093 if (!path)
5094 return -ENOMEM;
5095
5096 eb = left_path->nodes[0];
5097 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005098 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5099 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005100
5101 if (left_type != BTRFS_FILE_EXTENT_REG) {
5102 ret = 0;
5103 goto out;
5104 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005105 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5106 left_len = btrfs_file_extent_num_bytes(eb, ei);
5107 left_offset = btrfs_file_extent_offset(eb, ei);
5108 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005109
5110 /*
5111 * Following comments will refer to these graphics. L is the left
5112 * extents which we are checking at the moment. 1-8 are the right
5113 * extents that we iterate.
5114 *
5115 * |-----L-----|
5116 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5117 *
5118 * |-----L-----|
5119 * |--1--|-2b-|...(same as above)
5120 *
5121 * Alternative situation. Happens on files where extents got split.
5122 * |-----L-----|
5123 * |-----------7-----------|-6-|
5124 *
5125 * Alternative situation. Happens on files which got larger.
5126 * |-----L-----|
5127 * |-8-|
5128 * Nothing follows after 8.
5129 */
5130
5131 key.objectid = ekey->objectid;
5132 key.type = BTRFS_EXTENT_DATA_KEY;
5133 key.offset = ekey->offset;
5134 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5135 if (ret < 0)
5136 goto out;
5137 if (ret) {
5138 ret = 0;
5139 goto out;
5140 }
5141
5142 /*
5143 * Handle special case where the right side has no extents at all.
5144 */
5145 eb = path->nodes[0];
5146 slot = path->slots[0];
5147 btrfs_item_key_to_cpu(eb, &found_key, slot);
5148 if (found_key.objectid != key.objectid ||
5149 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005150 /* If we're a hole then just pretend nothing changed */
5151 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005152 goto out;
5153 }
5154
5155 /*
5156 * We're now on 2a, 2b or 7.
5157 */
5158 key = found_key;
5159 while (key.offset < ekey->offset + left_len) {
5160 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5161 right_type = btrfs_file_extent_type(eb, ei);
Filipe Manana7016b202017-04-04 20:31:00 +01005162 if (right_type != BTRFS_FILE_EXTENT_REG &&
5163 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005164 ret = 0;
5165 goto out;
5166 }
5167
Filipe Manana7016b202017-04-04 20:31:00 +01005168 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5169 right_len = btrfs_file_extent_inline_len(eb, slot, ei);
5170 right_len = PAGE_ALIGN(right_len);
5171 } else {
5172 right_len = btrfs_file_extent_num_bytes(eb, ei);
5173 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005174
Alexander Block31db9f72012-07-25 23:19:24 +02005175 /*
5176 * Are we at extent 8? If yes, we know the extent is changed.
5177 * This may only happen on the first iteration.
5178 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005179 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005180 /* If we're a hole just pretend nothing changed */
5181 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005182 goto out;
5183 }
5184
Filipe Manana7016b202017-04-04 20:31:00 +01005185 /*
5186 * We just wanted to see if when we have an inline extent, what
5187 * follows it is a regular extent (wanted to check the above
5188 * condition for inline extents too). This should normally not
5189 * happen but it's possible for example when we have an inline
5190 * compressed extent representing data with a size matching
5191 * the page size (currently the same as sector size).
5192 */
5193 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5194 ret = 0;
5195 goto out;
5196 }
5197
Filipe Manana97d65c12017-07-06 15:31:46 +01005198 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5199 right_offset = btrfs_file_extent_offset(eb, ei);
5200 right_gen = btrfs_file_extent_generation(eb, ei);
5201
Alexander Block31db9f72012-07-25 23:19:24 +02005202 left_offset_fixed = left_offset;
5203 if (key.offset < ekey->offset) {
5204 /* Fix the right offset for 2a and 7. */
5205 right_offset += ekey->offset - key.offset;
5206 } else {
5207 /* Fix the left offset for all behind 2a and 2b */
5208 left_offset_fixed += key.offset - ekey->offset;
5209 }
5210
5211 /*
5212 * Check if we have the same extent.
5213 */
Alexander Block39540962012-08-01 12:46:05 +02005214 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005215 left_offset_fixed != right_offset ||
5216 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005217 ret = 0;
5218 goto out;
5219 }
5220
5221 /*
5222 * Go to the next extent.
5223 */
5224 ret = btrfs_next_item(sctx->parent_root, path);
5225 if (ret < 0)
5226 goto out;
5227 if (!ret) {
5228 eb = path->nodes[0];
5229 slot = path->slots[0];
5230 btrfs_item_key_to_cpu(eb, &found_key, slot);
5231 }
5232 if (ret || found_key.objectid != key.objectid ||
5233 found_key.type != key.type) {
5234 key.offset += right_len;
5235 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005236 }
5237 if (found_key.offset != key.offset + right_len) {
5238 ret = 0;
5239 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005240 }
5241 key = found_key;
5242 }
5243
5244 /*
5245 * We're now behind the left extent (treat as unchanged) or at the end
5246 * of the right side (treat as changed).
5247 */
5248 if (key.offset >= ekey->offset + left_len)
5249 ret = 1;
5250 else
5251 ret = 0;
5252
5253
5254out:
5255 btrfs_free_path(path);
5256 return ret;
5257}
5258
Josef Bacik16e75492013-10-22 12:18:51 -04005259static int get_last_extent(struct send_ctx *sctx, u64 offset)
5260{
5261 struct btrfs_path *path;
5262 struct btrfs_root *root = sctx->send_root;
5263 struct btrfs_file_extent_item *fi;
5264 struct btrfs_key key;
5265 u64 extent_end;
5266 u8 type;
5267 int ret;
5268
5269 path = alloc_path_for_send();
5270 if (!path)
5271 return -ENOMEM;
5272
5273 sctx->cur_inode_last_extent = 0;
5274
5275 key.objectid = sctx->cur_ino;
5276 key.type = BTRFS_EXTENT_DATA_KEY;
5277 key.offset = offset;
5278 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5279 if (ret < 0)
5280 goto out;
5281 ret = 0;
5282 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5283 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5284 goto out;
5285
5286 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5287 struct btrfs_file_extent_item);
5288 type = btrfs_file_extent_type(path->nodes[0], fi);
5289 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005290 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5291 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005292 extent_end = ALIGN(key.offset + size,
5293 sctx->send_root->sectorsize);
5294 } else {
5295 extent_end = key.offset +
5296 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5297 }
5298 sctx->cur_inode_last_extent = extent_end;
5299out:
5300 btrfs_free_path(path);
5301 return ret;
5302}
5303
5304static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5305 struct btrfs_key *key)
5306{
5307 struct btrfs_file_extent_item *fi;
5308 u64 extent_end;
5309 u8 type;
5310 int ret = 0;
5311
5312 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5313 return 0;
5314
5315 if (sctx->cur_inode_last_extent == (u64)-1) {
5316 ret = get_last_extent(sctx, key->offset - 1);
5317 if (ret)
5318 return ret;
5319 }
5320
5321 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5322 struct btrfs_file_extent_item);
5323 type = btrfs_file_extent_type(path->nodes[0], fi);
5324 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005325 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5326 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005327 extent_end = ALIGN(key->offset + size,
5328 sctx->send_root->sectorsize);
5329 } else {
5330 extent_end = key->offset +
5331 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5332 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005333
5334 if (path->slots[0] == 0 &&
5335 sctx->cur_inode_last_extent < key->offset) {
5336 /*
5337 * We might have skipped entire leafs that contained only
5338 * file extent items for our current inode. These leafs have
5339 * a generation number smaller (older) than the one in the
5340 * current leaf and the leaf our last extent came from, and
5341 * are located between these 2 leafs.
5342 */
5343 ret = get_last_extent(sctx, key->offset - 1);
5344 if (ret)
5345 return ret;
5346 }
5347
Josef Bacik16e75492013-10-22 12:18:51 -04005348 if (sctx->cur_inode_last_extent < key->offset)
5349 ret = send_hole(sctx, key->offset);
5350 sctx->cur_inode_last_extent = extent_end;
5351 return ret;
5352}
5353
Alexander Block31db9f72012-07-25 23:19:24 +02005354static int process_extent(struct send_ctx *sctx,
5355 struct btrfs_path *path,
5356 struct btrfs_key *key)
5357{
Alexander Block31db9f72012-07-25 23:19:24 +02005358 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005359 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005360
5361 if (S_ISLNK(sctx->cur_inode_mode))
5362 return 0;
5363
5364 if (sctx->parent_root && !sctx->cur_inode_new) {
5365 ret = is_extent_unchanged(sctx, path, key);
5366 if (ret < 0)
5367 goto out;
5368 if (ret) {
5369 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005370 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005371 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005372 } else {
5373 struct btrfs_file_extent_item *ei;
5374 u8 type;
5375
5376 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5377 struct btrfs_file_extent_item);
5378 type = btrfs_file_extent_type(path->nodes[0], ei);
5379 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5380 type == BTRFS_FILE_EXTENT_REG) {
5381 /*
5382 * The send spec does not have a prealloc command yet,
5383 * so just leave a hole for prealloc'ed extents until
5384 * we have enough commands queued up to justify rev'ing
5385 * the send spec.
5386 */
5387 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5388 ret = 0;
5389 goto out;
5390 }
5391
5392 /* Have a hole, just skip it. */
5393 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5394 ret = 0;
5395 goto out;
5396 }
5397 }
Alexander Block31db9f72012-07-25 23:19:24 +02005398 }
5399
5400 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5401 sctx->cur_inode_size, &found_clone);
5402 if (ret != -ENOENT && ret < 0)
5403 goto out;
5404
5405 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005406 if (ret)
5407 goto out;
5408out_hole:
5409 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005410out:
5411 return ret;
5412}
5413
5414static int process_all_extents(struct send_ctx *sctx)
5415{
5416 int ret;
5417 struct btrfs_root *root;
5418 struct btrfs_path *path;
5419 struct btrfs_key key;
5420 struct btrfs_key found_key;
5421 struct extent_buffer *eb;
5422 int slot;
5423
5424 root = sctx->send_root;
5425 path = alloc_path_for_send();
5426 if (!path)
5427 return -ENOMEM;
5428
5429 key.objectid = sctx->cmp_key->objectid;
5430 key.type = BTRFS_EXTENT_DATA_KEY;
5431 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005432 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5433 if (ret < 0)
5434 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005435
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005436 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005437 eb = path->nodes[0];
5438 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005439
5440 if (slot >= btrfs_header_nritems(eb)) {
5441 ret = btrfs_next_leaf(root, path);
5442 if (ret < 0) {
5443 goto out;
5444 } else if (ret > 0) {
5445 ret = 0;
5446 break;
5447 }
5448 continue;
5449 }
5450
Alexander Block31db9f72012-07-25 23:19:24 +02005451 btrfs_item_key_to_cpu(eb, &found_key, slot);
5452
5453 if (found_key.objectid != key.objectid ||
5454 found_key.type != key.type) {
5455 ret = 0;
5456 goto out;
5457 }
5458
5459 ret = process_extent(sctx, path, &found_key);
5460 if (ret < 0)
5461 goto out;
5462
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005463 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005464 }
5465
5466out:
5467 btrfs_free_path(path);
5468 return ret;
5469}
5470
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005471static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5472 int *pending_move,
5473 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005474{
5475 int ret = 0;
5476
5477 if (sctx->cur_ino == 0)
5478 goto out;
5479 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005480 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005481 goto out;
5482 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5483 goto out;
5484
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005485 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005486 if (ret < 0)
5487 goto out;
5488
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005489 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005490out:
5491 return ret;
5492}
5493
5494static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5495{
5496 int ret = 0;
5497 u64 left_mode;
5498 u64 left_uid;
5499 u64 left_gid;
5500 u64 right_mode;
5501 u64 right_uid;
5502 u64 right_gid;
5503 int need_chmod = 0;
5504 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005505 int pending_move = 0;
5506 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005507
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005508 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5509 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005510 if (ret < 0)
5511 goto out;
5512
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005513 /*
5514 * We have processed the refs and thus need to advance send_progress.
5515 * Now, calls to get_cur_xxx will take the updated refs of the current
5516 * inode into account.
5517 *
5518 * On the other hand, if our current inode is a directory and couldn't
5519 * be moved/renamed because its parent was renamed/moved too and it has
5520 * a higher inode number, we can only move/rename our current inode
5521 * after we moved/renamed its parent. Therefore in this case operate on
5522 * the old path (pre move/rename) of our current inode, and the
5523 * move/rename will be performed later.
5524 */
5525 if (refs_processed && !pending_move)
5526 sctx->send_progress = sctx->cur_ino + 1;
5527
Alexander Block31db9f72012-07-25 23:19:24 +02005528 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5529 goto out;
5530 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5531 goto out;
5532
5533 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005534 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005535 if (ret < 0)
5536 goto out;
5537
Alex Lyakase2d044f2012-10-17 13:52:47 +00005538 if (!sctx->parent_root || sctx->cur_inode_new) {
5539 need_chown = 1;
5540 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005541 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005542 } else {
5543 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5544 NULL, NULL, &right_mode, &right_uid,
5545 &right_gid, NULL);
5546 if (ret < 0)
5547 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005548
Alex Lyakase2d044f2012-10-17 13:52:47 +00005549 if (left_uid != right_uid || left_gid != right_gid)
5550 need_chown = 1;
5551 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5552 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005553 }
5554
5555 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005556 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005557 if (sctx->cur_inode_last_extent == (u64)-1 ||
5558 sctx->cur_inode_last_extent <
5559 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005560 ret = get_last_extent(sctx, (u64)-1);
5561 if (ret)
5562 goto out;
5563 }
5564 if (sctx->cur_inode_last_extent <
5565 sctx->cur_inode_size) {
5566 ret = send_hole(sctx, sctx->cur_inode_size);
5567 if (ret)
5568 goto out;
5569 }
5570 }
Alexander Block31db9f72012-07-25 23:19:24 +02005571 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5572 sctx->cur_inode_size);
5573 if (ret < 0)
5574 goto out;
5575 }
5576
5577 if (need_chown) {
5578 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5579 left_uid, left_gid);
5580 if (ret < 0)
5581 goto out;
5582 }
5583 if (need_chmod) {
5584 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5585 left_mode);
5586 if (ret < 0)
5587 goto out;
5588 }
5589
5590 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005591 * If other directory inodes depended on our current directory
5592 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005593 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005594 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5595 ret = apply_children_dir_moves(sctx);
5596 if (ret)
5597 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005598 /*
5599 * Need to send that every time, no matter if it actually
5600 * changed between the two trees as we have done changes to
5601 * the inode before. If our inode is a directory and it's
5602 * waiting to be moved/renamed, we will send its utimes when
5603 * it's moved/renamed, therefore we don't need to do it here.
5604 */
5605 sctx->send_progress = sctx->cur_ino + 1;
5606 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5607 if (ret < 0)
5608 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005609 }
5610
Alexander Block31db9f72012-07-25 23:19:24 +02005611out:
5612 return ret;
5613}
5614
5615static int changed_inode(struct send_ctx *sctx,
5616 enum btrfs_compare_tree_result result)
5617{
5618 int ret = 0;
5619 struct btrfs_key *key = sctx->cmp_key;
5620 struct btrfs_inode_item *left_ii = NULL;
5621 struct btrfs_inode_item *right_ii = NULL;
5622 u64 left_gen = 0;
5623 u64 right_gen = 0;
5624
Alexander Block31db9f72012-07-25 23:19:24 +02005625 sctx->cur_ino = key->objectid;
5626 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005627 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005628
5629 /*
5630 * Set send_progress to current inode. This will tell all get_cur_xxx
5631 * functions that the current inode's refs are not updated yet. Later,
5632 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5633 */
Alexander Block31db9f72012-07-25 23:19:24 +02005634 sctx->send_progress = sctx->cur_ino;
5635
5636 if (result == BTRFS_COMPARE_TREE_NEW ||
5637 result == BTRFS_COMPARE_TREE_CHANGED) {
5638 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5639 sctx->left_path->slots[0],
5640 struct btrfs_inode_item);
5641 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5642 left_ii);
5643 } else {
5644 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5645 sctx->right_path->slots[0],
5646 struct btrfs_inode_item);
5647 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5648 right_ii);
5649 }
5650 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5651 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5652 sctx->right_path->slots[0],
5653 struct btrfs_inode_item);
5654
5655 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5656 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005657
5658 /*
5659 * The cur_ino = root dir case is special here. We can't treat
5660 * the inode as deleted+reused because it would generate a
5661 * stream that tries to delete/mkdir the root dir.
5662 */
5663 if (left_gen != right_gen &&
5664 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005665 sctx->cur_inode_new_gen = 1;
5666 }
5667
5668 if (result == BTRFS_COMPARE_TREE_NEW) {
5669 sctx->cur_inode_gen = left_gen;
5670 sctx->cur_inode_new = 1;
5671 sctx->cur_inode_deleted = 0;
5672 sctx->cur_inode_size = btrfs_inode_size(
5673 sctx->left_path->nodes[0], left_ii);
5674 sctx->cur_inode_mode = btrfs_inode_mode(
5675 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005676 sctx->cur_inode_rdev = btrfs_inode_rdev(
5677 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005678 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005679 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005680 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5681 sctx->cur_inode_gen = right_gen;
5682 sctx->cur_inode_new = 0;
5683 sctx->cur_inode_deleted = 1;
5684 sctx->cur_inode_size = btrfs_inode_size(
5685 sctx->right_path->nodes[0], right_ii);
5686 sctx->cur_inode_mode = btrfs_inode_mode(
5687 sctx->right_path->nodes[0], right_ii);
5688 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005689 /*
5690 * We need to do some special handling in case the inode was
5691 * reported as changed with a changed generation number. This
5692 * means that the original inode was deleted and new inode
5693 * reused the same inum. So we have to treat the old inode as
5694 * deleted and the new one as new.
5695 */
Alexander Block31db9f72012-07-25 23:19:24 +02005696 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005697 /*
5698 * First, process the inode as if it was deleted.
5699 */
Alexander Block31db9f72012-07-25 23:19:24 +02005700 sctx->cur_inode_gen = right_gen;
5701 sctx->cur_inode_new = 0;
5702 sctx->cur_inode_deleted = 1;
5703 sctx->cur_inode_size = btrfs_inode_size(
5704 sctx->right_path->nodes[0], right_ii);
5705 sctx->cur_inode_mode = btrfs_inode_mode(
5706 sctx->right_path->nodes[0], right_ii);
5707 ret = process_all_refs(sctx,
5708 BTRFS_COMPARE_TREE_DELETED);
5709 if (ret < 0)
5710 goto out;
5711
Alexander Block766702e2012-07-28 14:11:31 +02005712 /*
5713 * Now process the inode as if it was new.
5714 */
Alexander Block31db9f72012-07-25 23:19:24 +02005715 sctx->cur_inode_gen = left_gen;
5716 sctx->cur_inode_new = 1;
5717 sctx->cur_inode_deleted = 0;
5718 sctx->cur_inode_size = btrfs_inode_size(
5719 sctx->left_path->nodes[0], left_ii);
5720 sctx->cur_inode_mode = btrfs_inode_mode(
5721 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005722 sctx->cur_inode_rdev = btrfs_inode_rdev(
5723 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005724 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005725 if (ret < 0)
5726 goto out;
5727
5728 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5729 if (ret < 0)
5730 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005731 /*
5732 * Advance send_progress now as we did not get into
5733 * process_recorded_refs_if_needed in the new_gen case.
5734 */
5735 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005736
5737 /*
5738 * Now process all extents and xattrs of the inode as if
5739 * they were all new.
5740 */
Alexander Block31db9f72012-07-25 23:19:24 +02005741 ret = process_all_extents(sctx);
5742 if (ret < 0)
5743 goto out;
5744 ret = process_all_new_xattrs(sctx);
5745 if (ret < 0)
5746 goto out;
5747 } else {
5748 sctx->cur_inode_gen = left_gen;
5749 sctx->cur_inode_new = 0;
5750 sctx->cur_inode_new_gen = 0;
5751 sctx->cur_inode_deleted = 0;
5752 sctx->cur_inode_size = btrfs_inode_size(
5753 sctx->left_path->nodes[0], left_ii);
5754 sctx->cur_inode_mode = btrfs_inode_mode(
5755 sctx->left_path->nodes[0], left_ii);
5756 }
5757 }
5758
5759out:
5760 return ret;
5761}
5762
Alexander Block766702e2012-07-28 14:11:31 +02005763/*
5764 * We have to process new refs before deleted refs, but compare_trees gives us
5765 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5766 * first and later process them in process_recorded_refs.
5767 * For the cur_inode_new_gen case, we skip recording completely because
5768 * changed_inode did already initiate processing of refs. The reason for this is
5769 * that in this case, compare_tree actually compares the refs of 2 different
5770 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5771 * refs of the right tree as deleted and all refs of the left tree as new.
5772 */
Alexander Block31db9f72012-07-25 23:19:24 +02005773static int changed_ref(struct send_ctx *sctx,
5774 enum btrfs_compare_tree_result result)
5775{
5776 int ret = 0;
5777
Filipe Manana95155582016-08-01 01:50:37 +01005778 if (sctx->cur_ino != sctx->cmp_key->objectid) {
5779 inconsistent_snapshot_error(sctx, result, "reference");
5780 return -EIO;
5781 }
Alexander Block31db9f72012-07-25 23:19:24 +02005782
5783 if (!sctx->cur_inode_new_gen &&
5784 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5785 if (result == BTRFS_COMPARE_TREE_NEW)
5786 ret = record_new_ref(sctx);
5787 else if (result == BTRFS_COMPARE_TREE_DELETED)
5788 ret = record_deleted_ref(sctx);
5789 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5790 ret = record_changed_ref(sctx);
5791 }
5792
5793 return ret;
5794}
5795
Alexander Block766702e2012-07-28 14:11:31 +02005796/*
5797 * Process new/deleted/changed xattrs. We skip processing in the
5798 * cur_inode_new_gen case because changed_inode did already initiate processing
5799 * of xattrs. The reason is the same as in changed_ref
5800 */
Alexander Block31db9f72012-07-25 23:19:24 +02005801static int changed_xattr(struct send_ctx *sctx,
5802 enum btrfs_compare_tree_result result)
5803{
5804 int ret = 0;
5805
Filipe Manana95155582016-08-01 01:50:37 +01005806 if (sctx->cur_ino != sctx->cmp_key->objectid) {
5807 inconsistent_snapshot_error(sctx, result, "xattr");
5808 return -EIO;
5809 }
Alexander Block31db9f72012-07-25 23:19:24 +02005810
5811 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5812 if (result == BTRFS_COMPARE_TREE_NEW)
5813 ret = process_new_xattr(sctx);
5814 else if (result == BTRFS_COMPARE_TREE_DELETED)
5815 ret = process_deleted_xattr(sctx);
5816 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5817 ret = process_changed_xattr(sctx);
5818 }
5819
5820 return ret;
5821}
5822
Alexander Block766702e2012-07-28 14:11:31 +02005823/*
5824 * Process new/deleted/changed extents. We skip processing in the
5825 * cur_inode_new_gen case because changed_inode did already initiate processing
5826 * of extents. The reason is the same as in changed_ref
5827 */
Alexander Block31db9f72012-07-25 23:19:24 +02005828static int changed_extent(struct send_ctx *sctx,
5829 enum btrfs_compare_tree_result result)
5830{
5831 int ret = 0;
5832
Filipe Manana95155582016-08-01 01:50:37 +01005833 if (sctx->cur_ino != sctx->cmp_key->objectid) {
Filipe Mananad5e84fd2016-09-19 10:57:40 +01005834
5835 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5836 struct extent_buffer *leaf_l;
5837 struct extent_buffer *leaf_r;
5838 struct btrfs_file_extent_item *ei_l;
5839 struct btrfs_file_extent_item *ei_r;
5840
5841 leaf_l = sctx->left_path->nodes[0];
5842 leaf_r = sctx->right_path->nodes[0];
5843 ei_l = btrfs_item_ptr(leaf_l,
5844 sctx->left_path->slots[0],
5845 struct btrfs_file_extent_item);
5846 ei_r = btrfs_item_ptr(leaf_r,
5847 sctx->right_path->slots[0],
5848 struct btrfs_file_extent_item);
5849
5850 /*
5851 * We may have found an extent item that has changed
5852 * only its disk_bytenr field and the corresponding
5853 * inode item was not updated. This case happens due to
5854 * very specific timings during relocation when a leaf
5855 * that contains file extent items is COWed while
5856 * relocation is ongoing and its in the stage where it
5857 * updates data pointers. So when this happens we can
5858 * safely ignore it since we know it's the same extent,
5859 * but just at different logical and physical locations
5860 * (when an extent is fully replaced with a new one, we
5861 * know the generation number must have changed too,
5862 * since snapshot creation implies committing the current
5863 * transaction, and the inode item must have been updated
5864 * as well).
5865 * This replacement of the disk_bytenr happens at
5866 * relocation.c:replace_file_extents() through
5867 * relocation.c:btrfs_reloc_cow_block().
5868 */
5869 if (btrfs_file_extent_generation(leaf_l, ei_l) ==
5870 btrfs_file_extent_generation(leaf_r, ei_r) &&
5871 btrfs_file_extent_ram_bytes(leaf_l, ei_l) ==
5872 btrfs_file_extent_ram_bytes(leaf_r, ei_r) &&
5873 btrfs_file_extent_compression(leaf_l, ei_l) ==
5874 btrfs_file_extent_compression(leaf_r, ei_r) &&
5875 btrfs_file_extent_encryption(leaf_l, ei_l) ==
5876 btrfs_file_extent_encryption(leaf_r, ei_r) &&
5877 btrfs_file_extent_other_encoding(leaf_l, ei_l) ==
5878 btrfs_file_extent_other_encoding(leaf_r, ei_r) &&
5879 btrfs_file_extent_type(leaf_l, ei_l) ==
5880 btrfs_file_extent_type(leaf_r, ei_r) &&
5881 btrfs_file_extent_disk_bytenr(leaf_l, ei_l) !=
5882 btrfs_file_extent_disk_bytenr(leaf_r, ei_r) &&
5883 btrfs_file_extent_disk_num_bytes(leaf_l, ei_l) ==
5884 btrfs_file_extent_disk_num_bytes(leaf_r, ei_r) &&
5885 btrfs_file_extent_offset(leaf_l, ei_l) ==
5886 btrfs_file_extent_offset(leaf_r, ei_r) &&
5887 btrfs_file_extent_num_bytes(leaf_l, ei_l) ==
5888 btrfs_file_extent_num_bytes(leaf_r, ei_r))
5889 return 0;
5890 }
5891
Filipe Manana95155582016-08-01 01:50:37 +01005892 inconsistent_snapshot_error(sctx, result, "extent");
5893 return -EIO;
5894 }
Alexander Block31db9f72012-07-25 23:19:24 +02005895
5896 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5897 if (result != BTRFS_COMPARE_TREE_DELETED)
5898 ret = process_extent(sctx, sctx->left_path,
5899 sctx->cmp_key);
5900 }
5901
5902 return ret;
5903}
5904
Josef Bacikba5e8f22013-08-16 16:52:55 -04005905static int dir_changed(struct send_ctx *sctx, u64 dir)
5906{
5907 u64 orig_gen, new_gen;
5908 int ret;
5909
5910 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5911 NULL, NULL);
5912 if (ret)
5913 return ret;
5914
5915 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5916 NULL, NULL, NULL);
5917 if (ret)
5918 return ret;
5919
5920 return (orig_gen != new_gen) ? 1 : 0;
5921}
5922
5923static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5924 struct btrfs_key *key)
5925{
5926 struct btrfs_inode_extref *extref;
5927 struct extent_buffer *leaf;
5928 u64 dirid = 0, last_dirid = 0;
5929 unsigned long ptr;
5930 u32 item_size;
5931 u32 cur_offset = 0;
5932 int ref_name_len;
5933 int ret = 0;
5934
5935 /* Easy case, just check this one dirid */
5936 if (key->type == BTRFS_INODE_REF_KEY) {
5937 dirid = key->offset;
5938
5939 ret = dir_changed(sctx, dirid);
5940 goto out;
5941 }
5942
5943 leaf = path->nodes[0];
5944 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5945 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5946 while (cur_offset < item_size) {
5947 extref = (struct btrfs_inode_extref *)(ptr +
5948 cur_offset);
5949 dirid = btrfs_inode_extref_parent(leaf, extref);
5950 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5951 cur_offset += ref_name_len + sizeof(*extref);
5952 if (dirid == last_dirid)
5953 continue;
5954 ret = dir_changed(sctx, dirid);
5955 if (ret)
5956 break;
5957 last_dirid = dirid;
5958 }
5959out:
5960 return ret;
5961}
5962
Alexander Block766702e2012-07-28 14:11:31 +02005963/*
5964 * Updates compare related fields in sctx and simply forwards to the actual
5965 * changed_xxx functions.
5966 */
Alexander Block31db9f72012-07-25 23:19:24 +02005967static int changed_cb(struct btrfs_root *left_root,
5968 struct btrfs_root *right_root,
5969 struct btrfs_path *left_path,
5970 struct btrfs_path *right_path,
5971 struct btrfs_key *key,
5972 enum btrfs_compare_tree_result result,
5973 void *ctx)
5974{
5975 int ret = 0;
5976 struct send_ctx *sctx = ctx;
5977
Josef Bacikba5e8f22013-08-16 16:52:55 -04005978 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005979 if (key->type == BTRFS_INODE_REF_KEY ||
5980 key->type == BTRFS_INODE_EXTREF_KEY) {
5981 ret = compare_refs(sctx, left_path, key);
5982 if (!ret)
5983 return 0;
5984 if (ret < 0)
5985 return ret;
5986 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5987 return maybe_send_hole(sctx, left_path, key);
5988 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005989 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005990 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005991 result = BTRFS_COMPARE_TREE_CHANGED;
5992 ret = 0;
5993 }
5994
Alexander Block31db9f72012-07-25 23:19:24 +02005995 sctx->left_path = left_path;
5996 sctx->right_path = right_path;
5997 sctx->cmp_key = key;
5998
5999 ret = finish_inode_if_needed(sctx, 0);
6000 if (ret < 0)
6001 goto out;
6002
Alexander Block2981e222012-08-01 14:47:03 +02006003 /* Ignore non-FS objects */
6004 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6005 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6006 goto out;
6007
Alexander Block31db9f72012-07-25 23:19:24 +02006008 if (key->type == BTRFS_INODE_ITEM_KEY)
6009 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00006010 else if (key->type == BTRFS_INODE_REF_KEY ||
6011 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02006012 ret = changed_ref(sctx, result);
6013 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6014 ret = changed_xattr(sctx, result);
6015 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6016 ret = changed_extent(sctx, result);
6017
6018out:
6019 return ret;
6020}
6021
6022static int full_send_tree(struct send_ctx *sctx)
6023{
6024 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006025 struct btrfs_root *send_root = sctx->send_root;
6026 struct btrfs_key key;
6027 struct btrfs_key found_key;
6028 struct btrfs_path *path;
6029 struct extent_buffer *eb;
6030 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006031
6032 path = alloc_path_for_send();
6033 if (!path)
6034 return -ENOMEM;
6035
Alexander Block31db9f72012-07-25 23:19:24 +02006036 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6037 key.type = BTRFS_INODE_ITEM_KEY;
6038 key.offset = 0;
6039
Alexander Block31db9f72012-07-25 23:19:24 +02006040 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6041 if (ret < 0)
6042 goto out;
6043 if (ret)
6044 goto out_finish;
6045
6046 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006047 eb = path->nodes[0];
6048 slot = path->slots[0];
6049 btrfs_item_key_to_cpu(eb, &found_key, slot);
6050
6051 ret = changed_cb(send_root, NULL, path, NULL,
6052 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
6053 if (ret < 0)
6054 goto out;
6055
6056 key.objectid = found_key.objectid;
6057 key.type = found_key.type;
6058 key.offset = found_key.offset + 1;
6059
6060 ret = btrfs_next_item(send_root, path);
6061 if (ret < 0)
6062 goto out;
6063 if (ret) {
6064 ret = 0;
6065 break;
6066 }
6067 }
6068
6069out_finish:
6070 ret = finish_inode_if_needed(sctx, 1);
6071
6072out:
6073 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006074 return ret;
6075}
6076
6077static int send_subvol(struct send_ctx *sctx)
6078{
6079 int ret;
6080
Stefan Behrensc2c71322013-04-10 17:10:52 +00006081 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6082 ret = send_header(sctx);
6083 if (ret < 0)
6084 goto out;
6085 }
Alexander Block31db9f72012-07-25 23:19:24 +02006086
6087 ret = send_subvol_begin(sctx);
6088 if (ret < 0)
6089 goto out;
6090
6091 if (sctx->parent_root) {
6092 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6093 changed_cb, sctx);
6094 if (ret < 0)
6095 goto out;
6096 ret = finish_inode_if_needed(sctx, 1);
6097 if (ret < 0)
6098 goto out;
6099 } else {
6100 ret = full_send_tree(sctx);
6101 if (ret < 0)
6102 goto out;
6103 }
6104
6105out:
Alexander Block31db9f72012-07-25 23:19:24 +02006106 free_recorded_refs(sctx);
6107 return ret;
6108}
6109
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006110/*
6111 * If orphan cleanup did remove any orphans from a root, it means the tree
6112 * was modified and therefore the commit root is not the same as the current
6113 * root anymore. This is a problem, because send uses the commit root and
6114 * therefore can see inode items that don't exist in the current root anymore,
6115 * and for example make calls to btrfs_iget, which will do tree lookups based
6116 * on the current root and not on the commit root. Those lookups will fail,
6117 * returning a -ESTALE error, and making send fail with that error. So make
6118 * sure a send does not see any orphans we have just removed, and that it will
6119 * see the same inodes regardless of whether a transaction commit happened
6120 * before it started (meaning that the commit root will be the same as the
6121 * current root) or not.
6122 */
6123static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6124{
6125 int i;
6126 struct btrfs_trans_handle *trans = NULL;
6127
6128again:
6129 if (sctx->parent_root &&
6130 sctx->parent_root->node != sctx->parent_root->commit_root)
6131 goto commit_trans;
6132
6133 for (i = 0; i < sctx->clone_roots_cnt; i++)
6134 if (sctx->clone_roots[i].root->node !=
6135 sctx->clone_roots[i].root->commit_root)
6136 goto commit_trans;
6137
6138 if (trans)
6139 return btrfs_end_transaction(trans, sctx->send_root);
6140
6141 return 0;
6142
6143commit_trans:
6144 /* Use any root, all fs roots will get their commit roots updated. */
6145 if (!trans) {
6146 trans = btrfs_join_transaction(sctx->send_root);
6147 if (IS_ERR(trans))
6148 return PTR_ERR(trans);
6149 goto again;
6150 }
6151
6152 return btrfs_commit_transaction(trans, sctx->send_root);
6153}
6154
David Sterba66ef7d62013-12-17 15:07:20 +01006155static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6156{
6157 spin_lock(&root->root_item_lock);
6158 root->send_in_progress--;
6159 /*
6160 * Not much left to do, we don't know why it's unbalanced and
6161 * can't blindly reset it to 0.
6162 */
6163 if (root->send_in_progress < 0)
6164 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02006165 "send_in_progres unbalanced %d root %llu",
David Sterba66ef7d62013-12-17 15:07:20 +01006166 root->send_in_progress, root->root_key.objectid);
6167 spin_unlock(&root->root_item_lock);
6168}
6169
Alexander Block31db9f72012-07-25 23:19:24 +02006170long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
6171{
6172 int ret = 0;
6173 struct btrfs_root *send_root;
6174 struct btrfs_root *clone_root;
6175 struct btrfs_fs_info *fs_info;
6176 struct btrfs_ioctl_send_args *arg = NULL;
6177 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006178 struct send_ctx *sctx = NULL;
6179 u32 i;
6180 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01006181 int clone_sources_to_rollback = 0;
David Sterbae55d1152016-04-11 18:52:02 +02006182 unsigned alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08006183 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08006184 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02006185
6186 if (!capable(CAP_SYS_ADMIN))
6187 return -EPERM;
6188
Al Viro496ad9a2013-01-23 17:07:38 -05006189 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02006190 fs_info = send_root->fs_info;
6191
Josef Bacik139f8072013-05-20 11:26:50 -04006192 /*
David Sterba2c686532013-12-16 17:34:17 +01006193 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02006194 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01006195 */
6196 spin_lock(&send_root->root_item_lock);
6197 send_root->send_in_progress++;
6198 spin_unlock(&send_root->root_item_lock);
6199
6200 /*
Josef Bacik139f8072013-05-20 11:26:50 -04006201 * This is done when we lookup the root, it should already be complete
6202 * by the time we get here.
6203 */
6204 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6205
6206 /*
David Sterba2c686532013-12-16 17:34:17 +01006207 * Userspace tools do the checks and warn the user if it's
6208 * not RO.
6209 */
6210 if (!btrfs_root_readonly(send_root)) {
6211 ret = -EPERM;
6212 goto out;
6213 }
6214
Alexander Block31db9f72012-07-25 23:19:24 +02006215 arg = memdup_user(arg_, sizeof(*arg));
6216 if (IS_ERR(arg)) {
6217 ret = PTR_ERR(arg);
6218 arg = NULL;
6219 goto out;
6220 }
6221
Dan Carpenter9c1433b2017-03-17 23:51:20 +03006222 /*
6223 * Check that we don't overflow at later allocations, we request
6224 * clone_sources_count + 1 items, and compare to unsigned long inside
6225 * access_ok.
6226 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006227 if (arg->clone_sources_count >
Dan Carpenter9c1433b2017-03-17 23:51:20 +03006228 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006229 ret = -EINVAL;
6230 goto out;
6231 }
6232
Alexander Block31db9f72012-07-25 23:19:24 +02006233 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05006234 sizeof(*arg->clone_sources) *
6235 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02006236 ret = -EFAULT;
6237 goto out;
6238 }
6239
Stefan Behrensc2c71322013-04-10 17:10:52 +00006240 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006241 ret = -EINVAL;
6242 goto out;
6243 }
6244
David Sterbae780b0d2016-01-18 18:42:13 +01006245 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006246 if (!sctx) {
6247 ret = -ENOMEM;
6248 goto out;
6249 }
6250
6251 INIT_LIST_HEAD(&sctx->new_refs);
6252 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01006253 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006254 INIT_LIST_HEAD(&sctx->name_cache_list);
6255
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006256 sctx->flags = arg->flags;
6257
Alexander Block31db9f72012-07-25 23:19:24 +02006258 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00006259 if (!sctx->send_filp) {
6260 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02006261 goto out;
6262 }
6263
Alexander Block31db9f72012-07-25 23:19:24 +02006264 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02006265 /*
6266 * Unlikely but possible, if the subvolume is marked for deletion but
6267 * is slow to remove the directory entry, send can still be started
6268 */
6269 if (btrfs_root_dead(sctx->send_root)) {
6270 ret = -EPERM;
6271 goto out;
6272 }
6273
Alexander Block31db9f72012-07-25 23:19:24 +02006274 sctx->clone_roots_cnt = arg->clone_sources_count;
6275
6276 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
David Sterba6ff48ce2016-04-11 18:40:08 +02006277 sctx->send_buf = kmalloc(sctx->send_max_size, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006278 if (!sctx->send_buf) {
David Sterba6ff48ce2016-04-11 18:40:08 +02006279 sctx->send_buf = vmalloc(sctx->send_max_size);
6280 if (!sctx->send_buf) {
6281 ret = -ENOMEM;
6282 goto out;
6283 }
Alexander Block31db9f72012-07-25 23:19:24 +02006284 }
6285
David Sterbaeb5b75f2016-04-11 18:40:08 +02006286 sctx->read_buf = kmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006287 if (!sctx->read_buf) {
David Sterbaeb5b75f2016-04-11 18:40:08 +02006288 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
6289 if (!sctx->read_buf) {
6290 ret = -ENOMEM;
6291 goto out;
6292 }
Alexander Block31db9f72012-07-25 23:19:24 +02006293 }
6294
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006295 sctx->pending_dir_moves = RB_ROOT;
6296 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00006297 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006298
David Sterbae55d1152016-04-11 18:52:02 +02006299 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6300
David Sterbac03d01f2016-04-11 18:40:08 +02006301 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006302 if (!sctx->clone_roots) {
David Sterbac03d01f2016-04-11 18:40:08 +02006303 sctx->clone_roots = vzalloc(alloc_size);
6304 if (!sctx->clone_roots) {
6305 ret = -ENOMEM;
6306 goto out;
6307 }
Alexander Block31db9f72012-07-25 23:19:24 +02006308 }
6309
David Sterbae55d1152016-04-11 18:52:02 +02006310 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6311
Alexander Block31db9f72012-07-25 23:19:24 +02006312 if (arg->clone_sources_count) {
David Sterba2f913062016-04-11 18:40:08 +02006313 clone_sources_tmp = kmalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006314 if (!clone_sources_tmp) {
David Sterba2f913062016-04-11 18:40:08 +02006315 clone_sources_tmp = vmalloc(alloc_size);
6316 if (!clone_sources_tmp) {
6317 ret = -ENOMEM;
6318 goto out;
6319 }
Alexander Block31db9f72012-07-25 23:19:24 +02006320 }
6321
6322 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02006323 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02006324 if (ret) {
6325 ret = -EFAULT;
6326 goto out;
6327 }
6328
6329 for (i = 0; i < arg->clone_sources_count; i++) {
6330 key.objectid = clone_sources_tmp[i];
6331 key.type = BTRFS_ROOT_ITEM_KEY;
6332 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006333
6334 index = srcu_read_lock(&fs_info->subvol_srcu);
6335
Alexander Block31db9f72012-07-25 23:19:24 +02006336 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02006337 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006338 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006339 ret = PTR_ERR(clone_root);
6340 goto out;
6341 }
David Sterba2c686532013-12-16 17:34:17 +01006342 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00006343 if (!btrfs_root_readonly(clone_root) ||
6344 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006345 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006346 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006347 ret = -EPERM;
6348 goto out;
6349 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00006350 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01006351 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006352 srcu_read_unlock(&fs_info->subvol_srcu, index);
6353
Alexander Block31db9f72012-07-25 23:19:24 +02006354 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00006355 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006356 }
David Sterba2f913062016-04-11 18:40:08 +02006357 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006358 clone_sources_tmp = NULL;
6359 }
6360
6361 if (arg->parent_root) {
6362 key.objectid = arg->parent_root;
6363 key.type = BTRFS_ROOT_ITEM_KEY;
6364 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006365
6366 index = srcu_read_lock(&fs_info->subvol_srcu);
6367
Alexander Block31db9f72012-07-25 23:19:24 +02006368 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006369 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006370 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006371 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02006372 goto out;
6373 }
Wang Shilong18f687d2014-01-07 17:25:19 +08006374
David Sterba2c686532013-12-16 17:34:17 +01006375 spin_lock(&sctx->parent_root->root_item_lock);
6376 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02006377 if (!btrfs_root_readonly(sctx->parent_root) ||
6378 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006379 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006380 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006381 ret = -EPERM;
6382 goto out;
6383 }
6384 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006385
6386 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006387 }
6388
6389 /*
6390 * Clones from send_root are allowed, but only if the clone source
6391 * is behind the current send position. This is checked while searching
6392 * for possible clone sources.
6393 */
6394 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6395
6396 /* We do a bsearch later */
6397 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6398 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6399 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08006400 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006401
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006402 ret = ensure_commit_roots_uptodate(sctx);
6403 if (ret)
6404 goto out;
6405
David Sterba2755a0d2014-07-31 00:43:18 +02006406 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02006407 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04006408 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02006409 if (ret < 0)
6410 goto out;
6411
Stefan Behrensc2c71322013-04-10 17:10:52 +00006412 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6413 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6414 if (ret < 0)
6415 goto out;
6416 ret = send_cmd(sctx);
6417 if (ret < 0)
6418 goto out;
6419 }
Alexander Block31db9f72012-07-25 23:19:24 +02006420
6421out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006422 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6423 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6424 struct rb_node *n;
6425 struct pending_dir_move *pm;
6426
6427 n = rb_first(&sctx->pending_dir_moves);
6428 pm = rb_entry(n, struct pending_dir_move, node);
6429 while (!list_empty(&pm->list)) {
6430 struct pending_dir_move *pm2;
6431
6432 pm2 = list_first_entry(&pm->list,
6433 struct pending_dir_move, list);
6434 free_pending_move(sctx, pm2);
6435 }
6436 free_pending_move(sctx, pm);
6437 }
6438
6439 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6440 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6441 struct rb_node *n;
6442 struct waiting_dir_move *dm;
6443
6444 n = rb_first(&sctx->waiting_dir_moves);
6445 dm = rb_entry(n, struct waiting_dir_move, node);
6446 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6447 kfree(dm);
6448 }
6449
Filipe Manana9dc44212014-02-19 14:31:44 +00006450 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6451 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6452 struct rb_node *n;
6453 struct orphan_dir_info *odi;
6454
6455 n = rb_first(&sctx->orphan_dirs);
6456 odi = rb_entry(n, struct orphan_dir_info, node);
6457 free_orphan_dir_info(sctx, odi);
6458 }
6459
Wang Shilong896c14f2014-01-07 17:25:18 +08006460 if (sort_clone_roots) {
6461 for (i = 0; i < sctx->clone_roots_cnt; i++)
6462 btrfs_root_dec_send_in_progress(
6463 sctx->clone_roots[i].root);
6464 } else {
6465 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6466 btrfs_root_dec_send_in_progress(
6467 sctx->clone_roots[i].root);
6468
6469 btrfs_root_dec_send_in_progress(send_root);
6470 }
David Sterba66ef7d62013-12-17 15:07:20 +01006471 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6472 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01006473
Alexander Block31db9f72012-07-25 23:19:24 +02006474 kfree(arg);
David Sterba2f913062016-04-11 18:40:08 +02006475 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006476
6477 if (sctx) {
6478 if (sctx->send_filp)
6479 fput(sctx->send_filp);
6480
David Sterbac03d01f2016-04-11 18:40:08 +02006481 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02006482 kvfree(sctx->send_buf);
David Sterbaeb5b75f2016-04-11 18:40:08 +02006483 kvfree(sctx->read_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02006484
6485 name_cache_free(sctx);
6486
6487 kfree(sctx);
6488 }
6489
6490 return ret;
6491}