blob: 6615d849f0f028639e5fdee466b60ce12a731308 [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>
Josef Bacik2351f432017-09-27 10:43:13 -040029#include <linux/compat.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020030#include <linux/crc32c.h>
Alexander Block31db9f72012-07-25 23:19:24 +020031
32#include "send.h"
33#include "backref.h"
34#include "locking.h"
35#include "disk-io.h"
36#include "btrfs_inode.h"
37#include "transaction.h"
Anand Jainebb87652016-03-10 17:26:59 +080038#include "compression.h"
Alexander Block31db9f72012-07-25 23:19:24 +020039
Alexander Block31db9f72012-07-25 23:19:24 +020040/*
41 * A fs_path is a helper to dynamically build path names with unknown size.
42 * It reallocates the internal buffer on demand.
43 * It allows fast adding of path elements on the right side (normal path) and
44 * fast adding to the left side (reversed path). A reversed path can also be
45 * unreversed if needed.
46 */
47struct fs_path {
48 union {
49 struct {
50 char *start;
51 char *end;
Alexander Block31db9f72012-07-25 23:19:24 +020052
53 char *buf;
David Sterba1f5a7ff2014-02-03 19:23:47 +010054 unsigned short buf_len:15;
55 unsigned short reversed:1;
Alexander Block31db9f72012-07-25 23:19:24 +020056 char inline_buf[];
57 };
David Sterbaace01052014-02-05 16:17:34 +010058 /*
59 * Average path length does not exceed 200 bytes, we'll have
60 * better packing in the slab and higher chance to satisfy
61 * a allocation later during send.
62 */
63 char pad[256];
Alexander Block31db9f72012-07-25 23:19:24 +020064 };
65};
66#define FS_PATH_INLINE_SIZE \
67 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
68
69
70/* reused for each extent */
71struct clone_root {
72 struct btrfs_root *root;
73 u64 ino;
74 u64 offset;
75
76 u64 found_refs;
77};
78
79#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
80#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
81
82struct send_ctx {
83 struct file *send_filp;
84 loff_t send_off;
85 char *send_buf;
86 u32 send_size;
87 u32 send_max_size;
88 u64 total_send_size;
89 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000090 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020091
Alexander Block31db9f72012-07-25 23:19:24 +020092 struct btrfs_root *send_root;
93 struct btrfs_root *parent_root;
94 struct clone_root *clone_roots;
95 int clone_roots_cnt;
96
97 /* current state of the compare_tree call */
98 struct btrfs_path *left_path;
99 struct btrfs_path *right_path;
100 struct btrfs_key *cmp_key;
101
102 /*
103 * infos of the currently processed inode. In case of deleted inodes,
104 * these are the values from the deleted inode.
105 */
106 u64 cur_ino;
107 u64 cur_inode_gen;
108 int cur_inode_new;
109 int cur_inode_new_gen;
110 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200111 u64 cur_inode_size;
112 u64 cur_inode_mode;
Liu Bo644d1942014-02-27 17:29:01 +0800113 u64 cur_inode_rdev;
Josef Bacik16e75492013-10-22 12:18:51 -0400114 u64 cur_inode_last_extent;
Filipe Mananaffa7c422018-02-06 20:40:40 +0000115 u64 cur_inode_next_write_offset;
Alexander Block31db9f72012-07-25 23:19:24 +0200116
117 u64 send_progress;
118
119 struct list_head new_refs;
120 struct list_head deleted_refs;
121
122 struct radix_tree_root name_cache;
123 struct list_head name_cache_list;
124 int name_cache_size;
125
Liu Bo2131bcd2014-03-05 10:07:35 +0800126 struct file_ra_state ra;
127
Alexander Block31db9f72012-07-25 23:19:24 +0200128 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000129
130 /*
131 * We process inodes by their increasing order, so if before an
132 * incremental send we reverse the parent/child relationship of
133 * directories such that a directory with a lower inode number was
134 * the parent of a directory with a higher inode number, and the one
135 * becoming the new parent got renamed too, we can't rename/move the
136 * directory with lower inode number when we finish processing it - we
137 * must process the directory with higher inode number first, then
138 * rename/move it and then rename/move the directory with lower inode
139 * number. Example follows.
140 *
141 * Tree state when the first send was performed:
142 *
143 * .
144 * |-- a (ino 257)
145 * |-- b (ino 258)
146 * |
147 * |
148 * |-- c (ino 259)
149 * | |-- d (ino 260)
150 * |
151 * |-- c2 (ino 261)
152 *
153 * Tree state when the second (incremental) send is performed:
154 *
155 * .
156 * |-- a (ino 257)
157 * |-- b (ino 258)
158 * |-- c2 (ino 261)
159 * |-- d2 (ino 260)
160 * |-- cc (ino 259)
161 *
162 * The sequence of steps that lead to the second state was:
163 *
164 * mv /a/b/c/d /a/b/c2/d2
165 * mv /a/b/c /a/b/c2/d2/cc
166 *
167 * "c" has lower inode number, but we can't move it (2nd mv operation)
168 * before we move "d", which has higher inode number.
169 *
170 * So we just memorize which move/rename operations must be performed
171 * later when their respective parent is processed and moved/renamed.
172 */
173
174 /* Indexed by parent directory inode number. */
175 struct rb_root pending_dir_moves;
176
177 /*
178 * Reverse index, indexed by the inode number of a directory that
179 * is waiting for the move/rename of its immediate parent before its
180 * own move/rename can be performed.
181 */
182 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000183
184 /*
185 * A directory that is going to be rm'ed might have a child directory
186 * which is in the pending directory moves index above. In this case,
187 * the directory can only be removed after the move/rename of its child
188 * is performed. Example:
189 *
190 * Parent snapshot:
191 *
192 * . (ino 256)
193 * |-- a/ (ino 257)
194 * |-- b/ (ino 258)
195 * |-- c/ (ino 259)
196 * | |-- x/ (ino 260)
197 * |
198 * |-- y/ (ino 261)
199 *
200 * Send snapshot:
201 *
202 * . (ino 256)
203 * |-- a/ (ino 257)
204 * |-- b/ (ino 258)
205 * |-- YY/ (ino 261)
206 * |-- x/ (ino 260)
207 *
208 * Sequence of steps that lead to the send snapshot:
209 * rm -f /a/b/c/foo.txt
210 * mv /a/b/y /a/b/YY
211 * mv /a/b/c/x /a/b/YY
212 * rmdir /a/b/c
213 *
214 * When the child is processed, its move/rename is delayed until its
215 * parent is processed (as explained above), but all other operations
216 * like update utimes, chown, chgrp, etc, are performed and the paths
217 * that it uses for those operations must use the orphanized name of
218 * its parent (the directory we're going to rm later), so we need to
219 * memorize that name.
220 *
221 * Indexed by the inode number of the directory to be deleted.
222 */
223 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000224};
225
226struct pending_dir_move {
227 struct rb_node node;
228 struct list_head list;
229 u64 parent_ino;
230 u64 ino;
231 u64 gen;
232 struct list_head update_refs;
233};
234
235struct waiting_dir_move {
236 struct rb_node node;
237 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000238 /*
239 * There might be some directory that could not be removed because it
240 * was waiting for this directory inode to be moved first. Therefore
241 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
242 */
243 u64 rmdir_ino;
Filipe Manana8b191a62015-04-09 14:09:14 +0100244 bool orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +0000245};
246
247struct orphan_dir_info {
248 struct rb_node node;
249 u64 ino;
250 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +0200251};
252
253struct name_cache_entry {
254 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200255 /*
256 * radix_tree has only 32bit entries but we need to handle 64bit inums.
257 * We use the lower 32bit of the 64bit inum to store it in the tree. If
258 * more then one inum would fall into the same entry, we use radix_list
259 * to store the additional entries. radix_list is also used to store
260 * entries where two entries have the same inum but different
261 * generations.
262 */
263 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200264 u64 ino;
265 u64 gen;
266 u64 parent_ino;
267 u64 parent_gen;
268 int ret;
269 int need_later_update;
270 int name_len;
271 char name[];
272};
273
David Sterbae67c7182018-02-19 17:24:18 +0100274__cold
Filipe Manana95155582016-08-01 01:50:37 +0100275static void inconsistent_snapshot_error(struct send_ctx *sctx,
276 enum btrfs_compare_tree_result result,
277 const char *what)
278{
279 const char *result_string;
280
281 switch (result) {
282 case BTRFS_COMPARE_TREE_NEW:
283 result_string = "new";
284 break;
285 case BTRFS_COMPARE_TREE_DELETED:
286 result_string = "deleted";
287 break;
288 case BTRFS_COMPARE_TREE_CHANGED:
289 result_string = "updated";
290 break;
291 case BTRFS_COMPARE_TREE_SAME:
292 ASSERT(0);
293 result_string = "unchanged";
294 break;
295 default:
296 ASSERT(0);
297 result_string = "unexpected";
298 }
299
300 btrfs_err(sctx->send_root->fs_info,
301 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
302 result_string, what, sctx->cmp_key->objectid,
303 sctx->send_root->root_key.objectid,
304 (sctx->parent_root ?
305 sctx->parent_root->root_key.objectid : 0));
306}
307
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000308static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
309
Filipe Manana9dc44212014-02-19 14:31:44 +0000310static struct waiting_dir_move *
311get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
312
313static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
314
Josef Bacik16e75492013-10-22 12:18:51 -0400315static int need_send_hole(struct send_ctx *sctx)
316{
317 return (sctx->parent_root && !sctx->cur_inode_new &&
318 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
319 S_ISREG(sctx->cur_inode_mode));
320}
321
Alexander Block31db9f72012-07-25 23:19:24 +0200322static void fs_path_reset(struct fs_path *p)
323{
324 if (p->reversed) {
325 p->start = p->buf + p->buf_len - 1;
326 p->end = p->start;
327 *p->start = 0;
328 } else {
329 p->start = p->buf;
330 p->end = p->start;
331 *p->start = 0;
332 }
333}
334
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000335static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200336{
337 struct fs_path *p;
338
David Sterbae780b0d2016-01-18 18:42:13 +0100339 p = kmalloc(sizeof(*p), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +0200340 if (!p)
341 return NULL;
342 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200343 p->buf = p->inline_buf;
344 p->buf_len = FS_PATH_INLINE_SIZE;
345 fs_path_reset(p);
346 return p;
347}
348
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000349static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200350{
351 struct fs_path *p;
352
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000353 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200354 if (!p)
355 return NULL;
356 p->reversed = 1;
357 fs_path_reset(p);
358 return p;
359}
360
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000361static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200362{
363 if (!p)
364 return;
David Sterbaace01052014-02-05 16:17:34 +0100365 if (p->buf != p->inline_buf)
366 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200367 kfree(p);
368}
369
370static int fs_path_len(struct fs_path *p)
371{
372 return p->end - p->start;
373}
374
375static int fs_path_ensure_buf(struct fs_path *p, int len)
376{
377 char *tmp_buf;
378 int path_len;
379 int old_buf_len;
380
381 len++;
382
383 if (p->buf_len >= len)
384 return 0;
385
Chris Masoncfd4a532014-04-26 05:02:03 -0700386 if (len > PATH_MAX) {
387 WARN_ON(1);
388 return -ENOMEM;
389 }
390
David Sterba1b2782c2014-02-25 19:32:59 +0100391 path_len = p->end - p->start;
392 old_buf_len = p->buf_len;
393
David Sterbaace01052014-02-05 16:17:34 +0100394 /*
395 * First time the inline_buf does not suffice
396 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100397 if (p->buf == p->inline_buf) {
David Sterbae780b0d2016-01-18 18:42:13 +0100398 tmp_buf = kmalloc(len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100399 if (tmp_buf)
400 memcpy(tmp_buf, p->buf, old_buf_len);
401 } else {
David Sterbae780b0d2016-01-18 18:42:13 +0100402 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100403 }
David Sterba9c9ca002014-02-25 19:33:08 +0100404 if (!tmp_buf)
405 return -ENOMEM;
406 p->buf = tmp_buf;
407 /*
408 * The real size of the buffer is bigger, this will let the fast path
409 * happen most of the time
410 */
411 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100412
Alexander Block31db9f72012-07-25 23:19:24 +0200413 if (p->reversed) {
414 tmp_buf = p->buf + old_buf_len - path_len - 1;
415 p->end = p->buf + p->buf_len - 1;
416 p->start = p->end - path_len;
417 memmove(p->start, tmp_buf, path_len + 1);
418 } else {
419 p->start = p->buf;
420 p->end = p->start + path_len;
421 }
422 return 0;
423}
424
David Sterbab23ab572014-02-03 19:23:19 +0100425static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
426 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200427{
428 int ret;
429 int new_len;
430
431 new_len = p->end - p->start + name_len;
432 if (p->start != p->end)
433 new_len++;
434 ret = fs_path_ensure_buf(p, new_len);
435 if (ret < 0)
436 goto out;
437
438 if (p->reversed) {
439 if (p->start != p->end)
440 *--p->start = '/';
441 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100442 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200443 } else {
444 if (p->start != p->end)
445 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100446 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200447 p->end += name_len;
448 *p->end = 0;
449 }
450
451out:
452 return ret;
453}
454
455static int fs_path_add(struct fs_path *p, const char *name, int name_len)
456{
457 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100458 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200459
David Sterbab23ab572014-02-03 19:23:19 +0100460 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200461 if (ret < 0)
462 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100463 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200464
465out:
466 return ret;
467}
468
469static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
470{
471 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100472 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200473
David Sterbab23ab572014-02-03 19:23:19 +0100474 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200475 if (ret < 0)
476 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100477 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200478
479out:
480 return ret;
481}
482
483static int fs_path_add_from_extent_buffer(struct fs_path *p,
484 struct extent_buffer *eb,
485 unsigned long off, int len)
486{
487 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100488 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200489
David Sterbab23ab572014-02-03 19:23:19 +0100490 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200491 if (ret < 0)
492 goto out;
493
David Sterbab23ab572014-02-03 19:23:19 +0100494 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200495
496out:
497 return ret;
498}
499
Alexander Block31db9f72012-07-25 23:19:24 +0200500static int fs_path_copy(struct fs_path *p, struct fs_path *from)
501{
502 int ret;
503
504 p->reversed = from->reversed;
505 fs_path_reset(p);
506
507 ret = fs_path_add_path(p, from);
508
509 return ret;
510}
511
512
513static void fs_path_unreverse(struct fs_path *p)
514{
515 char *tmp;
516 int len;
517
518 if (!p->reversed)
519 return;
520
521 tmp = p->start;
522 len = p->end - p->start;
523 p->start = p->buf;
524 p->end = p->start + len;
525 memmove(p->start, tmp, len + 1);
526 p->reversed = 0;
527}
528
529static struct btrfs_path *alloc_path_for_send(void)
530{
531 struct btrfs_path *path;
532
533 path = btrfs_alloc_path();
534 if (!path)
535 return NULL;
536 path->search_commit_root = 1;
537 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400538 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200539 return path;
540}
541
Eric Sandeen48a3b632013-04-25 20:41:01 +0000542static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200543{
544 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200545 u32 pos = 0;
546
Alexander Block31db9f72012-07-25 23:19:24 +0200547 while (pos < len) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200548 ret = kernel_write(filp, buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200549 /* TODO handle that correctly */
550 /*if (ret == -ERESTARTSYS) {
551 continue;
552 }*/
553 if (ret < 0)
Christoph Hellwig8e931572017-09-01 17:39:19 +0200554 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200555 if (ret == 0) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200556 return -EIO;
Alexander Block31db9f72012-07-25 23:19:24 +0200557 }
558 pos += ret;
559 }
560
Christoph Hellwig8e931572017-09-01 17:39:19 +0200561 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200562}
563
564static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
565{
566 struct btrfs_tlv_header *hdr;
567 int total_len = sizeof(*hdr) + len;
568 int left = sctx->send_max_size - sctx->send_size;
569
570 if (unlikely(left < total_len))
571 return -EOVERFLOW;
572
573 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
574 hdr->tlv_type = cpu_to_le16(attr);
575 hdr->tlv_len = cpu_to_le16(len);
576 memcpy(hdr + 1, data, len);
577 sctx->send_size += total_len;
578
579 return 0;
580}
581
David Sterba95bc79d2013-12-16 17:34:10 +0100582#define TLV_PUT_DEFINE_INT(bits) \
583 static int tlv_put_u##bits(struct send_ctx *sctx, \
584 u##bits attr, u##bits value) \
585 { \
586 __le##bits __tmp = cpu_to_le##bits(value); \
587 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
588 }
Alexander Block31db9f72012-07-25 23:19:24 +0200589
David Sterba95bc79d2013-12-16 17:34:10 +0100590TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200591
592static int tlv_put_string(struct send_ctx *sctx, u16 attr,
593 const char *str, int len)
594{
595 if (len == -1)
596 len = strlen(str);
597 return tlv_put(sctx, attr, str, len);
598}
599
600static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
601 const u8 *uuid)
602{
603 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
604}
605
Alexander Block31db9f72012-07-25 23:19:24 +0200606static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
607 struct extent_buffer *eb,
608 struct btrfs_timespec *ts)
609{
610 struct btrfs_timespec bts;
611 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
612 return tlv_put(sctx, attr, &bts, sizeof(bts));
613}
614
615
616#define TLV_PUT(sctx, attrtype, attrlen, data) \
617 do { \
618 ret = tlv_put(sctx, attrtype, attrlen, data); \
619 if (ret < 0) \
620 goto tlv_put_failure; \
621 } while (0)
622
623#define TLV_PUT_INT(sctx, attrtype, bits, value) \
624 do { \
625 ret = tlv_put_u##bits(sctx, attrtype, value); \
626 if (ret < 0) \
627 goto tlv_put_failure; \
628 } while (0)
629
630#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
631#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
632#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
633#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
634#define TLV_PUT_STRING(sctx, attrtype, str, len) \
635 do { \
636 ret = tlv_put_string(sctx, attrtype, str, len); \
637 if (ret < 0) \
638 goto tlv_put_failure; \
639 } while (0)
640#define TLV_PUT_PATH(sctx, attrtype, p) \
641 do { \
642 ret = tlv_put_string(sctx, attrtype, p->start, \
643 p->end - p->start); \
644 if (ret < 0) \
645 goto tlv_put_failure; \
646 } while(0)
647#define TLV_PUT_UUID(sctx, attrtype, uuid) \
648 do { \
649 ret = tlv_put_uuid(sctx, attrtype, uuid); \
650 if (ret < 0) \
651 goto tlv_put_failure; \
652 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200653#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
654 do { \
655 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
656 if (ret < 0) \
657 goto tlv_put_failure; \
658 } while (0)
659
660static int send_header(struct send_ctx *sctx)
661{
662 struct btrfs_stream_header hdr;
663
664 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
665 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
666
Anand Jain1bcea352012-09-14 00:04:21 -0600667 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
668 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200669}
670
671/*
672 * For each command/item we want to send to userspace, we call this function.
673 */
674static int begin_cmd(struct send_ctx *sctx, int cmd)
675{
676 struct btrfs_cmd_header *hdr;
677
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530678 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200679 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200680
681 BUG_ON(sctx->send_size);
682
683 sctx->send_size += sizeof(*hdr);
684 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
685 hdr->cmd = cpu_to_le16(cmd);
686
687 return 0;
688}
689
690static int send_cmd(struct send_ctx *sctx)
691{
692 int ret;
693 struct btrfs_cmd_header *hdr;
694 u32 crc;
695
696 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
697 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
698 hdr->crc = 0;
699
Nikolay Borisov9678c542018-01-08 11:45:05 +0200700 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200701 hdr->crc = cpu_to_le32(crc);
702
Anand Jain1bcea352012-09-14 00:04:21 -0600703 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
704 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200705
706 sctx->total_send_size += sctx->send_size;
707 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
708 sctx->send_size = 0;
709
710 return ret;
711}
712
713/*
714 * Sends a move instruction to user space
715 */
716static int send_rename(struct send_ctx *sctx,
717 struct fs_path *from, struct fs_path *to)
718{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400719 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200720 int ret;
721
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400722 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200723
724 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
725 if (ret < 0)
726 goto out;
727
728 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
729 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
730
731 ret = send_cmd(sctx);
732
733tlv_put_failure:
734out:
735 return ret;
736}
737
738/*
739 * Sends a link instruction to user space
740 */
741static int send_link(struct send_ctx *sctx,
742 struct fs_path *path, struct fs_path *lnk)
743{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400744 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200745 int ret;
746
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400747 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200748
749 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
750 if (ret < 0)
751 goto out;
752
753 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
754 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
755
756 ret = send_cmd(sctx);
757
758tlv_put_failure:
759out:
760 return ret;
761}
762
763/*
764 * Sends an unlink instruction to user space
765 */
766static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
767{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400768 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200769 int ret;
770
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400771 btrfs_debug(fs_info, "send_unlink %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200772
773 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
774 if (ret < 0)
775 goto out;
776
777 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
778
779 ret = send_cmd(sctx);
780
781tlv_put_failure:
782out:
783 return ret;
784}
785
786/*
787 * Sends a rmdir instruction to user space
788 */
789static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
790{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400791 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200792 int ret;
793
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400794 btrfs_debug(fs_info, "send_rmdir %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200795
796 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
797 if (ret < 0)
798 goto out;
799
800 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
801
802 ret = send_cmd(sctx);
803
804tlv_put_failure:
805out:
806 return ret;
807}
808
809/*
810 * Helper function to retrieve some fields from an inode item.
811 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400812static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
813 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
814 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200815{
816 int ret;
817 struct btrfs_inode_item *ii;
818 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200819
820 key.objectid = ino;
821 key.type = BTRFS_INODE_ITEM_KEY;
822 key.offset = 0;
823 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200824 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400825 if (ret > 0)
826 ret = -ENOENT;
827 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200828 }
829
830 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
831 struct btrfs_inode_item);
832 if (size)
833 *size = btrfs_inode_size(path->nodes[0], ii);
834 if (gen)
835 *gen = btrfs_inode_generation(path->nodes[0], ii);
836 if (mode)
837 *mode = btrfs_inode_mode(path->nodes[0], ii);
838 if (uid)
839 *uid = btrfs_inode_uid(path->nodes[0], ii);
840 if (gid)
841 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200842 if (rdev)
843 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200844
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400845 return ret;
846}
847
848static int get_inode_info(struct btrfs_root *root,
849 u64 ino, u64 *size, u64 *gen,
850 u64 *mode, u64 *uid, u64 *gid,
851 u64 *rdev)
852{
853 struct btrfs_path *path;
854 int ret;
855
856 path = alloc_path_for_send();
857 if (!path)
858 return -ENOMEM;
859 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
860 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200861 btrfs_free_path(path);
862 return ret;
863}
864
865typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
866 struct fs_path *p,
867 void *ctx);
868
869/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000870 * Helper function to iterate the entries in ONE btrfs_inode_ref or
871 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200872 * The iterate callback may return a non zero value to stop iteration. This can
873 * be a negative value for error codes or 1 to simply stop it.
874 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000875 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200876 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000877static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200878 struct btrfs_key *found_key, int resolve,
879 iterate_inode_ref_t iterate, void *ctx)
880{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000881 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200882 struct btrfs_item *item;
883 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000884 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200885 struct btrfs_path *tmp_path;
886 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000887 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200888 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000889 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200890 u32 name_len;
891 char *start;
892 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000893 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200894 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000895 u64 dir;
896 unsigned long name_off;
897 unsigned long elem_size;
898 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200899
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000900 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200901 if (!p)
902 return -ENOMEM;
903
904 tmp_path = alloc_path_for_send();
905 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000906 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200907 return -ENOMEM;
908 }
909
Alexander Block31db9f72012-07-25 23:19:24 +0200910
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000911 if (found_key->type == BTRFS_INODE_REF_KEY) {
912 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
913 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100914 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000915 total = btrfs_item_size(eb, item);
916 elem_size = sizeof(*iref);
917 } else {
918 ptr = btrfs_item_ptr_offset(eb, slot);
919 total = btrfs_item_size_nr(eb, slot);
920 elem_size = sizeof(*extref);
921 }
922
Alexander Block31db9f72012-07-25 23:19:24 +0200923 while (cur < total) {
924 fs_path_reset(p);
925
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000926 if (found_key->type == BTRFS_INODE_REF_KEY) {
927 iref = (struct btrfs_inode_ref *)(ptr + cur);
928 name_len = btrfs_inode_ref_name_len(eb, iref);
929 name_off = (unsigned long)(iref + 1);
930 index = btrfs_inode_ref_index(eb, iref);
931 dir = found_key->offset;
932 } else {
933 extref = (struct btrfs_inode_extref *)(ptr + cur);
934 name_len = btrfs_inode_extref_name_len(eb, extref);
935 name_off = (unsigned long)&extref->name;
936 index = btrfs_inode_extref_index(eb, extref);
937 dir = btrfs_inode_extref_parent(eb, extref);
938 }
939
Alexander Block31db9f72012-07-25 23:19:24 +0200940 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000941 start = btrfs_ref_to_path(root, tmp_path, name_len,
942 name_off, eb, dir,
943 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200944 if (IS_ERR(start)) {
945 ret = PTR_ERR(start);
946 goto out;
947 }
948 if (start < p->buf) {
949 /* overflow , try again with larger buffer */
950 ret = fs_path_ensure_buf(p,
951 p->buf_len + p->buf - start);
952 if (ret < 0)
953 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000954 start = btrfs_ref_to_path(root, tmp_path,
955 name_len, name_off,
956 eb, dir,
957 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200958 if (IS_ERR(start)) {
959 ret = PTR_ERR(start);
960 goto out;
961 }
962 BUG_ON(start < p->buf);
963 }
964 p->start = start;
965 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000966 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
967 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200968 if (ret < 0)
969 goto out;
970 }
971
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000972 cur += elem_size + name_len;
973 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200974 if (ret)
975 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200976 num++;
977 }
978
979out:
980 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000981 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200982 return ret;
983}
984
985typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
986 const char *name, int name_len,
987 const char *data, int data_len,
988 u8 type, void *ctx);
989
990/*
991 * Helper function to iterate the entries in ONE btrfs_dir_item.
992 * The iterate callback may return a non zero value to stop iteration. This can
993 * be a negative value for error codes or 1 to simply stop it.
994 *
995 * path must point to the dir item when called.
996 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000997static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200998 iterate_dir_item_t iterate, void *ctx)
999{
1000 int ret = 0;
1001 struct extent_buffer *eb;
1002 struct btrfs_item *item;
1003 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +02001004 struct btrfs_key di_key;
1005 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +01001006 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +02001007 u32 name_len;
1008 u32 data_len;
1009 u32 cur;
1010 u32 len;
1011 u32 total;
1012 int slot;
1013 int num;
1014 u8 type;
1015
Filipe Manana4395e0c2014-08-20 10:45:45 +01001016 /*
1017 * Start with a small buffer (1 page). If later we end up needing more
1018 * space, which can happen for xattrs on a fs with a leaf size greater
1019 * then the page size, attempt to increase the buffer. Typically xattr
1020 * values are small.
1021 */
1022 buf_len = PATH_MAX;
David Sterbae780b0d2016-01-18 18:42:13 +01001023 buf = kmalloc(buf_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02001024 if (!buf) {
1025 ret = -ENOMEM;
1026 goto out;
1027 }
1028
Alexander Block31db9f72012-07-25 23:19:24 +02001029 eb = path->nodes[0];
1030 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001031 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001032 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1033 cur = 0;
1034 len = 0;
1035 total = btrfs_item_size(eb, item);
1036
1037 num = 0;
1038 while (cur < total) {
1039 name_len = btrfs_dir_name_len(eb, di);
1040 data_len = btrfs_dir_data_len(eb, di);
1041 type = btrfs_dir_type(eb, di);
1042 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1043
Filipe Manana7e3ae332014-05-23 20:15:16 +01001044 if (type == BTRFS_FT_XATTR) {
1045 if (name_len > XATTR_NAME_MAX) {
1046 ret = -ENAMETOOLONG;
1047 goto out;
1048 }
Jeff Mahoneyda170662016-06-15 09:22:56 -04001049 if (name_len + data_len >
1050 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001051 ret = -E2BIG;
1052 goto out;
1053 }
1054 } else {
1055 /*
1056 * Path too long
1057 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001058 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001059 ret = -ENAMETOOLONG;
1060 goto out;
1061 }
Alexander Block31db9f72012-07-25 23:19:24 +02001062 }
1063
Filipe Manana4395e0c2014-08-20 10:45:45 +01001064 if (name_len + data_len > buf_len) {
1065 buf_len = name_len + data_len;
1066 if (is_vmalloc_addr(buf)) {
1067 vfree(buf);
1068 buf = NULL;
1069 } else {
1070 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001071 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001072
1073 if (!tmp)
1074 kfree(buf);
1075 buf = tmp;
1076 }
1077 if (!buf) {
David Sterbaf11f7442017-05-31 18:40:02 +02001078 buf = kvmalloc(buf_len, GFP_KERNEL);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001079 if (!buf) {
1080 ret = -ENOMEM;
1081 goto out;
1082 }
1083 }
1084 }
1085
Alexander Block31db9f72012-07-25 23:19:24 +02001086 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1087 name_len + data_len);
1088
1089 len = sizeof(*di) + name_len + data_len;
1090 di = (struct btrfs_dir_item *)((char *)di + len);
1091 cur += len;
1092
1093 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1094 data_len, type, ctx);
1095 if (ret < 0)
1096 goto out;
1097 if (ret) {
1098 ret = 0;
1099 goto out;
1100 }
1101
1102 num++;
1103 }
1104
1105out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001106 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001107 return ret;
1108}
1109
1110static int __copy_first_ref(int num, u64 dir, int index,
1111 struct fs_path *p, void *ctx)
1112{
1113 int ret;
1114 struct fs_path *pt = ctx;
1115
1116 ret = fs_path_copy(pt, p);
1117 if (ret < 0)
1118 return ret;
1119
1120 /* we want the first only */
1121 return 1;
1122}
1123
1124/*
1125 * Retrieve the first path of an inode. If an inode has more then one
1126 * ref/hardlink, this is ignored.
1127 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001128static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001129 u64 ino, struct fs_path *path)
1130{
1131 int ret;
1132 struct btrfs_key key, found_key;
1133 struct btrfs_path *p;
1134
1135 p = alloc_path_for_send();
1136 if (!p)
1137 return -ENOMEM;
1138
1139 fs_path_reset(path);
1140
1141 key.objectid = ino;
1142 key.type = BTRFS_INODE_REF_KEY;
1143 key.offset = 0;
1144
1145 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1146 if (ret < 0)
1147 goto out;
1148 if (ret) {
1149 ret = 1;
1150 goto out;
1151 }
1152 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1153 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001154 (found_key.type != BTRFS_INODE_REF_KEY &&
1155 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001156 ret = -ENOENT;
1157 goto out;
1158 }
1159
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001160 ret = iterate_inode_ref(root, p, &found_key, 1,
1161 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001162 if (ret < 0)
1163 goto out;
1164 ret = 0;
1165
1166out:
1167 btrfs_free_path(p);
1168 return ret;
1169}
1170
1171struct backref_ctx {
1172 struct send_ctx *sctx;
1173
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001174 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001175 /* number of total found references */
1176 u64 found;
1177
1178 /*
1179 * used for clones found in send_root. clones found behind cur_objectid
1180 * and cur_offset are not considered as allowed clones.
1181 */
1182 u64 cur_objectid;
1183 u64 cur_offset;
1184
1185 /* may be truncated in case it's the last extent in a file */
1186 u64 extent_len;
1187
Filipe Manana619d8c42015-05-03 01:56:00 +01001188 /* data offset in the file extent item */
1189 u64 data_offset;
1190
Alexander Block31db9f72012-07-25 23:19:24 +02001191 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001192 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001193};
1194
1195static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1196{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001197 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001198 struct clone_root *cr = (struct clone_root *)elt;
1199
1200 if (root < cr->root->objectid)
1201 return -1;
1202 if (root > cr->root->objectid)
1203 return 1;
1204 return 0;
1205}
1206
1207static int __clone_root_cmp_sort(const void *e1, const void *e2)
1208{
1209 struct clone_root *cr1 = (struct clone_root *)e1;
1210 struct clone_root *cr2 = (struct clone_root *)e2;
1211
1212 if (cr1->root->objectid < cr2->root->objectid)
1213 return -1;
1214 if (cr1->root->objectid > cr2->root->objectid)
1215 return 1;
1216 return 0;
1217}
1218
1219/*
1220 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001221 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001222 */
1223static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1224{
1225 struct backref_ctx *bctx = ctx_;
1226 struct clone_root *found;
1227 int ret;
1228 u64 i_size;
1229
1230 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001231 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001232 bctx->sctx->clone_roots_cnt,
1233 sizeof(struct clone_root),
1234 __clone_root_cmp_bsearch);
1235 if (!found)
1236 return 0;
1237
1238 if (found->root == bctx->sctx->send_root &&
1239 ino == bctx->cur_objectid &&
1240 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001241 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001242 }
1243
1244 /*
Alexander Block766702e2012-07-28 14:11:31 +02001245 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001246 * accept clones from these extents.
1247 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001248 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1249 NULL, NULL, NULL);
1250 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001251 if (ret < 0)
1252 return ret;
1253
Filipe Manana619d8c42015-05-03 01:56:00 +01001254 if (offset + bctx->data_offset + bctx->extent_len > i_size)
Alexander Block31db9f72012-07-25 23:19:24 +02001255 return 0;
1256
1257 /*
1258 * Make sure we don't consider clones from send_root that are
1259 * behind the current inode/offset.
1260 */
1261 if (found->root == bctx->sctx->send_root) {
1262 /*
1263 * TODO for the moment we don't accept clones from the inode
1264 * that is currently send. We may change this when
1265 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1266 * file.
1267 */
1268 if (ino >= bctx->cur_objectid)
1269 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001270 }
1271
1272 bctx->found++;
1273 found->found_refs++;
1274 if (ino < found->ino) {
1275 found->ino = ino;
1276 found->offset = offset;
1277 } else if (found->ino == ino) {
1278 /*
1279 * same extent found more then once in the same file.
1280 */
1281 if (found->offset > offset + bctx->extent_len)
1282 found->offset = offset;
1283 }
1284
1285 return 0;
1286}
1287
1288/*
Alexander Block766702e2012-07-28 14:11:31 +02001289 * Given an inode, offset and extent item, it finds a good clone for a clone
1290 * instruction. Returns -ENOENT when none could be found. The function makes
1291 * sure that the returned clone is usable at the point where sending is at the
1292 * moment. This means, that no clones are accepted which lie behind the current
1293 * inode+offset.
1294 *
Alexander Block31db9f72012-07-25 23:19:24 +02001295 * path must point to the extent item when called.
1296 */
1297static int find_extent_clone(struct send_ctx *sctx,
1298 struct btrfs_path *path,
1299 u64 ino, u64 data_offset,
1300 u64 ino_size,
1301 struct clone_root **found)
1302{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001303 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001304 int ret;
1305 int extent_type;
1306 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001307 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001308 u64 num_bytes;
1309 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001310 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001311 struct btrfs_file_extent_item *fi;
1312 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001313 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001314 struct clone_root *cur_clone_root;
1315 struct btrfs_key found_key;
1316 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001317 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001318 u32 i;
1319
1320 tmp_path = alloc_path_for_send();
1321 if (!tmp_path)
1322 return -ENOMEM;
1323
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001324 /* We only use this path under the commit sem */
1325 tmp_path->need_commit_sem = 0;
1326
David Sterbae780b0d2016-01-18 18:42:13 +01001327 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001328 if (!backref_ctx) {
1329 ret = -ENOMEM;
1330 goto out;
1331 }
1332
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001333 backref_ctx->path = tmp_path;
1334
Alexander Block31db9f72012-07-25 23:19:24 +02001335 if (data_offset >= ino_size) {
1336 /*
1337 * There may be extents that lie behind the file's size.
1338 * I at least had this in combination with snapshotting while
1339 * writing large files.
1340 */
1341 ret = 0;
1342 goto out;
1343 }
1344
1345 fi = btrfs_item_ptr(eb, path->slots[0],
1346 struct btrfs_file_extent_item);
1347 extent_type = btrfs_file_extent_type(eb, fi);
1348 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1349 ret = -ENOENT;
1350 goto out;
1351 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001352 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001353
1354 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001355 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1356 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001357 ret = -ENOENT;
1358 goto out;
1359 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001360 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001361
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001362 down_read(&fs_info->commit_root_sem);
1363 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001364 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001365 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001366 btrfs_release_path(tmp_path);
1367
1368 if (ret < 0)
1369 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001370 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001371 ret = -EIO;
1372 goto out;
1373 }
1374
1375 /*
1376 * Setup the clone roots.
1377 */
1378 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1379 cur_clone_root = sctx->clone_roots + i;
1380 cur_clone_root->ino = (u64)-1;
1381 cur_clone_root->offset = 0;
1382 cur_clone_root->found_refs = 0;
1383 }
1384
Alexander Block35075bb2012-07-28 12:44:34 +02001385 backref_ctx->sctx = sctx;
1386 backref_ctx->found = 0;
1387 backref_ctx->cur_objectid = ino;
1388 backref_ctx->cur_offset = data_offset;
1389 backref_ctx->found_itself = 0;
1390 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001391 /*
1392 * For non-compressed extents iterate_extent_inodes() gives us extent
1393 * offsets that already take into account the data offset, but not for
1394 * compressed extents, since the offset is logical and not relative to
1395 * the physical extent locations. We must take this into account to
1396 * avoid sending clone offsets that go beyond the source file's size,
1397 * which would result in the clone ioctl failing with -EINVAL on the
1398 * receiving end.
1399 */
1400 if (compressed == BTRFS_COMPRESS_NONE)
1401 backref_ctx->data_offset = 0;
1402 else
1403 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001404
1405 /*
1406 * The last extent of a file may be too large due to page alignment.
1407 * We need to adjust extent_len in this case so that the checks in
1408 * __iterate_backrefs work.
1409 */
1410 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001411 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001412
1413 /*
1414 * Now collect all backrefs.
1415 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001416 if (compressed == BTRFS_COMPRESS_NONE)
1417 extent_item_pos = logical - found_key.objectid;
1418 else
1419 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001420 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1421 extent_item_pos, 1, __iterate_backrefs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001422 backref_ctx, false);
Chris Mason74dd17f2012-08-07 16:25:13 -04001423
Alexander Block31db9f72012-07-25 23:19:24 +02001424 if (ret < 0)
1425 goto out;
1426
Alexander Block35075bb2012-07-28 12:44:34 +02001427 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001428 /* found a bug in backref code? */
1429 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001430 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001431 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001432 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001433 goto out;
1434 }
1435
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001436 btrfs_debug(fs_info,
1437 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1438 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001439
Alexander Block35075bb2012-07-28 12:44:34 +02001440 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001441 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001442
1443 cur_clone_root = NULL;
1444 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1445 if (sctx->clone_roots[i].found_refs) {
1446 if (!cur_clone_root)
1447 cur_clone_root = sctx->clone_roots + i;
1448 else if (sctx->clone_roots[i].root == sctx->send_root)
1449 /* prefer clones from send_root over others */
1450 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001451 }
1452
1453 }
1454
1455 if (cur_clone_root) {
1456 *found = cur_clone_root;
1457 ret = 0;
1458 } else {
1459 ret = -ENOENT;
1460 }
1461
1462out:
1463 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001464 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001465 return ret;
1466}
1467
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001468static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001469 u64 ino,
1470 struct fs_path *dest)
1471{
1472 int ret;
1473 struct btrfs_path *path;
1474 struct btrfs_key key;
1475 struct btrfs_file_extent_item *ei;
1476 u8 type;
1477 u8 compression;
1478 unsigned long off;
1479 int len;
1480
1481 path = alloc_path_for_send();
1482 if (!path)
1483 return -ENOMEM;
1484
1485 key.objectid = ino;
1486 key.type = BTRFS_EXTENT_DATA_KEY;
1487 key.offset = 0;
1488 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1489 if (ret < 0)
1490 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001491 if (ret) {
1492 /*
1493 * An empty symlink inode. Can happen in rare error paths when
1494 * creating a symlink (transaction committed before the inode
1495 * eviction handler removed the symlink inode items and a crash
1496 * happened in between or the subvol was snapshoted in between).
1497 * Print an informative message to dmesg/syslog so that the user
1498 * can delete the symlink.
1499 */
1500 btrfs_err(root->fs_info,
1501 "Found empty symlink inode %llu at root %llu",
1502 ino, root->root_key.objectid);
1503 ret = -EIO;
1504 goto out;
1505 }
Alexander Block31db9f72012-07-25 23:19:24 +02001506
1507 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1508 struct btrfs_file_extent_item);
1509 type = btrfs_file_extent_type(path->nodes[0], ei);
1510 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1511 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1512 BUG_ON(compression);
1513
1514 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001515 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001516
1517 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001518
1519out:
1520 btrfs_free_path(path);
1521 return ret;
1522}
1523
1524/*
1525 * Helper function to generate a file name that is unique in the root of
1526 * send_root and parent_root. This is used to generate names for orphan inodes.
1527 */
1528static int gen_unique_name(struct send_ctx *sctx,
1529 u64 ino, u64 gen,
1530 struct fs_path *dest)
1531{
1532 int ret = 0;
1533 struct btrfs_path *path;
1534 struct btrfs_dir_item *di;
1535 char tmp[64];
1536 int len;
1537 u64 idx = 0;
1538
1539 path = alloc_path_for_send();
1540 if (!path)
1541 return -ENOMEM;
1542
1543 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001544 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001545 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001546 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001547
1548 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1549 path, BTRFS_FIRST_FREE_OBJECTID,
1550 tmp, strlen(tmp), 0);
1551 btrfs_release_path(path);
1552 if (IS_ERR(di)) {
1553 ret = PTR_ERR(di);
1554 goto out;
1555 }
1556 if (di) {
1557 /* not unique, try again */
1558 idx++;
1559 continue;
1560 }
1561
1562 if (!sctx->parent_root) {
1563 /* unique */
1564 ret = 0;
1565 break;
1566 }
1567
1568 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1569 path, BTRFS_FIRST_FREE_OBJECTID,
1570 tmp, strlen(tmp), 0);
1571 btrfs_release_path(path);
1572 if (IS_ERR(di)) {
1573 ret = PTR_ERR(di);
1574 goto out;
1575 }
1576 if (di) {
1577 /* not unique, try again */
1578 idx++;
1579 continue;
1580 }
1581 /* unique */
1582 break;
1583 }
1584
1585 ret = fs_path_add(dest, tmp, strlen(tmp));
1586
1587out:
1588 btrfs_free_path(path);
1589 return ret;
1590}
1591
1592enum inode_state {
1593 inode_state_no_change,
1594 inode_state_will_create,
1595 inode_state_did_create,
1596 inode_state_will_delete,
1597 inode_state_did_delete,
1598};
1599
1600static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1601{
1602 int ret;
1603 int left_ret;
1604 int right_ret;
1605 u64 left_gen;
1606 u64 right_gen;
1607
1608 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001609 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001610 if (ret < 0 && ret != -ENOENT)
1611 goto out;
1612 left_ret = ret;
1613
1614 if (!sctx->parent_root) {
1615 right_ret = -ENOENT;
1616 } else {
1617 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001618 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001619 if (ret < 0 && ret != -ENOENT)
1620 goto out;
1621 right_ret = ret;
1622 }
1623
1624 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001625 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001626 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001627 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001628 if (ino < sctx->send_progress)
1629 ret = inode_state_did_create;
1630 else
1631 ret = inode_state_will_create;
1632 } else if (right_gen == gen) {
1633 if (ino < sctx->send_progress)
1634 ret = inode_state_did_delete;
1635 else
1636 ret = inode_state_will_delete;
1637 } else {
1638 ret = -ENOENT;
1639 }
1640 } else if (!left_ret) {
1641 if (left_gen == gen) {
1642 if (ino < sctx->send_progress)
1643 ret = inode_state_did_create;
1644 else
1645 ret = inode_state_will_create;
1646 } else {
1647 ret = -ENOENT;
1648 }
1649 } else if (!right_ret) {
1650 if (right_gen == gen) {
1651 if (ino < sctx->send_progress)
1652 ret = inode_state_did_delete;
1653 else
1654 ret = inode_state_will_delete;
1655 } else {
1656 ret = -ENOENT;
1657 }
1658 } else {
1659 ret = -ENOENT;
1660 }
1661
1662out:
1663 return ret;
1664}
1665
1666static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1667{
1668 int ret;
1669
Robbie Ko4dd99202017-01-05 16:24:55 +08001670 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1671 return 1;
1672
Alexander Block31db9f72012-07-25 23:19:24 +02001673 ret = get_cur_inode_state(sctx, ino, gen);
1674 if (ret < 0)
1675 goto out;
1676
1677 if (ret == inode_state_no_change ||
1678 ret == inode_state_did_create ||
1679 ret == inode_state_will_delete)
1680 ret = 1;
1681 else
1682 ret = 0;
1683
1684out:
1685 return ret;
1686}
1687
1688/*
1689 * Helper function to lookup a dir item in a dir.
1690 */
1691static int lookup_dir_item_inode(struct btrfs_root *root,
1692 u64 dir, const char *name, int name_len,
1693 u64 *found_inode,
1694 u8 *found_type)
1695{
1696 int ret = 0;
1697 struct btrfs_dir_item *di;
1698 struct btrfs_key key;
1699 struct btrfs_path *path;
1700
1701 path = alloc_path_for_send();
1702 if (!path)
1703 return -ENOMEM;
1704
1705 di = btrfs_lookup_dir_item(NULL, root, path,
1706 dir, name, name_len, 0);
1707 if (!di) {
1708 ret = -ENOENT;
1709 goto out;
1710 }
1711 if (IS_ERR(di)) {
1712 ret = PTR_ERR(di);
1713 goto out;
1714 }
1715 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001716 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1717 ret = -ENOENT;
1718 goto out;
1719 }
Alexander Block31db9f72012-07-25 23:19:24 +02001720 *found_inode = key.objectid;
1721 *found_type = btrfs_dir_type(path->nodes[0], di);
1722
1723out:
1724 btrfs_free_path(path);
1725 return ret;
1726}
1727
Alexander Block766702e2012-07-28 14:11:31 +02001728/*
1729 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1730 * generation of the parent dir and the name of the dir entry.
1731 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001732static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001733 u64 *dir, u64 *dir_gen, struct fs_path *name)
1734{
1735 int ret;
1736 struct btrfs_key key;
1737 struct btrfs_key found_key;
1738 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001739 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001740 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001741
1742 path = alloc_path_for_send();
1743 if (!path)
1744 return -ENOMEM;
1745
1746 key.objectid = ino;
1747 key.type = BTRFS_INODE_REF_KEY;
1748 key.offset = 0;
1749
1750 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1751 if (ret < 0)
1752 goto out;
1753 if (!ret)
1754 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1755 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001756 if (ret || found_key.objectid != ino ||
1757 (found_key.type != BTRFS_INODE_REF_KEY &&
1758 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001759 ret = -ENOENT;
1760 goto out;
1761 }
1762
Filipe Manana51a60252014-05-13 22:01:02 +01001763 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001764 struct btrfs_inode_ref *iref;
1765 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1766 struct btrfs_inode_ref);
1767 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1768 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1769 (unsigned long)(iref + 1),
1770 len);
1771 parent_dir = found_key.offset;
1772 } else {
1773 struct btrfs_inode_extref *extref;
1774 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1775 struct btrfs_inode_extref);
1776 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1777 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1778 (unsigned long)&extref->name, len);
1779 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1780 }
Alexander Block31db9f72012-07-25 23:19:24 +02001781 if (ret < 0)
1782 goto out;
1783 btrfs_release_path(path);
1784
Filipe Mananab46ab972014-03-21 12:46:54 +00001785 if (dir_gen) {
1786 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1787 NULL, NULL, NULL);
1788 if (ret < 0)
1789 goto out;
1790 }
Alexander Block31db9f72012-07-25 23:19:24 +02001791
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001792 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001793
1794out:
1795 btrfs_free_path(path);
1796 return ret;
1797}
1798
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001799static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001800 u64 ino, u64 dir,
1801 const char *name, int name_len)
1802{
1803 int ret;
1804 struct fs_path *tmp_name;
1805 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001806
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001807 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001808 if (!tmp_name)
1809 return -ENOMEM;
1810
Filipe Mananab46ab972014-03-21 12:46:54 +00001811 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001812 if (ret < 0)
1813 goto out;
1814
Alexander Blockb9291af2012-07-28 11:07:18 +02001815 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001816 ret = 0;
1817 goto out;
1818 }
1819
Alexander Blocke938c8a2012-07-28 16:33:49 +02001820 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001821
1822out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001823 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001824 return ret;
1825}
1826
Alexander Block766702e2012-07-28 14:11:31 +02001827/*
1828 * Used by process_recorded_refs to determine if a new ref would overwrite an
1829 * already existing ref. In case it detects an overwrite, it returns the
1830 * inode/gen in who_ino/who_gen.
1831 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1832 * to make sure later references to the overwritten inode are possible.
1833 * Orphanizing is however only required for the first ref of an inode.
1834 * process_recorded_refs does an additional is_first_ref check to see if
1835 * orphanizing is really required.
1836 */
Alexander Block31db9f72012-07-25 23:19:24 +02001837static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1838 const char *name, int name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01001839 u64 *who_ino, u64 *who_gen, u64 *who_mode)
Alexander Block31db9f72012-07-25 23:19:24 +02001840{
1841 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001842 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001843 u64 other_inode = 0;
1844 u8 other_type = 0;
1845
1846 if (!sctx->parent_root)
1847 goto out;
1848
1849 ret = is_inode_existent(sctx, dir, dir_gen);
1850 if (ret <= 0)
1851 goto out;
1852
Josef Bacikebdad912013-08-06 16:47:48 -04001853 /*
1854 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001855 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001856 * and we can just unlink this entry.
1857 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001858 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001859 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1860 NULL, NULL, NULL);
1861 if (ret < 0 && ret != -ENOENT)
1862 goto out;
1863 if (ret) {
1864 ret = 0;
1865 goto out;
1866 }
1867 if (gen != dir_gen)
1868 goto out;
1869 }
1870
Alexander Block31db9f72012-07-25 23:19:24 +02001871 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1872 &other_inode, &other_type);
1873 if (ret < 0 && ret != -ENOENT)
1874 goto out;
1875 if (ret) {
1876 ret = 0;
1877 goto out;
1878 }
1879
Alexander Block766702e2012-07-28 14:11:31 +02001880 /*
1881 * Check if the overwritten ref was already processed. If yes, the ref
1882 * was already unlinked/moved, so we can safely assume that we will not
1883 * overwrite anything at this point in time.
1884 */
Robbie Ko801bec32015-06-23 18:39:46 +08001885 if (other_inode > sctx->send_progress ||
1886 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001887 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Filipe Mananaf5962782017-06-22 20:03:51 +01001888 who_gen, who_mode, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001889 if (ret < 0)
1890 goto out;
1891
1892 ret = 1;
1893 *who_ino = other_inode;
1894 } else {
1895 ret = 0;
1896 }
1897
1898out:
1899 return ret;
1900}
1901
Alexander Block766702e2012-07-28 14:11:31 +02001902/*
1903 * Checks if the ref was overwritten by an already processed inode. This is
1904 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1905 * thus the orphan name needs be used.
1906 * process_recorded_refs also uses it to avoid unlinking of refs that were
1907 * overwritten.
1908 */
Alexander Block31db9f72012-07-25 23:19:24 +02001909static int did_overwrite_ref(struct send_ctx *sctx,
1910 u64 dir, u64 dir_gen,
1911 u64 ino, u64 ino_gen,
1912 const char *name, int name_len)
1913{
1914 int ret = 0;
1915 u64 gen;
1916 u64 ow_inode;
1917 u8 other_type;
1918
1919 if (!sctx->parent_root)
1920 goto out;
1921
1922 ret = is_inode_existent(sctx, dir, dir_gen);
1923 if (ret <= 0)
1924 goto out;
1925
Robbie Ko01914102017-01-05 16:24:58 +08001926 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1927 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1928 NULL, NULL, NULL);
1929 if (ret < 0 && ret != -ENOENT)
1930 goto out;
1931 if (ret) {
1932 ret = 0;
1933 goto out;
1934 }
1935 if (gen != dir_gen)
1936 goto out;
1937 }
1938
Alexander Block31db9f72012-07-25 23:19:24 +02001939 /* check if the ref was overwritten by another ref */
1940 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1941 &ow_inode, &other_type);
1942 if (ret < 0 && ret != -ENOENT)
1943 goto out;
1944 if (ret) {
1945 /* was never and will never be overwritten */
1946 ret = 0;
1947 goto out;
1948 }
1949
1950 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001951 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001952 if (ret < 0)
1953 goto out;
1954
1955 if (ow_inode == ino && gen == ino_gen) {
1956 ret = 0;
1957 goto out;
1958 }
1959
Filipe Manana8b191a62015-04-09 14:09:14 +01001960 /*
1961 * We know that it is or will be overwritten. Check this now.
1962 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001963 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1964 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001965 */
Filipe Mananab786f162015-09-26 15:30:23 +01001966 if ((ow_inode < sctx->send_progress) ||
1967 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1968 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001969 ret = 1;
1970 else
1971 ret = 0;
1972
1973out:
1974 return ret;
1975}
1976
Alexander Block766702e2012-07-28 14:11:31 +02001977/*
1978 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1979 * that got overwritten. This is used by process_recorded_refs to determine
1980 * if it has to use the path as returned by get_cur_path or the orphan name.
1981 */
Alexander Block31db9f72012-07-25 23:19:24 +02001982static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1983{
1984 int ret = 0;
1985 struct fs_path *name = NULL;
1986 u64 dir;
1987 u64 dir_gen;
1988
1989 if (!sctx->parent_root)
1990 goto out;
1991
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001992 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001993 if (!name)
1994 return -ENOMEM;
1995
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001996 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001997 if (ret < 0)
1998 goto out;
1999
2000 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2001 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02002002
2003out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002004 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002005 return ret;
2006}
2007
Alexander Block766702e2012-07-28 14:11:31 +02002008/*
2009 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2010 * so we need to do some special handling in case we have clashes. This function
2011 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002012 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002013 */
Alexander Block31db9f72012-07-25 23:19:24 +02002014static int name_cache_insert(struct send_ctx *sctx,
2015 struct name_cache_entry *nce)
2016{
2017 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002018 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002019
Alexander Block7e0926f2012-07-28 14:20:58 +02002020 nce_head = radix_tree_lookup(&sctx->name_cache,
2021 (unsigned long)nce->ino);
2022 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002023 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002024 if (!nce_head) {
2025 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002026 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002027 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002028 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002029
Alexander Block7e0926f2012-07-28 14:20:58 +02002030 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002031 if (ret < 0) {
2032 kfree(nce_head);
2033 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002034 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002035 }
Alexander Block31db9f72012-07-25 23:19:24 +02002036 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002037 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002038 list_add_tail(&nce->list, &sctx->name_cache_list);
2039 sctx->name_cache_size++;
2040
2041 return ret;
2042}
2043
2044static void name_cache_delete(struct send_ctx *sctx,
2045 struct name_cache_entry *nce)
2046{
Alexander Block7e0926f2012-07-28 14:20:58 +02002047 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002048
Alexander Block7e0926f2012-07-28 14:20:58 +02002049 nce_head = radix_tree_lookup(&sctx->name_cache,
2050 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002051 if (!nce_head) {
2052 btrfs_err(sctx->send_root->fs_info,
2053 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2054 nce->ino, sctx->name_cache_size);
2055 }
Alexander Block31db9f72012-07-25 23:19:24 +02002056
Alexander Block7e0926f2012-07-28 14:20:58 +02002057 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002058 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002059 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002060
David Sterba57fb8912014-02-03 19:24:40 +01002061 /*
2062 * We may not get to the final release of nce_head if the lookup fails
2063 */
2064 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002065 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2066 kfree(nce_head);
2067 }
Alexander Block31db9f72012-07-25 23:19:24 +02002068}
2069
2070static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2071 u64 ino, u64 gen)
2072{
Alexander Block7e0926f2012-07-28 14:20:58 +02002073 struct list_head *nce_head;
2074 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002075
Alexander Block7e0926f2012-07-28 14:20:58 +02002076 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2077 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002078 return NULL;
2079
Alexander Block7e0926f2012-07-28 14:20:58 +02002080 list_for_each_entry(cur, nce_head, radix_list) {
2081 if (cur->ino == ino && cur->gen == gen)
2082 return cur;
2083 }
Alexander Block31db9f72012-07-25 23:19:24 +02002084 return NULL;
2085}
2086
Alexander Block766702e2012-07-28 14:11:31 +02002087/*
2088 * Removes the entry from the list and adds it back to the end. This marks the
2089 * entry as recently used so that name_cache_clean_unused does not remove it.
2090 */
Alexander Block31db9f72012-07-25 23:19:24 +02002091static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2092{
2093 list_del(&nce->list);
2094 list_add_tail(&nce->list, &sctx->name_cache_list);
2095}
2096
Alexander Block766702e2012-07-28 14:11:31 +02002097/*
2098 * Remove some entries from the beginning of name_cache_list.
2099 */
Alexander Block31db9f72012-07-25 23:19:24 +02002100static void name_cache_clean_unused(struct send_ctx *sctx)
2101{
2102 struct name_cache_entry *nce;
2103
2104 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2105 return;
2106
2107 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2108 nce = list_entry(sctx->name_cache_list.next,
2109 struct name_cache_entry, list);
2110 name_cache_delete(sctx, nce);
2111 kfree(nce);
2112 }
2113}
2114
2115static void name_cache_free(struct send_ctx *sctx)
2116{
2117 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002118
Alexander Blocke938c8a2012-07-28 16:33:49 +02002119 while (!list_empty(&sctx->name_cache_list)) {
2120 nce = list_entry(sctx->name_cache_list.next,
2121 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002122 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002123 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002124 }
2125}
2126
Alexander Block766702e2012-07-28 14:11:31 +02002127/*
2128 * Used by get_cur_path for each ref up to the root.
2129 * Returns 0 if it succeeded.
2130 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2131 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2132 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2133 * Returns <0 in case of error.
2134 */
Alexander Block31db9f72012-07-25 23:19:24 +02002135static int __get_cur_name_and_parent(struct send_ctx *sctx,
2136 u64 ino, u64 gen,
2137 u64 *parent_ino,
2138 u64 *parent_gen,
2139 struct fs_path *dest)
2140{
2141 int ret;
2142 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002143 struct name_cache_entry *nce = NULL;
2144
Alexander Block766702e2012-07-28 14:11:31 +02002145 /*
2146 * First check if we already did a call to this function with the same
2147 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2148 * return the cached result.
2149 */
Alexander Block31db9f72012-07-25 23:19:24 +02002150 nce = name_cache_search(sctx, ino, gen);
2151 if (nce) {
2152 if (ino < sctx->send_progress && nce->need_later_update) {
2153 name_cache_delete(sctx, nce);
2154 kfree(nce);
2155 nce = NULL;
2156 } else {
2157 name_cache_used(sctx, nce);
2158 *parent_ino = nce->parent_ino;
2159 *parent_gen = nce->parent_gen;
2160 ret = fs_path_add(dest, nce->name, nce->name_len);
2161 if (ret < 0)
2162 goto out;
2163 ret = nce->ret;
2164 goto out;
2165 }
2166 }
2167
Alexander Block766702e2012-07-28 14:11:31 +02002168 /*
2169 * If the inode is not existent yet, add the orphan name and return 1.
2170 * This should only happen for the parent dir that we determine in
2171 * __record_new_ref
2172 */
Alexander Block31db9f72012-07-25 23:19:24 +02002173 ret = is_inode_existent(sctx, ino, gen);
2174 if (ret < 0)
2175 goto out;
2176
2177 if (!ret) {
2178 ret = gen_unique_name(sctx, ino, gen, dest);
2179 if (ret < 0)
2180 goto out;
2181 ret = 1;
2182 goto out_cache;
2183 }
2184
Alexander Block766702e2012-07-28 14:11:31 +02002185 /*
2186 * Depending on whether the inode was already processed or not, use
2187 * send_root or parent_root for ref lookup.
2188 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002189 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002190 ret = get_first_ref(sctx->send_root, ino,
2191 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002192 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002193 ret = get_first_ref(sctx->parent_root, ino,
2194 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002195 if (ret < 0)
2196 goto out;
2197
Alexander Block766702e2012-07-28 14:11:31 +02002198 /*
2199 * Check if the ref was overwritten by an inode's ref that was processed
2200 * earlier. If yes, treat as orphan and return 1.
2201 */
Alexander Block31db9f72012-07-25 23:19:24 +02002202 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2203 dest->start, dest->end - dest->start);
2204 if (ret < 0)
2205 goto out;
2206 if (ret) {
2207 fs_path_reset(dest);
2208 ret = gen_unique_name(sctx, ino, gen, dest);
2209 if (ret < 0)
2210 goto out;
2211 ret = 1;
2212 }
2213
2214out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002215 /*
2216 * Store the result of the lookup in the name cache.
2217 */
David Sterbae780b0d2016-01-18 18:42:13 +01002218 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002219 if (!nce) {
2220 ret = -ENOMEM;
2221 goto out;
2222 }
2223
2224 nce->ino = ino;
2225 nce->gen = gen;
2226 nce->parent_ino = *parent_ino;
2227 nce->parent_gen = *parent_gen;
2228 nce->name_len = fs_path_len(dest);
2229 nce->ret = ret;
2230 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002231
2232 if (ino < sctx->send_progress)
2233 nce->need_later_update = 0;
2234 else
2235 nce->need_later_update = 1;
2236
2237 nce_ret = name_cache_insert(sctx, nce);
2238 if (nce_ret < 0)
2239 ret = nce_ret;
2240 name_cache_clean_unused(sctx);
2241
2242out:
Alexander Block31db9f72012-07-25 23:19:24 +02002243 return ret;
2244}
2245
2246/*
2247 * Magic happens here. This function returns the first ref to an inode as it
2248 * would look like while receiving the stream at this point in time.
2249 * We walk the path up to the root. For every inode in between, we check if it
2250 * was already processed/sent. If yes, we continue with the parent as found
2251 * in send_root. If not, we continue with the parent as found in parent_root.
2252 * If we encounter an inode that was deleted at this point in time, we use the
2253 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2254 * that were not created yet and overwritten inodes/refs.
2255 *
2256 * When do we have have orphan inodes:
2257 * 1. When an inode is freshly created and thus no valid refs are available yet
2258 * 2. When a directory lost all it's refs (deleted) but still has dir items
2259 * inside which were not processed yet (pending for move/delete). If anyone
2260 * tried to get the path to the dir items, it would get a path inside that
2261 * orphan directory.
2262 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2263 * of an unprocessed inode. If in that case the first ref would be
2264 * overwritten, the overwritten inode gets "orphanized". Later when we
2265 * process this overwritten inode, it is restored at a new place by moving
2266 * the orphan inode.
2267 *
2268 * sctx->send_progress tells this function at which point in time receiving
2269 * would be.
2270 */
2271static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2272 struct fs_path *dest)
2273{
2274 int ret = 0;
2275 struct fs_path *name = NULL;
2276 u64 parent_inode = 0;
2277 u64 parent_gen = 0;
2278 int stop = 0;
2279
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002280 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002281 if (!name) {
2282 ret = -ENOMEM;
2283 goto out;
2284 }
2285
2286 dest->reversed = 1;
2287 fs_path_reset(dest);
2288
2289 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002290 struct waiting_dir_move *wdm;
2291
Alexander Block31db9f72012-07-25 23:19:24 +02002292 fs_path_reset(name);
2293
Filipe Manana9dc44212014-02-19 14:31:44 +00002294 if (is_waiting_for_rm(sctx, ino)) {
2295 ret = gen_unique_name(sctx, ino, gen, name);
2296 if (ret < 0)
2297 goto out;
2298 ret = fs_path_add_path(dest, name);
2299 break;
2300 }
2301
Filipe Manana8b191a62015-04-09 14:09:14 +01002302 wdm = get_waiting_dir_move(sctx, ino);
2303 if (wdm && wdm->orphanized) {
2304 ret = gen_unique_name(sctx, ino, gen, name);
2305 stop = 1;
2306 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002307 ret = get_first_ref(sctx->parent_root, ino,
2308 &parent_inode, &parent_gen, name);
2309 } else {
2310 ret = __get_cur_name_and_parent(sctx, ino, gen,
2311 &parent_inode,
2312 &parent_gen, name);
2313 if (ret)
2314 stop = 1;
2315 }
2316
Alexander Block31db9f72012-07-25 23:19:24 +02002317 if (ret < 0)
2318 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002319
Alexander Block31db9f72012-07-25 23:19:24 +02002320 ret = fs_path_add_path(dest, name);
2321 if (ret < 0)
2322 goto out;
2323
2324 ino = parent_inode;
2325 gen = parent_gen;
2326 }
2327
2328out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002329 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002330 if (!ret)
2331 fs_path_unreverse(dest);
2332 return ret;
2333}
2334
2335/*
Alexander Block31db9f72012-07-25 23:19:24 +02002336 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2337 */
2338static int send_subvol_begin(struct send_ctx *sctx)
2339{
2340 int ret;
2341 struct btrfs_root *send_root = sctx->send_root;
2342 struct btrfs_root *parent_root = sctx->parent_root;
2343 struct btrfs_path *path;
2344 struct btrfs_key key;
2345 struct btrfs_root_ref *ref;
2346 struct extent_buffer *leaf;
2347 char *name = NULL;
2348 int namelen;
2349
Wang Shilongffcfaf82014-01-15 00:26:43 +08002350 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002351 if (!path)
2352 return -ENOMEM;
2353
David Sterbae780b0d2016-01-18 18:42:13 +01002354 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002355 if (!name) {
2356 btrfs_free_path(path);
2357 return -ENOMEM;
2358 }
2359
2360 key.objectid = send_root->objectid;
2361 key.type = BTRFS_ROOT_BACKREF_KEY;
2362 key.offset = 0;
2363
2364 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2365 &key, path, 1, 0);
2366 if (ret < 0)
2367 goto out;
2368 if (ret) {
2369 ret = -ENOENT;
2370 goto out;
2371 }
2372
2373 leaf = path->nodes[0];
2374 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2375 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2376 key.objectid != send_root->objectid) {
2377 ret = -ENOENT;
2378 goto out;
2379 }
2380 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2381 namelen = btrfs_root_ref_name_len(leaf, ref);
2382 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2383 btrfs_release_path(path);
2384
Alexander Block31db9f72012-07-25 23:19:24 +02002385 if (parent_root) {
2386 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2387 if (ret < 0)
2388 goto out;
2389 } else {
2390 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2391 if (ret < 0)
2392 goto out;
2393 }
2394
2395 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002396
2397 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2398 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2399 sctx->send_root->root_item.received_uuid);
2400 else
2401 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2402 sctx->send_root->root_item.uuid);
2403
Alexander Block31db9f72012-07-25 23:19:24 +02002404 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002405 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002406 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002407 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2408 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2409 parent_root->root_item.received_uuid);
2410 else
2411 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2412 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002413 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002414 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002415 }
2416
2417 ret = send_cmd(sctx);
2418
2419tlv_put_failure:
2420out:
2421 btrfs_free_path(path);
2422 kfree(name);
2423 return ret;
2424}
2425
2426static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2427{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002428 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002429 int ret = 0;
2430 struct fs_path *p;
2431
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002432 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002433
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002434 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002435 if (!p)
2436 return -ENOMEM;
2437
2438 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2439 if (ret < 0)
2440 goto out;
2441
2442 ret = get_cur_path(sctx, ino, gen, p);
2443 if (ret < 0)
2444 goto out;
2445 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2446 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2447
2448 ret = send_cmd(sctx);
2449
2450tlv_put_failure:
2451out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002452 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002453 return ret;
2454}
2455
2456static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2457{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002458 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002459 int ret = 0;
2460 struct fs_path *p;
2461
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002462 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002463
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002464 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002465 if (!p)
2466 return -ENOMEM;
2467
2468 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2469 if (ret < 0)
2470 goto out;
2471
2472 ret = get_cur_path(sctx, ino, gen, p);
2473 if (ret < 0)
2474 goto out;
2475 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2476 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2477
2478 ret = send_cmd(sctx);
2479
2480tlv_put_failure:
2481out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002482 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002483 return ret;
2484}
2485
2486static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2487{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002488 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002489 int ret = 0;
2490 struct fs_path *p;
2491
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002492 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2493 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002494
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002495 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002496 if (!p)
2497 return -ENOMEM;
2498
2499 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2500 if (ret < 0)
2501 goto out;
2502
2503 ret = get_cur_path(sctx, ino, gen, p);
2504 if (ret < 0)
2505 goto out;
2506 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2507 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2508 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2509
2510 ret = send_cmd(sctx);
2511
2512tlv_put_failure:
2513out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002514 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002515 return ret;
2516}
2517
2518static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2519{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002520 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002521 int ret = 0;
2522 struct fs_path *p = NULL;
2523 struct btrfs_inode_item *ii;
2524 struct btrfs_path *path = NULL;
2525 struct extent_buffer *eb;
2526 struct btrfs_key key;
2527 int slot;
2528
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002529 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002530
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002531 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002532 if (!p)
2533 return -ENOMEM;
2534
2535 path = alloc_path_for_send();
2536 if (!path) {
2537 ret = -ENOMEM;
2538 goto out;
2539 }
2540
2541 key.objectid = ino;
2542 key.type = BTRFS_INODE_ITEM_KEY;
2543 key.offset = 0;
2544 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002545 if (ret > 0)
2546 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002547 if (ret < 0)
2548 goto out;
2549
2550 eb = path->nodes[0];
2551 slot = path->slots[0];
2552 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2553
2554 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2555 if (ret < 0)
2556 goto out;
2557
2558 ret = get_cur_path(sctx, ino, gen, p);
2559 if (ret < 0)
2560 goto out;
2561 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002562 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2563 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2564 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002565 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002566
2567 ret = send_cmd(sctx);
2568
2569tlv_put_failure:
2570out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002571 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002572 btrfs_free_path(path);
2573 return ret;
2574}
2575
2576/*
2577 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2578 * a valid path yet because we did not process the refs yet. So, the inode
2579 * is created as orphan.
2580 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002581static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002582{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002583 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002584 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002585 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002586 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002587 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002588 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002589 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002590
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002591 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002592
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002593 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002594 if (!p)
2595 return -ENOMEM;
2596
Liu Bo644d1942014-02-27 17:29:01 +08002597 if (ino != sctx->cur_ino) {
2598 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2599 NULL, NULL, &rdev);
2600 if (ret < 0)
2601 goto out;
2602 } else {
2603 gen = sctx->cur_inode_gen;
2604 mode = sctx->cur_inode_mode;
2605 rdev = sctx->cur_inode_rdev;
2606 }
Alexander Block31db9f72012-07-25 23:19:24 +02002607
Alexander Blocke938c8a2012-07-28 16:33:49 +02002608 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002609 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002610 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002611 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002612 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002613 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002614 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002615 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002616 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002617 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002618 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002619 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002620 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002621 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002622 (int)(mode & S_IFMT));
Tsutomu Itohca6842b2016-01-22 09:13:25 +09002623 ret = -EOPNOTSUPP;
Alexander Block31db9f72012-07-25 23:19:24 +02002624 goto out;
2625 }
2626
2627 ret = begin_cmd(sctx, cmd);
2628 if (ret < 0)
2629 goto out;
2630
Alexander Block1f4692d2012-07-28 10:42:24 +02002631 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002632 if (ret < 0)
2633 goto out;
2634
2635 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002636 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002637
2638 if (S_ISLNK(mode)) {
2639 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002640 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002641 if (ret < 0)
2642 goto out;
2643 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2644 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2645 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002646 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2647 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002648 }
2649
2650 ret = send_cmd(sctx);
2651 if (ret < 0)
2652 goto out;
2653
2654
2655tlv_put_failure:
2656out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002657 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002658 return ret;
2659}
2660
Alexander Block1f4692d2012-07-28 10:42:24 +02002661/*
2662 * We need some special handling for inodes that get processed before the parent
2663 * directory got created. See process_recorded_refs for details.
2664 * This function does the check if we already created the dir out of order.
2665 */
2666static int did_create_dir(struct send_ctx *sctx, u64 dir)
2667{
2668 int ret = 0;
2669 struct btrfs_path *path = NULL;
2670 struct btrfs_key key;
2671 struct btrfs_key found_key;
2672 struct btrfs_key di_key;
2673 struct extent_buffer *eb;
2674 struct btrfs_dir_item *di;
2675 int slot;
2676
2677 path = alloc_path_for_send();
2678 if (!path) {
2679 ret = -ENOMEM;
2680 goto out;
2681 }
2682
2683 key.objectid = dir;
2684 key.type = BTRFS_DIR_INDEX_KEY;
2685 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002686 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2687 if (ret < 0)
2688 goto out;
2689
Alexander Block1f4692d2012-07-28 10:42:24 +02002690 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002691 eb = path->nodes[0];
2692 slot = path->slots[0];
2693 if (slot >= btrfs_header_nritems(eb)) {
2694 ret = btrfs_next_leaf(sctx->send_root, path);
2695 if (ret < 0) {
2696 goto out;
2697 } else if (ret > 0) {
2698 ret = 0;
2699 break;
2700 }
2701 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002702 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002703
2704 btrfs_item_key_to_cpu(eb, &found_key, slot);
2705 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002706 found_key.type != key.type) {
2707 ret = 0;
2708 goto out;
2709 }
2710
2711 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2712 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2713
Josef Bacika0525412013-08-12 10:56:14 -04002714 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2715 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002716 ret = 1;
2717 goto out;
2718 }
2719
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002720 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002721 }
2722
2723out:
2724 btrfs_free_path(path);
2725 return ret;
2726}
2727
2728/*
2729 * Only creates the inode if it is:
2730 * 1. Not a directory
2731 * 2. Or a directory which was not created already due to out of order
2732 * directories. See did_create_dir and process_recorded_refs for details.
2733 */
2734static int send_create_inode_if_needed(struct send_ctx *sctx)
2735{
2736 int ret;
2737
2738 if (S_ISDIR(sctx->cur_inode_mode)) {
2739 ret = did_create_dir(sctx, sctx->cur_ino);
2740 if (ret < 0)
2741 goto out;
2742 if (ret) {
2743 ret = 0;
2744 goto out;
2745 }
2746 }
2747
2748 ret = send_create_inode(sctx, sctx->cur_ino);
2749 if (ret < 0)
2750 goto out;
2751
2752out:
2753 return ret;
2754}
2755
Alexander Block31db9f72012-07-25 23:19:24 +02002756struct recorded_ref {
2757 struct list_head list;
Alexander Block31db9f72012-07-25 23:19:24 +02002758 char *name;
2759 struct fs_path *full_path;
2760 u64 dir;
2761 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002762 int name_len;
2763};
2764
Filipe Mananafdb13882017-06-13 14:13:11 +01002765static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2766{
2767 ref->full_path = path;
2768 ref->name = (char *)kbasename(ref->full_path->start);
2769 ref->name_len = ref->full_path->end - ref->name;
2770}
2771
Alexander Block31db9f72012-07-25 23:19:24 +02002772/*
2773 * We need to process new refs before deleted refs, but compare_tree gives us
2774 * everything mixed. So we first record all refs and later process them.
2775 * This function is a helper to record one ref.
2776 */
Liu Boa4d96d62014-03-03 21:31:03 +08002777static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002778 u64 dir_gen, struct fs_path *path)
2779{
2780 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002781
David Sterbae780b0d2016-01-18 18:42:13 +01002782 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002783 if (!ref)
2784 return -ENOMEM;
2785
2786 ref->dir = dir;
2787 ref->dir_gen = dir_gen;
Filipe Mananafdb13882017-06-13 14:13:11 +01002788 set_ref_path(ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02002789 list_add_tail(&ref->list, head);
2790 return 0;
2791}
2792
Josef Bacikba5e8f22013-08-16 16:52:55 -04002793static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2794{
2795 struct recorded_ref *new;
2796
David Sterbae780b0d2016-01-18 18:42:13 +01002797 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002798 if (!new)
2799 return -ENOMEM;
2800
2801 new->dir = ref->dir;
2802 new->dir_gen = ref->dir_gen;
2803 new->full_path = NULL;
2804 INIT_LIST_HEAD(&new->list);
2805 list_add_tail(&new->list, list);
2806 return 0;
2807}
2808
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002809static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002810{
2811 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002812
Alexander Blocke938c8a2012-07-28 16:33:49 +02002813 while (!list_empty(head)) {
2814 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002815 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002816 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002817 kfree(cur);
2818 }
Alexander Block31db9f72012-07-25 23:19:24 +02002819}
2820
2821static void free_recorded_refs(struct send_ctx *sctx)
2822{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002823 __free_recorded_refs(&sctx->new_refs);
2824 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002825}
2826
2827/*
Alexander Block766702e2012-07-28 14:11:31 +02002828 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002829 * ref of an unprocessed inode gets overwritten and for all non empty
2830 * directories.
2831 */
2832static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2833 struct fs_path *path)
2834{
2835 int ret;
2836 struct fs_path *orphan;
2837
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002838 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002839 if (!orphan)
2840 return -ENOMEM;
2841
2842 ret = gen_unique_name(sctx, ino, gen, orphan);
2843 if (ret < 0)
2844 goto out;
2845
2846 ret = send_rename(sctx, path, orphan);
2847
2848out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002849 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002850 return ret;
2851}
2852
Filipe Manana9dc44212014-02-19 14:31:44 +00002853static struct orphan_dir_info *
2854add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2855{
2856 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2857 struct rb_node *parent = NULL;
2858 struct orphan_dir_info *entry, *odi;
2859
David Sterbae780b0d2016-01-18 18:42:13 +01002860 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
Filipe Manana9dc44212014-02-19 14:31:44 +00002861 if (!odi)
2862 return ERR_PTR(-ENOMEM);
2863 odi->ino = dir_ino;
2864 odi->gen = 0;
2865
2866 while (*p) {
2867 parent = *p;
2868 entry = rb_entry(parent, struct orphan_dir_info, node);
2869 if (dir_ino < entry->ino) {
2870 p = &(*p)->rb_left;
2871 } else if (dir_ino > entry->ino) {
2872 p = &(*p)->rb_right;
2873 } else {
2874 kfree(odi);
2875 return entry;
2876 }
2877 }
2878
2879 rb_link_node(&odi->node, parent, p);
2880 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2881 return odi;
2882}
2883
2884static struct orphan_dir_info *
2885get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2886{
2887 struct rb_node *n = sctx->orphan_dirs.rb_node;
2888 struct orphan_dir_info *entry;
2889
2890 while (n) {
2891 entry = rb_entry(n, struct orphan_dir_info, node);
2892 if (dir_ino < entry->ino)
2893 n = n->rb_left;
2894 else if (dir_ino > entry->ino)
2895 n = n->rb_right;
2896 else
2897 return entry;
2898 }
2899 return NULL;
2900}
2901
2902static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2903{
2904 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2905
2906 return odi != NULL;
2907}
2908
2909static void free_orphan_dir_info(struct send_ctx *sctx,
2910 struct orphan_dir_info *odi)
2911{
2912 if (!odi)
2913 return;
2914 rb_erase(&odi->node, &sctx->orphan_dirs);
2915 kfree(odi);
2916}
2917
Alexander Block31db9f72012-07-25 23:19:24 +02002918/*
2919 * Returns 1 if a directory can be removed at this point in time.
2920 * We check this by iterating all dir items and checking if the inode behind
2921 * the dir item was already processed.
2922 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002923static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2924 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002925{
2926 int ret = 0;
2927 struct btrfs_root *root = sctx->parent_root;
2928 struct btrfs_path *path;
2929 struct btrfs_key key;
2930 struct btrfs_key found_key;
2931 struct btrfs_key loc;
2932 struct btrfs_dir_item *di;
2933
Alexander Block6d85ed02012-08-01 14:48:59 +02002934 /*
2935 * Don't try to rmdir the top/root subvolume dir.
2936 */
2937 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2938 return 0;
2939
Alexander Block31db9f72012-07-25 23:19:24 +02002940 path = alloc_path_for_send();
2941 if (!path)
2942 return -ENOMEM;
2943
2944 key.objectid = dir;
2945 key.type = BTRFS_DIR_INDEX_KEY;
2946 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002947 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2948 if (ret < 0)
2949 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002950
2951 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002952 struct waiting_dir_move *dm;
2953
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002954 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2955 ret = btrfs_next_leaf(root, path);
2956 if (ret < 0)
2957 goto out;
2958 else if (ret > 0)
2959 break;
2960 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002961 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002962 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2963 path->slots[0]);
2964 if (found_key.objectid != key.objectid ||
2965 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002966 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002967
2968 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2969 struct btrfs_dir_item);
2970 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2971
Filipe Manana9dc44212014-02-19 14:31:44 +00002972 dm = get_waiting_dir_move(sctx, loc.objectid);
2973 if (dm) {
2974 struct orphan_dir_info *odi;
2975
2976 odi = add_orphan_dir_info(sctx, dir);
2977 if (IS_ERR(odi)) {
2978 ret = PTR_ERR(odi);
2979 goto out;
2980 }
2981 odi->gen = dir_gen;
2982 dm->rmdir_ino = dir;
2983 ret = 0;
2984 goto out;
2985 }
2986
Alexander Block31db9f72012-07-25 23:19:24 +02002987 if (loc.objectid > send_progress) {
Robbie Ko443f9d22015-06-22 17:08:45 +08002988 struct orphan_dir_info *odi;
2989
2990 odi = get_orphan_dir_info(sctx, dir);
2991 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02002992 ret = 0;
2993 goto out;
2994 }
2995
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002996 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002997 }
2998
2999 ret = 1;
3000
3001out:
3002 btrfs_free_path(path);
3003 return ret;
3004}
3005
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003006static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3007{
Filipe Manana9dc44212014-02-19 14:31:44 +00003008 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003009
Filipe Manana9dc44212014-02-19 14:31:44 +00003010 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003011}
3012
Filipe Manana8b191a62015-04-09 14:09:14 +01003013static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003014{
3015 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3016 struct rb_node *parent = NULL;
3017 struct waiting_dir_move *entry, *dm;
3018
David Sterbae780b0d2016-01-18 18:42:13 +01003019 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003020 if (!dm)
3021 return -ENOMEM;
3022 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003023 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003024 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003025
3026 while (*p) {
3027 parent = *p;
3028 entry = rb_entry(parent, struct waiting_dir_move, node);
3029 if (ino < entry->ino) {
3030 p = &(*p)->rb_left;
3031 } else if (ino > entry->ino) {
3032 p = &(*p)->rb_right;
3033 } else {
3034 kfree(dm);
3035 return -EEXIST;
3036 }
3037 }
3038
3039 rb_link_node(&dm->node, parent, p);
3040 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3041 return 0;
3042}
3043
Filipe Manana9dc44212014-02-19 14:31:44 +00003044static struct waiting_dir_move *
3045get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003046{
3047 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3048 struct waiting_dir_move *entry;
3049
3050 while (n) {
3051 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003052 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003053 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003054 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003055 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003056 else
3057 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003058 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003059 return NULL;
3060}
3061
3062static void free_waiting_dir_move(struct send_ctx *sctx,
3063 struct waiting_dir_move *dm)
3064{
3065 if (!dm)
3066 return;
3067 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3068 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003069}
3070
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003071static int add_pending_dir_move(struct send_ctx *sctx,
3072 u64 ino,
3073 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003074 u64 parent_ino,
3075 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003076 struct list_head *deleted_refs,
3077 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003078{
3079 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3080 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003081 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003082 struct recorded_ref *cur;
3083 int exists = 0;
3084 int ret;
3085
David Sterbae780b0d2016-01-18 18:42:13 +01003086 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003087 if (!pm)
3088 return -ENOMEM;
3089 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003090 pm->ino = ino;
3091 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003092 INIT_LIST_HEAD(&pm->list);
3093 INIT_LIST_HEAD(&pm->update_refs);
3094 RB_CLEAR_NODE(&pm->node);
3095
3096 while (*p) {
3097 parent = *p;
3098 entry = rb_entry(parent, struct pending_dir_move, node);
3099 if (parent_ino < entry->parent_ino) {
3100 p = &(*p)->rb_left;
3101 } else if (parent_ino > entry->parent_ino) {
3102 p = &(*p)->rb_right;
3103 } else {
3104 exists = 1;
3105 break;
3106 }
3107 }
3108
Filipe Mananaf9594922014-03-27 20:14:01 +00003109 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003110 ret = dup_ref(cur, &pm->update_refs);
3111 if (ret < 0)
3112 goto out;
3113 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003114 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003115 ret = dup_ref(cur, &pm->update_refs);
3116 if (ret < 0)
3117 goto out;
3118 }
3119
Filipe Manana8b191a62015-04-09 14:09:14 +01003120 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003121 if (ret)
3122 goto out;
3123
3124 if (exists) {
3125 list_add_tail(&pm->list, &entry->list);
3126 } else {
3127 rb_link_node(&pm->node, parent, p);
3128 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3129 }
3130 ret = 0;
3131out:
3132 if (ret) {
3133 __free_recorded_refs(&pm->update_refs);
3134 kfree(pm);
3135 }
3136 return ret;
3137}
3138
3139static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3140 u64 parent_ino)
3141{
3142 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3143 struct pending_dir_move *entry;
3144
3145 while (n) {
3146 entry = rb_entry(n, struct pending_dir_move, node);
3147 if (parent_ino < entry->parent_ino)
3148 n = n->rb_left;
3149 else if (parent_ino > entry->parent_ino)
3150 n = n->rb_right;
3151 else
3152 return entry;
3153 }
3154 return NULL;
3155}
3156
Robbie Ko801bec32015-06-23 18:39:46 +08003157static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3158 u64 ino, u64 gen, u64 *ancestor_ino)
3159{
3160 int ret = 0;
3161 u64 parent_inode = 0;
3162 u64 parent_gen = 0;
3163 u64 start_ino = ino;
3164
3165 *ancestor_ino = 0;
3166 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3167 fs_path_reset(name);
3168
3169 if (is_waiting_for_rm(sctx, ino))
3170 break;
3171 if (is_waiting_for_move(sctx, ino)) {
3172 if (*ancestor_ino == 0)
3173 *ancestor_ino = ino;
3174 ret = get_first_ref(sctx->parent_root, ino,
3175 &parent_inode, &parent_gen, name);
3176 } else {
3177 ret = __get_cur_name_and_parent(sctx, ino, gen,
3178 &parent_inode,
3179 &parent_gen, name);
3180 if (ret > 0) {
3181 ret = 0;
3182 break;
3183 }
3184 }
3185 if (ret < 0)
3186 break;
3187 if (parent_inode == start_ino) {
3188 ret = 1;
3189 if (*ancestor_ino == 0)
3190 *ancestor_ino = ino;
3191 break;
3192 }
3193 ino = parent_inode;
3194 gen = parent_gen;
3195 }
3196 return ret;
3197}
3198
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003199static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3200{
3201 struct fs_path *from_path = NULL;
3202 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003203 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003204 u64 orig_progress = sctx->send_progress;
3205 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003206 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003207 struct waiting_dir_move *dm = NULL;
3208 u64 rmdir_ino = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003209 u64 ancestor;
3210 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003211 int ret;
3212
Filipe Manana2b863a12014-02-16 13:43:11 +00003213 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003214 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003215 if (!name || !from_path) {
3216 ret = -ENOMEM;
3217 goto out;
3218 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003219
Filipe Manana9dc44212014-02-19 14:31:44 +00003220 dm = get_waiting_dir_move(sctx, pm->ino);
3221 ASSERT(dm);
3222 rmdir_ino = dm->rmdir_ino;
Robbie Ko801bec32015-06-23 18:39:46 +08003223 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003224 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003225
Robbie Ko801bec32015-06-23 18:39:46 +08003226 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003227 ret = gen_unique_name(sctx, pm->ino,
3228 pm->gen, from_path);
3229 } else {
3230 ret = get_first_ref(sctx->parent_root, pm->ino,
3231 &parent_ino, &parent_gen, name);
3232 if (ret < 0)
3233 goto out;
3234 ret = get_cur_path(sctx, parent_ino, parent_gen,
3235 from_path);
3236 if (ret < 0)
3237 goto out;
3238 ret = fs_path_add_path(from_path, name);
3239 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003240 if (ret < 0)
3241 goto out;
3242
Filipe Mananaf9594922014-03-27 20:14:01 +00003243 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003244 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003245 if (ret < 0)
3246 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003247 if (ret) {
3248 LIST_HEAD(deleted_refs);
3249 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3250 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3251 &pm->update_refs, &deleted_refs,
3252 is_orphan);
3253 if (ret < 0)
3254 goto out;
3255 if (rmdir_ino) {
3256 dm = get_waiting_dir_move(sctx, pm->ino);
3257 ASSERT(dm);
3258 dm->rmdir_ino = rmdir_ino;
3259 }
3260 goto out;
3261 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003262 fs_path_reset(name);
3263 to_path = name;
3264 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003265 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3266 if (ret < 0)
3267 goto out;
3268
3269 ret = send_rename(sctx, from_path, to_path);
3270 if (ret < 0)
3271 goto out;
3272
Filipe Manana9dc44212014-02-19 14:31:44 +00003273 if (rmdir_ino) {
3274 struct orphan_dir_info *odi;
3275
3276 odi = get_orphan_dir_info(sctx, rmdir_ino);
3277 if (!odi) {
3278 /* already deleted */
3279 goto finish;
3280 }
Robbie Ko99ea42d2015-06-23 18:39:49 +08003281 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003282 if (ret < 0)
3283 goto out;
3284 if (!ret)
3285 goto finish;
3286
3287 name = fs_path_alloc();
3288 if (!name) {
3289 ret = -ENOMEM;
3290 goto out;
3291 }
3292 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3293 if (ret < 0)
3294 goto out;
3295 ret = send_rmdir(sctx, name);
3296 if (ret < 0)
3297 goto out;
3298 free_orphan_dir_info(sctx, odi);
3299 }
3300
3301finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003302 ret = send_utimes(sctx, pm->ino, pm->gen);
3303 if (ret < 0)
3304 goto out;
3305
3306 /*
3307 * After rename/move, need to update the utimes of both new parent(s)
3308 * and old parent(s).
3309 */
3310 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003311 /*
3312 * The parent inode might have been deleted in the send snapshot
3313 */
3314 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3315 NULL, NULL, NULL, NULL, NULL);
3316 if (ret == -ENOENT) {
3317 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003318 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003319 }
3320 if (ret < 0)
3321 goto out;
3322
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003323 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3324 if (ret < 0)
3325 goto out;
3326 }
3327
3328out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003329 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003330 fs_path_free(from_path);
3331 fs_path_free(to_path);
3332 sctx->send_progress = orig_progress;
3333
3334 return ret;
3335}
3336
3337static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3338{
3339 if (!list_empty(&m->list))
3340 list_del(&m->list);
3341 if (!RB_EMPTY_NODE(&m->node))
3342 rb_erase(&m->node, &sctx->pending_dir_moves);
3343 __free_recorded_refs(&m->update_refs);
3344 kfree(m);
3345}
3346
3347static void tail_append_pending_moves(struct pending_dir_move *moves,
3348 struct list_head *stack)
3349{
3350 if (list_empty(&moves->list)) {
3351 list_add_tail(&moves->list, stack);
3352 } else {
3353 LIST_HEAD(list);
3354 list_splice_init(&moves->list, &list);
3355 list_add_tail(&moves->list, stack);
3356 list_splice_tail(&list, stack);
3357 }
3358}
3359
3360static int apply_children_dir_moves(struct send_ctx *sctx)
3361{
3362 struct pending_dir_move *pm;
3363 struct list_head stack;
3364 u64 parent_ino = sctx->cur_ino;
3365 int ret = 0;
3366
3367 pm = get_pending_dir_moves(sctx, parent_ino);
3368 if (!pm)
3369 return 0;
3370
3371 INIT_LIST_HEAD(&stack);
3372 tail_append_pending_moves(pm, &stack);
3373
3374 while (!list_empty(&stack)) {
3375 pm = list_first_entry(&stack, struct pending_dir_move, list);
3376 parent_ino = pm->ino;
3377 ret = apply_dir_move(sctx, pm);
3378 free_pending_move(sctx, pm);
3379 if (ret)
3380 goto out;
3381 pm = get_pending_dir_moves(sctx, parent_ino);
3382 if (pm)
3383 tail_append_pending_moves(pm, &stack);
3384 }
3385 return 0;
3386
3387out:
3388 while (!list_empty(&stack)) {
3389 pm = list_first_entry(&stack, struct pending_dir_move, list);
3390 free_pending_move(sctx, pm);
3391 }
3392 return ret;
3393}
3394
Filipe Manana84471e22015-02-28 22:29:22 +00003395/*
3396 * We might need to delay a directory rename even when no ancestor directory
3397 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3398 * renamed. This happens when we rename a directory to the old name (the name
3399 * in the parent root) of some other unrelated directory that got its rename
3400 * delayed due to some ancestor with higher number that got renamed.
3401 *
3402 * Example:
3403 *
3404 * Parent snapshot:
3405 * . (ino 256)
3406 * |---- a/ (ino 257)
3407 * | |---- file (ino 260)
3408 * |
3409 * |---- b/ (ino 258)
3410 * |---- c/ (ino 259)
3411 *
3412 * Send snapshot:
3413 * . (ino 256)
3414 * |---- a/ (ino 258)
3415 * |---- x/ (ino 259)
3416 * |---- y/ (ino 257)
3417 * |----- file (ino 260)
3418 *
3419 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3420 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3421 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3422 * must issue is:
3423 *
3424 * 1 - rename 259 from 'c' to 'x'
3425 * 2 - rename 257 from 'a' to 'x/y'
3426 * 3 - rename 258 from 'b' to 'a'
3427 *
3428 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3429 * be done right away and < 0 on error.
3430 */
3431static int wait_for_dest_dir_move(struct send_ctx *sctx,
3432 struct recorded_ref *parent_ref,
3433 const bool is_orphan)
3434{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003435 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003436 struct btrfs_path *path;
3437 struct btrfs_key key;
3438 struct btrfs_key di_key;
3439 struct btrfs_dir_item *di;
3440 u64 left_gen;
3441 u64 right_gen;
3442 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003443 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003444
3445 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3446 return 0;
3447
3448 path = alloc_path_for_send();
3449 if (!path)
3450 return -ENOMEM;
3451
3452 key.objectid = parent_ref->dir;
3453 key.type = BTRFS_DIR_ITEM_KEY;
3454 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3455
3456 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3457 if (ret < 0) {
3458 goto out;
3459 } else if (ret > 0) {
3460 ret = 0;
3461 goto out;
3462 }
3463
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003464 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3465 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003466 if (!di) {
3467 ret = 0;
3468 goto out;
3469 }
3470 /*
3471 * di_key.objectid has the number of the inode that has a dentry in the
3472 * parent directory with the same name that sctx->cur_ino is being
3473 * renamed to. We need to check if that inode is in the send root as
3474 * well and if it is currently marked as an inode with a pending rename,
3475 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3476 * that it happens after that other inode is renamed.
3477 */
3478 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3479 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3480 ret = 0;
3481 goto out;
3482 }
3483
3484 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3485 &left_gen, NULL, NULL, NULL, NULL);
3486 if (ret < 0)
3487 goto out;
3488 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3489 &right_gen, NULL, NULL, NULL, NULL);
3490 if (ret < 0) {
3491 if (ret == -ENOENT)
3492 ret = 0;
3493 goto out;
3494 }
3495
3496 /* Different inode, no need to delay the rename of sctx->cur_ino */
3497 if (right_gen != left_gen) {
3498 ret = 0;
3499 goto out;
3500 }
3501
Robbie Ko801bec32015-06-23 18:39:46 +08003502 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3503 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003504 ret = add_pending_dir_move(sctx,
3505 sctx->cur_ino,
3506 sctx->cur_inode_gen,
3507 di_key.objectid,
3508 &sctx->new_refs,
3509 &sctx->deleted_refs,
3510 is_orphan);
3511 if (!ret)
3512 ret = 1;
3513 }
3514out:
3515 btrfs_free_path(path);
3516 return ret;
3517}
3518
Filipe Manana80aa6022015-03-27 17:50:45 +00003519/*
Filipe Mananaea37d592017-11-17 01:54:00 +00003520 * Check if inode ino2, or any of its ancestors, is inode ino1.
3521 * Return 1 if true, 0 if false and < 0 on error.
3522 */
3523static int check_ino_in_path(struct btrfs_root *root,
3524 const u64 ino1,
3525 const u64 ino1_gen,
3526 const u64 ino2,
3527 const u64 ino2_gen,
3528 struct fs_path *fs_path)
3529{
3530 u64 ino = ino2;
3531
3532 if (ino1 == ino2)
3533 return ino1_gen == ino2_gen;
3534
3535 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3536 u64 parent;
3537 u64 parent_gen;
3538 int ret;
3539
3540 fs_path_reset(fs_path);
3541 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3542 if (ret < 0)
3543 return ret;
3544 if (parent == ino1)
3545 return parent_gen == ino1_gen;
3546 ino = parent;
3547 }
3548 return 0;
3549}
3550
3551/*
3552 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3553 * possible path (in case ino2 is not a directory and has multiple hard links).
Filipe Manana80aa6022015-03-27 17:50:45 +00003554 * Return 1 if true, 0 if false and < 0 on error.
3555 */
3556static int is_ancestor(struct btrfs_root *root,
3557 const u64 ino1,
3558 const u64 ino1_gen,
3559 const u64 ino2,
3560 struct fs_path *fs_path)
3561{
Filipe Mananaea37d592017-11-17 01:54:00 +00003562 bool free_fs_path = false;
Filipe Manana72c36682017-06-07 11:41:29 +01003563 int ret = 0;
Filipe Mananaea37d592017-11-17 01:54:00 +00003564 struct btrfs_path *path = NULL;
3565 struct btrfs_key key;
Filipe Manana72c36682017-06-07 11:41:29 +01003566
3567 if (!fs_path) {
3568 fs_path = fs_path_alloc();
3569 if (!fs_path)
3570 return -ENOMEM;
Filipe Mananaea37d592017-11-17 01:54:00 +00003571 free_fs_path = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003572 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003573
Filipe Mananaea37d592017-11-17 01:54:00 +00003574 path = alloc_path_for_send();
3575 if (!path) {
3576 ret = -ENOMEM;
3577 goto out;
Filipe Manana80aa6022015-03-27 17:50:45 +00003578 }
Filipe Mananaea37d592017-11-17 01:54:00 +00003579
3580 key.objectid = ino2;
3581 key.type = BTRFS_INODE_REF_KEY;
3582 key.offset = 0;
3583
3584 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3585 if (ret < 0)
3586 goto out;
3587
3588 while (true) {
3589 struct extent_buffer *leaf = path->nodes[0];
3590 int slot = path->slots[0];
3591 u32 cur_offset = 0;
3592 u32 item_size;
3593
3594 if (slot >= btrfs_header_nritems(leaf)) {
3595 ret = btrfs_next_leaf(root, path);
3596 if (ret < 0)
3597 goto out;
3598 if (ret > 0)
3599 break;
3600 continue;
3601 }
3602
3603 btrfs_item_key_to_cpu(leaf, &key, slot);
3604 if (key.objectid != ino2)
3605 break;
3606 if (key.type != BTRFS_INODE_REF_KEY &&
3607 key.type != BTRFS_INODE_EXTREF_KEY)
3608 break;
3609
3610 item_size = btrfs_item_size_nr(leaf, slot);
3611 while (cur_offset < item_size) {
3612 u64 parent;
3613 u64 parent_gen;
3614
3615 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3616 unsigned long ptr;
3617 struct btrfs_inode_extref *extref;
3618
3619 ptr = btrfs_item_ptr_offset(leaf, slot);
3620 extref = (struct btrfs_inode_extref *)
3621 (ptr + cur_offset);
3622 parent = btrfs_inode_extref_parent(leaf,
3623 extref);
3624 cur_offset += sizeof(*extref);
3625 cur_offset += btrfs_inode_extref_name_len(leaf,
3626 extref);
3627 } else {
3628 parent = key.offset;
3629 cur_offset = item_size;
3630 }
3631
3632 ret = get_inode_info(root, parent, NULL, &parent_gen,
3633 NULL, NULL, NULL, NULL);
3634 if (ret < 0)
3635 goto out;
3636 ret = check_ino_in_path(root, ino1, ino1_gen,
3637 parent, parent_gen, fs_path);
3638 if (ret)
3639 goto out;
3640 }
3641 path->slots[0]++;
3642 }
3643 ret = 0;
Filipe Manana72c36682017-06-07 11:41:29 +01003644 out:
Filipe Mananaea37d592017-11-17 01:54:00 +00003645 btrfs_free_path(path);
3646 if (free_fs_path)
Filipe Manana72c36682017-06-07 11:41:29 +01003647 fs_path_free(fs_path);
3648 return ret;
Filipe Manana80aa6022015-03-27 17:50:45 +00003649}
3650
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003651static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003652 struct recorded_ref *parent_ref,
3653 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003654{
Filipe Mananaf9594922014-03-27 20:14:01 +00003655 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003656 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003657 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003658 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003659 struct fs_path *path_before = NULL;
3660 struct fs_path *path_after = NULL;
3661 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003662
3663 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003664 path_before = fs_path_alloc();
3665 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003666 ret = -ENOMEM;
3667 goto out;
3668 }
3669
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003670 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003671 * Our current directory inode may not yet be renamed/moved because some
3672 * ancestor (immediate or not) has to be renamed/moved first. So find if
3673 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003674 * that ancestor is processed to avoid path build infinite loops (done
3675 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003676 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003677 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003678 u64 parent_ino_after_gen;
3679
Filipe Mananaf9594922014-03-27 20:14:01 +00003680 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003681 /*
3682 * If the current inode is an ancestor of ino in the
3683 * parent root, we need to delay the rename of the
3684 * current inode, otherwise don't delayed the rename
3685 * because we can end up with a circular dependency
3686 * of renames, resulting in some directories never
3687 * getting the respective rename operations issued in
3688 * the send stream or getting into infinite path build
3689 * loops.
3690 */
3691 ret = is_ancestor(sctx->parent_root,
3692 sctx->cur_ino, sctx->cur_inode_gen,
3693 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003694 if (ret)
3695 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003696 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003697
3698 fs_path_reset(path_before);
3699 fs_path_reset(path_after);
3700
3701 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003702 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003703 if (ret < 0)
3704 goto out;
3705 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3706 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003707 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003708 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003709 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003710 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003711 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003712 }
3713
3714 len1 = fs_path_len(path_before);
3715 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003716 if (ino > sctx->cur_ino &&
3717 (parent_ino_before != parent_ino_after || len1 != len2 ||
3718 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003719 u64 parent_ino_gen;
3720
3721 ret = get_inode_info(sctx->parent_root, ino, NULL,
3722 &parent_ino_gen, NULL, NULL, NULL,
3723 NULL);
3724 if (ret < 0)
3725 goto out;
3726 if (ino_gen == parent_ino_gen) {
3727 ret = 1;
3728 break;
3729 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003730 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003731 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003732 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003733 }
3734
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003735out:
3736 fs_path_free(path_before);
3737 fs_path_free(path_after);
3738
Filipe Mananaf9594922014-03-27 20:14:01 +00003739 if (ret == 1) {
3740 ret = add_pending_dir_move(sctx,
3741 sctx->cur_ino,
3742 sctx->cur_inode_gen,
3743 ino,
3744 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003745 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003746 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003747 if (!ret)
3748 ret = 1;
3749 }
3750
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003751 return ret;
3752}
3753
Filipe Mananaf5962782017-06-22 20:03:51 +01003754static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3755{
3756 int ret;
3757 struct fs_path *new_path;
3758
3759 /*
3760 * Our reference's name member points to its full_path member string, so
3761 * we use here a new path.
3762 */
3763 new_path = fs_path_alloc();
3764 if (!new_path)
3765 return -ENOMEM;
3766
3767 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3768 if (ret < 0) {
3769 fs_path_free(new_path);
3770 return ret;
3771 }
3772 ret = fs_path_add(new_path, ref->name, ref->name_len);
3773 if (ret < 0) {
3774 fs_path_free(new_path);
3775 return ret;
3776 }
3777
3778 fs_path_free(ref->full_path);
3779 set_ref_path(ref, new_path);
3780
3781 return 0;
3782}
3783
Alexander Block31db9f72012-07-25 23:19:24 +02003784/*
3785 * This does all the move/link/unlink/rmdir magic.
3786 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003787static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003788{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003789 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003790 int ret = 0;
3791 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003792 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003793 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003794 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003795 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003796 u64 ow_gen;
Filipe Mananaf5962782017-06-22 20:03:51 +01003797 u64 ow_mode;
Alexander Block31db9f72012-07-25 23:19:24 +02003798 int did_overwrite = 0;
3799 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003800 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003801 bool can_rename = true;
Filipe Mananaf5962782017-06-22 20:03:51 +01003802 bool orphanized_dir = false;
Filipe Mananafdb13882017-06-13 14:13:11 +01003803 bool orphanized_ancestor = false;
Alexander Block31db9f72012-07-25 23:19:24 +02003804
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003805 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003806
Alexander Block6d85ed02012-08-01 14:48:59 +02003807 /*
3808 * This should never happen as the root dir always has the same ref
3809 * which is always '..'
3810 */
3811 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003812 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003813
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003814 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003815 if (!valid_path) {
3816 ret = -ENOMEM;
3817 goto out;
3818 }
3819
Alexander Block31db9f72012-07-25 23:19:24 +02003820 /*
3821 * First, check if the first ref of the current inode was overwritten
3822 * before. If yes, we know that the current inode was already orphanized
3823 * and thus use the orphan name. If not, we can use get_cur_path to
3824 * get the path of the first ref as it would like while receiving at
3825 * this point in time.
3826 * New inodes are always orphan at the beginning, so force to use the
3827 * orphan name in this case.
3828 * The first ref is stored in valid_path and will be updated if it
3829 * gets moved around.
3830 */
3831 if (!sctx->cur_inode_new) {
3832 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3833 sctx->cur_inode_gen);
3834 if (ret < 0)
3835 goto out;
3836 if (ret)
3837 did_overwrite = 1;
3838 }
3839 if (sctx->cur_inode_new || did_overwrite) {
3840 ret = gen_unique_name(sctx, sctx->cur_ino,
3841 sctx->cur_inode_gen, valid_path);
3842 if (ret < 0)
3843 goto out;
3844 is_orphan = 1;
3845 } else {
3846 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3847 valid_path);
3848 if (ret < 0)
3849 goto out;
3850 }
3851
3852 list_for_each_entry(cur, &sctx->new_refs, list) {
3853 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003854 * We may have refs where the parent directory does not exist
3855 * yet. This happens if the parent directories inum is higher
3856 * the the current inum. To handle this case, we create the
3857 * parent directory out of order. But we need to check if this
3858 * did already happen before due to other refs in the same dir.
3859 */
3860 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3861 if (ret < 0)
3862 goto out;
3863 if (ret == inode_state_will_create) {
3864 ret = 0;
3865 /*
3866 * First check if any of the current inodes refs did
3867 * already create the dir.
3868 */
3869 list_for_each_entry(cur2, &sctx->new_refs, list) {
3870 if (cur == cur2)
3871 break;
3872 if (cur2->dir == cur->dir) {
3873 ret = 1;
3874 break;
3875 }
3876 }
3877
3878 /*
3879 * If that did not happen, check if a previous inode
3880 * did already create the dir.
3881 */
3882 if (!ret)
3883 ret = did_create_dir(sctx, cur->dir);
3884 if (ret < 0)
3885 goto out;
3886 if (!ret) {
3887 ret = send_create_inode(sctx, cur->dir);
3888 if (ret < 0)
3889 goto out;
3890 }
3891 }
3892
3893 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003894 * Check if this new ref would overwrite the first ref of
3895 * another unprocessed inode. If yes, orphanize the
3896 * overwritten inode. If we find an overwritten ref that is
3897 * not the first ref, simply unlink it.
3898 */
3899 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3900 cur->name, cur->name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01003901 &ow_inode, &ow_gen, &ow_mode);
Alexander Block31db9f72012-07-25 23:19:24 +02003902 if (ret < 0)
3903 goto out;
3904 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003905 ret = is_first_ref(sctx->parent_root,
3906 ow_inode, cur->dir, cur->name,
3907 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003908 if (ret < 0)
3909 goto out;
3910 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003911 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003912 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003913
Alexander Block31db9f72012-07-25 23:19:24 +02003914 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3915 cur->full_path);
3916 if (ret < 0)
3917 goto out;
Filipe Mananaf5962782017-06-22 20:03:51 +01003918 if (S_ISDIR(ow_mode))
3919 orphanized_dir = true;
Robbie Ko801bec32015-06-23 18:39:46 +08003920
3921 /*
3922 * If ow_inode has its rename operation delayed
3923 * make sure that its orphanized name is used in
3924 * the source path when performing its rename
3925 * operation.
3926 */
3927 if (is_waiting_for_move(sctx, ow_inode)) {
3928 wdm = get_waiting_dir_move(sctx,
3929 ow_inode);
3930 ASSERT(wdm);
3931 wdm->orphanized = true;
3932 }
3933
Filipe Manana8996a482015-03-12 15:16:20 +00003934 /*
3935 * Make sure we clear our orphanized inode's
3936 * name from the name cache. This is because the
3937 * inode ow_inode might be an ancestor of some
3938 * other inode that will be orphanized as well
3939 * later and has an inode number greater than
3940 * sctx->send_progress. We need to prevent
3941 * future name lookups from using the old name
3942 * and get instead the orphan name.
3943 */
3944 nce = name_cache_search(sctx, ow_inode, ow_gen);
3945 if (nce) {
3946 name_cache_delete(sctx, nce);
3947 kfree(nce);
3948 }
Robbie Ko801bec32015-06-23 18:39:46 +08003949
3950 /*
3951 * ow_inode might currently be an ancestor of
3952 * cur_ino, therefore compute valid_path (the
3953 * current path of cur_ino) again because it
3954 * might contain the pre-orphanization name of
3955 * ow_inode, which is no longer valid.
3956 */
Filipe Manana72c36682017-06-07 11:41:29 +01003957 ret = is_ancestor(sctx->parent_root,
3958 ow_inode, ow_gen,
3959 sctx->cur_ino, NULL);
3960 if (ret > 0) {
Filipe Mananafdb13882017-06-13 14:13:11 +01003961 orphanized_ancestor = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003962 fs_path_reset(valid_path);
3963 ret = get_cur_path(sctx, sctx->cur_ino,
3964 sctx->cur_inode_gen,
3965 valid_path);
3966 }
Robbie Ko801bec32015-06-23 18:39:46 +08003967 if (ret < 0)
3968 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003969 } else {
3970 ret = send_unlink(sctx, cur->full_path);
3971 if (ret < 0)
3972 goto out;
3973 }
3974 }
3975
Filipe Manana84471e22015-02-28 22:29:22 +00003976 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3977 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3978 if (ret < 0)
3979 goto out;
3980 if (ret == 1) {
3981 can_rename = false;
3982 *pending_move = 1;
3983 }
3984 }
3985
Filipe Manana8b191a62015-04-09 14:09:14 +01003986 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3987 can_rename) {
3988 ret = wait_for_parent_move(sctx, cur, is_orphan);
3989 if (ret < 0)
3990 goto out;
3991 if (ret == 1) {
3992 can_rename = false;
3993 *pending_move = 1;
3994 }
3995 }
3996
Alexander Block31db9f72012-07-25 23:19:24 +02003997 /*
3998 * link/move the ref to the new place. If we have an orphan
3999 * inode, move it and update valid_path. If not, link or move
4000 * it depending on the inode mode.
4001 */
Filipe Manana84471e22015-02-28 22:29:22 +00004002 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004003 ret = send_rename(sctx, valid_path, cur->full_path);
4004 if (ret < 0)
4005 goto out;
4006 is_orphan = 0;
4007 ret = fs_path_copy(valid_path, cur->full_path);
4008 if (ret < 0)
4009 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00004010 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004011 if (S_ISDIR(sctx->cur_inode_mode)) {
4012 /*
4013 * Dirs can't be linked, so move it. For moved
4014 * dirs, we always have one new and one deleted
4015 * ref. The deleted ref is ignored later.
4016 */
Filipe Manana8b191a62015-04-09 14:09:14 +01004017 ret = send_rename(sctx, valid_path,
4018 cur->full_path);
4019 if (!ret)
4020 ret = fs_path_copy(valid_path,
4021 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004022 if (ret < 0)
4023 goto out;
4024 } else {
Filipe Mananaf5962782017-06-22 20:03:51 +01004025 /*
4026 * We might have previously orphanized an inode
4027 * which is an ancestor of our current inode,
4028 * so our reference's full path, which was
4029 * computed before any such orphanizations, must
4030 * be updated.
4031 */
4032 if (orphanized_dir) {
4033 ret = update_ref_path(sctx, cur);
4034 if (ret < 0)
4035 goto out;
4036 }
Alexander Block31db9f72012-07-25 23:19:24 +02004037 ret = send_link(sctx, cur->full_path,
4038 valid_path);
4039 if (ret < 0)
4040 goto out;
4041 }
4042 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004043 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004044 if (ret < 0)
4045 goto out;
4046 }
4047
4048 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4049 /*
4050 * Check if we can already rmdir the directory. If not,
4051 * orphanize it. For every dir item inside that gets deleted
4052 * later, we do this check again and rmdir it then if possible.
4053 * See the use of check_dirs for more details.
4054 */
Filipe Manana9dc44212014-02-19 14:31:44 +00004055 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4056 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004057 if (ret < 0)
4058 goto out;
4059 if (ret) {
4060 ret = send_rmdir(sctx, valid_path);
4061 if (ret < 0)
4062 goto out;
4063 } else if (!is_orphan) {
4064 ret = orphanize_inode(sctx, sctx->cur_ino,
4065 sctx->cur_inode_gen, valid_path);
4066 if (ret < 0)
4067 goto out;
4068 is_orphan = 1;
4069 }
4070
4071 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004072 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004073 if (ret < 0)
4074 goto out;
4075 }
Alexander Blockccf16262012-07-28 11:46:29 +02004076 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4077 !list_empty(&sctx->deleted_refs)) {
4078 /*
4079 * We have a moved dir. Add the old parent to check_dirs
4080 */
4081 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4082 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04004083 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02004084 if (ret < 0)
4085 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004086 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4087 /*
4088 * We have a non dir inode. Go through all deleted refs and
4089 * unlink them if they were not already overwritten by other
4090 * inodes.
4091 */
4092 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4093 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4094 sctx->cur_ino, sctx->cur_inode_gen,
4095 cur->name, cur->name_len);
4096 if (ret < 0)
4097 goto out;
4098 if (!ret) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004099 /*
4100 * If we orphanized any ancestor before, we need
4101 * to recompute the full path for deleted names,
4102 * since any such path was computed before we
4103 * processed any references and orphanized any
4104 * ancestor inode.
4105 */
4106 if (orphanized_ancestor) {
Filipe Mananaf5962782017-06-22 20:03:51 +01004107 ret = update_ref_path(sctx, cur);
4108 if (ret < 0)
Filipe Mananafdb13882017-06-13 14:13:11 +01004109 goto out;
Filipe Mananafdb13882017-06-13 14:13:11 +01004110 }
Alexander Block1f4692d2012-07-28 10:42:24 +02004111 ret = send_unlink(sctx, cur->full_path);
4112 if (ret < 0)
4113 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004114 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004115 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004116 if (ret < 0)
4117 goto out;
4118 }
Alexander Block31db9f72012-07-25 23:19:24 +02004119 /*
4120 * If the inode is still orphan, unlink the orphan. This may
4121 * happen when a previous inode did overwrite the first ref
4122 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02004123 * inode. Unlinking does not mean that the inode is deleted in
4124 * all cases. There may still be links to this inode in other
4125 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02004126 */
Alexander Block1f4692d2012-07-28 10:42:24 +02004127 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02004128 ret = send_unlink(sctx, valid_path);
4129 if (ret < 0)
4130 goto out;
4131 }
4132 }
4133
4134 /*
4135 * We did collect all parent dirs where cur_inode was once located. We
4136 * now go through all these dirs and check if they are pending for
4137 * deletion and if it's finally possible to perform the rmdir now.
4138 * We also update the inode stats of the parent dirs here.
4139 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004140 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02004141 /*
4142 * In case we had refs into dirs that were not processed yet,
4143 * we don't need to do the utime and rmdir logic for these dirs.
4144 * The dir will be processed later.
4145 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004146 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02004147 continue;
4148
Josef Bacikba5e8f22013-08-16 16:52:55 -04004149 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004150 if (ret < 0)
4151 goto out;
4152
4153 if (ret == inode_state_did_create ||
4154 ret == inode_state_no_change) {
4155 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004156 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004157 if (ret < 0)
4158 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004159 } else if (ret == inode_state_did_delete &&
4160 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004161 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4162 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004163 if (ret < 0)
4164 goto out;
4165 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004166 ret = get_cur_path(sctx, cur->dir,
4167 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004168 if (ret < 0)
4169 goto out;
4170 ret = send_rmdir(sctx, valid_path);
4171 if (ret < 0)
4172 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004173 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004174 }
4175 }
4176 }
4177
Alexander Block31db9f72012-07-25 23:19:24 +02004178 ret = 0;
4179
4180out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004181 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004182 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004183 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004184 return ret;
4185}
4186
Nikolay Borisova0357512017-08-21 12:43:44 +03004187static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4188 void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004189{
4190 int ret = 0;
4191 struct send_ctx *sctx = ctx;
4192 struct fs_path *p;
4193 u64 gen;
4194
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004195 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004196 if (!p)
4197 return -ENOMEM;
4198
Liu Boa4d96d62014-03-03 21:31:03 +08004199 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004200 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004201 if (ret < 0)
4202 goto out;
4203
Alexander Block31db9f72012-07-25 23:19:24 +02004204 ret = get_cur_path(sctx, dir, gen, p);
4205 if (ret < 0)
4206 goto out;
4207 ret = fs_path_add_path(p, name);
4208 if (ret < 0)
4209 goto out;
4210
Liu Boa4d96d62014-03-03 21:31:03 +08004211 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004212
4213out:
4214 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004215 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004216 return ret;
4217}
4218
Liu Boa4d96d62014-03-03 21:31:03 +08004219static int __record_new_ref(int num, u64 dir, int index,
4220 struct fs_path *name,
4221 void *ctx)
4222{
4223 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004224 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
Liu Boa4d96d62014-03-03 21:31:03 +08004225}
4226
4227
Alexander Block31db9f72012-07-25 23:19:24 +02004228static int __record_deleted_ref(int num, u64 dir, int index,
4229 struct fs_path *name,
4230 void *ctx)
4231{
Alexander Block31db9f72012-07-25 23:19:24 +02004232 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004233 return record_ref(sctx->parent_root, dir, name, ctx,
4234 &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004235}
4236
4237static int record_new_ref(struct send_ctx *sctx)
4238{
4239 int ret;
4240
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004241 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4242 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004243 if (ret < 0)
4244 goto out;
4245 ret = 0;
4246
4247out:
4248 return ret;
4249}
4250
4251static int record_deleted_ref(struct send_ctx *sctx)
4252{
4253 int ret;
4254
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004255 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4256 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004257 if (ret < 0)
4258 goto out;
4259 ret = 0;
4260
4261out:
4262 return ret;
4263}
4264
4265struct find_ref_ctx {
4266 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004267 u64 dir_gen;
4268 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004269 struct fs_path *name;
4270 int found_idx;
4271};
4272
4273static int __find_iref(int num, u64 dir, int index,
4274 struct fs_path *name,
4275 void *ctx_)
4276{
4277 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004278 u64 dir_gen;
4279 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004280
4281 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4282 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004283 /*
4284 * To avoid doing extra lookups we'll only do this if everything
4285 * else matches.
4286 */
4287 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4288 NULL, NULL, NULL);
4289 if (ret)
4290 return ret;
4291 if (dir_gen != ctx->dir_gen)
4292 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004293 ctx->found_idx = num;
4294 return 1;
4295 }
4296 return 0;
4297}
4298
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004299static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004300 struct btrfs_path *path,
4301 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004302 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004303{
4304 int ret;
4305 struct find_ref_ctx ctx;
4306
4307 ctx.dir = dir;
4308 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004309 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004310 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004311 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004312
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004313 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004314 if (ret < 0)
4315 return ret;
4316
4317 if (ctx.found_idx == -1)
4318 return -ENOENT;
4319
4320 return ctx.found_idx;
4321}
4322
4323static int __record_changed_new_ref(int num, u64 dir, int index,
4324 struct fs_path *name,
4325 void *ctx)
4326{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004327 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004328 int ret;
4329 struct send_ctx *sctx = ctx;
4330
Josef Bacikba5e8f22013-08-16 16:52:55 -04004331 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4332 NULL, NULL, NULL);
4333 if (ret)
4334 return ret;
4335
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004336 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004337 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004338 if (ret == -ENOENT)
4339 ret = __record_new_ref(num, dir, index, name, sctx);
4340 else if (ret > 0)
4341 ret = 0;
4342
4343 return ret;
4344}
4345
4346static int __record_changed_deleted_ref(int num, u64 dir, int index,
4347 struct fs_path *name,
4348 void *ctx)
4349{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004350 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004351 int ret;
4352 struct send_ctx *sctx = ctx;
4353
Josef Bacikba5e8f22013-08-16 16:52:55 -04004354 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4355 NULL, NULL, NULL);
4356 if (ret)
4357 return ret;
4358
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004359 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004360 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004361 if (ret == -ENOENT)
4362 ret = __record_deleted_ref(num, dir, index, name, sctx);
4363 else if (ret > 0)
4364 ret = 0;
4365
4366 return ret;
4367}
4368
4369static int record_changed_ref(struct send_ctx *sctx)
4370{
4371 int ret = 0;
4372
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004373 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004374 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4375 if (ret < 0)
4376 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004377 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004378 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4379 if (ret < 0)
4380 goto out;
4381 ret = 0;
4382
4383out:
4384 return ret;
4385}
4386
4387/*
4388 * Record and process all refs at once. Needed when an inode changes the
4389 * generation number, which means that it was deleted and recreated.
4390 */
4391static int process_all_refs(struct send_ctx *sctx,
4392 enum btrfs_compare_tree_result cmd)
4393{
4394 int ret;
4395 struct btrfs_root *root;
4396 struct btrfs_path *path;
4397 struct btrfs_key key;
4398 struct btrfs_key found_key;
4399 struct extent_buffer *eb;
4400 int slot;
4401 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004402 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004403
4404 path = alloc_path_for_send();
4405 if (!path)
4406 return -ENOMEM;
4407
4408 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4409 root = sctx->send_root;
4410 cb = __record_new_ref;
4411 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4412 root = sctx->parent_root;
4413 cb = __record_deleted_ref;
4414 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004415 btrfs_err(sctx->send_root->fs_info,
4416 "Wrong command %d in process_all_refs", cmd);
4417 ret = -EINVAL;
4418 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004419 }
4420
4421 key.objectid = sctx->cmp_key->objectid;
4422 key.type = BTRFS_INODE_REF_KEY;
4423 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4425 if (ret < 0)
4426 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004427
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004428 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004429 eb = path->nodes[0];
4430 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004431 if (slot >= btrfs_header_nritems(eb)) {
4432 ret = btrfs_next_leaf(root, path);
4433 if (ret < 0)
4434 goto out;
4435 else if (ret > 0)
4436 break;
4437 continue;
4438 }
4439
Alexander Block31db9f72012-07-25 23:19:24 +02004440 btrfs_item_key_to_cpu(eb, &found_key, slot);
4441
4442 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004443 (found_key.type != BTRFS_INODE_REF_KEY &&
4444 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004445 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004446
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004447 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004448 if (ret < 0)
4449 goto out;
4450
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004451 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004452 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004453 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004454
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004455 /*
4456 * We don't actually care about pending_move as we are simply
4457 * re-creating this inode and will be rename'ing it into place once we
4458 * rename the parent directory.
4459 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004460 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004461out:
4462 btrfs_free_path(path);
4463 return ret;
4464}
4465
4466static int send_set_xattr(struct send_ctx *sctx,
4467 struct fs_path *path,
4468 const char *name, int name_len,
4469 const char *data, int data_len)
4470{
4471 int ret = 0;
4472
4473 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4474 if (ret < 0)
4475 goto out;
4476
4477 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4478 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4479 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4480
4481 ret = send_cmd(sctx);
4482
4483tlv_put_failure:
4484out:
4485 return ret;
4486}
4487
4488static int send_remove_xattr(struct send_ctx *sctx,
4489 struct fs_path *path,
4490 const char *name, int name_len)
4491{
4492 int ret = 0;
4493
4494 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4495 if (ret < 0)
4496 goto out;
4497
4498 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4499 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4500
4501 ret = send_cmd(sctx);
4502
4503tlv_put_failure:
4504out:
4505 return ret;
4506}
4507
4508static int __process_new_xattr(int num, struct btrfs_key *di_key,
4509 const char *name, int name_len,
4510 const char *data, int data_len,
4511 u8 type, void *ctx)
4512{
4513 int ret;
4514 struct send_ctx *sctx = ctx;
4515 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004516 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004517
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004518 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004519 if (!p)
4520 return -ENOMEM;
4521
4522 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004523 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004524 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004525 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004526 * only contains the version number and no entries.
4527 */
4528 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4529 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4530 if (data_len == 0) {
4531 dummy_acl.a_version =
4532 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4533 data = (char *)&dummy_acl;
4534 data_len = sizeof(dummy_acl);
4535 }
4536 }
4537
4538 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4539 if (ret < 0)
4540 goto out;
4541
4542 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4543
4544out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004545 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004546 return ret;
4547}
4548
4549static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4550 const char *name, int name_len,
4551 const char *data, int data_len,
4552 u8 type, void *ctx)
4553{
4554 int ret;
4555 struct send_ctx *sctx = ctx;
4556 struct fs_path *p;
4557
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004558 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004559 if (!p)
4560 return -ENOMEM;
4561
4562 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4563 if (ret < 0)
4564 goto out;
4565
4566 ret = send_remove_xattr(sctx, p, name, name_len);
4567
4568out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004569 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004570 return ret;
4571}
4572
4573static int process_new_xattr(struct send_ctx *sctx)
4574{
4575 int ret = 0;
4576
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004577 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004578 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004579
4580 return ret;
4581}
4582
4583static int process_deleted_xattr(struct send_ctx *sctx)
4584{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004585 return iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004586 __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004587}
4588
4589struct find_xattr_ctx {
4590 const char *name;
4591 int name_len;
4592 int found_idx;
4593 char *found_data;
4594 int found_data_len;
4595};
4596
4597static int __find_xattr(int num, struct btrfs_key *di_key,
4598 const char *name, int name_len,
4599 const char *data, int data_len,
4600 u8 type, void *vctx)
4601{
4602 struct find_xattr_ctx *ctx = vctx;
4603
4604 if (name_len == ctx->name_len &&
4605 strncmp(name, ctx->name, name_len) == 0) {
4606 ctx->found_idx = num;
4607 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004608 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004609 if (!ctx->found_data)
4610 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004611 return 1;
4612 }
4613 return 0;
4614}
4615
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004616static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004617 struct btrfs_path *path,
4618 struct btrfs_key *key,
4619 const char *name, int name_len,
4620 char **data, int *data_len)
4621{
4622 int ret;
4623 struct find_xattr_ctx ctx;
4624
4625 ctx.name = name;
4626 ctx.name_len = name_len;
4627 ctx.found_idx = -1;
4628 ctx.found_data = NULL;
4629 ctx.found_data_len = 0;
4630
Nikolay Borisova0357512017-08-21 12:43:44 +03004631 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004632 if (ret < 0)
4633 return ret;
4634
4635 if (ctx.found_idx == -1)
4636 return -ENOENT;
4637 if (data) {
4638 *data = ctx.found_data;
4639 *data_len = ctx.found_data_len;
4640 } else {
4641 kfree(ctx.found_data);
4642 }
4643 return ctx.found_idx;
4644}
4645
4646
4647static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4648 const char *name, int name_len,
4649 const char *data, int data_len,
4650 u8 type, void *ctx)
4651{
4652 int ret;
4653 struct send_ctx *sctx = ctx;
4654 char *found_data = NULL;
4655 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004656
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004657 ret = find_xattr(sctx->parent_root, sctx->right_path,
4658 sctx->cmp_key, name, name_len, &found_data,
4659 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004660 if (ret == -ENOENT) {
4661 ret = __process_new_xattr(num, di_key, name, name_len, data,
4662 data_len, type, ctx);
4663 } else if (ret >= 0) {
4664 if (data_len != found_data_len ||
4665 memcmp(data, found_data, data_len)) {
4666 ret = __process_new_xattr(num, di_key, name, name_len,
4667 data, data_len, type, ctx);
4668 } else {
4669 ret = 0;
4670 }
4671 }
4672
4673 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004674 return ret;
4675}
4676
4677static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4678 const char *name, int name_len,
4679 const char *data, int data_len,
4680 u8 type, void *ctx)
4681{
4682 int ret;
4683 struct send_ctx *sctx = ctx;
4684
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004685 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4686 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004687 if (ret == -ENOENT)
4688 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4689 data_len, type, ctx);
4690 else if (ret >= 0)
4691 ret = 0;
4692
4693 return ret;
4694}
4695
4696static int process_changed_xattr(struct send_ctx *sctx)
4697{
4698 int ret = 0;
4699
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004700 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004701 __process_changed_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004702 if (ret < 0)
4703 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004704 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004705 __process_changed_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004706
4707out:
4708 return ret;
4709}
4710
4711static int process_all_new_xattrs(struct send_ctx *sctx)
4712{
4713 int ret;
4714 struct btrfs_root *root;
4715 struct btrfs_path *path;
4716 struct btrfs_key key;
4717 struct btrfs_key found_key;
4718 struct extent_buffer *eb;
4719 int slot;
4720
4721 path = alloc_path_for_send();
4722 if (!path)
4723 return -ENOMEM;
4724
4725 root = sctx->send_root;
4726
4727 key.objectid = sctx->cmp_key->objectid;
4728 key.type = BTRFS_XATTR_ITEM_KEY;
4729 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004730 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4731 if (ret < 0)
4732 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004733
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004734 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004735 eb = path->nodes[0];
4736 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004737 if (slot >= btrfs_header_nritems(eb)) {
4738 ret = btrfs_next_leaf(root, path);
4739 if (ret < 0) {
4740 goto out;
4741 } else if (ret > 0) {
4742 ret = 0;
4743 break;
4744 }
4745 continue;
4746 }
Alexander Block31db9f72012-07-25 23:19:24 +02004747
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004748 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004749 if (found_key.objectid != key.objectid ||
4750 found_key.type != key.type) {
4751 ret = 0;
4752 goto out;
4753 }
4754
Nikolay Borisova0357512017-08-21 12:43:44 +03004755 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004756 if (ret < 0)
4757 goto out;
4758
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004759 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004760 }
4761
4762out:
4763 btrfs_free_path(path);
4764 return ret;
4765}
4766
Josef Baciked259092013-10-25 11:36:01 -04004767static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4768{
4769 struct btrfs_root *root = sctx->send_root;
4770 struct btrfs_fs_info *fs_info = root->fs_info;
4771 struct inode *inode;
4772 struct page *page;
4773 char *addr;
4774 struct btrfs_key key;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004775 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004776 pgoff_t last_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004777 unsigned pg_offset = offset & ~PAGE_MASK;
Josef Baciked259092013-10-25 11:36:01 -04004778 ssize_t ret = 0;
4779
4780 key.objectid = sctx->cur_ino;
4781 key.type = BTRFS_INODE_ITEM_KEY;
4782 key.offset = 0;
4783
4784 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4785 if (IS_ERR(inode))
4786 return PTR_ERR(inode);
4787
4788 if (offset + len > i_size_read(inode)) {
4789 if (offset > i_size_read(inode))
4790 len = 0;
4791 else
4792 len = offset - i_size_read(inode);
4793 }
4794 if (len == 0)
4795 goto out;
4796
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004797 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004798
4799 /* initial readahead */
4800 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4801 file_ra_state_init(&sctx->ra, inode->i_mapping);
Liu Bo2131bcd2014-03-05 10:07:35 +08004802
Josef Baciked259092013-10-25 11:36:01 -04004803 while (index <= last_index) {
4804 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004805 PAGE_SIZE - pg_offset);
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004806
4807 page = find_lock_page(inode->i_mapping, index);
Josef Baciked259092013-10-25 11:36:01 -04004808 if (!page) {
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004809 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4810 NULL, index, last_index + 1 - index);
4811
4812 page = find_or_create_page(inode->i_mapping, index,
4813 GFP_KERNEL);
4814 if (!page) {
4815 ret = -ENOMEM;
4816 break;
4817 }
4818 }
4819
4820 if (PageReadahead(page)) {
4821 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4822 NULL, page, index, last_index + 1 - index);
Josef Baciked259092013-10-25 11:36:01 -04004823 }
4824
4825 if (!PageUptodate(page)) {
4826 btrfs_readpage(NULL, page);
4827 lock_page(page);
4828 if (!PageUptodate(page)) {
4829 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004830 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004831 ret = -EIO;
4832 break;
4833 }
4834 }
4835
4836 addr = kmap(page);
4837 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4838 kunmap(page);
4839 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004840 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004841 index++;
4842 pg_offset = 0;
4843 len -= cur_len;
4844 ret += cur_len;
4845 }
4846out:
4847 iput(inode);
4848 return ret;
4849}
4850
Alexander Block31db9f72012-07-25 23:19:24 +02004851/*
4852 * Read some bytes from the current inode/file and send a write command to
4853 * user space.
4854 */
4855static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4856{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004857 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02004858 int ret = 0;
4859 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004860 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004861
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004862 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004863 if (!p)
4864 return -ENOMEM;
4865
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004866 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004867
Josef Baciked259092013-10-25 11:36:01 -04004868 num_read = fill_read_buf(sctx, offset, len);
4869 if (num_read <= 0) {
4870 if (num_read < 0)
4871 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004872 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004873 }
Alexander Block31db9f72012-07-25 23:19:24 +02004874
4875 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4876 if (ret < 0)
4877 goto out;
4878
4879 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4880 if (ret < 0)
4881 goto out;
4882
4883 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4884 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004885 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004886
4887 ret = send_cmd(sctx);
4888
4889tlv_put_failure:
4890out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004891 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004892 if (ret < 0)
4893 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004894 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004895}
4896
4897/*
4898 * Send a clone command to user space.
4899 */
4900static int send_clone(struct send_ctx *sctx,
4901 u64 offset, u32 len,
4902 struct clone_root *clone_root)
4903{
4904 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004905 struct fs_path *p;
4906 u64 gen;
4907
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004908 btrfs_debug(sctx->send_root->fs_info,
4909 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4910 offset, len, clone_root->root->objectid, clone_root->ino,
4911 clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02004912
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004913 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004914 if (!p)
4915 return -ENOMEM;
4916
4917 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4918 if (ret < 0)
4919 goto out;
4920
4921 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4922 if (ret < 0)
4923 goto out;
4924
4925 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4926 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4927 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4928
Alexander Blocke938c8a2012-07-28 16:33:49 +02004929 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004930 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004931 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004932 if (ret < 0)
4933 goto out;
4934 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4935 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004936 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004937 }
4938 if (ret < 0)
4939 goto out;
4940
Josef Bacik37b8d272015-06-04 17:17:25 -04004941 /*
4942 * If the parent we're using has a received_uuid set then use that as
4943 * our clone source as that is what we will look for when doing a
4944 * receive.
4945 *
4946 * This covers the case that we create a snapshot off of a received
4947 * subvolume and then use that as the parent and try to receive on a
4948 * different host.
4949 */
4950 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4951 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4952 clone_root->root->root_item.received_uuid);
4953 else
4954 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4955 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004956 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004957 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004958 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4959 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4960 clone_root->offset);
4961
4962 ret = send_cmd(sctx);
4963
4964tlv_put_failure:
4965out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004966 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004967 return ret;
4968}
4969
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004970/*
4971 * Send an update extent command to user space.
4972 */
4973static int send_update_extent(struct send_ctx *sctx,
4974 u64 offset, u32 len)
4975{
4976 int ret = 0;
4977 struct fs_path *p;
4978
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004979 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004980 if (!p)
4981 return -ENOMEM;
4982
4983 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4984 if (ret < 0)
4985 goto out;
4986
4987 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4988 if (ret < 0)
4989 goto out;
4990
4991 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4992 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4993 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4994
4995 ret = send_cmd(sctx);
4996
4997tlv_put_failure:
4998out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004999 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005000 return ret;
5001}
5002
Josef Bacik16e75492013-10-22 12:18:51 -04005003static int send_hole(struct send_ctx *sctx, u64 end)
5004{
5005 struct fs_path *p = NULL;
5006 u64 offset = sctx->cur_inode_last_extent;
5007 u64 len;
5008 int ret = 0;
5009
Filipe Mananad4dfc0f2018-02-06 20:39:20 +00005010 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5011 return send_update_extent(sctx, offset, end - offset);
5012
Josef Bacik16e75492013-10-22 12:18:51 -04005013 p = fs_path_alloc();
5014 if (!p)
5015 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01005016 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5017 if (ret < 0)
5018 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04005019 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
5020 while (offset < end) {
5021 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
5022
5023 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5024 if (ret < 0)
5025 break;
Josef Bacik16e75492013-10-22 12:18:51 -04005026 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5027 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5028 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
5029 ret = send_cmd(sctx);
5030 if (ret < 0)
5031 break;
5032 offset += len;
5033 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005034 sctx->cur_inode_next_write_offset = offset;
Josef Bacik16e75492013-10-22 12:18:51 -04005035tlv_put_failure:
5036 fs_path_free(p);
5037 return ret;
5038}
5039
Filipe Mananad906d492015-10-02 10:47:34 +01005040static int send_extent_data(struct send_ctx *sctx,
5041 const u64 offset,
5042 const u64 len)
5043{
5044 u64 sent = 0;
5045
5046 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5047 return send_update_extent(sctx, offset, len);
5048
5049 while (sent < len) {
5050 u64 size = len - sent;
5051 int ret;
5052
5053 if (size > BTRFS_SEND_READ_SIZE)
5054 size = BTRFS_SEND_READ_SIZE;
5055 ret = send_write(sctx, offset + sent, size);
5056 if (ret < 0)
5057 return ret;
5058 if (!ret)
5059 break;
5060 sent += ret;
5061 }
5062 return 0;
5063}
5064
5065static int clone_range(struct send_ctx *sctx,
5066 struct clone_root *clone_root,
5067 const u64 disk_byte,
5068 u64 data_offset,
5069 u64 offset,
5070 u64 len)
5071{
5072 struct btrfs_path *path;
5073 struct btrfs_key key;
5074 int ret;
5075
Filipe Manana72610b12017-08-10 22:54:51 +01005076 /*
5077 * Prevent cloning from a zero offset with a length matching the sector
5078 * size because in some scenarios this will make the receiver fail.
5079 *
5080 * For example, if in the source filesystem the extent at offset 0
5081 * has a length of sectorsize and it was written using direct IO, then
5082 * it can never be an inline extent (even if compression is enabled).
5083 * Then this extent can be cloned in the original filesystem to a non
5084 * zero file offset, but it may not be possible to clone in the
5085 * destination filesystem because it can be inlined due to compression
5086 * on the destination filesystem (as the receiver's write operations are
5087 * always done using buffered IO). The same happens when the original
5088 * filesystem does not have compression enabled but the destination
5089 * filesystem has.
5090 */
5091 if (clone_root->offset == 0 &&
5092 len == sctx->send_root->fs_info->sectorsize)
5093 return send_extent_data(sctx, offset, len);
5094
Filipe Mananad906d492015-10-02 10:47:34 +01005095 path = alloc_path_for_send();
5096 if (!path)
5097 return -ENOMEM;
5098
5099 /*
5100 * We can't send a clone operation for the entire range if we find
5101 * extent items in the respective range in the source file that
5102 * refer to different extents or if we find holes.
5103 * So check for that and do a mix of clone and regular write/copy
5104 * operations if needed.
5105 *
5106 * Example:
5107 *
5108 * mkfs.btrfs -f /dev/sda
5109 * mount /dev/sda /mnt
5110 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5111 * cp --reflink=always /mnt/foo /mnt/bar
5112 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5113 * btrfs subvolume snapshot -r /mnt /mnt/snap
5114 *
5115 * If when we send the snapshot and we are processing file bar (which
5116 * has a higher inode number than foo) we blindly send a clone operation
5117 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5118 * a file bar that matches the content of file foo - iow, doesn't match
5119 * the content from bar in the original filesystem.
5120 */
5121 key.objectid = clone_root->ino;
5122 key.type = BTRFS_EXTENT_DATA_KEY;
5123 key.offset = clone_root->offset;
5124 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5125 if (ret < 0)
5126 goto out;
5127 if (ret > 0 && path->slots[0] > 0) {
5128 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5129 if (key.objectid == clone_root->ino &&
5130 key.type == BTRFS_EXTENT_DATA_KEY)
5131 path->slots[0]--;
5132 }
5133
5134 while (true) {
5135 struct extent_buffer *leaf = path->nodes[0];
5136 int slot = path->slots[0];
5137 struct btrfs_file_extent_item *ei;
5138 u8 type;
5139 u64 ext_len;
5140 u64 clone_len;
5141
5142 if (slot >= btrfs_header_nritems(leaf)) {
5143 ret = btrfs_next_leaf(clone_root->root, path);
5144 if (ret < 0)
5145 goto out;
5146 else if (ret > 0)
5147 break;
5148 continue;
5149 }
5150
5151 btrfs_item_key_to_cpu(leaf, &key, slot);
5152
5153 /*
5154 * We might have an implicit trailing hole (NO_HOLES feature
5155 * enabled). We deal with it after leaving this loop.
5156 */
5157 if (key.objectid != clone_root->ino ||
5158 key.type != BTRFS_EXTENT_DATA_KEY)
5159 break;
5160
5161 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5162 type = btrfs_file_extent_type(leaf, ei);
5163 if (type == BTRFS_FILE_EXTENT_INLINE) {
5164 ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005165 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01005166 } else {
5167 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5168 }
5169
5170 if (key.offset + ext_len <= clone_root->offset)
5171 goto next;
5172
5173 if (key.offset > clone_root->offset) {
5174 /* Implicit hole, NO_HOLES feature enabled. */
5175 u64 hole_len = key.offset - clone_root->offset;
5176
5177 if (hole_len > len)
5178 hole_len = len;
5179 ret = send_extent_data(sctx, offset, hole_len);
5180 if (ret < 0)
5181 goto out;
5182
5183 len -= hole_len;
5184 if (len == 0)
5185 break;
5186 offset += hole_len;
5187 clone_root->offset += hole_len;
5188 data_offset += hole_len;
5189 }
5190
5191 if (key.offset >= clone_root->offset + len)
5192 break;
5193
5194 clone_len = min_t(u64, ext_len, len);
5195
5196 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5197 btrfs_file_extent_offset(leaf, ei) == data_offset)
5198 ret = send_clone(sctx, offset, clone_len, clone_root);
5199 else
5200 ret = send_extent_data(sctx, offset, clone_len);
5201
5202 if (ret < 0)
5203 goto out;
5204
5205 len -= clone_len;
5206 if (len == 0)
5207 break;
5208 offset += clone_len;
5209 clone_root->offset += clone_len;
5210 data_offset += clone_len;
5211next:
5212 path->slots[0]++;
5213 }
5214
5215 if (len > 0)
5216 ret = send_extent_data(sctx, offset, len);
5217 else
5218 ret = 0;
5219out:
5220 btrfs_free_path(path);
5221 return ret;
5222}
5223
Alexander Block31db9f72012-07-25 23:19:24 +02005224static int send_write_or_clone(struct send_ctx *sctx,
5225 struct btrfs_path *path,
5226 struct btrfs_key *key,
5227 struct clone_root *clone_root)
5228{
5229 int ret = 0;
5230 struct btrfs_file_extent_item *ei;
5231 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02005232 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02005233 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005234 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005235
5236 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5237 struct btrfs_file_extent_item);
5238 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005239 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005240 len = btrfs_file_extent_inline_len(path->nodes[0],
5241 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005242 /*
5243 * it is possible the inline item won't cover the whole page,
5244 * but there may be items after this page. Make
5245 * sure to send the whole thing
5246 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005247 len = PAGE_ALIGN(len);
Chris Mason74dd17f2012-08-07 16:25:13 -04005248 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02005249 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005250 }
Alexander Block31db9f72012-07-25 23:19:24 +02005251
5252 if (offset + len > sctx->cur_inode_size)
5253 len = sctx->cur_inode_size - offset;
5254 if (len == 0) {
5255 ret = 0;
5256 goto out;
5257 }
5258
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005259 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01005260 u64 disk_byte;
5261 u64 data_offset;
5262
5263 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5264 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5265 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5266 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005267 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005268 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005269 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005270 sctx->cur_inode_next_write_offset = offset + len;
Alexander Block31db9f72012-07-25 23:19:24 +02005271out:
5272 return ret;
5273}
5274
5275static int is_extent_unchanged(struct send_ctx *sctx,
5276 struct btrfs_path *left_path,
5277 struct btrfs_key *ekey)
5278{
5279 int ret = 0;
5280 struct btrfs_key key;
5281 struct btrfs_path *path = NULL;
5282 struct extent_buffer *eb;
5283 int slot;
5284 struct btrfs_key found_key;
5285 struct btrfs_file_extent_item *ei;
5286 u64 left_disknr;
5287 u64 right_disknr;
5288 u64 left_offset;
5289 u64 right_offset;
5290 u64 left_offset_fixed;
5291 u64 left_len;
5292 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005293 u64 left_gen;
5294 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005295 u8 left_type;
5296 u8 right_type;
5297
5298 path = alloc_path_for_send();
5299 if (!path)
5300 return -ENOMEM;
5301
5302 eb = left_path->nodes[0];
5303 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005304 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5305 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005306
5307 if (left_type != BTRFS_FILE_EXTENT_REG) {
5308 ret = 0;
5309 goto out;
5310 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005311 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5312 left_len = btrfs_file_extent_num_bytes(eb, ei);
5313 left_offset = btrfs_file_extent_offset(eb, ei);
5314 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005315
5316 /*
5317 * Following comments will refer to these graphics. L is the left
5318 * extents which we are checking at the moment. 1-8 are the right
5319 * extents that we iterate.
5320 *
5321 * |-----L-----|
5322 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5323 *
5324 * |-----L-----|
5325 * |--1--|-2b-|...(same as above)
5326 *
5327 * Alternative situation. Happens on files where extents got split.
5328 * |-----L-----|
5329 * |-----------7-----------|-6-|
5330 *
5331 * Alternative situation. Happens on files which got larger.
5332 * |-----L-----|
5333 * |-8-|
5334 * Nothing follows after 8.
5335 */
5336
5337 key.objectid = ekey->objectid;
5338 key.type = BTRFS_EXTENT_DATA_KEY;
5339 key.offset = ekey->offset;
5340 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5341 if (ret < 0)
5342 goto out;
5343 if (ret) {
5344 ret = 0;
5345 goto out;
5346 }
5347
5348 /*
5349 * Handle special case where the right side has no extents at all.
5350 */
5351 eb = path->nodes[0];
5352 slot = path->slots[0];
5353 btrfs_item_key_to_cpu(eb, &found_key, slot);
5354 if (found_key.objectid != key.objectid ||
5355 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005356 /* If we're a hole then just pretend nothing changed */
5357 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005358 goto out;
5359 }
5360
5361 /*
5362 * We're now on 2a, 2b or 7.
5363 */
5364 key = found_key;
5365 while (key.offset < ekey->offset + left_len) {
5366 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5367 right_type = btrfs_file_extent_type(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005368 if (right_type != BTRFS_FILE_EXTENT_REG &&
5369 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005370 ret = 0;
5371 goto out;
5372 }
5373
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005374 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5375 right_len = btrfs_file_extent_inline_len(eb, slot, ei);
5376 right_len = PAGE_ALIGN(right_len);
5377 } else {
5378 right_len = btrfs_file_extent_num_bytes(eb, ei);
5379 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005380
Alexander Block31db9f72012-07-25 23:19:24 +02005381 /*
5382 * Are we at extent 8? If yes, we know the extent is changed.
5383 * This may only happen on the first iteration.
5384 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005385 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005386 /* If we're a hole just pretend nothing changed */
5387 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005388 goto out;
5389 }
5390
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005391 /*
5392 * We just wanted to see if when we have an inline extent, what
5393 * follows it is a regular extent (wanted to check the above
5394 * condition for inline extents too). This should normally not
5395 * happen but it's possible for example when we have an inline
5396 * compressed extent representing data with a size matching
5397 * the page size (currently the same as sector size).
5398 */
5399 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5400 ret = 0;
5401 goto out;
5402 }
5403
Filipe Manana24e52b12017-07-06 15:31:46 +01005404 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5405 right_offset = btrfs_file_extent_offset(eb, ei);
5406 right_gen = btrfs_file_extent_generation(eb, ei);
5407
Alexander Block31db9f72012-07-25 23:19:24 +02005408 left_offset_fixed = left_offset;
5409 if (key.offset < ekey->offset) {
5410 /* Fix the right offset for 2a and 7. */
5411 right_offset += ekey->offset - key.offset;
5412 } else {
5413 /* Fix the left offset for all behind 2a and 2b */
5414 left_offset_fixed += key.offset - ekey->offset;
5415 }
5416
5417 /*
5418 * Check if we have the same extent.
5419 */
Alexander Block39540962012-08-01 12:46:05 +02005420 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005421 left_offset_fixed != right_offset ||
5422 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005423 ret = 0;
5424 goto out;
5425 }
5426
5427 /*
5428 * Go to the next extent.
5429 */
5430 ret = btrfs_next_item(sctx->parent_root, path);
5431 if (ret < 0)
5432 goto out;
5433 if (!ret) {
5434 eb = path->nodes[0];
5435 slot = path->slots[0];
5436 btrfs_item_key_to_cpu(eb, &found_key, slot);
5437 }
5438 if (ret || found_key.objectid != key.objectid ||
5439 found_key.type != key.type) {
5440 key.offset += right_len;
5441 break;
Jan Schmidtadaa4b8e2013-03-21 14:30:23 +00005442 }
5443 if (found_key.offset != key.offset + right_len) {
5444 ret = 0;
5445 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005446 }
5447 key = found_key;
5448 }
5449
5450 /*
5451 * We're now behind the left extent (treat as unchanged) or at the end
5452 * of the right side (treat as changed).
5453 */
5454 if (key.offset >= ekey->offset + left_len)
5455 ret = 1;
5456 else
5457 ret = 0;
5458
5459
5460out:
5461 btrfs_free_path(path);
5462 return ret;
5463}
5464
Josef Bacik16e75492013-10-22 12:18:51 -04005465static int get_last_extent(struct send_ctx *sctx, u64 offset)
5466{
5467 struct btrfs_path *path;
5468 struct btrfs_root *root = sctx->send_root;
5469 struct btrfs_file_extent_item *fi;
5470 struct btrfs_key key;
5471 u64 extent_end;
5472 u8 type;
5473 int ret;
5474
5475 path = alloc_path_for_send();
5476 if (!path)
5477 return -ENOMEM;
5478
5479 sctx->cur_inode_last_extent = 0;
5480
5481 key.objectid = sctx->cur_ino;
5482 key.type = BTRFS_EXTENT_DATA_KEY;
5483 key.offset = offset;
5484 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5485 if (ret < 0)
5486 goto out;
5487 ret = 0;
5488 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5489 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5490 goto out;
5491
5492 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5493 struct btrfs_file_extent_item);
5494 type = btrfs_file_extent_type(path->nodes[0], fi);
5495 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005496 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5497 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005498 extent_end = ALIGN(key.offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005499 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005500 } else {
5501 extent_end = key.offset +
5502 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5503 }
5504 sctx->cur_inode_last_extent = extent_end;
5505out:
5506 btrfs_free_path(path);
5507 return ret;
5508}
5509
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005510static int range_is_hole_in_parent(struct send_ctx *sctx,
5511 const u64 start,
5512 const u64 end)
5513{
5514 struct btrfs_path *path;
5515 struct btrfs_key key;
5516 struct btrfs_root *root = sctx->parent_root;
5517 u64 search_start = start;
5518 int ret;
5519
5520 path = alloc_path_for_send();
5521 if (!path)
5522 return -ENOMEM;
5523
5524 key.objectid = sctx->cur_ino;
5525 key.type = BTRFS_EXTENT_DATA_KEY;
5526 key.offset = search_start;
5527 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5528 if (ret < 0)
5529 goto out;
5530 if (ret > 0 && path->slots[0] > 0)
5531 path->slots[0]--;
5532
5533 while (search_start < end) {
5534 struct extent_buffer *leaf = path->nodes[0];
5535 int slot = path->slots[0];
5536 struct btrfs_file_extent_item *fi;
5537 u64 extent_end;
5538
5539 if (slot >= btrfs_header_nritems(leaf)) {
5540 ret = btrfs_next_leaf(root, path);
5541 if (ret < 0)
5542 goto out;
5543 else if (ret > 0)
5544 break;
5545 continue;
5546 }
5547
5548 btrfs_item_key_to_cpu(leaf, &key, slot);
5549 if (key.objectid < sctx->cur_ino ||
5550 key.type < BTRFS_EXTENT_DATA_KEY)
5551 goto next;
5552 if (key.objectid > sctx->cur_ino ||
5553 key.type > BTRFS_EXTENT_DATA_KEY ||
5554 key.offset >= end)
5555 break;
5556
5557 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5558 if (btrfs_file_extent_type(leaf, fi) ==
5559 BTRFS_FILE_EXTENT_INLINE) {
5560 u64 size = btrfs_file_extent_inline_len(leaf, slot, fi);
5561
5562 extent_end = ALIGN(key.offset + size,
5563 root->fs_info->sectorsize);
5564 } else {
5565 extent_end = key.offset +
5566 btrfs_file_extent_num_bytes(leaf, fi);
5567 }
5568 if (extent_end <= start)
5569 goto next;
5570 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5571 search_start = extent_end;
5572 goto next;
5573 }
5574 ret = 0;
5575 goto out;
5576next:
5577 path->slots[0]++;
5578 }
5579 ret = 1;
5580out:
5581 btrfs_free_path(path);
5582 return ret;
5583}
5584
Josef Bacik16e75492013-10-22 12:18:51 -04005585static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5586 struct btrfs_key *key)
5587{
5588 struct btrfs_file_extent_item *fi;
5589 u64 extent_end;
5590 u8 type;
5591 int ret = 0;
5592
5593 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5594 return 0;
5595
5596 if (sctx->cur_inode_last_extent == (u64)-1) {
5597 ret = get_last_extent(sctx, key->offset - 1);
5598 if (ret)
5599 return ret;
5600 }
5601
5602 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5603 struct btrfs_file_extent_item);
5604 type = btrfs_file_extent_type(path->nodes[0], fi);
5605 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005606 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5607 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005608 extent_end = ALIGN(key->offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005609 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005610 } else {
5611 extent_end = key->offset +
5612 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5613 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005614
5615 if (path->slots[0] == 0 &&
5616 sctx->cur_inode_last_extent < key->offset) {
5617 /*
5618 * We might have skipped entire leafs that contained only
5619 * file extent items for our current inode. These leafs have
5620 * a generation number smaller (older) than the one in the
5621 * current leaf and the leaf our last extent came from, and
5622 * are located between these 2 leafs.
5623 */
5624 ret = get_last_extent(sctx, key->offset - 1);
5625 if (ret)
5626 return ret;
5627 }
5628
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005629 if (sctx->cur_inode_last_extent < key->offset) {
5630 ret = range_is_hole_in_parent(sctx,
5631 sctx->cur_inode_last_extent,
5632 key->offset);
5633 if (ret < 0)
5634 return ret;
5635 else if (ret == 0)
5636 ret = send_hole(sctx, key->offset);
5637 else
5638 ret = 0;
5639 }
Josef Bacik16e75492013-10-22 12:18:51 -04005640 sctx->cur_inode_last_extent = extent_end;
5641 return ret;
5642}
5643
Alexander Block31db9f72012-07-25 23:19:24 +02005644static int process_extent(struct send_ctx *sctx,
5645 struct btrfs_path *path,
5646 struct btrfs_key *key)
5647{
Alexander Block31db9f72012-07-25 23:19:24 +02005648 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005649 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005650
5651 if (S_ISLNK(sctx->cur_inode_mode))
5652 return 0;
5653
5654 if (sctx->parent_root && !sctx->cur_inode_new) {
5655 ret = is_extent_unchanged(sctx, path, key);
5656 if (ret < 0)
5657 goto out;
5658 if (ret) {
5659 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005660 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005661 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005662 } else {
5663 struct btrfs_file_extent_item *ei;
5664 u8 type;
5665
5666 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5667 struct btrfs_file_extent_item);
5668 type = btrfs_file_extent_type(path->nodes[0], ei);
5669 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5670 type == BTRFS_FILE_EXTENT_REG) {
5671 /*
5672 * The send spec does not have a prealloc command yet,
5673 * so just leave a hole for prealloc'ed extents until
5674 * we have enough commands queued up to justify rev'ing
5675 * the send spec.
5676 */
5677 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5678 ret = 0;
5679 goto out;
5680 }
5681
5682 /* Have a hole, just skip it. */
5683 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5684 ret = 0;
5685 goto out;
5686 }
5687 }
Alexander Block31db9f72012-07-25 23:19:24 +02005688 }
5689
5690 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5691 sctx->cur_inode_size, &found_clone);
5692 if (ret != -ENOENT && ret < 0)
5693 goto out;
5694
5695 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005696 if (ret)
5697 goto out;
5698out_hole:
5699 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005700out:
5701 return ret;
5702}
5703
5704static int process_all_extents(struct send_ctx *sctx)
5705{
5706 int ret;
5707 struct btrfs_root *root;
5708 struct btrfs_path *path;
5709 struct btrfs_key key;
5710 struct btrfs_key found_key;
5711 struct extent_buffer *eb;
5712 int slot;
5713
5714 root = sctx->send_root;
5715 path = alloc_path_for_send();
5716 if (!path)
5717 return -ENOMEM;
5718
5719 key.objectid = sctx->cmp_key->objectid;
5720 key.type = BTRFS_EXTENT_DATA_KEY;
5721 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005722 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5723 if (ret < 0)
5724 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005725
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005726 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005727 eb = path->nodes[0];
5728 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005729
5730 if (slot >= btrfs_header_nritems(eb)) {
5731 ret = btrfs_next_leaf(root, path);
5732 if (ret < 0) {
5733 goto out;
5734 } else if (ret > 0) {
5735 ret = 0;
5736 break;
5737 }
5738 continue;
5739 }
5740
Alexander Block31db9f72012-07-25 23:19:24 +02005741 btrfs_item_key_to_cpu(eb, &found_key, slot);
5742
5743 if (found_key.objectid != key.objectid ||
5744 found_key.type != key.type) {
5745 ret = 0;
5746 goto out;
5747 }
5748
5749 ret = process_extent(sctx, path, &found_key);
5750 if (ret < 0)
5751 goto out;
5752
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005753 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005754 }
5755
5756out:
5757 btrfs_free_path(path);
5758 return ret;
5759}
5760
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005761static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5762 int *pending_move,
5763 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005764{
5765 int ret = 0;
5766
5767 if (sctx->cur_ino == 0)
5768 goto out;
5769 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005770 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005771 goto out;
5772 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5773 goto out;
5774
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005775 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005776 if (ret < 0)
5777 goto out;
5778
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005779 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005780out:
5781 return ret;
5782}
5783
5784static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5785{
5786 int ret = 0;
5787 u64 left_mode;
5788 u64 left_uid;
5789 u64 left_gid;
5790 u64 right_mode;
5791 u64 right_uid;
5792 u64 right_gid;
5793 int need_chmod = 0;
5794 int need_chown = 0;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005795 int need_truncate = 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005796 int pending_move = 0;
5797 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005798
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005799 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5800 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005801 if (ret < 0)
5802 goto out;
5803
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005804 /*
5805 * We have processed the refs and thus need to advance send_progress.
5806 * Now, calls to get_cur_xxx will take the updated refs of the current
5807 * inode into account.
5808 *
5809 * On the other hand, if our current inode is a directory and couldn't
5810 * be moved/renamed because its parent was renamed/moved too and it has
5811 * a higher inode number, we can only move/rename our current inode
5812 * after we moved/renamed its parent. Therefore in this case operate on
5813 * the old path (pre move/rename) of our current inode, and the
5814 * move/rename will be performed later.
5815 */
5816 if (refs_processed && !pending_move)
5817 sctx->send_progress = sctx->cur_ino + 1;
5818
Alexander Block31db9f72012-07-25 23:19:24 +02005819 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5820 goto out;
5821 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5822 goto out;
5823
5824 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005825 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005826 if (ret < 0)
5827 goto out;
5828
Alex Lyakase2d044f2012-10-17 13:52:47 +00005829 if (!sctx->parent_root || sctx->cur_inode_new) {
5830 need_chown = 1;
5831 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005832 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005833 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
5834 need_truncate = 0;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005835 } else {
Filipe Mananaffa7c422018-02-06 20:40:40 +00005836 u64 old_size;
5837
Alex Lyakase2d044f2012-10-17 13:52:47 +00005838 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
Filipe Mananaffa7c422018-02-06 20:40:40 +00005839 &old_size, NULL, &right_mode, &right_uid,
Alex Lyakase2d044f2012-10-17 13:52:47 +00005840 &right_gid, NULL);
5841 if (ret < 0)
5842 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005843
Alex Lyakase2d044f2012-10-17 13:52:47 +00005844 if (left_uid != right_uid || left_gid != right_gid)
5845 need_chown = 1;
5846 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5847 need_chmod = 1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005848 if ((old_size == sctx->cur_inode_size) ||
5849 (sctx->cur_inode_size > old_size &&
5850 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
5851 need_truncate = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005852 }
5853
5854 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005855 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005856 if (sctx->cur_inode_last_extent == (u64)-1 ||
5857 sctx->cur_inode_last_extent <
5858 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005859 ret = get_last_extent(sctx, (u64)-1);
5860 if (ret)
5861 goto out;
5862 }
5863 if (sctx->cur_inode_last_extent <
5864 sctx->cur_inode_size) {
5865 ret = send_hole(sctx, sctx->cur_inode_size);
5866 if (ret)
5867 goto out;
5868 }
5869 }
Filipe Mananaffa7c422018-02-06 20:40:40 +00005870 if (need_truncate) {
5871 ret = send_truncate(sctx, sctx->cur_ino,
5872 sctx->cur_inode_gen,
5873 sctx->cur_inode_size);
5874 if (ret < 0)
5875 goto out;
5876 }
Alexander Block31db9f72012-07-25 23:19:24 +02005877 }
5878
5879 if (need_chown) {
5880 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5881 left_uid, left_gid);
5882 if (ret < 0)
5883 goto out;
5884 }
5885 if (need_chmod) {
5886 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5887 left_mode);
5888 if (ret < 0)
5889 goto out;
5890 }
5891
5892 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005893 * If other directory inodes depended on our current directory
5894 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005895 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005896 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5897 ret = apply_children_dir_moves(sctx);
5898 if (ret)
5899 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005900 /*
5901 * Need to send that every time, no matter if it actually
5902 * changed between the two trees as we have done changes to
5903 * the inode before. If our inode is a directory and it's
5904 * waiting to be moved/renamed, we will send its utimes when
5905 * it's moved/renamed, therefore we don't need to do it here.
5906 */
5907 sctx->send_progress = sctx->cur_ino + 1;
5908 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5909 if (ret < 0)
5910 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005911 }
5912
Alexander Block31db9f72012-07-25 23:19:24 +02005913out:
5914 return ret;
5915}
5916
5917static int changed_inode(struct send_ctx *sctx,
5918 enum btrfs_compare_tree_result result)
5919{
5920 int ret = 0;
5921 struct btrfs_key *key = sctx->cmp_key;
5922 struct btrfs_inode_item *left_ii = NULL;
5923 struct btrfs_inode_item *right_ii = NULL;
5924 u64 left_gen = 0;
5925 u64 right_gen = 0;
5926
Alexander Block31db9f72012-07-25 23:19:24 +02005927 sctx->cur_ino = key->objectid;
5928 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005929 sctx->cur_inode_last_extent = (u64)-1;
Filipe Mananaffa7c422018-02-06 20:40:40 +00005930 sctx->cur_inode_next_write_offset = 0;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005931
5932 /*
5933 * Set send_progress to current inode. This will tell all get_cur_xxx
5934 * functions that the current inode's refs are not updated yet. Later,
5935 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5936 */
Alexander Block31db9f72012-07-25 23:19:24 +02005937 sctx->send_progress = sctx->cur_ino;
5938
5939 if (result == BTRFS_COMPARE_TREE_NEW ||
5940 result == BTRFS_COMPARE_TREE_CHANGED) {
5941 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5942 sctx->left_path->slots[0],
5943 struct btrfs_inode_item);
5944 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5945 left_ii);
5946 } else {
5947 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5948 sctx->right_path->slots[0],
5949 struct btrfs_inode_item);
5950 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5951 right_ii);
5952 }
5953 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5954 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5955 sctx->right_path->slots[0],
5956 struct btrfs_inode_item);
5957
5958 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5959 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005960
5961 /*
5962 * The cur_ino = root dir case is special here. We can't treat
5963 * the inode as deleted+reused because it would generate a
5964 * stream that tries to delete/mkdir the root dir.
5965 */
5966 if (left_gen != right_gen &&
5967 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005968 sctx->cur_inode_new_gen = 1;
5969 }
5970
5971 if (result == BTRFS_COMPARE_TREE_NEW) {
5972 sctx->cur_inode_gen = left_gen;
5973 sctx->cur_inode_new = 1;
5974 sctx->cur_inode_deleted = 0;
5975 sctx->cur_inode_size = btrfs_inode_size(
5976 sctx->left_path->nodes[0], left_ii);
5977 sctx->cur_inode_mode = btrfs_inode_mode(
5978 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005979 sctx->cur_inode_rdev = btrfs_inode_rdev(
5980 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005981 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005982 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005983 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5984 sctx->cur_inode_gen = right_gen;
5985 sctx->cur_inode_new = 0;
5986 sctx->cur_inode_deleted = 1;
5987 sctx->cur_inode_size = btrfs_inode_size(
5988 sctx->right_path->nodes[0], right_ii);
5989 sctx->cur_inode_mode = btrfs_inode_mode(
5990 sctx->right_path->nodes[0], right_ii);
5991 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005992 /*
5993 * We need to do some special handling in case the inode was
5994 * reported as changed with a changed generation number. This
5995 * means that the original inode was deleted and new inode
5996 * reused the same inum. So we have to treat the old inode as
5997 * deleted and the new one as new.
5998 */
Alexander Block31db9f72012-07-25 23:19:24 +02005999 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02006000 /*
6001 * First, process the inode as if it was deleted.
6002 */
Alexander Block31db9f72012-07-25 23:19:24 +02006003 sctx->cur_inode_gen = right_gen;
6004 sctx->cur_inode_new = 0;
6005 sctx->cur_inode_deleted = 1;
6006 sctx->cur_inode_size = btrfs_inode_size(
6007 sctx->right_path->nodes[0], right_ii);
6008 sctx->cur_inode_mode = btrfs_inode_mode(
6009 sctx->right_path->nodes[0], right_ii);
6010 ret = process_all_refs(sctx,
6011 BTRFS_COMPARE_TREE_DELETED);
6012 if (ret < 0)
6013 goto out;
6014
Alexander Block766702e2012-07-28 14:11:31 +02006015 /*
6016 * Now process the inode as if it was new.
6017 */
Alexander Block31db9f72012-07-25 23:19:24 +02006018 sctx->cur_inode_gen = left_gen;
6019 sctx->cur_inode_new = 1;
6020 sctx->cur_inode_deleted = 0;
6021 sctx->cur_inode_size = btrfs_inode_size(
6022 sctx->left_path->nodes[0], left_ii);
6023 sctx->cur_inode_mode = btrfs_inode_mode(
6024 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006025 sctx->cur_inode_rdev = btrfs_inode_rdev(
6026 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02006027 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006028 if (ret < 0)
6029 goto out;
6030
6031 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6032 if (ret < 0)
6033 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006034 /*
6035 * Advance send_progress now as we did not get into
6036 * process_recorded_refs_if_needed in the new_gen case.
6037 */
6038 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02006039
6040 /*
6041 * Now process all extents and xattrs of the inode as if
6042 * they were all new.
6043 */
Alexander Block31db9f72012-07-25 23:19:24 +02006044 ret = process_all_extents(sctx);
6045 if (ret < 0)
6046 goto out;
6047 ret = process_all_new_xattrs(sctx);
6048 if (ret < 0)
6049 goto out;
6050 } else {
6051 sctx->cur_inode_gen = left_gen;
6052 sctx->cur_inode_new = 0;
6053 sctx->cur_inode_new_gen = 0;
6054 sctx->cur_inode_deleted = 0;
6055 sctx->cur_inode_size = btrfs_inode_size(
6056 sctx->left_path->nodes[0], left_ii);
6057 sctx->cur_inode_mode = btrfs_inode_mode(
6058 sctx->left_path->nodes[0], left_ii);
6059 }
6060 }
6061
6062out:
6063 return ret;
6064}
6065
Alexander Block766702e2012-07-28 14:11:31 +02006066/*
6067 * We have to process new refs before deleted refs, but compare_trees gives us
6068 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6069 * first and later process them in process_recorded_refs.
6070 * For the cur_inode_new_gen case, we skip recording completely because
6071 * changed_inode did already initiate processing of refs. The reason for this is
6072 * that in this case, compare_tree actually compares the refs of 2 different
6073 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6074 * refs of the right tree as deleted and all refs of the left tree as new.
6075 */
Alexander Block31db9f72012-07-25 23:19:24 +02006076static int changed_ref(struct send_ctx *sctx,
6077 enum btrfs_compare_tree_result result)
6078{
6079 int ret = 0;
6080
Filipe Manana95155582016-08-01 01:50:37 +01006081 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6082 inconsistent_snapshot_error(sctx, result, "reference");
6083 return -EIO;
6084 }
Alexander Block31db9f72012-07-25 23:19:24 +02006085
6086 if (!sctx->cur_inode_new_gen &&
6087 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6088 if (result == BTRFS_COMPARE_TREE_NEW)
6089 ret = record_new_ref(sctx);
6090 else if (result == BTRFS_COMPARE_TREE_DELETED)
6091 ret = record_deleted_ref(sctx);
6092 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6093 ret = record_changed_ref(sctx);
6094 }
6095
6096 return ret;
6097}
6098
Alexander Block766702e2012-07-28 14:11:31 +02006099/*
6100 * Process new/deleted/changed xattrs. We skip processing in the
6101 * cur_inode_new_gen case because changed_inode did already initiate processing
6102 * of xattrs. The reason is the same as in changed_ref
6103 */
Alexander Block31db9f72012-07-25 23:19:24 +02006104static int changed_xattr(struct send_ctx *sctx,
6105 enum btrfs_compare_tree_result result)
6106{
6107 int ret = 0;
6108
Filipe Manana95155582016-08-01 01:50:37 +01006109 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6110 inconsistent_snapshot_error(sctx, result, "xattr");
6111 return -EIO;
6112 }
Alexander Block31db9f72012-07-25 23:19:24 +02006113
6114 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6115 if (result == BTRFS_COMPARE_TREE_NEW)
6116 ret = process_new_xattr(sctx);
6117 else if (result == BTRFS_COMPARE_TREE_DELETED)
6118 ret = process_deleted_xattr(sctx);
6119 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6120 ret = process_changed_xattr(sctx);
6121 }
6122
6123 return ret;
6124}
6125
Alexander Block766702e2012-07-28 14:11:31 +02006126/*
6127 * Process new/deleted/changed extents. We skip processing in the
6128 * cur_inode_new_gen case because changed_inode did already initiate processing
6129 * of extents. The reason is the same as in changed_ref
6130 */
Alexander Block31db9f72012-07-25 23:19:24 +02006131static int changed_extent(struct send_ctx *sctx,
6132 enum btrfs_compare_tree_result result)
6133{
6134 int ret = 0;
6135
Filipe Manana95155582016-08-01 01:50:37 +01006136 if (sctx->cur_ino != sctx->cmp_key->objectid) {
Filipe Mananad5e84fd2016-09-19 10:57:40 +01006137
6138 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6139 struct extent_buffer *leaf_l;
6140 struct extent_buffer *leaf_r;
6141 struct btrfs_file_extent_item *ei_l;
6142 struct btrfs_file_extent_item *ei_r;
6143
6144 leaf_l = sctx->left_path->nodes[0];
6145 leaf_r = sctx->right_path->nodes[0];
6146 ei_l = btrfs_item_ptr(leaf_l,
6147 sctx->left_path->slots[0],
6148 struct btrfs_file_extent_item);
6149 ei_r = btrfs_item_ptr(leaf_r,
6150 sctx->right_path->slots[0],
6151 struct btrfs_file_extent_item);
6152
6153 /*
6154 * We may have found an extent item that has changed
6155 * only its disk_bytenr field and the corresponding
6156 * inode item was not updated. This case happens due to
6157 * very specific timings during relocation when a leaf
6158 * that contains file extent items is COWed while
6159 * relocation is ongoing and its in the stage where it
6160 * updates data pointers. So when this happens we can
6161 * safely ignore it since we know it's the same extent,
6162 * but just at different logical and physical locations
6163 * (when an extent is fully replaced with a new one, we
6164 * know the generation number must have changed too,
6165 * since snapshot creation implies committing the current
6166 * transaction, and the inode item must have been updated
6167 * as well).
6168 * This replacement of the disk_bytenr happens at
6169 * relocation.c:replace_file_extents() through
6170 * relocation.c:btrfs_reloc_cow_block().
6171 */
6172 if (btrfs_file_extent_generation(leaf_l, ei_l) ==
6173 btrfs_file_extent_generation(leaf_r, ei_r) &&
6174 btrfs_file_extent_ram_bytes(leaf_l, ei_l) ==
6175 btrfs_file_extent_ram_bytes(leaf_r, ei_r) &&
6176 btrfs_file_extent_compression(leaf_l, ei_l) ==
6177 btrfs_file_extent_compression(leaf_r, ei_r) &&
6178 btrfs_file_extent_encryption(leaf_l, ei_l) ==
6179 btrfs_file_extent_encryption(leaf_r, ei_r) &&
6180 btrfs_file_extent_other_encoding(leaf_l, ei_l) ==
6181 btrfs_file_extent_other_encoding(leaf_r, ei_r) &&
6182 btrfs_file_extent_type(leaf_l, ei_l) ==
6183 btrfs_file_extent_type(leaf_r, ei_r) &&
6184 btrfs_file_extent_disk_bytenr(leaf_l, ei_l) !=
6185 btrfs_file_extent_disk_bytenr(leaf_r, ei_r) &&
6186 btrfs_file_extent_disk_num_bytes(leaf_l, ei_l) ==
6187 btrfs_file_extent_disk_num_bytes(leaf_r, ei_r) &&
6188 btrfs_file_extent_offset(leaf_l, ei_l) ==
6189 btrfs_file_extent_offset(leaf_r, ei_r) &&
6190 btrfs_file_extent_num_bytes(leaf_l, ei_l) ==
6191 btrfs_file_extent_num_bytes(leaf_r, ei_r))
6192 return 0;
6193 }
6194
Filipe Manana95155582016-08-01 01:50:37 +01006195 inconsistent_snapshot_error(sctx, result, "extent");
6196 return -EIO;
6197 }
Alexander Block31db9f72012-07-25 23:19:24 +02006198
6199 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6200 if (result != BTRFS_COMPARE_TREE_DELETED)
6201 ret = process_extent(sctx, sctx->left_path,
6202 sctx->cmp_key);
6203 }
6204
6205 return ret;
6206}
6207
Josef Bacikba5e8f22013-08-16 16:52:55 -04006208static int dir_changed(struct send_ctx *sctx, u64 dir)
6209{
6210 u64 orig_gen, new_gen;
6211 int ret;
6212
6213 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6214 NULL, NULL);
6215 if (ret)
6216 return ret;
6217
6218 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6219 NULL, NULL, NULL);
6220 if (ret)
6221 return ret;
6222
6223 return (orig_gen != new_gen) ? 1 : 0;
6224}
6225
6226static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6227 struct btrfs_key *key)
6228{
6229 struct btrfs_inode_extref *extref;
6230 struct extent_buffer *leaf;
6231 u64 dirid = 0, last_dirid = 0;
6232 unsigned long ptr;
6233 u32 item_size;
6234 u32 cur_offset = 0;
6235 int ref_name_len;
6236 int ret = 0;
6237
6238 /* Easy case, just check this one dirid */
6239 if (key->type == BTRFS_INODE_REF_KEY) {
6240 dirid = key->offset;
6241
6242 ret = dir_changed(sctx, dirid);
6243 goto out;
6244 }
6245
6246 leaf = path->nodes[0];
6247 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6248 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6249 while (cur_offset < item_size) {
6250 extref = (struct btrfs_inode_extref *)(ptr +
6251 cur_offset);
6252 dirid = btrfs_inode_extref_parent(leaf, extref);
6253 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6254 cur_offset += ref_name_len + sizeof(*extref);
6255 if (dirid == last_dirid)
6256 continue;
6257 ret = dir_changed(sctx, dirid);
6258 if (ret)
6259 break;
6260 last_dirid = dirid;
6261 }
6262out:
6263 return ret;
6264}
6265
Alexander Block766702e2012-07-28 14:11:31 +02006266/*
6267 * Updates compare related fields in sctx and simply forwards to the actual
6268 * changed_xxx functions.
6269 */
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006270static int changed_cb(struct btrfs_path *left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02006271 struct btrfs_path *right_path,
6272 struct btrfs_key *key,
6273 enum btrfs_compare_tree_result result,
6274 void *ctx)
6275{
6276 int ret = 0;
6277 struct send_ctx *sctx = ctx;
6278
Josef Bacikba5e8f22013-08-16 16:52:55 -04006279 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04006280 if (key->type == BTRFS_INODE_REF_KEY ||
6281 key->type == BTRFS_INODE_EXTREF_KEY) {
6282 ret = compare_refs(sctx, left_path, key);
6283 if (!ret)
6284 return 0;
6285 if (ret < 0)
6286 return ret;
6287 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6288 return maybe_send_hole(sctx, left_path, key);
6289 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04006290 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006291 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04006292 result = BTRFS_COMPARE_TREE_CHANGED;
6293 ret = 0;
6294 }
6295
Alexander Block31db9f72012-07-25 23:19:24 +02006296 sctx->left_path = left_path;
6297 sctx->right_path = right_path;
6298 sctx->cmp_key = key;
6299
6300 ret = finish_inode_if_needed(sctx, 0);
6301 if (ret < 0)
6302 goto out;
6303
Alexander Block2981e222012-08-01 14:47:03 +02006304 /* Ignore non-FS objects */
6305 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6306 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6307 goto out;
6308
Alexander Block31db9f72012-07-25 23:19:24 +02006309 if (key->type == BTRFS_INODE_ITEM_KEY)
6310 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00006311 else if (key->type == BTRFS_INODE_REF_KEY ||
6312 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02006313 ret = changed_ref(sctx, result);
6314 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6315 ret = changed_xattr(sctx, result);
6316 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6317 ret = changed_extent(sctx, result);
6318
6319out:
6320 return ret;
6321}
6322
6323static int full_send_tree(struct send_ctx *sctx)
6324{
6325 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006326 struct btrfs_root *send_root = sctx->send_root;
6327 struct btrfs_key key;
6328 struct btrfs_key found_key;
6329 struct btrfs_path *path;
6330 struct extent_buffer *eb;
6331 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006332
6333 path = alloc_path_for_send();
6334 if (!path)
6335 return -ENOMEM;
6336
Alexander Block31db9f72012-07-25 23:19:24 +02006337 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6338 key.type = BTRFS_INODE_ITEM_KEY;
6339 key.offset = 0;
6340
Alexander Block31db9f72012-07-25 23:19:24 +02006341 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6342 if (ret < 0)
6343 goto out;
6344 if (ret)
6345 goto out_finish;
6346
6347 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006348 eb = path->nodes[0];
6349 slot = path->slots[0];
6350 btrfs_item_key_to_cpu(eb, &found_key, slot);
6351
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006352 ret = changed_cb(path, NULL, &found_key,
6353 BTRFS_COMPARE_TREE_NEW, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006354 if (ret < 0)
6355 goto out;
6356
6357 key.objectid = found_key.objectid;
6358 key.type = found_key.type;
6359 key.offset = found_key.offset + 1;
6360
6361 ret = btrfs_next_item(send_root, path);
6362 if (ret < 0)
6363 goto out;
6364 if (ret) {
6365 ret = 0;
6366 break;
6367 }
6368 }
6369
6370out_finish:
6371 ret = finish_inode_if_needed(sctx, 1);
6372
6373out:
6374 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006375 return ret;
6376}
6377
6378static int send_subvol(struct send_ctx *sctx)
6379{
6380 int ret;
6381
Stefan Behrensc2c71322013-04-10 17:10:52 +00006382 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6383 ret = send_header(sctx);
6384 if (ret < 0)
6385 goto out;
6386 }
Alexander Block31db9f72012-07-25 23:19:24 +02006387
6388 ret = send_subvol_begin(sctx);
6389 if (ret < 0)
6390 goto out;
6391
6392 if (sctx->parent_root) {
6393 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6394 changed_cb, sctx);
6395 if (ret < 0)
6396 goto out;
6397 ret = finish_inode_if_needed(sctx, 1);
6398 if (ret < 0)
6399 goto out;
6400 } else {
6401 ret = full_send_tree(sctx);
6402 if (ret < 0)
6403 goto out;
6404 }
6405
6406out:
Alexander Block31db9f72012-07-25 23:19:24 +02006407 free_recorded_refs(sctx);
6408 return ret;
6409}
6410
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006411/*
6412 * If orphan cleanup did remove any orphans from a root, it means the tree
6413 * was modified and therefore the commit root is not the same as the current
6414 * root anymore. This is a problem, because send uses the commit root and
6415 * therefore can see inode items that don't exist in the current root anymore,
6416 * and for example make calls to btrfs_iget, which will do tree lookups based
6417 * on the current root and not on the commit root. Those lookups will fail,
6418 * returning a -ESTALE error, and making send fail with that error. So make
6419 * sure a send does not see any orphans we have just removed, and that it will
6420 * see the same inodes regardless of whether a transaction commit happened
6421 * before it started (meaning that the commit root will be the same as the
6422 * current root) or not.
6423 */
6424static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6425{
6426 int i;
6427 struct btrfs_trans_handle *trans = NULL;
6428
6429again:
6430 if (sctx->parent_root &&
6431 sctx->parent_root->node != sctx->parent_root->commit_root)
6432 goto commit_trans;
6433
6434 for (i = 0; i < sctx->clone_roots_cnt; i++)
6435 if (sctx->clone_roots[i].root->node !=
6436 sctx->clone_roots[i].root->commit_root)
6437 goto commit_trans;
6438
6439 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006440 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006441
6442 return 0;
6443
6444commit_trans:
6445 /* Use any root, all fs roots will get their commit roots updated. */
6446 if (!trans) {
6447 trans = btrfs_join_transaction(sctx->send_root);
6448 if (IS_ERR(trans))
6449 return PTR_ERR(trans);
6450 goto again;
6451 }
6452
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006453 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006454}
6455
David Sterba66ef7d62013-12-17 15:07:20 +01006456static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6457{
6458 spin_lock(&root->root_item_lock);
6459 root->send_in_progress--;
6460 /*
6461 * Not much left to do, we don't know why it's unbalanced and
6462 * can't blindly reset it to 0.
6463 */
6464 if (root->send_in_progress < 0)
6465 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006466 "send_in_progres unbalanced %d root %llu",
6467 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01006468 spin_unlock(&root->root_item_lock);
6469}
6470
Josef Bacik2351f432017-09-27 10:43:13 -04006471long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
Alexander Block31db9f72012-07-25 23:19:24 +02006472{
6473 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006474 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6475 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02006476 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02006477 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006478 struct send_ctx *sctx = NULL;
6479 u32 i;
6480 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01006481 int clone_sources_to_rollback = 0;
David Sterbae55d1152016-04-11 18:52:02 +02006482 unsigned alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08006483 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08006484 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02006485
6486 if (!capable(CAP_SYS_ADMIN))
6487 return -EPERM;
6488
Josef Bacik139f8072013-05-20 11:26:50 -04006489 /*
David Sterba2c686532013-12-16 17:34:17 +01006490 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02006491 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01006492 */
6493 spin_lock(&send_root->root_item_lock);
6494 send_root->send_in_progress++;
6495 spin_unlock(&send_root->root_item_lock);
6496
6497 /*
Josef Bacik139f8072013-05-20 11:26:50 -04006498 * This is done when we lookup the root, it should already be complete
6499 * by the time we get here.
6500 */
6501 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6502
6503 /*
David Sterba2c686532013-12-16 17:34:17 +01006504 * Userspace tools do the checks and warn the user if it's
6505 * not RO.
6506 */
6507 if (!btrfs_root_readonly(send_root)) {
6508 ret = -EPERM;
6509 goto out;
6510 }
6511
Dan Carpenter457ae722017-03-17 23:51:20 +03006512 /*
6513 * Check that we don't overflow at later allocations, we request
6514 * clone_sources_count + 1 items, and compare to unsigned long inside
6515 * access_ok.
6516 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006517 if (arg->clone_sources_count >
Dan Carpenter457ae722017-03-17 23:51:20 +03006518 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006519 ret = -EINVAL;
6520 goto out;
6521 }
6522
Alexander Block31db9f72012-07-25 23:19:24 +02006523 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05006524 sizeof(*arg->clone_sources) *
6525 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02006526 ret = -EFAULT;
6527 goto out;
6528 }
6529
Stefan Behrensc2c71322013-04-10 17:10:52 +00006530 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006531 ret = -EINVAL;
6532 goto out;
6533 }
6534
David Sterbae780b0d2016-01-18 18:42:13 +01006535 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006536 if (!sctx) {
6537 ret = -ENOMEM;
6538 goto out;
6539 }
6540
6541 INIT_LIST_HEAD(&sctx->new_refs);
6542 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01006543 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006544 INIT_LIST_HEAD(&sctx->name_cache_list);
6545
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006546 sctx->flags = arg->flags;
6547
Alexander Block31db9f72012-07-25 23:19:24 +02006548 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00006549 if (!sctx->send_filp) {
6550 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02006551 goto out;
6552 }
6553
Alexander Block31db9f72012-07-25 23:19:24 +02006554 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02006555 /*
6556 * Unlikely but possible, if the subvolume is marked for deletion but
6557 * is slow to remove the directory entry, send can still be started
6558 */
6559 if (btrfs_root_dead(sctx->send_root)) {
6560 ret = -EPERM;
6561 goto out;
6562 }
6563
Alexander Block31db9f72012-07-25 23:19:24 +02006564 sctx->clone_roots_cnt = arg->clone_sources_count;
6565
6566 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
Michal Hocko752ade62017-05-08 15:57:27 -07006567 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006568 if (!sctx->send_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006569 ret = -ENOMEM;
6570 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006571 }
6572
Michal Hocko752ade62017-05-08 15:57:27 -07006573 sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006574 if (!sctx->read_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006575 ret = -ENOMEM;
6576 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006577 }
6578
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006579 sctx->pending_dir_moves = RB_ROOT;
6580 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00006581 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006582
David Sterbae55d1152016-04-11 18:52:02 +02006583 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6584
David Sterba818e0102017-05-31 18:40:02 +02006585 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006586 if (!sctx->clone_roots) {
David Sterba818e0102017-05-31 18:40:02 +02006587 ret = -ENOMEM;
6588 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006589 }
6590
David Sterbae55d1152016-04-11 18:52:02 +02006591 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6592
Alexander Block31db9f72012-07-25 23:19:24 +02006593 if (arg->clone_sources_count) {
Michal Hocko752ade62017-05-08 15:57:27 -07006594 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006595 if (!clone_sources_tmp) {
Michal Hocko752ade62017-05-08 15:57:27 -07006596 ret = -ENOMEM;
6597 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006598 }
6599
6600 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02006601 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02006602 if (ret) {
6603 ret = -EFAULT;
6604 goto out;
6605 }
6606
6607 for (i = 0; i < arg->clone_sources_count; i++) {
6608 key.objectid = clone_sources_tmp[i];
6609 key.type = BTRFS_ROOT_ITEM_KEY;
6610 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006611
6612 index = srcu_read_lock(&fs_info->subvol_srcu);
6613
Alexander Block31db9f72012-07-25 23:19:24 +02006614 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02006615 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006616 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006617 ret = PTR_ERR(clone_root);
6618 goto out;
6619 }
David Sterba2c686532013-12-16 17:34:17 +01006620 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00006621 if (!btrfs_root_readonly(clone_root) ||
6622 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006623 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006624 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006625 ret = -EPERM;
6626 goto out;
6627 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00006628 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01006629 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006630 srcu_read_unlock(&fs_info->subvol_srcu, index);
6631
Alexander Block31db9f72012-07-25 23:19:24 +02006632 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00006633 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006634 }
David Sterba2f913062016-04-11 18:40:08 +02006635 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006636 clone_sources_tmp = NULL;
6637 }
6638
6639 if (arg->parent_root) {
6640 key.objectid = arg->parent_root;
6641 key.type = BTRFS_ROOT_ITEM_KEY;
6642 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006643
6644 index = srcu_read_lock(&fs_info->subvol_srcu);
6645
Alexander Block31db9f72012-07-25 23:19:24 +02006646 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006647 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006648 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006649 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02006650 goto out;
6651 }
Wang Shilong18f687d2014-01-07 17:25:19 +08006652
David Sterba2c686532013-12-16 17:34:17 +01006653 spin_lock(&sctx->parent_root->root_item_lock);
6654 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02006655 if (!btrfs_root_readonly(sctx->parent_root) ||
6656 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006657 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006658 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006659 ret = -EPERM;
6660 goto out;
6661 }
6662 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006663
6664 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006665 }
6666
6667 /*
6668 * Clones from send_root are allowed, but only if the clone source
6669 * is behind the current send position. This is checked while searching
6670 * for possible clone sources.
6671 */
6672 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6673
6674 /* We do a bsearch later */
6675 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6676 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6677 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08006678 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006679
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006680 ret = ensure_commit_roots_uptodate(sctx);
6681 if (ret)
6682 goto out;
6683
David Sterba2755a0d2014-07-31 00:43:18 +02006684 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02006685 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04006686 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02006687 if (ret < 0)
6688 goto out;
6689
Stefan Behrensc2c71322013-04-10 17:10:52 +00006690 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6691 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6692 if (ret < 0)
6693 goto out;
6694 ret = send_cmd(sctx);
6695 if (ret < 0)
6696 goto out;
6697 }
Alexander Block31db9f72012-07-25 23:19:24 +02006698
6699out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006700 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6701 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6702 struct rb_node *n;
6703 struct pending_dir_move *pm;
6704
6705 n = rb_first(&sctx->pending_dir_moves);
6706 pm = rb_entry(n, struct pending_dir_move, node);
6707 while (!list_empty(&pm->list)) {
6708 struct pending_dir_move *pm2;
6709
6710 pm2 = list_first_entry(&pm->list,
6711 struct pending_dir_move, list);
6712 free_pending_move(sctx, pm2);
6713 }
6714 free_pending_move(sctx, pm);
6715 }
6716
6717 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6718 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6719 struct rb_node *n;
6720 struct waiting_dir_move *dm;
6721
6722 n = rb_first(&sctx->waiting_dir_moves);
6723 dm = rb_entry(n, struct waiting_dir_move, node);
6724 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6725 kfree(dm);
6726 }
6727
Filipe Manana9dc44212014-02-19 14:31:44 +00006728 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6729 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6730 struct rb_node *n;
6731 struct orphan_dir_info *odi;
6732
6733 n = rb_first(&sctx->orphan_dirs);
6734 odi = rb_entry(n, struct orphan_dir_info, node);
6735 free_orphan_dir_info(sctx, odi);
6736 }
6737
Wang Shilong896c14f2014-01-07 17:25:18 +08006738 if (sort_clone_roots) {
6739 for (i = 0; i < sctx->clone_roots_cnt; i++)
6740 btrfs_root_dec_send_in_progress(
6741 sctx->clone_roots[i].root);
6742 } else {
6743 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6744 btrfs_root_dec_send_in_progress(
6745 sctx->clone_roots[i].root);
6746
6747 btrfs_root_dec_send_in_progress(send_root);
6748 }
David Sterba66ef7d62013-12-17 15:07:20 +01006749 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6750 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01006751
David Sterba2f913062016-04-11 18:40:08 +02006752 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006753
6754 if (sctx) {
6755 if (sctx->send_filp)
6756 fput(sctx->send_filp);
6757
David Sterbac03d01f2016-04-11 18:40:08 +02006758 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02006759 kvfree(sctx->send_buf);
David Sterbaeb5b75f2016-04-11 18:40:08 +02006760 kvfree(sctx->read_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02006761
6762 name_cache_free(sctx);
6763
6764 kfree(sctx);
6765 }
6766
6767 return ret;
6768}