blob: ced935bf5f957a89b621dcedd2cacb0fe85136e7 [file] [log] [blame]
Alexander Block31db9f72012-07-25 23:19:24 +02001/*
2 * Copyright (C) 2012 Alexander Block. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/bsearch.h>
20#include <linux/fs.h>
21#include <linux/file.h>
22#include <linux/sort.h>
23#include <linux/mount.h>
24#include <linux/xattr.h>
25#include <linux/posix_acl_xattr.h>
26#include <linux/radix-tree.h>
Stephen Rothwella1857eb2012-07-27 10:11:13 +100027#include <linux/vmalloc.h>
Andy Shevchenkoed848852013-08-21 10:32:13 +030028#include <linux/string.h>
Alexander Block31db9f72012-07-25 23:19:24 +020029
30#include "send.h"
31#include "backref.h"
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +000032#include "hash.h"
Alexander Block31db9f72012-07-25 23:19:24 +020033#include "locking.h"
34#include "disk-io.h"
35#include "btrfs_inode.h"
36#include "transaction.h"
37
38static int g_verbose = 0;
39
40#define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
41
42/*
43 * A fs_path is a helper to dynamically build path names with unknown size.
44 * It reallocates the internal buffer on demand.
45 * It allows fast adding of path elements on the right side (normal path) and
46 * fast adding to the left side (reversed path). A reversed path can also be
47 * unreversed if needed.
48 */
49struct fs_path {
50 union {
51 struct {
52 char *start;
53 char *end;
Alexander Block31db9f72012-07-25 23:19:24 +020054
55 char *buf;
David Sterba1f5a7ff2014-02-03 19:23:47 +010056 unsigned short buf_len:15;
57 unsigned short reversed:1;
Alexander Block31db9f72012-07-25 23:19:24 +020058 char inline_buf[];
59 };
David Sterbaace01052014-02-05 16:17:34 +010060 /*
61 * Average path length does not exceed 200 bytes, we'll have
62 * better packing in the slab and higher chance to satisfy
63 * a allocation later during send.
64 */
65 char pad[256];
Alexander Block31db9f72012-07-25 23:19:24 +020066 };
67};
68#define FS_PATH_INLINE_SIZE \
69 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
70
71
72/* reused for each extent */
73struct clone_root {
74 struct btrfs_root *root;
75 u64 ino;
76 u64 offset;
77
78 u64 found_refs;
79};
80
81#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
82#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
83
84struct send_ctx {
85 struct file *send_filp;
86 loff_t send_off;
87 char *send_buf;
88 u32 send_size;
89 u32 send_max_size;
90 u64 total_send_size;
91 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000092 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020093
Alexander Block31db9f72012-07-25 23:19:24 +020094 struct btrfs_root *send_root;
95 struct btrfs_root *parent_root;
96 struct clone_root *clone_roots;
97 int clone_roots_cnt;
98
99 /* current state of the compare_tree call */
100 struct btrfs_path *left_path;
101 struct btrfs_path *right_path;
102 struct btrfs_key *cmp_key;
103
104 /*
105 * infos of the currently processed inode. In case of deleted inodes,
106 * these are the values from the deleted inode.
107 */
108 u64 cur_ino;
109 u64 cur_inode_gen;
110 int cur_inode_new;
111 int cur_inode_new_gen;
112 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200113 u64 cur_inode_size;
114 u64 cur_inode_mode;
Liu Bo644d1942014-02-27 17:29:01 +0800115 u64 cur_inode_rdev;
Josef Bacik16e75492013-10-22 12:18:51 -0400116 u64 cur_inode_last_extent;
Alexander Block31db9f72012-07-25 23:19:24 +0200117
118 u64 send_progress;
119
120 struct list_head new_refs;
121 struct list_head deleted_refs;
122
123 struct radix_tree_root name_cache;
124 struct list_head name_cache_list;
125 int name_cache_size;
126
Liu Bo2131bcd32014-03-05 10:07:35 +0800127 struct file_ra_state ra;
128
Alexander Block31db9f72012-07-25 23:19:24 +0200129 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000130
131 /*
132 * We process inodes by their increasing order, so if before an
133 * incremental send we reverse the parent/child relationship of
134 * directories such that a directory with a lower inode number was
135 * the parent of a directory with a higher inode number, and the one
136 * becoming the new parent got renamed too, we can't rename/move the
137 * directory with lower inode number when we finish processing it - we
138 * must process the directory with higher inode number first, then
139 * rename/move it and then rename/move the directory with lower inode
140 * number. Example follows.
141 *
142 * Tree state when the first send was performed:
143 *
144 * .
145 * |-- a (ino 257)
146 * |-- b (ino 258)
147 * |
148 * |
149 * |-- c (ino 259)
150 * | |-- d (ino 260)
151 * |
152 * |-- c2 (ino 261)
153 *
154 * Tree state when the second (incremental) send is performed:
155 *
156 * .
157 * |-- a (ino 257)
158 * |-- b (ino 258)
159 * |-- c2 (ino 261)
160 * |-- d2 (ino 260)
161 * |-- cc (ino 259)
162 *
163 * The sequence of steps that lead to the second state was:
164 *
165 * mv /a/b/c/d /a/b/c2/d2
166 * mv /a/b/c /a/b/c2/d2/cc
167 *
168 * "c" has lower inode number, but we can't move it (2nd mv operation)
169 * before we move "d", which has higher inode number.
170 *
171 * So we just memorize which move/rename operations must be performed
172 * later when their respective parent is processed and moved/renamed.
173 */
174
175 /* Indexed by parent directory inode number. */
176 struct rb_root pending_dir_moves;
177
178 /*
179 * Reverse index, indexed by the inode number of a directory that
180 * is waiting for the move/rename of its immediate parent before its
181 * own move/rename can be performed.
182 */
183 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000184
185 /*
186 * A directory that is going to be rm'ed might have a child directory
187 * which is in the pending directory moves index above. In this case,
188 * the directory can only be removed after the move/rename of its child
189 * is performed. Example:
190 *
191 * Parent snapshot:
192 *
193 * . (ino 256)
194 * |-- a/ (ino 257)
195 * |-- b/ (ino 258)
196 * |-- c/ (ino 259)
197 * | |-- x/ (ino 260)
198 * |
199 * |-- y/ (ino 261)
200 *
201 * Send snapshot:
202 *
203 * . (ino 256)
204 * |-- a/ (ino 257)
205 * |-- b/ (ino 258)
206 * |-- YY/ (ino 261)
207 * |-- x/ (ino 260)
208 *
209 * Sequence of steps that lead to the send snapshot:
210 * rm -f /a/b/c/foo.txt
211 * mv /a/b/y /a/b/YY
212 * mv /a/b/c/x /a/b/YY
213 * rmdir /a/b/c
214 *
215 * When the child is processed, its move/rename is delayed until its
216 * parent is processed (as explained above), but all other operations
217 * like update utimes, chown, chgrp, etc, are performed and the paths
218 * that it uses for those operations must use the orphanized name of
219 * its parent (the directory we're going to rm later), so we need to
220 * memorize that name.
221 *
222 * Indexed by the inode number of the directory to be deleted.
223 */
224 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000225};
226
227struct pending_dir_move {
228 struct rb_node node;
229 struct list_head list;
230 u64 parent_ino;
231 u64 ino;
232 u64 gen;
Filipe Manana84471e22015-02-28 22:29:22 +0000233 bool is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000234 struct list_head update_refs;
235};
236
237struct waiting_dir_move {
238 struct rb_node node;
239 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000240 /*
241 * There might be some directory that could not be removed because it
242 * was waiting for this directory inode to be moved first. Therefore
243 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
244 */
245 u64 rmdir_ino;
Filipe Manana8b191a62015-04-09 14:09:14 +0100246 bool orphanized;
Filipe Manana9dc44212014-02-19 14:31:44 +0000247};
248
249struct orphan_dir_info {
250 struct rb_node node;
251 u64 ino;
252 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +0200253};
254
255struct name_cache_entry {
256 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200257 /*
258 * radix_tree has only 32bit entries but we need to handle 64bit inums.
259 * We use the lower 32bit of the 64bit inum to store it in the tree. If
260 * more then one inum would fall into the same entry, we use radix_list
261 * to store the additional entries. radix_list is also used to store
262 * entries where two entries have the same inum but different
263 * generations.
264 */
265 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200266 u64 ino;
267 u64 gen;
268 u64 parent_ino;
269 u64 parent_gen;
270 int ret;
271 int need_later_update;
272 int name_len;
273 char name[];
274};
275
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000276static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
277
Filipe Manana9dc44212014-02-19 14:31:44 +0000278static struct waiting_dir_move *
279get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
280
281static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
282
Josef Bacik16e75492013-10-22 12:18:51 -0400283static int need_send_hole(struct send_ctx *sctx)
284{
285 return (sctx->parent_root && !sctx->cur_inode_new &&
286 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
287 S_ISREG(sctx->cur_inode_mode));
288}
289
Alexander Block31db9f72012-07-25 23:19:24 +0200290static void fs_path_reset(struct fs_path *p)
291{
292 if (p->reversed) {
293 p->start = p->buf + p->buf_len - 1;
294 p->end = p->start;
295 *p->start = 0;
296 } else {
297 p->start = p->buf;
298 p->end = p->start;
299 *p->start = 0;
300 }
301}
302
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000303static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200304{
305 struct fs_path *p;
306
307 p = kmalloc(sizeof(*p), GFP_NOFS);
308 if (!p)
309 return NULL;
310 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200311 p->buf = p->inline_buf;
312 p->buf_len = FS_PATH_INLINE_SIZE;
313 fs_path_reset(p);
314 return p;
315}
316
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000317static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200318{
319 struct fs_path *p;
320
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000321 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200322 if (!p)
323 return NULL;
324 p->reversed = 1;
325 fs_path_reset(p);
326 return p;
327}
328
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000329static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200330{
331 if (!p)
332 return;
David Sterbaace01052014-02-05 16:17:34 +0100333 if (p->buf != p->inline_buf)
334 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200335 kfree(p);
336}
337
338static int fs_path_len(struct fs_path *p)
339{
340 return p->end - p->start;
341}
342
343static int fs_path_ensure_buf(struct fs_path *p, int len)
344{
345 char *tmp_buf;
346 int path_len;
347 int old_buf_len;
348
349 len++;
350
351 if (p->buf_len >= len)
352 return 0;
353
Chris Masoncfd4a532014-04-26 05:02:03 -0700354 if (len > PATH_MAX) {
355 WARN_ON(1);
356 return -ENOMEM;
357 }
358
David Sterba1b2782c2014-02-25 19:32:59 +0100359 path_len = p->end - p->start;
360 old_buf_len = p->buf_len;
361
David Sterbaace01052014-02-05 16:17:34 +0100362 /*
363 * First time the inline_buf does not suffice
364 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100365 if (p->buf == p->inline_buf) {
David Sterba9c9ca002014-02-25 19:33:08 +0100366 tmp_buf = kmalloc(len, GFP_NOFS);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100367 if (tmp_buf)
368 memcpy(tmp_buf, p->buf, old_buf_len);
369 } else {
David Sterba9c9ca002014-02-25 19:33:08 +0100370 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100371 }
David Sterba9c9ca002014-02-25 19:33:08 +0100372 if (!tmp_buf)
373 return -ENOMEM;
374 p->buf = tmp_buf;
375 /*
376 * The real size of the buffer is bigger, this will let the fast path
377 * happen most of the time
378 */
379 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100380
Alexander Block31db9f72012-07-25 23:19:24 +0200381 if (p->reversed) {
382 tmp_buf = p->buf + old_buf_len - path_len - 1;
383 p->end = p->buf + p->buf_len - 1;
384 p->start = p->end - path_len;
385 memmove(p->start, tmp_buf, path_len + 1);
386 } else {
387 p->start = p->buf;
388 p->end = p->start + path_len;
389 }
390 return 0;
391}
392
David Sterbab23ab572014-02-03 19:23:19 +0100393static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
394 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200395{
396 int ret;
397 int new_len;
398
399 new_len = p->end - p->start + name_len;
400 if (p->start != p->end)
401 new_len++;
402 ret = fs_path_ensure_buf(p, new_len);
403 if (ret < 0)
404 goto out;
405
406 if (p->reversed) {
407 if (p->start != p->end)
408 *--p->start = '/';
409 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100410 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200411 } else {
412 if (p->start != p->end)
413 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100414 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200415 p->end += name_len;
416 *p->end = 0;
417 }
418
419out:
420 return ret;
421}
422
423static int fs_path_add(struct fs_path *p, const char *name, int name_len)
424{
425 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100426 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200427
David Sterbab23ab572014-02-03 19:23:19 +0100428 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200429 if (ret < 0)
430 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100431 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200432
433out:
434 return ret;
435}
436
437static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
438{
439 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100440 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200441
David Sterbab23ab572014-02-03 19:23:19 +0100442 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200443 if (ret < 0)
444 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100445 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200446
447out:
448 return ret;
449}
450
451static int fs_path_add_from_extent_buffer(struct fs_path *p,
452 struct extent_buffer *eb,
453 unsigned long off, int len)
454{
455 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100456 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200457
David Sterbab23ab572014-02-03 19:23:19 +0100458 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200459 if (ret < 0)
460 goto out;
461
David Sterbab23ab572014-02-03 19:23:19 +0100462 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200463
464out:
465 return ret;
466}
467
Alexander Block31db9f72012-07-25 23:19:24 +0200468static int fs_path_copy(struct fs_path *p, struct fs_path *from)
469{
470 int ret;
471
472 p->reversed = from->reversed;
473 fs_path_reset(p);
474
475 ret = fs_path_add_path(p, from);
476
477 return ret;
478}
479
480
481static void fs_path_unreverse(struct fs_path *p)
482{
483 char *tmp;
484 int len;
485
486 if (!p->reversed)
487 return;
488
489 tmp = p->start;
490 len = p->end - p->start;
491 p->start = p->buf;
492 p->end = p->start + len;
493 memmove(p->start, tmp, len + 1);
494 p->reversed = 0;
495}
496
497static struct btrfs_path *alloc_path_for_send(void)
498{
499 struct btrfs_path *path;
500
501 path = btrfs_alloc_path();
502 if (!path)
503 return NULL;
504 path->search_commit_root = 1;
505 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400506 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200507 return path;
508}
509
Eric Sandeen48a3b632013-04-25 20:41:01 +0000510static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200511{
512 int ret;
513 mm_segment_t old_fs;
514 u32 pos = 0;
515
516 old_fs = get_fs();
517 set_fs(KERNEL_DS);
518
519 while (pos < len) {
Fabian Frederickd447d0d2014-07-15 21:17:17 +0200520 ret = vfs_write(filp, (__force const char __user *)buf + pos,
521 len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200522 /* TODO handle that correctly */
523 /*if (ret == -ERESTARTSYS) {
524 continue;
525 }*/
526 if (ret < 0)
527 goto out;
528 if (ret == 0) {
529 ret = -EIO;
530 goto out;
531 }
532 pos += ret;
533 }
534
535 ret = 0;
536
537out:
538 set_fs(old_fs);
539 return ret;
540}
541
542static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
543{
544 struct btrfs_tlv_header *hdr;
545 int total_len = sizeof(*hdr) + len;
546 int left = sctx->send_max_size - sctx->send_size;
547
548 if (unlikely(left < total_len))
549 return -EOVERFLOW;
550
551 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
552 hdr->tlv_type = cpu_to_le16(attr);
553 hdr->tlv_len = cpu_to_le16(len);
554 memcpy(hdr + 1, data, len);
555 sctx->send_size += total_len;
556
557 return 0;
558}
559
David Sterba95bc79d2013-12-16 17:34:10 +0100560#define TLV_PUT_DEFINE_INT(bits) \
561 static int tlv_put_u##bits(struct send_ctx *sctx, \
562 u##bits attr, u##bits value) \
563 { \
564 __le##bits __tmp = cpu_to_le##bits(value); \
565 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
566 }
Alexander Block31db9f72012-07-25 23:19:24 +0200567
David Sterba95bc79d2013-12-16 17:34:10 +0100568TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200569
570static int tlv_put_string(struct send_ctx *sctx, u16 attr,
571 const char *str, int len)
572{
573 if (len == -1)
574 len = strlen(str);
575 return tlv_put(sctx, attr, str, len);
576}
577
578static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
579 const u8 *uuid)
580{
581 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
582}
583
Alexander Block31db9f72012-07-25 23:19:24 +0200584static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
585 struct extent_buffer *eb,
586 struct btrfs_timespec *ts)
587{
588 struct btrfs_timespec bts;
589 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
590 return tlv_put(sctx, attr, &bts, sizeof(bts));
591}
592
593
594#define TLV_PUT(sctx, attrtype, attrlen, data) \
595 do { \
596 ret = tlv_put(sctx, attrtype, attrlen, data); \
597 if (ret < 0) \
598 goto tlv_put_failure; \
599 } while (0)
600
601#define TLV_PUT_INT(sctx, attrtype, bits, value) \
602 do { \
603 ret = tlv_put_u##bits(sctx, attrtype, value); \
604 if (ret < 0) \
605 goto tlv_put_failure; \
606 } while (0)
607
608#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
609#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
610#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
611#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
612#define TLV_PUT_STRING(sctx, attrtype, str, len) \
613 do { \
614 ret = tlv_put_string(sctx, attrtype, str, len); \
615 if (ret < 0) \
616 goto tlv_put_failure; \
617 } while (0)
618#define TLV_PUT_PATH(sctx, attrtype, p) \
619 do { \
620 ret = tlv_put_string(sctx, attrtype, p->start, \
621 p->end - p->start); \
622 if (ret < 0) \
623 goto tlv_put_failure; \
624 } while(0)
625#define TLV_PUT_UUID(sctx, attrtype, uuid) \
626 do { \
627 ret = tlv_put_uuid(sctx, attrtype, uuid); \
628 if (ret < 0) \
629 goto tlv_put_failure; \
630 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200631#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
632 do { \
633 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
634 if (ret < 0) \
635 goto tlv_put_failure; \
636 } while (0)
637
638static int send_header(struct send_ctx *sctx)
639{
640 struct btrfs_stream_header hdr;
641
642 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
643 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
644
Anand Jain1bcea352012-09-14 00:04:21 -0600645 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
646 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200647}
648
649/*
650 * For each command/item we want to send to userspace, we call this function.
651 */
652static int begin_cmd(struct send_ctx *sctx, int cmd)
653{
654 struct btrfs_cmd_header *hdr;
655
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530656 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200657 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200658
659 BUG_ON(sctx->send_size);
660
661 sctx->send_size += sizeof(*hdr);
662 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
663 hdr->cmd = cpu_to_le16(cmd);
664
665 return 0;
666}
667
668static int send_cmd(struct send_ctx *sctx)
669{
670 int ret;
671 struct btrfs_cmd_header *hdr;
672 u32 crc;
673
674 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
675 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
676 hdr->crc = 0;
677
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000678 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200679 hdr->crc = cpu_to_le32(crc);
680
Anand Jain1bcea352012-09-14 00:04:21 -0600681 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
682 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200683
684 sctx->total_send_size += sctx->send_size;
685 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
686 sctx->send_size = 0;
687
688 return ret;
689}
690
691/*
692 * Sends a move instruction to user space
693 */
694static int send_rename(struct send_ctx *sctx,
695 struct fs_path *from, struct fs_path *to)
696{
697 int ret;
698
699verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
700
701 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
702 if (ret < 0)
703 goto out;
704
705 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
706 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
707
708 ret = send_cmd(sctx);
709
710tlv_put_failure:
711out:
712 return ret;
713}
714
715/*
716 * Sends a link instruction to user space
717 */
718static int send_link(struct send_ctx *sctx,
719 struct fs_path *path, struct fs_path *lnk)
720{
721 int ret;
722
723verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
724
725 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
726 if (ret < 0)
727 goto out;
728
729 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
730 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
731
732 ret = send_cmd(sctx);
733
734tlv_put_failure:
735out:
736 return ret;
737}
738
739/*
740 * Sends an unlink instruction to user space
741 */
742static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
743{
744 int ret;
745
746verbose_printk("btrfs: send_unlink %s\n", path->start);
747
748 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
749 if (ret < 0)
750 goto out;
751
752 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
753
754 ret = send_cmd(sctx);
755
756tlv_put_failure:
757out:
758 return ret;
759}
760
761/*
762 * Sends a rmdir instruction to user space
763 */
764static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
765{
766 int ret;
767
768verbose_printk("btrfs: send_rmdir %s\n", path->start);
769
770 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
771 if (ret < 0)
772 goto out;
773
774 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
775
776 ret = send_cmd(sctx);
777
778tlv_put_failure:
779out:
780 return ret;
781}
782
783/*
784 * Helper function to retrieve some fields from an inode item.
785 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400786static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
787 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
788 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200789{
790 int ret;
791 struct btrfs_inode_item *ii;
792 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200793
794 key.objectid = ino;
795 key.type = BTRFS_INODE_ITEM_KEY;
796 key.offset = 0;
797 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200798 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400799 if (ret > 0)
800 ret = -ENOENT;
801 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200802 }
803
804 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
805 struct btrfs_inode_item);
806 if (size)
807 *size = btrfs_inode_size(path->nodes[0], ii);
808 if (gen)
809 *gen = btrfs_inode_generation(path->nodes[0], ii);
810 if (mode)
811 *mode = btrfs_inode_mode(path->nodes[0], ii);
812 if (uid)
813 *uid = btrfs_inode_uid(path->nodes[0], ii);
814 if (gid)
815 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200816 if (rdev)
817 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200818
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400819 return ret;
820}
821
822static int get_inode_info(struct btrfs_root *root,
823 u64 ino, u64 *size, u64 *gen,
824 u64 *mode, u64 *uid, u64 *gid,
825 u64 *rdev)
826{
827 struct btrfs_path *path;
828 int ret;
829
830 path = alloc_path_for_send();
831 if (!path)
832 return -ENOMEM;
833 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
834 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200835 btrfs_free_path(path);
836 return ret;
837}
838
839typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
840 struct fs_path *p,
841 void *ctx);
842
843/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000844 * Helper function to iterate the entries in ONE btrfs_inode_ref or
845 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200846 * The iterate callback may return a non zero value to stop iteration. This can
847 * be a negative value for error codes or 1 to simply stop it.
848 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000849 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200850 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000851static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200852 struct btrfs_key *found_key, int resolve,
853 iterate_inode_ref_t iterate, void *ctx)
854{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000855 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200856 struct btrfs_item *item;
857 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000858 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200859 struct btrfs_path *tmp_path;
860 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000861 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200862 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000863 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200864 u32 name_len;
865 char *start;
866 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000867 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200868 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000869 u64 dir;
870 unsigned long name_off;
871 unsigned long elem_size;
872 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200873
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000874 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200875 if (!p)
876 return -ENOMEM;
877
878 tmp_path = alloc_path_for_send();
879 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000880 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200881 return -ENOMEM;
882 }
883
Alexander Block31db9f72012-07-25 23:19:24 +0200884
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000885 if (found_key->type == BTRFS_INODE_REF_KEY) {
886 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
887 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100888 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000889 total = btrfs_item_size(eb, item);
890 elem_size = sizeof(*iref);
891 } else {
892 ptr = btrfs_item_ptr_offset(eb, slot);
893 total = btrfs_item_size_nr(eb, slot);
894 elem_size = sizeof(*extref);
895 }
896
Alexander Block31db9f72012-07-25 23:19:24 +0200897 while (cur < total) {
898 fs_path_reset(p);
899
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000900 if (found_key->type == BTRFS_INODE_REF_KEY) {
901 iref = (struct btrfs_inode_ref *)(ptr + cur);
902 name_len = btrfs_inode_ref_name_len(eb, iref);
903 name_off = (unsigned long)(iref + 1);
904 index = btrfs_inode_ref_index(eb, iref);
905 dir = found_key->offset;
906 } else {
907 extref = (struct btrfs_inode_extref *)(ptr + cur);
908 name_len = btrfs_inode_extref_name_len(eb, extref);
909 name_off = (unsigned long)&extref->name;
910 index = btrfs_inode_extref_index(eb, extref);
911 dir = btrfs_inode_extref_parent(eb, extref);
912 }
913
Alexander Block31db9f72012-07-25 23:19:24 +0200914 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000915 start = btrfs_ref_to_path(root, tmp_path, name_len,
916 name_off, eb, dir,
917 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200918 if (IS_ERR(start)) {
919 ret = PTR_ERR(start);
920 goto out;
921 }
922 if (start < p->buf) {
923 /* overflow , try again with larger buffer */
924 ret = fs_path_ensure_buf(p,
925 p->buf_len + p->buf - start);
926 if (ret < 0)
927 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000928 start = btrfs_ref_to_path(root, tmp_path,
929 name_len, name_off,
930 eb, dir,
931 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200932 if (IS_ERR(start)) {
933 ret = PTR_ERR(start);
934 goto out;
935 }
936 BUG_ON(start < p->buf);
937 }
938 p->start = start;
939 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000940 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
941 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200942 if (ret < 0)
943 goto out;
944 }
945
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000946 cur += elem_size + name_len;
947 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200948 if (ret)
949 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200950 num++;
951 }
952
953out:
954 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000955 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200956 return ret;
957}
958
959typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
960 const char *name, int name_len,
961 const char *data, int data_len,
962 u8 type, void *ctx);
963
964/*
965 * Helper function to iterate the entries in ONE btrfs_dir_item.
966 * The iterate callback may return a non zero value to stop iteration. This can
967 * be a negative value for error codes or 1 to simply stop it.
968 *
969 * path must point to the dir item when called.
970 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000971static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200972 struct btrfs_key *found_key,
973 iterate_dir_item_t iterate, void *ctx)
974{
975 int ret = 0;
976 struct extent_buffer *eb;
977 struct btrfs_item *item;
978 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200979 struct btrfs_key di_key;
980 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +0100981 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +0200982 u32 name_len;
983 u32 data_len;
984 u32 cur;
985 u32 len;
986 u32 total;
987 int slot;
988 int num;
989 u8 type;
990
Filipe Manana4395e0c2014-08-20 10:45:45 +0100991 /*
992 * Start with a small buffer (1 page). If later we end up needing more
993 * space, which can happen for xattrs on a fs with a leaf size greater
994 * then the page size, attempt to increase the buffer. Typically xattr
995 * values are small.
996 */
997 buf_len = PATH_MAX;
Alexander Block31db9f72012-07-25 23:19:24 +0200998 buf = kmalloc(buf_len, GFP_NOFS);
999 if (!buf) {
1000 ret = -ENOMEM;
1001 goto out;
1002 }
1003
Alexander Block31db9f72012-07-25 23:19:24 +02001004 eb = path->nodes[0];
1005 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001006 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001007 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1008 cur = 0;
1009 len = 0;
1010 total = btrfs_item_size(eb, item);
1011
1012 num = 0;
1013 while (cur < total) {
1014 name_len = btrfs_dir_name_len(eb, di);
1015 data_len = btrfs_dir_data_len(eb, di);
1016 type = btrfs_dir_type(eb, di);
1017 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1018
Filipe Manana7e3ae332014-05-23 20:15:16 +01001019 if (type == BTRFS_FT_XATTR) {
1020 if (name_len > XATTR_NAME_MAX) {
1021 ret = -ENAMETOOLONG;
1022 goto out;
1023 }
Filipe Manana4395e0c2014-08-20 10:45:45 +01001024 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001025 ret = -E2BIG;
1026 goto out;
1027 }
1028 } else {
1029 /*
1030 * Path too long
1031 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001032 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001033 ret = -ENAMETOOLONG;
1034 goto out;
1035 }
Alexander Block31db9f72012-07-25 23:19:24 +02001036 }
1037
Filipe Manana4395e0c2014-08-20 10:45:45 +01001038 if (name_len + data_len > buf_len) {
1039 buf_len = name_len + data_len;
1040 if (is_vmalloc_addr(buf)) {
1041 vfree(buf);
1042 buf = NULL;
1043 } else {
1044 char *tmp = krealloc(buf, buf_len,
1045 GFP_NOFS | __GFP_NOWARN);
1046
1047 if (!tmp)
1048 kfree(buf);
1049 buf = tmp;
1050 }
1051 if (!buf) {
1052 buf = vmalloc(buf_len);
1053 if (!buf) {
1054 ret = -ENOMEM;
1055 goto out;
1056 }
1057 }
1058 }
1059
Alexander Block31db9f72012-07-25 23:19:24 +02001060 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1061 name_len + data_len);
1062
1063 len = sizeof(*di) + name_len + data_len;
1064 di = (struct btrfs_dir_item *)((char *)di + len);
1065 cur += len;
1066
1067 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1068 data_len, type, ctx);
1069 if (ret < 0)
1070 goto out;
1071 if (ret) {
1072 ret = 0;
1073 goto out;
1074 }
1075
1076 num++;
1077 }
1078
1079out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001080 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001081 return ret;
1082}
1083
1084static int __copy_first_ref(int num, u64 dir, int index,
1085 struct fs_path *p, void *ctx)
1086{
1087 int ret;
1088 struct fs_path *pt = ctx;
1089
1090 ret = fs_path_copy(pt, p);
1091 if (ret < 0)
1092 return ret;
1093
1094 /* we want the first only */
1095 return 1;
1096}
1097
1098/*
1099 * Retrieve the first path of an inode. If an inode has more then one
1100 * ref/hardlink, this is ignored.
1101 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001102static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001103 u64 ino, struct fs_path *path)
1104{
1105 int ret;
1106 struct btrfs_key key, found_key;
1107 struct btrfs_path *p;
1108
1109 p = alloc_path_for_send();
1110 if (!p)
1111 return -ENOMEM;
1112
1113 fs_path_reset(path);
1114
1115 key.objectid = ino;
1116 key.type = BTRFS_INODE_REF_KEY;
1117 key.offset = 0;
1118
1119 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1120 if (ret < 0)
1121 goto out;
1122 if (ret) {
1123 ret = 1;
1124 goto out;
1125 }
1126 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1127 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001128 (found_key.type != BTRFS_INODE_REF_KEY &&
1129 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001130 ret = -ENOENT;
1131 goto out;
1132 }
1133
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001134 ret = iterate_inode_ref(root, p, &found_key, 1,
1135 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001136 if (ret < 0)
1137 goto out;
1138 ret = 0;
1139
1140out:
1141 btrfs_free_path(p);
1142 return ret;
1143}
1144
1145struct backref_ctx {
1146 struct send_ctx *sctx;
1147
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001148 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001149 /* number of total found references */
1150 u64 found;
1151
1152 /*
1153 * used for clones found in send_root. clones found behind cur_objectid
1154 * and cur_offset are not considered as allowed clones.
1155 */
1156 u64 cur_objectid;
1157 u64 cur_offset;
1158
1159 /* may be truncated in case it's the last extent in a file */
1160 u64 extent_len;
1161
Filipe Manana619d8c42015-05-03 01:56:00 +01001162 /* data offset in the file extent item */
1163 u64 data_offset;
1164
Alexander Block31db9f72012-07-25 23:19:24 +02001165 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001166 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001167};
1168
1169static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1170{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001171 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001172 struct clone_root *cr = (struct clone_root *)elt;
1173
1174 if (root < cr->root->objectid)
1175 return -1;
1176 if (root > cr->root->objectid)
1177 return 1;
1178 return 0;
1179}
1180
1181static int __clone_root_cmp_sort(const void *e1, const void *e2)
1182{
1183 struct clone_root *cr1 = (struct clone_root *)e1;
1184 struct clone_root *cr2 = (struct clone_root *)e2;
1185
1186 if (cr1->root->objectid < cr2->root->objectid)
1187 return -1;
1188 if (cr1->root->objectid > cr2->root->objectid)
1189 return 1;
1190 return 0;
1191}
1192
1193/*
1194 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001195 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001196 */
1197static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1198{
1199 struct backref_ctx *bctx = ctx_;
1200 struct clone_root *found;
1201 int ret;
1202 u64 i_size;
1203
1204 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001205 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001206 bctx->sctx->clone_roots_cnt,
1207 sizeof(struct clone_root),
1208 __clone_root_cmp_bsearch);
1209 if (!found)
1210 return 0;
1211
1212 if (found->root == bctx->sctx->send_root &&
1213 ino == bctx->cur_objectid &&
1214 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001215 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001216 }
1217
1218 /*
Alexander Block766702e2012-07-28 14:11:31 +02001219 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001220 * accept clones from these extents.
1221 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001222 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1223 NULL, NULL, NULL);
1224 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001225 if (ret < 0)
1226 return ret;
1227
Filipe Manana619d8c42015-05-03 01:56:00 +01001228 if (offset + bctx->data_offset + bctx->extent_len > i_size)
Alexander Block31db9f72012-07-25 23:19:24 +02001229 return 0;
1230
1231 /*
1232 * Make sure we don't consider clones from send_root that are
1233 * behind the current inode/offset.
1234 */
1235 if (found->root == bctx->sctx->send_root) {
1236 /*
1237 * TODO for the moment we don't accept clones from the inode
1238 * that is currently send. We may change this when
1239 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1240 * file.
1241 */
1242 if (ino >= bctx->cur_objectid)
1243 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001244#if 0
1245 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001246 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001247 if (offset + bctx->extent_len > bctx->cur_offset)
1248 return 0;
1249#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001250 }
1251
1252 bctx->found++;
1253 found->found_refs++;
1254 if (ino < found->ino) {
1255 found->ino = ino;
1256 found->offset = offset;
1257 } else if (found->ino == ino) {
1258 /*
1259 * same extent found more then once in the same file.
1260 */
1261 if (found->offset > offset + bctx->extent_len)
1262 found->offset = offset;
1263 }
1264
1265 return 0;
1266}
1267
1268/*
Alexander Block766702e2012-07-28 14:11:31 +02001269 * Given an inode, offset and extent item, it finds a good clone for a clone
1270 * instruction. Returns -ENOENT when none could be found. The function makes
1271 * sure that the returned clone is usable at the point where sending is at the
1272 * moment. This means, that no clones are accepted which lie behind the current
1273 * inode+offset.
1274 *
Alexander Block31db9f72012-07-25 23:19:24 +02001275 * path must point to the extent item when called.
1276 */
1277static int find_extent_clone(struct send_ctx *sctx,
1278 struct btrfs_path *path,
1279 u64 ino, u64 data_offset,
1280 u64 ino_size,
1281 struct clone_root **found)
1282{
1283 int ret;
1284 int extent_type;
1285 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001286 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001287 u64 num_bytes;
1288 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001289 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001290 struct btrfs_file_extent_item *fi;
1291 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001292 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001293 struct clone_root *cur_clone_root;
1294 struct btrfs_key found_key;
1295 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001296 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001297 u32 i;
1298
1299 tmp_path = alloc_path_for_send();
1300 if (!tmp_path)
1301 return -ENOMEM;
1302
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001303 /* We only use this path under the commit sem */
1304 tmp_path->need_commit_sem = 0;
1305
Alexander Block35075bb2012-07-28 12:44:34 +02001306 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1307 if (!backref_ctx) {
1308 ret = -ENOMEM;
1309 goto out;
1310 }
1311
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001312 backref_ctx->path = tmp_path;
1313
Alexander Block31db9f72012-07-25 23:19:24 +02001314 if (data_offset >= ino_size) {
1315 /*
1316 * There may be extents that lie behind the file's size.
1317 * I at least had this in combination with snapshotting while
1318 * writing large files.
1319 */
1320 ret = 0;
1321 goto out;
1322 }
1323
1324 fi = btrfs_item_ptr(eb, path->slots[0],
1325 struct btrfs_file_extent_item);
1326 extent_type = btrfs_file_extent_type(eb, fi);
1327 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1328 ret = -ENOENT;
1329 goto out;
1330 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001331 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001332
1333 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001334 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1335 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001336 ret = -ENOENT;
1337 goto out;
1338 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001339 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001340
Josef Bacik9e351cc2014-03-13 15:42:13 -04001341 down_read(&sctx->send_root->fs_info->commit_root_sem);
Liu Bo69917e42012-09-07 20:01:28 -06001342 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1343 &found_key, &flags);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001344 up_read(&sctx->send_root->fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001345 btrfs_release_path(tmp_path);
1346
1347 if (ret < 0)
1348 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001349 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001350 ret = -EIO;
1351 goto out;
1352 }
1353
1354 /*
1355 * Setup the clone roots.
1356 */
1357 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1358 cur_clone_root = sctx->clone_roots + i;
1359 cur_clone_root->ino = (u64)-1;
1360 cur_clone_root->offset = 0;
1361 cur_clone_root->found_refs = 0;
1362 }
1363
Alexander Block35075bb2012-07-28 12:44:34 +02001364 backref_ctx->sctx = sctx;
1365 backref_ctx->found = 0;
1366 backref_ctx->cur_objectid = ino;
1367 backref_ctx->cur_offset = data_offset;
1368 backref_ctx->found_itself = 0;
1369 backref_ctx->extent_len = num_bytes;
Filipe Manana619d8c42015-05-03 01:56:00 +01001370 /*
1371 * For non-compressed extents iterate_extent_inodes() gives us extent
1372 * offsets that already take into account the data offset, but not for
1373 * compressed extents, since the offset is logical and not relative to
1374 * the physical extent locations. We must take this into account to
1375 * avoid sending clone offsets that go beyond the source file's size,
1376 * which would result in the clone ioctl failing with -EINVAL on the
1377 * receiving end.
1378 */
1379 if (compressed == BTRFS_COMPRESS_NONE)
1380 backref_ctx->data_offset = 0;
1381 else
1382 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001383
1384 /*
1385 * The last extent of a file may be too large due to page alignment.
1386 * We need to adjust extent_len in this case so that the checks in
1387 * __iterate_backrefs work.
1388 */
1389 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001390 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001391
1392 /*
1393 * Now collect all backrefs.
1394 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001395 if (compressed == BTRFS_COMPRESS_NONE)
1396 extent_item_pos = logical - found_key.objectid;
1397 else
1398 extent_item_pos = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001399 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1400 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001401 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001402
Alexander Block31db9f72012-07-25 23:19:24 +02001403 if (ret < 0)
1404 goto out;
1405
Alexander Block35075bb2012-07-28 12:44:34 +02001406 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001407 /* found a bug in backref code? */
1408 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001409 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001410 "send_root. inode=%llu, offset=%llu, "
David Sterba351fd352014-05-15 16:48:20 +02001411 "disk_byte=%llu found extent=%llu",
Chris Mason74dd17f2012-08-07 16:25:13 -04001412 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001413 goto out;
1414 }
1415
1416verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1417 "ino=%llu, "
1418 "num_bytes=%llu, logical=%llu\n",
1419 data_offset, ino, num_bytes, logical);
1420
Alexander Block35075bb2012-07-28 12:44:34 +02001421 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001422 verbose_printk("btrfs: no clones found\n");
1423
1424 cur_clone_root = NULL;
1425 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1426 if (sctx->clone_roots[i].found_refs) {
1427 if (!cur_clone_root)
1428 cur_clone_root = sctx->clone_roots + i;
1429 else if (sctx->clone_roots[i].root == sctx->send_root)
1430 /* prefer clones from send_root over others */
1431 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001432 }
1433
1434 }
1435
1436 if (cur_clone_root) {
1437 *found = cur_clone_root;
1438 ret = 0;
1439 } else {
1440 ret = -ENOENT;
1441 }
1442
1443out:
1444 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001445 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001446 return ret;
1447}
1448
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001449static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001450 u64 ino,
1451 struct fs_path *dest)
1452{
1453 int ret;
1454 struct btrfs_path *path;
1455 struct btrfs_key key;
1456 struct btrfs_file_extent_item *ei;
1457 u8 type;
1458 u8 compression;
1459 unsigned long off;
1460 int len;
1461
1462 path = alloc_path_for_send();
1463 if (!path)
1464 return -ENOMEM;
1465
1466 key.objectid = ino;
1467 key.type = BTRFS_EXTENT_DATA_KEY;
1468 key.offset = 0;
1469 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1470 if (ret < 0)
1471 goto out;
1472 BUG_ON(ret);
1473
1474 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1475 struct btrfs_file_extent_item);
1476 type = btrfs_file_extent_type(path->nodes[0], ei);
1477 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1478 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1479 BUG_ON(compression);
1480
1481 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001482 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001483
1484 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001485
1486out:
1487 btrfs_free_path(path);
1488 return ret;
1489}
1490
1491/*
1492 * Helper function to generate a file name that is unique in the root of
1493 * send_root and parent_root. This is used to generate names for orphan inodes.
1494 */
1495static int gen_unique_name(struct send_ctx *sctx,
1496 u64 ino, u64 gen,
1497 struct fs_path *dest)
1498{
1499 int ret = 0;
1500 struct btrfs_path *path;
1501 struct btrfs_dir_item *di;
1502 char tmp[64];
1503 int len;
1504 u64 idx = 0;
1505
1506 path = alloc_path_for_send();
1507 if (!path)
1508 return -ENOMEM;
1509
1510 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001511 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001512 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001513 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001514
1515 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1516 path, BTRFS_FIRST_FREE_OBJECTID,
1517 tmp, strlen(tmp), 0);
1518 btrfs_release_path(path);
1519 if (IS_ERR(di)) {
1520 ret = PTR_ERR(di);
1521 goto out;
1522 }
1523 if (di) {
1524 /* not unique, try again */
1525 idx++;
1526 continue;
1527 }
1528
1529 if (!sctx->parent_root) {
1530 /* unique */
1531 ret = 0;
1532 break;
1533 }
1534
1535 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1536 path, BTRFS_FIRST_FREE_OBJECTID,
1537 tmp, strlen(tmp), 0);
1538 btrfs_release_path(path);
1539 if (IS_ERR(di)) {
1540 ret = PTR_ERR(di);
1541 goto out;
1542 }
1543 if (di) {
1544 /* not unique, try again */
1545 idx++;
1546 continue;
1547 }
1548 /* unique */
1549 break;
1550 }
1551
1552 ret = fs_path_add(dest, tmp, strlen(tmp));
1553
1554out:
1555 btrfs_free_path(path);
1556 return ret;
1557}
1558
1559enum inode_state {
1560 inode_state_no_change,
1561 inode_state_will_create,
1562 inode_state_did_create,
1563 inode_state_will_delete,
1564 inode_state_did_delete,
1565};
1566
1567static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1568{
1569 int ret;
1570 int left_ret;
1571 int right_ret;
1572 u64 left_gen;
1573 u64 right_gen;
1574
1575 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001576 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001577 if (ret < 0 && ret != -ENOENT)
1578 goto out;
1579 left_ret = ret;
1580
1581 if (!sctx->parent_root) {
1582 right_ret = -ENOENT;
1583 } else {
1584 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001585 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001586 if (ret < 0 && ret != -ENOENT)
1587 goto out;
1588 right_ret = ret;
1589 }
1590
1591 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001592 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001593 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001594 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001595 if (ino < sctx->send_progress)
1596 ret = inode_state_did_create;
1597 else
1598 ret = inode_state_will_create;
1599 } else if (right_gen == gen) {
1600 if (ino < sctx->send_progress)
1601 ret = inode_state_did_delete;
1602 else
1603 ret = inode_state_will_delete;
1604 } else {
1605 ret = -ENOENT;
1606 }
1607 } else if (!left_ret) {
1608 if (left_gen == gen) {
1609 if (ino < sctx->send_progress)
1610 ret = inode_state_did_create;
1611 else
1612 ret = inode_state_will_create;
1613 } else {
1614 ret = -ENOENT;
1615 }
1616 } else if (!right_ret) {
1617 if (right_gen == gen) {
1618 if (ino < sctx->send_progress)
1619 ret = inode_state_did_delete;
1620 else
1621 ret = inode_state_will_delete;
1622 } else {
1623 ret = -ENOENT;
1624 }
1625 } else {
1626 ret = -ENOENT;
1627 }
1628
1629out:
1630 return ret;
1631}
1632
1633static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1634{
1635 int ret;
1636
1637 ret = get_cur_inode_state(sctx, ino, gen);
1638 if (ret < 0)
1639 goto out;
1640
1641 if (ret == inode_state_no_change ||
1642 ret == inode_state_did_create ||
1643 ret == inode_state_will_delete)
1644 ret = 1;
1645 else
1646 ret = 0;
1647
1648out:
1649 return ret;
1650}
1651
1652/*
1653 * Helper function to lookup a dir item in a dir.
1654 */
1655static int lookup_dir_item_inode(struct btrfs_root *root,
1656 u64 dir, const char *name, int name_len,
1657 u64 *found_inode,
1658 u8 *found_type)
1659{
1660 int ret = 0;
1661 struct btrfs_dir_item *di;
1662 struct btrfs_key key;
1663 struct btrfs_path *path;
1664
1665 path = alloc_path_for_send();
1666 if (!path)
1667 return -ENOMEM;
1668
1669 di = btrfs_lookup_dir_item(NULL, root, path,
1670 dir, name, name_len, 0);
1671 if (!di) {
1672 ret = -ENOENT;
1673 goto out;
1674 }
1675 if (IS_ERR(di)) {
1676 ret = PTR_ERR(di);
1677 goto out;
1678 }
1679 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001680 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1681 ret = -ENOENT;
1682 goto out;
1683 }
Alexander Block31db9f72012-07-25 23:19:24 +02001684 *found_inode = key.objectid;
1685 *found_type = btrfs_dir_type(path->nodes[0], di);
1686
1687out:
1688 btrfs_free_path(path);
1689 return ret;
1690}
1691
Alexander Block766702e2012-07-28 14:11:31 +02001692/*
1693 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1694 * generation of the parent dir and the name of the dir entry.
1695 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001696static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001697 u64 *dir, u64 *dir_gen, struct fs_path *name)
1698{
1699 int ret;
1700 struct btrfs_key key;
1701 struct btrfs_key found_key;
1702 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001703 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001704 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001705
1706 path = alloc_path_for_send();
1707 if (!path)
1708 return -ENOMEM;
1709
1710 key.objectid = ino;
1711 key.type = BTRFS_INODE_REF_KEY;
1712 key.offset = 0;
1713
1714 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1715 if (ret < 0)
1716 goto out;
1717 if (!ret)
1718 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1719 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001720 if (ret || found_key.objectid != ino ||
1721 (found_key.type != BTRFS_INODE_REF_KEY &&
1722 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001723 ret = -ENOENT;
1724 goto out;
1725 }
1726
Filipe Manana51a60252014-05-13 22:01:02 +01001727 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001728 struct btrfs_inode_ref *iref;
1729 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1730 struct btrfs_inode_ref);
1731 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1732 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1733 (unsigned long)(iref + 1),
1734 len);
1735 parent_dir = found_key.offset;
1736 } else {
1737 struct btrfs_inode_extref *extref;
1738 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1739 struct btrfs_inode_extref);
1740 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1741 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1742 (unsigned long)&extref->name, len);
1743 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1744 }
Alexander Block31db9f72012-07-25 23:19:24 +02001745 if (ret < 0)
1746 goto out;
1747 btrfs_release_path(path);
1748
Filipe Mananab46ab972014-03-21 12:46:54 +00001749 if (dir_gen) {
1750 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1751 NULL, NULL, NULL);
1752 if (ret < 0)
1753 goto out;
1754 }
Alexander Block31db9f72012-07-25 23:19:24 +02001755
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001756 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001757
1758out:
1759 btrfs_free_path(path);
1760 return ret;
1761}
1762
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001763static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001764 u64 ino, u64 dir,
1765 const char *name, int name_len)
1766{
1767 int ret;
1768 struct fs_path *tmp_name;
1769 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001770
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001771 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001772 if (!tmp_name)
1773 return -ENOMEM;
1774
Filipe Mananab46ab972014-03-21 12:46:54 +00001775 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001776 if (ret < 0)
1777 goto out;
1778
Alexander Blockb9291af2012-07-28 11:07:18 +02001779 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001780 ret = 0;
1781 goto out;
1782 }
1783
Alexander Blocke938c8a2012-07-28 16:33:49 +02001784 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001785
1786out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001787 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001788 return ret;
1789}
1790
Alexander Block766702e2012-07-28 14:11:31 +02001791/*
1792 * Used by process_recorded_refs to determine if a new ref would overwrite an
1793 * already existing ref. In case it detects an overwrite, it returns the
1794 * inode/gen in who_ino/who_gen.
1795 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1796 * to make sure later references to the overwritten inode are possible.
1797 * Orphanizing is however only required for the first ref of an inode.
1798 * process_recorded_refs does an additional is_first_ref check to see if
1799 * orphanizing is really required.
1800 */
Alexander Block31db9f72012-07-25 23:19:24 +02001801static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1802 const char *name, int name_len,
1803 u64 *who_ino, u64 *who_gen)
1804{
1805 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001806 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001807 u64 other_inode = 0;
1808 u8 other_type = 0;
1809
1810 if (!sctx->parent_root)
1811 goto out;
1812
1813 ret = is_inode_existent(sctx, dir, dir_gen);
1814 if (ret <= 0)
1815 goto out;
1816
Josef Bacikebdad912013-08-06 16:47:48 -04001817 /*
1818 * If we have a parent root we need to verify that the parent dir was
1819 * not delted and then re-created, if it was then we have no overwrite
1820 * and we can just unlink this entry.
1821 */
1822 if (sctx->parent_root) {
1823 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1824 NULL, NULL, NULL);
1825 if (ret < 0 && ret != -ENOENT)
1826 goto out;
1827 if (ret) {
1828 ret = 0;
1829 goto out;
1830 }
1831 if (gen != dir_gen)
1832 goto out;
1833 }
1834
Alexander Block31db9f72012-07-25 23:19:24 +02001835 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1836 &other_inode, &other_type);
1837 if (ret < 0 && ret != -ENOENT)
1838 goto out;
1839 if (ret) {
1840 ret = 0;
1841 goto out;
1842 }
1843
Alexander Block766702e2012-07-28 14:11:31 +02001844 /*
1845 * Check if the overwritten ref was already processed. If yes, the ref
1846 * was already unlinked/moved, so we can safely assume that we will not
1847 * overwrite anything at this point in time.
1848 */
Alexander Block31db9f72012-07-25 23:19:24 +02001849 if (other_inode > sctx->send_progress) {
1850 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001851 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001852 if (ret < 0)
1853 goto out;
1854
1855 ret = 1;
1856 *who_ino = other_inode;
1857 } else {
1858 ret = 0;
1859 }
1860
1861out:
1862 return ret;
1863}
1864
Alexander Block766702e2012-07-28 14:11:31 +02001865/*
1866 * Checks if the ref was overwritten by an already processed inode. This is
1867 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1868 * thus the orphan name needs be used.
1869 * process_recorded_refs also uses it to avoid unlinking of refs that were
1870 * overwritten.
1871 */
Alexander Block31db9f72012-07-25 23:19:24 +02001872static int did_overwrite_ref(struct send_ctx *sctx,
1873 u64 dir, u64 dir_gen,
1874 u64 ino, u64 ino_gen,
1875 const char *name, int name_len)
1876{
1877 int ret = 0;
1878 u64 gen;
1879 u64 ow_inode;
1880 u8 other_type;
1881
1882 if (!sctx->parent_root)
1883 goto out;
1884
1885 ret = is_inode_existent(sctx, dir, dir_gen);
1886 if (ret <= 0)
1887 goto out;
1888
1889 /* check if the ref was overwritten by another ref */
1890 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1891 &ow_inode, &other_type);
1892 if (ret < 0 && ret != -ENOENT)
1893 goto out;
1894 if (ret) {
1895 /* was never and will never be overwritten */
1896 ret = 0;
1897 goto out;
1898 }
1899
1900 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001901 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001902 if (ret < 0)
1903 goto out;
1904
1905 if (ow_inode == ino && gen == ino_gen) {
1906 ret = 0;
1907 goto out;
1908 }
1909
Filipe Manana8b191a62015-04-09 14:09:14 +01001910 /*
1911 * We know that it is or will be overwritten. Check this now.
1912 * The current inode being processed might have been the one that caused
Filipe Mananab786f162015-09-26 15:30:23 +01001913 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1914 * the current inode being processed.
Filipe Manana8b191a62015-04-09 14:09:14 +01001915 */
Filipe Mananab786f162015-09-26 15:30:23 +01001916 if ((ow_inode < sctx->send_progress) ||
1917 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1918 gen == sctx->cur_inode_gen))
Alexander Block31db9f72012-07-25 23:19:24 +02001919 ret = 1;
1920 else
1921 ret = 0;
1922
1923out:
1924 return ret;
1925}
1926
Alexander Block766702e2012-07-28 14:11:31 +02001927/*
1928 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1929 * that got overwritten. This is used by process_recorded_refs to determine
1930 * if it has to use the path as returned by get_cur_path or the orphan name.
1931 */
Alexander Block31db9f72012-07-25 23:19:24 +02001932static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1933{
1934 int ret = 0;
1935 struct fs_path *name = NULL;
1936 u64 dir;
1937 u64 dir_gen;
1938
1939 if (!sctx->parent_root)
1940 goto out;
1941
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001942 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001943 if (!name)
1944 return -ENOMEM;
1945
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001946 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001947 if (ret < 0)
1948 goto out;
1949
1950 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1951 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001952
1953out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001954 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001955 return ret;
1956}
1957
Alexander Block766702e2012-07-28 14:11:31 +02001958/*
1959 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1960 * so we need to do some special handling in case we have clashes. This function
1961 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001962 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001963 */
Alexander Block31db9f72012-07-25 23:19:24 +02001964static int name_cache_insert(struct send_ctx *sctx,
1965 struct name_cache_entry *nce)
1966{
1967 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001968 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001969
Alexander Block7e0926f2012-07-28 14:20:58 +02001970 nce_head = radix_tree_lookup(&sctx->name_cache,
1971 (unsigned long)nce->ino);
1972 if (!nce_head) {
1973 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001974 if (!nce_head) {
1975 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001976 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001977 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001978 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001979
Alexander Block7e0926f2012-07-28 14:20:58 +02001980 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001981 if (ret < 0) {
1982 kfree(nce_head);
1983 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001984 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001985 }
Alexander Block31db9f72012-07-25 23:19:24 +02001986 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001987 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001988 list_add_tail(&nce->list, &sctx->name_cache_list);
1989 sctx->name_cache_size++;
1990
1991 return ret;
1992}
1993
1994static void name_cache_delete(struct send_ctx *sctx,
1995 struct name_cache_entry *nce)
1996{
Alexander Block7e0926f2012-07-28 14:20:58 +02001997 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001998
Alexander Block7e0926f2012-07-28 14:20:58 +02001999 nce_head = radix_tree_lookup(&sctx->name_cache,
2000 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01002001 if (!nce_head) {
2002 btrfs_err(sctx->send_root->fs_info,
2003 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2004 nce->ino, sctx->name_cache_size);
2005 }
Alexander Block31db9f72012-07-25 23:19:24 +02002006
Alexander Block7e0926f2012-07-28 14:20:58 +02002007 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02002008 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002009 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02002010
David Sterba57fb8912014-02-03 19:24:40 +01002011 /*
2012 * We may not get to the final release of nce_head if the lookup fails
2013 */
2014 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002015 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2016 kfree(nce_head);
2017 }
Alexander Block31db9f72012-07-25 23:19:24 +02002018}
2019
2020static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2021 u64 ino, u64 gen)
2022{
Alexander Block7e0926f2012-07-28 14:20:58 +02002023 struct list_head *nce_head;
2024 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002025
Alexander Block7e0926f2012-07-28 14:20:58 +02002026 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2027 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002028 return NULL;
2029
Alexander Block7e0926f2012-07-28 14:20:58 +02002030 list_for_each_entry(cur, nce_head, radix_list) {
2031 if (cur->ino == ino && cur->gen == gen)
2032 return cur;
2033 }
Alexander Block31db9f72012-07-25 23:19:24 +02002034 return NULL;
2035}
2036
Alexander Block766702e2012-07-28 14:11:31 +02002037/*
2038 * Removes the entry from the list and adds it back to the end. This marks the
2039 * entry as recently used so that name_cache_clean_unused does not remove it.
2040 */
Alexander Block31db9f72012-07-25 23:19:24 +02002041static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2042{
2043 list_del(&nce->list);
2044 list_add_tail(&nce->list, &sctx->name_cache_list);
2045}
2046
Alexander Block766702e2012-07-28 14:11:31 +02002047/*
2048 * Remove some entries from the beginning of name_cache_list.
2049 */
Alexander Block31db9f72012-07-25 23:19:24 +02002050static void name_cache_clean_unused(struct send_ctx *sctx)
2051{
2052 struct name_cache_entry *nce;
2053
2054 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2055 return;
2056
2057 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2058 nce = list_entry(sctx->name_cache_list.next,
2059 struct name_cache_entry, list);
2060 name_cache_delete(sctx, nce);
2061 kfree(nce);
2062 }
2063}
2064
2065static void name_cache_free(struct send_ctx *sctx)
2066{
2067 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002068
Alexander Blocke938c8a2012-07-28 16:33:49 +02002069 while (!list_empty(&sctx->name_cache_list)) {
2070 nce = list_entry(sctx->name_cache_list.next,
2071 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002072 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002073 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002074 }
2075}
2076
Alexander Block766702e2012-07-28 14:11:31 +02002077/*
2078 * Used by get_cur_path for each ref up to the root.
2079 * Returns 0 if it succeeded.
2080 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2081 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2082 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2083 * Returns <0 in case of error.
2084 */
Alexander Block31db9f72012-07-25 23:19:24 +02002085static int __get_cur_name_and_parent(struct send_ctx *sctx,
2086 u64 ino, u64 gen,
2087 u64 *parent_ino,
2088 u64 *parent_gen,
2089 struct fs_path *dest)
2090{
2091 int ret;
2092 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002093 struct name_cache_entry *nce = NULL;
2094
Alexander Block766702e2012-07-28 14:11:31 +02002095 /*
2096 * First check if we already did a call to this function with the same
2097 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2098 * return the cached result.
2099 */
Alexander Block31db9f72012-07-25 23:19:24 +02002100 nce = name_cache_search(sctx, ino, gen);
2101 if (nce) {
2102 if (ino < sctx->send_progress && nce->need_later_update) {
2103 name_cache_delete(sctx, nce);
2104 kfree(nce);
2105 nce = NULL;
2106 } else {
2107 name_cache_used(sctx, nce);
2108 *parent_ino = nce->parent_ino;
2109 *parent_gen = nce->parent_gen;
2110 ret = fs_path_add(dest, nce->name, nce->name_len);
2111 if (ret < 0)
2112 goto out;
2113 ret = nce->ret;
2114 goto out;
2115 }
2116 }
2117
Alexander Block766702e2012-07-28 14:11:31 +02002118 /*
2119 * If the inode is not existent yet, add the orphan name and return 1.
2120 * This should only happen for the parent dir that we determine in
2121 * __record_new_ref
2122 */
Alexander Block31db9f72012-07-25 23:19:24 +02002123 ret = is_inode_existent(sctx, ino, gen);
2124 if (ret < 0)
2125 goto out;
2126
2127 if (!ret) {
2128 ret = gen_unique_name(sctx, ino, gen, dest);
2129 if (ret < 0)
2130 goto out;
2131 ret = 1;
2132 goto out_cache;
2133 }
2134
Alexander Block766702e2012-07-28 14:11:31 +02002135 /*
2136 * Depending on whether the inode was already processed or not, use
2137 * send_root or parent_root for ref lookup.
2138 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002139 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002140 ret = get_first_ref(sctx->send_root, ino,
2141 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002142 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002143 ret = get_first_ref(sctx->parent_root, ino,
2144 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002145 if (ret < 0)
2146 goto out;
2147
Alexander Block766702e2012-07-28 14:11:31 +02002148 /*
2149 * Check if the ref was overwritten by an inode's ref that was processed
2150 * earlier. If yes, treat as orphan and return 1.
2151 */
Alexander Block31db9f72012-07-25 23:19:24 +02002152 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2153 dest->start, dest->end - dest->start);
2154 if (ret < 0)
2155 goto out;
2156 if (ret) {
2157 fs_path_reset(dest);
2158 ret = gen_unique_name(sctx, ino, gen, dest);
2159 if (ret < 0)
2160 goto out;
2161 ret = 1;
2162 }
2163
2164out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002165 /*
2166 * Store the result of the lookup in the name cache.
2167 */
Alexander Block31db9f72012-07-25 23:19:24 +02002168 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2169 if (!nce) {
2170 ret = -ENOMEM;
2171 goto out;
2172 }
2173
2174 nce->ino = ino;
2175 nce->gen = gen;
2176 nce->parent_ino = *parent_ino;
2177 nce->parent_gen = *parent_gen;
2178 nce->name_len = fs_path_len(dest);
2179 nce->ret = ret;
2180 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002181
2182 if (ino < sctx->send_progress)
2183 nce->need_later_update = 0;
2184 else
2185 nce->need_later_update = 1;
2186
2187 nce_ret = name_cache_insert(sctx, nce);
2188 if (nce_ret < 0)
2189 ret = nce_ret;
2190 name_cache_clean_unused(sctx);
2191
2192out:
Alexander Block31db9f72012-07-25 23:19:24 +02002193 return ret;
2194}
2195
2196/*
2197 * Magic happens here. This function returns the first ref to an inode as it
2198 * would look like while receiving the stream at this point in time.
2199 * We walk the path up to the root. For every inode in between, we check if it
2200 * was already processed/sent. If yes, we continue with the parent as found
2201 * in send_root. If not, we continue with the parent as found in parent_root.
2202 * If we encounter an inode that was deleted at this point in time, we use the
2203 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2204 * that were not created yet and overwritten inodes/refs.
2205 *
2206 * When do we have have orphan inodes:
2207 * 1. When an inode is freshly created and thus no valid refs are available yet
2208 * 2. When a directory lost all it's refs (deleted) but still has dir items
2209 * inside which were not processed yet (pending for move/delete). If anyone
2210 * tried to get the path to the dir items, it would get a path inside that
2211 * orphan directory.
2212 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2213 * of an unprocessed inode. If in that case the first ref would be
2214 * overwritten, the overwritten inode gets "orphanized". Later when we
2215 * process this overwritten inode, it is restored at a new place by moving
2216 * the orphan inode.
2217 *
2218 * sctx->send_progress tells this function at which point in time receiving
2219 * would be.
2220 */
2221static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2222 struct fs_path *dest)
2223{
2224 int ret = 0;
2225 struct fs_path *name = NULL;
2226 u64 parent_inode = 0;
2227 u64 parent_gen = 0;
2228 int stop = 0;
2229
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002230 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002231 if (!name) {
2232 ret = -ENOMEM;
2233 goto out;
2234 }
2235
2236 dest->reversed = 1;
2237 fs_path_reset(dest);
2238
2239 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
Filipe Manana8b191a62015-04-09 14:09:14 +01002240 struct waiting_dir_move *wdm;
2241
Alexander Block31db9f72012-07-25 23:19:24 +02002242 fs_path_reset(name);
2243
Filipe Manana9dc44212014-02-19 14:31:44 +00002244 if (is_waiting_for_rm(sctx, ino)) {
2245 ret = gen_unique_name(sctx, ino, gen, name);
2246 if (ret < 0)
2247 goto out;
2248 ret = fs_path_add_path(dest, name);
2249 break;
2250 }
2251
Filipe Manana8b191a62015-04-09 14:09:14 +01002252 wdm = get_waiting_dir_move(sctx, ino);
2253 if (wdm && wdm->orphanized) {
2254 ret = gen_unique_name(sctx, ino, gen, name);
2255 stop = 1;
2256 } else if (wdm) {
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002257 ret = get_first_ref(sctx->parent_root, ino,
2258 &parent_inode, &parent_gen, name);
2259 } else {
2260 ret = __get_cur_name_and_parent(sctx, ino, gen,
2261 &parent_inode,
2262 &parent_gen, name);
2263 if (ret)
2264 stop = 1;
2265 }
2266
Alexander Block31db9f72012-07-25 23:19:24 +02002267 if (ret < 0)
2268 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002269
Alexander Block31db9f72012-07-25 23:19:24 +02002270 ret = fs_path_add_path(dest, name);
2271 if (ret < 0)
2272 goto out;
2273
2274 ino = parent_inode;
2275 gen = parent_gen;
2276 }
2277
2278out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002279 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002280 if (!ret)
2281 fs_path_unreverse(dest);
2282 return ret;
2283}
2284
2285/*
Alexander Block31db9f72012-07-25 23:19:24 +02002286 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2287 */
2288static int send_subvol_begin(struct send_ctx *sctx)
2289{
2290 int ret;
2291 struct btrfs_root *send_root = sctx->send_root;
2292 struct btrfs_root *parent_root = sctx->parent_root;
2293 struct btrfs_path *path;
2294 struct btrfs_key key;
2295 struct btrfs_root_ref *ref;
2296 struct extent_buffer *leaf;
2297 char *name = NULL;
2298 int namelen;
2299
Wang Shilongffcfaf82014-01-15 00:26:43 +08002300 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002301 if (!path)
2302 return -ENOMEM;
2303
2304 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2305 if (!name) {
2306 btrfs_free_path(path);
2307 return -ENOMEM;
2308 }
2309
2310 key.objectid = send_root->objectid;
2311 key.type = BTRFS_ROOT_BACKREF_KEY;
2312 key.offset = 0;
2313
2314 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2315 &key, path, 1, 0);
2316 if (ret < 0)
2317 goto out;
2318 if (ret) {
2319 ret = -ENOENT;
2320 goto out;
2321 }
2322
2323 leaf = path->nodes[0];
2324 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2325 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2326 key.objectid != send_root->objectid) {
2327 ret = -ENOENT;
2328 goto out;
2329 }
2330 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2331 namelen = btrfs_root_ref_name_len(leaf, ref);
2332 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2333 btrfs_release_path(path);
2334
Alexander Block31db9f72012-07-25 23:19:24 +02002335 if (parent_root) {
2336 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2337 if (ret < 0)
2338 goto out;
2339 } else {
2340 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2341 if (ret < 0)
2342 goto out;
2343 }
2344
2345 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2346 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2347 sctx->send_root->root_item.uuid);
2348 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002349 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002350 if (parent_root) {
Josef Bacik37b8d272015-06-04 17:17:25 -04002351 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2352 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2353 parent_root->root_item.received_uuid);
2354 else
2355 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2356 parent_root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02002357 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002358 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002359 }
2360
2361 ret = send_cmd(sctx);
2362
2363tlv_put_failure:
2364out:
2365 btrfs_free_path(path);
2366 kfree(name);
2367 return ret;
2368}
2369
2370static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2371{
2372 int ret = 0;
2373 struct fs_path *p;
2374
2375verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2376
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002377 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002378 if (!p)
2379 return -ENOMEM;
2380
2381 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2382 if (ret < 0)
2383 goto out;
2384
2385 ret = get_cur_path(sctx, ino, gen, p);
2386 if (ret < 0)
2387 goto out;
2388 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2389 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2390
2391 ret = send_cmd(sctx);
2392
2393tlv_put_failure:
2394out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002395 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002396 return ret;
2397}
2398
2399static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2400{
2401 int ret = 0;
2402 struct fs_path *p;
2403
2404verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2405
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002406 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002407 if (!p)
2408 return -ENOMEM;
2409
2410 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2411 if (ret < 0)
2412 goto out;
2413
2414 ret = get_cur_path(sctx, ino, gen, p);
2415 if (ret < 0)
2416 goto out;
2417 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2418 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2419
2420 ret = send_cmd(sctx);
2421
2422tlv_put_failure:
2423out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002424 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002425 return ret;
2426}
2427
2428static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2429{
2430 int ret = 0;
2431 struct fs_path *p;
2432
2433verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2434
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002435 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002436 if (!p)
2437 return -ENOMEM;
2438
2439 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2440 if (ret < 0)
2441 goto out;
2442
2443 ret = get_cur_path(sctx, ino, gen, p);
2444 if (ret < 0)
2445 goto out;
2446 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2447 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2448 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2449
2450 ret = send_cmd(sctx);
2451
2452tlv_put_failure:
2453out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002454 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002455 return ret;
2456}
2457
2458static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2459{
2460 int ret = 0;
2461 struct fs_path *p = NULL;
2462 struct btrfs_inode_item *ii;
2463 struct btrfs_path *path = NULL;
2464 struct extent_buffer *eb;
2465 struct btrfs_key key;
2466 int slot;
2467
2468verbose_printk("btrfs: send_utimes %llu\n", ino);
2469
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002470 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002471 if (!p)
2472 return -ENOMEM;
2473
2474 path = alloc_path_for_send();
2475 if (!path) {
2476 ret = -ENOMEM;
2477 goto out;
2478 }
2479
2480 key.objectid = ino;
2481 key.type = BTRFS_INODE_ITEM_KEY;
2482 key.offset = 0;
2483 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2484 if (ret < 0)
2485 goto out;
2486
2487 eb = path->nodes[0];
2488 slot = path->slots[0];
2489 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2490
2491 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2492 if (ret < 0)
2493 goto out;
2494
2495 ret = get_cur_path(sctx, ino, gen, p);
2496 if (ret < 0)
2497 goto out;
2498 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002499 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2500 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2501 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002502 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002503
2504 ret = send_cmd(sctx);
2505
2506tlv_put_failure:
2507out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002508 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002509 btrfs_free_path(path);
2510 return ret;
2511}
2512
2513/*
2514 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2515 * a valid path yet because we did not process the refs yet. So, the inode
2516 * is created as orphan.
2517 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002518static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002519{
2520 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002521 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002522 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002523 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002524 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002525 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002526
Alexander Block1f4692d2012-07-28 10:42:24 +02002527verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002528
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002529 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002530 if (!p)
2531 return -ENOMEM;
2532
Liu Bo644d1942014-02-27 17:29:01 +08002533 if (ino != sctx->cur_ino) {
2534 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2535 NULL, NULL, &rdev);
2536 if (ret < 0)
2537 goto out;
2538 } else {
2539 gen = sctx->cur_inode_gen;
2540 mode = sctx->cur_inode_mode;
2541 rdev = sctx->cur_inode_rdev;
2542 }
Alexander Block31db9f72012-07-25 23:19:24 +02002543
Alexander Blocke938c8a2012-07-28 16:33:49 +02002544 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002545 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002546 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002547 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002548 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002549 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002550 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002551 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002552 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002553 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002554 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002555 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002556 } else {
David Sterbaf14d1042015-10-08 11:37:06 +02002557 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
Alexander Block31db9f72012-07-25 23:19:24 +02002558 (int)(mode & S_IFMT));
2559 ret = -ENOTSUPP;
2560 goto out;
2561 }
2562
2563 ret = begin_cmd(sctx, cmd);
2564 if (ret < 0)
2565 goto out;
2566
Alexander Block1f4692d2012-07-28 10:42:24 +02002567 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002568 if (ret < 0)
2569 goto out;
2570
2571 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002572 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002573
2574 if (S_ISLNK(mode)) {
2575 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002576 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002577 if (ret < 0)
2578 goto out;
2579 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2580 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2581 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002582 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2583 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002584 }
2585
2586 ret = send_cmd(sctx);
2587 if (ret < 0)
2588 goto out;
2589
2590
2591tlv_put_failure:
2592out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002593 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002594 return ret;
2595}
2596
Alexander Block1f4692d2012-07-28 10:42:24 +02002597/*
2598 * We need some special handling for inodes that get processed before the parent
2599 * directory got created. See process_recorded_refs for details.
2600 * This function does the check if we already created the dir out of order.
2601 */
2602static int did_create_dir(struct send_ctx *sctx, u64 dir)
2603{
2604 int ret = 0;
2605 struct btrfs_path *path = NULL;
2606 struct btrfs_key key;
2607 struct btrfs_key found_key;
2608 struct btrfs_key di_key;
2609 struct extent_buffer *eb;
2610 struct btrfs_dir_item *di;
2611 int slot;
2612
2613 path = alloc_path_for_send();
2614 if (!path) {
2615 ret = -ENOMEM;
2616 goto out;
2617 }
2618
2619 key.objectid = dir;
2620 key.type = BTRFS_DIR_INDEX_KEY;
2621 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002622 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2623 if (ret < 0)
2624 goto out;
2625
Alexander Block1f4692d2012-07-28 10:42:24 +02002626 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002627 eb = path->nodes[0];
2628 slot = path->slots[0];
2629 if (slot >= btrfs_header_nritems(eb)) {
2630 ret = btrfs_next_leaf(sctx->send_root, path);
2631 if (ret < 0) {
2632 goto out;
2633 } else if (ret > 0) {
2634 ret = 0;
2635 break;
2636 }
2637 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002638 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002639
2640 btrfs_item_key_to_cpu(eb, &found_key, slot);
2641 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002642 found_key.type != key.type) {
2643 ret = 0;
2644 goto out;
2645 }
2646
2647 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2648 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2649
Josef Bacika0525412013-08-12 10:56:14 -04002650 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2651 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002652 ret = 1;
2653 goto out;
2654 }
2655
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002656 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002657 }
2658
2659out:
2660 btrfs_free_path(path);
2661 return ret;
2662}
2663
2664/*
2665 * Only creates the inode if it is:
2666 * 1. Not a directory
2667 * 2. Or a directory which was not created already due to out of order
2668 * directories. See did_create_dir and process_recorded_refs for details.
2669 */
2670static int send_create_inode_if_needed(struct send_ctx *sctx)
2671{
2672 int ret;
2673
2674 if (S_ISDIR(sctx->cur_inode_mode)) {
2675 ret = did_create_dir(sctx, sctx->cur_ino);
2676 if (ret < 0)
2677 goto out;
2678 if (ret) {
2679 ret = 0;
2680 goto out;
2681 }
2682 }
2683
2684 ret = send_create_inode(sctx, sctx->cur_ino);
2685 if (ret < 0)
2686 goto out;
2687
2688out:
2689 return ret;
2690}
2691
Alexander Block31db9f72012-07-25 23:19:24 +02002692struct recorded_ref {
2693 struct list_head list;
2694 char *dir_path;
2695 char *name;
2696 struct fs_path *full_path;
2697 u64 dir;
2698 u64 dir_gen;
2699 int dir_path_len;
2700 int name_len;
2701};
2702
2703/*
2704 * We need to process new refs before deleted refs, but compare_tree gives us
2705 * everything mixed. So we first record all refs and later process them.
2706 * This function is a helper to record one ref.
2707 */
Liu Boa4d96d62014-03-03 21:31:03 +08002708static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002709 u64 dir_gen, struct fs_path *path)
2710{
2711 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002712
2713 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2714 if (!ref)
2715 return -ENOMEM;
2716
2717 ref->dir = dir;
2718 ref->dir_gen = dir_gen;
2719 ref->full_path = path;
2720
Andy Shevchenkoed848852013-08-21 10:32:13 +03002721 ref->name = (char *)kbasename(ref->full_path->start);
2722 ref->name_len = ref->full_path->end - ref->name;
2723 ref->dir_path = ref->full_path->start;
2724 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002725 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002726 else
Alexander Block31db9f72012-07-25 23:19:24 +02002727 ref->dir_path_len = ref->full_path->end -
2728 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002729
2730 list_add_tail(&ref->list, head);
2731 return 0;
2732}
2733
Josef Bacikba5e8f22013-08-16 16:52:55 -04002734static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2735{
2736 struct recorded_ref *new;
2737
2738 new = kmalloc(sizeof(*ref), GFP_NOFS);
2739 if (!new)
2740 return -ENOMEM;
2741
2742 new->dir = ref->dir;
2743 new->dir_gen = ref->dir_gen;
2744 new->full_path = NULL;
2745 INIT_LIST_HEAD(&new->list);
2746 list_add_tail(&new->list, list);
2747 return 0;
2748}
2749
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002750static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002751{
2752 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002753
Alexander Blocke938c8a2012-07-28 16:33:49 +02002754 while (!list_empty(head)) {
2755 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002756 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002757 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002758 kfree(cur);
2759 }
Alexander Block31db9f72012-07-25 23:19:24 +02002760}
2761
2762static void free_recorded_refs(struct send_ctx *sctx)
2763{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002764 __free_recorded_refs(&sctx->new_refs);
2765 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002766}
2767
2768/*
Alexander Block766702e2012-07-28 14:11:31 +02002769 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002770 * ref of an unprocessed inode gets overwritten and for all non empty
2771 * directories.
2772 */
2773static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2774 struct fs_path *path)
2775{
2776 int ret;
2777 struct fs_path *orphan;
2778
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002779 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002780 if (!orphan)
2781 return -ENOMEM;
2782
2783 ret = gen_unique_name(sctx, ino, gen, orphan);
2784 if (ret < 0)
2785 goto out;
2786
2787 ret = send_rename(sctx, path, orphan);
2788
2789out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002790 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002791 return ret;
2792}
2793
Filipe Manana9dc44212014-02-19 14:31:44 +00002794static struct orphan_dir_info *
2795add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2796{
2797 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2798 struct rb_node *parent = NULL;
2799 struct orphan_dir_info *entry, *odi;
2800
2801 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2802 if (!odi)
2803 return ERR_PTR(-ENOMEM);
2804 odi->ino = dir_ino;
2805 odi->gen = 0;
2806
2807 while (*p) {
2808 parent = *p;
2809 entry = rb_entry(parent, struct orphan_dir_info, node);
2810 if (dir_ino < entry->ino) {
2811 p = &(*p)->rb_left;
2812 } else if (dir_ino > entry->ino) {
2813 p = &(*p)->rb_right;
2814 } else {
2815 kfree(odi);
2816 return entry;
2817 }
2818 }
2819
2820 rb_link_node(&odi->node, parent, p);
2821 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2822 return odi;
2823}
2824
2825static struct orphan_dir_info *
2826get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2827{
2828 struct rb_node *n = sctx->orphan_dirs.rb_node;
2829 struct orphan_dir_info *entry;
2830
2831 while (n) {
2832 entry = rb_entry(n, struct orphan_dir_info, node);
2833 if (dir_ino < entry->ino)
2834 n = n->rb_left;
2835 else if (dir_ino > entry->ino)
2836 n = n->rb_right;
2837 else
2838 return entry;
2839 }
2840 return NULL;
2841}
2842
2843static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2844{
2845 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2846
2847 return odi != NULL;
2848}
2849
2850static void free_orphan_dir_info(struct send_ctx *sctx,
2851 struct orphan_dir_info *odi)
2852{
2853 if (!odi)
2854 return;
2855 rb_erase(&odi->node, &sctx->orphan_dirs);
2856 kfree(odi);
2857}
2858
Alexander Block31db9f72012-07-25 23:19:24 +02002859/*
2860 * Returns 1 if a directory can be removed at this point in time.
2861 * We check this by iterating all dir items and checking if the inode behind
2862 * the dir item was already processed.
2863 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002864static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2865 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002866{
2867 int ret = 0;
2868 struct btrfs_root *root = sctx->parent_root;
2869 struct btrfs_path *path;
2870 struct btrfs_key key;
2871 struct btrfs_key found_key;
2872 struct btrfs_key loc;
2873 struct btrfs_dir_item *di;
2874
Alexander Block6d85ed02012-08-01 14:48:59 +02002875 /*
2876 * Don't try to rmdir the top/root subvolume dir.
2877 */
2878 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2879 return 0;
2880
Alexander Block31db9f72012-07-25 23:19:24 +02002881 path = alloc_path_for_send();
2882 if (!path)
2883 return -ENOMEM;
2884
2885 key.objectid = dir;
2886 key.type = BTRFS_DIR_INDEX_KEY;
2887 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002888 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2889 if (ret < 0)
2890 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002891
2892 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002893 struct waiting_dir_move *dm;
2894
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002895 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2896 ret = btrfs_next_leaf(root, path);
2897 if (ret < 0)
2898 goto out;
2899 else if (ret > 0)
2900 break;
2901 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002902 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002903 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2904 path->slots[0]);
2905 if (found_key.objectid != key.objectid ||
2906 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002907 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002908
2909 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2910 struct btrfs_dir_item);
2911 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2912
Filipe Manana9dc44212014-02-19 14:31:44 +00002913 dm = get_waiting_dir_move(sctx, loc.objectid);
2914 if (dm) {
2915 struct orphan_dir_info *odi;
2916
2917 odi = add_orphan_dir_info(sctx, dir);
2918 if (IS_ERR(odi)) {
2919 ret = PTR_ERR(odi);
2920 goto out;
2921 }
2922 odi->gen = dir_gen;
2923 dm->rmdir_ino = dir;
2924 ret = 0;
2925 goto out;
2926 }
2927
Alexander Block31db9f72012-07-25 23:19:24 +02002928 if (loc.objectid > send_progress) {
2929 ret = 0;
2930 goto out;
2931 }
2932
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002933 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002934 }
2935
2936 ret = 1;
2937
2938out:
2939 btrfs_free_path(path);
2940 return ret;
2941}
2942
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002943static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2944{
Filipe Manana9dc44212014-02-19 14:31:44 +00002945 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002946
Filipe Manana9dc44212014-02-19 14:31:44 +00002947 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002948}
2949
Filipe Manana8b191a62015-04-09 14:09:14 +01002950static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002951{
2952 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2953 struct rb_node *parent = NULL;
2954 struct waiting_dir_move *entry, *dm;
2955
2956 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2957 if (!dm)
2958 return -ENOMEM;
2959 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00002960 dm->rmdir_ino = 0;
Filipe Manana8b191a62015-04-09 14:09:14 +01002961 dm->orphanized = orphanized;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002962
2963 while (*p) {
2964 parent = *p;
2965 entry = rb_entry(parent, struct waiting_dir_move, node);
2966 if (ino < entry->ino) {
2967 p = &(*p)->rb_left;
2968 } else if (ino > entry->ino) {
2969 p = &(*p)->rb_right;
2970 } else {
2971 kfree(dm);
2972 return -EEXIST;
2973 }
2974 }
2975
2976 rb_link_node(&dm->node, parent, p);
2977 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2978 return 0;
2979}
2980
Filipe Manana9dc44212014-02-19 14:31:44 +00002981static struct waiting_dir_move *
2982get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002983{
2984 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2985 struct waiting_dir_move *entry;
2986
2987 while (n) {
2988 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00002989 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002990 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00002991 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002992 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002993 else
2994 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002995 }
Filipe Manana9dc44212014-02-19 14:31:44 +00002996 return NULL;
2997}
2998
2999static void free_waiting_dir_move(struct send_ctx *sctx,
3000 struct waiting_dir_move *dm)
3001{
3002 if (!dm)
3003 return;
3004 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3005 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003006}
3007
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003008static int add_pending_dir_move(struct send_ctx *sctx,
3009 u64 ino,
3010 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00003011 u64 parent_ino,
3012 struct list_head *new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003013 struct list_head *deleted_refs,
3014 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003015{
3016 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3017 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07003018 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003019 struct recorded_ref *cur;
3020 int exists = 0;
3021 int ret;
3022
3023 pm = kmalloc(sizeof(*pm), GFP_NOFS);
3024 if (!pm)
3025 return -ENOMEM;
3026 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003027 pm->ino = ino;
3028 pm->gen = ino_gen;
Filipe Manana84471e22015-02-28 22:29:22 +00003029 pm->is_orphan = is_orphan;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003030 INIT_LIST_HEAD(&pm->list);
3031 INIT_LIST_HEAD(&pm->update_refs);
3032 RB_CLEAR_NODE(&pm->node);
3033
3034 while (*p) {
3035 parent = *p;
3036 entry = rb_entry(parent, struct pending_dir_move, node);
3037 if (parent_ino < entry->parent_ino) {
3038 p = &(*p)->rb_left;
3039 } else if (parent_ino > entry->parent_ino) {
3040 p = &(*p)->rb_right;
3041 } else {
3042 exists = 1;
3043 break;
3044 }
3045 }
3046
Filipe Mananaf9594922014-03-27 20:14:01 +00003047 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003048 ret = dup_ref(cur, &pm->update_refs);
3049 if (ret < 0)
3050 goto out;
3051 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003052 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003053 ret = dup_ref(cur, &pm->update_refs);
3054 if (ret < 0)
3055 goto out;
3056 }
3057
Filipe Manana8b191a62015-04-09 14:09:14 +01003058 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003059 if (ret)
3060 goto out;
3061
3062 if (exists) {
3063 list_add_tail(&pm->list, &entry->list);
3064 } else {
3065 rb_link_node(&pm->node, parent, p);
3066 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3067 }
3068 ret = 0;
3069out:
3070 if (ret) {
3071 __free_recorded_refs(&pm->update_refs);
3072 kfree(pm);
3073 }
3074 return ret;
3075}
3076
3077static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3078 u64 parent_ino)
3079{
3080 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3081 struct pending_dir_move *entry;
3082
3083 while (n) {
3084 entry = rb_entry(n, struct pending_dir_move, node);
3085 if (parent_ino < entry->parent_ino)
3086 n = n->rb_left;
3087 else if (parent_ino > entry->parent_ino)
3088 n = n->rb_right;
3089 else
3090 return entry;
3091 }
3092 return NULL;
3093}
3094
3095static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3096{
3097 struct fs_path *from_path = NULL;
3098 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003099 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003100 u64 orig_progress = sctx->send_progress;
3101 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003102 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003103 struct waiting_dir_move *dm = NULL;
3104 u64 rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003105 int ret;
3106
Filipe Manana2b863a12014-02-16 13:43:11 +00003107 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003108 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003109 if (!name || !from_path) {
3110 ret = -ENOMEM;
3111 goto out;
3112 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003113
Filipe Manana9dc44212014-02-19 14:31:44 +00003114 dm = get_waiting_dir_move(sctx, pm->ino);
3115 ASSERT(dm);
3116 rmdir_ino = dm->rmdir_ino;
3117 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003118
Filipe Manana84471e22015-02-28 22:29:22 +00003119 if (pm->is_orphan) {
3120 ret = gen_unique_name(sctx, pm->ino,
3121 pm->gen, from_path);
3122 } else {
3123 ret = get_first_ref(sctx->parent_root, pm->ino,
3124 &parent_ino, &parent_gen, name);
3125 if (ret < 0)
3126 goto out;
3127 ret = get_cur_path(sctx, parent_ino, parent_gen,
3128 from_path);
3129 if (ret < 0)
3130 goto out;
3131 ret = fs_path_add_path(from_path, name);
3132 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003133 if (ret < 0)
3134 goto out;
3135
Filipe Mananaf9594922014-03-27 20:14:01 +00003136 sctx->send_progress = sctx->cur_ino + 1;
Filipe Mananac992ec92014-03-22 17:15:24 +00003137 fs_path_reset(name);
3138 to_path = name;
3139 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003140 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3141 if (ret < 0)
3142 goto out;
3143
3144 ret = send_rename(sctx, from_path, to_path);
3145 if (ret < 0)
3146 goto out;
3147
Filipe Manana9dc44212014-02-19 14:31:44 +00003148 if (rmdir_ino) {
3149 struct orphan_dir_info *odi;
3150
3151 odi = get_orphan_dir_info(sctx, rmdir_ino);
3152 if (!odi) {
3153 /* already deleted */
3154 goto finish;
3155 }
3156 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3157 if (ret < 0)
3158 goto out;
3159 if (!ret)
3160 goto finish;
3161
3162 name = fs_path_alloc();
3163 if (!name) {
3164 ret = -ENOMEM;
3165 goto out;
3166 }
3167 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3168 if (ret < 0)
3169 goto out;
3170 ret = send_rmdir(sctx, name);
3171 if (ret < 0)
3172 goto out;
3173 free_orphan_dir_info(sctx, odi);
3174 }
3175
3176finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003177 ret = send_utimes(sctx, pm->ino, pm->gen);
3178 if (ret < 0)
3179 goto out;
3180
3181 /*
3182 * After rename/move, need to update the utimes of both new parent(s)
3183 * and old parent(s).
3184 */
3185 list_for_each_entry(cur, &pm->update_refs, list) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003186 if (cur->dir == rmdir_ino)
3187 continue;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003188 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3189 if (ret < 0)
3190 goto out;
3191 }
3192
3193out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003194 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003195 fs_path_free(from_path);
3196 fs_path_free(to_path);
3197 sctx->send_progress = orig_progress;
3198
3199 return ret;
3200}
3201
3202static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3203{
3204 if (!list_empty(&m->list))
3205 list_del(&m->list);
3206 if (!RB_EMPTY_NODE(&m->node))
3207 rb_erase(&m->node, &sctx->pending_dir_moves);
3208 __free_recorded_refs(&m->update_refs);
3209 kfree(m);
3210}
3211
3212static void tail_append_pending_moves(struct pending_dir_move *moves,
3213 struct list_head *stack)
3214{
3215 if (list_empty(&moves->list)) {
3216 list_add_tail(&moves->list, stack);
3217 } else {
3218 LIST_HEAD(list);
3219 list_splice_init(&moves->list, &list);
3220 list_add_tail(&moves->list, stack);
3221 list_splice_tail(&list, stack);
3222 }
3223}
3224
3225static int apply_children_dir_moves(struct send_ctx *sctx)
3226{
3227 struct pending_dir_move *pm;
3228 struct list_head stack;
3229 u64 parent_ino = sctx->cur_ino;
3230 int ret = 0;
3231
3232 pm = get_pending_dir_moves(sctx, parent_ino);
3233 if (!pm)
3234 return 0;
3235
3236 INIT_LIST_HEAD(&stack);
3237 tail_append_pending_moves(pm, &stack);
3238
3239 while (!list_empty(&stack)) {
3240 pm = list_first_entry(&stack, struct pending_dir_move, list);
3241 parent_ino = pm->ino;
3242 ret = apply_dir_move(sctx, pm);
3243 free_pending_move(sctx, pm);
3244 if (ret)
3245 goto out;
3246 pm = get_pending_dir_moves(sctx, parent_ino);
3247 if (pm)
3248 tail_append_pending_moves(pm, &stack);
3249 }
3250 return 0;
3251
3252out:
3253 while (!list_empty(&stack)) {
3254 pm = list_first_entry(&stack, struct pending_dir_move, list);
3255 free_pending_move(sctx, pm);
3256 }
3257 return ret;
3258}
3259
Filipe Manana84471e22015-02-28 22:29:22 +00003260/*
3261 * We might need to delay a directory rename even when no ancestor directory
3262 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3263 * renamed. This happens when we rename a directory to the old name (the name
3264 * in the parent root) of some other unrelated directory that got its rename
3265 * delayed due to some ancestor with higher number that got renamed.
3266 *
3267 * Example:
3268 *
3269 * Parent snapshot:
3270 * . (ino 256)
3271 * |---- a/ (ino 257)
3272 * | |---- file (ino 260)
3273 * |
3274 * |---- b/ (ino 258)
3275 * |---- c/ (ino 259)
3276 *
3277 * Send snapshot:
3278 * . (ino 256)
3279 * |---- a/ (ino 258)
3280 * |---- x/ (ino 259)
3281 * |---- y/ (ino 257)
3282 * |----- file (ino 260)
3283 *
3284 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3285 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3286 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3287 * must issue is:
3288 *
3289 * 1 - rename 259 from 'c' to 'x'
3290 * 2 - rename 257 from 'a' to 'x/y'
3291 * 3 - rename 258 from 'b' to 'a'
3292 *
3293 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3294 * be done right away and < 0 on error.
3295 */
3296static int wait_for_dest_dir_move(struct send_ctx *sctx,
3297 struct recorded_ref *parent_ref,
3298 const bool is_orphan)
3299{
3300 struct btrfs_path *path;
3301 struct btrfs_key key;
3302 struct btrfs_key di_key;
3303 struct btrfs_dir_item *di;
3304 u64 left_gen;
3305 u64 right_gen;
3306 int ret = 0;
3307
3308 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3309 return 0;
3310
3311 path = alloc_path_for_send();
3312 if (!path)
3313 return -ENOMEM;
3314
3315 key.objectid = parent_ref->dir;
3316 key.type = BTRFS_DIR_ITEM_KEY;
3317 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3318
3319 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3320 if (ret < 0) {
3321 goto out;
3322 } else if (ret > 0) {
3323 ret = 0;
3324 goto out;
3325 }
3326
3327 di = btrfs_match_dir_item_name(sctx->parent_root, path,
3328 parent_ref->name, parent_ref->name_len);
3329 if (!di) {
3330 ret = 0;
3331 goto out;
3332 }
3333 /*
3334 * di_key.objectid has the number of the inode that has a dentry in the
3335 * parent directory with the same name that sctx->cur_ino is being
3336 * renamed to. We need to check if that inode is in the send root as
3337 * well and if it is currently marked as an inode with a pending rename,
3338 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3339 * that it happens after that other inode is renamed.
3340 */
3341 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3342 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3343 ret = 0;
3344 goto out;
3345 }
3346
3347 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3348 &left_gen, NULL, NULL, NULL, NULL);
3349 if (ret < 0)
3350 goto out;
3351 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3352 &right_gen, NULL, NULL, NULL, NULL);
3353 if (ret < 0) {
3354 if (ret == -ENOENT)
3355 ret = 0;
3356 goto out;
3357 }
3358
3359 /* Different inode, no need to delay the rename of sctx->cur_ino */
3360 if (right_gen != left_gen) {
3361 ret = 0;
3362 goto out;
3363 }
3364
3365 if (is_waiting_for_move(sctx, di_key.objectid)) {
3366 ret = add_pending_dir_move(sctx,
3367 sctx->cur_ino,
3368 sctx->cur_inode_gen,
3369 di_key.objectid,
3370 &sctx->new_refs,
3371 &sctx->deleted_refs,
3372 is_orphan);
3373 if (!ret)
3374 ret = 1;
3375 }
3376out:
3377 btrfs_free_path(path);
3378 return ret;
3379}
3380
Filipe Manana80aa6022015-03-27 17:50:45 +00003381/*
3382 * Check if ino ino1 is an ancestor of inode ino2 in the given root.
3383 * Return 1 if true, 0 if false and < 0 on error.
3384 */
3385static int is_ancestor(struct btrfs_root *root,
3386 const u64 ino1,
3387 const u64 ino1_gen,
3388 const u64 ino2,
3389 struct fs_path *fs_path)
3390{
3391 u64 ino = ino2;
3392
3393 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3394 int ret;
3395 u64 parent;
3396 u64 parent_gen;
3397
3398 fs_path_reset(fs_path);
3399 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3400 if (ret < 0) {
3401 if (ret == -ENOENT && ino == ino2)
3402 ret = 0;
3403 return ret;
3404 }
3405 if (parent == ino1)
3406 return parent_gen == ino1_gen ? 1 : 0;
3407 ino = parent;
3408 }
3409 return 0;
3410}
3411
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003412static int wait_for_parent_move(struct send_ctx *sctx,
Filipe Manana8b191a62015-04-09 14:09:14 +01003413 struct recorded_ref *parent_ref,
3414 const bool is_orphan)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003415{
Filipe Mananaf9594922014-03-27 20:14:01 +00003416 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003417 u64 ino = parent_ref->dir;
3418 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003419 struct fs_path *path_before = NULL;
3420 struct fs_path *path_after = NULL;
3421 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003422
3423 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003424 path_before = fs_path_alloc();
3425 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003426 ret = -ENOMEM;
3427 goto out;
3428 }
3429
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003430 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003431 * Our current directory inode may not yet be renamed/moved because some
3432 * ancestor (immediate or not) has to be renamed/moved first. So find if
3433 * such ancestor exists and make sure our own rename/move happens after
Filipe Manana80aa6022015-03-27 17:50:45 +00003434 * that ancestor is processed to avoid path build infinite loops (done
3435 * at get_cur_path()).
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003436 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003437 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3438 if (is_waiting_for_move(sctx, ino)) {
Filipe Manana80aa6022015-03-27 17:50:45 +00003439 /*
3440 * If the current inode is an ancestor of ino in the
3441 * parent root, we need to delay the rename of the
3442 * current inode, otherwise don't delayed the rename
3443 * because we can end up with a circular dependency
3444 * of renames, resulting in some directories never
3445 * getting the respective rename operations issued in
3446 * the send stream or getting into infinite path build
3447 * loops.
3448 */
3449 ret = is_ancestor(sctx->parent_root,
3450 sctx->cur_ino, sctx->cur_inode_gen,
3451 ino, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003452 break;
3453 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003454
3455 fs_path_reset(path_before);
3456 fs_path_reset(path_after);
3457
3458 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananaf9594922014-03-27 20:14:01 +00003459 NULL, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003460 if (ret < 0)
3461 goto out;
3462 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3463 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003464 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003465 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003466 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003467 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003468 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003469 }
3470
3471 len1 = fs_path_len(path_before);
3472 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003473 if (ino > sctx->cur_ino &&
3474 (parent_ino_before != parent_ino_after || len1 != len2 ||
3475 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003476 ret = 1;
Filipe Mananaf9594922014-03-27 20:14:01 +00003477 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003478 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003479 ino = parent_ino_after;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003480 }
3481
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003482out:
3483 fs_path_free(path_before);
3484 fs_path_free(path_after);
3485
Filipe Mananaf9594922014-03-27 20:14:01 +00003486 if (ret == 1) {
3487 ret = add_pending_dir_move(sctx,
3488 sctx->cur_ino,
3489 sctx->cur_inode_gen,
3490 ino,
3491 &sctx->new_refs,
Filipe Manana84471e22015-02-28 22:29:22 +00003492 &sctx->deleted_refs,
Filipe Manana8b191a62015-04-09 14:09:14 +01003493 is_orphan);
Filipe Mananaf9594922014-03-27 20:14:01 +00003494 if (!ret)
3495 ret = 1;
3496 }
3497
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003498 return ret;
3499}
3500
Alexander Block31db9f72012-07-25 23:19:24 +02003501/*
3502 * This does all the move/link/unlink/rmdir magic.
3503 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003504static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003505{
3506 int ret = 0;
3507 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003508 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003509 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003510 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003511 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003512 u64 ow_gen;
3513 int did_overwrite = 0;
3514 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003515 u64 last_dir_ino_rm = 0;
Filipe Manana84471e22015-02-28 22:29:22 +00003516 bool can_rename = true;
Alexander Block31db9f72012-07-25 23:19:24 +02003517
3518verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3519
Alexander Block6d85ed02012-08-01 14:48:59 +02003520 /*
3521 * This should never happen as the root dir always has the same ref
3522 * which is always '..'
3523 */
3524 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003525 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003526
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003527 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003528 if (!valid_path) {
3529 ret = -ENOMEM;
3530 goto out;
3531 }
3532
Alexander Block31db9f72012-07-25 23:19:24 +02003533 /*
3534 * First, check if the first ref of the current inode was overwritten
3535 * before. If yes, we know that the current inode was already orphanized
3536 * and thus use the orphan name. If not, we can use get_cur_path to
3537 * get the path of the first ref as it would like while receiving at
3538 * this point in time.
3539 * New inodes are always orphan at the beginning, so force to use the
3540 * orphan name in this case.
3541 * The first ref is stored in valid_path and will be updated if it
3542 * gets moved around.
3543 */
3544 if (!sctx->cur_inode_new) {
3545 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3546 sctx->cur_inode_gen);
3547 if (ret < 0)
3548 goto out;
3549 if (ret)
3550 did_overwrite = 1;
3551 }
3552 if (sctx->cur_inode_new || did_overwrite) {
3553 ret = gen_unique_name(sctx, sctx->cur_ino,
3554 sctx->cur_inode_gen, valid_path);
3555 if (ret < 0)
3556 goto out;
3557 is_orphan = 1;
3558 } else {
3559 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3560 valid_path);
3561 if (ret < 0)
3562 goto out;
3563 }
3564
3565 list_for_each_entry(cur, &sctx->new_refs, list) {
3566 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003567 * We may have refs where the parent directory does not exist
3568 * yet. This happens if the parent directories inum is higher
3569 * the the current inum. To handle this case, we create the
3570 * parent directory out of order. But we need to check if this
3571 * did already happen before due to other refs in the same dir.
3572 */
3573 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3574 if (ret < 0)
3575 goto out;
3576 if (ret == inode_state_will_create) {
3577 ret = 0;
3578 /*
3579 * First check if any of the current inodes refs did
3580 * already create the dir.
3581 */
3582 list_for_each_entry(cur2, &sctx->new_refs, list) {
3583 if (cur == cur2)
3584 break;
3585 if (cur2->dir == cur->dir) {
3586 ret = 1;
3587 break;
3588 }
3589 }
3590
3591 /*
3592 * If that did not happen, check if a previous inode
3593 * did already create the dir.
3594 */
3595 if (!ret)
3596 ret = did_create_dir(sctx, cur->dir);
3597 if (ret < 0)
3598 goto out;
3599 if (!ret) {
3600 ret = send_create_inode(sctx, cur->dir);
3601 if (ret < 0)
3602 goto out;
3603 }
3604 }
3605
3606 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003607 * Check if this new ref would overwrite the first ref of
3608 * another unprocessed inode. If yes, orphanize the
3609 * overwritten inode. If we find an overwritten ref that is
3610 * not the first ref, simply unlink it.
3611 */
3612 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3613 cur->name, cur->name_len,
3614 &ow_inode, &ow_gen);
3615 if (ret < 0)
3616 goto out;
3617 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003618 ret = is_first_ref(sctx->parent_root,
3619 ow_inode, cur->dir, cur->name,
3620 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003621 if (ret < 0)
3622 goto out;
3623 if (ret) {
Filipe Manana8996a482015-03-12 15:16:20 +00003624 struct name_cache_entry *nce;
3625
Alexander Block31db9f72012-07-25 23:19:24 +02003626 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3627 cur->full_path);
3628 if (ret < 0)
3629 goto out;
Filipe Manana8996a482015-03-12 15:16:20 +00003630 /*
3631 * Make sure we clear our orphanized inode's
3632 * name from the name cache. This is because the
3633 * inode ow_inode might be an ancestor of some
3634 * other inode that will be orphanized as well
3635 * later and has an inode number greater than
3636 * sctx->send_progress. We need to prevent
3637 * future name lookups from using the old name
3638 * and get instead the orphan name.
3639 */
3640 nce = name_cache_search(sctx, ow_inode, ow_gen);
3641 if (nce) {
3642 name_cache_delete(sctx, nce);
3643 kfree(nce);
3644 }
Alexander Block31db9f72012-07-25 23:19:24 +02003645 } else {
3646 ret = send_unlink(sctx, cur->full_path);
3647 if (ret < 0)
3648 goto out;
3649 }
3650 }
3651
Filipe Manana84471e22015-02-28 22:29:22 +00003652 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3653 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3654 if (ret < 0)
3655 goto out;
3656 if (ret == 1) {
3657 can_rename = false;
3658 *pending_move = 1;
3659 }
3660 }
3661
Filipe Manana8b191a62015-04-09 14:09:14 +01003662 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3663 can_rename) {
3664 ret = wait_for_parent_move(sctx, cur, is_orphan);
3665 if (ret < 0)
3666 goto out;
3667 if (ret == 1) {
3668 can_rename = false;
3669 *pending_move = 1;
3670 }
3671 }
3672
Alexander Block31db9f72012-07-25 23:19:24 +02003673 /*
3674 * link/move the ref to the new place. If we have an orphan
3675 * inode, move it and update valid_path. If not, link or move
3676 * it depending on the inode mode.
3677 */
Filipe Manana84471e22015-02-28 22:29:22 +00003678 if (is_orphan && can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003679 ret = send_rename(sctx, valid_path, cur->full_path);
3680 if (ret < 0)
3681 goto out;
3682 is_orphan = 0;
3683 ret = fs_path_copy(valid_path, cur->full_path);
3684 if (ret < 0)
3685 goto out;
Filipe Manana84471e22015-02-28 22:29:22 +00003686 } else if (can_rename) {
Alexander Block31db9f72012-07-25 23:19:24 +02003687 if (S_ISDIR(sctx->cur_inode_mode)) {
3688 /*
3689 * Dirs can't be linked, so move it. For moved
3690 * dirs, we always have one new and one deleted
3691 * ref. The deleted ref is ignored later.
3692 */
Filipe Manana8b191a62015-04-09 14:09:14 +01003693 ret = send_rename(sctx, valid_path,
3694 cur->full_path);
3695 if (!ret)
3696 ret = fs_path_copy(valid_path,
3697 cur->full_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003698 if (ret < 0)
3699 goto out;
3700 } else {
3701 ret = send_link(sctx, cur->full_path,
3702 valid_path);
3703 if (ret < 0)
3704 goto out;
3705 }
3706 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003707 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003708 if (ret < 0)
3709 goto out;
3710 }
3711
3712 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3713 /*
3714 * Check if we can already rmdir the directory. If not,
3715 * orphanize it. For every dir item inside that gets deleted
3716 * later, we do this check again and rmdir it then if possible.
3717 * See the use of check_dirs for more details.
3718 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003719 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3720 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003721 if (ret < 0)
3722 goto out;
3723 if (ret) {
3724 ret = send_rmdir(sctx, valid_path);
3725 if (ret < 0)
3726 goto out;
3727 } else if (!is_orphan) {
3728 ret = orphanize_inode(sctx, sctx->cur_ino,
3729 sctx->cur_inode_gen, valid_path);
3730 if (ret < 0)
3731 goto out;
3732 is_orphan = 1;
3733 }
3734
3735 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003736 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003737 if (ret < 0)
3738 goto out;
3739 }
Alexander Blockccf16262012-07-28 11:46:29 +02003740 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3741 !list_empty(&sctx->deleted_refs)) {
3742 /*
3743 * We have a moved dir. Add the old parent to check_dirs
3744 */
3745 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3746 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003747 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003748 if (ret < 0)
3749 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003750 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3751 /*
3752 * We have a non dir inode. Go through all deleted refs and
3753 * unlink them if they were not already overwritten by other
3754 * inodes.
3755 */
3756 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3757 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3758 sctx->cur_ino, sctx->cur_inode_gen,
3759 cur->name, cur->name_len);
3760 if (ret < 0)
3761 goto out;
3762 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003763 ret = send_unlink(sctx, cur->full_path);
3764 if (ret < 0)
3765 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003766 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003767 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003768 if (ret < 0)
3769 goto out;
3770 }
Alexander Block31db9f72012-07-25 23:19:24 +02003771 /*
3772 * If the inode is still orphan, unlink the orphan. This may
3773 * happen when a previous inode did overwrite the first ref
3774 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003775 * inode. Unlinking does not mean that the inode is deleted in
3776 * all cases. There may still be links to this inode in other
3777 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003778 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003779 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003780 ret = send_unlink(sctx, valid_path);
3781 if (ret < 0)
3782 goto out;
3783 }
3784 }
3785
3786 /*
3787 * We did collect all parent dirs where cur_inode was once located. We
3788 * now go through all these dirs and check if they are pending for
3789 * deletion and if it's finally possible to perform the rmdir now.
3790 * We also update the inode stats of the parent dirs here.
3791 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003792 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003793 /*
3794 * In case we had refs into dirs that were not processed yet,
3795 * we don't need to do the utime and rmdir logic for these dirs.
3796 * The dir will be processed later.
3797 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003798 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003799 continue;
3800
Josef Bacikba5e8f22013-08-16 16:52:55 -04003801 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003802 if (ret < 0)
3803 goto out;
3804
3805 if (ret == inode_state_did_create ||
3806 ret == inode_state_no_change) {
3807 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003808 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003809 if (ret < 0)
3810 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003811 } else if (ret == inode_state_did_delete &&
3812 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003813 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3814 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003815 if (ret < 0)
3816 goto out;
3817 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003818 ret = get_cur_path(sctx, cur->dir,
3819 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003820 if (ret < 0)
3821 goto out;
3822 ret = send_rmdir(sctx, valid_path);
3823 if (ret < 0)
3824 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003825 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02003826 }
3827 }
3828 }
3829
Alexander Block31db9f72012-07-25 23:19:24 +02003830 ret = 0;
3831
3832out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04003833 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003834 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003835 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003836 return ret;
3837}
3838
Liu Boa4d96d62014-03-03 21:31:03 +08003839static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3840 struct fs_path *name, void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02003841{
3842 int ret = 0;
3843 struct send_ctx *sctx = ctx;
3844 struct fs_path *p;
3845 u64 gen;
3846
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003847 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003848 if (!p)
3849 return -ENOMEM;
3850
Liu Boa4d96d62014-03-03 21:31:03 +08003851 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003852 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003853 if (ret < 0)
3854 goto out;
3855
Alexander Block31db9f72012-07-25 23:19:24 +02003856 ret = get_cur_path(sctx, dir, gen, p);
3857 if (ret < 0)
3858 goto out;
3859 ret = fs_path_add_path(p, name);
3860 if (ret < 0)
3861 goto out;
3862
Liu Boa4d96d62014-03-03 21:31:03 +08003863 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003864
3865out:
3866 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003867 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003868 return ret;
3869}
3870
Liu Boa4d96d62014-03-03 21:31:03 +08003871static int __record_new_ref(int num, u64 dir, int index,
3872 struct fs_path *name,
3873 void *ctx)
3874{
3875 struct send_ctx *sctx = ctx;
3876 return record_ref(sctx->send_root, num, dir, index, name,
3877 ctx, &sctx->new_refs);
3878}
3879
3880
Alexander Block31db9f72012-07-25 23:19:24 +02003881static int __record_deleted_ref(int num, u64 dir, int index,
3882 struct fs_path *name,
3883 void *ctx)
3884{
Alexander Block31db9f72012-07-25 23:19:24 +02003885 struct send_ctx *sctx = ctx;
Liu Boa4d96d62014-03-03 21:31:03 +08003886 return record_ref(sctx->parent_root, num, dir, index, name,
3887 ctx, &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02003888}
3889
3890static int record_new_ref(struct send_ctx *sctx)
3891{
3892 int ret;
3893
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003894 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3895 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003896 if (ret < 0)
3897 goto out;
3898 ret = 0;
3899
3900out:
3901 return ret;
3902}
3903
3904static int record_deleted_ref(struct send_ctx *sctx)
3905{
3906 int ret;
3907
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003908 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3909 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003910 if (ret < 0)
3911 goto out;
3912 ret = 0;
3913
3914out:
3915 return ret;
3916}
3917
3918struct find_ref_ctx {
3919 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003920 u64 dir_gen;
3921 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003922 struct fs_path *name;
3923 int found_idx;
3924};
3925
3926static int __find_iref(int num, u64 dir, int index,
3927 struct fs_path *name,
3928 void *ctx_)
3929{
3930 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003931 u64 dir_gen;
3932 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003933
3934 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3935 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003936 /*
3937 * To avoid doing extra lookups we'll only do this if everything
3938 * else matches.
3939 */
3940 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3941 NULL, NULL, NULL);
3942 if (ret)
3943 return ret;
3944 if (dir_gen != ctx->dir_gen)
3945 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003946 ctx->found_idx = num;
3947 return 1;
3948 }
3949 return 0;
3950}
3951
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003952static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003953 struct btrfs_path *path,
3954 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003955 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003956{
3957 int ret;
3958 struct find_ref_ctx ctx;
3959
3960 ctx.dir = dir;
3961 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003962 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003963 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003964 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003965
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003966 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003967 if (ret < 0)
3968 return ret;
3969
3970 if (ctx.found_idx == -1)
3971 return -ENOENT;
3972
3973 return ctx.found_idx;
3974}
3975
3976static int __record_changed_new_ref(int num, u64 dir, int index,
3977 struct fs_path *name,
3978 void *ctx)
3979{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003980 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003981 int ret;
3982 struct send_ctx *sctx = ctx;
3983
Josef Bacikba5e8f22013-08-16 16:52:55 -04003984 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3985 NULL, NULL, NULL);
3986 if (ret)
3987 return ret;
3988
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003989 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003990 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003991 if (ret == -ENOENT)
3992 ret = __record_new_ref(num, dir, index, name, sctx);
3993 else if (ret > 0)
3994 ret = 0;
3995
3996 return ret;
3997}
3998
3999static int __record_changed_deleted_ref(int num, u64 dir, int index,
4000 struct fs_path *name,
4001 void *ctx)
4002{
Josef Bacikba5e8f22013-08-16 16:52:55 -04004003 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004004 int ret;
4005 struct send_ctx *sctx = ctx;
4006
Josef Bacikba5e8f22013-08-16 16:52:55 -04004007 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4008 NULL, NULL, NULL);
4009 if (ret)
4010 return ret;
4011
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004012 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04004013 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02004014 if (ret == -ENOENT)
4015 ret = __record_deleted_ref(num, dir, index, name, sctx);
4016 else if (ret > 0)
4017 ret = 0;
4018
4019 return ret;
4020}
4021
4022static int record_changed_ref(struct send_ctx *sctx)
4023{
4024 int ret = 0;
4025
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004026 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004027 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4028 if (ret < 0)
4029 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004030 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004031 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4032 if (ret < 0)
4033 goto out;
4034 ret = 0;
4035
4036out:
4037 return ret;
4038}
4039
4040/*
4041 * Record and process all refs at once. Needed when an inode changes the
4042 * generation number, which means that it was deleted and recreated.
4043 */
4044static int process_all_refs(struct send_ctx *sctx,
4045 enum btrfs_compare_tree_result cmd)
4046{
4047 int ret;
4048 struct btrfs_root *root;
4049 struct btrfs_path *path;
4050 struct btrfs_key key;
4051 struct btrfs_key found_key;
4052 struct extent_buffer *eb;
4053 int slot;
4054 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004055 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004056
4057 path = alloc_path_for_send();
4058 if (!path)
4059 return -ENOMEM;
4060
4061 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4062 root = sctx->send_root;
4063 cb = __record_new_ref;
4064 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4065 root = sctx->parent_root;
4066 cb = __record_deleted_ref;
4067 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01004068 btrfs_err(sctx->send_root->fs_info,
4069 "Wrong command %d in process_all_refs", cmd);
4070 ret = -EINVAL;
4071 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004072 }
4073
4074 key.objectid = sctx->cmp_key->objectid;
4075 key.type = BTRFS_INODE_REF_KEY;
4076 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004077 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4078 if (ret < 0)
4079 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004080
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004081 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004082 eb = path->nodes[0];
4083 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004084 if (slot >= btrfs_header_nritems(eb)) {
4085 ret = btrfs_next_leaf(root, path);
4086 if (ret < 0)
4087 goto out;
4088 else if (ret > 0)
4089 break;
4090 continue;
4091 }
4092
Alexander Block31db9f72012-07-25 23:19:24 +02004093 btrfs_item_key_to_cpu(eb, &found_key, slot);
4094
4095 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004096 (found_key.type != BTRFS_INODE_REF_KEY &&
4097 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02004098 break;
Alexander Block31db9f72012-07-25 23:19:24 +02004099
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004100 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004101 if (ret < 0)
4102 goto out;
4103
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004104 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004105 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02004106 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004107
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004108 ret = process_recorded_refs(sctx, &pending_move);
4109 /* Only applicable to an incremental send. */
4110 ASSERT(pending_move == 0);
Alexander Block31db9f72012-07-25 23:19:24 +02004111
4112out:
4113 btrfs_free_path(path);
4114 return ret;
4115}
4116
4117static int send_set_xattr(struct send_ctx *sctx,
4118 struct fs_path *path,
4119 const char *name, int name_len,
4120 const char *data, int data_len)
4121{
4122 int ret = 0;
4123
4124 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4125 if (ret < 0)
4126 goto out;
4127
4128 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4129 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4130 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4131
4132 ret = send_cmd(sctx);
4133
4134tlv_put_failure:
4135out:
4136 return ret;
4137}
4138
4139static int send_remove_xattr(struct send_ctx *sctx,
4140 struct fs_path *path,
4141 const char *name, int name_len)
4142{
4143 int ret = 0;
4144
4145 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4146 if (ret < 0)
4147 goto out;
4148
4149 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4150 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4151
4152 ret = send_cmd(sctx);
4153
4154tlv_put_failure:
4155out:
4156 return ret;
4157}
4158
4159static int __process_new_xattr(int num, struct btrfs_key *di_key,
4160 const char *name, int name_len,
4161 const char *data, int data_len,
4162 u8 type, void *ctx)
4163{
4164 int ret;
4165 struct send_ctx *sctx = ctx;
4166 struct fs_path *p;
4167 posix_acl_xattr_header dummy_acl;
4168
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004169 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004170 if (!p)
4171 return -ENOMEM;
4172
4173 /*
4174 * This hack is needed because empty acl's are stored as zero byte
4175 * data in xattrs. Problem with that is, that receiving these zero byte
4176 * acl's will fail later. To fix this, we send a dummy acl list that
4177 * only contains the version number and no entries.
4178 */
4179 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4180 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4181 if (data_len == 0) {
4182 dummy_acl.a_version =
4183 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4184 data = (char *)&dummy_acl;
4185 data_len = sizeof(dummy_acl);
4186 }
4187 }
4188
4189 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4190 if (ret < 0)
4191 goto out;
4192
4193 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4194
4195out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004196 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004197 return ret;
4198}
4199
4200static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4201 const char *name, int name_len,
4202 const char *data, int data_len,
4203 u8 type, void *ctx)
4204{
4205 int ret;
4206 struct send_ctx *sctx = ctx;
4207 struct fs_path *p;
4208
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004209 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004210 if (!p)
4211 return -ENOMEM;
4212
4213 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4214 if (ret < 0)
4215 goto out;
4216
4217 ret = send_remove_xattr(sctx, p, name, name_len);
4218
4219out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004220 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004221 return ret;
4222}
4223
4224static int process_new_xattr(struct send_ctx *sctx)
4225{
4226 int ret = 0;
4227
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004228 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4229 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004230
4231 return ret;
4232}
4233
4234static int process_deleted_xattr(struct send_ctx *sctx)
4235{
4236 int ret;
4237
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004238 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4239 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004240
4241 return ret;
4242}
4243
4244struct find_xattr_ctx {
4245 const char *name;
4246 int name_len;
4247 int found_idx;
4248 char *found_data;
4249 int found_data_len;
4250};
4251
4252static int __find_xattr(int num, struct btrfs_key *di_key,
4253 const char *name, int name_len,
4254 const char *data, int data_len,
4255 u8 type, void *vctx)
4256{
4257 struct find_xattr_ctx *ctx = vctx;
4258
4259 if (name_len == ctx->name_len &&
4260 strncmp(name, ctx->name, name_len) == 0) {
4261 ctx->found_idx = num;
4262 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00004263 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02004264 if (!ctx->found_data)
4265 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004266 return 1;
4267 }
4268 return 0;
4269}
4270
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004271static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004272 struct btrfs_path *path,
4273 struct btrfs_key *key,
4274 const char *name, int name_len,
4275 char **data, int *data_len)
4276{
4277 int ret;
4278 struct find_xattr_ctx ctx;
4279
4280 ctx.name = name;
4281 ctx.name_len = name_len;
4282 ctx.found_idx = -1;
4283 ctx.found_data = NULL;
4284 ctx.found_data_len = 0;
4285
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004286 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004287 if (ret < 0)
4288 return ret;
4289
4290 if (ctx.found_idx == -1)
4291 return -ENOENT;
4292 if (data) {
4293 *data = ctx.found_data;
4294 *data_len = ctx.found_data_len;
4295 } else {
4296 kfree(ctx.found_data);
4297 }
4298 return ctx.found_idx;
4299}
4300
4301
4302static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4303 const char *name, int name_len,
4304 const char *data, int data_len,
4305 u8 type, void *ctx)
4306{
4307 int ret;
4308 struct send_ctx *sctx = ctx;
4309 char *found_data = NULL;
4310 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004311
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004312 ret = find_xattr(sctx->parent_root, sctx->right_path,
4313 sctx->cmp_key, name, name_len, &found_data,
4314 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004315 if (ret == -ENOENT) {
4316 ret = __process_new_xattr(num, di_key, name, name_len, data,
4317 data_len, type, ctx);
4318 } else if (ret >= 0) {
4319 if (data_len != found_data_len ||
4320 memcmp(data, found_data, data_len)) {
4321 ret = __process_new_xattr(num, di_key, name, name_len,
4322 data, data_len, type, ctx);
4323 } else {
4324 ret = 0;
4325 }
4326 }
4327
4328 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004329 return ret;
4330}
4331
4332static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4333 const char *name, int name_len,
4334 const char *data, int data_len,
4335 u8 type, void *ctx)
4336{
4337 int ret;
4338 struct send_ctx *sctx = ctx;
4339
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004340 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4341 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004342 if (ret == -ENOENT)
4343 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4344 data_len, type, ctx);
4345 else if (ret >= 0)
4346 ret = 0;
4347
4348 return ret;
4349}
4350
4351static int process_changed_xattr(struct send_ctx *sctx)
4352{
4353 int ret = 0;
4354
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004355 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004356 sctx->cmp_key, __process_changed_new_xattr, sctx);
4357 if (ret < 0)
4358 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004359 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004360 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4361
4362out:
4363 return ret;
4364}
4365
4366static int process_all_new_xattrs(struct send_ctx *sctx)
4367{
4368 int ret;
4369 struct btrfs_root *root;
4370 struct btrfs_path *path;
4371 struct btrfs_key key;
4372 struct btrfs_key found_key;
4373 struct extent_buffer *eb;
4374 int slot;
4375
4376 path = alloc_path_for_send();
4377 if (!path)
4378 return -ENOMEM;
4379
4380 root = sctx->send_root;
4381
4382 key.objectid = sctx->cmp_key->objectid;
4383 key.type = BTRFS_XATTR_ITEM_KEY;
4384 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004385 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4386 if (ret < 0)
4387 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004388
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004389 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004390 eb = path->nodes[0];
4391 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004392 if (slot >= btrfs_header_nritems(eb)) {
4393 ret = btrfs_next_leaf(root, path);
4394 if (ret < 0) {
4395 goto out;
4396 } else if (ret > 0) {
4397 ret = 0;
4398 break;
4399 }
4400 continue;
4401 }
Alexander Block31db9f72012-07-25 23:19:24 +02004402
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004403 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004404 if (found_key.objectid != key.objectid ||
4405 found_key.type != key.type) {
4406 ret = 0;
4407 goto out;
4408 }
4409
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004410 ret = iterate_dir_item(root, path, &found_key,
4411 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004412 if (ret < 0)
4413 goto out;
4414
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004415 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004416 }
4417
4418out:
4419 btrfs_free_path(path);
4420 return ret;
4421}
4422
Josef Baciked259092013-10-25 11:36:01 -04004423static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4424{
4425 struct btrfs_root *root = sctx->send_root;
4426 struct btrfs_fs_info *fs_info = root->fs_info;
4427 struct inode *inode;
4428 struct page *page;
4429 char *addr;
4430 struct btrfs_key key;
4431 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4432 pgoff_t last_index;
4433 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4434 ssize_t ret = 0;
4435
4436 key.objectid = sctx->cur_ino;
4437 key.type = BTRFS_INODE_ITEM_KEY;
4438 key.offset = 0;
4439
4440 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4441 if (IS_ERR(inode))
4442 return PTR_ERR(inode);
4443
4444 if (offset + len > i_size_read(inode)) {
4445 if (offset > i_size_read(inode))
4446 len = 0;
4447 else
4448 len = offset - i_size_read(inode);
4449 }
4450 if (len == 0)
4451 goto out;
4452
4453 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
Liu Bo2131bcd32014-03-05 10:07:35 +08004454
4455 /* initial readahead */
4456 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4457 file_ra_state_init(&sctx->ra, inode->i_mapping);
4458 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4459 last_index - index + 1);
4460
Josef Baciked259092013-10-25 11:36:01 -04004461 while (index <= last_index) {
4462 unsigned cur_len = min_t(unsigned, len,
4463 PAGE_CACHE_SIZE - pg_offset);
4464 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4465 if (!page) {
4466 ret = -ENOMEM;
4467 break;
4468 }
4469
4470 if (!PageUptodate(page)) {
4471 btrfs_readpage(NULL, page);
4472 lock_page(page);
4473 if (!PageUptodate(page)) {
4474 unlock_page(page);
4475 page_cache_release(page);
4476 ret = -EIO;
4477 break;
4478 }
4479 }
4480
4481 addr = kmap(page);
4482 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4483 kunmap(page);
4484 unlock_page(page);
4485 page_cache_release(page);
4486 index++;
4487 pg_offset = 0;
4488 len -= cur_len;
4489 ret += cur_len;
4490 }
4491out:
4492 iput(inode);
4493 return ret;
4494}
4495
Alexander Block31db9f72012-07-25 23:19:24 +02004496/*
4497 * Read some bytes from the current inode/file and send a write command to
4498 * user space.
4499 */
4500static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4501{
4502 int ret = 0;
4503 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004504 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004505
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004506 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004507 if (!p)
4508 return -ENOMEM;
4509
Alexander Block31db9f72012-07-25 23:19:24 +02004510verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4511
Josef Baciked259092013-10-25 11:36:01 -04004512 num_read = fill_read_buf(sctx, offset, len);
4513 if (num_read <= 0) {
4514 if (num_read < 0)
4515 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004516 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004517 }
Alexander Block31db9f72012-07-25 23:19:24 +02004518
4519 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4520 if (ret < 0)
4521 goto out;
4522
4523 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4524 if (ret < 0)
4525 goto out;
4526
4527 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4528 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004529 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004530
4531 ret = send_cmd(sctx);
4532
4533tlv_put_failure:
4534out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004535 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004536 if (ret < 0)
4537 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004538 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004539}
4540
4541/*
4542 * Send a clone command to user space.
4543 */
4544static int send_clone(struct send_ctx *sctx,
4545 u64 offset, u32 len,
4546 struct clone_root *clone_root)
4547{
4548 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004549 struct fs_path *p;
4550 u64 gen;
4551
4552verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4553 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4554 clone_root->root->objectid, clone_root->ino,
4555 clone_root->offset);
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 = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4562 if (ret < 0)
4563 goto out;
4564
4565 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4566 if (ret < 0)
4567 goto out;
4568
4569 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4570 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4571 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4572
Alexander Blocke938c8a2012-07-28 16:33:49 +02004573 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004574 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004575 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004576 if (ret < 0)
4577 goto out;
4578 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4579 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004580 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004581 }
4582 if (ret < 0)
4583 goto out;
4584
Josef Bacik37b8d272015-06-04 17:17:25 -04004585 /*
4586 * If the parent we're using has a received_uuid set then use that as
4587 * our clone source as that is what we will look for when doing a
4588 * receive.
4589 *
4590 * This covers the case that we create a snapshot off of a received
4591 * subvolume and then use that as the parent and try to receive on a
4592 * different host.
4593 */
4594 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4595 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4596 clone_root->root->root_item.received_uuid);
4597 else
4598 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4599 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004600 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004601 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004602 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4603 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4604 clone_root->offset);
4605
4606 ret = send_cmd(sctx);
4607
4608tlv_put_failure:
4609out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004610 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004611 return ret;
4612}
4613
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004614/*
4615 * Send an update extent command to user space.
4616 */
4617static int send_update_extent(struct send_ctx *sctx,
4618 u64 offset, u32 len)
4619{
4620 int ret = 0;
4621 struct fs_path *p;
4622
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004623 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004624 if (!p)
4625 return -ENOMEM;
4626
4627 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4628 if (ret < 0)
4629 goto out;
4630
4631 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4632 if (ret < 0)
4633 goto out;
4634
4635 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4636 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4637 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4638
4639 ret = send_cmd(sctx);
4640
4641tlv_put_failure:
4642out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004643 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004644 return ret;
4645}
4646
Josef Bacik16e75492013-10-22 12:18:51 -04004647static int send_hole(struct send_ctx *sctx, u64 end)
4648{
4649 struct fs_path *p = NULL;
4650 u64 offset = sctx->cur_inode_last_extent;
4651 u64 len;
4652 int ret = 0;
4653
4654 p = fs_path_alloc();
4655 if (!p)
4656 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01004657 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4658 if (ret < 0)
4659 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04004660 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4661 while (offset < end) {
4662 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4663
4664 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4665 if (ret < 0)
4666 break;
Josef Bacik16e75492013-10-22 12:18:51 -04004667 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4668 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4669 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4670 ret = send_cmd(sctx);
4671 if (ret < 0)
4672 break;
4673 offset += len;
4674 }
4675tlv_put_failure:
4676 fs_path_free(p);
4677 return ret;
4678}
4679
Filipe Mananad906d492015-10-02 10:47:34 +01004680static int send_extent_data(struct send_ctx *sctx,
4681 const u64 offset,
4682 const u64 len)
4683{
4684 u64 sent = 0;
4685
4686 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
4687 return send_update_extent(sctx, offset, len);
4688
4689 while (sent < len) {
4690 u64 size = len - sent;
4691 int ret;
4692
4693 if (size > BTRFS_SEND_READ_SIZE)
4694 size = BTRFS_SEND_READ_SIZE;
4695 ret = send_write(sctx, offset + sent, size);
4696 if (ret < 0)
4697 return ret;
4698 if (!ret)
4699 break;
4700 sent += ret;
4701 }
4702 return 0;
4703}
4704
4705static int clone_range(struct send_ctx *sctx,
4706 struct clone_root *clone_root,
4707 const u64 disk_byte,
4708 u64 data_offset,
4709 u64 offset,
4710 u64 len)
4711{
4712 struct btrfs_path *path;
4713 struct btrfs_key key;
4714 int ret;
4715
4716 path = alloc_path_for_send();
4717 if (!path)
4718 return -ENOMEM;
4719
4720 /*
4721 * We can't send a clone operation for the entire range if we find
4722 * extent items in the respective range in the source file that
4723 * refer to different extents or if we find holes.
4724 * So check for that and do a mix of clone and regular write/copy
4725 * operations if needed.
4726 *
4727 * Example:
4728 *
4729 * mkfs.btrfs -f /dev/sda
4730 * mount /dev/sda /mnt
4731 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
4732 * cp --reflink=always /mnt/foo /mnt/bar
4733 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
4734 * btrfs subvolume snapshot -r /mnt /mnt/snap
4735 *
4736 * If when we send the snapshot and we are processing file bar (which
4737 * has a higher inode number than foo) we blindly send a clone operation
4738 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
4739 * a file bar that matches the content of file foo - iow, doesn't match
4740 * the content from bar in the original filesystem.
4741 */
4742 key.objectid = clone_root->ino;
4743 key.type = BTRFS_EXTENT_DATA_KEY;
4744 key.offset = clone_root->offset;
4745 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
4746 if (ret < 0)
4747 goto out;
4748 if (ret > 0 && path->slots[0] > 0) {
4749 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
4750 if (key.objectid == clone_root->ino &&
4751 key.type == BTRFS_EXTENT_DATA_KEY)
4752 path->slots[0]--;
4753 }
4754
4755 while (true) {
4756 struct extent_buffer *leaf = path->nodes[0];
4757 int slot = path->slots[0];
4758 struct btrfs_file_extent_item *ei;
4759 u8 type;
4760 u64 ext_len;
4761 u64 clone_len;
4762
4763 if (slot >= btrfs_header_nritems(leaf)) {
4764 ret = btrfs_next_leaf(clone_root->root, path);
4765 if (ret < 0)
4766 goto out;
4767 else if (ret > 0)
4768 break;
4769 continue;
4770 }
4771
4772 btrfs_item_key_to_cpu(leaf, &key, slot);
4773
4774 /*
4775 * We might have an implicit trailing hole (NO_HOLES feature
4776 * enabled). We deal with it after leaving this loop.
4777 */
4778 if (key.objectid != clone_root->ino ||
4779 key.type != BTRFS_EXTENT_DATA_KEY)
4780 break;
4781
4782 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4783 type = btrfs_file_extent_type(leaf, ei);
4784 if (type == BTRFS_FILE_EXTENT_INLINE) {
4785 ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
4786 ext_len = PAGE_CACHE_ALIGN(ext_len);
4787 } else {
4788 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
4789 }
4790
4791 if (key.offset + ext_len <= clone_root->offset)
4792 goto next;
4793
4794 if (key.offset > clone_root->offset) {
4795 /* Implicit hole, NO_HOLES feature enabled. */
4796 u64 hole_len = key.offset - clone_root->offset;
4797
4798 if (hole_len > len)
4799 hole_len = len;
4800 ret = send_extent_data(sctx, offset, hole_len);
4801 if (ret < 0)
4802 goto out;
4803
4804 len -= hole_len;
4805 if (len == 0)
4806 break;
4807 offset += hole_len;
4808 clone_root->offset += hole_len;
4809 data_offset += hole_len;
4810 }
4811
4812 if (key.offset >= clone_root->offset + len)
4813 break;
4814
4815 clone_len = min_t(u64, ext_len, len);
4816
4817 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
4818 btrfs_file_extent_offset(leaf, ei) == data_offset)
4819 ret = send_clone(sctx, offset, clone_len, clone_root);
4820 else
4821 ret = send_extent_data(sctx, offset, clone_len);
4822
4823 if (ret < 0)
4824 goto out;
4825
4826 len -= clone_len;
4827 if (len == 0)
4828 break;
4829 offset += clone_len;
4830 clone_root->offset += clone_len;
4831 data_offset += clone_len;
4832next:
4833 path->slots[0]++;
4834 }
4835
4836 if (len > 0)
4837 ret = send_extent_data(sctx, offset, len);
4838 else
4839 ret = 0;
4840out:
4841 btrfs_free_path(path);
4842 return ret;
4843}
4844
Alexander Block31db9f72012-07-25 23:19:24 +02004845static int send_write_or_clone(struct send_ctx *sctx,
4846 struct btrfs_path *path,
4847 struct btrfs_key *key,
4848 struct clone_root *clone_root)
4849{
4850 int ret = 0;
4851 struct btrfs_file_extent_item *ei;
4852 u64 offset = key->offset;
Alexander Block31db9f72012-07-25 23:19:24 +02004853 u64 len;
Alexander Block31db9f72012-07-25 23:19:24 +02004854 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004855 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02004856
4857 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4858 struct btrfs_file_extent_item);
4859 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004860 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004861 len = btrfs_file_extent_inline_len(path->nodes[0],
4862 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004863 /*
4864 * it is possible the inline item won't cover the whole page,
4865 * but there may be items after this page. Make
4866 * sure to send the whole thing
4867 */
4868 len = PAGE_CACHE_ALIGN(len);
4869 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004870 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004871 }
Alexander Block31db9f72012-07-25 23:19:24 +02004872
4873 if (offset + len > sctx->cur_inode_size)
4874 len = sctx->cur_inode_size - offset;
4875 if (len == 0) {
4876 ret = 0;
4877 goto out;
4878 }
4879
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004880 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Filipe Mananad906d492015-10-02 10:47:34 +01004881 u64 disk_byte;
4882 u64 data_offset;
4883
4884 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
4885 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
4886 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
4887 offset, len);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004888 } else {
Filipe Mananad906d492015-10-02 10:47:34 +01004889 ret = send_extent_data(sctx, offset, len);
Alexander Block31db9f72012-07-25 23:19:24 +02004890 }
Alexander Block31db9f72012-07-25 23:19:24 +02004891out:
4892 return ret;
4893}
4894
4895static int is_extent_unchanged(struct send_ctx *sctx,
4896 struct btrfs_path *left_path,
4897 struct btrfs_key *ekey)
4898{
4899 int ret = 0;
4900 struct btrfs_key key;
4901 struct btrfs_path *path = NULL;
4902 struct extent_buffer *eb;
4903 int slot;
4904 struct btrfs_key found_key;
4905 struct btrfs_file_extent_item *ei;
4906 u64 left_disknr;
4907 u64 right_disknr;
4908 u64 left_offset;
4909 u64 right_offset;
4910 u64 left_offset_fixed;
4911 u64 left_len;
4912 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04004913 u64 left_gen;
4914 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004915 u8 left_type;
4916 u8 right_type;
4917
4918 path = alloc_path_for_send();
4919 if (!path)
4920 return -ENOMEM;
4921
4922 eb = left_path->nodes[0];
4923 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02004924 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4925 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004926
4927 if (left_type != BTRFS_FILE_EXTENT_REG) {
4928 ret = 0;
4929 goto out;
4930 }
Chris Mason74dd17f2012-08-07 16:25:13 -04004931 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4932 left_len = btrfs_file_extent_num_bytes(eb, ei);
4933 left_offset = btrfs_file_extent_offset(eb, ei);
4934 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004935
4936 /*
4937 * Following comments will refer to these graphics. L is the left
4938 * extents which we are checking at the moment. 1-8 are the right
4939 * extents that we iterate.
4940 *
4941 * |-----L-----|
4942 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4943 *
4944 * |-----L-----|
4945 * |--1--|-2b-|...(same as above)
4946 *
4947 * Alternative situation. Happens on files where extents got split.
4948 * |-----L-----|
4949 * |-----------7-----------|-6-|
4950 *
4951 * Alternative situation. Happens on files which got larger.
4952 * |-----L-----|
4953 * |-8-|
4954 * Nothing follows after 8.
4955 */
4956
4957 key.objectid = ekey->objectid;
4958 key.type = BTRFS_EXTENT_DATA_KEY;
4959 key.offset = ekey->offset;
4960 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4961 if (ret < 0)
4962 goto out;
4963 if (ret) {
4964 ret = 0;
4965 goto out;
4966 }
4967
4968 /*
4969 * Handle special case where the right side has no extents at all.
4970 */
4971 eb = path->nodes[0];
4972 slot = path->slots[0];
4973 btrfs_item_key_to_cpu(eb, &found_key, slot);
4974 if (found_key.objectid != key.objectid ||
4975 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004976 /* If we're a hole then just pretend nothing changed */
4977 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004978 goto out;
4979 }
4980
4981 /*
4982 * We're now on 2a, 2b or 7.
4983 */
4984 key = found_key;
4985 while (key.offset < ekey->offset + left_len) {
4986 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4987 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004988 if (right_type != BTRFS_FILE_EXTENT_REG) {
4989 ret = 0;
4990 goto out;
4991 }
4992
Josef Bacik007d31f2013-10-31 16:49:02 -04004993 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4994 right_len = btrfs_file_extent_num_bytes(eb, ei);
4995 right_offset = btrfs_file_extent_offset(eb, ei);
4996 right_gen = btrfs_file_extent_generation(eb, ei);
4997
Alexander Block31db9f72012-07-25 23:19:24 +02004998 /*
4999 * Are we at extent 8? If yes, we know the extent is changed.
5000 * This may only happen on the first iteration.
5001 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02005002 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04005003 /* If we're a hole just pretend nothing changed */
5004 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005005 goto out;
5006 }
5007
5008 left_offset_fixed = left_offset;
5009 if (key.offset < ekey->offset) {
5010 /* Fix the right offset for 2a and 7. */
5011 right_offset += ekey->offset - key.offset;
5012 } else {
5013 /* Fix the left offset for all behind 2a and 2b */
5014 left_offset_fixed += key.offset - ekey->offset;
5015 }
5016
5017 /*
5018 * Check if we have the same extent.
5019 */
Alexander Block39540962012-08-01 12:46:05 +02005020 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04005021 left_offset_fixed != right_offset ||
5022 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02005023 ret = 0;
5024 goto out;
5025 }
5026
5027 /*
5028 * Go to the next extent.
5029 */
5030 ret = btrfs_next_item(sctx->parent_root, path);
5031 if (ret < 0)
5032 goto out;
5033 if (!ret) {
5034 eb = path->nodes[0];
5035 slot = path->slots[0];
5036 btrfs_item_key_to_cpu(eb, &found_key, slot);
5037 }
5038 if (ret || found_key.objectid != key.objectid ||
5039 found_key.type != key.type) {
5040 key.offset += right_len;
5041 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00005042 }
5043 if (found_key.offset != key.offset + right_len) {
5044 ret = 0;
5045 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005046 }
5047 key = found_key;
5048 }
5049
5050 /*
5051 * We're now behind the left extent (treat as unchanged) or at the end
5052 * of the right side (treat as changed).
5053 */
5054 if (key.offset >= ekey->offset + left_len)
5055 ret = 1;
5056 else
5057 ret = 0;
5058
5059
5060out:
5061 btrfs_free_path(path);
5062 return ret;
5063}
5064
Josef Bacik16e75492013-10-22 12:18:51 -04005065static int get_last_extent(struct send_ctx *sctx, u64 offset)
5066{
5067 struct btrfs_path *path;
5068 struct btrfs_root *root = sctx->send_root;
5069 struct btrfs_file_extent_item *fi;
5070 struct btrfs_key key;
5071 u64 extent_end;
5072 u8 type;
5073 int ret;
5074
5075 path = alloc_path_for_send();
5076 if (!path)
5077 return -ENOMEM;
5078
5079 sctx->cur_inode_last_extent = 0;
5080
5081 key.objectid = sctx->cur_ino;
5082 key.type = BTRFS_EXTENT_DATA_KEY;
5083 key.offset = offset;
5084 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5085 if (ret < 0)
5086 goto out;
5087 ret = 0;
5088 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5089 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5090 goto out;
5091
5092 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5093 struct btrfs_file_extent_item);
5094 type = btrfs_file_extent_type(path->nodes[0], fi);
5095 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005096 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5097 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005098 extent_end = ALIGN(key.offset + size,
5099 sctx->send_root->sectorsize);
5100 } else {
5101 extent_end = key.offset +
5102 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5103 }
5104 sctx->cur_inode_last_extent = extent_end;
5105out:
5106 btrfs_free_path(path);
5107 return ret;
5108}
5109
5110static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5111 struct btrfs_key *key)
5112{
5113 struct btrfs_file_extent_item *fi;
5114 u64 extent_end;
5115 u8 type;
5116 int ret = 0;
5117
5118 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5119 return 0;
5120
5121 if (sctx->cur_inode_last_extent == (u64)-1) {
5122 ret = get_last_extent(sctx, key->offset - 1);
5123 if (ret)
5124 return ret;
5125 }
5126
5127 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5128 struct btrfs_file_extent_item);
5129 type = btrfs_file_extent_type(path->nodes[0], fi);
5130 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08005131 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
5132 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04005133 extent_end = ALIGN(key->offset + size,
5134 sctx->send_root->sectorsize);
5135 } else {
5136 extent_end = key->offset +
5137 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5138 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00005139
5140 if (path->slots[0] == 0 &&
5141 sctx->cur_inode_last_extent < key->offset) {
5142 /*
5143 * We might have skipped entire leafs that contained only
5144 * file extent items for our current inode. These leafs have
5145 * a generation number smaller (older) than the one in the
5146 * current leaf and the leaf our last extent came from, and
5147 * are located between these 2 leafs.
5148 */
5149 ret = get_last_extent(sctx, key->offset - 1);
5150 if (ret)
5151 return ret;
5152 }
5153
Josef Bacik16e75492013-10-22 12:18:51 -04005154 if (sctx->cur_inode_last_extent < key->offset)
5155 ret = send_hole(sctx, key->offset);
5156 sctx->cur_inode_last_extent = extent_end;
5157 return ret;
5158}
5159
Alexander Block31db9f72012-07-25 23:19:24 +02005160static int process_extent(struct send_ctx *sctx,
5161 struct btrfs_path *path,
5162 struct btrfs_key *key)
5163{
Alexander Block31db9f72012-07-25 23:19:24 +02005164 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04005165 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005166
5167 if (S_ISLNK(sctx->cur_inode_mode))
5168 return 0;
5169
5170 if (sctx->parent_root && !sctx->cur_inode_new) {
5171 ret = is_extent_unchanged(sctx, path, key);
5172 if (ret < 0)
5173 goto out;
5174 if (ret) {
5175 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005176 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02005177 }
Josef Bacik57cfd462013-08-20 15:55:39 -04005178 } else {
5179 struct btrfs_file_extent_item *ei;
5180 u8 type;
5181
5182 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5183 struct btrfs_file_extent_item);
5184 type = btrfs_file_extent_type(path->nodes[0], ei);
5185 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5186 type == BTRFS_FILE_EXTENT_REG) {
5187 /*
5188 * The send spec does not have a prealloc command yet,
5189 * so just leave a hole for prealloc'ed extents until
5190 * we have enough commands queued up to justify rev'ing
5191 * the send spec.
5192 */
5193 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5194 ret = 0;
5195 goto out;
5196 }
5197
5198 /* Have a hole, just skip it. */
5199 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5200 ret = 0;
5201 goto out;
5202 }
5203 }
Alexander Block31db9f72012-07-25 23:19:24 +02005204 }
5205
5206 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5207 sctx->cur_inode_size, &found_clone);
5208 if (ret != -ENOENT && ret < 0)
5209 goto out;
5210
5211 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04005212 if (ret)
5213 goto out;
5214out_hole:
5215 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02005216out:
5217 return ret;
5218}
5219
5220static int process_all_extents(struct send_ctx *sctx)
5221{
5222 int ret;
5223 struct btrfs_root *root;
5224 struct btrfs_path *path;
5225 struct btrfs_key key;
5226 struct btrfs_key found_key;
5227 struct extent_buffer *eb;
5228 int slot;
5229
5230 root = sctx->send_root;
5231 path = alloc_path_for_send();
5232 if (!path)
5233 return -ENOMEM;
5234
5235 key.objectid = sctx->cmp_key->objectid;
5236 key.type = BTRFS_EXTENT_DATA_KEY;
5237 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005238 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5239 if (ret < 0)
5240 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005241
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005242 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005243 eb = path->nodes[0];
5244 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005245
5246 if (slot >= btrfs_header_nritems(eb)) {
5247 ret = btrfs_next_leaf(root, path);
5248 if (ret < 0) {
5249 goto out;
5250 } else if (ret > 0) {
5251 ret = 0;
5252 break;
5253 }
5254 continue;
5255 }
5256
Alexander Block31db9f72012-07-25 23:19:24 +02005257 btrfs_item_key_to_cpu(eb, &found_key, slot);
5258
5259 if (found_key.objectid != key.objectid ||
5260 found_key.type != key.type) {
5261 ret = 0;
5262 goto out;
5263 }
5264
5265 ret = process_extent(sctx, path, &found_key);
5266 if (ret < 0)
5267 goto out;
5268
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00005269 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02005270 }
5271
5272out:
5273 btrfs_free_path(path);
5274 return ret;
5275}
5276
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005277static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5278 int *pending_move,
5279 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02005280{
5281 int ret = 0;
5282
5283 if (sctx->cur_ino == 0)
5284 goto out;
5285 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005286 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005287 goto out;
5288 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5289 goto out;
5290
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005291 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02005292 if (ret < 0)
5293 goto out;
5294
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005295 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005296out:
5297 return ret;
5298}
5299
5300static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5301{
5302 int ret = 0;
5303 u64 left_mode;
5304 u64 left_uid;
5305 u64 left_gid;
5306 u64 right_mode;
5307 u64 right_uid;
5308 u64 right_gid;
5309 int need_chmod = 0;
5310 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005311 int pending_move = 0;
5312 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02005313
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005314 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5315 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02005316 if (ret < 0)
5317 goto out;
5318
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005319 /*
5320 * We have processed the refs and thus need to advance send_progress.
5321 * Now, calls to get_cur_xxx will take the updated refs of the current
5322 * inode into account.
5323 *
5324 * On the other hand, if our current inode is a directory and couldn't
5325 * be moved/renamed because its parent was renamed/moved too and it has
5326 * a higher inode number, we can only move/rename our current inode
5327 * after we moved/renamed its parent. Therefore in this case operate on
5328 * the old path (pre move/rename) of our current inode, and the
5329 * move/rename will be performed later.
5330 */
5331 if (refs_processed && !pending_move)
5332 sctx->send_progress = sctx->cur_ino + 1;
5333
Alexander Block31db9f72012-07-25 23:19:24 +02005334 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5335 goto out;
5336 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5337 goto out;
5338
5339 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02005340 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02005341 if (ret < 0)
5342 goto out;
5343
Alex Lyakase2d044f2012-10-17 13:52:47 +00005344 if (!sctx->parent_root || sctx->cur_inode_new) {
5345 need_chown = 1;
5346 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005347 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005348 } else {
5349 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5350 NULL, NULL, &right_mode, &right_uid,
5351 &right_gid, NULL);
5352 if (ret < 0)
5353 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005354
Alex Lyakase2d044f2012-10-17 13:52:47 +00005355 if (left_uid != right_uid || left_gid != right_gid)
5356 need_chown = 1;
5357 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5358 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005359 }
5360
5361 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005362 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005363 if (sctx->cur_inode_last_extent == (u64)-1 ||
5364 sctx->cur_inode_last_extent <
5365 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005366 ret = get_last_extent(sctx, (u64)-1);
5367 if (ret)
5368 goto out;
5369 }
5370 if (sctx->cur_inode_last_extent <
5371 sctx->cur_inode_size) {
5372 ret = send_hole(sctx, sctx->cur_inode_size);
5373 if (ret)
5374 goto out;
5375 }
5376 }
Alexander Block31db9f72012-07-25 23:19:24 +02005377 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5378 sctx->cur_inode_size);
5379 if (ret < 0)
5380 goto out;
5381 }
5382
5383 if (need_chown) {
5384 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5385 left_uid, left_gid);
5386 if (ret < 0)
5387 goto out;
5388 }
5389 if (need_chmod) {
5390 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5391 left_mode);
5392 if (ret < 0)
5393 goto out;
5394 }
5395
5396 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005397 * If other directory inodes depended on our current directory
5398 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005399 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005400 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5401 ret = apply_children_dir_moves(sctx);
5402 if (ret)
5403 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005404 /*
5405 * Need to send that every time, no matter if it actually
5406 * changed between the two trees as we have done changes to
5407 * the inode before. If our inode is a directory and it's
5408 * waiting to be moved/renamed, we will send its utimes when
5409 * it's moved/renamed, therefore we don't need to do it here.
5410 */
5411 sctx->send_progress = sctx->cur_ino + 1;
5412 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5413 if (ret < 0)
5414 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005415 }
5416
Alexander Block31db9f72012-07-25 23:19:24 +02005417out:
5418 return ret;
5419}
5420
5421static int changed_inode(struct send_ctx *sctx,
5422 enum btrfs_compare_tree_result result)
5423{
5424 int ret = 0;
5425 struct btrfs_key *key = sctx->cmp_key;
5426 struct btrfs_inode_item *left_ii = NULL;
5427 struct btrfs_inode_item *right_ii = NULL;
5428 u64 left_gen = 0;
5429 u64 right_gen = 0;
5430
Alexander Block31db9f72012-07-25 23:19:24 +02005431 sctx->cur_ino = key->objectid;
5432 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005433 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005434
5435 /*
5436 * Set send_progress to current inode. This will tell all get_cur_xxx
5437 * functions that the current inode's refs are not updated yet. Later,
5438 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5439 */
Alexander Block31db9f72012-07-25 23:19:24 +02005440 sctx->send_progress = sctx->cur_ino;
5441
5442 if (result == BTRFS_COMPARE_TREE_NEW ||
5443 result == BTRFS_COMPARE_TREE_CHANGED) {
5444 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5445 sctx->left_path->slots[0],
5446 struct btrfs_inode_item);
5447 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5448 left_ii);
5449 } else {
5450 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5451 sctx->right_path->slots[0],
5452 struct btrfs_inode_item);
5453 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5454 right_ii);
5455 }
5456 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5457 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5458 sctx->right_path->slots[0],
5459 struct btrfs_inode_item);
5460
5461 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5462 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005463
5464 /*
5465 * The cur_ino = root dir case is special here. We can't treat
5466 * the inode as deleted+reused because it would generate a
5467 * stream that tries to delete/mkdir the root dir.
5468 */
5469 if (left_gen != right_gen &&
5470 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005471 sctx->cur_inode_new_gen = 1;
5472 }
5473
5474 if (result == BTRFS_COMPARE_TREE_NEW) {
5475 sctx->cur_inode_gen = left_gen;
5476 sctx->cur_inode_new = 1;
5477 sctx->cur_inode_deleted = 0;
5478 sctx->cur_inode_size = btrfs_inode_size(
5479 sctx->left_path->nodes[0], left_ii);
5480 sctx->cur_inode_mode = btrfs_inode_mode(
5481 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005482 sctx->cur_inode_rdev = btrfs_inode_rdev(
5483 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005484 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005485 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005486 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5487 sctx->cur_inode_gen = right_gen;
5488 sctx->cur_inode_new = 0;
5489 sctx->cur_inode_deleted = 1;
5490 sctx->cur_inode_size = btrfs_inode_size(
5491 sctx->right_path->nodes[0], right_ii);
5492 sctx->cur_inode_mode = btrfs_inode_mode(
5493 sctx->right_path->nodes[0], right_ii);
5494 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005495 /*
5496 * We need to do some special handling in case the inode was
5497 * reported as changed with a changed generation number. This
5498 * means that the original inode was deleted and new inode
5499 * reused the same inum. So we have to treat the old inode as
5500 * deleted and the new one as new.
5501 */
Alexander Block31db9f72012-07-25 23:19:24 +02005502 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005503 /*
5504 * First, process the inode as if it was deleted.
5505 */
Alexander Block31db9f72012-07-25 23:19:24 +02005506 sctx->cur_inode_gen = right_gen;
5507 sctx->cur_inode_new = 0;
5508 sctx->cur_inode_deleted = 1;
5509 sctx->cur_inode_size = btrfs_inode_size(
5510 sctx->right_path->nodes[0], right_ii);
5511 sctx->cur_inode_mode = btrfs_inode_mode(
5512 sctx->right_path->nodes[0], right_ii);
5513 ret = process_all_refs(sctx,
5514 BTRFS_COMPARE_TREE_DELETED);
5515 if (ret < 0)
5516 goto out;
5517
Alexander Block766702e2012-07-28 14:11:31 +02005518 /*
5519 * Now process the inode as if it was new.
5520 */
Alexander Block31db9f72012-07-25 23:19:24 +02005521 sctx->cur_inode_gen = left_gen;
5522 sctx->cur_inode_new = 1;
5523 sctx->cur_inode_deleted = 0;
5524 sctx->cur_inode_size = btrfs_inode_size(
5525 sctx->left_path->nodes[0], left_ii);
5526 sctx->cur_inode_mode = btrfs_inode_mode(
5527 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005528 sctx->cur_inode_rdev = btrfs_inode_rdev(
5529 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005530 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005531 if (ret < 0)
5532 goto out;
5533
5534 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5535 if (ret < 0)
5536 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005537 /*
5538 * Advance send_progress now as we did not get into
5539 * process_recorded_refs_if_needed in the new_gen case.
5540 */
5541 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005542
5543 /*
5544 * Now process all extents and xattrs of the inode as if
5545 * they were all new.
5546 */
Alexander Block31db9f72012-07-25 23:19:24 +02005547 ret = process_all_extents(sctx);
5548 if (ret < 0)
5549 goto out;
5550 ret = process_all_new_xattrs(sctx);
5551 if (ret < 0)
5552 goto out;
5553 } else {
5554 sctx->cur_inode_gen = left_gen;
5555 sctx->cur_inode_new = 0;
5556 sctx->cur_inode_new_gen = 0;
5557 sctx->cur_inode_deleted = 0;
5558 sctx->cur_inode_size = btrfs_inode_size(
5559 sctx->left_path->nodes[0], left_ii);
5560 sctx->cur_inode_mode = btrfs_inode_mode(
5561 sctx->left_path->nodes[0], left_ii);
5562 }
5563 }
5564
5565out:
5566 return ret;
5567}
5568
Alexander Block766702e2012-07-28 14:11:31 +02005569/*
5570 * We have to process new refs before deleted refs, but compare_trees gives us
5571 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5572 * first and later process them in process_recorded_refs.
5573 * For the cur_inode_new_gen case, we skip recording completely because
5574 * changed_inode did already initiate processing of refs. The reason for this is
5575 * that in this case, compare_tree actually compares the refs of 2 different
5576 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5577 * refs of the right tree as deleted and all refs of the left tree as new.
5578 */
Alexander Block31db9f72012-07-25 23:19:24 +02005579static int changed_ref(struct send_ctx *sctx,
5580 enum btrfs_compare_tree_result result)
5581{
5582 int ret = 0;
5583
5584 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5585
5586 if (!sctx->cur_inode_new_gen &&
5587 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5588 if (result == BTRFS_COMPARE_TREE_NEW)
5589 ret = record_new_ref(sctx);
5590 else if (result == BTRFS_COMPARE_TREE_DELETED)
5591 ret = record_deleted_ref(sctx);
5592 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5593 ret = record_changed_ref(sctx);
5594 }
5595
5596 return ret;
5597}
5598
Alexander Block766702e2012-07-28 14:11:31 +02005599/*
5600 * Process new/deleted/changed xattrs. We skip processing in the
5601 * cur_inode_new_gen case because changed_inode did already initiate processing
5602 * of xattrs. The reason is the same as in changed_ref
5603 */
Alexander Block31db9f72012-07-25 23:19:24 +02005604static int changed_xattr(struct send_ctx *sctx,
5605 enum btrfs_compare_tree_result result)
5606{
5607 int ret = 0;
5608
5609 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5610
5611 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5612 if (result == BTRFS_COMPARE_TREE_NEW)
5613 ret = process_new_xattr(sctx);
5614 else if (result == BTRFS_COMPARE_TREE_DELETED)
5615 ret = process_deleted_xattr(sctx);
5616 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5617 ret = process_changed_xattr(sctx);
5618 }
5619
5620 return ret;
5621}
5622
Alexander Block766702e2012-07-28 14:11:31 +02005623/*
5624 * Process new/deleted/changed extents. We skip processing in the
5625 * cur_inode_new_gen case because changed_inode did already initiate processing
5626 * of extents. The reason is the same as in changed_ref
5627 */
Alexander Block31db9f72012-07-25 23:19:24 +02005628static int changed_extent(struct send_ctx *sctx,
5629 enum btrfs_compare_tree_result result)
5630{
5631 int ret = 0;
5632
5633 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5634
5635 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5636 if (result != BTRFS_COMPARE_TREE_DELETED)
5637 ret = process_extent(sctx, sctx->left_path,
5638 sctx->cmp_key);
5639 }
5640
5641 return ret;
5642}
5643
Josef Bacikba5e8f22013-08-16 16:52:55 -04005644static int dir_changed(struct send_ctx *sctx, u64 dir)
5645{
5646 u64 orig_gen, new_gen;
5647 int ret;
5648
5649 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5650 NULL, NULL);
5651 if (ret)
5652 return ret;
5653
5654 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5655 NULL, NULL, NULL);
5656 if (ret)
5657 return ret;
5658
5659 return (orig_gen != new_gen) ? 1 : 0;
5660}
5661
5662static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5663 struct btrfs_key *key)
5664{
5665 struct btrfs_inode_extref *extref;
5666 struct extent_buffer *leaf;
5667 u64 dirid = 0, last_dirid = 0;
5668 unsigned long ptr;
5669 u32 item_size;
5670 u32 cur_offset = 0;
5671 int ref_name_len;
5672 int ret = 0;
5673
5674 /* Easy case, just check this one dirid */
5675 if (key->type == BTRFS_INODE_REF_KEY) {
5676 dirid = key->offset;
5677
5678 ret = dir_changed(sctx, dirid);
5679 goto out;
5680 }
5681
5682 leaf = path->nodes[0];
5683 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5684 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5685 while (cur_offset < item_size) {
5686 extref = (struct btrfs_inode_extref *)(ptr +
5687 cur_offset);
5688 dirid = btrfs_inode_extref_parent(leaf, extref);
5689 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5690 cur_offset += ref_name_len + sizeof(*extref);
5691 if (dirid == last_dirid)
5692 continue;
5693 ret = dir_changed(sctx, dirid);
5694 if (ret)
5695 break;
5696 last_dirid = dirid;
5697 }
5698out:
5699 return ret;
5700}
5701
Alexander Block766702e2012-07-28 14:11:31 +02005702/*
5703 * Updates compare related fields in sctx and simply forwards to the actual
5704 * changed_xxx functions.
5705 */
Alexander Block31db9f72012-07-25 23:19:24 +02005706static int changed_cb(struct btrfs_root *left_root,
5707 struct btrfs_root *right_root,
5708 struct btrfs_path *left_path,
5709 struct btrfs_path *right_path,
5710 struct btrfs_key *key,
5711 enum btrfs_compare_tree_result result,
5712 void *ctx)
5713{
5714 int ret = 0;
5715 struct send_ctx *sctx = ctx;
5716
Josef Bacikba5e8f22013-08-16 16:52:55 -04005717 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005718 if (key->type == BTRFS_INODE_REF_KEY ||
5719 key->type == BTRFS_INODE_EXTREF_KEY) {
5720 ret = compare_refs(sctx, left_path, key);
5721 if (!ret)
5722 return 0;
5723 if (ret < 0)
5724 return ret;
5725 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5726 return maybe_send_hole(sctx, left_path, key);
5727 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005728 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005729 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005730 result = BTRFS_COMPARE_TREE_CHANGED;
5731 ret = 0;
5732 }
5733
Alexander Block31db9f72012-07-25 23:19:24 +02005734 sctx->left_path = left_path;
5735 sctx->right_path = right_path;
5736 sctx->cmp_key = key;
5737
5738 ret = finish_inode_if_needed(sctx, 0);
5739 if (ret < 0)
5740 goto out;
5741
Alexander Block2981e222012-08-01 14:47:03 +02005742 /* Ignore non-FS objects */
5743 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5744 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5745 goto out;
5746
Alexander Block31db9f72012-07-25 23:19:24 +02005747 if (key->type == BTRFS_INODE_ITEM_KEY)
5748 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005749 else if (key->type == BTRFS_INODE_REF_KEY ||
5750 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005751 ret = changed_ref(sctx, result);
5752 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5753 ret = changed_xattr(sctx, result);
5754 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5755 ret = changed_extent(sctx, result);
5756
5757out:
5758 return ret;
5759}
5760
5761static int full_send_tree(struct send_ctx *sctx)
5762{
5763 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005764 struct btrfs_root *send_root = sctx->send_root;
5765 struct btrfs_key key;
5766 struct btrfs_key found_key;
5767 struct btrfs_path *path;
5768 struct extent_buffer *eb;
5769 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02005770
5771 path = alloc_path_for_send();
5772 if (!path)
5773 return -ENOMEM;
5774
Alexander Block31db9f72012-07-25 23:19:24 +02005775 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5776 key.type = BTRFS_INODE_ITEM_KEY;
5777 key.offset = 0;
5778
Alexander Block31db9f72012-07-25 23:19:24 +02005779 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5780 if (ret < 0)
5781 goto out;
5782 if (ret)
5783 goto out_finish;
5784
5785 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005786 eb = path->nodes[0];
5787 slot = path->slots[0];
5788 btrfs_item_key_to_cpu(eb, &found_key, slot);
5789
5790 ret = changed_cb(send_root, NULL, path, NULL,
5791 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5792 if (ret < 0)
5793 goto out;
5794
5795 key.objectid = found_key.objectid;
5796 key.type = found_key.type;
5797 key.offset = found_key.offset + 1;
5798
5799 ret = btrfs_next_item(send_root, path);
5800 if (ret < 0)
5801 goto out;
5802 if (ret) {
5803 ret = 0;
5804 break;
5805 }
5806 }
5807
5808out_finish:
5809 ret = finish_inode_if_needed(sctx, 1);
5810
5811out:
5812 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02005813 return ret;
5814}
5815
5816static int send_subvol(struct send_ctx *sctx)
5817{
5818 int ret;
5819
Stefan Behrensc2c71322013-04-10 17:10:52 +00005820 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5821 ret = send_header(sctx);
5822 if (ret < 0)
5823 goto out;
5824 }
Alexander Block31db9f72012-07-25 23:19:24 +02005825
5826 ret = send_subvol_begin(sctx);
5827 if (ret < 0)
5828 goto out;
5829
5830 if (sctx->parent_root) {
5831 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5832 changed_cb, sctx);
5833 if (ret < 0)
5834 goto out;
5835 ret = finish_inode_if_needed(sctx, 1);
5836 if (ret < 0)
5837 goto out;
5838 } else {
5839 ret = full_send_tree(sctx);
5840 if (ret < 0)
5841 goto out;
5842 }
5843
5844out:
Alexander Block31db9f72012-07-25 23:19:24 +02005845 free_recorded_refs(sctx);
5846 return ret;
5847}
5848
Filipe Mananae5fa8f82014-10-21 11:11:41 +01005849/*
5850 * If orphan cleanup did remove any orphans from a root, it means the tree
5851 * was modified and therefore the commit root is not the same as the current
5852 * root anymore. This is a problem, because send uses the commit root and
5853 * therefore can see inode items that don't exist in the current root anymore,
5854 * and for example make calls to btrfs_iget, which will do tree lookups based
5855 * on the current root and not on the commit root. Those lookups will fail,
5856 * returning a -ESTALE error, and making send fail with that error. So make
5857 * sure a send does not see any orphans we have just removed, and that it will
5858 * see the same inodes regardless of whether a transaction commit happened
5859 * before it started (meaning that the commit root will be the same as the
5860 * current root) or not.
5861 */
5862static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
5863{
5864 int i;
5865 struct btrfs_trans_handle *trans = NULL;
5866
5867again:
5868 if (sctx->parent_root &&
5869 sctx->parent_root->node != sctx->parent_root->commit_root)
5870 goto commit_trans;
5871
5872 for (i = 0; i < sctx->clone_roots_cnt; i++)
5873 if (sctx->clone_roots[i].root->node !=
5874 sctx->clone_roots[i].root->commit_root)
5875 goto commit_trans;
5876
5877 if (trans)
5878 return btrfs_end_transaction(trans, sctx->send_root);
5879
5880 return 0;
5881
5882commit_trans:
5883 /* Use any root, all fs roots will get their commit roots updated. */
5884 if (!trans) {
5885 trans = btrfs_join_transaction(sctx->send_root);
5886 if (IS_ERR(trans))
5887 return PTR_ERR(trans);
5888 goto again;
5889 }
5890
5891 return btrfs_commit_transaction(trans, sctx->send_root);
5892}
5893
David Sterba66ef7d62013-12-17 15:07:20 +01005894static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5895{
5896 spin_lock(&root->root_item_lock);
5897 root->send_in_progress--;
5898 /*
5899 * Not much left to do, we don't know why it's unbalanced and
5900 * can't blindly reset it to 0.
5901 */
5902 if (root->send_in_progress < 0)
5903 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02005904 "send_in_progres unbalanced %d root %llu",
David Sterba66ef7d62013-12-17 15:07:20 +01005905 root->send_in_progress, root->root_key.objectid);
5906 spin_unlock(&root->root_item_lock);
5907}
5908
Alexander Block31db9f72012-07-25 23:19:24 +02005909long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5910{
5911 int ret = 0;
5912 struct btrfs_root *send_root;
5913 struct btrfs_root *clone_root;
5914 struct btrfs_fs_info *fs_info;
5915 struct btrfs_ioctl_send_args *arg = NULL;
5916 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02005917 struct send_ctx *sctx = NULL;
5918 u32 i;
5919 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01005920 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08005921 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08005922 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02005923
5924 if (!capable(CAP_SYS_ADMIN))
5925 return -EPERM;
5926
Al Viro496ad9a2013-01-23 17:07:38 -05005927 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02005928 fs_info = send_root->fs_info;
5929
Josef Bacik139f8072013-05-20 11:26:50 -04005930 /*
David Sterba2c686532013-12-16 17:34:17 +01005931 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02005932 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01005933 */
5934 spin_lock(&send_root->root_item_lock);
5935 send_root->send_in_progress++;
5936 spin_unlock(&send_root->root_item_lock);
5937
5938 /*
Josef Bacik139f8072013-05-20 11:26:50 -04005939 * This is done when we lookup the root, it should already be complete
5940 * by the time we get here.
5941 */
5942 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5943
5944 /*
David Sterba2c686532013-12-16 17:34:17 +01005945 * Userspace tools do the checks and warn the user if it's
5946 * not RO.
5947 */
5948 if (!btrfs_root_readonly(send_root)) {
5949 ret = -EPERM;
5950 goto out;
5951 }
5952
Alexander Block31db9f72012-07-25 23:19:24 +02005953 arg = memdup_user(arg_, sizeof(*arg));
5954 if (IS_ERR(arg)) {
5955 ret = PTR_ERR(arg);
5956 arg = NULL;
5957 goto out;
5958 }
5959
5960 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05005961 sizeof(*arg->clone_sources) *
5962 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02005963 ret = -EFAULT;
5964 goto out;
5965 }
5966
Stefan Behrensc2c71322013-04-10 17:10:52 +00005967 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005968 ret = -EINVAL;
5969 goto out;
5970 }
5971
Alexander Block31db9f72012-07-25 23:19:24 +02005972 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5973 if (!sctx) {
5974 ret = -ENOMEM;
5975 goto out;
5976 }
5977
5978 INIT_LIST_HEAD(&sctx->new_refs);
5979 INIT_LIST_HEAD(&sctx->deleted_refs);
5980 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5981 INIT_LIST_HEAD(&sctx->name_cache_list);
5982
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005983 sctx->flags = arg->flags;
5984
Alexander Block31db9f72012-07-25 23:19:24 +02005985 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00005986 if (!sctx->send_filp) {
5987 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02005988 goto out;
5989 }
5990
Alexander Block31db9f72012-07-25 23:19:24 +02005991 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02005992 /*
5993 * Unlikely but possible, if the subvolume is marked for deletion but
5994 * is slow to remove the directory entry, send can still be started
5995 */
5996 if (btrfs_root_dead(sctx->send_root)) {
5997 ret = -EPERM;
5998 goto out;
5999 }
6000
Alexander Block31db9f72012-07-25 23:19:24 +02006001 sctx->clone_roots_cnt = arg->clone_sources_count;
6002
6003 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
6004 sctx->send_buf = vmalloc(sctx->send_max_size);
6005 if (!sctx->send_buf) {
6006 ret = -ENOMEM;
6007 goto out;
6008 }
6009
6010 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
6011 if (!sctx->read_buf) {
6012 ret = -ENOMEM;
6013 goto out;
6014 }
6015
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006016 sctx->pending_dir_moves = RB_ROOT;
6017 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00006018 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006019
Alexander Block31db9f72012-07-25 23:19:24 +02006020 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
6021 (arg->clone_sources_count + 1));
6022 if (!sctx->clone_roots) {
6023 ret = -ENOMEM;
6024 goto out;
6025 }
6026
6027 if (arg->clone_sources_count) {
6028 clone_sources_tmp = vmalloc(arg->clone_sources_count *
6029 sizeof(*arg->clone_sources));
6030 if (!clone_sources_tmp) {
6031 ret = -ENOMEM;
6032 goto out;
6033 }
6034
6035 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
6036 arg->clone_sources_count *
6037 sizeof(*arg->clone_sources));
6038 if (ret) {
6039 ret = -EFAULT;
6040 goto out;
6041 }
6042
6043 for (i = 0; i < arg->clone_sources_count; i++) {
6044 key.objectid = clone_sources_tmp[i];
6045 key.type = BTRFS_ROOT_ITEM_KEY;
6046 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006047
6048 index = srcu_read_lock(&fs_info->subvol_srcu);
6049
Alexander Block31db9f72012-07-25 23:19:24 +02006050 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02006051 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006052 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006053 ret = PTR_ERR(clone_root);
6054 goto out;
6055 }
David Sterba2c686532013-12-16 17:34:17 +01006056 spin_lock(&clone_root->root_item_lock);
Filipe Manana5cc2b172015-03-02 20:53:52 +00006057 if (!btrfs_root_readonly(clone_root) ||
6058 btrfs_root_dead(clone_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006059 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006060 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006061 ret = -EPERM;
6062 goto out;
6063 }
Filipe Manana2f1f4652015-03-02 20:53:53 +00006064 clone_root->send_in_progress++;
David Sterba2c686532013-12-16 17:34:17 +01006065 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006066 srcu_read_unlock(&fs_info->subvol_srcu, index);
6067
Alexander Block31db9f72012-07-25 23:19:24 +02006068 sctx->clone_roots[i].root = clone_root;
Filipe Manana2f1f4652015-03-02 20:53:53 +00006069 clone_sources_to_rollback = i + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006070 }
6071 vfree(clone_sources_tmp);
6072 clone_sources_tmp = NULL;
6073 }
6074
6075 if (arg->parent_root) {
6076 key.objectid = arg->parent_root;
6077 key.type = BTRFS_ROOT_ITEM_KEY;
6078 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08006079
6080 index = srcu_read_lock(&fs_info->subvol_srcu);
6081
Alexander Block31db9f72012-07-25 23:19:24 +02006082 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006083 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08006084 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00006085 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02006086 goto out;
6087 }
Wang Shilong18f687d2014-01-07 17:25:19 +08006088
David Sterba2c686532013-12-16 17:34:17 +01006089 spin_lock(&sctx->parent_root->root_item_lock);
6090 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02006091 if (!btrfs_root_readonly(sctx->parent_root) ||
6092 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01006093 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006094 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01006095 ret = -EPERM;
6096 goto out;
6097 }
6098 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08006099
6100 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02006101 }
6102
6103 /*
6104 * Clones from send_root are allowed, but only if the clone source
6105 * is behind the current send position. This is checked while searching
6106 * for possible clone sources.
6107 */
6108 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6109
6110 /* We do a bsearch later */
6111 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6112 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6113 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08006114 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02006115
Filipe Mananae5fa8f82014-10-21 11:11:41 +01006116 ret = ensure_commit_roots_uptodate(sctx);
6117 if (ret)
6118 goto out;
6119
David Sterba2755a0d2014-07-31 00:43:18 +02006120 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02006121 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04006122 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02006123 if (ret < 0)
6124 goto out;
6125
Stefan Behrensc2c71322013-04-10 17:10:52 +00006126 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6127 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6128 if (ret < 0)
6129 goto out;
6130 ret = send_cmd(sctx);
6131 if (ret < 0)
6132 goto out;
6133 }
Alexander Block31db9f72012-07-25 23:19:24 +02006134
6135out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00006136 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6137 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6138 struct rb_node *n;
6139 struct pending_dir_move *pm;
6140
6141 n = rb_first(&sctx->pending_dir_moves);
6142 pm = rb_entry(n, struct pending_dir_move, node);
6143 while (!list_empty(&pm->list)) {
6144 struct pending_dir_move *pm2;
6145
6146 pm2 = list_first_entry(&pm->list,
6147 struct pending_dir_move, list);
6148 free_pending_move(sctx, pm2);
6149 }
6150 free_pending_move(sctx, pm);
6151 }
6152
6153 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6154 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6155 struct rb_node *n;
6156 struct waiting_dir_move *dm;
6157
6158 n = rb_first(&sctx->waiting_dir_moves);
6159 dm = rb_entry(n, struct waiting_dir_move, node);
6160 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6161 kfree(dm);
6162 }
6163
Filipe Manana9dc44212014-02-19 14:31:44 +00006164 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6165 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6166 struct rb_node *n;
6167 struct orphan_dir_info *odi;
6168
6169 n = rb_first(&sctx->orphan_dirs);
6170 odi = rb_entry(n, struct orphan_dir_info, node);
6171 free_orphan_dir_info(sctx, odi);
6172 }
6173
Wang Shilong896c14f2014-01-07 17:25:18 +08006174 if (sort_clone_roots) {
6175 for (i = 0; i < sctx->clone_roots_cnt; i++)
6176 btrfs_root_dec_send_in_progress(
6177 sctx->clone_roots[i].root);
6178 } else {
6179 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6180 btrfs_root_dec_send_in_progress(
6181 sctx->clone_roots[i].root);
6182
6183 btrfs_root_dec_send_in_progress(send_root);
6184 }
David Sterba66ef7d62013-12-17 15:07:20 +01006185 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6186 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01006187
Alexander Block31db9f72012-07-25 23:19:24 +02006188 kfree(arg);
6189 vfree(clone_sources_tmp);
6190
6191 if (sctx) {
6192 if (sctx->send_filp)
6193 fput(sctx->send_filp);
6194
6195 vfree(sctx->clone_roots);
6196 vfree(sctx->send_buf);
6197 vfree(sctx->read_buf);
6198
6199 name_cache_free(sctx);
6200
6201 kfree(sctx);
6202 }
6203
6204 return ret;
6205}