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