blob: edfc7ba38b33e44fa16cd48d3b1c5549929bf410 [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/*
Filipe Mananab6af12a2019-10-30 12:23:01 +000040 * Maximum number of references an extent can have in order for us to attempt to
41 * issue clone operations instead of write operations. This currently exists to
42 * avoid hitting limitations of the backreference walking code (taking a lot of
43 * time and using too much memory for extents with large number of references).
44 */
45#define SEND_MAX_EXTENT_REFS 64
46
47/*
Alexander Block31db9f72012-07-25 23:19:24 +020048 * A fs_path is a helper to dynamically build path names with unknown size.
49 * It reallocates the internal buffer on demand.
50 * It allows fast adding of path elements on the right side (normal path) and
51 * fast adding to the left side (reversed path). A reversed path can also be
52 * unreversed if needed.
53 */
54struct fs_path {
55 union {
56 struct {
57 char *start;
58 char *end;
Alexander Block31db9f72012-07-25 23:19:24 +020059
60 char *buf;
David Sterba1f5a7ff2014-02-03 19:23:47 +010061 unsigned short buf_len:15;
62 unsigned short reversed:1;
Alexander Block31db9f72012-07-25 23:19:24 +020063 char inline_buf[];
64 };
David Sterbaace01052014-02-05 16:17:34 +010065 /*
66 * Average path length does not exceed 200 bytes, we'll have
67 * better packing in the slab and higher chance to satisfy
68 * a allocation later during send.
69 */
70 char pad[256];
Alexander Block31db9f72012-07-25 23:19:24 +020071 };
72};
73#define FS_PATH_INLINE_SIZE \
74 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
75
76
77/* reused for each extent */
78struct clone_root {
79 struct btrfs_root *root;
80 u64 ino;
81 u64 offset;
82
83 u64 found_refs;
84};
85
86#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
87#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
88
89struct send_ctx {
90 struct file *send_filp;
91 loff_t send_off;
92 char *send_buf;
93 u32 send_size;
94 u32 send_max_size;
95 u64 total_send_size;
96 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000097 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020098
Alexander Block31db9f72012-07-25 23:19:24 +020099 struct btrfs_root *send_root;
100 struct btrfs_root *parent_root;
101 struct clone_root *clone_roots;
102 int clone_roots_cnt;
103
104 /* current state of the compare_tree call */
105 struct btrfs_path *left_path;
106 struct btrfs_path *right_path;
107 struct btrfs_key *cmp_key;
108
109 /*
110 * infos of the currently processed inode. In case of deleted inodes,
111 * these are the values from the deleted inode.
112 */
113 u64 cur_ino;
114 u64 cur_inode_gen;
115 int cur_inode_new;
116 int cur_inode_new_gen;
117 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200118 u64 cur_inode_size;
119 u64 cur_inode_mode;
Liu Bo644d1942014-02-27 17:29:01 +0800120 u64 cur_inode_rdev;
Josef Bacik16e75492013-10-22 12:18:51 -0400121 u64 cur_inode_last_extent;
Alexander Block31db9f72012-07-25 23:19:24 +0200122
123 u64 send_progress;
124
125 struct list_head new_refs;
126 struct list_head deleted_refs;
127
128 struct radix_tree_root name_cache;
129 struct list_head name_cache_list;
130 int name_cache_size;
131
Liu Bo2131bcd32014-03-05 10:07:35 +0800132 struct file_ra_state ra;
133
Alexander Block31db9f72012-07-25 23:19:24 +0200134 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000135
136 /*
137 * We process inodes by their increasing order, so if before an
138 * incremental send we reverse the parent/child relationship of
139 * directories such that a directory with a lower inode number was
140 * the parent of a directory with a higher inode number, and the one
141 * becoming the new parent got renamed too, we can't rename/move the
142 * directory with lower inode number when we finish processing it - we
143 * must process the directory with higher inode number first, then
144 * rename/move it and then rename/move the directory with lower inode
145 * number. Example follows.
146 *
147 * Tree state when the first send was performed:
148 *
149 * .
150 * |-- a (ino 257)
151 * |-- b (ino 258)
152 * |
153 * |
154 * |-- c (ino 259)
155 * | |-- d (ino 260)
156 * |
157 * |-- c2 (ino 261)
158 *
159 * Tree state when the second (incremental) send is performed:
160 *
161 * .
162 * |-- a (ino 257)
163 * |-- b (ino 258)
164 * |-- c2 (ino 261)
165 * |-- d2 (ino 260)
166 * |-- cc (ino 259)
167 *
168 * The sequence of steps that lead to the second state was:
169 *
170 * mv /a/b/c/d /a/b/c2/d2
171 * mv /a/b/c /a/b/c2/d2/cc
172 *
173 * "c" has lower inode number, but we can't move it (2nd mv operation)
174 * before we move "d", which has higher inode number.
175 *
176 * So we just memorize which move/rename operations must be performed
177 * later when their respective parent is processed and moved/renamed.
178 */
179
180 /* Indexed by parent directory inode number. */
181 struct rb_root pending_dir_moves;
182
183 /*
184 * Reverse index, indexed by the inode number of a directory that
185 * is waiting for the move/rename of its immediate parent before its
186 * own move/rename can be performed.
187 */
188 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000189
190 /*
191 * A directory that is going to be rm'ed might have a child directory
192 * which is in the pending directory moves index above. In this case,
193 * the directory can only be removed after the move/rename of its child
194 * is performed. Example:
195 *
196 * Parent snapshot:
197 *
198 * . (ino 256)
199 * |-- a/ (ino 257)
200 * |-- b/ (ino 258)
201 * |-- c/ (ino 259)
202 * | |-- x/ (ino 260)
203 * |
204 * |-- y/ (ino 261)
205 *
206 * Send snapshot:
207 *
208 * . (ino 256)
209 * |-- a/ (ino 257)
210 * |-- b/ (ino 258)
211 * |-- YY/ (ino 261)
212 * |-- x/ (ino 260)
213 *
214 * Sequence of steps that lead to the send snapshot:
215 * rm -f /a/b/c/foo.txt
216 * mv /a/b/y /a/b/YY
217 * mv /a/b/c/x /a/b/YY
218 * rmdir /a/b/c
219 *
220 * When the child is processed, its move/rename is delayed until its
221 * parent is processed (as explained above), but all other operations
222 * like update utimes, chown, chgrp, etc, are performed and the paths
223 * that it uses for those operations must use the orphanized name of
224 * its parent (the directory we're going to rm later), so we need to
225 * memorize that name.
226 *
227 * Indexed by the inode number of the directory to be deleted.
228 */
229 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000230};
231
232struct pending_dir_move {
233 struct rb_node node;
234 struct list_head list;
235 u64 parent_ino;
236 u64 ino;
237 u64 gen;
238 struct list_head update_refs;
239};
240
241struct waiting_dir_move {
242 struct rb_node node;
243 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000244 /*
245 * There might be some directory that could not be removed because it
246 * was waiting for this directory inode to be moved first. Therefore
247 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
248 */
249 u64 rmdir_ino;
Filipe Manana8b191a62015-04-09 14:09:14 +0100250 bool orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +0000251};
252
253struct orphan_dir_info {
254 struct rb_node node;
255 u64 ino;
256 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +0200257};
258
259struct name_cache_entry {
260 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200261 /*
262 * radix_tree has only 32bit entries but we need to handle 64bit inums.
263 * We use the lower 32bit of the 64bit inum to store it in the tree. If
264 * more then one inum would fall into the same entry, we use radix_list
265 * to store the additional entries. radix_list is also used to store
266 * entries where two entries have the same inum but different
267 * generations.
268 */
269 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200270 u64 ino;
271 u64 gen;
272 u64 parent_ino;
273 u64 parent_gen;
274 int ret;
275 int need_later_update;
276 int name_len;
277 char name[];
278};
279
Filipe Manana95155582016-08-01 01:50:37 +0100280static void inconsistent_snapshot_error(struct send_ctx *sctx,
281 enum btrfs_compare_tree_result result,
282 const char *what)
283{
284 const char *result_string;
285
286 switch (result) {
287 case BTRFS_COMPARE_TREE_NEW:
288 result_string = "new";
289 break;
290 case BTRFS_COMPARE_TREE_DELETED:
291 result_string = "deleted";
292 break;
293 case BTRFS_COMPARE_TREE_CHANGED:
294 result_string = "updated";
295 break;
296 case BTRFS_COMPARE_TREE_SAME:
297 ASSERT(0);
298 result_string = "unchanged";
299 break;
300 default:
301 ASSERT(0);
302 result_string = "unexpected";
303 }
304
305 btrfs_err(sctx->send_root->fs_info,
306 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
307 result_string, what, sctx->cmp_key->objectid,
308 sctx->send_root->root_key.objectid,
309 (sctx->parent_root ?
310 sctx->parent_root->root_key.objectid : 0));
311}
312
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000313static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
314
Filipe Manana9dc44212014-02-19 14:31:44 +0000315static struct waiting_dir_move *
316get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
317
318static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
319
Josef Bacik16e75492013-10-22 12:18:51 -0400320static int need_send_hole(struct send_ctx *sctx)
321{
322 return (sctx->parent_root && !sctx->cur_inode_new &&
323 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
324 S_ISREG(sctx->cur_inode_mode));
325}
326
Alexander Block31db9f72012-07-25 23:19:24 +0200327static void fs_path_reset(struct fs_path *p)
328{
329 if (p->reversed) {
330 p->start = p->buf + p->buf_len - 1;
331 p->end = p->start;
332 *p->start = 0;
333 } else {
334 p->start = p->buf;
335 p->end = p->start;
336 *p->start = 0;
337 }
338}
339
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000340static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200341{
342 struct fs_path *p;
343
David Sterbae780b0d2016-01-18 18:42:13 +0100344 p = kmalloc(sizeof(*p), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +0200345 if (!p)
346 return NULL;
347 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200348 p->buf = p->inline_buf;
349 p->buf_len = FS_PATH_INLINE_SIZE;
350 fs_path_reset(p);
351 return p;
352}
353
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000354static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200355{
356 struct fs_path *p;
357
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000358 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200359 if (!p)
360 return NULL;
361 p->reversed = 1;
362 fs_path_reset(p);
363 return p;
364}
365
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000366static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200367{
368 if (!p)
369 return;
David Sterbaace01052014-02-05 16:17:34 +0100370 if (p->buf != p->inline_buf)
371 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200372 kfree(p);
373}
374
375static int fs_path_len(struct fs_path *p)
376{
377 return p->end - p->start;
378}
379
380static int fs_path_ensure_buf(struct fs_path *p, int len)
381{
382 char *tmp_buf;
383 int path_len;
384 int old_buf_len;
385
386 len++;
387
388 if (p->buf_len >= len)
389 return 0;
390
Chris Masoncfd4a532014-04-26 05:02:03 -0700391 if (len > PATH_MAX) {
392 WARN_ON(1);
393 return -ENOMEM;
394 }
395
David Sterba1b2782c2014-02-25 19:32:59 +0100396 path_len = p->end - p->start;
397 old_buf_len = p->buf_len;
398
David Sterbaace01052014-02-05 16:17:34 +0100399 /*
400 * First time the inline_buf does not suffice
401 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100402 if (p->buf == p->inline_buf) {
David Sterbae780b0d2016-01-18 18:42:13 +0100403 tmp_buf = kmalloc(len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100404 if (tmp_buf)
405 memcpy(tmp_buf, p->buf, old_buf_len);
406 } else {
David Sterbae780b0d2016-01-18 18:42:13 +0100407 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100408 }
David Sterba9c9ca002014-02-25 19:33:08 +0100409 if (!tmp_buf)
410 return -ENOMEM;
411 p->buf = tmp_buf;
412 /*
413 * The real size of the buffer is bigger, this will let the fast path
414 * happen most of the time
415 */
416 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100417
Alexander Block31db9f72012-07-25 23:19:24 +0200418 if (p->reversed) {
419 tmp_buf = p->buf + old_buf_len - path_len - 1;
420 p->end = p->buf + p->buf_len - 1;
421 p->start = p->end - path_len;
422 memmove(p->start, tmp_buf, path_len + 1);
423 } else {
424 p->start = p->buf;
425 p->end = p->start + path_len;
426 }
427 return 0;
428}
429
David Sterbab23ab572014-02-03 19:23:19 +0100430static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
431 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200432{
433 int ret;
434 int new_len;
435
436 new_len = p->end - p->start + name_len;
437 if (p->start != p->end)
438 new_len++;
439 ret = fs_path_ensure_buf(p, new_len);
440 if (ret < 0)
441 goto out;
442
443 if (p->reversed) {
444 if (p->start != p->end)
445 *--p->start = '/';
446 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100447 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200448 } else {
449 if (p->start != p->end)
450 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100451 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200452 p->end += name_len;
453 *p->end = 0;
454 }
455
456out:
457 return ret;
458}
459
460static int fs_path_add(struct fs_path *p, const char *name, int name_len)
461{
462 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100463 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200464
David Sterbab23ab572014-02-03 19:23:19 +0100465 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200466 if (ret < 0)
467 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100468 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200469
470out:
471 return ret;
472}
473
474static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
475{
476 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100477 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200478
David Sterbab23ab572014-02-03 19:23:19 +0100479 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200480 if (ret < 0)
481 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100482 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200483
484out:
485 return ret;
486}
487
488static int fs_path_add_from_extent_buffer(struct fs_path *p,
489 struct extent_buffer *eb,
490 unsigned long off, int len)
491{
492 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100493 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200494
David Sterbab23ab572014-02-03 19:23:19 +0100495 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200496 if (ret < 0)
497 goto out;
498
David Sterbab23ab572014-02-03 19:23:19 +0100499 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200500
501out:
502 return ret;
503}
504
Alexander Block31db9f72012-07-25 23:19:24 +0200505static int fs_path_copy(struct fs_path *p, struct fs_path *from)
506{
507 int ret;
508
509 p->reversed = from->reversed;
510 fs_path_reset(p);
511
512 ret = fs_path_add_path(p, from);
513
514 return ret;
515}
516
517
518static void fs_path_unreverse(struct fs_path *p)
519{
520 char *tmp;
521 int len;
522
523 if (!p->reversed)
524 return;
525
526 tmp = p->start;
527 len = p->end - p->start;
528 p->start = p->buf;
529 p->end = p->start + len;
530 memmove(p->start, tmp, len + 1);
531 p->reversed = 0;
532}
533
534static struct btrfs_path *alloc_path_for_send(void)
535{
536 struct btrfs_path *path;
537
538 path = btrfs_alloc_path();
539 if (!path)
540 return NULL;
541 path->search_commit_root = 1;
542 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400543 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200544 return path;
545}
546
Eric Sandeen48a3b632013-04-25 20:41:01 +0000547static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200548{
549 int ret;
550 mm_segment_t old_fs;
551 u32 pos = 0;
552
553 old_fs = get_fs();
554 set_fs(KERNEL_DS);
555
556 while (pos < len) {
Fabian Frederickd447d0d2014-07-15 21:17:17 +0200557 ret = vfs_write(filp, (__force const char __user *)buf + pos,
558 len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200559 /* TODO handle that correctly */
560 /*if (ret == -ERESTARTSYS) {
561 continue;
562 }*/
563 if (ret < 0)
564 goto out;
565 if (ret == 0) {
566 ret = -EIO;
567 goto out;
568 }
569 pos += ret;
570 }
571
572 ret = 0;
573
574out:
575 set_fs(old_fs);
576 return ret;
577}
578
579static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
580{
581 struct btrfs_tlv_header *hdr;
582 int total_len = sizeof(*hdr) + len;
583 int left = sctx->send_max_size - sctx->send_size;
584
585 if (unlikely(left < total_len))
586 return -EOVERFLOW;
587
588 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
589 hdr->tlv_type = cpu_to_le16(attr);
590 hdr->tlv_len = cpu_to_le16(len);
591 memcpy(hdr + 1, data, len);
592 sctx->send_size += total_len;
593
594 return 0;
595}
596
David Sterba95bc79d2013-12-16 17:34:10 +0100597#define TLV_PUT_DEFINE_INT(bits) \
598 static int tlv_put_u##bits(struct send_ctx *sctx, \
599 u##bits attr, u##bits value) \
600 { \
601 __le##bits __tmp = cpu_to_le##bits(value); \
602 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
603 }
Alexander Block31db9f72012-07-25 23:19:24 +0200604
David Sterba95bc79d2013-12-16 17:34:10 +0100605TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200606
607static int tlv_put_string(struct send_ctx *sctx, u16 attr,
608 const char *str, int len)
609{
610 if (len == -1)
611 len = strlen(str);
612 return tlv_put(sctx, attr, str, len);
613}
614
615static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
616 const u8 *uuid)
617{
618 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
619}
620
Alexander Block31db9f72012-07-25 23:19:24 +0200621static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
622 struct extent_buffer *eb,
623 struct btrfs_timespec *ts)
624{
625 struct btrfs_timespec bts;
626 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
627 return tlv_put(sctx, attr, &bts, sizeof(bts));
628}
629
630
631#define TLV_PUT(sctx, attrtype, attrlen, data) \
632 do { \
633 ret = tlv_put(sctx, attrtype, attrlen, data); \
634 if (ret < 0) \
635 goto tlv_put_failure; \
636 } while (0)
637
638#define TLV_PUT_INT(sctx, attrtype, bits, value) \
639 do { \
640 ret = tlv_put_u##bits(sctx, attrtype, value); \
641 if (ret < 0) \
642 goto tlv_put_failure; \
643 } while (0)
644
645#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
646#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
647#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
648#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
649#define TLV_PUT_STRING(sctx, attrtype, str, len) \
650 do { \
651 ret = tlv_put_string(sctx, attrtype, str, len); \
652 if (ret < 0) \
653 goto tlv_put_failure; \
654 } while (0)
655#define TLV_PUT_PATH(sctx, attrtype, p) \
656 do { \
657 ret = tlv_put_string(sctx, attrtype, p->start, \
658 p->end - p->start); \
659 if (ret < 0) \
660 goto tlv_put_failure; \
661 } while(0)
662#define TLV_PUT_UUID(sctx, attrtype, uuid) \
663 do { \
664 ret = tlv_put_uuid(sctx, attrtype, uuid); \
665 if (ret < 0) \
666 goto tlv_put_failure; \
667 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200668#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
669 do { \
670 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
671 if (ret < 0) \
672 goto tlv_put_failure; \
673 } while (0)
674
675static int send_header(struct send_ctx *sctx)
676{
677 struct btrfs_stream_header hdr;
678
679 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
680 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
681
Anand Jain1bcea352012-09-14 00:04:21 -0600682 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
683 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200684}
685
686/*
687 * For each command/item we want to send to userspace, we call this function.
688 */
689static int begin_cmd(struct send_ctx *sctx, int cmd)
690{
691 struct btrfs_cmd_header *hdr;
692
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530693 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200694 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200695
696 BUG_ON(sctx->send_size);
697
698 sctx->send_size += sizeof(*hdr);
699 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
700 hdr->cmd = cpu_to_le16(cmd);
701
702 return 0;
703}
704
705static int send_cmd(struct send_ctx *sctx)
706{
707 int ret;
708 struct btrfs_cmd_header *hdr;
709 u32 crc;
710
711 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
712 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
713 hdr->crc = 0;
714
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000715 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200716 hdr->crc = cpu_to_le32(crc);
717
Anand Jain1bcea352012-09-14 00:04:21 -0600718 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
719 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200720
721 sctx->total_send_size += sctx->send_size;
722 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
723 sctx->send_size = 0;
724
725 return ret;
726}
727
728/*
729 * Sends a move instruction to user space
730 */
731static int send_rename(struct send_ctx *sctx,
732 struct fs_path *from, struct fs_path *to)
733{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400734 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200735 int ret;
736
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400737 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200738
739 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
740 if (ret < 0)
741 goto out;
742
743 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
744 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
745
746 ret = send_cmd(sctx);
747
748tlv_put_failure:
749out:
750 return ret;
751}
752
753/*
754 * Sends a link instruction to user space
755 */
756static int send_link(struct send_ctx *sctx,
757 struct fs_path *path, struct fs_path *lnk)
758{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400759 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200760 int ret;
761
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400762 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200763
764 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
765 if (ret < 0)
766 goto out;
767
768 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
769 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
770
771 ret = send_cmd(sctx);
772
773tlv_put_failure:
774out:
775 return ret;
776}
777
778/*
779 * Sends an unlink instruction to user space
780 */
781static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
782{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400783 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200784 int ret;
785
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400786 btrfs_debug(fs_info, "send_unlink %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200787
788 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
789 if (ret < 0)
790 goto out;
791
792 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
793
794 ret = send_cmd(sctx);
795
796tlv_put_failure:
797out:
798 return ret;
799}
800
801/*
802 * Sends a rmdir instruction to user space
803 */
804static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
805{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400806 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200807 int ret;
808
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400809 btrfs_debug(fs_info, "send_rmdir %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200810
811 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
812 if (ret < 0)
813 goto out;
814
815 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
816
817 ret = send_cmd(sctx);
818
819tlv_put_failure:
820out:
821 return ret;
822}
823
824/*
825 * Helper function to retrieve some fields from an inode item.
826 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400827static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
828 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
829 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200830{
831 int ret;
832 struct btrfs_inode_item *ii;
833 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200834
835 key.objectid = ino;
836 key.type = BTRFS_INODE_ITEM_KEY;
837 key.offset = 0;
838 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200839 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400840 if (ret > 0)
841 ret = -ENOENT;
842 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200843 }
844
845 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
846 struct btrfs_inode_item);
847 if (size)
848 *size = btrfs_inode_size(path->nodes[0], ii);
849 if (gen)
850 *gen = btrfs_inode_generation(path->nodes[0], ii);
851 if (mode)
852 *mode = btrfs_inode_mode(path->nodes[0], ii);
853 if (uid)
854 *uid = btrfs_inode_uid(path->nodes[0], ii);
855 if (gid)
856 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200857 if (rdev)
858 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200859
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400860 return ret;
861}
862
863static int get_inode_info(struct btrfs_root *root,
864 u64 ino, u64 *size, u64 *gen,
865 u64 *mode, u64 *uid, u64 *gid,
866 u64 *rdev)
867{
868 struct btrfs_path *path;
869 int ret;
870
871 path = alloc_path_for_send();
872 if (!path)
873 return -ENOMEM;
874 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
875 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200876 btrfs_free_path(path);
877 return ret;
878}
879
880typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
881 struct fs_path *p,
882 void *ctx);
883
884/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000885 * Helper function to iterate the entries in ONE btrfs_inode_ref or
886 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200887 * The iterate callback may return a non zero value to stop iteration. This can
888 * be a negative value for error codes or 1 to simply stop it.
889 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000890 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200891 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000892static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200893 struct btrfs_key *found_key, int resolve,
894 iterate_inode_ref_t iterate, void *ctx)
895{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000896 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200897 struct btrfs_item *item;
898 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000899 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200900 struct btrfs_path *tmp_path;
901 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000902 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200903 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000904 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200905 u32 name_len;
906 char *start;
907 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000908 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200909 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000910 u64 dir;
911 unsigned long name_off;
912 unsigned long elem_size;
913 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200914
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000915 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200916 if (!p)
917 return -ENOMEM;
918
919 tmp_path = alloc_path_for_send();
920 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000921 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200922 return -ENOMEM;
923 }
924
Alexander Block31db9f72012-07-25 23:19:24 +0200925
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000926 if (found_key->type == BTRFS_INODE_REF_KEY) {
927 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
928 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100929 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000930 total = btrfs_item_size(eb, item);
931 elem_size = sizeof(*iref);
932 } else {
933 ptr = btrfs_item_ptr_offset(eb, slot);
934 total = btrfs_item_size_nr(eb, slot);
935 elem_size = sizeof(*extref);
936 }
937
Alexander Block31db9f72012-07-25 23:19:24 +0200938 while (cur < total) {
939 fs_path_reset(p);
940
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000941 if (found_key->type == BTRFS_INODE_REF_KEY) {
942 iref = (struct btrfs_inode_ref *)(ptr + cur);
943 name_len = btrfs_inode_ref_name_len(eb, iref);
944 name_off = (unsigned long)(iref + 1);
945 index = btrfs_inode_ref_index(eb, iref);
946 dir = found_key->offset;
947 } else {
948 extref = (struct btrfs_inode_extref *)(ptr + cur);
949 name_len = btrfs_inode_extref_name_len(eb, extref);
950 name_off = (unsigned long)&extref->name;
951 index = btrfs_inode_extref_index(eb, extref);
952 dir = btrfs_inode_extref_parent(eb, extref);
953 }
954
Alexander Block31db9f72012-07-25 23:19:24 +0200955 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000956 start = btrfs_ref_to_path(root, tmp_path, name_len,
957 name_off, eb, dir,
958 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200959 if (IS_ERR(start)) {
960 ret = PTR_ERR(start);
961 goto out;
962 }
963 if (start < p->buf) {
964 /* overflow , try again with larger buffer */
965 ret = fs_path_ensure_buf(p,
966 p->buf_len + p->buf - start);
967 if (ret < 0)
968 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000969 start = btrfs_ref_to_path(root, tmp_path,
970 name_len, name_off,
971 eb, dir,
972 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200973 if (IS_ERR(start)) {
974 ret = PTR_ERR(start);
975 goto out;
976 }
977 BUG_ON(start < p->buf);
978 }
979 p->start = start;
980 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000981 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
982 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200983 if (ret < 0)
984 goto out;
985 }
986
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000987 cur += elem_size + name_len;
988 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200989 if (ret)
990 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200991 num++;
992 }
993
994out:
995 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000996 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200997 return ret;
998}
999
1000typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
1001 const char *name, int name_len,
1002 const char *data, int data_len,
1003 u8 type, void *ctx);
1004
1005/*
1006 * Helper function to iterate the entries in ONE btrfs_dir_item.
1007 * The iterate callback may return a non zero value to stop iteration. This can
1008 * be a negative value for error codes or 1 to simply stop it.
1009 *
1010 * path must point to the dir item when called.
1011 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001012static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +02001013 struct btrfs_key *found_key,
1014 iterate_dir_item_t iterate, void *ctx)
1015{
1016 int ret = 0;
1017 struct extent_buffer *eb;
1018 struct btrfs_item *item;
1019 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +02001020 struct btrfs_key di_key;
1021 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +01001022 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +02001023 u32 name_len;
1024 u32 data_len;
1025 u32 cur;
1026 u32 len;
1027 u32 total;
1028 int slot;
1029 int num;
1030 u8 type;
1031
Filipe Manana4395e0c2014-08-20 10:45:45 +01001032 /*
1033 * Start with a small buffer (1 page). If later we end up needing more
1034 * space, which can happen for xattrs on a fs with a leaf size greater
1035 * then the page size, attempt to increase the buffer. Typically xattr
1036 * values are small.
1037 */
1038 buf_len = PATH_MAX;
David Sterbae780b0d2016-01-18 18:42:13 +01001039 buf = kmalloc(buf_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02001040 if (!buf) {
1041 ret = -ENOMEM;
1042 goto out;
1043 }
1044
Alexander Block31db9f72012-07-25 23:19:24 +02001045 eb = path->nodes[0];
1046 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001047 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001048 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1049 cur = 0;
1050 len = 0;
1051 total = btrfs_item_size(eb, item);
1052
1053 num = 0;
1054 while (cur < total) {
1055 name_len = btrfs_dir_name_len(eb, di);
1056 data_len = btrfs_dir_data_len(eb, di);
1057 type = btrfs_dir_type(eb, di);
1058 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1059
Filipe Manana7e3ae332014-05-23 20:15:16 +01001060 if (type == BTRFS_FT_XATTR) {
1061 if (name_len > XATTR_NAME_MAX) {
1062 ret = -ENAMETOOLONG;
1063 goto out;
1064 }
Filipe Manana4395e0c2014-08-20 10:45:45 +01001065 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001066 ret = -E2BIG;
1067 goto out;
1068 }
1069 } else {
1070 /*
1071 * Path too long
1072 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001073 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001074 ret = -ENAMETOOLONG;
1075 goto out;
1076 }
Alexander Block31db9f72012-07-25 23:19:24 +02001077 }
1078
Filipe Manana4395e0c2014-08-20 10:45:45 +01001079 if (name_len + data_len > buf_len) {
1080 buf_len = name_len + data_len;
1081 if (is_vmalloc_addr(buf)) {
1082 vfree(buf);
1083 buf = NULL;
1084 } else {
1085 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001086 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001087
1088 if (!tmp)
1089 kfree(buf);
1090 buf = tmp;
1091 }
1092 if (!buf) {
1093 buf = vmalloc(buf_len);
1094 if (!buf) {
1095 ret = -ENOMEM;
1096 goto out;
1097 }
1098 }
1099 }
1100
Alexander Block31db9f72012-07-25 23:19:24 +02001101 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1102 name_len + data_len);
1103
1104 len = sizeof(*di) + name_len + data_len;
1105 di = (struct btrfs_dir_item *)((char *)di + len);
1106 cur += len;
1107
1108 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1109 data_len, type, ctx);
1110 if (ret < 0)
1111 goto out;
1112 if (ret) {
1113 ret = 0;
1114 goto out;
1115 }
1116
1117 num++;
1118 }
1119
1120out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001121 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001122 return ret;
1123}
1124
1125static int __copy_first_ref(int num, u64 dir, int index,
1126 struct fs_path *p, void *ctx)
1127{
1128 int ret;
1129 struct fs_path *pt = ctx;
1130
1131 ret = fs_path_copy(pt, p);
1132 if (ret < 0)
1133 return ret;
1134
1135 /* we want the first only */
1136 return 1;
1137}
1138
1139/*
1140 * Retrieve the first path of an inode. If an inode has more then one
1141 * ref/hardlink, this is ignored.
1142 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001143static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001144 u64 ino, struct fs_path *path)
1145{
1146 int ret;
1147 struct btrfs_key key, found_key;
1148 struct btrfs_path *p;
1149
1150 p = alloc_path_for_send();
1151 if (!p)
1152 return -ENOMEM;
1153
1154 fs_path_reset(path);
1155
1156 key.objectid = ino;
1157 key.type = BTRFS_INODE_REF_KEY;
1158 key.offset = 0;
1159
1160 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1161 if (ret < 0)
1162 goto out;
1163 if (ret) {
1164 ret = 1;
1165 goto out;
1166 }
1167 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1168 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001169 (found_key.type != BTRFS_INODE_REF_KEY &&
1170 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001171 ret = -ENOENT;
1172 goto out;
1173 }
1174
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001175 ret = iterate_inode_ref(root, p, &found_key, 1,
1176 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001177 if (ret < 0)
1178 goto out;
1179 ret = 0;
1180
1181out:
1182 btrfs_free_path(p);
1183 return ret;
1184}
1185
1186struct backref_ctx {
1187 struct send_ctx *sctx;
1188
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001189 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001190 /* number of total found references */
1191 u64 found;
1192
1193 /*
1194 * used for clones found in send_root. clones found behind cur_objectid
1195 * and cur_offset are not considered as allowed clones.
1196 */
1197 u64 cur_objectid;
1198 u64 cur_offset;
1199
1200 /* may be truncated in case it's the last extent in a file */
1201 u64 extent_len;
1202
Filipe Manana619d8c42015-05-03 01:56:00 +01001203 /* data offset in the file extent item */
1204 u64 data_offset;
1205
Alexander Block31db9f72012-07-25 23:19:24 +02001206 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001207 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001208};
1209
1210static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1211{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001212 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001213 struct clone_root *cr = (struct clone_root *)elt;
1214
1215 if (root < cr->root->objectid)
1216 return -1;
1217 if (root > cr->root->objectid)
1218 return 1;
1219 return 0;
1220}
1221
1222static int __clone_root_cmp_sort(const void *e1, const void *e2)
1223{
1224 struct clone_root *cr1 = (struct clone_root *)e1;
1225 struct clone_root *cr2 = (struct clone_root *)e2;
1226
1227 if (cr1->root->objectid < cr2->root->objectid)
1228 return -1;
1229 if (cr1->root->objectid > cr2->root->objectid)
1230 return 1;
1231 return 0;
1232}
1233
1234/*
1235 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001236 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001237 */
1238static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1239{
1240 struct backref_ctx *bctx = ctx_;
1241 struct clone_root *found;
1242 int ret;
1243 u64 i_size;
1244
1245 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001246 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001247 bctx->sctx->clone_roots_cnt,
1248 sizeof(struct clone_root),
1249 __clone_root_cmp_bsearch);
1250 if (!found)
1251 return 0;
1252
1253 if (found->root == bctx->sctx->send_root &&
1254 ino == bctx->cur_objectid &&
1255 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001256 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001257 }
1258
1259 /*
Alexander Block766702e2012-07-28 14:11:31 +02001260 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001261 * accept clones from these extents.
1262 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001263 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1264 NULL, NULL, NULL);
1265 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001266 if (ret < 0)
1267 return ret;
1268
Filipe Manana619d8c42015-05-03 01:56:00 +01001269 if (offset + bctx->data_offset + bctx->extent_len > i_size)
Alexander Block31db9f72012-07-25 23:19:24 +02001270 return 0;
1271
1272 /*
1273 * Make sure we don't consider clones from send_root that are
1274 * behind the current inode/offset.
1275 */
1276 if (found->root == bctx->sctx->send_root) {
1277 /*
1278 * TODO for the moment we don't accept clones from the inode
1279 * that is currently send. We may change this when
1280 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1281 * file.
1282 */
1283 if (ino >= bctx->cur_objectid)
1284 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001285#if 0
1286 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001287 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001288 if (offset + bctx->extent_len > bctx->cur_offset)
1289 return 0;
1290#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001291 }
1292
1293 bctx->found++;
1294 found->found_refs++;
1295 if (ino < found->ino) {
1296 found->ino = ino;
1297 found->offset = offset;
1298 } else if (found->ino == ino) {
1299 /*
1300 * same extent found more then once in the same file.
1301 */
1302 if (found->offset > offset + bctx->extent_len)
1303 found->offset = offset;
1304 }
1305
1306 return 0;
1307}
1308
1309/*
Alexander Block766702e2012-07-28 14:11:31 +02001310 * Given an inode, offset and extent item, it finds a good clone for a clone
1311 * instruction. Returns -ENOENT when none could be found. The function makes
1312 * sure that the returned clone is usable at the point where sending is at the
1313 * moment. This means, that no clones are accepted which lie behind the current
1314 * inode+offset.
1315 *
Alexander Block31db9f72012-07-25 23:19:24 +02001316 * path must point to the extent item when called.
1317 */
1318static int find_extent_clone(struct send_ctx *sctx,
1319 struct btrfs_path *path,
1320 u64 ino, u64 data_offset,
1321 u64 ino_size,
1322 struct clone_root **found)
1323{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001324 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001325 int ret;
1326 int extent_type;
1327 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001328 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001329 u64 num_bytes;
1330 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001331 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001332 struct btrfs_file_extent_item *fi;
1333 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001334 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001335 struct clone_root *cur_clone_root;
1336 struct btrfs_key found_key;
1337 struct btrfs_path *tmp_path;
Filipe Mananab6af12a2019-10-30 12:23:01 +00001338 struct btrfs_extent_item *ei;
Chris Mason74dd17f2012-08-07 16:25:13 -04001339 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001340 u32 i;
1341
1342 tmp_path = alloc_path_for_send();
1343 if (!tmp_path)
1344 return -ENOMEM;
1345
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001346 /* We only use this path under the commit sem */
1347 tmp_path->need_commit_sem = 0;
1348
David Sterbae780b0d2016-01-18 18:42:13 +01001349 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001350 if (!backref_ctx) {
1351 ret = -ENOMEM;
1352 goto out;
1353 }
1354
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001355 backref_ctx->path = tmp_path;
1356
Alexander Block31db9f72012-07-25 23:19:24 +02001357 if (data_offset >= ino_size) {
1358 /*
1359 * There may be extents that lie behind the file's size.
1360 * I at least had this in combination with snapshotting while
1361 * writing large files.
1362 */
1363 ret = 0;
1364 goto out;
1365 }
1366
1367 fi = btrfs_item_ptr(eb, path->slots[0],
1368 struct btrfs_file_extent_item);
1369 extent_type = btrfs_file_extent_type(eb, fi);
1370 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1371 ret = -ENOENT;
1372 goto out;
1373 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001374 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001375
1376 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001377 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1378 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001379 ret = -ENOENT;
1380 goto out;
1381 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001382 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001383
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001384 down_read(&fs_info->commit_root_sem);
1385 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001386 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001387 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001388
1389 if (ret < 0)
1390 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001391 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001392 ret = -EIO;
1393 goto out;
1394 }
1395
Filipe Mananab6af12a2019-10-30 12:23:01 +00001396 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1397 struct btrfs_extent_item);
1398 /*
1399 * Backreference walking (iterate_extent_inodes() below) is currently
1400 * too expensive when an extent has a large number of references, both
1401 * in time spent and used memory. So for now just fallback to write
1402 * operations instead of clone operations when an extent has more than
1403 * a certain amount of references.
1404 */
1405 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1406 ret = -ENOENT;
1407 goto out;
1408 }
1409 btrfs_release_path(tmp_path);
1410
Alexander Block31db9f72012-07-25 23:19:24 +02001411 /*
1412 * Setup the clone roots.
1413 */
1414 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1415 cur_clone_root = sctx->clone_roots + i;
1416 cur_clone_root->ino = (u64)-1;
1417 cur_clone_root->offset = 0;
1418 cur_clone_root->found_refs = 0;
1419 }
1420
Alexander Block35075bb2012-07-28 12:44:34 +02001421 backref_ctx->sctx = sctx;
1422 backref_ctx->found = 0;
1423 backref_ctx->cur_objectid = ino;
1424 backref_ctx->cur_offset = data_offset;
1425 backref_ctx->found_itself = 0;
1426 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001427 /*
1428 * For non-compressed extents iterate_extent_inodes() gives us extent
1429 * offsets that already take into account the data offset, but not for
1430 * compressed extents, since the offset is logical and not relative to
1431 * the physical extent locations. We must take this into account to
1432 * avoid sending clone offsets that go beyond the source file's size,
1433 * which would result in the clone ioctl failing with -EINVAL on the
1434 * receiving end.
1435 */
1436 if (compressed == BTRFS_COMPRESS_NONE)
1437 backref_ctx->data_offset = 0;
1438 else
1439 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001440
1441 /*
1442 * The last extent of a file may be too large due to page alignment.
1443 * We need to adjust extent_len in this case so that the checks in
1444 * __iterate_backrefs work.
1445 */
1446 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001447 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001448
1449 /*
1450 * Now collect all backrefs.
1451 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001452 if (compressed == BTRFS_COMPRESS_NONE)
1453 extent_item_pos = logical - found_key.objectid;
1454 else
1455 extent_item_pos = 0;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001456 ret = iterate_extent_inodes(fs_info,
Alexander Block31db9f72012-07-25 23:19:24 +02001457 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001458 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001459
Alexander Block31db9f72012-07-25 23:19:24 +02001460 if (ret < 0)
1461 goto out;
1462
Alexander Block35075bb2012-07-28 12:44:34 +02001463 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001464 /* found a bug in backref code? */
1465 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001466 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001467 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001468 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001469 goto out;
1470 }
1471
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001472 btrfs_debug(fs_info,
1473 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1474 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001475
Alexander Block35075bb2012-07-28 12:44:34 +02001476 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001477 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001478
1479 cur_clone_root = NULL;
1480 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1481 if (sctx->clone_roots[i].found_refs) {
1482 if (!cur_clone_root)
1483 cur_clone_root = sctx->clone_roots + i;
1484 else if (sctx->clone_roots[i].root == sctx->send_root)
1485 /* prefer clones from send_root over others */
1486 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001487 }
1488
1489 }
1490
1491 if (cur_clone_root) {
1492 *found = cur_clone_root;
1493 ret = 0;
1494 } else {
1495 ret = -ENOENT;
1496 }
1497
1498out:
1499 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001500 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001501 return ret;
1502}
1503
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001504static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001505 u64 ino,
1506 struct fs_path *dest)
1507{
1508 int ret;
1509 struct btrfs_path *path;
1510 struct btrfs_key key;
1511 struct btrfs_file_extent_item *ei;
1512 u8 type;
1513 u8 compression;
1514 unsigned long off;
1515 int len;
1516
1517 path = alloc_path_for_send();
1518 if (!path)
1519 return -ENOMEM;
1520
1521 key.objectid = ino;
1522 key.type = BTRFS_EXTENT_DATA_KEY;
1523 key.offset = 0;
1524 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1525 if (ret < 0)
1526 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001527 if (ret) {
1528 /*
1529 * An empty symlink inode. Can happen in rare error paths when
1530 * creating a symlink (transaction committed before the inode
1531 * eviction handler removed the symlink inode items and a crash
1532 * happened in between or the subvol was snapshoted in between).
1533 * Print an informative message to dmesg/syslog so that the user
1534 * can delete the symlink.
1535 */
1536 btrfs_err(root->fs_info,
1537 "Found empty symlink inode %llu at root %llu",
1538 ino, root->root_key.objectid);
1539 ret = -EIO;
1540 goto out;
1541 }
Alexander Block31db9f72012-07-25 23:19:24 +02001542
1543 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1544 struct btrfs_file_extent_item);
1545 type = btrfs_file_extent_type(path->nodes[0], ei);
1546 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1547 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1548 BUG_ON(compression);
1549
1550 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001551 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001552
1553 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001554
1555out:
1556 btrfs_free_path(path);
1557 return ret;
1558}
1559
1560/*
1561 * Helper function to generate a file name that is unique in the root of
1562 * send_root and parent_root. This is used to generate names for orphan inodes.
1563 */
1564static int gen_unique_name(struct send_ctx *sctx,
1565 u64 ino, u64 gen,
1566 struct fs_path *dest)
1567{
1568 int ret = 0;
1569 struct btrfs_path *path;
1570 struct btrfs_dir_item *di;
1571 char tmp[64];
1572 int len;
1573 u64 idx = 0;
1574
1575 path = alloc_path_for_send();
1576 if (!path)
1577 return -ENOMEM;
1578
1579 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001580 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001581 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001582 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001583
1584 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1585 path, BTRFS_FIRST_FREE_OBJECTID,
1586 tmp, strlen(tmp), 0);
1587 btrfs_release_path(path);
1588 if (IS_ERR(di)) {
1589 ret = PTR_ERR(di);
1590 goto out;
1591 }
1592 if (di) {
1593 /* not unique, try again */
1594 idx++;
1595 continue;
1596 }
1597
1598 if (!sctx->parent_root) {
1599 /* unique */
1600 ret = 0;
1601 break;
1602 }
1603
1604 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1605 path, BTRFS_FIRST_FREE_OBJECTID,
1606 tmp, strlen(tmp), 0);
1607 btrfs_release_path(path);
1608 if (IS_ERR(di)) {
1609 ret = PTR_ERR(di);
1610 goto out;
1611 }
1612 if (di) {
1613 /* not unique, try again */
1614 idx++;
1615 continue;
1616 }
1617 /* unique */
1618 break;
1619 }
1620
1621 ret = fs_path_add(dest, tmp, strlen(tmp));
1622
1623out:
1624 btrfs_free_path(path);
1625 return ret;
1626}
1627
1628enum inode_state {
1629 inode_state_no_change,
1630 inode_state_will_create,
1631 inode_state_did_create,
1632 inode_state_will_delete,
1633 inode_state_did_delete,
1634};
1635
1636static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1637{
1638 int ret;
1639 int left_ret;
1640 int right_ret;
1641 u64 left_gen;
1642 u64 right_gen;
1643
1644 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001645 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001646 if (ret < 0 && ret != -ENOENT)
1647 goto out;
1648 left_ret = ret;
1649
1650 if (!sctx->parent_root) {
1651 right_ret = -ENOENT;
1652 } else {
1653 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001654 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001655 if (ret < 0 && ret != -ENOENT)
1656 goto out;
1657 right_ret = ret;
1658 }
1659
1660 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001661 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001662 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001663 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001664 if (ino < sctx->send_progress)
1665 ret = inode_state_did_create;
1666 else
1667 ret = inode_state_will_create;
1668 } else if (right_gen == gen) {
1669 if (ino < sctx->send_progress)
1670 ret = inode_state_did_delete;
1671 else
1672 ret = inode_state_will_delete;
1673 } else {
1674 ret = -ENOENT;
1675 }
1676 } else if (!left_ret) {
1677 if (left_gen == gen) {
1678 if (ino < sctx->send_progress)
1679 ret = inode_state_did_create;
1680 else
1681 ret = inode_state_will_create;
1682 } else {
1683 ret = -ENOENT;
1684 }
1685 } else if (!right_ret) {
1686 if (right_gen == gen) {
1687 if (ino < sctx->send_progress)
1688 ret = inode_state_did_delete;
1689 else
1690 ret = inode_state_will_delete;
1691 } else {
1692 ret = -ENOENT;
1693 }
1694 } else {
1695 ret = -ENOENT;
1696 }
1697
1698out:
1699 return ret;
1700}
1701
1702static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1703{
1704 int ret;
1705
Robbie Koe215b6b2017-01-05 16:24:55 +08001706 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1707 return 1;
1708
Alexander Block31db9f72012-07-25 23:19:24 +02001709 ret = get_cur_inode_state(sctx, ino, gen);
1710 if (ret < 0)
1711 goto out;
1712
1713 if (ret == inode_state_no_change ||
1714 ret == inode_state_did_create ||
1715 ret == inode_state_will_delete)
1716 ret = 1;
1717 else
1718 ret = 0;
1719
1720out:
1721 return ret;
1722}
1723
1724/*
1725 * Helper function to lookup a dir item in a dir.
1726 */
1727static int lookup_dir_item_inode(struct btrfs_root *root,
1728 u64 dir, const char *name, int name_len,
1729 u64 *found_inode,
1730 u8 *found_type)
1731{
1732 int ret = 0;
1733 struct btrfs_dir_item *di;
1734 struct btrfs_key key;
1735 struct btrfs_path *path;
1736
1737 path = alloc_path_for_send();
1738 if (!path)
1739 return -ENOMEM;
1740
1741 di = btrfs_lookup_dir_item(NULL, root, path,
1742 dir, name, name_len, 0);
1743 if (!di) {
1744 ret = -ENOENT;
1745 goto out;
1746 }
1747 if (IS_ERR(di)) {
1748 ret = PTR_ERR(di);
1749 goto out;
1750 }
1751 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001752 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1753 ret = -ENOENT;
1754 goto out;
1755 }
Alexander Block31db9f72012-07-25 23:19:24 +02001756 *found_inode = key.objectid;
1757 *found_type = btrfs_dir_type(path->nodes[0], di);
1758
1759out:
1760 btrfs_free_path(path);
1761 return ret;
1762}
1763
Alexander Block766702e2012-07-28 14:11:31 +02001764/*
1765 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1766 * generation of the parent dir and the name of the dir entry.
1767 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001768static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001769 u64 *dir, u64 *dir_gen, struct fs_path *name)
1770{
1771 int ret;
1772 struct btrfs_key key;
1773 struct btrfs_key found_key;
1774 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001775 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001776 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001777
1778 path = alloc_path_for_send();
1779 if (!path)
1780 return -ENOMEM;
1781
1782 key.objectid = ino;
1783 key.type = BTRFS_INODE_REF_KEY;
1784 key.offset = 0;
1785
1786 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1787 if (ret < 0)
1788 goto out;
1789 if (!ret)
1790 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1791 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001792 if (ret || found_key.objectid != ino ||
1793 (found_key.type != BTRFS_INODE_REF_KEY &&
1794 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001795 ret = -ENOENT;
1796 goto out;
1797 }
1798
Filipe Manana51a60252014-05-13 22:01:02 +01001799 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001800 struct btrfs_inode_ref *iref;
1801 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1802 struct btrfs_inode_ref);
1803 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1804 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1805 (unsigned long)(iref + 1),
1806 len);
1807 parent_dir = found_key.offset;
1808 } else {
1809 struct btrfs_inode_extref *extref;
1810 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1811 struct btrfs_inode_extref);
1812 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1813 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1814 (unsigned long)&extref->name, len);
1815 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1816 }
Alexander Block31db9f72012-07-25 23:19:24 +02001817 if (ret < 0)
1818 goto out;
1819 btrfs_release_path(path);
1820
Filipe Mananab46ab972014-03-21 12:46:54 +00001821 if (dir_gen) {
1822 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1823 NULL, NULL, NULL);
1824 if (ret < 0)
1825 goto out;
1826 }
Alexander Block31db9f72012-07-25 23:19:24 +02001827
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001828 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001829
1830out:
1831 btrfs_free_path(path);
1832 return ret;
1833}
1834
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001835static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001836 u64 ino, u64 dir,
1837 const char *name, int name_len)
1838{
1839 int ret;
1840 struct fs_path *tmp_name;
1841 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001842
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001843 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001844 if (!tmp_name)
1845 return -ENOMEM;
1846
Filipe Mananab46ab972014-03-21 12:46:54 +00001847 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001848 if (ret < 0)
1849 goto out;
1850
Alexander Blockb9291af2012-07-28 11:07:18 +02001851 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001852 ret = 0;
1853 goto out;
1854 }
1855
Alexander Blocke938c8a2012-07-28 16:33:49 +02001856 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001857
1858out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001859 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001860 return ret;
1861}
1862
Alexander Block766702e2012-07-28 14:11:31 +02001863/*
1864 * Used by process_recorded_refs to determine if a new ref would overwrite an
1865 * already existing ref. In case it detects an overwrite, it returns the
1866 * inode/gen in who_ino/who_gen.
1867 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1868 * to make sure later references to the overwritten inode are possible.
1869 * Orphanizing is however only required for the first ref of an inode.
1870 * process_recorded_refs does an additional is_first_ref check to see if
1871 * orphanizing is really required.
1872 */
Alexander Block31db9f72012-07-25 23:19:24 +02001873static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1874 const char *name, int name_len,
1875 u64 *who_ino, u64 *who_gen)
1876{
1877 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001878 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001879 u64 other_inode = 0;
1880 u8 other_type = 0;
1881
1882 if (!sctx->parent_root)
1883 goto out;
1884
1885 ret = is_inode_existent(sctx, dir, dir_gen);
1886 if (ret <= 0)
1887 goto out;
1888
Josef Bacikebdad912013-08-06 16:47:48 -04001889 /*
1890 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001891 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001892 * and we can just unlink this entry.
1893 */
Robbie Koe215b6b2017-01-05 16:24:55 +08001894 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001895 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1896 NULL, NULL, NULL);
1897 if (ret < 0 && ret != -ENOENT)
1898 goto out;
1899 if (ret) {
1900 ret = 0;
1901 goto out;
1902 }
1903 if (gen != dir_gen)
1904 goto out;
1905 }
1906
Alexander Block31db9f72012-07-25 23:19:24 +02001907 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1908 &other_inode, &other_type);
1909 if (ret < 0 && ret != -ENOENT)
1910 goto out;
1911 if (ret) {
1912 ret = 0;
1913 goto out;
1914 }
1915
Alexander Block766702e2012-07-28 14:11:31 +02001916 /*
1917 * Check if the overwritten ref was already processed. If yes, the ref
1918 * was already unlinked/moved, so we can safely assume that we will not
1919 * overwrite anything at this point in time.
1920 */
Robbie Ko801bec32015-06-23 18:39:46 +08001921 if (other_inode > sctx->send_progress ||
1922 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001923 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001924 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001925 if (ret < 0)
1926 goto out;
1927
1928 ret = 1;
1929 *who_ino = other_inode;
1930 } else {
1931 ret = 0;
1932 }
1933
1934out:
1935 return ret;
1936}
1937
Alexander Block766702e2012-07-28 14:11:31 +02001938/*
1939 * Checks if the ref was overwritten by an already processed inode. This is
1940 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1941 * thus the orphan name needs be used.
1942 * process_recorded_refs also uses it to avoid unlinking of refs that were
1943 * overwritten.
1944 */
Alexander Block31db9f72012-07-25 23:19:24 +02001945static int did_overwrite_ref(struct send_ctx *sctx,
1946 u64 dir, u64 dir_gen,
1947 u64 ino, u64 ino_gen,
1948 const char *name, int name_len)
1949{
1950 int ret = 0;
1951 u64 gen;
1952 u64 ow_inode;
1953 u8 other_type;
1954
1955 if (!sctx->parent_root)
1956 goto out;
1957
1958 ret = is_inode_existent(sctx, dir, dir_gen);
1959 if (ret <= 0)
1960 goto out;
1961
1962 /* check if the ref was overwritten by another ref */
1963 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1964 &ow_inode, &other_type);
1965 if (ret < 0 && ret != -ENOENT)
1966 goto out;
1967 if (ret) {
1968 /* was never and will never be overwritten */
1969 ret = 0;
1970 goto out;
1971 }
1972
1973 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001974 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001975 if (ret < 0)
1976 goto out;
1977
1978 if (ow_inode == ino && gen == ino_gen) {
1979 ret = 0;
1980 goto out;
1981 }
1982
Filipe Manana8b191a62015-04-09 14:09:14 +01001983 /*
1984 * We know that it is or will be overwritten. Check this now.
1985 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001986 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1987 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001988 */
Filipe Mananab786f162015-09-26 15:30:23 +01001989 if ((ow_inode < sctx->send_progress) ||
1990 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1991 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001992 ret = 1;
1993 else
1994 ret = 0;
1995
1996out:
1997 return ret;
1998}
1999
Alexander Block766702e2012-07-28 14:11:31 +02002000/*
2001 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
2002 * that got overwritten. This is used by process_recorded_refs to determine
2003 * if it has to use the path as returned by get_cur_path or the orphan name.
2004 */
Alexander Block31db9f72012-07-25 23:19:24 +02002005static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
2006{
2007 int ret = 0;
2008 struct fs_path *name = NULL;
2009 u64 dir;
2010 u64 dir_gen;
2011
2012 if (!sctx->parent_root)
2013 goto out;
2014
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002015 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002016 if (!name)
2017 return -ENOMEM;
2018
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002019 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02002020 if (ret < 0)
2021 goto out;
2022
2023 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2024 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02002025
2026out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002027 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002028 return ret;
2029}
2030
Alexander Block766702e2012-07-28 14:11:31 +02002031/*
2032 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2033 * so we need to do some special handling in case we have clashes. This function
2034 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002035 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002036 */
Alexander Block31db9f72012-07-25 23:19:24 +02002037static int name_cache_insert(struct send_ctx *sctx,
2038 struct name_cache_entry *nce)
2039{
2040 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002041 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002042
Alexander Block7e0926f2012-07-28 14:20:58 +02002043 nce_head = radix_tree_lookup(&sctx->name_cache,
2044 (unsigned long)nce->ino);
2045 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002046 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002047 if (!nce_head) {
2048 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002049 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002050 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002051 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002052
Alexander Block7e0926f2012-07-28 14:20:58 +02002053 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002054 if (ret < 0) {
2055 kfree(nce_head);
2056 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002057 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002058 }
Alexander Block31db9f72012-07-25 23:19:24 +02002059 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002060 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002061 list_add_tail(&nce->list, &sctx->name_cache_list);
2062 sctx->name_cache_size++;
2063
2064 return ret;
2065}
2066
2067static void name_cache_delete(struct send_ctx *sctx,
2068 struct name_cache_entry *nce)
2069{
Alexander Block7e0926f2012-07-28 14:20:58 +02002070 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002071
Alexander Block7e0926f2012-07-28 14:20:58 +02002072 nce_head = radix_tree_lookup(&sctx->name_cache,
2073 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002074 if (!nce_head) {
2075 btrfs_err(sctx->send_root->fs_info,
2076 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2077 nce->ino, sctx->name_cache_size);
2078 }
Alexander Block31db9f72012-07-25 23:19:24 +02002079
Alexander Block7e0926f2012-07-28 14:20:58 +02002080 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002081 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002082 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002083
David Sterba57fb8912014-02-03 19:24:40 +01002084 /*
2085 * We may not get to the final release of nce_head if the lookup fails
2086 */
2087 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002088 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2089 kfree(nce_head);
2090 }
Alexander Block31db9f72012-07-25 23:19:24 +02002091}
2092
2093static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2094 u64 ino, u64 gen)
2095{
Alexander Block7e0926f2012-07-28 14:20:58 +02002096 struct list_head *nce_head;
2097 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002098
Alexander Block7e0926f2012-07-28 14:20:58 +02002099 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2100 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002101 return NULL;
2102
Alexander Block7e0926f2012-07-28 14:20:58 +02002103 list_for_each_entry(cur, nce_head, radix_list) {
2104 if (cur->ino == ino && cur->gen == gen)
2105 return cur;
2106 }
Alexander Block31db9f72012-07-25 23:19:24 +02002107 return NULL;
2108}
2109
Alexander Block766702e2012-07-28 14:11:31 +02002110/*
2111 * Removes the entry from the list and adds it back to the end. This marks the
2112 * entry as recently used so that name_cache_clean_unused does not remove it.
2113 */
Alexander Block31db9f72012-07-25 23:19:24 +02002114static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2115{
2116 list_del(&nce->list);
2117 list_add_tail(&nce->list, &sctx->name_cache_list);
2118}
2119
Alexander Block766702e2012-07-28 14:11:31 +02002120/*
2121 * Remove some entries from the beginning of name_cache_list.
2122 */
Alexander Block31db9f72012-07-25 23:19:24 +02002123static void name_cache_clean_unused(struct send_ctx *sctx)
2124{
2125 struct name_cache_entry *nce;
2126
2127 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2128 return;
2129
2130 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2131 nce = list_entry(sctx->name_cache_list.next,
2132 struct name_cache_entry, list);
2133 name_cache_delete(sctx, nce);
2134 kfree(nce);
2135 }
2136}
2137
2138static void name_cache_free(struct send_ctx *sctx)
2139{
2140 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002141
Alexander Blocke938c8a2012-07-28 16:33:49 +02002142 while (!list_empty(&sctx->name_cache_list)) {
2143 nce = list_entry(sctx->name_cache_list.next,
2144 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002145 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002146 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002147 }
2148}
2149
Alexander Block766702e2012-07-28 14:11:31 +02002150/*
2151 * Used by get_cur_path for each ref up to the root.
2152 * Returns 0 if it succeeded.
2153 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2154 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2155 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2156 * Returns <0 in case of error.
2157 */
Alexander Block31db9f72012-07-25 23:19:24 +02002158static int __get_cur_name_and_parent(struct send_ctx *sctx,
2159 u64 ino, u64 gen,
2160 u64 *parent_ino,
2161 u64 *parent_gen,
2162 struct fs_path *dest)
2163{
2164 int ret;
2165 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002166 struct name_cache_entry *nce = NULL;
2167
Alexander Block766702e2012-07-28 14:11:31 +02002168 /*
2169 * First check if we already did a call to this function with the same
2170 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2171 * return the cached result.
2172 */
Alexander Block31db9f72012-07-25 23:19:24 +02002173 nce = name_cache_search(sctx, ino, gen);
2174 if (nce) {
2175 if (ino < sctx->send_progress && nce->need_later_update) {
2176 name_cache_delete(sctx, nce);
2177 kfree(nce);
2178 nce = NULL;
2179 } else {
2180 name_cache_used(sctx, nce);
2181 *parent_ino = nce->parent_ino;
2182 *parent_gen = nce->parent_gen;
2183 ret = fs_path_add(dest, nce->name, nce->name_len);
2184 if (ret < 0)
2185 goto out;
2186 ret = nce->ret;
2187 goto out;
2188 }
2189 }
2190
Alexander Block766702e2012-07-28 14:11:31 +02002191 /*
2192 * If the inode is not existent yet, add the orphan name and return 1.
2193 * This should only happen for the parent dir that we determine in
2194 * __record_new_ref
2195 */
Alexander Block31db9f72012-07-25 23:19:24 +02002196 ret = is_inode_existent(sctx, ino, gen);
2197 if (ret < 0)
2198 goto out;
2199
2200 if (!ret) {
2201 ret = gen_unique_name(sctx, ino, gen, dest);
2202 if (ret < 0)
2203 goto out;
2204 ret = 1;
2205 goto out_cache;
2206 }
2207
Alexander Block766702e2012-07-28 14:11:31 +02002208 /*
2209 * Depending on whether the inode was already processed or not, use
2210 * send_root or parent_root for ref lookup.
2211 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002212 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002213 ret = get_first_ref(sctx->send_root, ino,
2214 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002215 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002216 ret = get_first_ref(sctx->parent_root, ino,
2217 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002218 if (ret < 0)
2219 goto out;
2220
Alexander Block766702e2012-07-28 14:11:31 +02002221 /*
2222 * Check if the ref was overwritten by an inode's ref that was processed
2223 * earlier. If yes, treat as orphan and return 1.
2224 */
Alexander Block31db9f72012-07-25 23:19:24 +02002225 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2226 dest->start, dest->end - dest->start);
2227 if (ret < 0)
2228 goto out;
2229 if (ret) {
2230 fs_path_reset(dest);
2231 ret = gen_unique_name(sctx, ino, gen, dest);
2232 if (ret < 0)
2233 goto out;
2234 ret = 1;
2235 }
2236
2237out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002238 /*
2239 * Store the result of the lookup in the name cache.
2240 */
David Sterbae780b0d2016-01-18 18:42:13 +01002241 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002242 if (!nce) {
2243 ret = -ENOMEM;
2244 goto out;
2245 }
2246
2247 nce->ino = ino;
2248 nce->gen = gen;
2249 nce->parent_ino = *parent_ino;
2250 nce->parent_gen = *parent_gen;
2251 nce->name_len = fs_path_len(dest);
2252 nce->ret = ret;
2253 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002254
2255 if (ino < sctx->send_progress)
2256 nce->need_later_update = 0;
2257 else
2258 nce->need_later_update = 1;
2259
2260 nce_ret = name_cache_insert(sctx, nce);
2261 if (nce_ret < 0)
2262 ret = nce_ret;
2263 name_cache_clean_unused(sctx);
2264
2265out:
Alexander Block31db9f72012-07-25 23:19:24 +02002266 return ret;
2267}
2268
2269/*
2270 * Magic happens here. This function returns the first ref to an inode as it
2271 * would look like while receiving the stream at this point in time.
2272 * We walk the path up to the root. For every inode in between, we check if it
2273 * was already processed/sent. If yes, we continue with the parent as found
2274 * in send_root. If not, we continue with the parent as found in parent_root.
2275 * If we encounter an inode that was deleted at this point in time, we use the
2276 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2277 * that were not created yet and overwritten inodes/refs.
2278 *
2279 * When do we have have orphan inodes:
2280 * 1. When an inode is freshly created and thus no valid refs are available yet
2281 * 2. When a directory lost all it's refs (deleted) but still has dir items
2282 * inside which were not processed yet (pending for move/delete). If anyone
2283 * tried to get the path to the dir items, it would get a path inside that
2284 * orphan directory.
2285 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2286 * of an unprocessed inode. If in that case the first ref would be
2287 * overwritten, the overwritten inode gets "orphanized". Later when we
2288 * process this overwritten inode, it is restored at a new place by moving
2289 * the orphan inode.
2290 *
2291 * sctx->send_progress tells this function at which point in time receiving
2292 * would be.
2293 */
2294static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2295 struct fs_path *dest)
2296{
2297 int ret = 0;
2298 struct fs_path *name = NULL;
2299 u64 parent_inode = 0;
2300 u64 parent_gen = 0;
2301 int stop = 0;
2302
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002303 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002304 if (!name) {
2305 ret = -ENOMEM;
2306 goto out;
2307 }
2308
2309 dest->reversed = 1;
2310 fs_path_reset(dest);
2311
2312 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002313 struct waiting_dir_move *wdm;
2314
Alexander Block31db9f72012-07-25 23:19:24 +02002315 fs_path_reset(name);
2316
Filipe Manana9dc44212014-02-19 14:31:44 +00002317 if (is_waiting_for_rm(sctx, ino)) {
2318 ret = gen_unique_name(sctx, ino, gen, name);
2319 if (ret < 0)
2320 goto out;
2321 ret = fs_path_add_path(dest, name);
2322 break;
2323 }
2324
Filipe Manana8b191a62015-04-09 14:09:14 +01002325 wdm = get_waiting_dir_move(sctx, ino);
2326 if (wdm && wdm->orphanized) {
2327 ret = gen_unique_name(sctx, ino, gen, name);
2328 stop = 1;
2329 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002330 ret = get_first_ref(sctx->parent_root, ino,
2331 &parent_inode, &parent_gen, name);
2332 } else {
2333 ret = __get_cur_name_and_parent(sctx, ino, gen,
2334 &parent_inode,
2335 &parent_gen, name);
2336 if (ret)
2337 stop = 1;
2338 }
2339
Alexander Block31db9f72012-07-25 23:19:24 +02002340 if (ret < 0)
2341 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002342
Alexander Block31db9f72012-07-25 23:19:24 +02002343 ret = fs_path_add_path(dest, name);
2344 if (ret < 0)
2345 goto out;
2346
2347 ino = parent_inode;
2348 gen = parent_gen;
2349 }
2350
2351out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002352 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002353 if (!ret)
2354 fs_path_unreverse(dest);
2355 return ret;
2356}
2357
2358/*
Alexander Block31db9f72012-07-25 23:19:24 +02002359 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2360 */
2361static int send_subvol_begin(struct send_ctx *sctx)
2362{
2363 int ret;
2364 struct btrfs_root *send_root = sctx->send_root;
2365 struct btrfs_root *parent_root = sctx->parent_root;
2366 struct btrfs_path *path;
2367 struct btrfs_key key;
2368 struct btrfs_root_ref *ref;
2369 struct extent_buffer *leaf;
2370 char *name = NULL;
2371 int namelen;
2372
Wang Shilongffcfaf82014-01-15 00:26:43 +08002373 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002374 if (!path)
2375 return -ENOMEM;
2376
David Sterbae780b0d2016-01-18 18:42:13 +01002377 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002378 if (!name) {
2379 btrfs_free_path(path);
2380 return -ENOMEM;
2381 }
2382
2383 key.objectid = send_root->objectid;
2384 key.type = BTRFS_ROOT_BACKREF_KEY;
2385 key.offset = 0;
2386
2387 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2388 &key, path, 1, 0);
2389 if (ret < 0)
2390 goto out;
2391 if (ret) {
2392 ret = -ENOENT;
2393 goto out;
2394 }
2395
2396 leaf = path->nodes[0];
2397 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2398 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2399 key.objectid != send_root->objectid) {
2400 ret = -ENOENT;
2401 goto out;
2402 }
2403 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2404 namelen = btrfs_root_ref_name_len(leaf, ref);
2405 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2406 btrfs_release_path(path);
2407
Alexander Block31db9f72012-07-25 23:19:24 +02002408 if (parent_root) {
2409 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2410 if (ret < 0)
2411 goto out;
2412 } else {
2413 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2414 if (ret < 0)
2415 goto out;
2416 }
2417
2418 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002419
2420 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2421 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2422 sctx->send_root->root_item.received_uuid);
2423 else
2424 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2425 sctx->send_root->root_item.uuid);
2426
Alexander Block31db9f72012-07-25 23:19:24 +02002427 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002428 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002429 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002430 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2431 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2432 parent_root->root_item.received_uuid);
2433 else
2434 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2435 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002436 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002437 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002438 }
2439
2440 ret = send_cmd(sctx);
2441
2442tlv_put_failure:
2443out:
2444 btrfs_free_path(path);
2445 kfree(name);
2446 return ret;
2447}
2448
2449static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2450{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002451 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002452 int ret = 0;
2453 struct fs_path *p;
2454
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002455 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002456
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002457 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002458 if (!p)
2459 return -ENOMEM;
2460
2461 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2462 if (ret < 0)
2463 goto out;
2464
2465 ret = get_cur_path(sctx, ino, gen, p);
2466 if (ret < 0)
2467 goto out;
2468 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2469 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2470
2471 ret = send_cmd(sctx);
2472
2473tlv_put_failure:
2474out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002475 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002476 return ret;
2477}
2478
2479static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2480{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002481 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002482 int ret = 0;
2483 struct fs_path *p;
2484
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002485 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002486
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002487 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002488 if (!p)
2489 return -ENOMEM;
2490
2491 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2492 if (ret < 0)
2493 goto out;
2494
2495 ret = get_cur_path(sctx, ino, gen, p);
2496 if (ret < 0)
2497 goto out;
2498 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2499 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2500
2501 ret = send_cmd(sctx);
2502
2503tlv_put_failure:
2504out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002505 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002506 return ret;
2507}
2508
2509static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2510{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002511 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002512 int ret = 0;
2513 struct fs_path *p;
2514
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002515 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2516 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002517
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002518 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002519 if (!p)
2520 return -ENOMEM;
2521
2522 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2523 if (ret < 0)
2524 goto out;
2525
2526 ret = get_cur_path(sctx, ino, gen, p);
2527 if (ret < 0)
2528 goto out;
2529 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2530 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2531 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2532
2533 ret = send_cmd(sctx);
2534
2535tlv_put_failure:
2536out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002537 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002538 return ret;
2539}
2540
2541static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2542{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002543 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002544 int ret = 0;
2545 struct fs_path *p = NULL;
2546 struct btrfs_inode_item *ii;
2547 struct btrfs_path *path = NULL;
2548 struct extent_buffer *eb;
2549 struct btrfs_key key;
2550 int slot;
2551
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002552 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002553
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002554 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002555 if (!p)
2556 return -ENOMEM;
2557
2558 path = alloc_path_for_send();
2559 if (!path) {
2560 ret = -ENOMEM;
2561 goto out;
2562 }
2563
2564 key.objectid = ino;
2565 key.type = BTRFS_INODE_ITEM_KEY;
2566 key.offset = 0;
2567 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002568 if (ret > 0)
2569 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002570 if (ret < 0)
2571 goto out;
2572
2573 eb = path->nodes[0];
2574 slot = path->slots[0];
2575 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2576
2577 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2578 if (ret < 0)
2579 goto out;
2580
2581 ret = get_cur_path(sctx, ino, gen, p);
2582 if (ret < 0)
2583 goto out;
2584 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002585 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2586 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2587 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002588 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002589
2590 ret = send_cmd(sctx);
2591
2592tlv_put_failure:
2593out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002594 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002595 btrfs_free_path(path);
2596 return ret;
2597}
2598
2599/*
2600 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2601 * a valid path yet because we did not process the refs yet. So, the inode
2602 * is created as orphan.
2603 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002604static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002605{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002606 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002607 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002608 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002609 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002610 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002611 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002612 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002613
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002614 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002615
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002616 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002617 if (!p)
2618 return -ENOMEM;
2619
Liu Bo644d1942014-02-27 17:29:01 +08002620 if (ino != sctx->cur_ino) {
2621 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2622 NULL, NULL, &rdev);
2623 if (ret < 0)
2624 goto out;
2625 } else {
2626 gen = sctx->cur_inode_gen;
2627 mode = sctx->cur_inode_mode;
2628 rdev = sctx->cur_inode_rdev;
2629 }
Alexander Block31db9f72012-07-25 23:19:24 +02002630
Alexander Blocke938c8a2012-07-28 16:33:49 +02002631 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002632 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002633 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002634 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002635 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002636 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002637 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002638 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002639 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002640 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002641 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002642 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002643 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002644 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002645 (int)(mode & S_IFMT));
2646 ret = -ENOTSUPP;
2647 goto out;
2648 }
2649
2650 ret = begin_cmd(sctx, cmd);
2651 if (ret < 0)
2652 goto out;
2653
Alexander Block1f4692d2012-07-28 10:42:24 +02002654 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002655 if (ret < 0)
2656 goto out;
2657
2658 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002659 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002660
2661 if (S_ISLNK(mode)) {
2662 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002663 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002664 if (ret < 0)
2665 goto out;
2666 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2667 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2668 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002669 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2670 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002671 }
2672
2673 ret = send_cmd(sctx);
2674 if (ret < 0)
2675 goto out;
2676
2677
2678tlv_put_failure:
2679out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002680 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002681 return ret;
2682}
2683
Alexander Block1f4692d2012-07-28 10:42:24 +02002684/*
2685 * We need some special handling for inodes that get processed before the parent
2686 * directory got created. See process_recorded_refs for details.
2687 * This function does the check if we already created the dir out of order.
2688 */
2689static int did_create_dir(struct send_ctx *sctx, u64 dir)
2690{
2691 int ret = 0;
2692 struct btrfs_path *path = NULL;
2693 struct btrfs_key key;
2694 struct btrfs_key found_key;
2695 struct btrfs_key di_key;
2696 struct extent_buffer *eb;
2697 struct btrfs_dir_item *di;
2698 int slot;
2699
2700 path = alloc_path_for_send();
2701 if (!path) {
2702 ret = -ENOMEM;
2703 goto out;
2704 }
2705
2706 key.objectid = dir;
2707 key.type = BTRFS_DIR_INDEX_KEY;
2708 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002709 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2710 if (ret < 0)
2711 goto out;
2712
Alexander Block1f4692d2012-07-28 10:42:24 +02002713 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002714 eb = path->nodes[0];
2715 slot = path->slots[0];
2716 if (slot >= btrfs_header_nritems(eb)) {
2717 ret = btrfs_next_leaf(sctx->send_root, path);
2718 if (ret < 0) {
2719 goto out;
2720 } else if (ret > 0) {
2721 ret = 0;
2722 break;
2723 }
2724 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002725 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002726
2727 btrfs_item_key_to_cpu(eb, &found_key, slot);
2728 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002729 found_key.type != key.type) {
2730 ret = 0;
2731 goto out;
2732 }
2733
2734 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2735 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2736
Josef Bacika0525412013-08-12 10:56:14 -04002737 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2738 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002739 ret = 1;
2740 goto out;
2741 }
2742
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002743 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002744 }
2745
2746out:
2747 btrfs_free_path(path);
2748 return ret;
2749}
2750
2751/*
2752 * Only creates the inode if it is:
2753 * 1. Not a directory
2754 * 2. Or a directory which was not created already due to out of order
2755 * directories. See did_create_dir and process_recorded_refs for details.
2756 */
2757static int send_create_inode_if_needed(struct send_ctx *sctx)
2758{
2759 int ret;
2760
2761 if (S_ISDIR(sctx->cur_inode_mode)) {
2762 ret = did_create_dir(sctx, sctx->cur_ino);
2763 if (ret < 0)
2764 goto out;
2765 if (ret) {
2766 ret = 0;
2767 goto out;
2768 }
2769 }
2770
2771 ret = send_create_inode(sctx, sctx->cur_ino);
2772 if (ret < 0)
2773 goto out;
2774
2775out:
2776 return ret;
2777}
2778
Alexander Block31db9f72012-07-25 23:19:24 +02002779struct recorded_ref {
2780 struct list_head list;
2781 char *dir_path;
2782 char *name;
2783 struct fs_path *full_path;
2784 u64 dir;
2785 u64 dir_gen;
2786 int dir_path_len;
2787 int name_len;
2788};
2789
2790/*
2791 * We need to process new refs before deleted refs, but compare_tree gives us
2792 * everything mixed. So we first record all refs and later process them.
2793 * This function is a helper to record one ref.
2794 */
Liu Boa4d96d62014-03-03 21:31:03 +08002795static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002796 u64 dir_gen, struct fs_path *path)
2797{
2798 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002799
David Sterbae780b0d2016-01-18 18:42:13 +01002800 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002801 if (!ref)
2802 return -ENOMEM;
2803
2804 ref->dir = dir;
2805 ref->dir_gen = dir_gen;
2806 ref->full_path = path;
2807
Andy Shevchenkoed848852013-08-21 10:32:13 +03002808 ref->name = (char *)kbasename(ref->full_path->start);
2809 ref->name_len = ref->full_path->end - ref->name;
2810 ref->dir_path = ref->full_path->start;
2811 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002812 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002813 else
Alexander Block31db9f72012-07-25 23:19:24 +02002814 ref->dir_path_len = ref->full_path->end -
2815 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002816
2817 list_add_tail(&ref->list, head);
2818 return 0;
2819}
2820
Josef Bacikba5e8f22013-08-16 16:52:55 -04002821static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2822{
2823 struct recorded_ref *new;
2824
David Sterbae780b0d2016-01-18 18:42:13 +01002825 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002826 if (!new)
2827 return -ENOMEM;
2828
2829 new->dir = ref->dir;
2830 new->dir_gen = ref->dir_gen;
2831 new->full_path = NULL;
2832 INIT_LIST_HEAD(&new->list);
2833 list_add_tail(&new->list, list);
2834 return 0;
2835}
2836
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002837static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002838{
2839 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002840
Alexander Blocke938c8a2012-07-28 16:33:49 +02002841 while (!list_empty(head)) {
2842 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002843 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002844 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002845 kfree(cur);
2846 }
Alexander Block31db9f72012-07-25 23:19:24 +02002847}
2848
2849static void free_recorded_refs(struct send_ctx *sctx)
2850{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002851 __free_recorded_refs(&sctx->new_refs);
2852 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002853}
2854
2855/*
Alexander Block766702e2012-07-28 14:11:31 +02002856 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002857 * ref of an unprocessed inode gets overwritten and for all non empty
2858 * directories.
2859 */
2860static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2861 struct fs_path *path)
2862{
2863 int ret;
2864 struct fs_path *orphan;
2865
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002866 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002867 if (!orphan)
2868 return -ENOMEM;
2869
2870 ret = gen_unique_name(sctx, ino, gen, orphan);
2871 if (ret < 0)
2872 goto out;
2873
2874 ret = send_rename(sctx, path, orphan);
2875
2876out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002877 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002878 return ret;
2879}
2880
Filipe Manana9dc44212014-02-19 14:31:44 +00002881static struct orphan_dir_info *
2882add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2883{
2884 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2885 struct rb_node *parent = NULL;
2886 struct orphan_dir_info *entry, *odi;
2887
David Sterbae780b0d2016-01-18 18:42:13 +01002888 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
Filipe Manana9dc44212014-02-19 14:31:44 +00002889 if (!odi)
2890 return ERR_PTR(-ENOMEM);
2891 odi->ino = dir_ino;
2892 odi->gen = 0;
2893
2894 while (*p) {
2895 parent = *p;
2896 entry = rb_entry(parent, struct orphan_dir_info, node);
2897 if (dir_ino < entry->ino) {
2898 p = &(*p)->rb_left;
2899 } else if (dir_ino > entry->ino) {
2900 p = &(*p)->rb_right;
2901 } else {
2902 kfree(odi);
2903 return entry;
2904 }
2905 }
2906
2907 rb_link_node(&odi->node, parent, p);
2908 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2909 return odi;
2910}
2911
2912static struct orphan_dir_info *
2913get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2914{
2915 struct rb_node *n = sctx->orphan_dirs.rb_node;
2916 struct orphan_dir_info *entry;
2917
2918 while (n) {
2919 entry = rb_entry(n, struct orphan_dir_info, node);
2920 if (dir_ino < entry->ino)
2921 n = n->rb_left;
2922 else if (dir_ino > entry->ino)
2923 n = n->rb_right;
2924 else
2925 return entry;
2926 }
2927 return NULL;
2928}
2929
2930static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2931{
2932 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2933
2934 return odi != NULL;
2935}
2936
2937static void free_orphan_dir_info(struct send_ctx *sctx,
2938 struct orphan_dir_info *odi)
2939{
2940 if (!odi)
2941 return;
2942 rb_erase(&odi->node, &sctx->orphan_dirs);
2943 kfree(odi);
2944}
2945
Alexander Block31db9f72012-07-25 23:19:24 +02002946/*
2947 * Returns 1 if a directory can be removed at this point in time.
2948 * We check this by iterating all dir items and checking if the inode behind
2949 * the dir item was already processed.
2950 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002951static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2952 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002953{
2954 int ret = 0;
2955 struct btrfs_root *root = sctx->parent_root;
2956 struct btrfs_path *path;
2957 struct btrfs_key key;
2958 struct btrfs_key found_key;
2959 struct btrfs_key loc;
2960 struct btrfs_dir_item *di;
2961
Alexander Block6d85ed02012-08-01 14:48:59 +02002962 /*
2963 * Don't try to rmdir the top/root subvolume dir.
2964 */
2965 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2966 return 0;
2967
Alexander Block31db9f72012-07-25 23:19:24 +02002968 path = alloc_path_for_send();
2969 if (!path)
2970 return -ENOMEM;
2971
2972 key.objectid = dir;
2973 key.type = BTRFS_DIR_INDEX_KEY;
2974 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002975 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2976 if (ret < 0)
2977 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002978
2979 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002980 struct waiting_dir_move *dm;
2981
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002982 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2983 ret = btrfs_next_leaf(root, path);
2984 if (ret < 0)
2985 goto out;
2986 else if (ret > 0)
2987 break;
2988 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002989 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002990 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2991 path->slots[0]);
2992 if (found_key.objectid != key.objectid ||
2993 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002994 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002995
2996 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2997 struct btrfs_dir_item);
2998 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2999
Filipe Manana9dc44212014-02-19 14:31:44 +00003000 dm = get_waiting_dir_move(sctx, loc.objectid);
3001 if (dm) {
3002 struct orphan_dir_info *odi;
3003
3004 odi = add_orphan_dir_info(sctx, dir);
3005 if (IS_ERR(odi)) {
3006 ret = PTR_ERR(odi);
3007 goto out;
3008 }
3009 odi->gen = dir_gen;
3010 dm->rmdir_ino = dir;
3011 ret = 0;
3012 goto out;
3013 }
3014
Alexander Block31db9f72012-07-25 23:19:24 +02003015 if (loc.objectid > send_progress) {
Robbie Ko443f9d22015-06-22 17:08:45 +08003016 struct orphan_dir_info *odi;
3017
3018 odi = get_orphan_dir_info(sctx, dir);
3019 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02003020 ret = 0;
3021 goto out;
3022 }
3023
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003024 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003025 }
3026
3027 ret = 1;
3028
3029out:
3030 btrfs_free_path(path);
3031 return ret;
3032}
3033
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003034static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3035{
Filipe Manana9dc44212014-02-19 14:31:44 +00003036 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003037
Filipe Manana9dc44212014-02-19 14:31:44 +00003038 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003039}
3040
Filipe Manana8b191a62015-04-09 14:09:14 +01003041static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003042{
3043 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3044 struct rb_node *parent = NULL;
3045 struct waiting_dir_move *entry, *dm;
3046
David Sterbae780b0d2016-01-18 18:42:13 +01003047 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003048 if (!dm)
3049 return -ENOMEM;
3050 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003051 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003052 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003053
3054 while (*p) {
3055 parent = *p;
3056 entry = rb_entry(parent, struct waiting_dir_move, node);
3057 if (ino < entry->ino) {
3058 p = &(*p)->rb_left;
3059 } else if (ino > entry->ino) {
3060 p = &(*p)->rb_right;
3061 } else {
3062 kfree(dm);
3063 return -EEXIST;
3064 }
3065 }
3066
3067 rb_link_node(&dm->node, parent, p);
3068 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3069 return 0;
3070}
3071
Filipe Manana9dc44212014-02-19 14:31:44 +00003072static struct waiting_dir_move *
3073get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003074{
3075 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3076 struct waiting_dir_move *entry;
3077
3078 while (n) {
3079 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003080 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003081 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003082 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003083 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003084 else
3085 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003086 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003087 return NULL;
3088}
3089
3090static void free_waiting_dir_move(struct send_ctx *sctx,
3091 struct waiting_dir_move *dm)
3092{
3093 if (!dm)
3094 return;
3095 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3096 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003097}
3098
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003099static int add_pending_dir_move(struct send_ctx *sctx,
3100 u64 ino,
3101 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003102 u64 parent_ino,
3103 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003104 struct list_head *deleted_refs,
3105 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003106{
3107 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3108 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003109 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003110 struct recorded_ref *cur;
3111 int exists = 0;
3112 int ret;
3113
David Sterbae780b0d2016-01-18 18:42:13 +01003114 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003115 if (!pm)
3116 return -ENOMEM;
3117 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003118 pm->ino = ino;
3119 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003120 INIT_LIST_HEAD(&pm->list);
3121 INIT_LIST_HEAD(&pm->update_refs);
3122 RB_CLEAR_NODE(&pm->node);
3123
3124 while (*p) {
3125 parent = *p;
3126 entry = rb_entry(parent, struct pending_dir_move, node);
3127 if (parent_ino < entry->parent_ino) {
3128 p = &(*p)->rb_left;
3129 } else if (parent_ino > entry->parent_ino) {
3130 p = &(*p)->rb_right;
3131 } else {
3132 exists = 1;
3133 break;
3134 }
3135 }
3136
Filipe Mananaf9594922014-03-27 20:14:01 +00003137 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003138 ret = dup_ref(cur, &pm->update_refs);
3139 if (ret < 0)
3140 goto out;
3141 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003142 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003143 ret = dup_ref(cur, &pm->update_refs);
3144 if (ret < 0)
3145 goto out;
3146 }
3147
Filipe Manana8b191a62015-04-09 14:09:14 +01003148 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003149 if (ret)
3150 goto out;
3151
3152 if (exists) {
3153 list_add_tail(&pm->list, &entry->list);
3154 } else {
3155 rb_link_node(&pm->node, parent, p);
3156 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3157 }
3158 ret = 0;
3159out:
3160 if (ret) {
3161 __free_recorded_refs(&pm->update_refs);
3162 kfree(pm);
3163 }
3164 return ret;
3165}
3166
3167static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3168 u64 parent_ino)
3169{
3170 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3171 struct pending_dir_move *entry;
3172
3173 while (n) {
3174 entry = rb_entry(n, struct pending_dir_move, node);
3175 if (parent_ino < entry->parent_ino)
3176 n = n->rb_left;
3177 else if (parent_ino > entry->parent_ino)
3178 n = n->rb_right;
3179 else
3180 return entry;
3181 }
3182 return NULL;
3183}
3184
Robbie Ko801bec32015-06-23 18:39:46 +08003185static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3186 u64 ino, u64 gen, u64 *ancestor_ino)
3187{
3188 int ret = 0;
3189 u64 parent_inode = 0;
3190 u64 parent_gen = 0;
3191 u64 start_ino = ino;
3192
3193 *ancestor_ino = 0;
3194 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3195 fs_path_reset(name);
3196
3197 if (is_waiting_for_rm(sctx, ino))
3198 break;
3199 if (is_waiting_for_move(sctx, ino)) {
3200 if (*ancestor_ino == 0)
3201 *ancestor_ino = ino;
3202 ret = get_first_ref(sctx->parent_root, ino,
3203 &parent_inode, &parent_gen, name);
3204 } else {
3205 ret = __get_cur_name_and_parent(sctx, ino, gen,
3206 &parent_inode,
3207 &parent_gen, name);
3208 if (ret > 0) {
3209 ret = 0;
3210 break;
3211 }
3212 }
3213 if (ret < 0)
3214 break;
3215 if (parent_inode == start_ino) {
3216 ret = 1;
3217 if (*ancestor_ino == 0)
3218 *ancestor_ino = ino;
3219 break;
3220 }
3221 ino = parent_inode;
3222 gen = parent_gen;
3223 }
3224 return ret;
3225}
3226
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003227static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3228{
3229 struct fs_path *from_path = NULL;
3230 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003231 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003232 u64 orig_progress = sctx->send_progress;
3233 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003234 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003235 struct waiting_dir_move *dm = NULL;
3236 u64 rmdir_ino = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003237 u64 ancestor;
3238 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003239 int ret;
3240
Filipe Manana2b863a12014-02-16 13:43:11 +00003241 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003242 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003243 if (!name || !from_path) {
3244 ret = -ENOMEM;
3245 goto out;
3246 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003247
Filipe Manana9dc44212014-02-19 14:31:44 +00003248 dm = get_waiting_dir_move(sctx, pm->ino);
3249 ASSERT(dm);
3250 rmdir_ino = dm->rmdir_ino;
Robbie Ko801bec32015-06-23 18:39:46 +08003251 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003252 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003253
Robbie Ko801bec32015-06-23 18:39:46 +08003254 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003255 ret = gen_unique_name(sctx, pm->ino,
3256 pm->gen, from_path);
3257 } else {
3258 ret = get_first_ref(sctx->parent_root, pm->ino,
3259 &parent_ino, &parent_gen, name);
3260 if (ret < 0)
3261 goto out;
3262 ret = get_cur_path(sctx, parent_ino, parent_gen,
3263 from_path);
3264 if (ret < 0)
3265 goto out;
3266 ret = fs_path_add_path(from_path, name);
3267 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003268 if (ret < 0)
3269 goto out;
3270
Filipe Mananaf9594922014-03-27 20:14:01 +00003271 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003272 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003273 if (ret < 0)
3274 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003275 if (ret) {
3276 LIST_HEAD(deleted_refs);
3277 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3278 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3279 &pm->update_refs, &deleted_refs,
3280 is_orphan);
3281 if (ret < 0)
3282 goto out;
3283 if (rmdir_ino) {
3284 dm = get_waiting_dir_move(sctx, pm->ino);
3285 ASSERT(dm);
3286 dm->rmdir_ino = rmdir_ino;
3287 }
3288 goto out;
3289 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003290 fs_path_reset(name);
3291 to_path = name;
3292 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003293 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3294 if (ret < 0)
3295 goto out;
3296
3297 ret = send_rename(sctx, from_path, to_path);
3298 if (ret < 0)
3299 goto out;
3300
Filipe Manana9dc44212014-02-19 14:31:44 +00003301 if (rmdir_ino) {
3302 struct orphan_dir_info *odi;
3303
3304 odi = get_orphan_dir_info(sctx, rmdir_ino);
3305 if (!odi) {
3306 /* already deleted */
3307 goto finish;
3308 }
Robbie Ko99ea42d2015-06-23 18:39:49 +08003309 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003310 if (ret < 0)
3311 goto out;
3312 if (!ret)
3313 goto finish;
3314
3315 name = fs_path_alloc();
3316 if (!name) {
3317 ret = -ENOMEM;
3318 goto out;
3319 }
3320 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3321 if (ret < 0)
3322 goto out;
3323 ret = send_rmdir(sctx, name);
3324 if (ret < 0)
3325 goto out;
3326 free_orphan_dir_info(sctx, odi);
3327 }
3328
3329finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003330 ret = send_utimes(sctx, pm->ino, pm->gen);
3331 if (ret < 0)
3332 goto out;
3333
3334 /*
3335 * After rename/move, need to update the utimes of both new parent(s)
3336 * and old parent(s).
3337 */
3338 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003339 /*
3340 * The parent inode might have been deleted in the send snapshot
3341 */
3342 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3343 NULL, NULL, NULL, NULL, NULL);
3344 if (ret == -ENOENT) {
3345 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003346 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003347 }
3348 if (ret < 0)
3349 goto out;
3350
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003351 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3352 if (ret < 0)
3353 goto out;
3354 }
3355
3356out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003357 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003358 fs_path_free(from_path);
3359 fs_path_free(to_path);
3360 sctx->send_progress = orig_progress;
3361
3362 return ret;
3363}
3364
3365static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3366{
3367 if (!list_empty(&m->list))
3368 list_del(&m->list);
3369 if (!RB_EMPTY_NODE(&m->node))
3370 rb_erase(&m->node, &sctx->pending_dir_moves);
3371 __free_recorded_refs(&m->update_refs);
3372 kfree(m);
3373}
3374
Robbie Koaeda9162018-11-14 18:32:37 +00003375static void tail_append_pending_moves(struct send_ctx *sctx,
3376 struct pending_dir_move *moves,
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003377 struct list_head *stack)
3378{
3379 if (list_empty(&moves->list)) {
3380 list_add_tail(&moves->list, stack);
3381 } else {
3382 LIST_HEAD(list);
3383 list_splice_init(&moves->list, &list);
3384 list_add_tail(&moves->list, stack);
3385 list_splice_tail(&list, stack);
3386 }
Robbie Koaeda9162018-11-14 18:32:37 +00003387 if (!RB_EMPTY_NODE(&moves->node)) {
3388 rb_erase(&moves->node, &sctx->pending_dir_moves);
3389 RB_CLEAR_NODE(&moves->node);
3390 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003391}
3392
3393static int apply_children_dir_moves(struct send_ctx *sctx)
3394{
3395 struct pending_dir_move *pm;
3396 struct list_head stack;
3397 u64 parent_ino = sctx->cur_ino;
3398 int ret = 0;
3399
3400 pm = get_pending_dir_moves(sctx, parent_ino);
3401 if (!pm)
3402 return 0;
3403
3404 INIT_LIST_HEAD(&stack);
Robbie Koaeda9162018-11-14 18:32:37 +00003405 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003406
3407 while (!list_empty(&stack)) {
3408 pm = list_first_entry(&stack, struct pending_dir_move, list);
3409 parent_ino = pm->ino;
3410 ret = apply_dir_move(sctx, pm);
3411 free_pending_move(sctx, pm);
3412 if (ret)
3413 goto out;
3414 pm = get_pending_dir_moves(sctx, parent_ino);
3415 if (pm)
Robbie Koaeda9162018-11-14 18:32:37 +00003416 tail_append_pending_moves(sctx, pm, &stack);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003417 }
3418 return 0;
3419
3420out:
3421 while (!list_empty(&stack)) {
3422 pm = list_first_entry(&stack, struct pending_dir_move, list);
3423 free_pending_move(sctx, pm);
3424 }
3425 return ret;
3426}
3427
Filipe Manana84471e22015-02-28 22:29:22 +00003428/*
3429 * We might need to delay a directory rename even when no ancestor directory
3430 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3431 * renamed. This happens when we rename a directory to the old name (the name
3432 * in the parent root) of some other unrelated directory that got its rename
3433 * delayed due to some ancestor with higher number that got renamed.
3434 *
3435 * Example:
3436 *
3437 * Parent snapshot:
3438 * . (ino 256)
3439 * |---- a/ (ino 257)
3440 * | |---- file (ino 260)
3441 * |
3442 * |---- b/ (ino 258)
3443 * |---- c/ (ino 259)
3444 *
3445 * Send snapshot:
3446 * . (ino 256)
3447 * |---- a/ (ino 258)
3448 * |---- x/ (ino 259)
3449 * |---- y/ (ino 257)
3450 * |----- file (ino 260)
3451 *
3452 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3453 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3454 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3455 * must issue is:
3456 *
3457 * 1 - rename 259 from 'c' to 'x'
3458 * 2 - rename 257 from 'a' to 'x/y'
3459 * 3 - rename 258 from 'b' to 'a'
3460 *
3461 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3462 * be done right away and < 0 on error.
3463 */
3464static int wait_for_dest_dir_move(struct send_ctx *sctx,
3465 struct recorded_ref *parent_ref,
3466 const bool is_orphan)
3467{
3468 struct btrfs_path *path;
3469 struct btrfs_key key;
3470 struct btrfs_key di_key;
3471 struct btrfs_dir_item *di;
3472 u64 left_gen;
3473 u64 right_gen;
3474 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003475 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003476
3477 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3478 return 0;
3479
3480 path = alloc_path_for_send();
3481 if (!path)
3482 return -ENOMEM;
3483
3484 key.objectid = parent_ref->dir;
3485 key.type = BTRFS_DIR_ITEM_KEY;
3486 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3487
3488 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3489 if (ret < 0) {
3490 goto out;
3491 } else if (ret > 0) {
3492 ret = 0;
3493 goto out;
3494 }
3495
3496 di = btrfs_match_dir_item_name(sctx->parent_root, path,
3497 parent_ref->name, parent_ref->name_len);
3498 if (!di) {
3499 ret = 0;
3500 goto out;
3501 }
3502 /*
3503 * di_key.objectid has the number of the inode that has a dentry in the
3504 * parent directory with the same name that sctx->cur_ino is being
3505 * renamed to. We need to check if that inode is in the send root as
3506 * well and if it is currently marked as an inode with a pending rename,
3507 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3508 * that it happens after that other inode is renamed.
3509 */
3510 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3511 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3512 ret = 0;
3513 goto out;
3514 }
3515
3516 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3517 &left_gen, NULL, NULL, NULL, NULL);
3518 if (ret < 0)
3519 goto out;
3520 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3521 &right_gen, NULL, NULL, NULL, NULL);
3522 if (ret < 0) {
3523 if (ret == -ENOENT)
3524 ret = 0;
3525 goto out;
3526 }
3527
3528 /* Different inode, no need to delay the rename of sctx->cur_ino */
3529 if (right_gen != left_gen) {
3530 ret = 0;
3531 goto out;
3532 }
3533
Robbie Ko801bec32015-06-23 18:39:46 +08003534 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3535 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003536 ret = add_pending_dir_move(sctx,
3537 sctx->cur_ino,
3538 sctx->cur_inode_gen,
3539 di_key.objectid,
3540 &sctx->new_refs,
3541 &sctx->deleted_refs,
3542 is_orphan);
3543 if (!ret)
3544 ret = 1;
3545 }
3546out:
3547 btrfs_free_path(path);
3548 return ret;
3549}
3550
Filipe Manana80aa6022015-03-27 17:50:45 +00003551/*
3552 * Check if ino ino1 is an ancestor of inode ino2 in the given root.
3553 * Return 1 if true, 0 if false and < 0 on error.
3554 */
3555static int is_ancestor(struct btrfs_root *root,
3556 const u64 ino1,
3557 const u64 ino1_gen,
3558 const u64 ino2,
3559 struct fs_path *fs_path)
3560{
3561 u64 ino = ino2;
3562
3563 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3564 int ret;
3565 u64 parent;
3566 u64 parent_gen;
3567
3568 fs_path_reset(fs_path);
3569 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3570 if (ret < 0) {
3571 if (ret == -ENOENT && ino == ino2)
3572 ret = 0;
3573 return ret;
3574 }
3575 if (parent == ino1)
3576 return parent_gen == ino1_gen ? 1 : 0;
3577 ino = parent;
3578 }
3579 return 0;
3580}
3581
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003582static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003583 struct recorded_ref *parent_ref,
3584 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003585{
Filipe Mananaf9594922014-03-27 20:14:01 +00003586 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003587 u64 ino = parent_ref->dir;
3588 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003589 struct fs_path *path_before = NULL;
3590 struct fs_path *path_after = NULL;
3591 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003592
3593 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003594 path_before = fs_path_alloc();
3595 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003596 ret = -ENOMEM;
3597 goto out;
3598 }
3599
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003600 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003601 * Our current directory inode may not yet be renamed/moved because some
3602 * ancestor (immediate or not) has to be renamed/moved first. So find if
3603 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003604 * that ancestor is processed to avoid path build infinite loops (done
3605 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003606 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003607 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3608 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003609 /*
3610 * If the current inode is an ancestor of ino in the
3611 * parent root, we need to delay the rename of the
3612 * current inode, otherwise don't delayed the rename
3613 * because we can end up with a circular dependency
3614 * of renames, resulting in some directories never
3615 * getting the respective rename operations issued in
3616 * the send stream or getting into infinite path build
3617 * loops.
3618 */
3619 ret = is_ancestor(sctx->parent_root,
3620 sctx->cur_ino, sctx->cur_inode_gen,
3621 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003622 if (ret)
3623 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003624 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003625
3626 fs_path_reset(path_before);
3627 fs_path_reset(path_after);
3628
3629 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananaf9594922014-03-27 20:14:01 +00003630 NULL, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003631 if (ret < 0)
3632 goto out;
3633 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3634 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003635 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003636 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003637 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003638 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003639 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003640 }
3641
3642 len1 = fs_path_len(path_before);
3643 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003644 if (ino > sctx->cur_ino &&
3645 (parent_ino_before != parent_ino_after || len1 != len2 ||
3646 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003647 ret = 1;
Filipe Mananaf9594922014-03-27 20:14:01 +00003648 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003649 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003650 ino = parent_ino_after;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003651 }
3652
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003653out:
3654 fs_path_free(path_before);
3655 fs_path_free(path_after);
3656
Filipe Mananaf9594922014-03-27 20:14:01 +00003657 if (ret == 1) {
3658 ret = add_pending_dir_move(sctx,
3659 sctx->cur_ino,
3660 sctx->cur_inode_gen,
3661 ino,
3662 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003663 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003664 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003665 if (!ret)
3666 ret = 1;
3667 }
3668
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003669 return ret;
3670}
3671
Alexander Block31db9f72012-07-25 23:19:24 +02003672/*
3673 * This does all the move/link/unlink/rmdir magic.
3674 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003675static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003676{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003677 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003678 int ret = 0;
3679 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003680 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003681 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003682 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003683 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003684 u64 ow_gen;
3685 int did_overwrite = 0;
3686 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003687 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003688 bool can_rename = true;
Alexander Block31db9f72012-07-25 23:19:24 +02003689
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003690 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003691
Alexander Block6d85ed02012-08-01 14:48:59 +02003692 /*
3693 * This should never happen as the root dir always has the same ref
3694 * which is always '..'
3695 */
3696 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003697 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003698
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003699 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003700 if (!valid_path) {
3701 ret = -ENOMEM;
3702 goto out;
3703 }
3704
Alexander Block31db9f72012-07-25 23:19:24 +02003705 /*
3706 * First, check if the first ref of the current inode was overwritten
3707 * before. If yes, we know that the current inode was already orphanized
3708 * and thus use the orphan name. If not, we can use get_cur_path to
3709 * get the path of the first ref as it would like while receiving at
3710 * this point in time.
3711 * New inodes are always orphan at the beginning, so force to use the
3712 * orphan name in this case.
3713 * The first ref is stored in valid_path and will be updated if it
3714 * gets moved around.
3715 */
3716 if (!sctx->cur_inode_new) {
3717 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3718 sctx->cur_inode_gen);
3719 if (ret < 0)
3720 goto out;
3721 if (ret)
3722 did_overwrite = 1;
3723 }
3724 if (sctx->cur_inode_new || did_overwrite) {
3725 ret = gen_unique_name(sctx, sctx->cur_ino,
3726 sctx->cur_inode_gen, valid_path);
3727 if (ret < 0)
3728 goto out;
3729 is_orphan = 1;
3730 } else {
3731 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3732 valid_path);
3733 if (ret < 0)
3734 goto out;
3735 }
3736
3737 list_for_each_entry(cur, &sctx->new_refs, list) {
3738 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003739 * We may have refs where the parent directory does not exist
3740 * yet. This happens if the parent directories inum is higher
3741 * the the current inum. To handle this case, we create the
3742 * parent directory out of order. But we need to check if this
3743 * did already happen before due to other refs in the same dir.
3744 */
3745 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3746 if (ret < 0)
3747 goto out;
3748 if (ret == inode_state_will_create) {
3749 ret = 0;
3750 /*
3751 * First check if any of the current inodes refs did
3752 * already create the dir.
3753 */
3754 list_for_each_entry(cur2, &sctx->new_refs, list) {
3755 if (cur == cur2)
3756 break;
3757 if (cur2->dir == cur->dir) {
3758 ret = 1;
3759 break;
3760 }
3761 }
3762
3763 /*
3764 * If that did not happen, check if a previous inode
3765 * did already create the dir.
3766 */
3767 if (!ret)
3768 ret = did_create_dir(sctx, cur->dir);
3769 if (ret < 0)
3770 goto out;
3771 if (!ret) {
3772 ret = send_create_inode(sctx, cur->dir);
3773 if (ret < 0)
3774 goto out;
3775 }
3776 }
3777
3778 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003779 * Check if this new ref would overwrite the first ref of
3780 * another unprocessed inode. If yes, orphanize the
3781 * overwritten inode. If we find an overwritten ref that is
3782 * not the first ref, simply unlink it.
3783 */
3784 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3785 cur->name, cur->name_len,
3786 &ow_inode, &ow_gen);
3787 if (ret < 0)
3788 goto out;
3789 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003790 ret = is_first_ref(sctx->parent_root,
3791 ow_inode, cur->dir, cur->name,
3792 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003793 if (ret < 0)
3794 goto out;
3795 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003796 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003797 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003798
Alexander Block31db9f72012-07-25 23:19:24 +02003799 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3800 cur->full_path);
3801 if (ret < 0)
3802 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003803
3804 /*
3805 * If ow_inode has its rename operation delayed
3806 * make sure that its orphanized name is used in
3807 * the source path when performing its rename
3808 * operation.
3809 */
3810 if (is_waiting_for_move(sctx, ow_inode)) {
3811 wdm = get_waiting_dir_move(sctx,
3812 ow_inode);
3813 ASSERT(wdm);
3814 wdm->orphanized = true;
3815 }
3816
Filipe Manana8996a482015-03-12 15:16:20 +00003817 /*
3818 * Make sure we clear our orphanized inode's
3819 * name from the name cache. This is because the
3820 * inode ow_inode might be an ancestor of some
3821 * other inode that will be orphanized as well
3822 * later and has an inode number greater than
3823 * sctx->send_progress. We need to prevent
3824 * future name lookups from using the old name
3825 * and get instead the orphan name.
3826 */
3827 nce = name_cache_search(sctx, ow_inode, ow_gen);
3828 if (nce) {
3829 name_cache_delete(sctx, nce);
3830 kfree(nce);
3831 }
Robbie Ko801bec32015-06-23 18:39:46 +08003832
3833 /*
3834 * ow_inode might currently be an ancestor of
3835 * cur_ino, therefore compute valid_path (the
3836 * current path of cur_ino) again because it
3837 * might contain the pre-orphanization name of
3838 * ow_inode, which is no longer valid.
3839 */
3840 fs_path_reset(valid_path);
3841 ret = get_cur_path(sctx, sctx->cur_ino,
3842 sctx->cur_inode_gen, valid_path);
3843 if (ret < 0)
3844 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003845 } else {
3846 ret = send_unlink(sctx, cur->full_path);
3847 if (ret < 0)
3848 goto out;
3849 }
3850 }
3851
Filipe Manana84471e22015-02-28 22:29:22 +00003852 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3853 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3854 if (ret < 0)
3855 goto out;
3856 if (ret == 1) {
3857 can_rename = false;
3858 *pending_move = 1;
3859 }
3860 }
3861
Filipe Manana8b191a62015-04-09 14:09:14 +01003862 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3863 can_rename) {
3864 ret = wait_for_parent_move(sctx, cur, is_orphan);
3865 if (ret < 0)
3866 goto out;
3867 if (ret == 1) {
3868 can_rename = false;
3869 *pending_move = 1;
3870 }
3871 }
3872
Alexander Block31db9f72012-07-25 23:19:24 +02003873 /*
3874 * link/move the ref to the new place. If we have an orphan
3875 * inode, move it and update valid_path. If not, link or move
3876 * it depending on the inode mode.
3877 */
Filipe Manana84471e22015-02-28 22:29:22 +00003878 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003879 ret = send_rename(sctx, valid_path, cur->full_path);
3880 if (ret < 0)
3881 goto out;
3882 is_orphan = 0;
3883 ret = fs_path_copy(valid_path, cur->full_path);
3884 if (ret < 0)
3885 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00003886 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003887 if (S_ISDIR(sctx->cur_inode_mode)) {
3888 /*
3889 * Dirs can't be linked, so move it. For moved
3890 * dirs, we always have one new and one deleted
3891 * ref. The deleted ref is ignored later.
3892 */
Filipe Manana8b191a62015-04-09 14:09:14 +01003893 ret = send_rename(sctx, valid_path,
3894 cur->full_path);
3895 if (!ret)
3896 ret = fs_path_copy(valid_path,
3897 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003898 if (ret < 0)
3899 goto out;
3900 } else {
3901 ret = send_link(sctx, cur->full_path,
3902 valid_path);
3903 if (ret < 0)
3904 goto out;
3905 }
3906 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003907 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003908 if (ret < 0)
3909 goto out;
3910 }
3911
3912 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3913 /*
3914 * Check if we can already rmdir the directory. If not,
3915 * orphanize it. For every dir item inside that gets deleted
3916 * later, we do this check again and rmdir it then if possible.
3917 * See the use of check_dirs for more details.
3918 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003919 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3920 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003921 if (ret < 0)
3922 goto out;
3923 if (ret) {
3924 ret = send_rmdir(sctx, valid_path);
3925 if (ret < 0)
3926 goto out;
3927 } else if (!is_orphan) {
3928 ret = orphanize_inode(sctx, sctx->cur_ino,
3929 sctx->cur_inode_gen, valid_path);
3930 if (ret < 0)
3931 goto out;
3932 is_orphan = 1;
3933 }
3934
3935 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003936 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003937 if (ret < 0)
3938 goto out;
3939 }
Alexander Blockccf16262012-07-28 11:46:29 +02003940 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3941 !list_empty(&sctx->deleted_refs)) {
3942 /*
3943 * We have a moved dir. Add the old parent to check_dirs
3944 */
3945 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3946 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003947 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003948 if (ret < 0)
3949 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003950 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3951 /*
3952 * We have a non dir inode. Go through all deleted refs and
3953 * unlink them if they were not already overwritten by other
3954 * inodes.
3955 */
3956 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3957 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3958 sctx->cur_ino, sctx->cur_inode_gen,
3959 cur->name, cur->name_len);
3960 if (ret < 0)
3961 goto out;
3962 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003963 ret = send_unlink(sctx, cur->full_path);
3964 if (ret < 0)
3965 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003966 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003967 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003968 if (ret < 0)
3969 goto out;
3970 }
Alexander Block31db9f72012-07-25 23:19:24 +02003971 /*
3972 * If the inode is still orphan, unlink the orphan. This may
3973 * happen when a previous inode did overwrite the first ref
3974 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003975 * inode. Unlinking does not mean that the inode is deleted in
3976 * all cases. There may still be links to this inode in other
3977 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003978 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003979 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003980 ret = send_unlink(sctx, valid_path);
3981 if (ret < 0)
3982 goto out;
3983 }
3984 }
3985
3986 /*
3987 * We did collect all parent dirs where cur_inode was once located. We
3988 * now go through all these dirs and check if they are pending for
3989 * deletion and if it's finally possible to perform the rmdir now.
3990 * We also update the inode stats of the parent dirs here.
3991 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003992 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003993 /*
3994 * In case we had refs into dirs that were not processed yet,
3995 * we don't need to do the utime and rmdir logic for these dirs.
3996 * The dir will be processed later.
3997 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003998 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003999 continue;
4000
Josef Bacikba5e8f22013-08-16 16:52:55 -04004001 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004002 if (ret < 0)
4003 goto out;
4004
4005 if (ret == inode_state_did_create ||
4006 ret == inode_state_no_change) {
4007 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004008 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004009 if (ret < 0)
4010 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004011 } else if (ret == inode_state_did_delete &&
4012 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004013 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4014 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004015 if (ret < 0)
4016 goto out;
4017 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004018 ret = get_cur_path(sctx, cur->dir,
4019 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004020 if (ret < 0)
4021 goto out;
4022 ret = send_rmdir(sctx, valid_path);
4023 if (ret < 0)
4024 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004025 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004026 }
4027 }
4028 }
4029
Alexander Block31db9f72012-07-25 23:19:24 +02004030 ret = 0;
4031
4032out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004033 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004034 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004035 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004036 return ret;
4037}
4038
Liu Boa4d96d62014-03-03 21:31:03 +08004039static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
4040 struct fs_path *name, void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004041{
4042 int ret = 0;
4043 struct send_ctx *sctx = ctx;
4044 struct fs_path *p;
4045 u64 gen;
4046
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004047 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004048 if (!p)
4049 return -ENOMEM;
4050
Liu Boa4d96d62014-03-03 21:31:03 +08004051 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004052 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004053 if (ret < 0)
4054 goto out;
4055
Alexander Block31db9f72012-07-25 23:19:24 +02004056 ret = get_cur_path(sctx, dir, gen, p);
4057 if (ret < 0)
4058 goto out;
4059 ret = fs_path_add_path(p, name);
4060 if (ret < 0)
4061 goto out;
4062
Liu Boa4d96d62014-03-03 21:31:03 +08004063 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004064
4065out:
4066 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004067 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004068 return ret;
4069}
4070
Liu Boa4d96d62014-03-03 21:31:03 +08004071static int __record_new_ref(int num, u64 dir, int index,
4072 struct fs_path *name,
4073 void *ctx)
4074{
4075 struct send_ctx *sctx = ctx;
4076 return record_ref(sctx->send_root, num, dir, index, name,
4077 ctx, &sctx->new_refs);
4078}
4079
4080
Alexander Block31db9f72012-07-25 23:19:24 +02004081static int __record_deleted_ref(int num, u64 dir, int index,
4082 struct fs_path *name,
4083 void *ctx)
4084{
Alexander Block31db9f72012-07-25 23:19:24 +02004085 struct send_ctx *sctx = ctx;
Liu Boa4d96d62014-03-03 21:31:03 +08004086 return record_ref(sctx->parent_root, num, dir, index, name,
4087 ctx, &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004088}
4089
4090static int record_new_ref(struct send_ctx *sctx)
4091{
4092 int ret;
4093
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004094 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4095 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004096 if (ret < 0)
4097 goto out;
4098 ret = 0;
4099
4100out:
4101 return ret;
4102}
4103
4104static int record_deleted_ref(struct send_ctx *sctx)
4105{
4106 int ret;
4107
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004108 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4109 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004110 if (ret < 0)
4111 goto out;
4112 ret = 0;
4113
4114out:
4115 return ret;
4116}
4117
4118struct find_ref_ctx {
4119 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004120 u64 dir_gen;
4121 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004122 struct fs_path *name;
4123 int found_idx;
4124};
4125
4126static int __find_iref(int num, u64 dir, int index,
4127 struct fs_path *name,
4128 void *ctx_)
4129{
4130 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004131 u64 dir_gen;
4132 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004133
4134 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4135 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004136 /*
4137 * To avoid doing extra lookups we'll only do this if everything
4138 * else matches.
4139 */
4140 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4141 NULL, NULL, NULL);
4142 if (ret)
4143 return ret;
4144 if (dir_gen != ctx->dir_gen)
4145 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004146 ctx->found_idx = num;
4147 return 1;
4148 }
4149 return 0;
4150}
4151
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004152static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004153 struct btrfs_path *path,
4154 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004155 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004156{
4157 int ret;
4158 struct find_ref_ctx ctx;
4159
4160 ctx.dir = dir;
4161 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004162 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004163 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004164 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004165
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004166 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004167 if (ret < 0)
4168 return ret;
4169
4170 if (ctx.found_idx == -1)
4171 return -ENOENT;
4172
4173 return ctx.found_idx;
4174}
4175
4176static int __record_changed_new_ref(int num, u64 dir, int index,
4177 struct fs_path *name,
4178 void *ctx)
4179{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004180 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004181 int ret;
4182 struct send_ctx *sctx = ctx;
4183
Josef Bacikba5e8f22013-08-16 16:52:55 -04004184 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4185 NULL, NULL, NULL);
4186 if (ret)
4187 return ret;
4188
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004189 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004190 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004191 if (ret == -ENOENT)
4192 ret = __record_new_ref(num, dir, index, name, sctx);
4193 else if (ret > 0)
4194 ret = 0;
4195
4196 return ret;
4197}
4198
4199static int __record_changed_deleted_ref(int num, u64 dir, int index,
4200 struct fs_path *name,
4201 void *ctx)
4202{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004203 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004204 int ret;
4205 struct send_ctx *sctx = ctx;
4206
Josef Bacikba5e8f22013-08-16 16:52:55 -04004207 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4208 NULL, NULL, NULL);
4209 if (ret)
4210 return ret;
4211
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004212 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004213 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004214 if (ret == -ENOENT)
4215 ret = __record_deleted_ref(num, dir, index, name, sctx);
4216 else if (ret > 0)
4217 ret = 0;
4218
4219 return ret;
4220}
4221
4222static int record_changed_ref(struct send_ctx *sctx)
4223{
4224 int ret = 0;
4225
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004226 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004227 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4228 if (ret < 0)
4229 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004230 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004231 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4232 if (ret < 0)
4233 goto out;
4234 ret = 0;
4235
4236out:
4237 return ret;
4238}
4239
4240/*
4241 * Record and process all refs at once. Needed when an inode changes the
4242 * generation number, which means that it was deleted and recreated.
4243 */
4244static int process_all_refs(struct send_ctx *sctx,
4245 enum btrfs_compare_tree_result cmd)
4246{
4247 int ret;
4248 struct btrfs_root *root;
4249 struct btrfs_path *path;
4250 struct btrfs_key key;
4251 struct btrfs_key found_key;
4252 struct extent_buffer *eb;
4253 int slot;
4254 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004255 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004256
4257 path = alloc_path_for_send();
4258 if (!path)
4259 return -ENOMEM;
4260
4261 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4262 root = sctx->send_root;
4263 cb = __record_new_ref;
4264 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4265 root = sctx->parent_root;
4266 cb = __record_deleted_ref;
4267 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004268 btrfs_err(sctx->send_root->fs_info,
4269 "Wrong command %d in process_all_refs", cmd);
4270 ret = -EINVAL;
4271 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004272 }
4273
4274 key.objectid = sctx->cmp_key->objectid;
4275 key.type = BTRFS_INODE_REF_KEY;
4276 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004277 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4278 if (ret < 0)
4279 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004280
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004281 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004282 eb = path->nodes[0];
4283 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004284 if (slot >= btrfs_header_nritems(eb)) {
4285 ret = btrfs_next_leaf(root, path);
4286 if (ret < 0)
4287 goto out;
4288 else if (ret > 0)
4289 break;
4290 continue;
4291 }
4292
Alexander Block31db9f72012-07-25 23:19:24 +02004293 btrfs_item_key_to_cpu(eb, &found_key, slot);
4294
4295 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004296 (found_key.type != BTRFS_INODE_REF_KEY &&
4297 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004298 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004299
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004300 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004301 if (ret < 0)
4302 goto out;
4303
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004304 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004305 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004306 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004307
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004308 /*
4309 * We don't actually care about pending_move as we are simply
4310 * re-creating this inode and will be rename'ing it into place once we
4311 * rename the parent directory.
4312 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004313 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004314out:
4315 btrfs_free_path(path);
4316 return ret;
4317}
4318
4319static int send_set_xattr(struct send_ctx *sctx,
4320 struct fs_path *path,
4321 const char *name, int name_len,
4322 const char *data, int data_len)
4323{
4324 int ret = 0;
4325
4326 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4327 if (ret < 0)
4328 goto out;
4329
4330 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4331 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4332 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4333
4334 ret = send_cmd(sctx);
4335
4336tlv_put_failure:
4337out:
4338 return ret;
4339}
4340
4341static int send_remove_xattr(struct send_ctx *sctx,
4342 struct fs_path *path,
4343 const char *name, int name_len)
4344{
4345 int ret = 0;
4346
4347 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4348 if (ret < 0)
4349 goto out;
4350
4351 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4352 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4353
4354 ret = send_cmd(sctx);
4355
4356tlv_put_failure:
4357out:
4358 return ret;
4359}
4360
4361static int __process_new_xattr(int num, struct btrfs_key *di_key,
4362 const char *name, int name_len,
4363 const char *data, int data_len,
4364 u8 type, void *ctx)
4365{
4366 int ret;
4367 struct send_ctx *sctx = ctx;
4368 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004369 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004370
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004371 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004372 if (!p)
4373 return -ENOMEM;
4374
4375 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004376 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004377 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004378 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004379 * only contains the version number and no entries.
4380 */
4381 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4382 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4383 if (data_len == 0) {
4384 dummy_acl.a_version =
4385 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4386 data = (char *)&dummy_acl;
4387 data_len = sizeof(dummy_acl);
4388 }
4389 }
4390
4391 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4392 if (ret < 0)
4393 goto out;
4394
4395 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4396
4397out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004398 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004399 return ret;
4400}
4401
4402static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4403 const char *name, int name_len,
4404 const char *data, int data_len,
4405 u8 type, void *ctx)
4406{
4407 int ret;
4408 struct send_ctx *sctx = ctx;
4409 struct fs_path *p;
4410
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004411 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004412 if (!p)
4413 return -ENOMEM;
4414
4415 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4416 if (ret < 0)
4417 goto out;
4418
4419 ret = send_remove_xattr(sctx, p, name, name_len);
4420
4421out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004422 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004423 return ret;
4424}
4425
4426static int process_new_xattr(struct send_ctx *sctx)
4427{
4428 int ret = 0;
4429
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004430 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4431 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004432
4433 return ret;
4434}
4435
4436static int process_deleted_xattr(struct send_ctx *sctx)
4437{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004438 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4439 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004440}
4441
4442struct find_xattr_ctx {
4443 const char *name;
4444 int name_len;
4445 int found_idx;
4446 char *found_data;
4447 int found_data_len;
4448};
4449
4450static int __find_xattr(int num, struct btrfs_key *di_key,
4451 const char *name, int name_len,
4452 const char *data, int data_len,
4453 u8 type, void *vctx)
4454{
4455 struct find_xattr_ctx *ctx = vctx;
4456
4457 if (name_len == ctx->name_len &&
4458 strncmp(name, ctx->name, name_len) == 0) {
4459 ctx->found_idx = num;
4460 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004461 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004462 if (!ctx->found_data)
4463 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004464 return 1;
4465 }
4466 return 0;
4467}
4468
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004469static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004470 struct btrfs_path *path,
4471 struct btrfs_key *key,
4472 const char *name, int name_len,
4473 char **data, int *data_len)
4474{
4475 int ret;
4476 struct find_xattr_ctx ctx;
4477
4478 ctx.name = name;
4479 ctx.name_len = name_len;
4480 ctx.found_idx = -1;
4481 ctx.found_data = NULL;
4482 ctx.found_data_len = 0;
4483
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004484 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004485 if (ret < 0)
4486 return ret;
4487
4488 if (ctx.found_idx == -1)
4489 return -ENOENT;
4490 if (data) {
4491 *data = ctx.found_data;
4492 *data_len = ctx.found_data_len;
4493 } else {
4494 kfree(ctx.found_data);
4495 }
4496 return ctx.found_idx;
4497}
4498
4499
4500static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4501 const char *name, int name_len,
4502 const char *data, int data_len,
4503 u8 type, void *ctx)
4504{
4505 int ret;
4506 struct send_ctx *sctx = ctx;
4507 char *found_data = NULL;
4508 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004509
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004510 ret = find_xattr(sctx->parent_root, sctx->right_path,
4511 sctx->cmp_key, name, name_len, &found_data,
4512 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004513 if (ret == -ENOENT) {
4514 ret = __process_new_xattr(num, di_key, name, name_len, data,
4515 data_len, type, ctx);
4516 } else if (ret >= 0) {
4517 if (data_len != found_data_len ||
4518 memcmp(data, found_data, data_len)) {
4519 ret = __process_new_xattr(num, di_key, name, name_len,
4520 data, data_len, type, ctx);
4521 } else {
4522 ret = 0;
4523 }
4524 }
4525
4526 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004527 return ret;
4528}
4529
4530static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4531 const char *name, int name_len,
4532 const char *data, int data_len,
4533 u8 type, void *ctx)
4534{
4535 int ret;
4536 struct send_ctx *sctx = ctx;
4537
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004538 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4539 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004540 if (ret == -ENOENT)
4541 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4542 data_len, type, ctx);
4543 else if (ret >= 0)
4544 ret = 0;
4545
4546 return ret;
4547}
4548
4549static int process_changed_xattr(struct send_ctx *sctx)
4550{
4551 int ret = 0;
4552
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004553 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004554 sctx->cmp_key, __process_changed_new_xattr, sctx);
4555 if (ret < 0)
4556 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004557 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004558 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4559
4560out:
4561 return ret;
4562}
4563
4564static int process_all_new_xattrs(struct send_ctx *sctx)
4565{
4566 int ret;
4567 struct btrfs_root *root;
4568 struct btrfs_path *path;
4569 struct btrfs_key key;
4570 struct btrfs_key found_key;
4571 struct extent_buffer *eb;
4572 int slot;
4573
4574 path = alloc_path_for_send();
4575 if (!path)
4576 return -ENOMEM;
4577
4578 root = sctx->send_root;
4579
4580 key.objectid = sctx->cmp_key->objectid;
4581 key.type = BTRFS_XATTR_ITEM_KEY;
4582 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004583 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4584 if (ret < 0)
4585 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004586
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004587 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004588 eb = path->nodes[0];
4589 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004590 if (slot >= btrfs_header_nritems(eb)) {
4591 ret = btrfs_next_leaf(root, path);
4592 if (ret < 0) {
4593 goto out;
4594 } else if (ret > 0) {
4595 ret = 0;
4596 break;
4597 }
4598 continue;
4599 }
Alexander Block31db9f72012-07-25 23:19:24 +02004600
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004601 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004602 if (found_key.objectid != key.objectid ||
4603 found_key.type != key.type) {
4604 ret = 0;
4605 goto out;
4606 }
4607
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004608 ret = iterate_dir_item(root, path, &found_key,
4609 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004610 if (ret < 0)
4611 goto out;
4612
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004613 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004614 }
4615
4616out:
4617 btrfs_free_path(path);
4618 return ret;
4619}
4620
Josef Baciked259092013-10-25 11:36:01 -04004621static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4622{
4623 struct btrfs_root *root = sctx->send_root;
4624 struct btrfs_fs_info *fs_info = root->fs_info;
4625 struct inode *inode;
4626 struct page *page;
4627 char *addr;
4628 struct btrfs_key key;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004629 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004630 pgoff_t last_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004631 unsigned pg_offset = offset & ~PAGE_MASK;
Josef Baciked259092013-10-25 11:36:01 -04004632 ssize_t ret = 0;
4633
4634 key.objectid = sctx->cur_ino;
4635 key.type = BTRFS_INODE_ITEM_KEY;
4636 key.offset = 0;
4637
4638 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4639 if (IS_ERR(inode))
4640 return PTR_ERR(inode);
4641
4642 if (offset + len > i_size_read(inode)) {
4643 if (offset > i_size_read(inode))
4644 len = 0;
4645 else
4646 len = offset - i_size_read(inode);
4647 }
4648 if (len == 0)
4649 goto out;
4650
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004651 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd32014-03-05 10:07:35 +08004652
4653 /* initial readahead */
4654 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4655 file_ra_state_init(&sctx->ra, inode->i_mapping);
4656 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4657 last_index - index + 1);
4658
Josef Baciked259092013-10-25 11:36:01 -04004659 while (index <= last_index) {
4660 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004661 PAGE_SIZE - pg_offset);
David Sterbae780b0d2016-01-18 18:42:13 +01004662 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
Josef Baciked259092013-10-25 11:36:01 -04004663 if (!page) {
4664 ret = -ENOMEM;
4665 break;
4666 }
4667
4668 if (!PageUptodate(page)) {
4669 btrfs_readpage(NULL, page);
4670 lock_page(page);
4671 if (!PageUptodate(page)) {
4672 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004673 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004674 ret = -EIO;
4675 break;
4676 }
4677 }
4678
4679 addr = kmap(page);
4680 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4681 kunmap(page);
4682 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004683 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004684 index++;
4685 pg_offset = 0;
4686 len -= cur_len;
4687 ret += cur_len;
4688 }
4689out:
4690 iput(inode);
4691 return ret;
4692}
4693
Alexander Block31db9f72012-07-25 23:19:24 +02004694/*
4695 * Read some bytes from the current inode/file and send a write command to
4696 * user space.
4697 */
4698static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4699{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004700 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02004701 int ret = 0;
4702 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004703 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004704
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004705 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004706 if (!p)
4707 return -ENOMEM;
4708
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004709 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004710
Josef Baciked259092013-10-25 11:36:01 -04004711 num_read = fill_read_buf(sctx, offset, len);
4712 if (num_read <= 0) {
4713 if (num_read < 0)
4714 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004715 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004716 }
Alexander Block31db9f72012-07-25 23:19:24 +02004717
4718 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4719 if (ret < 0)
4720 goto out;
4721
4722 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4723 if (ret < 0)
4724 goto out;
4725
4726 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4727 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004728 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004729
4730 ret = send_cmd(sctx);
4731
4732tlv_put_failure:
4733out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004734 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004735 if (ret < 0)
4736 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004737 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004738}
4739
4740/*
4741 * Send a clone command to user space.
4742 */
4743static int send_clone(struct send_ctx *sctx,
4744 u64 offset, u32 len,
4745 struct clone_root *clone_root)
4746{
4747 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004748 struct fs_path *p;
4749 u64 gen;
4750
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004751 btrfs_debug(sctx->send_root->fs_info,
4752 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4753 offset, len, clone_root->root->objectid, clone_root->ino,
4754 clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02004755
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004756 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004757 if (!p)
4758 return -ENOMEM;
4759
4760 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4761 if (ret < 0)
4762 goto out;
4763
4764 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4765 if (ret < 0)
4766 goto out;
4767
4768 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4769 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4770 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4771
Alexander Blocke938c8a2012-07-28 16:33:49 +02004772 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004773 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004774 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004775 if (ret < 0)
4776 goto out;
4777 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4778 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004779 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004780 }
4781 if (ret < 0)
4782 goto out;
4783
Josef Bacik37b8d272015-06-04 17:17:25 -04004784 /*
4785 * If the parent we're using has a received_uuid set then use that as
4786 * our clone source as that is what we will look for when doing a
4787 * receive.
4788 *
4789 * This covers the case that we create a snapshot off of a received
4790 * subvolume and then use that as the parent and try to receive on a
4791 * different host.
4792 */
4793 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4794 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4795 clone_root->root->root_item.received_uuid);
4796 else
4797 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4798 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004799 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004800 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004801 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4802 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4803 clone_root->offset);
4804
4805 ret = send_cmd(sctx);
4806
4807tlv_put_failure:
4808out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004809 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004810 return ret;
4811}
4812
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004813/*
4814 * Send an update extent command to user space.
4815 */
4816static int send_update_extent(struct send_ctx *sctx,
4817 u64 offset, u32 len)
4818{
4819 int ret = 0;
4820 struct fs_path *p;
4821
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004822 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004823 if (!p)
4824 return -ENOMEM;
4825
4826 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4827 if (ret < 0)
4828 goto out;
4829
4830 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4831 if (ret < 0)
4832 goto out;
4833
4834 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4835 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4836 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4837
4838 ret = send_cmd(sctx);
4839
4840tlv_put_failure:
4841out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004842 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004843 return ret;
4844}
4845
Josef Bacik16e75492013-10-22 12:18:51 -04004846static int send_hole(struct send_ctx *sctx, u64 end)
4847{
4848 struct fs_path *p = NULL;
4849 u64 offset = sctx->cur_inode_last_extent;
4850 u64 len;
4851 int ret = 0;
4852
Filipe Mananab672f4b2018-02-06 20:39:20 +00004853 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
4854 return send_update_extent(sctx, offset, end - offset);
4855
Josef Bacik16e75492013-10-22 12:18:51 -04004856 p = fs_path_alloc();
4857 if (!p)
4858 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01004859 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4860 if (ret < 0)
4861 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04004862 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4863 while (offset < end) {
4864 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4865
4866 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4867 if (ret < 0)
4868 break;
Josef Bacik16e75492013-10-22 12:18:51 -04004869 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4870 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4871 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4872 ret = send_cmd(sctx);
4873 if (ret < 0)
4874 break;
4875 offset += len;
4876 }
4877tlv_put_failure:
4878 fs_path_free(p);
4879 return ret;
4880}
4881
Filipe Mananad906d492015-10-02 10:47:34 +01004882static int send_extent_data(struct send_ctx *sctx,
4883 const u64 offset,
4884 const u64 len)
4885{
4886 u64 sent = 0;
4887
4888 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
4889 return send_update_extent(sctx, offset, len);
4890
4891 while (sent < len) {
4892 u64 size = len - sent;
4893 int ret;
4894
4895 if (size > BTRFS_SEND_READ_SIZE)
4896 size = BTRFS_SEND_READ_SIZE;
4897 ret = send_write(sctx, offset + sent, size);
4898 if (ret < 0)
4899 return ret;
4900 if (!ret)
4901 break;
4902 sent += ret;
4903 }
4904 return 0;
4905}
4906
4907static int clone_range(struct send_ctx *sctx,
4908 struct clone_root *clone_root,
4909 const u64 disk_byte,
4910 u64 data_offset,
4911 u64 offset,
4912 u64 len)
4913{
4914 struct btrfs_path *path;
4915 struct btrfs_key key;
4916 int ret;
4917
4918 path = alloc_path_for_send();
4919 if (!path)
4920 return -ENOMEM;
4921
4922 /*
4923 * We can't send a clone operation for the entire range if we find
4924 * extent items in the respective range in the source file that
4925 * refer to different extents or if we find holes.
4926 * So check for that and do a mix of clone and regular write/copy
4927 * operations if needed.
4928 *
4929 * Example:
4930 *
4931 * mkfs.btrfs -f /dev/sda
4932 * mount /dev/sda /mnt
4933 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
4934 * cp --reflink=always /mnt/foo /mnt/bar
4935 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
4936 * btrfs subvolume snapshot -r /mnt /mnt/snap
4937 *
4938 * If when we send the snapshot and we are processing file bar (which
4939 * has a higher inode number than foo) we blindly send a clone operation
4940 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
4941 * a file bar that matches the content of file foo - iow, doesn't match
4942 * the content from bar in the original filesystem.
4943 */
4944 key.objectid = clone_root->ino;
4945 key.type = BTRFS_EXTENT_DATA_KEY;
4946 key.offset = clone_root->offset;
4947 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
4948 if (ret < 0)
4949 goto out;
4950 if (ret > 0 && path->slots[0] > 0) {
4951 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
4952 if (key.objectid == clone_root->ino &&
4953 key.type == BTRFS_EXTENT_DATA_KEY)
4954 path->slots[0]--;
4955 }
4956
4957 while (true) {
4958 struct extent_buffer *leaf = path->nodes[0];
4959 int slot = path->slots[0];
4960 struct btrfs_file_extent_item *ei;
4961 u8 type;
4962 u64 ext_len;
4963 u64 clone_len;
4964
4965 if (slot >= btrfs_header_nritems(leaf)) {
4966 ret = btrfs_next_leaf(clone_root->root, path);
4967 if (ret < 0)
4968 goto out;
4969 else if (ret > 0)
4970 break;
4971 continue;
4972 }
4973
4974 btrfs_item_key_to_cpu(leaf, &key, slot);
4975
4976 /*
4977 * We might have an implicit trailing hole (NO_HOLES feature
4978 * enabled). We deal with it after leaving this loop.
4979 */
4980 if (key.objectid != clone_root->ino ||
4981 key.type != BTRFS_EXTENT_DATA_KEY)
4982 break;
4983
4984 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4985 type = btrfs_file_extent_type(leaf, ei);
4986 if (type == BTRFS_FILE_EXTENT_INLINE) {
4987 ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004988 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01004989 } else {
4990 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
4991 }
4992
4993 if (key.offset + ext_len <= clone_root->offset)
4994 goto next;
4995
4996 if (key.offset > clone_root->offset) {
4997 /* Implicit hole, NO_HOLES feature enabled. */
4998 u64 hole_len = key.offset - clone_root->offset;
4999
5000 if (hole_len > len)
5001 hole_len = len;
5002 ret = send_extent_data(sctx, offset, hole_len);
5003 if (ret < 0)
5004 goto out;
5005
5006 len -= hole_len;
5007 if (len == 0)
5008 break;
5009 offset += hole_len;
5010 clone_root->offset += hole_len;
5011 data_offset += hole_len;
5012 }
5013
5014 if (key.offset >= clone_root->offset + len)
5015 break;
5016
5017 clone_len = min_t(u64, ext_len, len);
5018
5019 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5020 btrfs_file_extent_offset(leaf, ei) == data_offset)
5021 ret = send_clone(sctx, offset, clone_len, clone_root);
5022 else
5023 ret = send_extent_data(sctx, offset, clone_len);
5024
5025 if (ret < 0)
5026 goto out;
5027
5028 len -= clone_len;
5029 if (len == 0)
5030 break;
5031 offset += clone_len;
5032 clone_root->offset += clone_len;
5033 data_offset += clone_len;
5034next:
5035 path->slots[0]++;
5036 }
5037
5038 if (len > 0)
5039 ret = send_extent_data(sctx, offset, len);
5040 else
5041 ret = 0;
5042out:
5043 btrfs_free_path(path);
5044 return ret;
5045}
5046
Alexander Block31db9f72012-07-25 23:19:24 +02005047static int send_write_or_clone(struct send_ctx *sctx,
5048 struct btrfs_path *path,
5049 struct btrfs_key *key,
5050 struct clone_root *clone_root)
5051{
5052 int ret = 0;
5053 struct btrfs_file_extent_item *ei;
5054 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02005055 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02005056 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005057 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005058
5059 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5060 struct btrfs_file_extent_item);
5061 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005062 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005063 len = btrfs_file_extent_inline_len(path->nodes[0],
5064 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005065 /*
5066 * it is possible the inline item won't cover the whole page,
5067 * but there may be items after this page. Make
5068 * sure to send the whole thing
5069 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005070 len = PAGE_ALIGN(len);
Chris Mason74dd17f2012-08-07 16:25:13 -04005071 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02005072 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005073 }
Alexander Block31db9f72012-07-25 23:19:24 +02005074
5075 if (offset + len > sctx->cur_inode_size)
5076 len = sctx->cur_inode_size - offset;
5077 if (len == 0) {
5078 ret = 0;
5079 goto out;
5080 }
5081
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005082 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01005083 u64 disk_byte;
5084 u64 data_offset;
5085
5086 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5087 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5088 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5089 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005090 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005091 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005092 }
Alexander Block31db9f72012-07-25 23:19:24 +02005093out:
5094 return ret;
5095}
5096
5097static int is_extent_unchanged(struct send_ctx *sctx,
5098 struct btrfs_path *left_path,
5099 struct btrfs_key *ekey)
5100{
5101 int ret = 0;
5102 struct btrfs_key key;
5103 struct btrfs_path *path = NULL;
5104 struct extent_buffer *eb;
5105 int slot;
5106 struct btrfs_key found_key;
5107 struct btrfs_file_extent_item *ei;
5108 u64 left_disknr;
5109 u64 right_disknr;
5110 u64 left_offset;
5111 u64 right_offset;
5112 u64 left_offset_fixed;
5113 u64 left_len;
5114 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005115 u64 left_gen;
5116 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005117 u8 left_type;
5118 u8 right_type;
5119
5120 path = alloc_path_for_send();
5121 if (!path)
5122 return -ENOMEM;
5123
5124 eb = left_path->nodes[0];
5125 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005126 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5127 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005128
5129 if (left_type != BTRFS_FILE_EXTENT_REG) {
5130 ret = 0;
5131 goto out;
5132 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005133 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5134 left_len = btrfs_file_extent_num_bytes(eb, ei);
5135 left_offset = btrfs_file_extent_offset(eb, ei);
5136 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005137
5138 /*
5139 * Following comments will refer to these graphics. L is the left
5140 * extents which we are checking at the moment. 1-8 are the right
5141 * extents that we iterate.
5142 *
5143 * |-----L-----|
5144 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5145 *
5146 * |-----L-----|
5147 * |--1--|-2b-|...(same as above)
5148 *
5149 * Alternative situation. Happens on files where extents got split.
5150 * |-----L-----|
5151 * |-----------7-----------|-6-|
5152 *
5153 * Alternative situation. Happens on files which got larger.
5154 * |-----L-----|
5155 * |-8-|
5156 * Nothing follows after 8.
5157 */
5158
5159 key.objectid = ekey->objectid;
5160 key.type = BTRFS_EXTENT_DATA_KEY;
5161 key.offset = ekey->offset;
5162 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5163 if (ret < 0)
5164 goto out;
5165 if (ret) {
5166 ret = 0;
5167 goto out;
5168 }
5169
5170 /*
5171 * Handle special case where the right side has no extents at all.
5172 */
5173 eb = path->nodes[0];
5174 slot = path->slots[0];
5175 btrfs_item_key_to_cpu(eb, &found_key, slot);
5176 if (found_key.objectid != key.objectid ||
5177 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005178 /* If we're a hole then just pretend nothing changed */
5179 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005180 goto out;
5181 }
5182
5183 /*
5184 * We're now on 2a, 2b or 7.
5185 */
5186 key = found_key;
5187 while (key.offset < ekey->offset + left_len) {
5188 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5189 right_type = btrfs_file_extent_type(eb, ei);
Filipe Manana7016b202017-04-04 20:31:00 +01005190 if (right_type != BTRFS_FILE_EXTENT_REG &&
5191 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005192 ret = 0;
5193 goto out;
5194 }
5195
Filipe Manana7016b202017-04-04 20:31:00 +01005196 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5197 right_len = btrfs_file_extent_inline_len(eb, slot, ei);
5198 right_len = PAGE_ALIGN(right_len);
5199 } else {
5200 right_len = btrfs_file_extent_num_bytes(eb, ei);
5201 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005202
Alexander Block31db9f72012-07-25 23:19:24 +02005203 /*
5204 * Are we at extent 8? If yes, we know the extent is changed.
5205 * This may only happen on the first iteration.
5206 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005207 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005208 /* If we're a hole just pretend nothing changed */
5209 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005210 goto out;
5211 }
5212
Filipe Manana7016b202017-04-04 20:31:00 +01005213 /*
5214 * We just wanted to see if when we have an inline extent, what
5215 * follows it is a regular extent (wanted to check the above
5216 * condition for inline extents too). This should normally not
5217 * happen but it's possible for example when we have an inline
5218 * compressed extent representing data with a size matching
5219 * the page size (currently the same as sector size).
5220 */
5221 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5222 ret = 0;
5223 goto out;
5224 }
5225
Filipe Manana97d65c12017-07-06 15:31:46 +01005226 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5227 right_offset = btrfs_file_extent_offset(eb, ei);
5228 right_gen = btrfs_file_extent_generation(eb, ei);
5229
Alexander Block31db9f72012-07-25 23:19:24 +02005230 left_offset_fixed = left_offset;
5231 if (key.offset < ekey->offset) {
5232 /* Fix the right offset for 2a and 7. */
5233 right_offset += ekey->offset - key.offset;
5234 } else {
5235 /* Fix the left offset for all behind 2a and 2b */
5236 left_offset_fixed += key.offset - ekey->offset;
5237 }
5238
5239 /*
5240 * Check if we have the same extent.
5241 */
Alexander Block39540962012-08-01 12:46:05 +02005242 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005243 left_offset_fixed != right_offset ||
5244 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005245 ret = 0;
5246 goto out;
5247 }
5248
5249 /*
5250 * Go to the next extent.
5251 */
5252 ret = btrfs_next_item(sctx->parent_root, path);
5253 if (ret < 0)
5254 goto out;
5255 if (!ret) {
5256 eb = path->nodes[0];
5257 slot = path->slots[0];
5258 btrfs_item_key_to_cpu(eb, &found_key, slot);
5259 }
5260 if (ret || found_key.objectid != key.objectid ||
5261 found_key.type != key.type) {
5262 key.offset += right_len;
5263 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005264 }
5265 if (found_key.offset != key.offset + right_len) {
5266 ret = 0;
5267 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005268 }
5269 key = found_key;
5270 }
5271
5272 /*
5273 * We're now behind the left extent (treat as unchanged) or at the end
5274 * of the right side (treat as changed).
5275 */
5276 if (key.offset >= ekey->offset + left_len)
5277 ret = 1;
5278 else
5279 ret = 0;
5280
5281
5282out:
5283 btrfs_free_path(path);
5284 return ret;
5285}
5286
Josef Bacik16e75492013-10-22 12:18:51 -04005287static int get_last_extent(struct send_ctx *sctx, u64 offset)
5288{
5289 struct btrfs_path *path;
5290 struct btrfs_root *root = sctx->send_root;
5291 struct btrfs_file_extent_item *fi;
5292 struct btrfs_key key;
5293 u64 extent_end;
5294 u8 type;
5295 int ret;
5296
5297 path = alloc_path_for_send();
5298 if (!path)
5299 return -ENOMEM;
5300
5301 sctx->cur_inode_last_extent = 0;
5302
5303 key.objectid = sctx->cur_ino;
5304 key.type = BTRFS_EXTENT_DATA_KEY;
5305 key.offset = offset;
5306 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5307 if (ret < 0)
5308 goto out;
5309 ret = 0;
5310 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5311 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5312 goto out;
5313
5314 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5315 struct btrfs_file_extent_item);
5316 type = btrfs_file_extent_type(path->nodes[0], fi);
5317 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005318 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5319 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005320 extent_end = ALIGN(key.offset + size,
5321 sctx->send_root->sectorsize);
5322 } else {
5323 extent_end = key.offset +
5324 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5325 }
5326 sctx->cur_inode_last_extent = extent_end;
5327out:
5328 btrfs_free_path(path);
5329 return ret;
5330}
5331
5332static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5333 struct btrfs_key *key)
5334{
5335 struct btrfs_file_extent_item *fi;
5336 u64 extent_end;
5337 u8 type;
5338 int ret = 0;
5339
5340 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5341 return 0;
5342
5343 if (sctx->cur_inode_last_extent == (u64)-1) {
5344 ret = get_last_extent(sctx, key->offset - 1);
5345 if (ret)
5346 return ret;
5347 }
5348
5349 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5350 struct btrfs_file_extent_item);
5351 type = btrfs_file_extent_type(path->nodes[0], fi);
5352 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005353 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5354 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005355 extent_end = ALIGN(key->offset + size,
5356 sctx->send_root->sectorsize);
5357 } else {
5358 extent_end = key->offset +
5359 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5360 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005361
5362 if (path->slots[0] == 0 &&
5363 sctx->cur_inode_last_extent < key->offset) {
5364 /*
5365 * We might have skipped entire leafs that contained only
5366 * file extent items for our current inode. These leafs have
5367 * a generation number smaller (older) than the one in the
5368 * current leaf and the leaf our last extent came from, and
5369 * are located between these 2 leafs.
5370 */
5371 ret = get_last_extent(sctx, key->offset - 1);
5372 if (ret)
5373 return ret;
5374 }
5375
Josef Bacik16e75492013-10-22 12:18:51 -04005376 if (sctx->cur_inode_last_extent < key->offset)
5377 ret = send_hole(sctx, key->offset);
5378 sctx->cur_inode_last_extent = extent_end;
5379 return ret;
5380}
5381
Alexander Block31db9f72012-07-25 23:19:24 +02005382static int process_extent(struct send_ctx *sctx,
5383 struct btrfs_path *path,
5384 struct btrfs_key *key)
5385{
Alexander Block31db9f72012-07-25 23:19:24 +02005386 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005387 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005388
5389 if (S_ISLNK(sctx->cur_inode_mode))
5390 return 0;
5391
5392 if (sctx->parent_root && !sctx->cur_inode_new) {
5393 ret = is_extent_unchanged(sctx, path, key);
5394 if (ret < 0)
5395 goto out;
5396 if (ret) {
5397 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005398 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005399 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005400 } else {
5401 struct btrfs_file_extent_item *ei;
5402 u8 type;
5403
5404 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5405 struct btrfs_file_extent_item);
5406 type = btrfs_file_extent_type(path->nodes[0], ei);
5407 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5408 type == BTRFS_FILE_EXTENT_REG) {
5409 /*
5410 * The send spec does not have a prealloc command yet,
5411 * so just leave a hole for prealloc'ed extents until
5412 * we have enough commands queued up to justify rev'ing
5413 * the send spec.
5414 */
5415 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5416 ret = 0;
5417 goto out;
5418 }
5419
5420 /* Have a hole, just skip it. */
5421 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5422 ret = 0;
5423 goto out;
5424 }
5425 }
Alexander Block31db9f72012-07-25 23:19:24 +02005426 }
5427
5428 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5429 sctx->cur_inode_size, &found_clone);
5430 if (ret != -ENOENT && ret < 0)
5431 goto out;
5432
5433 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005434 if (ret)
5435 goto out;
5436out_hole:
5437 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005438out:
5439 return ret;
5440}
5441
5442static int process_all_extents(struct send_ctx *sctx)
5443{
5444 int ret;
5445 struct btrfs_root *root;
5446 struct btrfs_path *path;
5447 struct btrfs_key key;
5448 struct btrfs_key found_key;
5449 struct extent_buffer *eb;
5450 int slot;
5451
5452 root = sctx->send_root;
5453 path = alloc_path_for_send();
5454 if (!path)
5455 return -ENOMEM;
5456
5457 key.objectid = sctx->cmp_key->objectid;
5458 key.type = BTRFS_EXTENT_DATA_KEY;
5459 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005460 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5461 if (ret < 0)
5462 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005463
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005464 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005465 eb = path->nodes[0];
5466 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005467
5468 if (slot >= btrfs_header_nritems(eb)) {
5469 ret = btrfs_next_leaf(root, path);
5470 if (ret < 0) {
5471 goto out;
5472 } else if (ret > 0) {
5473 ret = 0;
5474 break;
5475 }
5476 continue;
5477 }
5478
Alexander Block31db9f72012-07-25 23:19:24 +02005479 btrfs_item_key_to_cpu(eb, &found_key, slot);
5480
5481 if (found_key.objectid != key.objectid ||
5482 found_key.type != key.type) {
5483 ret = 0;
5484 goto out;
5485 }
5486
5487 ret = process_extent(sctx, path, &found_key);
5488 if (ret < 0)
5489 goto out;
5490
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005491 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005492 }
5493
5494out:
5495 btrfs_free_path(path);
5496 return ret;
5497}
5498
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005499static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5500 int *pending_move,
5501 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005502{
5503 int ret = 0;
5504
5505 if (sctx->cur_ino == 0)
5506 goto out;
5507 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005508 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005509 goto out;
5510 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5511 goto out;
5512
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005513 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005514 if (ret < 0)
5515 goto out;
5516
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005517 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005518out:
5519 return ret;
5520}
5521
5522static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5523{
5524 int ret = 0;
5525 u64 left_mode;
5526 u64 left_uid;
5527 u64 left_gid;
5528 u64 right_mode;
5529 u64 right_uid;
5530 u64 right_gid;
5531 int need_chmod = 0;
5532 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005533 int pending_move = 0;
5534 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005535
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005536 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5537 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005538 if (ret < 0)
5539 goto out;
5540
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005541 /*
5542 * We have processed the refs and thus need to advance send_progress.
5543 * Now, calls to get_cur_xxx will take the updated refs of the current
5544 * inode into account.
5545 *
5546 * On the other hand, if our current inode is a directory and couldn't
5547 * be moved/renamed because its parent was renamed/moved too and it has
5548 * a higher inode number, we can only move/rename our current inode
5549 * after we moved/renamed its parent. Therefore in this case operate on
5550 * the old path (pre move/rename) of our current inode, and the
5551 * move/rename will be performed later.
5552 */
5553 if (refs_processed && !pending_move)
5554 sctx->send_progress = sctx->cur_ino + 1;
5555
Alexander Block31db9f72012-07-25 23:19:24 +02005556 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5557 goto out;
5558 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5559 goto out;
5560
5561 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005562 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005563 if (ret < 0)
5564 goto out;
5565
Alex Lyakase2d044f2012-10-17 13:52:47 +00005566 if (!sctx->parent_root || sctx->cur_inode_new) {
5567 need_chown = 1;
5568 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005569 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005570 } else {
5571 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5572 NULL, NULL, &right_mode, &right_uid,
5573 &right_gid, NULL);
5574 if (ret < 0)
5575 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005576
Alex Lyakase2d044f2012-10-17 13:52:47 +00005577 if (left_uid != right_uid || left_gid != right_gid)
5578 need_chown = 1;
5579 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5580 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005581 }
5582
5583 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005584 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005585 if (sctx->cur_inode_last_extent == (u64)-1 ||
5586 sctx->cur_inode_last_extent <
5587 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005588 ret = get_last_extent(sctx, (u64)-1);
5589 if (ret)
5590 goto out;
5591 }
5592 if (sctx->cur_inode_last_extent <
5593 sctx->cur_inode_size) {
5594 ret = send_hole(sctx, sctx->cur_inode_size);
5595 if (ret)
5596 goto out;
5597 }
5598 }
Alexander Block31db9f72012-07-25 23:19:24 +02005599 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5600 sctx->cur_inode_size);
5601 if (ret < 0)
5602 goto out;
5603 }
5604
5605 if (need_chown) {
5606 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5607 left_uid, left_gid);
5608 if (ret < 0)
5609 goto out;
5610 }
5611 if (need_chmod) {
5612 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5613 left_mode);
5614 if (ret < 0)
5615 goto out;
5616 }
5617
5618 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005619 * If other directory inodes depended on our current directory
5620 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005621 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005622 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5623 ret = apply_children_dir_moves(sctx);
5624 if (ret)
5625 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005626 /*
5627 * Need to send that every time, no matter if it actually
5628 * changed between the two trees as we have done changes to
5629 * the inode before. If our inode is a directory and it's
5630 * waiting to be moved/renamed, we will send its utimes when
5631 * it's moved/renamed, therefore we don't need to do it here.
5632 */
5633 sctx->send_progress = sctx->cur_ino + 1;
5634 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5635 if (ret < 0)
5636 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005637 }
5638
Alexander Block31db9f72012-07-25 23:19:24 +02005639out:
5640 return ret;
5641}
5642
5643static int changed_inode(struct send_ctx *sctx,
5644 enum btrfs_compare_tree_result result)
5645{
5646 int ret = 0;
5647 struct btrfs_key *key = sctx->cmp_key;
5648 struct btrfs_inode_item *left_ii = NULL;
5649 struct btrfs_inode_item *right_ii = NULL;
5650 u64 left_gen = 0;
5651 u64 right_gen = 0;
5652
Alexander Block31db9f72012-07-25 23:19:24 +02005653 sctx->cur_ino = key->objectid;
5654 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005655 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005656
5657 /*
5658 * Set send_progress to current inode. This will tell all get_cur_xxx
5659 * functions that the current inode's refs are not updated yet. Later,
5660 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5661 */
Alexander Block31db9f72012-07-25 23:19:24 +02005662 sctx->send_progress = sctx->cur_ino;
5663
5664 if (result == BTRFS_COMPARE_TREE_NEW ||
5665 result == BTRFS_COMPARE_TREE_CHANGED) {
5666 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5667 sctx->left_path->slots[0],
5668 struct btrfs_inode_item);
5669 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5670 left_ii);
5671 } else {
5672 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5673 sctx->right_path->slots[0],
5674 struct btrfs_inode_item);
5675 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5676 right_ii);
5677 }
5678 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5679 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5680 sctx->right_path->slots[0],
5681 struct btrfs_inode_item);
5682
5683 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5684 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005685
5686 /*
5687 * The cur_ino = root dir case is special here. We can't treat
5688 * the inode as deleted+reused because it would generate a
5689 * stream that tries to delete/mkdir the root dir.
5690 */
5691 if (left_gen != right_gen &&
5692 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005693 sctx->cur_inode_new_gen = 1;
5694 }
5695
5696 if (result == BTRFS_COMPARE_TREE_NEW) {
5697 sctx->cur_inode_gen = left_gen;
5698 sctx->cur_inode_new = 1;
5699 sctx->cur_inode_deleted = 0;
5700 sctx->cur_inode_size = btrfs_inode_size(
5701 sctx->left_path->nodes[0], left_ii);
5702 sctx->cur_inode_mode = btrfs_inode_mode(
5703 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005704 sctx->cur_inode_rdev = btrfs_inode_rdev(
5705 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005706 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005707 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005708 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5709 sctx->cur_inode_gen = right_gen;
5710 sctx->cur_inode_new = 0;
5711 sctx->cur_inode_deleted = 1;
5712 sctx->cur_inode_size = btrfs_inode_size(
5713 sctx->right_path->nodes[0], right_ii);
5714 sctx->cur_inode_mode = btrfs_inode_mode(
5715 sctx->right_path->nodes[0], right_ii);
5716 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005717 /*
5718 * We need to do some special handling in case the inode was
5719 * reported as changed with a changed generation number. This
5720 * means that the original inode was deleted and new inode
5721 * reused the same inum. So we have to treat the old inode as
5722 * deleted and the new one as new.
5723 */
Alexander Block31db9f72012-07-25 23:19:24 +02005724 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005725 /*
5726 * First, process the inode as if it was deleted.
5727 */
Alexander Block31db9f72012-07-25 23:19:24 +02005728 sctx->cur_inode_gen = right_gen;
5729 sctx->cur_inode_new = 0;
5730 sctx->cur_inode_deleted = 1;
5731 sctx->cur_inode_size = btrfs_inode_size(
5732 sctx->right_path->nodes[0], right_ii);
5733 sctx->cur_inode_mode = btrfs_inode_mode(
5734 sctx->right_path->nodes[0], right_ii);
5735 ret = process_all_refs(sctx,
5736 BTRFS_COMPARE_TREE_DELETED);
5737 if (ret < 0)
5738 goto out;
5739
Alexander Block766702e2012-07-28 14:11:31 +02005740 /*
5741 * Now process the inode as if it was new.
5742 */
Alexander Block31db9f72012-07-25 23:19:24 +02005743 sctx->cur_inode_gen = left_gen;
5744 sctx->cur_inode_new = 1;
5745 sctx->cur_inode_deleted = 0;
5746 sctx->cur_inode_size = btrfs_inode_size(
5747 sctx->left_path->nodes[0], left_ii);
5748 sctx->cur_inode_mode = btrfs_inode_mode(
5749 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005750 sctx->cur_inode_rdev = btrfs_inode_rdev(
5751 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005752 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005753 if (ret < 0)
5754 goto out;
5755
5756 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5757 if (ret < 0)
5758 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005759 /*
5760 * Advance send_progress now as we did not get into
5761 * process_recorded_refs_if_needed in the new_gen case.
5762 */
5763 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005764
5765 /*
5766 * Now process all extents and xattrs of the inode as if
5767 * they were all new.
5768 */
Alexander Block31db9f72012-07-25 23:19:24 +02005769 ret = process_all_extents(sctx);
5770 if (ret < 0)
5771 goto out;
5772 ret = process_all_new_xattrs(sctx);
5773 if (ret < 0)
5774 goto out;
5775 } else {
5776 sctx->cur_inode_gen = left_gen;
5777 sctx->cur_inode_new = 0;
5778 sctx->cur_inode_new_gen = 0;
5779 sctx->cur_inode_deleted = 0;
5780 sctx->cur_inode_size = btrfs_inode_size(
5781 sctx->left_path->nodes[0], left_ii);
5782 sctx->cur_inode_mode = btrfs_inode_mode(
5783 sctx->left_path->nodes[0], left_ii);
5784 }
5785 }
5786
5787out:
5788 return ret;
5789}
5790
Alexander Block766702e2012-07-28 14:11:31 +02005791/*
5792 * We have to process new refs before deleted refs, but compare_trees gives us
5793 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5794 * first and later process them in process_recorded_refs.
5795 * For the cur_inode_new_gen case, we skip recording completely because
5796 * changed_inode did already initiate processing of refs. The reason for this is
5797 * that in this case, compare_tree actually compares the refs of 2 different
5798 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5799 * refs of the right tree as deleted and all refs of the left tree as new.
5800 */
Alexander Block31db9f72012-07-25 23:19:24 +02005801static int changed_ref(struct send_ctx *sctx,
5802 enum btrfs_compare_tree_result result)
5803{
5804 int ret = 0;
5805
Filipe Manana95155582016-08-01 01:50:37 +01005806 if (sctx->cur_ino != sctx->cmp_key->objectid) {
5807 inconsistent_snapshot_error(sctx, result, "reference");
5808 return -EIO;
5809 }
Alexander Block31db9f72012-07-25 23:19:24 +02005810
5811 if (!sctx->cur_inode_new_gen &&
5812 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5813 if (result == BTRFS_COMPARE_TREE_NEW)
5814 ret = record_new_ref(sctx);
5815 else if (result == BTRFS_COMPARE_TREE_DELETED)
5816 ret = record_deleted_ref(sctx);
5817 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5818 ret = record_changed_ref(sctx);
5819 }
5820
5821 return ret;
5822}
5823
Alexander Block766702e2012-07-28 14:11:31 +02005824/*
5825 * Process new/deleted/changed xattrs. We skip processing in the
5826 * cur_inode_new_gen case because changed_inode did already initiate processing
5827 * of xattrs. The reason is the same as in changed_ref
5828 */
Alexander Block31db9f72012-07-25 23:19:24 +02005829static int changed_xattr(struct send_ctx *sctx,
5830 enum btrfs_compare_tree_result result)
5831{
5832 int ret = 0;
5833
Filipe Manana95155582016-08-01 01:50:37 +01005834 if (sctx->cur_ino != sctx->cmp_key->objectid) {
5835 inconsistent_snapshot_error(sctx, result, "xattr");
5836 return -EIO;
5837 }
Alexander Block31db9f72012-07-25 23:19:24 +02005838
5839 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5840 if (result == BTRFS_COMPARE_TREE_NEW)
5841 ret = process_new_xattr(sctx);
5842 else if (result == BTRFS_COMPARE_TREE_DELETED)
5843 ret = process_deleted_xattr(sctx);
5844 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5845 ret = process_changed_xattr(sctx);
5846 }
5847
5848 return ret;
5849}
5850
Alexander Block766702e2012-07-28 14:11:31 +02005851/*
5852 * Process new/deleted/changed extents. We skip processing in the
5853 * cur_inode_new_gen case because changed_inode did already initiate processing
5854 * of extents. The reason is the same as in changed_ref
5855 */
Alexander Block31db9f72012-07-25 23:19:24 +02005856static int changed_extent(struct send_ctx *sctx,
5857 enum btrfs_compare_tree_result result)
5858{
5859 int ret = 0;
5860
Filipe Manana09c63dc2019-07-17 13:23:39 +01005861 /*
5862 * We have found an extent item that changed without the inode item
5863 * having changed. This can happen either after relocation (where the
5864 * disk_bytenr of an extent item is replaced at
5865 * relocation.c:replace_file_extents()) or after deduplication into a
5866 * file in both the parent and send snapshots (where an extent item can
5867 * get modified or replaced with a new one). Note that deduplication
5868 * updates the inode item, but it only changes the iversion (sequence
5869 * field in the inode item) of the inode, so if a file is deduplicated
5870 * the same amount of times in both the parent and send snapshots, its
5871 * iversion becames the same in both snapshots, whence the inode item is
5872 * the same on both snapshots.
5873 */
5874 if (sctx->cur_ino != sctx->cmp_key->objectid)
5875 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005876
5877 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5878 if (result != BTRFS_COMPARE_TREE_DELETED)
5879 ret = process_extent(sctx, sctx->left_path,
5880 sctx->cmp_key);
5881 }
5882
5883 return ret;
5884}
5885
Josef Bacikba5e8f22013-08-16 16:52:55 -04005886static int dir_changed(struct send_ctx *sctx, u64 dir)
5887{
5888 u64 orig_gen, new_gen;
5889 int ret;
5890
5891 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5892 NULL, NULL);
5893 if (ret)
5894 return ret;
5895
5896 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5897 NULL, NULL, NULL);
5898 if (ret)
5899 return ret;
5900
5901 return (orig_gen != new_gen) ? 1 : 0;
5902}
5903
5904static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5905 struct btrfs_key *key)
5906{
5907 struct btrfs_inode_extref *extref;
5908 struct extent_buffer *leaf;
5909 u64 dirid = 0, last_dirid = 0;
5910 unsigned long ptr;
5911 u32 item_size;
5912 u32 cur_offset = 0;
5913 int ref_name_len;
5914 int ret = 0;
5915
5916 /* Easy case, just check this one dirid */
5917 if (key->type == BTRFS_INODE_REF_KEY) {
5918 dirid = key->offset;
5919
5920 ret = dir_changed(sctx, dirid);
5921 goto out;
5922 }
5923
5924 leaf = path->nodes[0];
5925 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5926 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5927 while (cur_offset < item_size) {
5928 extref = (struct btrfs_inode_extref *)(ptr +
5929 cur_offset);
5930 dirid = btrfs_inode_extref_parent(leaf, extref);
5931 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5932 cur_offset += ref_name_len + sizeof(*extref);
5933 if (dirid == last_dirid)
5934 continue;
5935 ret = dir_changed(sctx, dirid);
5936 if (ret)
5937 break;
5938 last_dirid = dirid;
5939 }
5940out:
5941 return ret;
5942}
5943
Alexander Block766702e2012-07-28 14:11:31 +02005944/*
5945 * Updates compare related fields in sctx and simply forwards to the actual
5946 * changed_xxx functions.
5947 */
Alexander Block31db9f72012-07-25 23:19:24 +02005948static int changed_cb(struct btrfs_root *left_root,
5949 struct btrfs_root *right_root,
5950 struct btrfs_path *left_path,
5951 struct btrfs_path *right_path,
5952 struct btrfs_key *key,
5953 enum btrfs_compare_tree_result result,
5954 void *ctx)
5955{
5956 int ret = 0;
5957 struct send_ctx *sctx = ctx;
5958
Josef Bacikba5e8f22013-08-16 16:52:55 -04005959 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005960 if (key->type == BTRFS_INODE_REF_KEY ||
5961 key->type == BTRFS_INODE_EXTREF_KEY) {
5962 ret = compare_refs(sctx, left_path, key);
5963 if (!ret)
5964 return 0;
5965 if (ret < 0)
5966 return ret;
5967 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5968 return maybe_send_hole(sctx, left_path, key);
5969 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005970 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005971 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005972 result = BTRFS_COMPARE_TREE_CHANGED;
5973 ret = 0;
5974 }
5975
Alexander Block31db9f72012-07-25 23:19:24 +02005976 sctx->left_path = left_path;
5977 sctx->right_path = right_path;
5978 sctx->cmp_key = key;
5979
5980 ret = finish_inode_if_needed(sctx, 0);
5981 if (ret < 0)
5982 goto out;
5983
Alexander Block2981e222012-08-01 14:47:03 +02005984 /* Ignore non-FS objects */
5985 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5986 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5987 goto out;
5988
Alexander Block31db9f72012-07-25 23:19:24 +02005989 if (key->type == BTRFS_INODE_ITEM_KEY)
5990 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005991 else if (key->type == BTRFS_INODE_REF_KEY ||
5992 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005993 ret = changed_ref(sctx, result);
5994 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5995 ret = changed_xattr(sctx, result);
5996 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5997 ret = changed_extent(sctx, result);
5998
5999out:
6000 return ret;
6001}
6002
6003static int full_send_tree(struct send_ctx *sctx)
6004{
6005 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006006 struct btrfs_root *send_root = sctx->send_root;
6007 struct btrfs_key key;
6008 struct btrfs_key found_key;
6009 struct btrfs_path *path;
6010 struct extent_buffer *eb;
6011 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006012
6013 path = alloc_path_for_send();
6014 if (!path)
6015 return -ENOMEM;
6016
Alexander Block31db9f72012-07-25 23:19:24 +02006017 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6018 key.type = BTRFS_INODE_ITEM_KEY;
6019 key.offset = 0;
6020
Alexander Block31db9f72012-07-25 23:19:24 +02006021 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6022 if (ret < 0)
6023 goto out;
6024 if (ret)
6025 goto out_finish;
6026
6027 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006028 eb = path->nodes[0];
6029 slot = path->slots[0];
6030 btrfs_item_key_to_cpu(eb, &found_key, slot);
6031
6032 ret = changed_cb(send_root, NULL, path, NULL,
6033 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
6034 if (ret < 0)
6035 goto out;
6036
6037 key.objectid = found_key.objectid;
6038 key.type = found_key.type;
6039 key.offset = found_key.offset + 1;
6040
6041 ret = btrfs_next_item(send_root, path);
6042 if (ret < 0)
6043 goto out;
6044 if (ret) {
6045 ret = 0;
6046 break;
6047 }
6048 }
6049
6050out_finish:
6051 ret = finish_inode_if_needed(sctx, 1);
6052
6053out:
6054 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006055 return ret;
6056}
6057
6058static int send_subvol(struct send_ctx *sctx)
6059{
6060 int ret;
6061
Stefan Behrensc2c71322013-04-10 17:10:52 +00006062 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6063 ret = send_header(sctx);
6064 if (ret < 0)
6065 goto out;
6066 }
Alexander Block31db9f72012-07-25 23:19:24 +02006067
6068 ret = send_subvol_begin(sctx);
6069 if (ret < 0)
6070 goto out;
6071
6072 if (sctx->parent_root) {
6073 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6074 changed_cb, sctx);
6075 if (ret < 0)
6076 goto out;
6077 ret = finish_inode_if_needed(sctx, 1);
6078 if (ret < 0)
6079 goto out;
6080 } else {
6081 ret = full_send_tree(sctx);
6082 if (ret < 0)
6083 goto out;
6084 }
6085
6086out:
Alexander Block31db9f72012-07-25 23:19:24 +02006087 free_recorded_refs(sctx);
6088 return ret;
6089}
6090
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006091/*
6092 * If orphan cleanup did remove any orphans from a root, it means the tree
6093 * was modified and therefore the commit root is not the same as the current
6094 * root anymore. This is a problem, because send uses the commit root and
6095 * therefore can see inode items that don't exist in the current root anymore,
6096 * and for example make calls to btrfs_iget, which will do tree lookups based
6097 * on the current root and not on the commit root. Those lookups will fail,
6098 * returning a -ESTALE error, and making send fail with that error. So make
6099 * sure a send does not see any orphans we have just removed, and that it will
6100 * see the same inodes regardless of whether a transaction commit happened
6101 * before it started (meaning that the commit root will be the same as the
6102 * current root) or not.
6103 */
6104static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6105{
6106 int i;
6107 struct btrfs_trans_handle *trans = NULL;
6108
6109again:
6110 if (sctx->parent_root &&
6111 sctx->parent_root->node != sctx->parent_root->commit_root)
6112 goto commit_trans;
6113
6114 for (i = 0; i < sctx->clone_roots_cnt; i++)
6115 if (sctx->clone_roots[i].root->node !=
6116 sctx->clone_roots[i].root->commit_root)
6117 goto commit_trans;
6118
6119 if (trans)
6120 return btrfs_end_transaction(trans, sctx->send_root);
6121
6122 return 0;
6123
6124commit_trans:
6125 /* Use any root, all fs roots will get their commit roots updated. */
6126 if (!trans) {
6127 trans = btrfs_join_transaction(sctx->send_root);
6128 if (IS_ERR(trans))
6129 return PTR_ERR(trans);
6130 goto again;
6131 }
6132
6133 return btrfs_commit_transaction(trans, sctx->send_root);
6134}
6135
David Sterba66ef7d62013-12-17 15:07:20 +01006136static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6137{
6138 spin_lock(&root->root_item_lock);
6139 root->send_in_progress--;
6140 /*
6141 * Not much left to do, we don't know why it's unbalanced and
6142 * can't blindly reset it to 0.
6143 */
6144 if (root->send_in_progress < 0)
6145 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02006146 "send_in_progres unbalanced %d root %llu",
David Sterba66ef7d62013-12-17 15:07:20 +01006147 root->send_in_progress, root->root_key.objectid);
6148 spin_unlock(&root->root_item_lock);
6149}
6150
Alexander Block31db9f72012-07-25 23:19:24 +02006151long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
6152{
6153 int ret = 0;
6154 struct btrfs_root *send_root;
6155 struct btrfs_root *clone_root;
6156 struct btrfs_fs_info *fs_info;
6157 struct btrfs_ioctl_send_args *arg = NULL;
6158 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006159 struct send_ctx *sctx = NULL;
6160 u32 i;
6161 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01006162 int clone_sources_to_rollback = 0;
David Sterbae55d1152016-04-11 18:52:02 +02006163 unsigned alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08006164 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08006165 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02006166
6167 if (!capable(CAP_SYS_ADMIN))
6168 return -EPERM;
6169
Al Viro496ad9a2013-01-23 17:07:38 -05006170 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02006171 fs_info = send_root->fs_info;
6172
Josef Bacik139f8072013-05-20 11:26:50 -04006173 /*
David Sterba2c686532013-12-16 17:34:17 +01006174 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02006175 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01006176 */
6177 spin_lock(&send_root->root_item_lock);
6178 send_root->send_in_progress++;
6179 spin_unlock(&send_root->root_item_lock);
6180
6181 /*
Josef Bacik139f8072013-05-20 11:26:50 -04006182 * This is done when we lookup the root, it should already be complete
6183 * by the time we get here.
6184 */
6185 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6186
6187 /*
David Sterba2c686532013-12-16 17:34:17 +01006188 * Userspace tools do the checks and warn the user if it's
6189 * not RO.
6190 */
6191 if (!btrfs_root_readonly(send_root)) {
6192 ret = -EPERM;
6193 goto out;
6194 }
6195
Alexander Block31db9f72012-07-25 23:19:24 +02006196 arg = memdup_user(arg_, sizeof(*arg));
6197 if (IS_ERR(arg)) {
6198 ret = PTR_ERR(arg);
6199 arg = NULL;
6200 goto out;
6201 }
6202
Dan Carpenter9c1433b2017-03-17 23:51:20 +03006203 /*
6204 * Check that we don't overflow at later allocations, we request
6205 * clone_sources_count + 1 items, and compare to unsigned long inside
6206 * access_ok.
6207 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006208 if (arg->clone_sources_count >
Dan Carpenter9c1433b2017-03-17 23:51:20 +03006209 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006210 ret = -EINVAL;
6211 goto out;
6212 }
6213
Alexander Block31db9f72012-07-25 23:19:24 +02006214 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05006215 sizeof(*arg->clone_sources) *
6216 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02006217 ret = -EFAULT;
6218 goto out;
6219 }
6220
Stefan Behrensc2c71322013-04-10 17:10:52 +00006221 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006222 ret = -EINVAL;
6223 goto out;
6224 }
6225
David Sterbae780b0d2016-01-18 18:42:13 +01006226 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006227 if (!sctx) {
6228 ret = -ENOMEM;
6229 goto out;
6230 }
6231
6232 INIT_LIST_HEAD(&sctx->new_refs);
6233 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01006234 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006235 INIT_LIST_HEAD(&sctx->name_cache_list);
6236
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006237 sctx->flags = arg->flags;
6238
Alexander Block31db9f72012-07-25 23:19:24 +02006239 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00006240 if (!sctx->send_filp) {
6241 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02006242 goto out;
6243 }
6244
Alexander Block31db9f72012-07-25 23:19:24 +02006245 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02006246 /*
6247 * Unlikely but possible, if the subvolume is marked for deletion but
6248 * is slow to remove the directory entry, send can still be started
6249 */
6250 if (btrfs_root_dead(sctx->send_root)) {
6251 ret = -EPERM;
6252 goto out;
6253 }
6254
Alexander Block31db9f72012-07-25 23:19:24 +02006255 sctx->clone_roots_cnt = arg->clone_sources_count;
6256
6257 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
David Sterba6ff48ce2016-04-11 18:40:08 +02006258 sctx->send_buf = kmalloc(sctx->send_max_size, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006259 if (!sctx->send_buf) {
David Sterba6ff48ce2016-04-11 18:40:08 +02006260 sctx->send_buf = vmalloc(sctx->send_max_size);
6261 if (!sctx->send_buf) {
6262 ret = -ENOMEM;
6263 goto out;
6264 }
Alexander Block31db9f72012-07-25 23:19:24 +02006265 }
6266
David Sterbaeb5b75f2016-04-11 18:40:08 +02006267 sctx->read_buf = kmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006268 if (!sctx->read_buf) {
David Sterbaeb5b75f2016-04-11 18:40:08 +02006269 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
6270 if (!sctx->read_buf) {
6271 ret = -ENOMEM;
6272 goto out;
6273 }
Alexander Block31db9f72012-07-25 23:19:24 +02006274 }
6275
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006276 sctx->pending_dir_moves = RB_ROOT;
6277 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00006278 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006279
David Sterbae55d1152016-04-11 18:52:02 +02006280 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6281
David Sterbac03d01f2016-04-11 18:40:08 +02006282 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006283 if (!sctx->clone_roots) {
David Sterbac03d01f2016-04-11 18:40:08 +02006284 sctx->clone_roots = vzalloc(alloc_size);
6285 if (!sctx->clone_roots) {
6286 ret = -ENOMEM;
6287 goto out;
6288 }
Alexander Block31db9f72012-07-25 23:19:24 +02006289 }
6290
David Sterbae55d1152016-04-11 18:52:02 +02006291 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6292
Alexander Block31db9f72012-07-25 23:19:24 +02006293 if (arg->clone_sources_count) {
David Sterba2f913062016-04-11 18:40:08 +02006294 clone_sources_tmp = kmalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +02006295 if (!clone_sources_tmp) {
David Sterba2f913062016-04-11 18:40:08 +02006296 clone_sources_tmp = vmalloc(alloc_size);
6297 if (!clone_sources_tmp) {
6298 ret = -ENOMEM;
6299 goto out;
6300 }
Alexander Block31db9f72012-07-25 23:19:24 +02006301 }
6302
6303 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02006304 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02006305 if (ret) {
6306 ret = -EFAULT;
6307 goto out;
6308 }
6309
6310 for (i = 0; i < arg->clone_sources_count; i++) {
6311 key.objectid = clone_sources_tmp[i];
6312 key.type = BTRFS_ROOT_ITEM_KEY;
6313 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006314
6315 index = srcu_read_lock(&fs_info->subvol_srcu);
6316
Alexander Block31db9f72012-07-25 23:19:24 +02006317 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02006318 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006319 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006320 ret = PTR_ERR(clone_root);
6321 goto out;
6322 }
David Sterba2c686532013-12-16 17:34:17 +01006323 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00006324 if (!btrfs_root_readonly(clone_root) ||
6325 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006326 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006327 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006328 ret = -EPERM;
6329 goto out;
6330 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00006331 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01006332 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006333 srcu_read_unlock(&fs_info->subvol_srcu, index);
6334
Alexander Block31db9f72012-07-25 23:19:24 +02006335 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00006336 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006337 }
David Sterba2f913062016-04-11 18:40:08 +02006338 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006339 clone_sources_tmp = NULL;
6340 }
6341
6342 if (arg->parent_root) {
6343 key.objectid = arg->parent_root;
6344 key.type = BTRFS_ROOT_ITEM_KEY;
6345 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006346
6347 index = srcu_read_lock(&fs_info->subvol_srcu);
6348
Alexander Block31db9f72012-07-25 23:19:24 +02006349 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006350 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006351 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006352 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02006353 goto out;
6354 }
Wang Shilong18f687d2014-01-07 17:25:19 +08006355
David Sterba2c686532013-12-16 17:34:17 +01006356 spin_lock(&sctx->parent_root->root_item_lock);
6357 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02006358 if (!btrfs_root_readonly(sctx->parent_root) ||
6359 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006360 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006361 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006362 ret = -EPERM;
6363 goto out;
6364 }
6365 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006366
6367 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006368 }
6369
6370 /*
6371 * Clones from send_root are allowed, but only if the clone source
6372 * is behind the current send position. This is checked while searching
6373 * for possible clone sources.
6374 */
6375 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6376
6377 /* We do a bsearch later */
6378 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6379 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6380 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08006381 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006382
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006383 ret = ensure_commit_roots_uptodate(sctx);
6384 if (ret)
6385 goto out;
6386
David Sterba2755a0d2014-07-31 00:43:18 +02006387 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02006388 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04006389 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02006390 if (ret < 0)
6391 goto out;
6392
Stefan Behrensc2c71322013-04-10 17:10:52 +00006393 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6394 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6395 if (ret < 0)
6396 goto out;
6397 ret = send_cmd(sctx);
6398 if (ret < 0)
6399 goto out;
6400 }
Alexander Block31db9f72012-07-25 23:19:24 +02006401
6402out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006403 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6404 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6405 struct rb_node *n;
6406 struct pending_dir_move *pm;
6407
6408 n = rb_first(&sctx->pending_dir_moves);
6409 pm = rb_entry(n, struct pending_dir_move, node);
6410 while (!list_empty(&pm->list)) {
6411 struct pending_dir_move *pm2;
6412
6413 pm2 = list_first_entry(&pm->list,
6414 struct pending_dir_move, list);
6415 free_pending_move(sctx, pm2);
6416 }
6417 free_pending_move(sctx, pm);
6418 }
6419
6420 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6421 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6422 struct rb_node *n;
6423 struct waiting_dir_move *dm;
6424
6425 n = rb_first(&sctx->waiting_dir_moves);
6426 dm = rb_entry(n, struct waiting_dir_move, node);
6427 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6428 kfree(dm);
6429 }
6430
Filipe Manana9dc44212014-02-19 14:31:44 +00006431 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6432 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6433 struct rb_node *n;
6434 struct orphan_dir_info *odi;
6435
6436 n = rb_first(&sctx->orphan_dirs);
6437 odi = rb_entry(n, struct orphan_dir_info, node);
6438 free_orphan_dir_info(sctx, odi);
6439 }
6440
Wang Shilong896c14f2014-01-07 17:25:18 +08006441 if (sort_clone_roots) {
6442 for (i = 0; i < sctx->clone_roots_cnt; i++)
6443 btrfs_root_dec_send_in_progress(
6444 sctx->clone_roots[i].root);
6445 } else {
6446 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6447 btrfs_root_dec_send_in_progress(
6448 sctx->clone_roots[i].root);
6449
6450 btrfs_root_dec_send_in_progress(send_root);
6451 }
David Sterba66ef7d62013-12-17 15:07:20 +01006452 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6453 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01006454
Alexander Block31db9f72012-07-25 23:19:24 +02006455 kfree(arg);
David Sterba2f913062016-04-11 18:40:08 +02006456 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006457
6458 if (sctx) {
6459 if (sctx->send_filp)
6460 fput(sctx->send_filp);
6461
David Sterbac03d01f2016-04-11 18:40:08 +02006462 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02006463 kvfree(sctx->send_buf);
David Sterbaeb5b75f2016-04-11 18:40:08 +02006464 kvfree(sctx->read_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02006465
6466 name_cache_free(sctx);
6467
6468 kfree(sctx);
6469 }
6470
6471 return ret;
6472}