blob: 085542832b9a9634a3d78e0f7e15bd49a4218e99 [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;
Alexander Block31db9f72012-07-25 23:19:24 +0200115
116 u64 send_progress;
117
118 struct list_head new_refs;
119 struct list_head deleted_refs;
120
121 struct radix_tree_root name_cache;
122 struct list_head name_cache_list;
123 int name_cache_size;
124
Liu Bo2131bcd2014-03-05 10:07:35 +0800125 struct file_ra_state ra;
126
Alexander Block31db9f72012-07-25 23:19:24 +0200127 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000128
129 /*
130 * We process inodes by their increasing order, so if before an
131 * incremental send we reverse the parent/child relationship of
132 * directories such that a directory with a lower inode number was
133 * the parent of a directory with a higher inode number, and the one
134 * becoming the new parent got renamed too, we can't rename/move the
135 * directory with lower inode number when we finish processing it - we
136 * must process the directory with higher inode number first, then
137 * rename/move it and then rename/move the directory with lower inode
138 * number. Example follows.
139 *
140 * Tree state when the first send was performed:
141 *
142 * .
143 * |-- a (ino 257)
144 * |-- b (ino 258)
145 * |
146 * |
147 * |-- c (ino 259)
148 * | |-- d (ino 260)
149 * |
150 * |-- c2 (ino 261)
151 *
152 * Tree state when the second (incremental) send is performed:
153 *
154 * .
155 * |-- a (ino 257)
156 * |-- b (ino 258)
157 * |-- c2 (ino 261)
158 * |-- d2 (ino 260)
159 * |-- cc (ino 259)
160 *
161 * The sequence of steps that lead to the second state was:
162 *
163 * mv /a/b/c/d /a/b/c2/d2
164 * mv /a/b/c /a/b/c2/d2/cc
165 *
166 * "c" has lower inode number, but we can't move it (2nd mv operation)
167 * before we move "d", which has higher inode number.
168 *
169 * So we just memorize which move/rename operations must be performed
170 * later when their respective parent is processed and moved/renamed.
171 */
172
173 /* Indexed by parent directory inode number. */
174 struct rb_root pending_dir_moves;
175
176 /*
177 * Reverse index, indexed by the inode number of a directory that
178 * is waiting for the move/rename of its immediate parent before its
179 * own move/rename can be performed.
180 */
181 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000182
183 /*
184 * A directory that is going to be rm'ed might have a child directory
185 * which is in the pending directory moves index above. In this case,
186 * the directory can only be removed after the move/rename of its child
187 * is performed. Example:
188 *
189 * Parent snapshot:
190 *
191 * . (ino 256)
192 * |-- a/ (ino 257)
193 * |-- b/ (ino 258)
194 * |-- c/ (ino 259)
195 * | |-- x/ (ino 260)
196 * |
197 * |-- y/ (ino 261)
198 *
199 * Send snapshot:
200 *
201 * . (ino 256)
202 * |-- a/ (ino 257)
203 * |-- b/ (ino 258)
204 * |-- YY/ (ino 261)
205 * |-- x/ (ino 260)
206 *
207 * Sequence of steps that lead to the send snapshot:
208 * rm -f /a/b/c/foo.txt
209 * mv /a/b/y /a/b/YY
210 * mv /a/b/c/x /a/b/YY
211 * rmdir /a/b/c
212 *
213 * When the child is processed, its move/rename is delayed until its
214 * parent is processed (as explained above), but all other operations
215 * like update utimes, chown, chgrp, etc, are performed and the paths
216 * that it uses for those operations must use the orphanized name of
217 * its parent (the directory we're going to rm later), so we need to
218 * memorize that name.
219 *
220 * Indexed by the inode number of the directory to be deleted.
221 */
222 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000223};
224
225struct pending_dir_move {
226 struct rb_node node;
227 struct list_head list;
228 u64 parent_ino;
229 u64 ino;
230 u64 gen;
231 struct list_head update_refs;
232};
233
234struct waiting_dir_move {
235 struct rb_node node;
236 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000237 /*
238 * There might be some directory that could not be removed because it
239 * was waiting for this directory inode to be moved first. Therefore
240 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
241 */
242 u64 rmdir_ino;
Filipe Manana8b191a62015-04-09 14:09:14 +0100243 bool orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +0000244};
245
246struct orphan_dir_info {
247 struct rb_node node;
248 u64 ino;
249 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +0200250};
251
252struct name_cache_entry {
253 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200254 /*
255 * radix_tree has only 32bit entries but we need to handle 64bit inums.
256 * We use the lower 32bit of the 64bit inum to store it in the tree. If
257 * more then one inum would fall into the same entry, we use radix_list
258 * to store the additional entries. radix_list is also used to store
259 * entries where two entries have the same inum but different
260 * generations.
261 */
262 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200263 u64 ino;
264 u64 gen;
265 u64 parent_ino;
266 u64 parent_gen;
267 int ret;
268 int need_later_update;
269 int name_len;
270 char name[];
271};
272
David Sterbae67c7182018-02-19 17:24:18 +0100273__cold
Filipe Manana95155582016-08-01 01:50:37 +0100274static void inconsistent_snapshot_error(struct send_ctx *sctx,
275 enum btrfs_compare_tree_result result,
276 const char *what)
277{
278 const char *result_string;
279
280 switch (result) {
281 case BTRFS_COMPARE_TREE_NEW:
282 result_string = "new";
283 break;
284 case BTRFS_COMPARE_TREE_DELETED:
285 result_string = "deleted";
286 break;
287 case BTRFS_COMPARE_TREE_CHANGED:
288 result_string = "updated";
289 break;
290 case BTRFS_COMPARE_TREE_SAME:
291 ASSERT(0);
292 result_string = "unchanged";
293 break;
294 default:
295 ASSERT(0);
296 result_string = "unexpected";
297 }
298
299 btrfs_err(sctx->send_root->fs_info,
300 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
301 result_string, what, sctx->cmp_key->objectid,
302 sctx->send_root->root_key.objectid,
303 (sctx->parent_root ?
304 sctx->parent_root->root_key.objectid : 0));
305}
306
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000307static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
308
Filipe Manana9dc44212014-02-19 14:31:44 +0000309static struct waiting_dir_move *
310get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
311
312static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
313
Josef Bacik16e75492013-10-22 12:18:51 -0400314static int need_send_hole(struct send_ctx *sctx)
315{
316 return (sctx->parent_root && !sctx->cur_inode_new &&
317 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
318 S_ISREG(sctx->cur_inode_mode));
319}
320
Alexander Block31db9f72012-07-25 23:19:24 +0200321static void fs_path_reset(struct fs_path *p)
322{
323 if (p->reversed) {
324 p->start = p->buf + p->buf_len - 1;
325 p->end = p->start;
326 *p->start = 0;
327 } else {
328 p->start = p->buf;
329 p->end = p->start;
330 *p->start = 0;
331 }
332}
333
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000334static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200335{
336 struct fs_path *p;
337
David Sterbae780b0d2016-01-18 18:42:13 +0100338 p = kmalloc(sizeof(*p), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +0200339 if (!p)
340 return NULL;
341 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200342 p->buf = p->inline_buf;
343 p->buf_len = FS_PATH_INLINE_SIZE;
344 fs_path_reset(p);
345 return p;
346}
347
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000348static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200349{
350 struct fs_path *p;
351
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000352 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200353 if (!p)
354 return NULL;
355 p->reversed = 1;
356 fs_path_reset(p);
357 return p;
358}
359
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000360static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200361{
362 if (!p)
363 return;
David Sterbaace01052014-02-05 16:17:34 +0100364 if (p->buf != p->inline_buf)
365 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200366 kfree(p);
367}
368
369static int fs_path_len(struct fs_path *p)
370{
371 return p->end - p->start;
372}
373
374static int fs_path_ensure_buf(struct fs_path *p, int len)
375{
376 char *tmp_buf;
377 int path_len;
378 int old_buf_len;
379
380 len++;
381
382 if (p->buf_len >= len)
383 return 0;
384
Chris Masoncfd4a532014-04-26 05:02:03 -0700385 if (len > PATH_MAX) {
386 WARN_ON(1);
387 return -ENOMEM;
388 }
389
David Sterba1b2782c2014-02-25 19:32:59 +0100390 path_len = p->end - p->start;
391 old_buf_len = p->buf_len;
392
David Sterbaace01052014-02-05 16:17:34 +0100393 /*
394 * First time the inline_buf does not suffice
395 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100396 if (p->buf == p->inline_buf) {
David Sterbae780b0d2016-01-18 18:42:13 +0100397 tmp_buf = kmalloc(len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100398 if (tmp_buf)
399 memcpy(tmp_buf, p->buf, old_buf_len);
400 } else {
David Sterbae780b0d2016-01-18 18:42:13 +0100401 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100402 }
David Sterba9c9ca002014-02-25 19:33:08 +0100403 if (!tmp_buf)
404 return -ENOMEM;
405 p->buf = tmp_buf;
406 /*
407 * The real size of the buffer is bigger, this will let the fast path
408 * happen most of the time
409 */
410 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100411
Alexander Block31db9f72012-07-25 23:19:24 +0200412 if (p->reversed) {
413 tmp_buf = p->buf + old_buf_len - path_len - 1;
414 p->end = p->buf + p->buf_len - 1;
415 p->start = p->end - path_len;
416 memmove(p->start, tmp_buf, path_len + 1);
417 } else {
418 p->start = p->buf;
419 p->end = p->start + path_len;
420 }
421 return 0;
422}
423
David Sterbab23ab572014-02-03 19:23:19 +0100424static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
425 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200426{
427 int ret;
428 int new_len;
429
430 new_len = p->end - p->start + name_len;
431 if (p->start != p->end)
432 new_len++;
433 ret = fs_path_ensure_buf(p, new_len);
434 if (ret < 0)
435 goto out;
436
437 if (p->reversed) {
438 if (p->start != p->end)
439 *--p->start = '/';
440 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100441 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200442 } else {
443 if (p->start != p->end)
444 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100445 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200446 p->end += name_len;
447 *p->end = 0;
448 }
449
450out:
451 return ret;
452}
453
454static int fs_path_add(struct fs_path *p, const char *name, int name_len)
455{
456 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100457 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200458
David Sterbab23ab572014-02-03 19:23:19 +0100459 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200460 if (ret < 0)
461 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100462 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200463
464out:
465 return ret;
466}
467
468static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
469{
470 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100471 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200472
David Sterbab23ab572014-02-03 19:23:19 +0100473 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200474 if (ret < 0)
475 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100476 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200477
478out:
479 return ret;
480}
481
482static int fs_path_add_from_extent_buffer(struct fs_path *p,
483 struct extent_buffer *eb,
484 unsigned long off, int len)
485{
486 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100487 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200488
David Sterbab23ab572014-02-03 19:23:19 +0100489 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200490 if (ret < 0)
491 goto out;
492
David Sterbab23ab572014-02-03 19:23:19 +0100493 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200494
495out:
496 return ret;
497}
498
Alexander Block31db9f72012-07-25 23:19:24 +0200499static int fs_path_copy(struct fs_path *p, struct fs_path *from)
500{
501 int ret;
502
503 p->reversed = from->reversed;
504 fs_path_reset(p);
505
506 ret = fs_path_add_path(p, from);
507
508 return ret;
509}
510
511
512static void fs_path_unreverse(struct fs_path *p)
513{
514 char *tmp;
515 int len;
516
517 if (!p->reversed)
518 return;
519
520 tmp = p->start;
521 len = p->end - p->start;
522 p->start = p->buf;
523 p->end = p->start + len;
524 memmove(p->start, tmp, len + 1);
525 p->reversed = 0;
526}
527
528static struct btrfs_path *alloc_path_for_send(void)
529{
530 struct btrfs_path *path;
531
532 path = btrfs_alloc_path();
533 if (!path)
534 return NULL;
535 path->search_commit_root = 1;
536 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400537 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200538 return path;
539}
540
Eric Sandeen48a3b632013-04-25 20:41:01 +0000541static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200542{
543 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200544 u32 pos = 0;
545
Alexander Block31db9f72012-07-25 23:19:24 +0200546 while (pos < len) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200547 ret = kernel_write(filp, buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200548 /* TODO handle that correctly */
549 /*if (ret == -ERESTARTSYS) {
550 continue;
551 }*/
552 if (ret < 0)
Christoph Hellwig8e931572017-09-01 17:39:19 +0200553 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200554 if (ret == 0) {
Christoph Hellwig8e931572017-09-01 17:39:19 +0200555 return -EIO;
Alexander Block31db9f72012-07-25 23:19:24 +0200556 }
557 pos += ret;
558 }
559
Christoph Hellwig8e931572017-09-01 17:39:19 +0200560 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200561}
562
563static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
564{
565 struct btrfs_tlv_header *hdr;
566 int total_len = sizeof(*hdr) + len;
567 int left = sctx->send_max_size - sctx->send_size;
568
569 if (unlikely(left < total_len))
570 return -EOVERFLOW;
571
572 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
573 hdr->tlv_type = cpu_to_le16(attr);
574 hdr->tlv_len = cpu_to_le16(len);
575 memcpy(hdr + 1, data, len);
576 sctx->send_size += total_len;
577
578 return 0;
579}
580
David Sterba95bc79d2013-12-16 17:34:10 +0100581#define TLV_PUT_DEFINE_INT(bits) \
582 static int tlv_put_u##bits(struct send_ctx *sctx, \
583 u##bits attr, u##bits value) \
584 { \
585 __le##bits __tmp = cpu_to_le##bits(value); \
586 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
587 }
Alexander Block31db9f72012-07-25 23:19:24 +0200588
David Sterba95bc79d2013-12-16 17:34:10 +0100589TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200590
591static int tlv_put_string(struct send_ctx *sctx, u16 attr,
592 const char *str, int len)
593{
594 if (len == -1)
595 len = strlen(str);
596 return tlv_put(sctx, attr, str, len);
597}
598
599static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
600 const u8 *uuid)
601{
602 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
603}
604
Alexander Block31db9f72012-07-25 23:19:24 +0200605static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
606 struct extent_buffer *eb,
607 struct btrfs_timespec *ts)
608{
609 struct btrfs_timespec bts;
610 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
611 return tlv_put(sctx, attr, &bts, sizeof(bts));
612}
613
614
615#define TLV_PUT(sctx, attrtype, attrlen, data) \
616 do { \
617 ret = tlv_put(sctx, attrtype, attrlen, data); \
618 if (ret < 0) \
619 goto tlv_put_failure; \
620 } while (0)
621
622#define TLV_PUT_INT(sctx, attrtype, bits, value) \
623 do { \
624 ret = tlv_put_u##bits(sctx, attrtype, value); \
625 if (ret < 0) \
626 goto tlv_put_failure; \
627 } while (0)
628
629#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
630#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
631#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
632#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
633#define TLV_PUT_STRING(sctx, attrtype, str, len) \
634 do { \
635 ret = tlv_put_string(sctx, attrtype, str, len); \
636 if (ret < 0) \
637 goto tlv_put_failure; \
638 } while (0)
639#define TLV_PUT_PATH(sctx, attrtype, p) \
640 do { \
641 ret = tlv_put_string(sctx, attrtype, p->start, \
642 p->end - p->start); \
643 if (ret < 0) \
644 goto tlv_put_failure; \
645 } while(0)
646#define TLV_PUT_UUID(sctx, attrtype, uuid) \
647 do { \
648 ret = tlv_put_uuid(sctx, attrtype, uuid); \
649 if (ret < 0) \
650 goto tlv_put_failure; \
651 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200652#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
653 do { \
654 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
655 if (ret < 0) \
656 goto tlv_put_failure; \
657 } while (0)
658
659static int send_header(struct send_ctx *sctx)
660{
661 struct btrfs_stream_header hdr;
662
663 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
664 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
665
Anand Jain1bcea352012-09-14 00:04:21 -0600666 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
667 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200668}
669
670/*
671 * For each command/item we want to send to userspace, we call this function.
672 */
673static int begin_cmd(struct send_ctx *sctx, int cmd)
674{
675 struct btrfs_cmd_header *hdr;
676
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530677 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200678 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200679
680 BUG_ON(sctx->send_size);
681
682 sctx->send_size += sizeof(*hdr);
683 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
684 hdr->cmd = cpu_to_le16(cmd);
685
686 return 0;
687}
688
689static int send_cmd(struct send_ctx *sctx)
690{
691 int ret;
692 struct btrfs_cmd_header *hdr;
693 u32 crc;
694
695 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
696 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
697 hdr->crc = 0;
698
Nikolay Borisov9678c542018-01-08 11:45:05 +0200699 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200700 hdr->crc = cpu_to_le32(crc);
701
Anand Jain1bcea352012-09-14 00:04:21 -0600702 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
703 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200704
705 sctx->total_send_size += sctx->send_size;
706 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
707 sctx->send_size = 0;
708
709 return ret;
710}
711
712/*
713 * Sends a move instruction to user space
714 */
715static int send_rename(struct send_ctx *sctx,
716 struct fs_path *from, struct fs_path *to)
717{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400718 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200719 int ret;
720
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400721 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200722
723 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
724 if (ret < 0)
725 goto out;
726
727 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
728 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
729
730 ret = send_cmd(sctx);
731
732tlv_put_failure:
733out:
734 return ret;
735}
736
737/*
738 * Sends a link instruction to user space
739 */
740static int send_link(struct send_ctx *sctx,
741 struct fs_path *path, struct fs_path *lnk)
742{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400743 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200744 int ret;
745
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400746 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200747
748 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
749 if (ret < 0)
750 goto out;
751
752 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
753 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
754
755 ret = send_cmd(sctx);
756
757tlv_put_failure:
758out:
759 return ret;
760}
761
762/*
763 * Sends an unlink instruction to user space
764 */
765static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
766{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400767 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200768 int ret;
769
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400770 btrfs_debug(fs_info, "send_unlink %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200771
772 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
773 if (ret < 0)
774 goto out;
775
776 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
777
778 ret = send_cmd(sctx);
779
780tlv_put_failure:
781out:
782 return ret;
783}
784
785/*
786 * Sends a rmdir instruction to user space
787 */
788static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
789{
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400790 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +0200791 int ret;
792
Jeff Mahoney04ab9562016-09-20 10:05:03 -0400793 btrfs_debug(fs_info, "send_rmdir %s", path->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200794
795 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
796 if (ret < 0)
797 goto out;
798
799 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
800
801 ret = send_cmd(sctx);
802
803tlv_put_failure:
804out:
805 return ret;
806}
807
808/*
809 * Helper function to retrieve some fields from an inode item.
810 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400811static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
812 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
813 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200814{
815 int ret;
816 struct btrfs_inode_item *ii;
817 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200818
819 key.objectid = ino;
820 key.type = BTRFS_INODE_ITEM_KEY;
821 key.offset = 0;
822 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200823 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400824 if (ret > 0)
825 ret = -ENOENT;
826 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200827 }
828
829 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
830 struct btrfs_inode_item);
831 if (size)
832 *size = btrfs_inode_size(path->nodes[0], ii);
833 if (gen)
834 *gen = btrfs_inode_generation(path->nodes[0], ii);
835 if (mode)
836 *mode = btrfs_inode_mode(path->nodes[0], ii);
837 if (uid)
838 *uid = btrfs_inode_uid(path->nodes[0], ii);
839 if (gid)
840 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200841 if (rdev)
842 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200843
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400844 return ret;
845}
846
847static int get_inode_info(struct btrfs_root *root,
848 u64 ino, u64 *size, u64 *gen,
849 u64 *mode, u64 *uid, u64 *gid,
850 u64 *rdev)
851{
852 struct btrfs_path *path;
853 int ret;
854
855 path = alloc_path_for_send();
856 if (!path)
857 return -ENOMEM;
858 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
859 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200860 btrfs_free_path(path);
861 return ret;
862}
863
864typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
865 struct fs_path *p,
866 void *ctx);
867
868/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000869 * Helper function to iterate the entries in ONE btrfs_inode_ref or
870 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200871 * The iterate callback may return a non zero value to stop iteration. This can
872 * be a negative value for error codes or 1 to simply stop it.
873 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000874 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200875 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000876static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200877 struct btrfs_key *found_key, int resolve,
878 iterate_inode_ref_t iterate, void *ctx)
879{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000880 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200881 struct btrfs_item *item;
882 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000883 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200884 struct btrfs_path *tmp_path;
885 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000886 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200887 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000888 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200889 u32 name_len;
890 char *start;
891 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000892 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200893 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000894 u64 dir;
895 unsigned long name_off;
896 unsigned long elem_size;
897 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200898
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000899 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200900 if (!p)
901 return -ENOMEM;
902
903 tmp_path = alloc_path_for_send();
904 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000905 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200906 return -ENOMEM;
907 }
908
Alexander Block31db9f72012-07-25 23:19:24 +0200909
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000910 if (found_key->type == BTRFS_INODE_REF_KEY) {
911 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
912 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100913 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000914 total = btrfs_item_size(eb, item);
915 elem_size = sizeof(*iref);
916 } else {
917 ptr = btrfs_item_ptr_offset(eb, slot);
918 total = btrfs_item_size_nr(eb, slot);
919 elem_size = sizeof(*extref);
920 }
921
Alexander Block31db9f72012-07-25 23:19:24 +0200922 while (cur < total) {
923 fs_path_reset(p);
924
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000925 if (found_key->type == BTRFS_INODE_REF_KEY) {
926 iref = (struct btrfs_inode_ref *)(ptr + cur);
927 name_len = btrfs_inode_ref_name_len(eb, iref);
928 name_off = (unsigned long)(iref + 1);
929 index = btrfs_inode_ref_index(eb, iref);
930 dir = found_key->offset;
931 } else {
932 extref = (struct btrfs_inode_extref *)(ptr + cur);
933 name_len = btrfs_inode_extref_name_len(eb, extref);
934 name_off = (unsigned long)&extref->name;
935 index = btrfs_inode_extref_index(eb, extref);
936 dir = btrfs_inode_extref_parent(eb, extref);
937 }
938
Alexander Block31db9f72012-07-25 23:19:24 +0200939 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000940 start = btrfs_ref_to_path(root, tmp_path, name_len,
941 name_off, eb, dir,
942 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200943 if (IS_ERR(start)) {
944 ret = PTR_ERR(start);
945 goto out;
946 }
947 if (start < p->buf) {
948 /* overflow , try again with larger buffer */
949 ret = fs_path_ensure_buf(p,
950 p->buf_len + p->buf - start);
951 if (ret < 0)
952 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000953 start = btrfs_ref_to_path(root, tmp_path,
954 name_len, name_off,
955 eb, dir,
956 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200957 if (IS_ERR(start)) {
958 ret = PTR_ERR(start);
959 goto out;
960 }
961 BUG_ON(start < p->buf);
962 }
963 p->start = start;
964 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000965 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
966 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200967 if (ret < 0)
968 goto out;
969 }
970
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000971 cur += elem_size + name_len;
972 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200973 if (ret)
974 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200975 num++;
976 }
977
978out:
979 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000980 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200981 return ret;
982}
983
984typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
985 const char *name, int name_len,
986 const char *data, int data_len,
987 u8 type, void *ctx);
988
989/*
990 * Helper function to iterate the entries in ONE btrfs_dir_item.
991 * The iterate callback may return a non zero value to stop iteration. This can
992 * be a negative value for error codes or 1 to simply stop it.
993 *
994 * path must point to the dir item when called.
995 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000996static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200997 iterate_dir_item_t iterate, void *ctx)
998{
999 int ret = 0;
1000 struct extent_buffer *eb;
1001 struct btrfs_item *item;
1002 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +02001003 struct btrfs_key di_key;
1004 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +01001005 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +02001006 u32 name_len;
1007 u32 data_len;
1008 u32 cur;
1009 u32 len;
1010 u32 total;
1011 int slot;
1012 int num;
1013 u8 type;
1014
Filipe Manana4395e0c2014-08-20 10:45:45 +01001015 /*
1016 * Start with a small buffer (1 page). If later we end up needing more
1017 * space, which can happen for xattrs on a fs with a leaf size greater
1018 * then the page size, attempt to increase the buffer. Typically xattr
1019 * values are small.
1020 */
1021 buf_len = PATH_MAX;
David Sterbae780b0d2016-01-18 18:42:13 +01001022 buf = kmalloc(buf_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02001023 if (!buf) {
1024 ret = -ENOMEM;
1025 goto out;
1026 }
1027
Alexander Block31db9f72012-07-25 23:19:24 +02001028 eb = path->nodes[0];
1029 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001030 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001031 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1032 cur = 0;
1033 len = 0;
1034 total = btrfs_item_size(eb, item);
1035
1036 num = 0;
1037 while (cur < total) {
1038 name_len = btrfs_dir_name_len(eb, di);
1039 data_len = btrfs_dir_data_len(eb, di);
1040 type = btrfs_dir_type(eb, di);
1041 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1042
Filipe Manana7e3ae332014-05-23 20:15:16 +01001043 if (type == BTRFS_FT_XATTR) {
1044 if (name_len > XATTR_NAME_MAX) {
1045 ret = -ENAMETOOLONG;
1046 goto out;
1047 }
Jeff Mahoneyda170662016-06-15 09:22:56 -04001048 if (name_len + data_len >
1049 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001050 ret = -E2BIG;
1051 goto out;
1052 }
1053 } else {
1054 /*
1055 * Path too long
1056 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001057 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001058 ret = -ENAMETOOLONG;
1059 goto out;
1060 }
Alexander Block31db9f72012-07-25 23:19:24 +02001061 }
1062
Filipe Manana4395e0c2014-08-20 10:45:45 +01001063 if (name_len + data_len > buf_len) {
1064 buf_len = name_len + data_len;
1065 if (is_vmalloc_addr(buf)) {
1066 vfree(buf);
1067 buf = NULL;
1068 } else {
1069 char *tmp = krealloc(buf, buf_len,
David Sterbae780b0d2016-01-18 18:42:13 +01001070 GFP_KERNEL | __GFP_NOWARN);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001071
1072 if (!tmp)
1073 kfree(buf);
1074 buf = tmp;
1075 }
1076 if (!buf) {
David Sterbaf11f7442017-05-31 18:40:02 +02001077 buf = kvmalloc(buf_len, GFP_KERNEL);
Filipe Manana4395e0c2014-08-20 10:45:45 +01001078 if (!buf) {
1079 ret = -ENOMEM;
1080 goto out;
1081 }
1082 }
1083 }
1084
Alexander Block31db9f72012-07-25 23:19:24 +02001085 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1086 name_len + data_len);
1087
1088 len = sizeof(*di) + name_len + data_len;
1089 di = (struct btrfs_dir_item *)((char *)di + len);
1090 cur += len;
1091
1092 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1093 data_len, type, ctx);
1094 if (ret < 0)
1095 goto out;
1096 if (ret) {
1097 ret = 0;
1098 goto out;
1099 }
1100
1101 num++;
1102 }
1103
1104out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001105 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001106 return ret;
1107}
1108
1109static int __copy_first_ref(int num, u64 dir, int index,
1110 struct fs_path *p, void *ctx)
1111{
1112 int ret;
1113 struct fs_path *pt = ctx;
1114
1115 ret = fs_path_copy(pt, p);
1116 if (ret < 0)
1117 return ret;
1118
1119 /* we want the first only */
1120 return 1;
1121}
1122
1123/*
1124 * Retrieve the first path of an inode. If an inode has more then one
1125 * ref/hardlink, this is ignored.
1126 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001127static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001128 u64 ino, struct fs_path *path)
1129{
1130 int ret;
1131 struct btrfs_key key, found_key;
1132 struct btrfs_path *p;
1133
1134 p = alloc_path_for_send();
1135 if (!p)
1136 return -ENOMEM;
1137
1138 fs_path_reset(path);
1139
1140 key.objectid = ino;
1141 key.type = BTRFS_INODE_REF_KEY;
1142 key.offset = 0;
1143
1144 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1145 if (ret < 0)
1146 goto out;
1147 if (ret) {
1148 ret = 1;
1149 goto out;
1150 }
1151 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1152 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001153 (found_key.type != BTRFS_INODE_REF_KEY &&
1154 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001155 ret = -ENOENT;
1156 goto out;
1157 }
1158
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001159 ret = iterate_inode_ref(root, p, &found_key, 1,
1160 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001161 if (ret < 0)
1162 goto out;
1163 ret = 0;
1164
1165out:
1166 btrfs_free_path(p);
1167 return ret;
1168}
1169
1170struct backref_ctx {
1171 struct send_ctx *sctx;
1172
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001173 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001174 /* number of total found references */
1175 u64 found;
1176
1177 /*
1178 * used for clones found in send_root. clones found behind cur_objectid
1179 * and cur_offset are not considered as allowed clones.
1180 */
1181 u64 cur_objectid;
1182 u64 cur_offset;
1183
1184 /* may be truncated in case it's the last extent in a file */
1185 u64 extent_len;
1186
Filipe Manana619d8c42015-05-03 01:56:00 +01001187 /* data offset in the file extent item */
1188 u64 data_offset;
1189
Alexander Block31db9f72012-07-25 23:19:24 +02001190 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001191 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001192};
1193
1194static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1195{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001196 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001197 struct clone_root *cr = (struct clone_root *)elt;
1198
1199 if (root < cr->root->objectid)
1200 return -1;
1201 if (root > cr->root->objectid)
1202 return 1;
1203 return 0;
1204}
1205
1206static int __clone_root_cmp_sort(const void *e1, const void *e2)
1207{
1208 struct clone_root *cr1 = (struct clone_root *)e1;
1209 struct clone_root *cr2 = (struct clone_root *)e2;
1210
1211 if (cr1->root->objectid < cr2->root->objectid)
1212 return -1;
1213 if (cr1->root->objectid > cr2->root->objectid)
1214 return 1;
1215 return 0;
1216}
1217
1218/*
1219 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001220 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001221 */
1222static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1223{
1224 struct backref_ctx *bctx = ctx_;
1225 struct clone_root *found;
1226 int ret;
1227 u64 i_size;
1228
1229 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001230 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001231 bctx->sctx->clone_roots_cnt,
1232 sizeof(struct clone_root),
1233 __clone_root_cmp_bsearch);
1234 if (!found)
1235 return 0;
1236
1237 if (found->root == bctx->sctx->send_root &&
1238 ino == bctx->cur_objectid &&
1239 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001240 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001241 }
1242
1243 /*
Alexander Block766702e2012-07-28 14:11:31 +02001244 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001245 * accept clones from these extents.
1246 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001247 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1248 NULL, NULL, NULL);
1249 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001250 if (ret < 0)
1251 return ret;
1252
Filipe Manana619d8c42015-05-03 01:56:00 +01001253 if (offset + bctx->data_offset + bctx->extent_len > i_size)
Alexander Block31db9f72012-07-25 23:19:24 +02001254 return 0;
1255
1256 /*
1257 * Make sure we don't consider clones from send_root that are
1258 * behind the current inode/offset.
1259 */
1260 if (found->root == bctx->sctx->send_root) {
1261 /*
1262 * TODO for the moment we don't accept clones from the inode
1263 * that is currently send. We may change this when
1264 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1265 * file.
1266 */
1267 if (ino >= bctx->cur_objectid)
1268 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001269 }
1270
1271 bctx->found++;
1272 found->found_refs++;
1273 if (ino < found->ino) {
1274 found->ino = ino;
1275 found->offset = offset;
1276 } else if (found->ino == ino) {
1277 /*
1278 * same extent found more then once in the same file.
1279 */
1280 if (found->offset > offset + bctx->extent_len)
1281 found->offset = offset;
1282 }
1283
1284 return 0;
1285}
1286
1287/*
Alexander Block766702e2012-07-28 14:11:31 +02001288 * Given an inode, offset and extent item, it finds a good clone for a clone
1289 * instruction. Returns -ENOENT when none could be found. The function makes
1290 * sure that the returned clone is usable at the point where sending is at the
1291 * moment. This means, that no clones are accepted which lie behind the current
1292 * inode+offset.
1293 *
Alexander Block31db9f72012-07-25 23:19:24 +02001294 * path must point to the extent item when called.
1295 */
1296static int find_extent_clone(struct send_ctx *sctx,
1297 struct btrfs_path *path,
1298 u64 ino, u64 data_offset,
1299 u64 ino_size,
1300 struct clone_root **found)
1301{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001302 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02001303 int ret;
1304 int extent_type;
1305 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001306 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001307 u64 num_bytes;
1308 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001309 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001310 struct btrfs_file_extent_item *fi;
1311 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001312 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001313 struct clone_root *cur_clone_root;
1314 struct btrfs_key found_key;
1315 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001316 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001317 u32 i;
1318
1319 tmp_path = alloc_path_for_send();
1320 if (!tmp_path)
1321 return -ENOMEM;
1322
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001323 /* We only use this path under the commit sem */
1324 tmp_path->need_commit_sem = 0;
1325
David Sterbae780b0d2016-01-18 18:42:13 +01001326 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
Alexander Block35075bb2012-07-28 12:44:34 +02001327 if (!backref_ctx) {
1328 ret = -ENOMEM;
1329 goto out;
1330 }
1331
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001332 backref_ctx->path = tmp_path;
1333
Alexander Block31db9f72012-07-25 23:19:24 +02001334 if (data_offset >= ino_size) {
1335 /*
1336 * There may be extents that lie behind the file's size.
1337 * I at least had this in combination with snapshotting while
1338 * writing large files.
1339 */
1340 ret = 0;
1341 goto out;
1342 }
1343
1344 fi = btrfs_item_ptr(eb, path->slots[0],
1345 struct btrfs_file_extent_item);
1346 extent_type = btrfs_file_extent_type(eb, fi);
1347 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1348 ret = -ENOENT;
1349 goto out;
1350 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001351 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001352
1353 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001354 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1355 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001356 ret = -ENOENT;
1357 goto out;
1358 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001359 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001360
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001361 down_read(&fs_info->commit_root_sem);
1362 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
Liu Bo69917e42012-09-07 20:01:28 -06001363 &found_key, &flags);
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001364 up_read(&fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001365 btrfs_release_path(tmp_path);
1366
1367 if (ret < 0)
1368 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001369 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001370 ret = -EIO;
1371 goto out;
1372 }
1373
1374 /*
1375 * Setup the clone roots.
1376 */
1377 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1378 cur_clone_root = sctx->clone_roots + i;
1379 cur_clone_root->ino = (u64)-1;
1380 cur_clone_root->offset = 0;
1381 cur_clone_root->found_refs = 0;
1382 }
1383
Alexander Block35075bb2012-07-28 12:44:34 +02001384 backref_ctx->sctx = sctx;
1385 backref_ctx->found = 0;
1386 backref_ctx->cur_objectid = ino;
1387 backref_ctx->cur_offset = data_offset;
1388 backref_ctx->found_itself = 0;
1389 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001390 /*
1391 * For non-compressed extents iterate_extent_inodes() gives us extent
1392 * offsets that already take into account the data offset, but not for
1393 * compressed extents, since the offset is logical and not relative to
1394 * the physical extent locations. We must take this into account to
1395 * avoid sending clone offsets that go beyond the source file's size,
1396 * which would result in the clone ioctl failing with -EINVAL on the
1397 * receiving end.
1398 */
1399 if (compressed == BTRFS_COMPRESS_NONE)
1400 backref_ctx->data_offset = 0;
1401 else
1402 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001403
1404 /*
1405 * The last extent of a file may be too large due to page alignment.
1406 * We need to adjust extent_len in this case so that the checks in
1407 * __iterate_backrefs work.
1408 */
1409 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001410 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001411
1412 /*
1413 * Now collect all backrefs.
1414 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001415 if (compressed == BTRFS_COMPRESS_NONE)
1416 extent_item_pos = logical - found_key.objectid;
1417 else
1418 extent_item_pos = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001419 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1420 extent_item_pos, 1, __iterate_backrefs,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001421 backref_ctx, false);
Chris Mason74dd17f2012-08-07 16:25:13 -04001422
Alexander Block31db9f72012-07-25 23:19:24 +02001423 if (ret < 0)
1424 goto out;
1425
Alexander Block35075bb2012-07-28 12:44:34 +02001426 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001427 /* found a bug in backref code? */
1428 ret = -EIO;
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001429 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001430 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001431 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001432 goto out;
1433 }
1434
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001435 btrfs_debug(fs_info,
1436 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1437 data_offset, ino, num_bytes, logical);
Alexander Block31db9f72012-07-25 23:19:24 +02001438
Alexander Block35075bb2012-07-28 12:44:34 +02001439 if (!backref_ctx->found)
Jeff Mahoney04ab9562016-09-20 10:05:03 -04001440 btrfs_debug(fs_info, "no clones found");
Alexander Block31db9f72012-07-25 23:19:24 +02001441
1442 cur_clone_root = NULL;
1443 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1444 if (sctx->clone_roots[i].found_refs) {
1445 if (!cur_clone_root)
1446 cur_clone_root = sctx->clone_roots + i;
1447 else if (sctx->clone_roots[i].root == sctx->send_root)
1448 /* prefer clones from send_root over others */
1449 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001450 }
1451
1452 }
1453
1454 if (cur_clone_root) {
1455 *found = cur_clone_root;
1456 ret = 0;
1457 } else {
1458 ret = -ENOENT;
1459 }
1460
1461out:
1462 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001463 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001464 return ret;
1465}
1466
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001467static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001468 u64 ino,
1469 struct fs_path *dest)
1470{
1471 int ret;
1472 struct btrfs_path *path;
1473 struct btrfs_key key;
1474 struct btrfs_file_extent_item *ei;
1475 u8 type;
1476 u8 compression;
1477 unsigned long off;
1478 int len;
1479
1480 path = alloc_path_for_send();
1481 if (!path)
1482 return -ENOMEM;
1483
1484 key.objectid = ino;
1485 key.type = BTRFS_EXTENT_DATA_KEY;
1486 key.offset = 0;
1487 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1488 if (ret < 0)
1489 goto out;
Filipe Mananaa8797192015-12-31 18:07:59 +00001490 if (ret) {
1491 /*
1492 * An empty symlink inode. Can happen in rare error paths when
1493 * creating a symlink (transaction committed before the inode
1494 * eviction handler removed the symlink inode items and a crash
1495 * happened in between or the subvol was snapshoted in between).
1496 * Print an informative message to dmesg/syslog so that the user
1497 * can delete the symlink.
1498 */
1499 btrfs_err(root->fs_info,
1500 "Found empty symlink inode %llu at root %llu",
1501 ino, root->root_key.objectid);
1502 ret = -EIO;
1503 goto out;
1504 }
Alexander Block31db9f72012-07-25 23:19:24 +02001505
1506 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1507 struct btrfs_file_extent_item);
1508 type = btrfs_file_extent_type(path->nodes[0], ei);
1509 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1510 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1511 BUG_ON(compression);
1512
1513 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001514 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001515
1516 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001517
1518out:
1519 btrfs_free_path(path);
1520 return ret;
1521}
1522
1523/*
1524 * Helper function to generate a file name that is unique in the root of
1525 * send_root and parent_root. This is used to generate names for orphan inodes.
1526 */
1527static int gen_unique_name(struct send_ctx *sctx,
1528 u64 ino, u64 gen,
1529 struct fs_path *dest)
1530{
1531 int ret = 0;
1532 struct btrfs_path *path;
1533 struct btrfs_dir_item *di;
1534 char tmp[64];
1535 int len;
1536 u64 idx = 0;
1537
1538 path = alloc_path_for_send();
1539 if (!path)
1540 return -ENOMEM;
1541
1542 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001543 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001544 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001545 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001546
1547 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1548 path, BTRFS_FIRST_FREE_OBJECTID,
1549 tmp, strlen(tmp), 0);
1550 btrfs_release_path(path);
1551 if (IS_ERR(di)) {
1552 ret = PTR_ERR(di);
1553 goto out;
1554 }
1555 if (di) {
1556 /* not unique, try again */
1557 idx++;
1558 continue;
1559 }
1560
1561 if (!sctx->parent_root) {
1562 /* unique */
1563 ret = 0;
1564 break;
1565 }
1566
1567 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1568 path, BTRFS_FIRST_FREE_OBJECTID,
1569 tmp, strlen(tmp), 0);
1570 btrfs_release_path(path);
1571 if (IS_ERR(di)) {
1572 ret = PTR_ERR(di);
1573 goto out;
1574 }
1575 if (di) {
1576 /* not unique, try again */
1577 idx++;
1578 continue;
1579 }
1580 /* unique */
1581 break;
1582 }
1583
1584 ret = fs_path_add(dest, tmp, strlen(tmp));
1585
1586out:
1587 btrfs_free_path(path);
1588 return ret;
1589}
1590
1591enum inode_state {
1592 inode_state_no_change,
1593 inode_state_will_create,
1594 inode_state_did_create,
1595 inode_state_will_delete,
1596 inode_state_did_delete,
1597};
1598
1599static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1600{
1601 int ret;
1602 int left_ret;
1603 int right_ret;
1604 u64 left_gen;
1605 u64 right_gen;
1606
1607 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001608 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001609 if (ret < 0 && ret != -ENOENT)
1610 goto out;
1611 left_ret = ret;
1612
1613 if (!sctx->parent_root) {
1614 right_ret = -ENOENT;
1615 } else {
1616 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001617 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001618 if (ret < 0 && ret != -ENOENT)
1619 goto out;
1620 right_ret = ret;
1621 }
1622
1623 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001624 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001625 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001626 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001627 if (ino < sctx->send_progress)
1628 ret = inode_state_did_create;
1629 else
1630 ret = inode_state_will_create;
1631 } else if (right_gen == gen) {
1632 if (ino < sctx->send_progress)
1633 ret = inode_state_did_delete;
1634 else
1635 ret = inode_state_will_delete;
1636 } else {
1637 ret = -ENOENT;
1638 }
1639 } else if (!left_ret) {
1640 if (left_gen == gen) {
1641 if (ino < sctx->send_progress)
1642 ret = inode_state_did_create;
1643 else
1644 ret = inode_state_will_create;
1645 } else {
1646 ret = -ENOENT;
1647 }
1648 } else if (!right_ret) {
1649 if (right_gen == gen) {
1650 if (ino < sctx->send_progress)
1651 ret = inode_state_did_delete;
1652 else
1653 ret = inode_state_will_delete;
1654 } else {
1655 ret = -ENOENT;
1656 }
1657 } else {
1658 ret = -ENOENT;
1659 }
1660
1661out:
1662 return ret;
1663}
1664
1665static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1666{
1667 int ret;
1668
Robbie Ko4dd99202017-01-05 16:24:55 +08001669 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1670 return 1;
1671
Alexander Block31db9f72012-07-25 23:19:24 +02001672 ret = get_cur_inode_state(sctx, ino, gen);
1673 if (ret < 0)
1674 goto out;
1675
1676 if (ret == inode_state_no_change ||
1677 ret == inode_state_did_create ||
1678 ret == inode_state_will_delete)
1679 ret = 1;
1680 else
1681 ret = 0;
1682
1683out:
1684 return ret;
1685}
1686
1687/*
1688 * Helper function to lookup a dir item in a dir.
1689 */
1690static int lookup_dir_item_inode(struct btrfs_root *root,
1691 u64 dir, const char *name, int name_len,
1692 u64 *found_inode,
1693 u8 *found_type)
1694{
1695 int ret = 0;
1696 struct btrfs_dir_item *di;
1697 struct btrfs_key key;
1698 struct btrfs_path *path;
1699
1700 path = alloc_path_for_send();
1701 if (!path)
1702 return -ENOMEM;
1703
1704 di = btrfs_lookup_dir_item(NULL, root, path,
1705 dir, name, name_len, 0);
1706 if (!di) {
1707 ret = -ENOENT;
1708 goto out;
1709 }
1710 if (IS_ERR(di)) {
1711 ret = PTR_ERR(di);
1712 goto out;
1713 }
1714 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001715 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1716 ret = -ENOENT;
1717 goto out;
1718 }
Alexander Block31db9f72012-07-25 23:19:24 +02001719 *found_inode = key.objectid;
1720 *found_type = btrfs_dir_type(path->nodes[0], di);
1721
1722out:
1723 btrfs_free_path(path);
1724 return ret;
1725}
1726
Alexander Block766702e2012-07-28 14:11:31 +02001727/*
1728 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1729 * generation of the parent dir and the name of the dir entry.
1730 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001731static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001732 u64 *dir, u64 *dir_gen, struct fs_path *name)
1733{
1734 int ret;
1735 struct btrfs_key key;
1736 struct btrfs_key found_key;
1737 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001738 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001739 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001740
1741 path = alloc_path_for_send();
1742 if (!path)
1743 return -ENOMEM;
1744
1745 key.objectid = ino;
1746 key.type = BTRFS_INODE_REF_KEY;
1747 key.offset = 0;
1748
1749 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1750 if (ret < 0)
1751 goto out;
1752 if (!ret)
1753 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1754 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001755 if (ret || found_key.objectid != ino ||
1756 (found_key.type != BTRFS_INODE_REF_KEY &&
1757 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001758 ret = -ENOENT;
1759 goto out;
1760 }
1761
Filipe Manana51a60252014-05-13 22:01:02 +01001762 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001763 struct btrfs_inode_ref *iref;
1764 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1765 struct btrfs_inode_ref);
1766 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1767 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1768 (unsigned long)(iref + 1),
1769 len);
1770 parent_dir = found_key.offset;
1771 } else {
1772 struct btrfs_inode_extref *extref;
1773 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1774 struct btrfs_inode_extref);
1775 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1776 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1777 (unsigned long)&extref->name, len);
1778 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1779 }
Alexander Block31db9f72012-07-25 23:19:24 +02001780 if (ret < 0)
1781 goto out;
1782 btrfs_release_path(path);
1783
Filipe Mananab46ab972014-03-21 12:46:54 +00001784 if (dir_gen) {
1785 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1786 NULL, NULL, NULL);
1787 if (ret < 0)
1788 goto out;
1789 }
Alexander Block31db9f72012-07-25 23:19:24 +02001790
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001791 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001792
1793out:
1794 btrfs_free_path(path);
1795 return ret;
1796}
1797
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001798static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001799 u64 ino, u64 dir,
1800 const char *name, int name_len)
1801{
1802 int ret;
1803 struct fs_path *tmp_name;
1804 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001805
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001806 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001807 if (!tmp_name)
1808 return -ENOMEM;
1809
Filipe Mananab46ab972014-03-21 12:46:54 +00001810 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001811 if (ret < 0)
1812 goto out;
1813
Alexander Blockb9291af2012-07-28 11:07:18 +02001814 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001815 ret = 0;
1816 goto out;
1817 }
1818
Alexander Blocke938c8a2012-07-28 16:33:49 +02001819 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001820
1821out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001822 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001823 return ret;
1824}
1825
Alexander Block766702e2012-07-28 14:11:31 +02001826/*
1827 * Used by process_recorded_refs to determine if a new ref would overwrite an
1828 * already existing ref. In case it detects an overwrite, it returns the
1829 * inode/gen in who_ino/who_gen.
1830 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1831 * to make sure later references to the overwritten inode are possible.
1832 * Orphanizing is however only required for the first ref of an inode.
1833 * process_recorded_refs does an additional is_first_ref check to see if
1834 * orphanizing is really required.
1835 */
Alexander Block31db9f72012-07-25 23:19:24 +02001836static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1837 const char *name, int name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01001838 u64 *who_ino, u64 *who_gen, u64 *who_mode)
Alexander Block31db9f72012-07-25 23:19:24 +02001839{
1840 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001841 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001842 u64 other_inode = 0;
1843 u8 other_type = 0;
1844
1845 if (!sctx->parent_root)
1846 goto out;
1847
1848 ret = is_inode_existent(sctx, dir, dir_gen);
1849 if (ret <= 0)
1850 goto out;
1851
Josef Bacikebdad912013-08-06 16:47:48 -04001852 /*
1853 * If we have a parent root we need to verify that the parent dir was
Nicholas D Steeves01327612016-05-19 21:18:45 -04001854 * not deleted and then re-created, if it was then we have no overwrite
Josef Bacikebdad912013-08-06 16:47:48 -04001855 * and we can just unlink this entry.
1856 */
Robbie Ko4dd99202017-01-05 16:24:55 +08001857 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacikebdad912013-08-06 16:47:48 -04001858 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1859 NULL, NULL, NULL);
1860 if (ret < 0 && ret != -ENOENT)
1861 goto out;
1862 if (ret) {
1863 ret = 0;
1864 goto out;
1865 }
1866 if (gen != dir_gen)
1867 goto out;
1868 }
1869
Alexander Block31db9f72012-07-25 23:19:24 +02001870 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1871 &other_inode, &other_type);
1872 if (ret < 0 && ret != -ENOENT)
1873 goto out;
1874 if (ret) {
1875 ret = 0;
1876 goto out;
1877 }
1878
Alexander Block766702e2012-07-28 14:11:31 +02001879 /*
1880 * Check if the overwritten ref was already processed. If yes, the ref
1881 * was already unlinked/moved, so we can safely assume that we will not
1882 * overwrite anything at this point in time.
1883 */
Robbie Ko801bec32015-06-23 18:39:46 +08001884 if (other_inode > sctx->send_progress ||
1885 is_waiting_for_move(sctx, other_inode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001886 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Filipe Mananaf5962782017-06-22 20:03:51 +01001887 who_gen, who_mode, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001888 if (ret < 0)
1889 goto out;
1890
1891 ret = 1;
1892 *who_ino = other_inode;
1893 } else {
1894 ret = 0;
1895 }
1896
1897out:
1898 return ret;
1899}
1900
Alexander Block766702e2012-07-28 14:11:31 +02001901/*
1902 * Checks if the ref was overwritten by an already processed inode. This is
1903 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1904 * thus the orphan name needs be used.
1905 * process_recorded_refs also uses it to avoid unlinking of refs that were
1906 * overwritten.
1907 */
Alexander Block31db9f72012-07-25 23:19:24 +02001908static int did_overwrite_ref(struct send_ctx *sctx,
1909 u64 dir, u64 dir_gen,
1910 u64 ino, u64 ino_gen,
1911 const char *name, int name_len)
1912{
1913 int ret = 0;
1914 u64 gen;
1915 u64 ow_inode;
1916 u8 other_type;
1917
1918 if (!sctx->parent_root)
1919 goto out;
1920
1921 ret = is_inode_existent(sctx, dir, dir_gen);
1922 if (ret <= 0)
1923 goto out;
1924
Robbie Ko01914102017-01-05 16:24:58 +08001925 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1926 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1927 NULL, NULL, NULL);
1928 if (ret < 0 && ret != -ENOENT)
1929 goto out;
1930 if (ret) {
1931 ret = 0;
1932 goto out;
1933 }
1934 if (gen != dir_gen)
1935 goto out;
1936 }
1937
Alexander Block31db9f72012-07-25 23:19:24 +02001938 /* check if the ref was overwritten by another ref */
1939 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1940 &ow_inode, &other_type);
1941 if (ret < 0 && ret != -ENOENT)
1942 goto out;
1943 if (ret) {
1944 /* was never and will never be overwritten */
1945 ret = 0;
1946 goto out;
1947 }
1948
1949 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001950 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001951 if (ret < 0)
1952 goto out;
1953
1954 if (ow_inode == ino && gen == ino_gen) {
1955 ret = 0;
1956 goto out;
1957 }
1958
Filipe Manana8b191a62015-04-09 14:09:14 +01001959 /*
1960 * We know that it is or will be overwritten. Check this now.
1961 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001962 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1963 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001964 */
Filipe Mananab786f162015-09-26 15:30:23 +01001965 if ((ow_inode < sctx->send_progress) ||
1966 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1967 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001968 ret = 1;
1969 else
1970 ret = 0;
1971
1972out:
1973 return ret;
1974}
1975
Alexander Block766702e2012-07-28 14:11:31 +02001976/*
1977 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1978 * that got overwritten. This is used by process_recorded_refs to determine
1979 * if it has to use the path as returned by get_cur_path or the orphan name.
1980 */
Alexander Block31db9f72012-07-25 23:19:24 +02001981static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1982{
1983 int ret = 0;
1984 struct fs_path *name = NULL;
1985 u64 dir;
1986 u64 dir_gen;
1987
1988 if (!sctx->parent_root)
1989 goto out;
1990
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001991 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001992 if (!name)
1993 return -ENOMEM;
1994
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001995 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001996 if (ret < 0)
1997 goto out;
1998
1999 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2000 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02002001
2002out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002003 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002004 return ret;
2005}
2006
Alexander Block766702e2012-07-28 14:11:31 +02002007/*
2008 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2009 * so we need to do some special handling in case we have clashes. This function
2010 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02002011 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02002012 */
Alexander Block31db9f72012-07-25 23:19:24 +02002013static int name_cache_insert(struct send_ctx *sctx,
2014 struct name_cache_entry *nce)
2015{
2016 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02002017 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002018
Alexander Block7e0926f2012-07-28 14:20:58 +02002019 nce_head = radix_tree_lookup(&sctx->name_cache,
2020 (unsigned long)nce->ino);
2021 if (!nce_head) {
David Sterbae780b0d2016-01-18 18:42:13 +01002022 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002023 if (!nce_head) {
2024 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002025 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00002026 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002027 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002028
Alexander Block7e0926f2012-07-28 14:20:58 +02002029 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02002030 if (ret < 0) {
2031 kfree(nce_head);
2032 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002033 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02002034 }
Alexander Block31db9f72012-07-25 23:19:24 +02002035 }
Alexander Block7e0926f2012-07-28 14:20:58 +02002036 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02002037 list_add_tail(&nce->list, &sctx->name_cache_list);
2038 sctx->name_cache_size++;
2039
2040 return ret;
2041}
2042
2043static void name_cache_delete(struct send_ctx *sctx,
2044 struct name_cache_entry *nce)
2045{
Alexander Block7e0926f2012-07-28 14:20:58 +02002046 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02002047
Alexander Block7e0926f2012-07-28 14:20:58 +02002048 nce_head = radix_tree_lookup(&sctx->name_cache,
2049 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002050 if (!nce_head) {
2051 btrfs_err(sctx->send_root->fs_info,
2052 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2053 nce->ino, sctx->name_cache_size);
2054 }
Alexander Block31db9f72012-07-25 23:19:24 +02002055
Alexander Block7e0926f2012-07-28 14:20:58 +02002056 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002057 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002058 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002059
David Sterba57fb8912014-02-03 19:24:40 +01002060 /*
2061 * We may not get to the final release of nce_head if the lookup fails
2062 */
2063 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002064 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2065 kfree(nce_head);
2066 }
Alexander Block31db9f72012-07-25 23:19:24 +02002067}
2068
2069static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2070 u64 ino, u64 gen)
2071{
Alexander Block7e0926f2012-07-28 14:20:58 +02002072 struct list_head *nce_head;
2073 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002074
Alexander Block7e0926f2012-07-28 14:20:58 +02002075 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2076 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002077 return NULL;
2078
Alexander Block7e0926f2012-07-28 14:20:58 +02002079 list_for_each_entry(cur, nce_head, radix_list) {
2080 if (cur->ino == ino && cur->gen == gen)
2081 return cur;
2082 }
Alexander Block31db9f72012-07-25 23:19:24 +02002083 return NULL;
2084}
2085
Alexander Block766702e2012-07-28 14:11:31 +02002086/*
2087 * Removes the entry from the list and adds it back to the end. This marks the
2088 * entry as recently used so that name_cache_clean_unused does not remove it.
2089 */
Alexander Block31db9f72012-07-25 23:19:24 +02002090static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2091{
2092 list_del(&nce->list);
2093 list_add_tail(&nce->list, &sctx->name_cache_list);
2094}
2095
Alexander Block766702e2012-07-28 14:11:31 +02002096/*
2097 * Remove some entries from the beginning of name_cache_list.
2098 */
Alexander Block31db9f72012-07-25 23:19:24 +02002099static void name_cache_clean_unused(struct send_ctx *sctx)
2100{
2101 struct name_cache_entry *nce;
2102
2103 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2104 return;
2105
2106 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2107 nce = list_entry(sctx->name_cache_list.next,
2108 struct name_cache_entry, list);
2109 name_cache_delete(sctx, nce);
2110 kfree(nce);
2111 }
2112}
2113
2114static void name_cache_free(struct send_ctx *sctx)
2115{
2116 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002117
Alexander Blocke938c8a2012-07-28 16:33:49 +02002118 while (!list_empty(&sctx->name_cache_list)) {
2119 nce = list_entry(sctx->name_cache_list.next,
2120 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002121 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002122 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002123 }
2124}
2125
Alexander Block766702e2012-07-28 14:11:31 +02002126/*
2127 * Used by get_cur_path for each ref up to the root.
2128 * Returns 0 if it succeeded.
2129 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2130 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2131 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2132 * Returns <0 in case of error.
2133 */
Alexander Block31db9f72012-07-25 23:19:24 +02002134static int __get_cur_name_and_parent(struct send_ctx *sctx,
2135 u64 ino, u64 gen,
2136 u64 *parent_ino,
2137 u64 *parent_gen,
2138 struct fs_path *dest)
2139{
2140 int ret;
2141 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002142 struct name_cache_entry *nce = NULL;
2143
Alexander Block766702e2012-07-28 14:11:31 +02002144 /*
2145 * First check if we already did a call to this function with the same
2146 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2147 * return the cached result.
2148 */
Alexander Block31db9f72012-07-25 23:19:24 +02002149 nce = name_cache_search(sctx, ino, gen);
2150 if (nce) {
2151 if (ino < sctx->send_progress && nce->need_later_update) {
2152 name_cache_delete(sctx, nce);
2153 kfree(nce);
2154 nce = NULL;
2155 } else {
2156 name_cache_used(sctx, nce);
2157 *parent_ino = nce->parent_ino;
2158 *parent_gen = nce->parent_gen;
2159 ret = fs_path_add(dest, nce->name, nce->name_len);
2160 if (ret < 0)
2161 goto out;
2162 ret = nce->ret;
2163 goto out;
2164 }
2165 }
2166
Alexander Block766702e2012-07-28 14:11:31 +02002167 /*
2168 * If the inode is not existent yet, add the orphan name and return 1.
2169 * This should only happen for the parent dir that we determine in
2170 * __record_new_ref
2171 */
Alexander Block31db9f72012-07-25 23:19:24 +02002172 ret = is_inode_existent(sctx, ino, gen);
2173 if (ret < 0)
2174 goto out;
2175
2176 if (!ret) {
2177 ret = gen_unique_name(sctx, ino, gen, dest);
2178 if (ret < 0)
2179 goto out;
2180 ret = 1;
2181 goto out_cache;
2182 }
2183
Alexander Block766702e2012-07-28 14:11:31 +02002184 /*
2185 * Depending on whether the inode was already processed or not, use
2186 * send_root or parent_root for ref lookup.
2187 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002188 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002189 ret = get_first_ref(sctx->send_root, ino,
2190 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002191 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002192 ret = get_first_ref(sctx->parent_root, ino,
2193 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002194 if (ret < 0)
2195 goto out;
2196
Alexander Block766702e2012-07-28 14:11:31 +02002197 /*
2198 * Check if the ref was overwritten by an inode's ref that was processed
2199 * earlier. If yes, treat as orphan and return 1.
2200 */
Alexander Block31db9f72012-07-25 23:19:24 +02002201 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2202 dest->start, dest->end - dest->start);
2203 if (ret < 0)
2204 goto out;
2205 if (ret) {
2206 fs_path_reset(dest);
2207 ret = gen_unique_name(sctx, ino, gen, dest);
2208 if (ret < 0)
2209 goto out;
2210 ret = 1;
2211 }
2212
2213out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002214 /*
2215 * Store the result of the lookup in the name cache.
2216 */
David Sterbae780b0d2016-01-18 18:42:13 +01002217 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002218 if (!nce) {
2219 ret = -ENOMEM;
2220 goto out;
2221 }
2222
2223 nce->ino = ino;
2224 nce->gen = gen;
2225 nce->parent_ino = *parent_ino;
2226 nce->parent_gen = *parent_gen;
2227 nce->name_len = fs_path_len(dest);
2228 nce->ret = ret;
2229 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002230
2231 if (ino < sctx->send_progress)
2232 nce->need_later_update = 0;
2233 else
2234 nce->need_later_update = 1;
2235
2236 nce_ret = name_cache_insert(sctx, nce);
2237 if (nce_ret < 0)
2238 ret = nce_ret;
2239 name_cache_clean_unused(sctx);
2240
2241out:
Alexander Block31db9f72012-07-25 23:19:24 +02002242 return ret;
2243}
2244
2245/*
2246 * Magic happens here. This function returns the first ref to an inode as it
2247 * would look like while receiving the stream at this point in time.
2248 * We walk the path up to the root. For every inode in between, we check if it
2249 * was already processed/sent. If yes, we continue with the parent as found
2250 * in send_root. If not, we continue with the parent as found in parent_root.
2251 * If we encounter an inode that was deleted at this point in time, we use the
2252 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2253 * that were not created yet and overwritten inodes/refs.
2254 *
2255 * When do we have have orphan inodes:
2256 * 1. When an inode is freshly created and thus no valid refs are available yet
2257 * 2. When a directory lost all it's refs (deleted) but still has dir items
2258 * inside which were not processed yet (pending for move/delete). If anyone
2259 * tried to get the path to the dir items, it would get a path inside that
2260 * orphan directory.
2261 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2262 * of an unprocessed inode. If in that case the first ref would be
2263 * overwritten, the overwritten inode gets "orphanized". Later when we
2264 * process this overwritten inode, it is restored at a new place by moving
2265 * the orphan inode.
2266 *
2267 * sctx->send_progress tells this function at which point in time receiving
2268 * would be.
2269 */
2270static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2271 struct fs_path *dest)
2272{
2273 int ret = 0;
2274 struct fs_path *name = NULL;
2275 u64 parent_inode = 0;
2276 u64 parent_gen = 0;
2277 int stop = 0;
2278
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002279 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002280 if (!name) {
2281 ret = -ENOMEM;
2282 goto out;
2283 }
2284
2285 dest->reversed = 1;
2286 fs_path_reset(dest);
2287
2288 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002289 struct waiting_dir_move *wdm;
2290
Alexander Block31db9f72012-07-25 23:19:24 +02002291 fs_path_reset(name);
2292
Filipe Manana9dc44212014-02-19 14:31:44 +00002293 if (is_waiting_for_rm(sctx, ino)) {
2294 ret = gen_unique_name(sctx, ino, gen, name);
2295 if (ret < 0)
2296 goto out;
2297 ret = fs_path_add_path(dest, name);
2298 break;
2299 }
2300
Filipe Manana8b191a62015-04-09 14:09:14 +01002301 wdm = get_waiting_dir_move(sctx, ino);
2302 if (wdm && wdm->orphanized) {
2303 ret = gen_unique_name(sctx, ino, gen, name);
2304 stop = 1;
2305 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002306 ret = get_first_ref(sctx->parent_root, ino,
2307 &parent_inode, &parent_gen, name);
2308 } else {
2309 ret = __get_cur_name_and_parent(sctx, ino, gen,
2310 &parent_inode,
2311 &parent_gen, name);
2312 if (ret)
2313 stop = 1;
2314 }
2315
Alexander Block31db9f72012-07-25 23:19:24 +02002316 if (ret < 0)
2317 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002318
Alexander Block31db9f72012-07-25 23:19:24 +02002319 ret = fs_path_add_path(dest, name);
2320 if (ret < 0)
2321 goto out;
2322
2323 ino = parent_inode;
2324 gen = parent_gen;
2325 }
2326
2327out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002328 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002329 if (!ret)
2330 fs_path_unreverse(dest);
2331 return ret;
2332}
2333
2334/*
Alexander Block31db9f72012-07-25 23:19:24 +02002335 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2336 */
2337static int send_subvol_begin(struct send_ctx *sctx)
2338{
2339 int ret;
2340 struct btrfs_root *send_root = sctx->send_root;
2341 struct btrfs_root *parent_root = sctx->parent_root;
2342 struct btrfs_path *path;
2343 struct btrfs_key key;
2344 struct btrfs_root_ref *ref;
2345 struct extent_buffer *leaf;
2346 char *name = NULL;
2347 int namelen;
2348
Wang Shilongffcfaf82014-01-15 00:26:43 +08002349 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002350 if (!path)
2351 return -ENOMEM;
2352
David Sterbae780b0d2016-01-18 18:42:13 +01002353 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002354 if (!name) {
2355 btrfs_free_path(path);
2356 return -ENOMEM;
2357 }
2358
2359 key.objectid = send_root->objectid;
2360 key.type = BTRFS_ROOT_BACKREF_KEY;
2361 key.offset = 0;
2362
2363 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2364 &key, path, 1, 0);
2365 if (ret < 0)
2366 goto out;
2367 if (ret) {
2368 ret = -ENOENT;
2369 goto out;
2370 }
2371
2372 leaf = path->nodes[0];
2373 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2374 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2375 key.objectid != send_root->objectid) {
2376 ret = -ENOENT;
2377 goto out;
2378 }
2379 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2380 namelen = btrfs_root_ref_name_len(leaf, ref);
2381 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2382 btrfs_release_path(path);
2383
Alexander Block31db9f72012-07-25 23:19:24 +02002384 if (parent_root) {
2385 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2386 if (ret < 0)
2387 goto out;
2388 } else {
2389 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2390 if (ret < 0)
2391 goto out;
2392 }
2393
2394 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
Robin Ruedeb96b1db2015-09-30 21:23:33 +02002395
2396 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2397 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2398 sctx->send_root->root_item.received_uuid);
2399 else
2400 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2401 sctx->send_root->root_item.uuid);
2402
Alexander Block31db9f72012-07-25 23:19:24 +02002403 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002404 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002405 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002406 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2407 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2408 parent_root->root_item.received_uuid);
2409 else
2410 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2411 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002412 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002413 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002414 }
2415
2416 ret = send_cmd(sctx);
2417
2418tlv_put_failure:
2419out:
2420 btrfs_free_path(path);
2421 kfree(name);
2422 return ret;
2423}
2424
2425static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2426{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002427 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002428 int ret = 0;
2429 struct fs_path *p;
2430
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002431 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
Alexander Block31db9f72012-07-25 23:19:24 +02002432
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002433 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002434 if (!p)
2435 return -ENOMEM;
2436
2437 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2438 if (ret < 0)
2439 goto out;
2440
2441 ret = get_cur_path(sctx, ino, gen, p);
2442 if (ret < 0)
2443 goto out;
2444 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2445 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2446
2447 ret = send_cmd(sctx);
2448
2449tlv_put_failure:
2450out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002451 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002452 return ret;
2453}
2454
2455static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2456{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002457 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002458 int ret = 0;
2459 struct fs_path *p;
2460
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002461 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002462
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002463 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002464 if (!p)
2465 return -ENOMEM;
2466
2467 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2468 if (ret < 0)
2469 goto out;
2470
2471 ret = get_cur_path(sctx, ino, gen, p);
2472 if (ret < 0)
2473 goto out;
2474 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2475 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2476
2477 ret = send_cmd(sctx);
2478
2479tlv_put_failure:
2480out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002481 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002482 return ret;
2483}
2484
2485static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2486{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002487 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002488 int ret = 0;
2489 struct fs_path *p;
2490
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002491 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2492 ino, uid, gid);
Alexander Block31db9f72012-07-25 23:19:24 +02002493
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002494 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002495 if (!p)
2496 return -ENOMEM;
2497
2498 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2499 if (ret < 0)
2500 goto out;
2501
2502 ret = get_cur_path(sctx, ino, gen, p);
2503 if (ret < 0)
2504 goto out;
2505 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2506 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2507 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2508
2509 ret = send_cmd(sctx);
2510
2511tlv_put_failure:
2512out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002513 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002514 return ret;
2515}
2516
2517static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2518{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002519 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002520 int ret = 0;
2521 struct fs_path *p = NULL;
2522 struct btrfs_inode_item *ii;
2523 struct btrfs_path *path = NULL;
2524 struct extent_buffer *eb;
2525 struct btrfs_key key;
2526 int slot;
2527
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002528 btrfs_debug(fs_info, "send_utimes %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002529
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002530 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002531 if (!p)
2532 return -ENOMEM;
2533
2534 path = alloc_path_for_send();
2535 if (!path) {
2536 ret = -ENOMEM;
2537 goto out;
2538 }
2539
2540 key.objectid = ino;
2541 key.type = BTRFS_INODE_ITEM_KEY;
2542 key.offset = 0;
2543 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
Filipe Manana15b253e2016-07-02 05:43:46 +01002544 if (ret > 0)
2545 ret = -ENOENT;
Alexander Block31db9f72012-07-25 23:19:24 +02002546 if (ret < 0)
2547 goto out;
2548
2549 eb = path->nodes[0];
2550 slot = path->slots[0];
2551 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2552
2553 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2554 if (ret < 0)
2555 goto out;
2556
2557 ret = get_cur_path(sctx, ino, gen, p);
2558 if (ret < 0)
2559 goto out;
2560 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002561 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2562 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2563 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002564 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002565
2566 ret = send_cmd(sctx);
2567
2568tlv_put_failure:
2569out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002570 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002571 btrfs_free_path(path);
2572 return ret;
2573}
2574
2575/*
2576 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2577 * a valid path yet because we did not process the refs yet. So, the inode
2578 * is created as orphan.
2579 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002580static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002581{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002582 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02002583 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002584 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002585 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002586 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002587 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002588 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002589
Jeff Mahoney04ab9562016-09-20 10:05:03 -04002590 btrfs_debug(fs_info, "send_create_inode %llu", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002591
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002592 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002593 if (!p)
2594 return -ENOMEM;
2595
Liu Bo644d1942014-02-27 17:29:01 +08002596 if (ino != sctx->cur_ino) {
2597 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2598 NULL, NULL, &rdev);
2599 if (ret < 0)
2600 goto out;
2601 } else {
2602 gen = sctx->cur_inode_gen;
2603 mode = sctx->cur_inode_mode;
2604 rdev = sctx->cur_inode_rdev;
2605 }
Alexander Block31db9f72012-07-25 23:19:24 +02002606
Alexander Blocke938c8a2012-07-28 16:33:49 +02002607 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002608 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002609 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002610 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002611 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002612 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002613 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002614 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002615 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002616 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002617 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002618 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002619 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002620 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002621 (int)(mode & S_IFMT));
Tsutomu Itohca6842b2016-01-22 09:13:25 +09002622 ret = -EOPNOTSUPP;
Alexander Block31db9f72012-07-25 23:19:24 +02002623 goto out;
2624 }
2625
2626 ret = begin_cmd(sctx, cmd);
2627 if (ret < 0)
2628 goto out;
2629
Alexander Block1f4692d2012-07-28 10:42:24 +02002630 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002631 if (ret < 0)
2632 goto out;
2633
2634 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002635 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002636
2637 if (S_ISLNK(mode)) {
2638 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002639 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002640 if (ret < 0)
2641 goto out;
2642 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2643 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2644 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002645 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2646 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002647 }
2648
2649 ret = send_cmd(sctx);
2650 if (ret < 0)
2651 goto out;
2652
2653
2654tlv_put_failure:
2655out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002656 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002657 return ret;
2658}
2659
Alexander Block1f4692d2012-07-28 10:42:24 +02002660/*
2661 * We need some special handling for inodes that get processed before the parent
2662 * directory got created. See process_recorded_refs for details.
2663 * This function does the check if we already created the dir out of order.
2664 */
2665static int did_create_dir(struct send_ctx *sctx, u64 dir)
2666{
2667 int ret = 0;
2668 struct btrfs_path *path = NULL;
2669 struct btrfs_key key;
2670 struct btrfs_key found_key;
2671 struct btrfs_key di_key;
2672 struct extent_buffer *eb;
2673 struct btrfs_dir_item *di;
2674 int slot;
2675
2676 path = alloc_path_for_send();
2677 if (!path) {
2678 ret = -ENOMEM;
2679 goto out;
2680 }
2681
2682 key.objectid = dir;
2683 key.type = BTRFS_DIR_INDEX_KEY;
2684 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002685 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2686 if (ret < 0)
2687 goto out;
2688
Alexander Block1f4692d2012-07-28 10:42:24 +02002689 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002690 eb = path->nodes[0];
2691 slot = path->slots[0];
2692 if (slot >= btrfs_header_nritems(eb)) {
2693 ret = btrfs_next_leaf(sctx->send_root, path);
2694 if (ret < 0) {
2695 goto out;
2696 } else if (ret > 0) {
2697 ret = 0;
2698 break;
2699 }
2700 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002701 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002702
2703 btrfs_item_key_to_cpu(eb, &found_key, slot);
2704 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002705 found_key.type != key.type) {
2706 ret = 0;
2707 goto out;
2708 }
2709
2710 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2711 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2712
Josef Bacika0525412013-08-12 10:56:14 -04002713 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2714 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002715 ret = 1;
2716 goto out;
2717 }
2718
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002719 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002720 }
2721
2722out:
2723 btrfs_free_path(path);
2724 return ret;
2725}
2726
2727/*
2728 * Only creates the inode if it is:
2729 * 1. Not a directory
2730 * 2. Or a directory which was not created already due to out of order
2731 * directories. See did_create_dir and process_recorded_refs for details.
2732 */
2733static int send_create_inode_if_needed(struct send_ctx *sctx)
2734{
2735 int ret;
2736
2737 if (S_ISDIR(sctx->cur_inode_mode)) {
2738 ret = did_create_dir(sctx, sctx->cur_ino);
2739 if (ret < 0)
2740 goto out;
2741 if (ret) {
2742 ret = 0;
2743 goto out;
2744 }
2745 }
2746
2747 ret = send_create_inode(sctx, sctx->cur_ino);
2748 if (ret < 0)
2749 goto out;
2750
2751out:
2752 return ret;
2753}
2754
Alexander Block31db9f72012-07-25 23:19:24 +02002755struct recorded_ref {
2756 struct list_head list;
Alexander Block31db9f72012-07-25 23:19:24 +02002757 char *name;
2758 struct fs_path *full_path;
2759 u64 dir;
2760 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002761 int name_len;
2762};
2763
Filipe Mananafdb13882017-06-13 14:13:11 +01002764static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2765{
2766 ref->full_path = path;
2767 ref->name = (char *)kbasename(ref->full_path->start);
2768 ref->name_len = ref->full_path->end - ref->name;
2769}
2770
Alexander Block31db9f72012-07-25 23:19:24 +02002771/*
2772 * We need to process new refs before deleted refs, but compare_tree gives us
2773 * everything mixed. So we first record all refs and later process them.
2774 * This function is a helper to record one ref.
2775 */
Liu Boa4d96d62014-03-03 21:31:03 +08002776static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002777 u64 dir_gen, struct fs_path *path)
2778{
2779 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002780
David Sterbae780b0d2016-01-18 18:42:13 +01002781 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02002782 if (!ref)
2783 return -ENOMEM;
2784
2785 ref->dir = dir;
2786 ref->dir_gen = dir_gen;
Filipe Mananafdb13882017-06-13 14:13:11 +01002787 set_ref_path(ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02002788 list_add_tail(&ref->list, head);
2789 return 0;
2790}
2791
Josef Bacikba5e8f22013-08-16 16:52:55 -04002792static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2793{
2794 struct recorded_ref *new;
2795
David Sterbae780b0d2016-01-18 18:42:13 +01002796 new = kmalloc(sizeof(*ref), GFP_KERNEL);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002797 if (!new)
2798 return -ENOMEM;
2799
2800 new->dir = ref->dir;
2801 new->dir_gen = ref->dir_gen;
2802 new->full_path = NULL;
2803 INIT_LIST_HEAD(&new->list);
2804 list_add_tail(&new->list, list);
2805 return 0;
2806}
2807
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002808static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002809{
2810 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002811
Alexander Blocke938c8a2012-07-28 16:33:49 +02002812 while (!list_empty(head)) {
2813 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002814 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002815 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002816 kfree(cur);
2817 }
Alexander Block31db9f72012-07-25 23:19:24 +02002818}
2819
2820static void free_recorded_refs(struct send_ctx *sctx)
2821{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002822 __free_recorded_refs(&sctx->new_refs);
2823 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002824}
2825
2826/*
Alexander Block766702e2012-07-28 14:11:31 +02002827 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002828 * ref of an unprocessed inode gets overwritten and for all non empty
2829 * directories.
2830 */
2831static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2832 struct fs_path *path)
2833{
2834 int ret;
2835 struct fs_path *orphan;
2836
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002837 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002838 if (!orphan)
2839 return -ENOMEM;
2840
2841 ret = gen_unique_name(sctx, ino, gen, orphan);
2842 if (ret < 0)
2843 goto out;
2844
2845 ret = send_rename(sctx, path, orphan);
2846
2847out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002848 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002849 return ret;
2850}
2851
Filipe Manana9dc44212014-02-19 14:31:44 +00002852static struct orphan_dir_info *
2853add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2854{
2855 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2856 struct rb_node *parent = NULL;
2857 struct orphan_dir_info *entry, *odi;
2858
David Sterbae780b0d2016-01-18 18:42:13 +01002859 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
Filipe Manana9dc44212014-02-19 14:31:44 +00002860 if (!odi)
2861 return ERR_PTR(-ENOMEM);
2862 odi->ino = dir_ino;
2863 odi->gen = 0;
2864
2865 while (*p) {
2866 parent = *p;
2867 entry = rb_entry(parent, struct orphan_dir_info, node);
2868 if (dir_ino < entry->ino) {
2869 p = &(*p)->rb_left;
2870 } else if (dir_ino > entry->ino) {
2871 p = &(*p)->rb_right;
2872 } else {
2873 kfree(odi);
2874 return entry;
2875 }
2876 }
2877
2878 rb_link_node(&odi->node, parent, p);
2879 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2880 return odi;
2881}
2882
2883static struct orphan_dir_info *
2884get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2885{
2886 struct rb_node *n = sctx->orphan_dirs.rb_node;
2887 struct orphan_dir_info *entry;
2888
2889 while (n) {
2890 entry = rb_entry(n, struct orphan_dir_info, node);
2891 if (dir_ino < entry->ino)
2892 n = n->rb_left;
2893 else if (dir_ino > entry->ino)
2894 n = n->rb_right;
2895 else
2896 return entry;
2897 }
2898 return NULL;
2899}
2900
2901static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2902{
2903 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2904
2905 return odi != NULL;
2906}
2907
2908static void free_orphan_dir_info(struct send_ctx *sctx,
2909 struct orphan_dir_info *odi)
2910{
2911 if (!odi)
2912 return;
2913 rb_erase(&odi->node, &sctx->orphan_dirs);
2914 kfree(odi);
2915}
2916
Alexander Block31db9f72012-07-25 23:19:24 +02002917/*
2918 * Returns 1 if a directory can be removed at this point in time.
2919 * We check this by iterating all dir items and checking if the inode behind
2920 * the dir item was already processed.
2921 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002922static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2923 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002924{
2925 int ret = 0;
2926 struct btrfs_root *root = sctx->parent_root;
2927 struct btrfs_path *path;
2928 struct btrfs_key key;
2929 struct btrfs_key found_key;
2930 struct btrfs_key loc;
2931 struct btrfs_dir_item *di;
2932
Alexander Block6d85ed02012-08-01 14:48:59 +02002933 /*
2934 * Don't try to rmdir the top/root subvolume dir.
2935 */
2936 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2937 return 0;
2938
Alexander Block31db9f72012-07-25 23:19:24 +02002939 path = alloc_path_for_send();
2940 if (!path)
2941 return -ENOMEM;
2942
2943 key.objectid = dir;
2944 key.type = BTRFS_DIR_INDEX_KEY;
2945 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002946 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2947 if (ret < 0)
2948 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002949
2950 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002951 struct waiting_dir_move *dm;
2952
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002953 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2954 ret = btrfs_next_leaf(root, path);
2955 if (ret < 0)
2956 goto out;
2957 else if (ret > 0)
2958 break;
2959 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002960 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002961 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2962 path->slots[0]);
2963 if (found_key.objectid != key.objectid ||
2964 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002965 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002966
2967 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2968 struct btrfs_dir_item);
2969 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2970
Filipe Manana9dc44212014-02-19 14:31:44 +00002971 dm = get_waiting_dir_move(sctx, loc.objectid);
2972 if (dm) {
2973 struct orphan_dir_info *odi;
2974
2975 odi = add_orphan_dir_info(sctx, dir);
2976 if (IS_ERR(odi)) {
2977 ret = PTR_ERR(odi);
2978 goto out;
2979 }
2980 odi->gen = dir_gen;
2981 dm->rmdir_ino = dir;
2982 ret = 0;
2983 goto out;
2984 }
2985
Alexander Block31db9f72012-07-25 23:19:24 +02002986 if (loc.objectid > send_progress) {
Robbie Ko443f9d22015-06-22 17:08:45 +08002987 struct orphan_dir_info *odi;
2988
2989 odi = get_orphan_dir_info(sctx, dir);
2990 free_orphan_dir_info(sctx, odi);
Alexander Block31db9f72012-07-25 23:19:24 +02002991 ret = 0;
2992 goto out;
2993 }
2994
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002995 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002996 }
2997
2998 ret = 1;
2999
3000out:
3001 btrfs_free_path(path);
3002 return ret;
3003}
3004
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003005static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3006{
Filipe Manana9dc44212014-02-19 14:31:44 +00003007 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003008
Filipe Manana9dc44212014-02-19 14:31:44 +00003009 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003010}
3011
Filipe Manana8b191a62015-04-09 14:09:14 +01003012static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003013{
3014 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3015 struct rb_node *parent = NULL;
3016 struct waiting_dir_move *entry, *dm;
3017
David Sterbae780b0d2016-01-18 18:42:13 +01003018 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003019 if (!dm)
3020 return -ENOMEM;
3021 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00003022 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01003023 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003024
3025 while (*p) {
3026 parent = *p;
3027 entry = rb_entry(parent, struct waiting_dir_move, node);
3028 if (ino < entry->ino) {
3029 p = &(*p)->rb_left;
3030 } else if (ino > entry->ino) {
3031 p = &(*p)->rb_right;
3032 } else {
3033 kfree(dm);
3034 return -EEXIST;
3035 }
3036 }
3037
3038 rb_link_node(&dm->node, parent, p);
3039 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3040 return 0;
3041}
3042
Filipe Manana9dc44212014-02-19 14:31:44 +00003043static struct waiting_dir_move *
3044get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003045{
3046 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3047 struct waiting_dir_move *entry;
3048
3049 while (n) {
3050 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00003051 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003052 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00003053 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003054 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00003055 else
3056 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003057 }
Filipe Manana9dc44212014-02-19 14:31:44 +00003058 return NULL;
3059}
3060
3061static void free_waiting_dir_move(struct send_ctx *sctx,
3062 struct waiting_dir_move *dm)
3063{
3064 if (!dm)
3065 return;
3066 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3067 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003068}
3069
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003070static int add_pending_dir_move(struct send_ctx *sctx,
3071 u64 ino,
3072 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003073 u64 parent_ino,
3074 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003075 struct list_head *deleted_refs,
3076 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003077{
3078 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3079 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003080 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003081 struct recorded_ref *cur;
3082 int exists = 0;
3083 int ret;
3084
David Sterbae780b0d2016-01-18 18:42:13 +01003085 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003086 if (!pm)
3087 return -ENOMEM;
3088 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003089 pm->ino = ino;
3090 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003091 INIT_LIST_HEAD(&pm->list);
3092 INIT_LIST_HEAD(&pm->update_refs);
3093 RB_CLEAR_NODE(&pm->node);
3094
3095 while (*p) {
3096 parent = *p;
3097 entry = rb_entry(parent, struct pending_dir_move, node);
3098 if (parent_ino < entry->parent_ino) {
3099 p = &(*p)->rb_left;
3100 } else if (parent_ino > entry->parent_ino) {
3101 p = &(*p)->rb_right;
3102 } else {
3103 exists = 1;
3104 break;
3105 }
3106 }
3107
Filipe Mananaf9594922014-03-27 20:14:01 +00003108 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003109 ret = dup_ref(cur, &pm->update_refs);
3110 if (ret < 0)
3111 goto out;
3112 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003113 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003114 ret = dup_ref(cur, &pm->update_refs);
3115 if (ret < 0)
3116 goto out;
3117 }
3118
Filipe Manana8b191a62015-04-09 14:09:14 +01003119 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003120 if (ret)
3121 goto out;
3122
3123 if (exists) {
3124 list_add_tail(&pm->list, &entry->list);
3125 } else {
3126 rb_link_node(&pm->node, parent, p);
3127 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3128 }
3129 ret = 0;
3130out:
3131 if (ret) {
3132 __free_recorded_refs(&pm->update_refs);
3133 kfree(pm);
3134 }
3135 return ret;
3136}
3137
3138static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3139 u64 parent_ino)
3140{
3141 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3142 struct pending_dir_move *entry;
3143
3144 while (n) {
3145 entry = rb_entry(n, struct pending_dir_move, node);
3146 if (parent_ino < entry->parent_ino)
3147 n = n->rb_left;
3148 else if (parent_ino > entry->parent_ino)
3149 n = n->rb_right;
3150 else
3151 return entry;
3152 }
3153 return NULL;
3154}
3155
Robbie Ko801bec32015-06-23 18:39:46 +08003156static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3157 u64 ino, u64 gen, u64 *ancestor_ino)
3158{
3159 int ret = 0;
3160 u64 parent_inode = 0;
3161 u64 parent_gen = 0;
3162 u64 start_ino = ino;
3163
3164 *ancestor_ino = 0;
3165 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3166 fs_path_reset(name);
3167
3168 if (is_waiting_for_rm(sctx, ino))
3169 break;
3170 if (is_waiting_for_move(sctx, ino)) {
3171 if (*ancestor_ino == 0)
3172 *ancestor_ino = ino;
3173 ret = get_first_ref(sctx->parent_root, ino,
3174 &parent_inode, &parent_gen, name);
3175 } else {
3176 ret = __get_cur_name_and_parent(sctx, ino, gen,
3177 &parent_inode,
3178 &parent_gen, name);
3179 if (ret > 0) {
3180 ret = 0;
3181 break;
3182 }
3183 }
3184 if (ret < 0)
3185 break;
3186 if (parent_inode == start_ino) {
3187 ret = 1;
3188 if (*ancestor_ino == 0)
3189 *ancestor_ino = ino;
3190 break;
3191 }
3192 ino = parent_inode;
3193 gen = parent_gen;
3194 }
3195 return ret;
3196}
3197
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003198static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3199{
3200 struct fs_path *from_path = NULL;
3201 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003202 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003203 u64 orig_progress = sctx->send_progress;
3204 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003205 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003206 struct waiting_dir_move *dm = NULL;
3207 u64 rmdir_ino = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003208 u64 ancestor;
3209 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003210 int ret;
3211
Filipe Manana2b863a12014-02-16 13:43:11 +00003212 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003213 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003214 if (!name || !from_path) {
3215 ret = -ENOMEM;
3216 goto out;
3217 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003218
Filipe Manana9dc44212014-02-19 14:31:44 +00003219 dm = get_waiting_dir_move(sctx, pm->ino);
3220 ASSERT(dm);
3221 rmdir_ino = dm->rmdir_ino;
Robbie Ko801bec32015-06-23 18:39:46 +08003222 is_orphan = dm->orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +00003223 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003224
Robbie Ko801bec32015-06-23 18:39:46 +08003225 if (is_orphan) {
Filipe Manana84471e22015-02-28 22:29:22 +00003226 ret = gen_unique_name(sctx, pm->ino,
3227 pm->gen, from_path);
3228 } else {
3229 ret = get_first_ref(sctx->parent_root, pm->ino,
3230 &parent_ino, &parent_gen, name);
3231 if (ret < 0)
3232 goto out;
3233 ret = get_cur_path(sctx, parent_ino, parent_gen,
3234 from_path);
3235 if (ret < 0)
3236 goto out;
3237 ret = fs_path_add_path(from_path, name);
3238 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003239 if (ret < 0)
3240 goto out;
3241
Filipe Mananaf9594922014-03-27 20:14:01 +00003242 sctx->send_progress = sctx->cur_ino + 1;
Robbie Ko801bec32015-06-23 18:39:46 +08003243 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
Filipe Manana7969e772016-06-17 17:13:36 +01003244 if (ret < 0)
3245 goto out;
Robbie Ko801bec32015-06-23 18:39:46 +08003246 if (ret) {
3247 LIST_HEAD(deleted_refs);
3248 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3249 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3250 &pm->update_refs, &deleted_refs,
3251 is_orphan);
3252 if (ret < 0)
3253 goto out;
3254 if (rmdir_ino) {
3255 dm = get_waiting_dir_move(sctx, pm->ino);
3256 ASSERT(dm);
3257 dm->rmdir_ino = rmdir_ino;
3258 }
3259 goto out;
3260 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003261 fs_path_reset(name);
3262 to_path = name;
3263 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003264 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3265 if (ret < 0)
3266 goto out;
3267
3268 ret = send_rename(sctx, from_path, to_path);
3269 if (ret < 0)
3270 goto out;
3271
Filipe Manana9dc44212014-02-19 14:31:44 +00003272 if (rmdir_ino) {
3273 struct orphan_dir_info *odi;
3274
3275 odi = get_orphan_dir_info(sctx, rmdir_ino);
3276 if (!odi) {
3277 /* already deleted */
3278 goto finish;
3279 }
Robbie Ko99ea42d2015-06-23 18:39:49 +08003280 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino);
Filipe Manana9dc44212014-02-19 14:31:44 +00003281 if (ret < 0)
3282 goto out;
3283 if (!ret)
3284 goto finish;
3285
3286 name = fs_path_alloc();
3287 if (!name) {
3288 ret = -ENOMEM;
3289 goto out;
3290 }
3291 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3292 if (ret < 0)
3293 goto out;
3294 ret = send_rmdir(sctx, name);
3295 if (ret < 0)
3296 goto out;
3297 free_orphan_dir_info(sctx, odi);
3298 }
3299
3300finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003301 ret = send_utimes(sctx, pm->ino, pm->gen);
3302 if (ret < 0)
3303 goto out;
3304
3305 /*
3306 * After rename/move, need to update the utimes of both new parent(s)
3307 * and old parent(s).
3308 */
3309 list_for_each_entry(cur, &pm->update_refs, list) {
Robbie Ko764433a2015-06-23 18:39:50 +08003310 /*
3311 * The parent inode might have been deleted in the send snapshot
3312 */
3313 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3314 NULL, NULL, NULL, NULL, NULL);
3315 if (ret == -ENOENT) {
3316 ret = 0;
Filipe Manana9dc44212014-02-19 14:31:44 +00003317 continue;
Robbie Ko764433a2015-06-23 18:39:50 +08003318 }
3319 if (ret < 0)
3320 goto out;
3321
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003322 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3323 if (ret < 0)
3324 goto out;
3325 }
3326
3327out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003328 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003329 fs_path_free(from_path);
3330 fs_path_free(to_path);
3331 sctx->send_progress = orig_progress;
3332
3333 return ret;
3334}
3335
3336static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3337{
3338 if (!list_empty(&m->list))
3339 list_del(&m->list);
3340 if (!RB_EMPTY_NODE(&m->node))
3341 rb_erase(&m->node, &sctx->pending_dir_moves);
3342 __free_recorded_refs(&m->update_refs);
3343 kfree(m);
3344}
3345
3346static void tail_append_pending_moves(struct pending_dir_move *moves,
3347 struct list_head *stack)
3348{
3349 if (list_empty(&moves->list)) {
3350 list_add_tail(&moves->list, stack);
3351 } else {
3352 LIST_HEAD(list);
3353 list_splice_init(&moves->list, &list);
3354 list_add_tail(&moves->list, stack);
3355 list_splice_tail(&list, stack);
3356 }
3357}
3358
3359static int apply_children_dir_moves(struct send_ctx *sctx)
3360{
3361 struct pending_dir_move *pm;
3362 struct list_head stack;
3363 u64 parent_ino = sctx->cur_ino;
3364 int ret = 0;
3365
3366 pm = get_pending_dir_moves(sctx, parent_ino);
3367 if (!pm)
3368 return 0;
3369
3370 INIT_LIST_HEAD(&stack);
3371 tail_append_pending_moves(pm, &stack);
3372
3373 while (!list_empty(&stack)) {
3374 pm = list_first_entry(&stack, struct pending_dir_move, list);
3375 parent_ino = pm->ino;
3376 ret = apply_dir_move(sctx, pm);
3377 free_pending_move(sctx, pm);
3378 if (ret)
3379 goto out;
3380 pm = get_pending_dir_moves(sctx, parent_ino);
3381 if (pm)
3382 tail_append_pending_moves(pm, &stack);
3383 }
3384 return 0;
3385
3386out:
3387 while (!list_empty(&stack)) {
3388 pm = list_first_entry(&stack, struct pending_dir_move, list);
3389 free_pending_move(sctx, pm);
3390 }
3391 return ret;
3392}
3393
Filipe Manana84471e22015-02-28 22:29:22 +00003394/*
3395 * We might need to delay a directory rename even when no ancestor directory
3396 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3397 * renamed. This happens when we rename a directory to the old name (the name
3398 * in the parent root) of some other unrelated directory that got its rename
3399 * delayed due to some ancestor with higher number that got renamed.
3400 *
3401 * Example:
3402 *
3403 * Parent snapshot:
3404 * . (ino 256)
3405 * |---- a/ (ino 257)
3406 * | |---- file (ino 260)
3407 * |
3408 * |---- b/ (ino 258)
3409 * |---- c/ (ino 259)
3410 *
3411 * Send snapshot:
3412 * . (ino 256)
3413 * |---- a/ (ino 258)
3414 * |---- x/ (ino 259)
3415 * |---- y/ (ino 257)
3416 * |----- file (ino 260)
3417 *
3418 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3419 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3420 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3421 * must issue is:
3422 *
3423 * 1 - rename 259 from 'c' to 'x'
3424 * 2 - rename 257 from 'a' to 'x/y'
3425 * 3 - rename 258 from 'b' to 'a'
3426 *
3427 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3428 * be done right away and < 0 on error.
3429 */
3430static int wait_for_dest_dir_move(struct send_ctx *sctx,
3431 struct recorded_ref *parent_ref,
3432 const bool is_orphan)
3433{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003434 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
Filipe Manana84471e22015-02-28 22:29:22 +00003435 struct btrfs_path *path;
3436 struct btrfs_key key;
3437 struct btrfs_key di_key;
3438 struct btrfs_dir_item *di;
3439 u64 left_gen;
3440 u64 right_gen;
3441 int ret = 0;
Robbie Ko801bec32015-06-23 18:39:46 +08003442 struct waiting_dir_move *wdm;
Filipe Manana84471e22015-02-28 22:29:22 +00003443
3444 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3445 return 0;
3446
3447 path = alloc_path_for_send();
3448 if (!path)
3449 return -ENOMEM;
3450
3451 key.objectid = parent_ref->dir;
3452 key.type = BTRFS_DIR_ITEM_KEY;
3453 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3454
3455 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3456 if (ret < 0) {
3457 goto out;
3458 } else if (ret > 0) {
3459 ret = 0;
3460 goto out;
3461 }
3462
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003463 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3464 parent_ref->name_len);
Filipe Manana84471e22015-02-28 22:29:22 +00003465 if (!di) {
3466 ret = 0;
3467 goto out;
3468 }
3469 /*
3470 * di_key.objectid has the number of the inode that has a dentry in the
3471 * parent directory with the same name that sctx->cur_ino is being
3472 * renamed to. We need to check if that inode is in the send root as
3473 * well and if it is currently marked as an inode with a pending rename,
3474 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3475 * that it happens after that other inode is renamed.
3476 */
3477 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3478 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3479 ret = 0;
3480 goto out;
3481 }
3482
3483 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3484 &left_gen, NULL, NULL, NULL, NULL);
3485 if (ret < 0)
3486 goto out;
3487 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3488 &right_gen, NULL, NULL, NULL, NULL);
3489 if (ret < 0) {
3490 if (ret == -ENOENT)
3491 ret = 0;
3492 goto out;
3493 }
3494
3495 /* Different inode, no need to delay the rename of sctx->cur_ino */
3496 if (right_gen != left_gen) {
3497 ret = 0;
3498 goto out;
3499 }
3500
Robbie Ko801bec32015-06-23 18:39:46 +08003501 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3502 if (wdm && !wdm->orphanized) {
Filipe Manana84471e22015-02-28 22:29:22 +00003503 ret = add_pending_dir_move(sctx,
3504 sctx->cur_ino,
3505 sctx->cur_inode_gen,
3506 di_key.objectid,
3507 &sctx->new_refs,
3508 &sctx->deleted_refs,
3509 is_orphan);
3510 if (!ret)
3511 ret = 1;
3512 }
3513out:
3514 btrfs_free_path(path);
3515 return ret;
3516}
3517
Filipe Manana80aa6022015-03-27 17:50:45 +00003518/*
Filipe Mananaea37d592017-11-17 01:54:00 +00003519 * Check if inode ino2, or any of its ancestors, is inode ino1.
3520 * Return 1 if true, 0 if false and < 0 on error.
3521 */
3522static int check_ino_in_path(struct btrfs_root *root,
3523 const u64 ino1,
3524 const u64 ino1_gen,
3525 const u64 ino2,
3526 const u64 ino2_gen,
3527 struct fs_path *fs_path)
3528{
3529 u64 ino = ino2;
3530
3531 if (ino1 == ino2)
3532 return ino1_gen == ino2_gen;
3533
3534 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3535 u64 parent;
3536 u64 parent_gen;
3537 int ret;
3538
3539 fs_path_reset(fs_path);
3540 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3541 if (ret < 0)
3542 return ret;
3543 if (parent == ino1)
3544 return parent_gen == ino1_gen;
3545 ino = parent;
3546 }
3547 return 0;
3548}
3549
3550/*
3551 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3552 * possible path (in case ino2 is not a directory and has multiple hard links).
Filipe Manana80aa6022015-03-27 17:50:45 +00003553 * Return 1 if true, 0 if false and < 0 on error.
3554 */
3555static int is_ancestor(struct btrfs_root *root,
3556 const u64 ino1,
3557 const u64 ino1_gen,
3558 const u64 ino2,
3559 struct fs_path *fs_path)
3560{
Filipe Mananaea37d592017-11-17 01:54:00 +00003561 bool free_fs_path = false;
Filipe Manana72c36682017-06-07 11:41:29 +01003562 int ret = 0;
Filipe Mananaea37d592017-11-17 01:54:00 +00003563 struct btrfs_path *path = NULL;
3564 struct btrfs_key key;
Filipe Manana72c36682017-06-07 11:41:29 +01003565
3566 if (!fs_path) {
3567 fs_path = fs_path_alloc();
3568 if (!fs_path)
3569 return -ENOMEM;
Filipe Mananaea37d592017-11-17 01:54:00 +00003570 free_fs_path = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003571 }
Filipe Manana80aa6022015-03-27 17:50:45 +00003572
Filipe Mananaea37d592017-11-17 01:54:00 +00003573 path = alloc_path_for_send();
3574 if (!path) {
3575 ret = -ENOMEM;
3576 goto out;
Filipe Manana80aa6022015-03-27 17:50:45 +00003577 }
Filipe Mananaea37d592017-11-17 01:54:00 +00003578
3579 key.objectid = ino2;
3580 key.type = BTRFS_INODE_REF_KEY;
3581 key.offset = 0;
3582
3583 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3584 if (ret < 0)
3585 goto out;
3586
3587 while (true) {
3588 struct extent_buffer *leaf = path->nodes[0];
3589 int slot = path->slots[0];
3590 u32 cur_offset = 0;
3591 u32 item_size;
3592
3593 if (slot >= btrfs_header_nritems(leaf)) {
3594 ret = btrfs_next_leaf(root, path);
3595 if (ret < 0)
3596 goto out;
3597 if (ret > 0)
3598 break;
3599 continue;
3600 }
3601
3602 btrfs_item_key_to_cpu(leaf, &key, slot);
3603 if (key.objectid != ino2)
3604 break;
3605 if (key.type != BTRFS_INODE_REF_KEY &&
3606 key.type != BTRFS_INODE_EXTREF_KEY)
3607 break;
3608
3609 item_size = btrfs_item_size_nr(leaf, slot);
3610 while (cur_offset < item_size) {
3611 u64 parent;
3612 u64 parent_gen;
3613
3614 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3615 unsigned long ptr;
3616 struct btrfs_inode_extref *extref;
3617
3618 ptr = btrfs_item_ptr_offset(leaf, slot);
3619 extref = (struct btrfs_inode_extref *)
3620 (ptr + cur_offset);
3621 parent = btrfs_inode_extref_parent(leaf,
3622 extref);
3623 cur_offset += sizeof(*extref);
3624 cur_offset += btrfs_inode_extref_name_len(leaf,
3625 extref);
3626 } else {
3627 parent = key.offset;
3628 cur_offset = item_size;
3629 }
3630
3631 ret = get_inode_info(root, parent, NULL, &parent_gen,
3632 NULL, NULL, NULL, NULL);
3633 if (ret < 0)
3634 goto out;
3635 ret = check_ino_in_path(root, ino1, ino1_gen,
3636 parent, parent_gen, fs_path);
3637 if (ret)
3638 goto out;
3639 }
3640 path->slots[0]++;
3641 }
3642 ret = 0;
Filipe Manana72c36682017-06-07 11:41:29 +01003643 out:
Filipe Mananaea37d592017-11-17 01:54:00 +00003644 btrfs_free_path(path);
3645 if (free_fs_path)
Filipe Manana72c36682017-06-07 11:41:29 +01003646 fs_path_free(fs_path);
3647 return ret;
Filipe Manana80aa6022015-03-27 17:50:45 +00003648}
3649
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003650static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003651 struct recorded_ref *parent_ref,
3652 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003653{
Filipe Mananaf9594922014-03-27 20:14:01 +00003654 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003655 u64 ino = parent_ref->dir;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003656 u64 ino_gen = parent_ref->dir_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003657 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003658 struct fs_path *path_before = NULL;
3659 struct fs_path *path_after = NULL;
3660 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003661
3662 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003663 path_before = fs_path_alloc();
3664 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003665 ret = -ENOMEM;
3666 goto out;
3667 }
3668
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003669 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003670 * Our current directory inode may not yet be renamed/moved because some
3671 * ancestor (immediate or not) has to be renamed/moved first. So find if
3672 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003673 * that ancestor is processed to avoid path build infinite loops (done
3674 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003675 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003676 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003677 u64 parent_ino_after_gen;
3678
Filipe Mananaf9594922014-03-27 20:14:01 +00003679 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003680 /*
3681 * If the current inode is an ancestor of ino in the
3682 * parent root, we need to delay the rename of the
3683 * current inode, otherwise don't delayed the rename
3684 * because we can end up with a circular dependency
3685 * of renames, resulting in some directories never
3686 * getting the respective rename operations issued in
3687 * the send stream or getting into infinite path build
3688 * loops.
3689 */
3690 ret = is_ancestor(sctx->parent_root,
3691 sctx->cur_ino, sctx->cur_inode_gen,
3692 ino, path_before);
Filipe Manana4122ea62016-06-27 16:54:44 +01003693 if (ret)
3694 break;
Filipe Mananaf9594922014-03-27 20:14:01 +00003695 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003696
3697 fs_path_reset(path_before);
3698 fs_path_reset(path_after);
3699
3700 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananafe9c7982017-01-11 02:15:39 +00003701 &parent_ino_after_gen, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003702 if (ret < 0)
3703 goto out;
3704 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3705 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003706 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003707 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003708 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003709 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003710 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003711 }
3712
3713 len1 = fs_path_len(path_before);
3714 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003715 if (ino > sctx->cur_ino &&
3716 (parent_ino_before != parent_ino_after || len1 != len2 ||
3717 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananafe9c7982017-01-11 02:15:39 +00003718 u64 parent_ino_gen;
3719
3720 ret = get_inode_info(sctx->parent_root, ino, NULL,
3721 &parent_ino_gen, NULL, NULL, NULL,
3722 NULL);
3723 if (ret < 0)
3724 goto out;
3725 if (ino_gen == parent_ino_gen) {
3726 ret = 1;
3727 break;
3728 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003729 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003730 ino = parent_ino_after;
Filipe Mananafe9c7982017-01-11 02:15:39 +00003731 ino_gen = parent_ino_after_gen;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003732 }
3733
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003734out:
3735 fs_path_free(path_before);
3736 fs_path_free(path_after);
3737
Filipe Mananaf9594922014-03-27 20:14:01 +00003738 if (ret == 1) {
3739 ret = add_pending_dir_move(sctx,
3740 sctx->cur_ino,
3741 sctx->cur_inode_gen,
3742 ino,
3743 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003744 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003745 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003746 if (!ret)
3747 ret = 1;
3748 }
3749
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003750 return ret;
3751}
3752
Filipe Mananaf5962782017-06-22 20:03:51 +01003753static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3754{
3755 int ret;
3756 struct fs_path *new_path;
3757
3758 /*
3759 * Our reference's name member points to its full_path member string, so
3760 * we use here a new path.
3761 */
3762 new_path = fs_path_alloc();
3763 if (!new_path)
3764 return -ENOMEM;
3765
3766 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3767 if (ret < 0) {
3768 fs_path_free(new_path);
3769 return ret;
3770 }
3771 ret = fs_path_add(new_path, ref->name, ref->name_len);
3772 if (ret < 0) {
3773 fs_path_free(new_path);
3774 return ret;
3775 }
3776
3777 fs_path_free(ref->full_path);
3778 set_ref_path(ref, new_path);
3779
3780 return 0;
3781}
3782
Alexander Block31db9f72012-07-25 23:19:24 +02003783/*
3784 * This does all the move/link/unlink/rmdir magic.
3785 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003786static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003787{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003788 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02003789 int ret = 0;
3790 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003791 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003792 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003793 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003794 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003795 u64 ow_gen;
Filipe Mananaf5962782017-06-22 20:03:51 +01003796 u64 ow_mode;
Alexander Block31db9f72012-07-25 23:19:24 +02003797 int did_overwrite = 0;
3798 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003799 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003800 bool can_rename = true;
Filipe Mananaf5962782017-06-22 20:03:51 +01003801 bool orphanized_dir = false;
Filipe Mananafdb13882017-06-13 14:13:11 +01003802 bool orphanized_ancestor = false;
Alexander Block31db9f72012-07-25 23:19:24 +02003803
Jeff Mahoney04ab9562016-09-20 10:05:03 -04003804 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003805
Alexander Block6d85ed02012-08-01 14:48:59 +02003806 /*
3807 * This should never happen as the root dir always has the same ref
3808 * which is always '..'
3809 */
3810 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003811 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003812
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003813 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003814 if (!valid_path) {
3815 ret = -ENOMEM;
3816 goto out;
3817 }
3818
Alexander Block31db9f72012-07-25 23:19:24 +02003819 /*
3820 * First, check if the first ref of the current inode was overwritten
3821 * before. If yes, we know that the current inode was already orphanized
3822 * and thus use the orphan name. If not, we can use get_cur_path to
3823 * get the path of the first ref as it would like while receiving at
3824 * this point in time.
3825 * New inodes are always orphan at the beginning, so force to use the
3826 * orphan name in this case.
3827 * The first ref is stored in valid_path and will be updated if it
3828 * gets moved around.
3829 */
3830 if (!sctx->cur_inode_new) {
3831 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3832 sctx->cur_inode_gen);
3833 if (ret < 0)
3834 goto out;
3835 if (ret)
3836 did_overwrite = 1;
3837 }
3838 if (sctx->cur_inode_new || did_overwrite) {
3839 ret = gen_unique_name(sctx, sctx->cur_ino,
3840 sctx->cur_inode_gen, valid_path);
3841 if (ret < 0)
3842 goto out;
3843 is_orphan = 1;
3844 } else {
3845 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3846 valid_path);
3847 if (ret < 0)
3848 goto out;
3849 }
3850
3851 list_for_each_entry(cur, &sctx->new_refs, list) {
3852 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003853 * We may have refs where the parent directory does not exist
3854 * yet. This happens if the parent directories inum is higher
3855 * the the current inum. To handle this case, we create the
3856 * parent directory out of order. But we need to check if this
3857 * did already happen before due to other refs in the same dir.
3858 */
3859 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3860 if (ret < 0)
3861 goto out;
3862 if (ret == inode_state_will_create) {
3863 ret = 0;
3864 /*
3865 * First check if any of the current inodes refs did
3866 * already create the dir.
3867 */
3868 list_for_each_entry(cur2, &sctx->new_refs, list) {
3869 if (cur == cur2)
3870 break;
3871 if (cur2->dir == cur->dir) {
3872 ret = 1;
3873 break;
3874 }
3875 }
3876
3877 /*
3878 * If that did not happen, check if a previous inode
3879 * did already create the dir.
3880 */
3881 if (!ret)
3882 ret = did_create_dir(sctx, cur->dir);
3883 if (ret < 0)
3884 goto out;
3885 if (!ret) {
3886 ret = send_create_inode(sctx, cur->dir);
3887 if (ret < 0)
3888 goto out;
3889 }
3890 }
3891
3892 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003893 * Check if this new ref would overwrite the first ref of
3894 * another unprocessed inode. If yes, orphanize the
3895 * overwritten inode. If we find an overwritten ref that is
3896 * not the first ref, simply unlink it.
3897 */
3898 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3899 cur->name, cur->name_len,
Filipe Mananaf5962782017-06-22 20:03:51 +01003900 &ow_inode, &ow_gen, &ow_mode);
Alexander Block31db9f72012-07-25 23:19:24 +02003901 if (ret < 0)
3902 goto out;
3903 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003904 ret = is_first_ref(sctx->parent_root,
3905 ow_inode, cur->dir, cur->name,
3906 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003907 if (ret < 0)
3908 goto out;
3909 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003910 struct name_cache_entry *nce;
Robbie Ko801bec32015-06-23 18:39:46 +08003911 struct waiting_dir_move *wdm;
Filipe Manana8996a482015-03-12 15:16:20 +00003912
Alexander Block31db9f72012-07-25 23:19:24 +02003913 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3914 cur->full_path);
3915 if (ret < 0)
3916 goto out;
Filipe Mananaf5962782017-06-22 20:03:51 +01003917 if (S_ISDIR(ow_mode))
3918 orphanized_dir = true;
Robbie Ko801bec32015-06-23 18:39:46 +08003919
3920 /*
3921 * If ow_inode has its rename operation delayed
3922 * make sure that its orphanized name is used in
3923 * the source path when performing its rename
3924 * operation.
3925 */
3926 if (is_waiting_for_move(sctx, ow_inode)) {
3927 wdm = get_waiting_dir_move(sctx,
3928 ow_inode);
3929 ASSERT(wdm);
3930 wdm->orphanized = true;
3931 }
3932
Filipe Manana8996a482015-03-12 15:16:20 +00003933 /*
3934 * Make sure we clear our orphanized inode's
3935 * name from the name cache. This is because the
3936 * inode ow_inode might be an ancestor of some
3937 * other inode that will be orphanized as well
3938 * later and has an inode number greater than
3939 * sctx->send_progress. We need to prevent
3940 * future name lookups from using the old name
3941 * and get instead the orphan name.
3942 */
3943 nce = name_cache_search(sctx, ow_inode, ow_gen);
3944 if (nce) {
3945 name_cache_delete(sctx, nce);
3946 kfree(nce);
3947 }
Robbie Ko801bec32015-06-23 18:39:46 +08003948
3949 /*
3950 * ow_inode might currently be an ancestor of
3951 * cur_ino, therefore compute valid_path (the
3952 * current path of cur_ino) again because it
3953 * might contain the pre-orphanization name of
3954 * ow_inode, which is no longer valid.
3955 */
Filipe Manana72c36682017-06-07 11:41:29 +01003956 ret = is_ancestor(sctx->parent_root,
3957 ow_inode, ow_gen,
3958 sctx->cur_ino, NULL);
3959 if (ret > 0) {
Filipe Mananafdb13882017-06-13 14:13:11 +01003960 orphanized_ancestor = true;
Filipe Manana72c36682017-06-07 11:41:29 +01003961 fs_path_reset(valid_path);
3962 ret = get_cur_path(sctx, sctx->cur_ino,
3963 sctx->cur_inode_gen,
3964 valid_path);
3965 }
Robbie Ko801bec32015-06-23 18:39:46 +08003966 if (ret < 0)
3967 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003968 } else {
3969 ret = send_unlink(sctx, cur->full_path);
3970 if (ret < 0)
3971 goto out;
3972 }
3973 }
3974
Filipe Manana84471e22015-02-28 22:29:22 +00003975 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3976 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3977 if (ret < 0)
3978 goto out;
3979 if (ret == 1) {
3980 can_rename = false;
3981 *pending_move = 1;
3982 }
3983 }
3984
Filipe Manana8b191a62015-04-09 14:09:14 +01003985 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3986 can_rename) {
3987 ret = wait_for_parent_move(sctx, cur, is_orphan);
3988 if (ret < 0)
3989 goto out;
3990 if (ret == 1) {
3991 can_rename = false;
3992 *pending_move = 1;
3993 }
3994 }
3995
Alexander Block31db9f72012-07-25 23:19:24 +02003996 /*
3997 * link/move the ref to the new place. If we have an orphan
3998 * inode, move it and update valid_path. If not, link or move
3999 * it depending on the inode mode.
4000 */
Filipe Manana84471e22015-02-28 22:29:22 +00004001 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004002 ret = send_rename(sctx, valid_path, cur->full_path);
4003 if (ret < 0)
4004 goto out;
4005 is_orphan = 0;
4006 ret = fs_path_copy(valid_path, cur->full_path);
4007 if (ret < 0)
4008 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00004009 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02004010 if (S_ISDIR(sctx->cur_inode_mode)) {
4011 /*
4012 * Dirs can't be linked, so move it. For moved
4013 * dirs, we always have one new and one deleted
4014 * ref. The deleted ref is ignored later.
4015 */
Filipe Manana8b191a62015-04-09 14:09:14 +01004016 ret = send_rename(sctx, valid_path,
4017 cur->full_path);
4018 if (!ret)
4019 ret = fs_path_copy(valid_path,
4020 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004021 if (ret < 0)
4022 goto out;
4023 } else {
Filipe Mananaf5962782017-06-22 20:03:51 +01004024 /*
4025 * We might have previously orphanized an inode
4026 * which is an ancestor of our current inode,
4027 * so our reference's full path, which was
4028 * computed before any such orphanizations, must
4029 * be updated.
4030 */
4031 if (orphanized_dir) {
4032 ret = update_ref_path(sctx, cur);
4033 if (ret < 0)
4034 goto out;
4035 }
Alexander Block31db9f72012-07-25 23:19:24 +02004036 ret = send_link(sctx, cur->full_path,
4037 valid_path);
4038 if (ret < 0)
4039 goto out;
4040 }
4041 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004042 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004043 if (ret < 0)
4044 goto out;
4045 }
4046
4047 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4048 /*
4049 * Check if we can already rmdir the directory. If not,
4050 * orphanize it. For every dir item inside that gets deleted
4051 * later, we do this check again and rmdir it then if possible.
4052 * See the use of check_dirs for more details.
4053 */
Filipe Manana9dc44212014-02-19 14:31:44 +00004054 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4055 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004056 if (ret < 0)
4057 goto out;
4058 if (ret) {
4059 ret = send_rmdir(sctx, valid_path);
4060 if (ret < 0)
4061 goto out;
4062 } else if (!is_orphan) {
4063 ret = orphanize_inode(sctx, sctx->cur_ino,
4064 sctx->cur_inode_gen, valid_path);
4065 if (ret < 0)
4066 goto out;
4067 is_orphan = 1;
4068 }
4069
4070 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004071 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004072 if (ret < 0)
4073 goto out;
4074 }
Alexander Blockccf16262012-07-28 11:46:29 +02004075 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4076 !list_empty(&sctx->deleted_refs)) {
4077 /*
4078 * We have a moved dir. Add the old parent to check_dirs
4079 */
4080 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4081 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04004082 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02004083 if (ret < 0)
4084 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004085 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4086 /*
4087 * We have a non dir inode. Go through all deleted refs and
4088 * unlink them if they were not already overwritten by other
4089 * inodes.
4090 */
4091 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4092 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4093 sctx->cur_ino, sctx->cur_inode_gen,
4094 cur->name, cur->name_len);
4095 if (ret < 0)
4096 goto out;
4097 if (!ret) {
Filipe Mananafdb13882017-06-13 14:13:11 +01004098 /*
4099 * If we orphanized any ancestor before, we need
4100 * to recompute the full path for deleted names,
4101 * since any such path was computed before we
4102 * processed any references and orphanized any
4103 * ancestor inode.
4104 */
4105 if (orphanized_ancestor) {
Filipe Mananaf5962782017-06-22 20:03:51 +01004106 ret = update_ref_path(sctx, cur);
4107 if (ret < 0)
Filipe Mananafdb13882017-06-13 14:13:11 +01004108 goto out;
Filipe Mananafdb13882017-06-13 14:13:11 +01004109 }
Alexander Block1f4692d2012-07-28 10:42:24 +02004110 ret = send_unlink(sctx, cur->full_path);
4111 if (ret < 0)
4112 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004113 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004114 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004115 if (ret < 0)
4116 goto out;
4117 }
Alexander Block31db9f72012-07-25 23:19:24 +02004118 /*
4119 * If the inode is still orphan, unlink the orphan. This may
4120 * happen when a previous inode did overwrite the first ref
4121 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02004122 * inode. Unlinking does not mean that the inode is deleted in
4123 * all cases. There may still be links to this inode in other
4124 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02004125 */
Alexander Block1f4692d2012-07-28 10:42:24 +02004126 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02004127 ret = send_unlink(sctx, valid_path);
4128 if (ret < 0)
4129 goto out;
4130 }
4131 }
4132
4133 /*
4134 * We did collect all parent dirs where cur_inode was once located. We
4135 * now go through all these dirs and check if they are pending for
4136 * deletion and if it's finally possible to perform the rmdir now.
4137 * We also update the inode stats of the parent dirs here.
4138 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004139 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02004140 /*
4141 * In case we had refs into dirs that were not processed yet,
4142 * we don't need to do the utime and rmdir logic for these dirs.
4143 * The dir will be processed later.
4144 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004145 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02004146 continue;
4147
Josef Bacikba5e8f22013-08-16 16:52:55 -04004148 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004149 if (ret < 0)
4150 goto out;
4151
4152 if (ret == inode_state_did_create ||
4153 ret == inode_state_no_change) {
4154 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04004155 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02004156 if (ret < 0)
4157 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004158 } else if (ret == inode_state_did_delete &&
4159 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00004160 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4161 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02004162 if (ret < 0)
4163 goto out;
4164 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004165 ret = get_cur_path(sctx, cur->dir,
4166 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004167 if (ret < 0)
4168 goto out;
4169 ret = send_rmdir(sctx, valid_path);
4170 if (ret < 0)
4171 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00004172 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02004173 }
4174 }
4175 }
4176
Alexander Block31db9f72012-07-25 23:19:24 +02004177 ret = 0;
4178
4179out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04004180 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02004181 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004182 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02004183 return ret;
4184}
4185
Nikolay Borisova0357512017-08-21 12:43:44 +03004186static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4187 void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02004188{
4189 int ret = 0;
4190 struct send_ctx *sctx = ctx;
4191 struct fs_path *p;
4192 u64 gen;
4193
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004194 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004195 if (!p)
4196 return -ENOMEM;
4197
Liu Boa4d96d62014-03-03 21:31:03 +08004198 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004199 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004200 if (ret < 0)
4201 goto out;
4202
Alexander Block31db9f72012-07-25 23:19:24 +02004203 ret = get_cur_path(sctx, dir, gen, p);
4204 if (ret < 0)
4205 goto out;
4206 ret = fs_path_add_path(p, name);
4207 if (ret < 0)
4208 goto out;
4209
Liu Boa4d96d62014-03-03 21:31:03 +08004210 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004211
4212out:
4213 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004214 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004215 return ret;
4216}
4217
Liu Boa4d96d62014-03-03 21:31:03 +08004218static int __record_new_ref(int num, u64 dir, int index,
4219 struct fs_path *name,
4220 void *ctx)
4221{
4222 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004223 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
Liu Boa4d96d62014-03-03 21:31:03 +08004224}
4225
4226
Alexander Block31db9f72012-07-25 23:19:24 +02004227static int __record_deleted_ref(int num, u64 dir, int index,
4228 struct fs_path *name,
4229 void *ctx)
4230{
Alexander Block31db9f72012-07-25 23:19:24 +02004231 struct send_ctx *sctx = ctx;
Nikolay Borisova0357512017-08-21 12:43:44 +03004232 return record_ref(sctx->parent_root, dir, name, ctx,
4233 &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02004234}
4235
4236static int record_new_ref(struct send_ctx *sctx)
4237{
4238 int ret;
4239
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004240 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4241 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004242 if (ret < 0)
4243 goto out;
4244 ret = 0;
4245
4246out:
4247 return ret;
4248}
4249
4250static int record_deleted_ref(struct send_ctx *sctx)
4251{
4252 int ret;
4253
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004254 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4255 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004256 if (ret < 0)
4257 goto out;
4258 ret = 0;
4259
4260out:
4261 return ret;
4262}
4263
4264struct find_ref_ctx {
4265 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004266 u64 dir_gen;
4267 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02004268 struct fs_path *name;
4269 int found_idx;
4270};
4271
4272static int __find_iref(int num, u64 dir, int index,
4273 struct fs_path *name,
4274 void *ctx_)
4275{
4276 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004277 u64 dir_gen;
4278 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004279
4280 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4281 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004282 /*
4283 * To avoid doing extra lookups we'll only do this if everything
4284 * else matches.
4285 */
4286 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4287 NULL, NULL, NULL);
4288 if (ret)
4289 return ret;
4290 if (dir_gen != ctx->dir_gen)
4291 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004292 ctx->found_idx = num;
4293 return 1;
4294 }
4295 return 0;
4296}
4297
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004298static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004299 struct btrfs_path *path,
4300 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004301 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02004302{
4303 int ret;
4304 struct find_ref_ctx ctx;
4305
4306 ctx.dir = dir;
4307 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004308 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004309 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04004310 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02004311
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004312 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004313 if (ret < 0)
4314 return ret;
4315
4316 if (ctx.found_idx == -1)
4317 return -ENOENT;
4318
4319 return ctx.found_idx;
4320}
4321
4322static int __record_changed_new_ref(int num, u64 dir, int index,
4323 struct fs_path *name,
4324 void *ctx)
4325{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004326 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004327 int ret;
4328 struct send_ctx *sctx = ctx;
4329
Josef Bacikba5e8f22013-08-16 16:52:55 -04004330 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4331 NULL, NULL, NULL);
4332 if (ret)
4333 return ret;
4334
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004335 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004336 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004337 if (ret == -ENOENT)
4338 ret = __record_new_ref(num, dir, index, name, sctx);
4339 else if (ret > 0)
4340 ret = 0;
4341
4342 return ret;
4343}
4344
4345static int __record_changed_deleted_ref(int num, u64 dir, int index,
4346 struct fs_path *name,
4347 void *ctx)
4348{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004349 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004350 int ret;
4351 struct send_ctx *sctx = ctx;
4352
Josef Bacikba5e8f22013-08-16 16:52:55 -04004353 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4354 NULL, NULL, NULL);
4355 if (ret)
4356 return ret;
4357
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004358 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004359 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004360 if (ret == -ENOENT)
4361 ret = __record_deleted_ref(num, dir, index, name, sctx);
4362 else if (ret > 0)
4363 ret = 0;
4364
4365 return ret;
4366}
4367
4368static int record_changed_ref(struct send_ctx *sctx)
4369{
4370 int ret = 0;
4371
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004372 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004373 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4374 if (ret < 0)
4375 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004376 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004377 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4378 if (ret < 0)
4379 goto out;
4380 ret = 0;
4381
4382out:
4383 return ret;
4384}
4385
4386/*
4387 * Record and process all refs at once. Needed when an inode changes the
4388 * generation number, which means that it was deleted and recreated.
4389 */
4390static int process_all_refs(struct send_ctx *sctx,
4391 enum btrfs_compare_tree_result cmd)
4392{
4393 int ret;
4394 struct btrfs_root *root;
4395 struct btrfs_path *path;
4396 struct btrfs_key key;
4397 struct btrfs_key found_key;
4398 struct extent_buffer *eb;
4399 int slot;
4400 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004401 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004402
4403 path = alloc_path_for_send();
4404 if (!path)
4405 return -ENOMEM;
4406
4407 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4408 root = sctx->send_root;
4409 cb = __record_new_ref;
4410 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4411 root = sctx->parent_root;
4412 cb = __record_deleted_ref;
4413 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004414 btrfs_err(sctx->send_root->fs_info,
4415 "Wrong command %d in process_all_refs", cmd);
4416 ret = -EINVAL;
4417 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004418 }
4419
4420 key.objectid = sctx->cmp_key->objectid;
4421 key.type = BTRFS_INODE_REF_KEY;
4422 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004423 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4424 if (ret < 0)
4425 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004426
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004427 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004428 eb = path->nodes[0];
4429 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004430 if (slot >= btrfs_header_nritems(eb)) {
4431 ret = btrfs_next_leaf(root, path);
4432 if (ret < 0)
4433 goto out;
4434 else if (ret > 0)
4435 break;
4436 continue;
4437 }
4438
Alexander Block31db9f72012-07-25 23:19:24 +02004439 btrfs_item_key_to_cpu(eb, &found_key, slot);
4440
4441 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004442 (found_key.type != BTRFS_INODE_REF_KEY &&
4443 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004444 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004445
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004446 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004447 if (ret < 0)
4448 goto out;
4449
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004450 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004451 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004452 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004453
Josef Bacik3dc09ec2016-08-24 11:57:52 -04004454 /*
4455 * We don't actually care about pending_move as we are simply
4456 * re-creating this inode and will be rename'ing it into place once we
4457 * rename the parent directory.
4458 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004459 ret = process_recorded_refs(sctx, &pending_move);
Alexander Block31db9f72012-07-25 23:19:24 +02004460out:
4461 btrfs_free_path(path);
4462 return ret;
4463}
4464
4465static int send_set_xattr(struct send_ctx *sctx,
4466 struct fs_path *path,
4467 const char *name, int name_len,
4468 const char *data, int data_len)
4469{
4470 int ret = 0;
4471
4472 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4473 if (ret < 0)
4474 goto out;
4475
4476 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4477 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4478 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4479
4480 ret = send_cmd(sctx);
4481
4482tlv_put_failure:
4483out:
4484 return ret;
4485}
4486
4487static int send_remove_xattr(struct send_ctx *sctx,
4488 struct fs_path *path,
4489 const char *name, int name_len)
4490{
4491 int ret = 0;
4492
4493 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4494 if (ret < 0)
4495 goto out;
4496
4497 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4498 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4499
4500 ret = send_cmd(sctx);
4501
4502tlv_put_failure:
4503out:
4504 return ret;
4505}
4506
4507static int __process_new_xattr(int num, struct btrfs_key *di_key,
4508 const char *name, int name_len,
4509 const char *data, int data_len,
4510 u8 type, void *ctx)
4511{
4512 int ret;
4513 struct send_ctx *sctx = ctx;
4514 struct fs_path *p;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02004515 struct posix_acl_xattr_header dummy_acl;
Alexander Block31db9f72012-07-25 23:19:24 +02004516
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004517 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004518 if (!p)
4519 return -ENOMEM;
4520
4521 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004522 * This hack is needed because empty acls are stored as zero byte
Alexander Block31db9f72012-07-25 23:19:24 +02004523 * data in xattrs. Problem with that is, that receiving these zero byte
Nicholas D Steeves01327612016-05-19 21:18:45 -04004524 * acls will fail later. To fix this, we send a dummy acl list that
Alexander Block31db9f72012-07-25 23:19:24 +02004525 * only contains the version number and no entries.
4526 */
4527 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4528 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4529 if (data_len == 0) {
4530 dummy_acl.a_version =
4531 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4532 data = (char *)&dummy_acl;
4533 data_len = sizeof(dummy_acl);
4534 }
4535 }
4536
4537 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4538 if (ret < 0)
4539 goto out;
4540
4541 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4542
4543out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004544 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004545 return ret;
4546}
4547
4548static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4549 const char *name, int name_len,
4550 const char *data, int data_len,
4551 u8 type, void *ctx)
4552{
4553 int ret;
4554 struct send_ctx *sctx = ctx;
4555 struct fs_path *p;
4556
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004557 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004558 if (!p)
4559 return -ENOMEM;
4560
4561 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4562 if (ret < 0)
4563 goto out;
4564
4565 ret = send_remove_xattr(sctx, p, name, name_len);
4566
4567out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004568 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004569 return ret;
4570}
4571
4572static int process_new_xattr(struct send_ctx *sctx)
4573{
4574 int ret = 0;
4575
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004576 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004577 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004578
4579 return ret;
4580}
4581
4582static int process_deleted_xattr(struct send_ctx *sctx)
4583{
Masahiro Yamadae2c89902016-09-13 04:35:52 +09004584 return iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004585 __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004586}
4587
4588struct find_xattr_ctx {
4589 const char *name;
4590 int name_len;
4591 int found_idx;
4592 char *found_data;
4593 int found_data_len;
4594};
4595
4596static int __find_xattr(int num, struct btrfs_key *di_key,
4597 const char *name, int name_len,
4598 const char *data, int data_len,
4599 u8 type, void *vctx)
4600{
4601 struct find_xattr_ctx *ctx = vctx;
4602
4603 if (name_len == ctx->name_len &&
4604 strncmp(name, ctx->name, name_len) == 0) {
4605 ctx->found_idx = num;
4606 ctx->found_data_len = data_len;
David Sterbae780b0d2016-01-18 18:42:13 +01004607 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02004608 if (!ctx->found_data)
4609 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004610 return 1;
4611 }
4612 return 0;
4613}
4614
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004615static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004616 struct btrfs_path *path,
4617 struct btrfs_key *key,
4618 const char *name, int name_len,
4619 char **data, int *data_len)
4620{
4621 int ret;
4622 struct find_xattr_ctx ctx;
4623
4624 ctx.name = name;
4625 ctx.name_len = name_len;
4626 ctx.found_idx = -1;
4627 ctx.found_data = NULL;
4628 ctx.found_data_len = 0;
4629
Nikolay Borisova0357512017-08-21 12:43:44 +03004630 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004631 if (ret < 0)
4632 return ret;
4633
4634 if (ctx.found_idx == -1)
4635 return -ENOENT;
4636 if (data) {
4637 *data = ctx.found_data;
4638 *data_len = ctx.found_data_len;
4639 } else {
4640 kfree(ctx.found_data);
4641 }
4642 return ctx.found_idx;
4643}
4644
4645
4646static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4647 const char *name, int name_len,
4648 const char *data, int data_len,
4649 u8 type, void *ctx)
4650{
4651 int ret;
4652 struct send_ctx *sctx = ctx;
4653 char *found_data = NULL;
4654 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004655
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004656 ret = find_xattr(sctx->parent_root, sctx->right_path,
4657 sctx->cmp_key, name, name_len, &found_data,
4658 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004659 if (ret == -ENOENT) {
4660 ret = __process_new_xattr(num, di_key, name, name_len, data,
4661 data_len, type, ctx);
4662 } else if (ret >= 0) {
4663 if (data_len != found_data_len ||
4664 memcmp(data, found_data, data_len)) {
4665 ret = __process_new_xattr(num, di_key, name, name_len,
4666 data, data_len, type, ctx);
4667 } else {
4668 ret = 0;
4669 }
4670 }
4671
4672 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004673 return ret;
4674}
4675
4676static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4677 const char *name, int name_len,
4678 const char *data, int data_len,
4679 u8 type, void *ctx)
4680{
4681 int ret;
4682 struct send_ctx *sctx = ctx;
4683
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004684 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4685 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004686 if (ret == -ENOENT)
4687 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4688 data_len, type, ctx);
4689 else if (ret >= 0)
4690 ret = 0;
4691
4692 return ret;
4693}
4694
4695static int process_changed_xattr(struct send_ctx *sctx)
4696{
4697 int ret = 0;
4698
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004699 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004700 __process_changed_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004701 if (ret < 0)
4702 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004703 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Nikolay Borisova0357512017-08-21 12:43:44 +03004704 __process_changed_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004705
4706out:
4707 return ret;
4708}
4709
4710static int process_all_new_xattrs(struct send_ctx *sctx)
4711{
4712 int ret;
4713 struct btrfs_root *root;
4714 struct btrfs_path *path;
4715 struct btrfs_key key;
4716 struct btrfs_key found_key;
4717 struct extent_buffer *eb;
4718 int slot;
4719
4720 path = alloc_path_for_send();
4721 if (!path)
4722 return -ENOMEM;
4723
4724 root = sctx->send_root;
4725
4726 key.objectid = sctx->cmp_key->objectid;
4727 key.type = BTRFS_XATTR_ITEM_KEY;
4728 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004729 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4730 if (ret < 0)
4731 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004732
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004733 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004734 eb = path->nodes[0];
4735 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004736 if (slot >= btrfs_header_nritems(eb)) {
4737 ret = btrfs_next_leaf(root, path);
4738 if (ret < 0) {
4739 goto out;
4740 } else if (ret > 0) {
4741 ret = 0;
4742 break;
4743 }
4744 continue;
4745 }
Alexander Block31db9f72012-07-25 23:19:24 +02004746
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004747 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004748 if (found_key.objectid != key.objectid ||
4749 found_key.type != key.type) {
4750 ret = 0;
4751 goto out;
4752 }
4753
Nikolay Borisova0357512017-08-21 12:43:44 +03004754 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004755 if (ret < 0)
4756 goto out;
4757
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004758 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004759 }
4760
4761out:
4762 btrfs_free_path(path);
4763 return ret;
4764}
4765
Josef Baciked259092013-10-25 11:36:01 -04004766static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4767{
4768 struct btrfs_root *root = sctx->send_root;
4769 struct btrfs_fs_info *fs_info = root->fs_info;
4770 struct inode *inode;
4771 struct page *page;
4772 char *addr;
4773 struct btrfs_key key;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004774 pgoff_t index = offset >> PAGE_SHIFT;
Josef Baciked259092013-10-25 11:36:01 -04004775 pgoff_t last_index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004776 unsigned pg_offset = offset & ~PAGE_MASK;
Josef Baciked259092013-10-25 11:36:01 -04004777 ssize_t ret = 0;
4778
4779 key.objectid = sctx->cur_ino;
4780 key.type = BTRFS_INODE_ITEM_KEY;
4781 key.offset = 0;
4782
4783 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4784 if (IS_ERR(inode))
4785 return PTR_ERR(inode);
4786
4787 if (offset + len > i_size_read(inode)) {
4788 if (offset > i_size_read(inode))
4789 len = 0;
4790 else
4791 len = offset - i_size_read(inode);
4792 }
4793 if (len == 0)
4794 goto out;
4795
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004796 last_index = (offset + len - 1) >> PAGE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004797
4798 /* initial readahead */
4799 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4800 file_ra_state_init(&sctx->ra, inode->i_mapping);
Liu Bo2131bcd2014-03-05 10:07:35 +08004801
Josef Baciked259092013-10-25 11:36:01 -04004802 while (index <= last_index) {
4803 unsigned cur_len = min_t(unsigned, len,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004804 PAGE_SIZE - pg_offset);
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004805
4806 page = find_lock_page(inode->i_mapping, index);
Josef Baciked259092013-10-25 11:36:01 -04004807 if (!page) {
Kuanling Huangeef16ba2017-09-15 16:47:45 +08004808 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4809 NULL, index, last_index + 1 - index);
4810
4811 page = find_or_create_page(inode->i_mapping, index,
4812 GFP_KERNEL);
4813 if (!page) {
4814 ret = -ENOMEM;
4815 break;
4816 }
4817 }
4818
4819 if (PageReadahead(page)) {
4820 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4821 NULL, page, index, last_index + 1 - index);
Josef Baciked259092013-10-25 11:36:01 -04004822 }
4823
4824 if (!PageUptodate(page)) {
4825 btrfs_readpage(NULL, page);
4826 lock_page(page);
4827 if (!PageUptodate(page)) {
4828 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004829 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004830 ret = -EIO;
4831 break;
4832 }
4833 }
4834
4835 addr = kmap(page);
4836 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4837 kunmap(page);
4838 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004839 put_page(page);
Josef Baciked259092013-10-25 11:36:01 -04004840 index++;
4841 pg_offset = 0;
4842 len -= cur_len;
4843 ret += cur_len;
4844 }
4845out:
4846 iput(inode);
4847 return ret;
4848}
4849
Alexander Block31db9f72012-07-25 23:19:24 +02004850/*
4851 * Read some bytes from the current inode/file and send a write command to
4852 * user space.
4853 */
4854static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4855{
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004856 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02004857 int ret = 0;
4858 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004859 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004860
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004861 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004862 if (!p)
4863 return -ENOMEM;
4864
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004865 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004866
Josef Baciked259092013-10-25 11:36:01 -04004867 num_read = fill_read_buf(sctx, offset, len);
4868 if (num_read <= 0) {
4869 if (num_read < 0)
4870 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004871 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004872 }
Alexander Block31db9f72012-07-25 23:19:24 +02004873
4874 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4875 if (ret < 0)
4876 goto out;
4877
4878 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4879 if (ret < 0)
4880 goto out;
4881
4882 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4883 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004884 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004885
4886 ret = send_cmd(sctx);
4887
4888tlv_put_failure:
4889out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004890 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004891 if (ret < 0)
4892 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004893 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004894}
4895
4896/*
4897 * Send a clone command to user space.
4898 */
4899static int send_clone(struct send_ctx *sctx,
4900 u64 offset, u32 len,
4901 struct clone_root *clone_root)
4902{
4903 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004904 struct fs_path *p;
4905 u64 gen;
4906
Jeff Mahoney04ab9562016-09-20 10:05:03 -04004907 btrfs_debug(sctx->send_root->fs_info,
4908 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4909 offset, len, clone_root->root->objectid, clone_root->ino,
4910 clone_root->offset);
Alexander Block31db9f72012-07-25 23:19:24 +02004911
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004912 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004913 if (!p)
4914 return -ENOMEM;
4915
4916 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4917 if (ret < 0)
4918 goto out;
4919
4920 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4921 if (ret < 0)
4922 goto out;
4923
4924 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4925 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4926 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4927
Alexander Blocke938c8a2012-07-28 16:33:49 +02004928 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004929 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004930 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004931 if (ret < 0)
4932 goto out;
4933 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4934 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004935 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004936 }
4937 if (ret < 0)
4938 goto out;
4939
Josef Bacik37b8d272015-06-04 17:17:25 -04004940 /*
4941 * If the parent we're using has a received_uuid set then use that as
4942 * our clone source as that is what we will look for when doing a
4943 * receive.
4944 *
4945 * This covers the case that we create a snapshot off of a received
4946 * subvolume and then use that as the parent and try to receive on a
4947 * different host.
4948 */
4949 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4950 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4951 clone_root->root->root_item.received_uuid);
4952 else
4953 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4954 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004955 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004956 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004957 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4958 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4959 clone_root->offset);
4960
4961 ret = send_cmd(sctx);
4962
4963tlv_put_failure:
4964out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004965 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004966 return ret;
4967}
4968
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004969/*
4970 * Send an update extent command to user space.
4971 */
4972static int send_update_extent(struct send_ctx *sctx,
4973 u64 offset, u32 len)
4974{
4975 int ret = 0;
4976 struct fs_path *p;
4977
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004978 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004979 if (!p)
4980 return -ENOMEM;
4981
4982 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4983 if (ret < 0)
4984 goto out;
4985
4986 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4987 if (ret < 0)
4988 goto out;
4989
4990 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4991 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4992 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4993
4994 ret = send_cmd(sctx);
4995
4996tlv_put_failure:
4997out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004998 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004999 return ret;
5000}
5001
Josef Bacik16e75492013-10-22 12:18:51 -04005002static int send_hole(struct send_ctx *sctx, u64 end)
5003{
5004 struct fs_path *p = NULL;
5005 u64 offset = sctx->cur_inode_last_extent;
5006 u64 len;
5007 int ret = 0;
5008
Filipe Mananad4dfc0f2018-02-06 20:39:20 +00005009 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5010 return send_update_extent(sctx, offset, end - offset);
5011
Josef Bacik16e75492013-10-22 12:18:51 -04005012 p = fs_path_alloc();
5013 if (!p)
5014 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01005015 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5016 if (ret < 0)
5017 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04005018 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
5019 while (offset < end) {
5020 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
5021
5022 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5023 if (ret < 0)
5024 break;
Josef Bacik16e75492013-10-22 12:18:51 -04005025 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5026 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5027 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
5028 ret = send_cmd(sctx);
5029 if (ret < 0)
5030 break;
5031 offset += len;
5032 }
5033tlv_put_failure:
5034 fs_path_free(p);
5035 return ret;
5036}
5037
Filipe Mananad906d492015-10-02 10:47:34 +01005038static int send_extent_data(struct send_ctx *sctx,
5039 const u64 offset,
5040 const u64 len)
5041{
5042 u64 sent = 0;
5043
5044 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5045 return send_update_extent(sctx, offset, len);
5046
5047 while (sent < len) {
5048 u64 size = len - sent;
5049 int ret;
5050
5051 if (size > BTRFS_SEND_READ_SIZE)
5052 size = BTRFS_SEND_READ_SIZE;
5053 ret = send_write(sctx, offset + sent, size);
5054 if (ret < 0)
5055 return ret;
5056 if (!ret)
5057 break;
5058 sent += ret;
5059 }
5060 return 0;
5061}
5062
5063static int clone_range(struct send_ctx *sctx,
5064 struct clone_root *clone_root,
5065 const u64 disk_byte,
5066 u64 data_offset,
5067 u64 offset,
5068 u64 len)
5069{
5070 struct btrfs_path *path;
5071 struct btrfs_key key;
5072 int ret;
5073
Filipe Manana72610b12017-08-10 22:54:51 +01005074 /*
5075 * Prevent cloning from a zero offset with a length matching the sector
5076 * size because in some scenarios this will make the receiver fail.
5077 *
5078 * For example, if in the source filesystem the extent at offset 0
5079 * has a length of sectorsize and it was written using direct IO, then
5080 * it can never be an inline extent (even if compression is enabled).
5081 * Then this extent can be cloned in the original filesystem to a non
5082 * zero file offset, but it may not be possible to clone in the
5083 * destination filesystem because it can be inlined due to compression
5084 * on the destination filesystem (as the receiver's write operations are
5085 * always done using buffered IO). The same happens when the original
5086 * filesystem does not have compression enabled but the destination
5087 * filesystem has.
5088 */
5089 if (clone_root->offset == 0 &&
5090 len == sctx->send_root->fs_info->sectorsize)
5091 return send_extent_data(sctx, offset, len);
5092
Filipe Mananad906d492015-10-02 10:47:34 +01005093 path = alloc_path_for_send();
5094 if (!path)
5095 return -ENOMEM;
5096
5097 /*
5098 * We can't send a clone operation for the entire range if we find
5099 * extent items in the respective range in the source file that
5100 * refer to different extents or if we find holes.
5101 * So check for that and do a mix of clone and regular write/copy
5102 * operations if needed.
5103 *
5104 * Example:
5105 *
5106 * mkfs.btrfs -f /dev/sda
5107 * mount /dev/sda /mnt
5108 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5109 * cp --reflink=always /mnt/foo /mnt/bar
5110 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5111 * btrfs subvolume snapshot -r /mnt /mnt/snap
5112 *
5113 * If when we send the snapshot and we are processing file bar (which
5114 * has a higher inode number than foo) we blindly send a clone operation
5115 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5116 * a file bar that matches the content of file foo - iow, doesn't match
5117 * the content from bar in the original filesystem.
5118 */
5119 key.objectid = clone_root->ino;
5120 key.type = BTRFS_EXTENT_DATA_KEY;
5121 key.offset = clone_root->offset;
5122 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5123 if (ret < 0)
5124 goto out;
5125 if (ret > 0 && path->slots[0] > 0) {
5126 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5127 if (key.objectid == clone_root->ino &&
5128 key.type == BTRFS_EXTENT_DATA_KEY)
5129 path->slots[0]--;
5130 }
5131
5132 while (true) {
5133 struct extent_buffer *leaf = path->nodes[0];
5134 int slot = path->slots[0];
5135 struct btrfs_file_extent_item *ei;
5136 u8 type;
5137 u64 ext_len;
5138 u64 clone_len;
5139
5140 if (slot >= btrfs_header_nritems(leaf)) {
5141 ret = btrfs_next_leaf(clone_root->root, path);
5142 if (ret < 0)
5143 goto out;
5144 else if (ret > 0)
5145 break;
5146 continue;
5147 }
5148
5149 btrfs_item_key_to_cpu(leaf, &key, slot);
5150
5151 /*
5152 * We might have an implicit trailing hole (NO_HOLES feature
5153 * enabled). We deal with it after leaving this loop.
5154 */
5155 if (key.objectid != clone_root->ino ||
5156 key.type != BTRFS_EXTENT_DATA_KEY)
5157 break;
5158
5159 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5160 type = btrfs_file_extent_type(leaf, ei);
5161 if (type == BTRFS_FILE_EXTENT_INLINE) {
5162 ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005163 ext_len = PAGE_ALIGN(ext_len);
Filipe Mananad906d492015-10-02 10:47:34 +01005164 } else {
5165 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5166 }
5167
5168 if (key.offset + ext_len <= clone_root->offset)
5169 goto next;
5170
5171 if (key.offset > clone_root->offset) {
5172 /* Implicit hole, NO_HOLES feature enabled. */
5173 u64 hole_len = key.offset - clone_root->offset;
5174
5175 if (hole_len > len)
5176 hole_len = len;
5177 ret = send_extent_data(sctx, offset, hole_len);
5178 if (ret < 0)
5179 goto out;
5180
5181 len -= hole_len;
5182 if (len == 0)
5183 break;
5184 offset += hole_len;
5185 clone_root->offset += hole_len;
5186 data_offset += hole_len;
5187 }
5188
5189 if (key.offset >= clone_root->offset + len)
5190 break;
5191
5192 clone_len = min_t(u64, ext_len, len);
5193
5194 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5195 btrfs_file_extent_offset(leaf, ei) == data_offset)
5196 ret = send_clone(sctx, offset, clone_len, clone_root);
5197 else
5198 ret = send_extent_data(sctx, offset, clone_len);
5199
5200 if (ret < 0)
5201 goto out;
5202
5203 len -= clone_len;
5204 if (len == 0)
5205 break;
5206 offset += clone_len;
5207 clone_root->offset += clone_len;
5208 data_offset += clone_len;
5209next:
5210 path->slots[0]++;
5211 }
5212
5213 if (len > 0)
5214 ret = send_extent_data(sctx, offset, len);
5215 else
5216 ret = 0;
5217out:
5218 btrfs_free_path(path);
5219 return ret;
5220}
5221
Alexander Block31db9f72012-07-25 23:19:24 +02005222static int send_write_or_clone(struct send_ctx *sctx,
5223 struct btrfs_path *path,
5224 struct btrfs_key *key,
5225 struct clone_root *clone_root)
5226{
5227 int ret = 0;
5228 struct btrfs_file_extent_item *ei;
5229 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02005230 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02005231 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005232 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02005233
5234 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5235 struct btrfs_file_extent_item);
5236 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005237 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005238 len = btrfs_file_extent_inline_len(path->nodes[0],
5239 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005240 /*
5241 * it is possible the inline item won't cover the whole page,
5242 * but there may be items after this page. Make
5243 * sure to send the whole thing
5244 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005245 len = PAGE_ALIGN(len);
Chris Mason74dd17f2012-08-07 16:25:13 -04005246 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02005247 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04005248 }
Alexander Block31db9f72012-07-25 23:19:24 +02005249
5250 if (offset + len > sctx->cur_inode_size)
5251 len = sctx->cur_inode_size - offset;
5252 if (len == 0) {
5253 ret = 0;
5254 goto out;
5255 }
5256
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00005257 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01005258 u64 disk_byte;
5259 u64 data_offset;
5260
5261 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5262 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5263 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5264 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005265 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01005266 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02005267 }
Alexander Block31db9f72012-07-25 23:19:24 +02005268out:
5269 return ret;
5270}
5271
5272static int is_extent_unchanged(struct send_ctx *sctx,
5273 struct btrfs_path *left_path,
5274 struct btrfs_key *ekey)
5275{
5276 int ret = 0;
5277 struct btrfs_key key;
5278 struct btrfs_path *path = NULL;
5279 struct extent_buffer *eb;
5280 int slot;
5281 struct btrfs_key found_key;
5282 struct btrfs_file_extent_item *ei;
5283 u64 left_disknr;
5284 u64 right_disknr;
5285 u64 left_offset;
5286 u64 right_offset;
5287 u64 left_offset_fixed;
5288 u64 left_len;
5289 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04005290 u64 left_gen;
5291 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02005292 u8 left_type;
5293 u8 right_type;
5294
5295 path = alloc_path_for_send();
5296 if (!path)
5297 return -ENOMEM;
5298
5299 eb = left_path->nodes[0];
5300 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02005301 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5302 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005303
5304 if (left_type != BTRFS_FILE_EXTENT_REG) {
5305 ret = 0;
5306 goto out;
5307 }
Chris Mason74dd17f2012-08-07 16:25:13 -04005308 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5309 left_len = btrfs_file_extent_num_bytes(eb, ei);
5310 left_offset = btrfs_file_extent_offset(eb, ei);
5311 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02005312
5313 /*
5314 * Following comments will refer to these graphics. L is the left
5315 * extents which we are checking at the moment. 1-8 are the right
5316 * extents that we iterate.
5317 *
5318 * |-----L-----|
5319 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5320 *
5321 * |-----L-----|
5322 * |--1--|-2b-|...(same as above)
5323 *
5324 * Alternative situation. Happens on files where extents got split.
5325 * |-----L-----|
5326 * |-----------7-----------|-6-|
5327 *
5328 * Alternative situation. Happens on files which got larger.
5329 * |-----L-----|
5330 * |-8-|
5331 * Nothing follows after 8.
5332 */
5333
5334 key.objectid = ekey->objectid;
5335 key.type = BTRFS_EXTENT_DATA_KEY;
5336 key.offset = ekey->offset;
5337 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5338 if (ret < 0)
5339 goto out;
5340 if (ret) {
5341 ret = 0;
5342 goto out;
5343 }
5344
5345 /*
5346 * Handle special case where the right side has no extents at all.
5347 */
5348 eb = path->nodes[0];
5349 slot = path->slots[0];
5350 btrfs_item_key_to_cpu(eb, &found_key, slot);
5351 if (found_key.objectid != key.objectid ||
5352 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005353 /* If we're a hole then just pretend nothing changed */
5354 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005355 goto out;
5356 }
5357
5358 /*
5359 * We're now on 2a, 2b or 7.
5360 */
5361 key = found_key;
5362 while (key.offset < ekey->offset + left_len) {
5363 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5364 right_type = btrfs_file_extent_type(eb, ei);
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005365 if (right_type != BTRFS_FILE_EXTENT_REG &&
5366 right_type != BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02005367 ret = 0;
5368 goto out;
5369 }
5370
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005371 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5372 right_len = btrfs_file_extent_inline_len(eb, slot, ei);
5373 right_len = PAGE_ALIGN(right_len);
5374 } else {
5375 right_len = btrfs_file_extent_num_bytes(eb, ei);
5376 }
Josef Bacik007d31f2013-10-31 16:49:02 -04005377
Alexander Block31db9f72012-07-25 23:19:24 +02005378 /*
5379 * Are we at extent 8? If yes, we know the extent is changed.
5380 * This may only happen on the first iteration.
5381 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005382 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005383 /* If we're a hole just pretend nothing changed */
5384 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005385 goto out;
5386 }
5387
Filipe Mananae1cbfd72017-04-04 20:31:00 +01005388 /*
5389 * We just wanted to see if when we have an inline extent, what
5390 * follows it is a regular extent (wanted to check the above
5391 * condition for inline extents too). This should normally not
5392 * happen but it's possible for example when we have an inline
5393 * compressed extent representing data with a size matching
5394 * the page size (currently the same as sector size).
5395 */
5396 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5397 ret = 0;
5398 goto out;
5399 }
5400
Filipe Manana24e52b12017-07-06 15:31:46 +01005401 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5402 right_offset = btrfs_file_extent_offset(eb, ei);
5403 right_gen = btrfs_file_extent_generation(eb, ei);
5404
Alexander Block31db9f72012-07-25 23:19:24 +02005405 left_offset_fixed = left_offset;
5406 if (key.offset < ekey->offset) {
5407 /* Fix the right offset for 2a and 7. */
5408 right_offset += ekey->offset - key.offset;
5409 } else {
5410 /* Fix the left offset for all behind 2a and 2b */
5411 left_offset_fixed += key.offset - ekey->offset;
5412 }
5413
5414 /*
5415 * Check if we have the same extent.
5416 */
Alexander Block39540962012-08-01 12:46:05 +02005417 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005418 left_offset_fixed != right_offset ||
5419 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005420 ret = 0;
5421 goto out;
5422 }
5423
5424 /*
5425 * Go to the next extent.
5426 */
5427 ret = btrfs_next_item(sctx->parent_root, path);
5428 if (ret < 0)
5429 goto out;
5430 if (!ret) {
5431 eb = path->nodes[0];
5432 slot = path->slots[0];
5433 btrfs_item_key_to_cpu(eb, &found_key, slot);
5434 }
5435 if (ret || found_key.objectid != key.objectid ||
5436 found_key.type != key.type) {
5437 key.offset += right_len;
5438 break;
Jan Schmidtadaa4b8e2013-03-21 14:30:23 +00005439 }
5440 if (found_key.offset != key.offset + right_len) {
5441 ret = 0;
5442 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005443 }
5444 key = found_key;
5445 }
5446
5447 /*
5448 * We're now behind the left extent (treat as unchanged) or at the end
5449 * of the right side (treat as changed).
5450 */
5451 if (key.offset >= ekey->offset + left_len)
5452 ret = 1;
5453 else
5454 ret = 0;
5455
5456
5457out:
5458 btrfs_free_path(path);
5459 return ret;
5460}
5461
Josef Bacik16e75492013-10-22 12:18:51 -04005462static int get_last_extent(struct send_ctx *sctx, u64 offset)
5463{
5464 struct btrfs_path *path;
5465 struct btrfs_root *root = sctx->send_root;
5466 struct btrfs_file_extent_item *fi;
5467 struct btrfs_key key;
5468 u64 extent_end;
5469 u8 type;
5470 int ret;
5471
5472 path = alloc_path_for_send();
5473 if (!path)
5474 return -ENOMEM;
5475
5476 sctx->cur_inode_last_extent = 0;
5477
5478 key.objectid = sctx->cur_ino;
5479 key.type = BTRFS_EXTENT_DATA_KEY;
5480 key.offset = offset;
5481 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5482 if (ret < 0)
5483 goto out;
5484 ret = 0;
5485 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5486 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5487 goto out;
5488
5489 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5490 struct btrfs_file_extent_item);
5491 type = btrfs_file_extent_type(path->nodes[0], fi);
5492 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005493 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5494 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005495 extent_end = ALIGN(key.offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005496 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005497 } else {
5498 extent_end = key.offset +
5499 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5500 }
5501 sctx->cur_inode_last_extent = extent_end;
5502out:
5503 btrfs_free_path(path);
5504 return ret;
5505}
5506
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005507static int range_is_hole_in_parent(struct send_ctx *sctx,
5508 const u64 start,
5509 const u64 end)
5510{
5511 struct btrfs_path *path;
5512 struct btrfs_key key;
5513 struct btrfs_root *root = sctx->parent_root;
5514 u64 search_start = start;
5515 int ret;
5516
5517 path = alloc_path_for_send();
5518 if (!path)
5519 return -ENOMEM;
5520
5521 key.objectid = sctx->cur_ino;
5522 key.type = BTRFS_EXTENT_DATA_KEY;
5523 key.offset = search_start;
5524 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5525 if (ret < 0)
5526 goto out;
5527 if (ret > 0 && path->slots[0] > 0)
5528 path->slots[0]--;
5529
5530 while (search_start < end) {
5531 struct extent_buffer *leaf = path->nodes[0];
5532 int slot = path->slots[0];
5533 struct btrfs_file_extent_item *fi;
5534 u64 extent_end;
5535
5536 if (slot >= btrfs_header_nritems(leaf)) {
5537 ret = btrfs_next_leaf(root, path);
5538 if (ret < 0)
5539 goto out;
5540 else if (ret > 0)
5541 break;
5542 continue;
5543 }
5544
5545 btrfs_item_key_to_cpu(leaf, &key, slot);
5546 if (key.objectid < sctx->cur_ino ||
5547 key.type < BTRFS_EXTENT_DATA_KEY)
5548 goto next;
5549 if (key.objectid > sctx->cur_ino ||
5550 key.type > BTRFS_EXTENT_DATA_KEY ||
5551 key.offset >= end)
5552 break;
5553
5554 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5555 if (btrfs_file_extent_type(leaf, fi) ==
5556 BTRFS_FILE_EXTENT_INLINE) {
5557 u64 size = btrfs_file_extent_inline_len(leaf, slot, fi);
5558
5559 extent_end = ALIGN(key.offset + size,
5560 root->fs_info->sectorsize);
5561 } else {
5562 extent_end = key.offset +
5563 btrfs_file_extent_num_bytes(leaf, fi);
5564 }
5565 if (extent_end <= start)
5566 goto next;
5567 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5568 search_start = extent_end;
5569 goto next;
5570 }
5571 ret = 0;
5572 goto out;
5573next:
5574 path->slots[0]++;
5575 }
5576 ret = 1;
5577out:
5578 btrfs_free_path(path);
5579 return ret;
5580}
5581
Josef Bacik16e75492013-10-22 12:18:51 -04005582static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5583 struct btrfs_key *key)
5584{
5585 struct btrfs_file_extent_item *fi;
5586 u64 extent_end;
5587 u8 type;
5588 int ret = 0;
5589
5590 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5591 return 0;
5592
5593 if (sctx->cur_inode_last_extent == (u64)-1) {
5594 ret = get_last_extent(sctx, key->offset - 1);
5595 if (ret)
5596 return ret;
5597 }
5598
5599 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5600 struct btrfs_file_extent_item);
5601 type = btrfs_file_extent_type(path->nodes[0], fi);
5602 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005603 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5604 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005605 extent_end = ALIGN(key->offset + size,
Jeff Mahoneyda170662016-06-15 09:22:56 -04005606 sctx->send_root->fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04005607 } else {
5608 extent_end = key->offset +
5609 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5610 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005611
5612 if (path->slots[0] == 0 &&
5613 sctx->cur_inode_last_extent < key->offset) {
5614 /*
5615 * We might have skipped entire leafs that contained only
5616 * file extent items for our current inode. These leafs have
5617 * a generation number smaller (older) than the one in the
5618 * current leaf and the leaf our last extent came from, and
5619 * are located between these 2 leafs.
5620 */
5621 ret = get_last_extent(sctx, key->offset - 1);
5622 if (ret)
5623 return ret;
5624 }
5625
Filipe Manana82bfb2e2017-02-14 17:56:32 +00005626 if (sctx->cur_inode_last_extent < key->offset) {
5627 ret = range_is_hole_in_parent(sctx,
5628 sctx->cur_inode_last_extent,
5629 key->offset);
5630 if (ret < 0)
5631 return ret;
5632 else if (ret == 0)
5633 ret = send_hole(sctx, key->offset);
5634 else
5635 ret = 0;
5636 }
Josef Bacik16e75492013-10-22 12:18:51 -04005637 sctx->cur_inode_last_extent = extent_end;
5638 return ret;
5639}
5640
Alexander Block31db9f72012-07-25 23:19:24 +02005641static int process_extent(struct send_ctx *sctx,
5642 struct btrfs_path *path,
5643 struct btrfs_key *key)
5644{
Alexander Block31db9f72012-07-25 23:19:24 +02005645 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005646 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005647
5648 if (S_ISLNK(sctx->cur_inode_mode))
5649 return 0;
5650
5651 if (sctx->parent_root && !sctx->cur_inode_new) {
5652 ret = is_extent_unchanged(sctx, path, key);
5653 if (ret < 0)
5654 goto out;
5655 if (ret) {
5656 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005657 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005658 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005659 } else {
5660 struct btrfs_file_extent_item *ei;
5661 u8 type;
5662
5663 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5664 struct btrfs_file_extent_item);
5665 type = btrfs_file_extent_type(path->nodes[0], ei);
5666 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5667 type == BTRFS_FILE_EXTENT_REG) {
5668 /*
5669 * The send spec does not have a prealloc command yet,
5670 * so just leave a hole for prealloc'ed extents until
5671 * we have enough commands queued up to justify rev'ing
5672 * the send spec.
5673 */
5674 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5675 ret = 0;
5676 goto out;
5677 }
5678
5679 /* Have a hole, just skip it. */
5680 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5681 ret = 0;
5682 goto out;
5683 }
5684 }
Alexander Block31db9f72012-07-25 23:19:24 +02005685 }
5686
5687 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5688 sctx->cur_inode_size, &found_clone);
5689 if (ret != -ENOENT && ret < 0)
5690 goto out;
5691
5692 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005693 if (ret)
5694 goto out;
5695out_hole:
5696 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005697out:
5698 return ret;
5699}
5700
5701static int process_all_extents(struct send_ctx *sctx)
5702{
5703 int ret;
5704 struct btrfs_root *root;
5705 struct btrfs_path *path;
5706 struct btrfs_key key;
5707 struct btrfs_key found_key;
5708 struct extent_buffer *eb;
5709 int slot;
5710
5711 root = sctx->send_root;
5712 path = alloc_path_for_send();
5713 if (!path)
5714 return -ENOMEM;
5715
5716 key.objectid = sctx->cmp_key->objectid;
5717 key.type = BTRFS_EXTENT_DATA_KEY;
5718 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005719 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5720 if (ret < 0)
5721 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005722
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005723 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005724 eb = path->nodes[0];
5725 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005726
5727 if (slot >= btrfs_header_nritems(eb)) {
5728 ret = btrfs_next_leaf(root, path);
5729 if (ret < 0) {
5730 goto out;
5731 } else if (ret > 0) {
5732 ret = 0;
5733 break;
5734 }
5735 continue;
5736 }
5737
Alexander Block31db9f72012-07-25 23:19:24 +02005738 btrfs_item_key_to_cpu(eb, &found_key, slot);
5739
5740 if (found_key.objectid != key.objectid ||
5741 found_key.type != key.type) {
5742 ret = 0;
5743 goto out;
5744 }
5745
5746 ret = process_extent(sctx, path, &found_key);
5747 if (ret < 0)
5748 goto out;
5749
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005750 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005751 }
5752
5753out:
5754 btrfs_free_path(path);
5755 return ret;
5756}
5757
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005758static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5759 int *pending_move,
5760 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005761{
5762 int ret = 0;
5763
5764 if (sctx->cur_ino == 0)
5765 goto out;
5766 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005767 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005768 goto out;
5769 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5770 goto out;
5771
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005772 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005773 if (ret < 0)
5774 goto out;
5775
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005776 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005777out:
5778 return ret;
5779}
5780
5781static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5782{
5783 int ret = 0;
5784 u64 left_mode;
5785 u64 left_uid;
5786 u64 left_gid;
5787 u64 right_mode;
5788 u64 right_uid;
5789 u64 right_gid;
5790 int need_chmod = 0;
5791 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005792 int pending_move = 0;
5793 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005794
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005795 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5796 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005797 if (ret < 0)
5798 goto out;
5799
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005800 /*
5801 * We have processed the refs and thus need to advance send_progress.
5802 * Now, calls to get_cur_xxx will take the updated refs of the current
5803 * inode into account.
5804 *
5805 * On the other hand, if our current inode is a directory and couldn't
5806 * be moved/renamed because its parent was renamed/moved too and it has
5807 * a higher inode number, we can only move/rename our current inode
5808 * after we moved/renamed its parent. Therefore in this case operate on
5809 * the old path (pre move/rename) of our current inode, and the
5810 * move/rename will be performed later.
5811 */
5812 if (refs_processed && !pending_move)
5813 sctx->send_progress = sctx->cur_ino + 1;
5814
Alexander Block31db9f72012-07-25 23:19:24 +02005815 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5816 goto out;
5817 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5818 goto out;
5819
5820 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005821 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005822 if (ret < 0)
5823 goto out;
5824
Alex Lyakase2d044f2012-10-17 13:52:47 +00005825 if (!sctx->parent_root || sctx->cur_inode_new) {
5826 need_chown = 1;
5827 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005828 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005829 } else {
5830 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5831 NULL, NULL, &right_mode, &right_uid,
5832 &right_gid, NULL);
5833 if (ret < 0)
5834 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005835
Alex Lyakase2d044f2012-10-17 13:52:47 +00005836 if (left_uid != right_uid || left_gid != right_gid)
5837 need_chown = 1;
5838 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5839 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005840 }
5841
5842 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005843 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005844 if (sctx->cur_inode_last_extent == (u64)-1 ||
5845 sctx->cur_inode_last_extent <
5846 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005847 ret = get_last_extent(sctx, (u64)-1);
5848 if (ret)
5849 goto out;
5850 }
5851 if (sctx->cur_inode_last_extent <
5852 sctx->cur_inode_size) {
5853 ret = send_hole(sctx, sctx->cur_inode_size);
5854 if (ret)
5855 goto out;
5856 }
5857 }
Alexander Block31db9f72012-07-25 23:19:24 +02005858 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5859 sctx->cur_inode_size);
5860 if (ret < 0)
5861 goto out;
5862 }
5863
5864 if (need_chown) {
5865 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5866 left_uid, left_gid);
5867 if (ret < 0)
5868 goto out;
5869 }
5870 if (need_chmod) {
5871 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5872 left_mode);
5873 if (ret < 0)
5874 goto out;
5875 }
5876
5877 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005878 * If other directory inodes depended on our current directory
5879 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005880 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005881 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5882 ret = apply_children_dir_moves(sctx);
5883 if (ret)
5884 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005885 /*
5886 * Need to send that every time, no matter if it actually
5887 * changed between the two trees as we have done changes to
5888 * the inode before. If our inode is a directory and it's
5889 * waiting to be moved/renamed, we will send its utimes when
5890 * it's moved/renamed, therefore we don't need to do it here.
5891 */
5892 sctx->send_progress = sctx->cur_ino + 1;
5893 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5894 if (ret < 0)
5895 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005896 }
5897
Alexander Block31db9f72012-07-25 23:19:24 +02005898out:
5899 return ret;
5900}
5901
5902static int changed_inode(struct send_ctx *sctx,
5903 enum btrfs_compare_tree_result result)
5904{
5905 int ret = 0;
5906 struct btrfs_key *key = sctx->cmp_key;
5907 struct btrfs_inode_item *left_ii = NULL;
5908 struct btrfs_inode_item *right_ii = NULL;
5909 u64 left_gen = 0;
5910 u64 right_gen = 0;
5911
Alexander Block31db9f72012-07-25 23:19:24 +02005912 sctx->cur_ino = key->objectid;
5913 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005914 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005915
5916 /*
5917 * Set send_progress to current inode. This will tell all get_cur_xxx
5918 * functions that the current inode's refs are not updated yet. Later,
5919 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5920 */
Alexander Block31db9f72012-07-25 23:19:24 +02005921 sctx->send_progress = sctx->cur_ino;
5922
5923 if (result == BTRFS_COMPARE_TREE_NEW ||
5924 result == BTRFS_COMPARE_TREE_CHANGED) {
5925 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5926 sctx->left_path->slots[0],
5927 struct btrfs_inode_item);
5928 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5929 left_ii);
5930 } else {
5931 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5932 sctx->right_path->slots[0],
5933 struct btrfs_inode_item);
5934 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5935 right_ii);
5936 }
5937 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5938 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5939 sctx->right_path->slots[0],
5940 struct btrfs_inode_item);
5941
5942 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5943 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005944
5945 /*
5946 * The cur_ino = root dir case is special here. We can't treat
5947 * the inode as deleted+reused because it would generate a
5948 * stream that tries to delete/mkdir the root dir.
5949 */
5950 if (left_gen != right_gen &&
5951 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005952 sctx->cur_inode_new_gen = 1;
5953 }
5954
5955 if (result == BTRFS_COMPARE_TREE_NEW) {
5956 sctx->cur_inode_gen = left_gen;
5957 sctx->cur_inode_new = 1;
5958 sctx->cur_inode_deleted = 0;
5959 sctx->cur_inode_size = btrfs_inode_size(
5960 sctx->left_path->nodes[0], left_ii);
5961 sctx->cur_inode_mode = btrfs_inode_mode(
5962 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005963 sctx->cur_inode_rdev = btrfs_inode_rdev(
5964 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005965 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005966 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005967 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5968 sctx->cur_inode_gen = right_gen;
5969 sctx->cur_inode_new = 0;
5970 sctx->cur_inode_deleted = 1;
5971 sctx->cur_inode_size = btrfs_inode_size(
5972 sctx->right_path->nodes[0], right_ii);
5973 sctx->cur_inode_mode = btrfs_inode_mode(
5974 sctx->right_path->nodes[0], right_ii);
5975 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005976 /*
5977 * We need to do some special handling in case the inode was
5978 * reported as changed with a changed generation number. This
5979 * means that the original inode was deleted and new inode
5980 * reused the same inum. So we have to treat the old inode as
5981 * deleted and the new one as new.
5982 */
Alexander Block31db9f72012-07-25 23:19:24 +02005983 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005984 /*
5985 * First, process the inode as if it was deleted.
5986 */
Alexander Block31db9f72012-07-25 23:19:24 +02005987 sctx->cur_inode_gen = right_gen;
5988 sctx->cur_inode_new = 0;
5989 sctx->cur_inode_deleted = 1;
5990 sctx->cur_inode_size = btrfs_inode_size(
5991 sctx->right_path->nodes[0], right_ii);
5992 sctx->cur_inode_mode = btrfs_inode_mode(
5993 sctx->right_path->nodes[0], right_ii);
5994 ret = process_all_refs(sctx,
5995 BTRFS_COMPARE_TREE_DELETED);
5996 if (ret < 0)
5997 goto out;
5998
Alexander Block766702e2012-07-28 14:11:31 +02005999 /*
6000 * Now process the inode as if it was new.
6001 */
Alexander Block31db9f72012-07-25 23:19:24 +02006002 sctx->cur_inode_gen = left_gen;
6003 sctx->cur_inode_new = 1;
6004 sctx->cur_inode_deleted = 0;
6005 sctx->cur_inode_size = btrfs_inode_size(
6006 sctx->left_path->nodes[0], left_ii);
6007 sctx->cur_inode_mode = btrfs_inode_mode(
6008 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08006009 sctx->cur_inode_rdev = btrfs_inode_rdev(
6010 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02006011 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006012 if (ret < 0)
6013 goto out;
6014
6015 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6016 if (ret < 0)
6017 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02006018 /*
6019 * Advance send_progress now as we did not get into
6020 * process_recorded_refs_if_needed in the new_gen case.
6021 */
6022 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02006023
6024 /*
6025 * Now process all extents and xattrs of the inode as if
6026 * they were all new.
6027 */
Alexander Block31db9f72012-07-25 23:19:24 +02006028 ret = process_all_extents(sctx);
6029 if (ret < 0)
6030 goto out;
6031 ret = process_all_new_xattrs(sctx);
6032 if (ret < 0)
6033 goto out;
6034 } else {
6035 sctx->cur_inode_gen = left_gen;
6036 sctx->cur_inode_new = 0;
6037 sctx->cur_inode_new_gen = 0;
6038 sctx->cur_inode_deleted = 0;
6039 sctx->cur_inode_size = btrfs_inode_size(
6040 sctx->left_path->nodes[0], left_ii);
6041 sctx->cur_inode_mode = btrfs_inode_mode(
6042 sctx->left_path->nodes[0], left_ii);
6043 }
6044 }
6045
6046out:
6047 return ret;
6048}
6049
Alexander Block766702e2012-07-28 14:11:31 +02006050/*
6051 * We have to process new refs before deleted refs, but compare_trees gives us
6052 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6053 * first and later process them in process_recorded_refs.
6054 * For the cur_inode_new_gen case, we skip recording completely because
6055 * changed_inode did already initiate processing of refs. The reason for this is
6056 * that in this case, compare_tree actually compares the refs of 2 different
6057 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6058 * refs of the right tree as deleted and all refs of the left tree as new.
6059 */
Alexander Block31db9f72012-07-25 23:19:24 +02006060static int changed_ref(struct send_ctx *sctx,
6061 enum btrfs_compare_tree_result result)
6062{
6063 int ret = 0;
6064
Filipe Manana95155582016-08-01 01:50:37 +01006065 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6066 inconsistent_snapshot_error(sctx, result, "reference");
6067 return -EIO;
6068 }
Alexander Block31db9f72012-07-25 23:19:24 +02006069
6070 if (!sctx->cur_inode_new_gen &&
6071 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6072 if (result == BTRFS_COMPARE_TREE_NEW)
6073 ret = record_new_ref(sctx);
6074 else if (result == BTRFS_COMPARE_TREE_DELETED)
6075 ret = record_deleted_ref(sctx);
6076 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6077 ret = record_changed_ref(sctx);
6078 }
6079
6080 return ret;
6081}
6082
Alexander Block766702e2012-07-28 14:11:31 +02006083/*
6084 * Process new/deleted/changed xattrs. We skip processing in the
6085 * cur_inode_new_gen case because changed_inode did already initiate processing
6086 * of xattrs. The reason is the same as in changed_ref
6087 */
Alexander Block31db9f72012-07-25 23:19:24 +02006088static int changed_xattr(struct send_ctx *sctx,
6089 enum btrfs_compare_tree_result result)
6090{
6091 int ret = 0;
6092
Filipe Manana95155582016-08-01 01:50:37 +01006093 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6094 inconsistent_snapshot_error(sctx, result, "xattr");
6095 return -EIO;
6096 }
Alexander Block31db9f72012-07-25 23:19:24 +02006097
6098 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6099 if (result == BTRFS_COMPARE_TREE_NEW)
6100 ret = process_new_xattr(sctx);
6101 else if (result == BTRFS_COMPARE_TREE_DELETED)
6102 ret = process_deleted_xattr(sctx);
6103 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6104 ret = process_changed_xattr(sctx);
6105 }
6106
6107 return ret;
6108}
6109
Alexander Block766702e2012-07-28 14:11:31 +02006110/*
6111 * Process new/deleted/changed extents. We skip processing in the
6112 * cur_inode_new_gen case because changed_inode did already initiate processing
6113 * of extents. The reason is the same as in changed_ref
6114 */
Alexander Block31db9f72012-07-25 23:19:24 +02006115static int changed_extent(struct send_ctx *sctx,
6116 enum btrfs_compare_tree_result result)
6117{
6118 int ret = 0;
6119
Filipe Manana95155582016-08-01 01:50:37 +01006120 if (sctx->cur_ino != sctx->cmp_key->objectid) {
Filipe Mananad5e84fd2016-09-19 10:57:40 +01006121
6122 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6123 struct extent_buffer *leaf_l;
6124 struct extent_buffer *leaf_r;
6125 struct btrfs_file_extent_item *ei_l;
6126 struct btrfs_file_extent_item *ei_r;
6127
6128 leaf_l = sctx->left_path->nodes[0];
6129 leaf_r = sctx->right_path->nodes[0];
6130 ei_l = btrfs_item_ptr(leaf_l,
6131 sctx->left_path->slots[0],
6132 struct btrfs_file_extent_item);
6133 ei_r = btrfs_item_ptr(leaf_r,
6134 sctx->right_path->slots[0],
6135 struct btrfs_file_extent_item);
6136
6137 /*
6138 * We may have found an extent item that has changed
6139 * only its disk_bytenr field and the corresponding
6140 * inode item was not updated. This case happens due to
6141 * very specific timings during relocation when a leaf
6142 * that contains file extent items is COWed while
6143 * relocation is ongoing and its in the stage where it
6144 * updates data pointers. So when this happens we can
6145 * safely ignore it since we know it's the same extent,
6146 * but just at different logical and physical locations
6147 * (when an extent is fully replaced with a new one, we
6148 * know the generation number must have changed too,
6149 * since snapshot creation implies committing the current
6150 * transaction, and the inode item must have been updated
6151 * as well).
6152 * This replacement of the disk_bytenr happens at
6153 * relocation.c:replace_file_extents() through
6154 * relocation.c:btrfs_reloc_cow_block().
6155 */
6156 if (btrfs_file_extent_generation(leaf_l, ei_l) ==
6157 btrfs_file_extent_generation(leaf_r, ei_r) &&
6158 btrfs_file_extent_ram_bytes(leaf_l, ei_l) ==
6159 btrfs_file_extent_ram_bytes(leaf_r, ei_r) &&
6160 btrfs_file_extent_compression(leaf_l, ei_l) ==
6161 btrfs_file_extent_compression(leaf_r, ei_r) &&
6162 btrfs_file_extent_encryption(leaf_l, ei_l) ==
6163 btrfs_file_extent_encryption(leaf_r, ei_r) &&
6164 btrfs_file_extent_other_encoding(leaf_l, ei_l) ==
6165 btrfs_file_extent_other_encoding(leaf_r, ei_r) &&
6166 btrfs_file_extent_type(leaf_l, ei_l) ==
6167 btrfs_file_extent_type(leaf_r, ei_r) &&
6168 btrfs_file_extent_disk_bytenr(leaf_l, ei_l) !=
6169 btrfs_file_extent_disk_bytenr(leaf_r, ei_r) &&
6170 btrfs_file_extent_disk_num_bytes(leaf_l, ei_l) ==
6171 btrfs_file_extent_disk_num_bytes(leaf_r, ei_r) &&
6172 btrfs_file_extent_offset(leaf_l, ei_l) ==
6173 btrfs_file_extent_offset(leaf_r, ei_r) &&
6174 btrfs_file_extent_num_bytes(leaf_l, ei_l) ==
6175 btrfs_file_extent_num_bytes(leaf_r, ei_r))
6176 return 0;
6177 }
6178
Filipe Manana95155582016-08-01 01:50:37 +01006179 inconsistent_snapshot_error(sctx, result, "extent");
6180 return -EIO;
6181 }
Alexander Block31db9f72012-07-25 23:19:24 +02006182
6183 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6184 if (result != BTRFS_COMPARE_TREE_DELETED)
6185 ret = process_extent(sctx, sctx->left_path,
6186 sctx->cmp_key);
6187 }
6188
6189 return ret;
6190}
6191
Josef Bacikba5e8f22013-08-16 16:52:55 -04006192static int dir_changed(struct send_ctx *sctx, u64 dir)
6193{
6194 u64 orig_gen, new_gen;
6195 int ret;
6196
6197 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6198 NULL, NULL);
6199 if (ret)
6200 return ret;
6201
6202 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6203 NULL, NULL, NULL);
6204 if (ret)
6205 return ret;
6206
6207 return (orig_gen != new_gen) ? 1 : 0;
6208}
6209
6210static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6211 struct btrfs_key *key)
6212{
6213 struct btrfs_inode_extref *extref;
6214 struct extent_buffer *leaf;
6215 u64 dirid = 0, last_dirid = 0;
6216 unsigned long ptr;
6217 u32 item_size;
6218 u32 cur_offset = 0;
6219 int ref_name_len;
6220 int ret = 0;
6221
6222 /* Easy case, just check this one dirid */
6223 if (key->type == BTRFS_INODE_REF_KEY) {
6224 dirid = key->offset;
6225
6226 ret = dir_changed(sctx, dirid);
6227 goto out;
6228 }
6229
6230 leaf = path->nodes[0];
6231 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6232 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6233 while (cur_offset < item_size) {
6234 extref = (struct btrfs_inode_extref *)(ptr +
6235 cur_offset);
6236 dirid = btrfs_inode_extref_parent(leaf, extref);
6237 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6238 cur_offset += ref_name_len + sizeof(*extref);
6239 if (dirid == last_dirid)
6240 continue;
6241 ret = dir_changed(sctx, dirid);
6242 if (ret)
6243 break;
6244 last_dirid = dirid;
6245 }
6246out:
6247 return ret;
6248}
6249
Alexander Block766702e2012-07-28 14:11:31 +02006250/*
6251 * Updates compare related fields in sctx and simply forwards to the actual
6252 * changed_xxx functions.
6253 */
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006254static int changed_cb(struct btrfs_path *left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02006255 struct btrfs_path *right_path,
6256 struct btrfs_key *key,
6257 enum btrfs_compare_tree_result result,
6258 void *ctx)
6259{
6260 int ret = 0;
6261 struct send_ctx *sctx = ctx;
6262
Josef Bacikba5e8f22013-08-16 16:52:55 -04006263 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04006264 if (key->type == BTRFS_INODE_REF_KEY ||
6265 key->type == BTRFS_INODE_EXTREF_KEY) {
6266 ret = compare_refs(sctx, left_path, key);
6267 if (!ret)
6268 return 0;
6269 if (ret < 0)
6270 return ret;
6271 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6272 return maybe_send_hole(sctx, left_path, key);
6273 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04006274 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04006275 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04006276 result = BTRFS_COMPARE_TREE_CHANGED;
6277 ret = 0;
6278 }
6279
Alexander Block31db9f72012-07-25 23:19:24 +02006280 sctx->left_path = left_path;
6281 sctx->right_path = right_path;
6282 sctx->cmp_key = key;
6283
6284 ret = finish_inode_if_needed(sctx, 0);
6285 if (ret < 0)
6286 goto out;
6287
Alexander Block2981e222012-08-01 14:47:03 +02006288 /* Ignore non-FS objects */
6289 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6290 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6291 goto out;
6292
Alexander Block31db9f72012-07-25 23:19:24 +02006293 if (key->type == BTRFS_INODE_ITEM_KEY)
6294 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00006295 else if (key->type == BTRFS_INODE_REF_KEY ||
6296 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02006297 ret = changed_ref(sctx, result);
6298 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6299 ret = changed_xattr(sctx, result);
6300 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6301 ret = changed_extent(sctx, result);
6302
6303out:
6304 return ret;
6305}
6306
6307static int full_send_tree(struct send_ctx *sctx)
6308{
6309 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02006310 struct btrfs_root *send_root = sctx->send_root;
6311 struct btrfs_key key;
6312 struct btrfs_key found_key;
6313 struct btrfs_path *path;
6314 struct extent_buffer *eb;
6315 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02006316
6317 path = alloc_path_for_send();
6318 if (!path)
6319 return -ENOMEM;
6320
Alexander Block31db9f72012-07-25 23:19:24 +02006321 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6322 key.type = BTRFS_INODE_ITEM_KEY;
6323 key.offset = 0;
6324
Alexander Block31db9f72012-07-25 23:19:24 +02006325 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6326 if (ret < 0)
6327 goto out;
6328 if (ret)
6329 goto out_finish;
6330
6331 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02006332 eb = path->nodes[0];
6333 slot = path->slots[0];
6334 btrfs_item_key_to_cpu(eb, &found_key, slot);
6335
Nikolay Borisovee8c4942017-08-21 12:43:45 +03006336 ret = changed_cb(path, NULL, &found_key,
6337 BTRFS_COMPARE_TREE_NEW, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02006338 if (ret < 0)
6339 goto out;
6340
6341 key.objectid = found_key.objectid;
6342 key.type = found_key.type;
6343 key.offset = found_key.offset + 1;
6344
6345 ret = btrfs_next_item(send_root, path);
6346 if (ret < 0)
6347 goto out;
6348 if (ret) {
6349 ret = 0;
6350 break;
6351 }
6352 }
6353
6354out_finish:
6355 ret = finish_inode_if_needed(sctx, 1);
6356
6357out:
6358 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02006359 return ret;
6360}
6361
6362static int send_subvol(struct send_ctx *sctx)
6363{
6364 int ret;
6365
Stefan Behrensc2c71322013-04-10 17:10:52 +00006366 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6367 ret = send_header(sctx);
6368 if (ret < 0)
6369 goto out;
6370 }
Alexander Block31db9f72012-07-25 23:19:24 +02006371
6372 ret = send_subvol_begin(sctx);
6373 if (ret < 0)
6374 goto out;
6375
6376 if (sctx->parent_root) {
6377 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6378 changed_cb, sctx);
6379 if (ret < 0)
6380 goto out;
6381 ret = finish_inode_if_needed(sctx, 1);
6382 if (ret < 0)
6383 goto out;
6384 } else {
6385 ret = full_send_tree(sctx);
6386 if (ret < 0)
6387 goto out;
6388 }
6389
6390out:
Alexander Block31db9f72012-07-25 23:19:24 +02006391 free_recorded_refs(sctx);
6392 return ret;
6393}
6394
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006395/*
6396 * If orphan cleanup did remove any orphans from a root, it means the tree
6397 * was modified and therefore the commit root is not the same as the current
6398 * root anymore. This is a problem, because send uses the commit root and
6399 * therefore can see inode items that don't exist in the current root anymore,
6400 * and for example make calls to btrfs_iget, which will do tree lookups based
6401 * on the current root and not on the commit root. Those lookups will fail,
6402 * returning a -ESTALE error, and making send fail with that error. So make
6403 * sure a send does not see any orphans we have just removed, and that it will
6404 * see the same inodes regardless of whether a transaction commit happened
6405 * before it started (meaning that the commit root will be the same as the
6406 * current root) or not.
6407 */
6408static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6409{
6410 int i;
6411 struct btrfs_trans_handle *trans = NULL;
6412
6413again:
6414 if (sctx->parent_root &&
6415 sctx->parent_root->node != sctx->parent_root->commit_root)
6416 goto commit_trans;
6417
6418 for (i = 0; i < sctx->clone_roots_cnt; i++)
6419 if (sctx->clone_roots[i].root->node !=
6420 sctx->clone_roots[i].root->commit_root)
6421 goto commit_trans;
6422
6423 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006424 return btrfs_end_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006425
6426 return 0;
6427
6428commit_trans:
6429 /* Use any root, all fs roots will get their commit roots updated. */
6430 if (!trans) {
6431 trans = btrfs_join_transaction(sctx->send_root);
6432 if (IS_ERR(trans))
6433 return PTR_ERR(trans);
6434 goto again;
6435 }
6436
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006437 return btrfs_commit_transaction(trans);
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006438}
6439
David Sterba66ef7d62013-12-17 15:07:20 +01006440static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6441{
6442 spin_lock(&root->root_item_lock);
6443 root->send_in_progress--;
6444 /*
6445 * Not much left to do, we don't know why it's unbalanced and
6446 * can't blindly reset it to 0.
6447 */
6448 if (root->send_in_progress < 0)
6449 btrfs_err(root->fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006450 "send_in_progres unbalanced %d root %llu",
6451 root->send_in_progress, root->root_key.objectid);
David Sterba66ef7d62013-12-17 15:07:20 +01006452 spin_unlock(&root->root_item_lock);
6453}
6454
Josef Bacik2351f432017-09-27 10:43:13 -04006455long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
Alexander Block31db9f72012-07-25 23:19:24 +02006456{
6457 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006458 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6459 struct btrfs_fs_info *fs_info = send_root->fs_info;
Alexander Block31db9f72012-07-25 23:19:24 +02006460 struct btrfs_root *clone_root;
Alexander Block31db9f72012-07-25 23:19:24 +02006461 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02006462 struct send_ctx *sctx = NULL;
6463 u32 i;
6464 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01006465 int clone_sources_to_rollback = 0;
David Sterbae55d1152016-04-11 18:52:02 +02006466 unsigned alloc_size;
Wang Shilong896c14f2014-01-07 17:25:18 +08006467 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08006468 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02006469
6470 if (!capable(CAP_SYS_ADMIN))
6471 return -EPERM;
6472
Josef Bacik139f8072013-05-20 11:26:50 -04006473 /*
David Sterba2c686532013-12-16 17:34:17 +01006474 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02006475 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01006476 */
6477 spin_lock(&send_root->root_item_lock);
6478 send_root->send_in_progress++;
6479 spin_unlock(&send_root->root_item_lock);
6480
6481 /*
Josef Bacik139f8072013-05-20 11:26:50 -04006482 * This is done when we lookup the root, it should already be complete
6483 * by the time we get here.
6484 */
6485 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6486
6487 /*
David Sterba2c686532013-12-16 17:34:17 +01006488 * Userspace tools do the checks and warn the user if it's
6489 * not RO.
6490 */
6491 if (!btrfs_root_readonly(send_root)) {
6492 ret = -EPERM;
6493 goto out;
6494 }
6495
Dan Carpenter457ae722017-03-17 23:51:20 +03006496 /*
6497 * Check that we don't overflow at later allocations, we request
6498 * clone_sources_count + 1 items, and compare to unsigned long inside
6499 * access_ok.
6500 */
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006501 if (arg->clone_sources_count >
Dan Carpenter457ae722017-03-17 23:51:20 +03006502 ULONG_MAX / sizeof(struct clone_root) - 1) {
Dan Carpenterf5ecec32016-04-13 09:40:59 +03006503 ret = -EINVAL;
6504 goto out;
6505 }
6506
Alexander Block31db9f72012-07-25 23:19:24 +02006507 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05006508 sizeof(*arg->clone_sources) *
6509 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02006510 ret = -EFAULT;
6511 goto out;
6512 }
6513
Stefan Behrensc2c71322013-04-10 17:10:52 +00006514 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006515 ret = -EINVAL;
6516 goto out;
6517 }
6518
David Sterbae780b0d2016-01-18 18:42:13 +01006519 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006520 if (!sctx) {
6521 ret = -ENOMEM;
6522 goto out;
6523 }
6524
6525 INIT_LIST_HEAD(&sctx->new_refs);
6526 INIT_LIST_HEAD(&sctx->deleted_refs);
David Sterbae780b0d2016-01-18 18:42:13 +01006527 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006528 INIT_LIST_HEAD(&sctx->name_cache_list);
6529
Mark Fashehcb95e7b2013-02-04 20:54:57 +00006530 sctx->flags = arg->flags;
6531
Alexander Block31db9f72012-07-25 23:19:24 +02006532 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00006533 if (!sctx->send_filp) {
6534 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02006535 goto out;
6536 }
6537
Alexander Block31db9f72012-07-25 23:19:24 +02006538 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02006539 /*
6540 * Unlikely but possible, if the subvolume is marked for deletion but
6541 * is slow to remove the directory entry, send can still be started
6542 */
6543 if (btrfs_root_dead(sctx->send_root)) {
6544 ret = -EPERM;
6545 goto out;
6546 }
6547
Alexander Block31db9f72012-07-25 23:19:24 +02006548 sctx->clone_roots_cnt = arg->clone_sources_count;
6549
6550 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
Michal Hocko752ade62017-05-08 15:57:27 -07006551 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006552 if (!sctx->send_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006553 ret = -ENOMEM;
6554 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006555 }
6556
Michal Hocko752ade62017-05-08 15:57:27 -07006557 sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006558 if (!sctx->read_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07006559 ret = -ENOMEM;
6560 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006561 }
6562
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006563 sctx->pending_dir_moves = RB_ROOT;
6564 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00006565 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006566
David Sterbae55d1152016-04-11 18:52:02 +02006567 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6568
David Sterba818e0102017-05-31 18:40:02 +02006569 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006570 if (!sctx->clone_roots) {
David Sterba818e0102017-05-31 18:40:02 +02006571 ret = -ENOMEM;
6572 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006573 }
6574
David Sterbae55d1152016-04-11 18:52:02 +02006575 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6576
Alexander Block31db9f72012-07-25 23:19:24 +02006577 if (arg->clone_sources_count) {
Michal Hocko752ade62017-05-08 15:57:27 -07006578 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
Alexander Block31db9f72012-07-25 23:19:24 +02006579 if (!clone_sources_tmp) {
Michal Hocko752ade62017-05-08 15:57:27 -07006580 ret = -ENOMEM;
6581 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02006582 }
6583
6584 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
David Sterbae55d1152016-04-11 18:52:02 +02006585 alloc_size);
Alexander Block31db9f72012-07-25 23:19:24 +02006586 if (ret) {
6587 ret = -EFAULT;
6588 goto out;
6589 }
6590
6591 for (i = 0; i < arg->clone_sources_count; i++) {
6592 key.objectid = clone_sources_tmp[i];
6593 key.type = BTRFS_ROOT_ITEM_KEY;
6594 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006595
6596 index = srcu_read_lock(&fs_info->subvol_srcu);
6597
Alexander Block31db9f72012-07-25 23:19:24 +02006598 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02006599 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006600 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006601 ret = PTR_ERR(clone_root);
6602 goto out;
6603 }
David Sterba2c686532013-12-16 17:34:17 +01006604 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00006605 if (!btrfs_root_readonly(clone_root) ||
6606 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006607 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006608 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006609 ret = -EPERM;
6610 goto out;
6611 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00006612 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01006613 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006614 srcu_read_unlock(&fs_info->subvol_srcu, index);
6615
Alexander Block31db9f72012-07-25 23:19:24 +02006616 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00006617 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006618 }
David Sterba2f913062016-04-11 18:40:08 +02006619 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006620 clone_sources_tmp = NULL;
6621 }
6622
6623 if (arg->parent_root) {
6624 key.objectid = arg->parent_root;
6625 key.type = BTRFS_ROOT_ITEM_KEY;
6626 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006627
6628 index = srcu_read_lock(&fs_info->subvol_srcu);
6629
Alexander Block31db9f72012-07-25 23:19:24 +02006630 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006631 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006632 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006633 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02006634 goto out;
6635 }
Wang Shilong18f687d2014-01-07 17:25:19 +08006636
David Sterba2c686532013-12-16 17:34:17 +01006637 spin_lock(&sctx->parent_root->root_item_lock);
6638 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02006639 if (!btrfs_root_readonly(sctx->parent_root) ||
6640 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006641 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006642 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006643 ret = -EPERM;
6644 goto out;
6645 }
6646 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006647
6648 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006649 }
6650
6651 /*
6652 * Clones from send_root are allowed, but only if the clone source
6653 * is behind the current send position. This is checked while searching
6654 * for possible clone sources.
6655 */
6656 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6657
6658 /* We do a bsearch later */
6659 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6660 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6661 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08006662 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006663
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006664 ret = ensure_commit_roots_uptodate(sctx);
6665 if (ret)
6666 goto out;
6667
David Sterba2755a0d2014-07-31 00:43:18 +02006668 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02006669 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04006670 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02006671 if (ret < 0)
6672 goto out;
6673
Stefan Behrensc2c71322013-04-10 17:10:52 +00006674 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6675 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6676 if (ret < 0)
6677 goto out;
6678 ret = send_cmd(sctx);
6679 if (ret < 0)
6680 goto out;
6681 }
Alexander Block31db9f72012-07-25 23:19:24 +02006682
6683out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006684 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6685 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6686 struct rb_node *n;
6687 struct pending_dir_move *pm;
6688
6689 n = rb_first(&sctx->pending_dir_moves);
6690 pm = rb_entry(n, struct pending_dir_move, node);
6691 while (!list_empty(&pm->list)) {
6692 struct pending_dir_move *pm2;
6693
6694 pm2 = list_first_entry(&pm->list,
6695 struct pending_dir_move, list);
6696 free_pending_move(sctx, pm2);
6697 }
6698 free_pending_move(sctx, pm);
6699 }
6700
6701 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6702 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6703 struct rb_node *n;
6704 struct waiting_dir_move *dm;
6705
6706 n = rb_first(&sctx->waiting_dir_moves);
6707 dm = rb_entry(n, struct waiting_dir_move, node);
6708 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6709 kfree(dm);
6710 }
6711
Filipe Manana9dc44212014-02-19 14:31:44 +00006712 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6713 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6714 struct rb_node *n;
6715 struct orphan_dir_info *odi;
6716
6717 n = rb_first(&sctx->orphan_dirs);
6718 odi = rb_entry(n, struct orphan_dir_info, node);
6719 free_orphan_dir_info(sctx, odi);
6720 }
6721
Wang Shilong896c14f2014-01-07 17:25:18 +08006722 if (sort_clone_roots) {
6723 for (i = 0; i < sctx->clone_roots_cnt; i++)
6724 btrfs_root_dec_send_in_progress(
6725 sctx->clone_roots[i].root);
6726 } else {
6727 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6728 btrfs_root_dec_send_in_progress(
6729 sctx->clone_roots[i].root);
6730
6731 btrfs_root_dec_send_in_progress(send_root);
6732 }
David Sterba66ef7d62013-12-17 15:07:20 +01006733 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6734 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01006735
David Sterba2f913062016-04-11 18:40:08 +02006736 kvfree(clone_sources_tmp);
Alexander Block31db9f72012-07-25 23:19:24 +02006737
6738 if (sctx) {
6739 if (sctx->send_filp)
6740 fput(sctx->send_filp);
6741
David Sterbac03d01f2016-04-11 18:40:08 +02006742 kvfree(sctx->clone_roots);
David Sterba6ff48ce2016-04-11 18:40:08 +02006743 kvfree(sctx->send_buf);
David Sterbaeb5b75f2016-04-11 18:40:08 +02006744 kvfree(sctx->read_buf);
Alexander Block31db9f72012-07-25 23:19:24 +02006745
6746 name_cache_free(sctx);
6747
6748 kfree(sctx);
6749 }
6750
6751 return ret;
6752}