blob: 15cdc67ce9ad7bdf5db2b07a2c9ea2d6c7636d83 [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;
233 struct list_head update_refs;
234};
235
236struct waiting_dir_move {
237 struct rb_node node;
238 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000239 /*
240 * There might be some directory that could not be removed because it
241 * was waiting for this directory inode to be moved first. Therefore
242 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
243 */
244 u64 rmdir_ino;
245};
246
247struct orphan_dir_info {
248 struct rb_node node;
249 u64 ino;
250 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +0200251};
252
253struct name_cache_entry {
254 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200255 /*
256 * radix_tree has only 32bit entries but we need to handle 64bit inums.
257 * We use the lower 32bit of the 64bit inum to store it in the tree. If
258 * more then one inum would fall into the same entry, we use radix_list
259 * to store the additional entries. radix_list is also used to store
260 * entries where two entries have the same inum but different
261 * generations.
262 */
263 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200264 u64 ino;
265 u64 gen;
266 u64 parent_ino;
267 u64 parent_gen;
268 int ret;
269 int need_later_update;
270 int name_len;
271 char name[];
272};
273
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000274static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
275
Filipe Manana9dc44212014-02-19 14:31:44 +0000276static struct waiting_dir_move *
277get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
278
279static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
280
Josef Bacik16e75492013-10-22 12:18:51 -0400281static int need_send_hole(struct send_ctx *sctx)
282{
283 return (sctx->parent_root && !sctx->cur_inode_new &&
284 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
285 S_ISREG(sctx->cur_inode_mode));
286}
287
Alexander Block31db9f72012-07-25 23:19:24 +0200288static void fs_path_reset(struct fs_path *p)
289{
290 if (p->reversed) {
291 p->start = p->buf + p->buf_len - 1;
292 p->end = p->start;
293 *p->start = 0;
294 } else {
295 p->start = p->buf;
296 p->end = p->start;
297 *p->start = 0;
298 }
299}
300
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000301static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200302{
303 struct fs_path *p;
304
305 p = kmalloc(sizeof(*p), GFP_NOFS);
306 if (!p)
307 return NULL;
308 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200309 p->buf = p->inline_buf;
310 p->buf_len = FS_PATH_INLINE_SIZE;
311 fs_path_reset(p);
312 return p;
313}
314
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000315static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200316{
317 struct fs_path *p;
318
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000319 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200320 if (!p)
321 return NULL;
322 p->reversed = 1;
323 fs_path_reset(p);
324 return p;
325}
326
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000327static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200328{
329 if (!p)
330 return;
David Sterbaace01052014-02-05 16:17:34 +0100331 if (p->buf != p->inline_buf)
332 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200333 kfree(p);
334}
335
336static int fs_path_len(struct fs_path *p)
337{
338 return p->end - p->start;
339}
340
341static int fs_path_ensure_buf(struct fs_path *p, int len)
342{
343 char *tmp_buf;
344 int path_len;
345 int old_buf_len;
346
347 len++;
348
349 if (p->buf_len >= len)
350 return 0;
351
Chris Masoncfd4a532014-04-26 05:02:03 -0700352 if (len > PATH_MAX) {
353 WARN_ON(1);
354 return -ENOMEM;
355 }
356
David Sterba1b2782c2014-02-25 19:32:59 +0100357 path_len = p->end - p->start;
358 old_buf_len = p->buf_len;
359
David Sterbaace01052014-02-05 16:17:34 +0100360 /*
361 * First time the inline_buf does not suffice
362 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100363 if (p->buf == p->inline_buf) {
David Sterba9c9ca002014-02-25 19:33:08 +0100364 tmp_buf = kmalloc(len, GFP_NOFS);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100365 if (tmp_buf)
366 memcpy(tmp_buf, p->buf, old_buf_len);
367 } else {
David Sterba9c9ca002014-02-25 19:33:08 +0100368 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100369 }
David Sterba9c9ca002014-02-25 19:33:08 +0100370 if (!tmp_buf)
371 return -ENOMEM;
372 p->buf = tmp_buf;
373 /*
374 * The real size of the buffer is bigger, this will let the fast path
375 * happen most of the time
376 */
377 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100378
Alexander Block31db9f72012-07-25 23:19:24 +0200379 if (p->reversed) {
380 tmp_buf = p->buf + old_buf_len - path_len - 1;
381 p->end = p->buf + p->buf_len - 1;
382 p->start = p->end - path_len;
383 memmove(p->start, tmp_buf, path_len + 1);
384 } else {
385 p->start = p->buf;
386 p->end = p->start + path_len;
387 }
388 return 0;
389}
390
David Sterbab23ab572014-02-03 19:23:19 +0100391static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
392 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200393{
394 int ret;
395 int new_len;
396
397 new_len = p->end - p->start + name_len;
398 if (p->start != p->end)
399 new_len++;
400 ret = fs_path_ensure_buf(p, new_len);
401 if (ret < 0)
402 goto out;
403
404 if (p->reversed) {
405 if (p->start != p->end)
406 *--p->start = '/';
407 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100408 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200409 } else {
410 if (p->start != p->end)
411 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100412 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200413 p->end += name_len;
414 *p->end = 0;
415 }
416
417out:
418 return ret;
419}
420
421static int fs_path_add(struct fs_path *p, const char *name, int name_len)
422{
423 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100424 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200425
David Sterbab23ab572014-02-03 19:23:19 +0100426 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200427 if (ret < 0)
428 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100429 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200430
431out:
432 return ret;
433}
434
435static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
436{
437 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100438 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200439
David Sterbab23ab572014-02-03 19:23:19 +0100440 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200441 if (ret < 0)
442 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100443 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200444
445out:
446 return ret;
447}
448
449static int fs_path_add_from_extent_buffer(struct fs_path *p,
450 struct extent_buffer *eb,
451 unsigned long off, int len)
452{
453 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100454 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200455
David Sterbab23ab572014-02-03 19:23:19 +0100456 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200457 if (ret < 0)
458 goto out;
459
David Sterbab23ab572014-02-03 19:23:19 +0100460 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200461
462out:
463 return ret;
464}
465
Alexander Block31db9f72012-07-25 23:19:24 +0200466static int fs_path_copy(struct fs_path *p, struct fs_path *from)
467{
468 int ret;
469
470 p->reversed = from->reversed;
471 fs_path_reset(p);
472
473 ret = fs_path_add_path(p, from);
474
475 return ret;
476}
477
478
479static void fs_path_unreverse(struct fs_path *p)
480{
481 char *tmp;
482 int len;
483
484 if (!p->reversed)
485 return;
486
487 tmp = p->start;
488 len = p->end - p->start;
489 p->start = p->buf;
490 p->end = p->start + len;
491 memmove(p->start, tmp, len + 1);
492 p->reversed = 0;
493}
494
495static struct btrfs_path *alloc_path_for_send(void)
496{
497 struct btrfs_path *path;
498
499 path = btrfs_alloc_path();
500 if (!path)
501 return NULL;
502 path->search_commit_root = 1;
503 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400504 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200505 return path;
506}
507
Eric Sandeen48a3b632013-04-25 20:41:01 +0000508static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200509{
510 int ret;
511 mm_segment_t old_fs;
512 u32 pos = 0;
513
514 old_fs = get_fs();
515 set_fs(KERNEL_DS);
516
517 while (pos < len) {
Anand Jain1bcea352012-09-14 00:04:21 -0600518 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200519 /* TODO handle that correctly */
520 /*if (ret == -ERESTARTSYS) {
521 continue;
522 }*/
523 if (ret < 0)
524 goto out;
525 if (ret == 0) {
526 ret = -EIO;
527 goto out;
528 }
529 pos += ret;
530 }
531
532 ret = 0;
533
534out:
535 set_fs(old_fs);
536 return ret;
537}
538
539static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
540{
541 struct btrfs_tlv_header *hdr;
542 int total_len = sizeof(*hdr) + len;
543 int left = sctx->send_max_size - sctx->send_size;
544
545 if (unlikely(left < total_len))
546 return -EOVERFLOW;
547
548 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
549 hdr->tlv_type = cpu_to_le16(attr);
550 hdr->tlv_len = cpu_to_le16(len);
551 memcpy(hdr + 1, data, len);
552 sctx->send_size += total_len;
553
554 return 0;
555}
556
David Sterba95bc79d2013-12-16 17:34:10 +0100557#define TLV_PUT_DEFINE_INT(bits) \
558 static int tlv_put_u##bits(struct send_ctx *sctx, \
559 u##bits attr, u##bits value) \
560 { \
561 __le##bits __tmp = cpu_to_le##bits(value); \
562 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
563 }
Alexander Block31db9f72012-07-25 23:19:24 +0200564
David Sterba95bc79d2013-12-16 17:34:10 +0100565TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200566
567static int tlv_put_string(struct send_ctx *sctx, u16 attr,
568 const char *str, int len)
569{
570 if (len == -1)
571 len = strlen(str);
572 return tlv_put(sctx, attr, str, len);
573}
574
575static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
576 const u8 *uuid)
577{
578 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
579}
580
Alexander Block31db9f72012-07-25 23:19:24 +0200581static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
582 struct extent_buffer *eb,
583 struct btrfs_timespec *ts)
584{
585 struct btrfs_timespec bts;
586 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
587 return tlv_put(sctx, attr, &bts, sizeof(bts));
588}
589
590
591#define TLV_PUT(sctx, attrtype, attrlen, data) \
592 do { \
593 ret = tlv_put(sctx, attrtype, attrlen, data); \
594 if (ret < 0) \
595 goto tlv_put_failure; \
596 } while (0)
597
598#define TLV_PUT_INT(sctx, attrtype, bits, value) \
599 do { \
600 ret = tlv_put_u##bits(sctx, attrtype, value); \
601 if (ret < 0) \
602 goto tlv_put_failure; \
603 } while (0)
604
605#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
606#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
607#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
608#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
609#define TLV_PUT_STRING(sctx, attrtype, str, len) \
610 do { \
611 ret = tlv_put_string(sctx, attrtype, str, len); \
612 if (ret < 0) \
613 goto tlv_put_failure; \
614 } while (0)
615#define TLV_PUT_PATH(sctx, attrtype, p) \
616 do { \
617 ret = tlv_put_string(sctx, attrtype, p->start, \
618 p->end - p->start); \
619 if (ret < 0) \
620 goto tlv_put_failure; \
621 } while(0)
622#define TLV_PUT_UUID(sctx, attrtype, uuid) \
623 do { \
624 ret = tlv_put_uuid(sctx, attrtype, uuid); \
625 if (ret < 0) \
626 goto tlv_put_failure; \
627 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200628#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
629 do { \
630 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
631 if (ret < 0) \
632 goto tlv_put_failure; \
633 } while (0)
634
635static int send_header(struct send_ctx *sctx)
636{
637 struct btrfs_stream_header hdr;
638
639 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
640 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
641
Anand Jain1bcea352012-09-14 00:04:21 -0600642 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
643 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200644}
645
646/*
647 * For each command/item we want to send to userspace, we call this function.
648 */
649static int begin_cmd(struct send_ctx *sctx, int cmd)
650{
651 struct btrfs_cmd_header *hdr;
652
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530653 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200654 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200655
656 BUG_ON(sctx->send_size);
657
658 sctx->send_size += sizeof(*hdr);
659 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
660 hdr->cmd = cpu_to_le16(cmd);
661
662 return 0;
663}
664
665static int send_cmd(struct send_ctx *sctx)
666{
667 int ret;
668 struct btrfs_cmd_header *hdr;
669 u32 crc;
670
671 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
672 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
673 hdr->crc = 0;
674
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000675 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200676 hdr->crc = cpu_to_le32(crc);
677
Anand Jain1bcea352012-09-14 00:04:21 -0600678 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
679 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200680
681 sctx->total_send_size += sctx->send_size;
682 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
683 sctx->send_size = 0;
684
685 return ret;
686}
687
688/*
689 * Sends a move instruction to user space
690 */
691static int send_rename(struct send_ctx *sctx,
692 struct fs_path *from, struct fs_path *to)
693{
694 int ret;
695
696verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
697
698 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
699 if (ret < 0)
700 goto out;
701
702 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
703 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
704
705 ret = send_cmd(sctx);
706
707tlv_put_failure:
708out:
709 return ret;
710}
711
712/*
713 * Sends a link instruction to user space
714 */
715static int send_link(struct send_ctx *sctx,
716 struct fs_path *path, struct fs_path *lnk)
717{
718 int ret;
719
720verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
721
722 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
723 if (ret < 0)
724 goto out;
725
726 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
727 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
728
729 ret = send_cmd(sctx);
730
731tlv_put_failure:
732out:
733 return ret;
734}
735
736/*
737 * Sends an unlink instruction to user space
738 */
739static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
740{
741 int ret;
742
743verbose_printk("btrfs: send_unlink %s\n", path->start);
744
745 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
746 if (ret < 0)
747 goto out;
748
749 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
750
751 ret = send_cmd(sctx);
752
753tlv_put_failure:
754out:
755 return ret;
756}
757
758/*
759 * Sends a rmdir instruction to user space
760 */
761static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
762{
763 int ret;
764
765verbose_printk("btrfs: send_rmdir %s\n", path->start);
766
767 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
768 if (ret < 0)
769 goto out;
770
771 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
772
773 ret = send_cmd(sctx);
774
775tlv_put_failure:
776out:
777 return ret;
778}
779
780/*
781 * Helper function to retrieve some fields from an inode item.
782 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400783static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
784 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
785 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200786{
787 int ret;
788 struct btrfs_inode_item *ii;
789 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200790
791 key.objectid = ino;
792 key.type = BTRFS_INODE_ITEM_KEY;
793 key.offset = 0;
794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200795 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400796 if (ret > 0)
797 ret = -ENOENT;
798 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200799 }
800
801 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
802 struct btrfs_inode_item);
803 if (size)
804 *size = btrfs_inode_size(path->nodes[0], ii);
805 if (gen)
806 *gen = btrfs_inode_generation(path->nodes[0], ii);
807 if (mode)
808 *mode = btrfs_inode_mode(path->nodes[0], ii);
809 if (uid)
810 *uid = btrfs_inode_uid(path->nodes[0], ii);
811 if (gid)
812 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200813 if (rdev)
814 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200815
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400816 return ret;
817}
818
819static int get_inode_info(struct btrfs_root *root,
820 u64 ino, u64 *size, u64 *gen,
821 u64 *mode, u64 *uid, u64 *gid,
822 u64 *rdev)
823{
824 struct btrfs_path *path;
825 int ret;
826
827 path = alloc_path_for_send();
828 if (!path)
829 return -ENOMEM;
830 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
831 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200832 btrfs_free_path(path);
833 return ret;
834}
835
836typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
837 struct fs_path *p,
838 void *ctx);
839
840/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000841 * Helper function to iterate the entries in ONE btrfs_inode_ref or
842 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200843 * The iterate callback may return a non zero value to stop iteration. This can
844 * be a negative value for error codes or 1 to simply stop it.
845 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000846 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200847 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000848static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200849 struct btrfs_key *found_key, int resolve,
850 iterate_inode_ref_t iterate, void *ctx)
851{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000852 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200853 struct btrfs_item *item;
854 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000855 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200856 struct btrfs_path *tmp_path;
857 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000858 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200859 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000860 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200861 u32 name_len;
862 char *start;
863 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000864 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200865 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000866 u64 dir;
867 unsigned long name_off;
868 unsigned long elem_size;
869 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200870
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000871 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200872 if (!p)
873 return -ENOMEM;
874
875 tmp_path = alloc_path_for_send();
876 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000877 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200878 return -ENOMEM;
879 }
880
Alexander Block31db9f72012-07-25 23:19:24 +0200881
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000882 if (found_key->type == BTRFS_INODE_REF_KEY) {
883 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
884 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100885 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000886 total = btrfs_item_size(eb, item);
887 elem_size = sizeof(*iref);
888 } else {
889 ptr = btrfs_item_ptr_offset(eb, slot);
890 total = btrfs_item_size_nr(eb, slot);
891 elem_size = sizeof(*extref);
892 }
893
Alexander Block31db9f72012-07-25 23:19:24 +0200894 while (cur < total) {
895 fs_path_reset(p);
896
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000897 if (found_key->type == BTRFS_INODE_REF_KEY) {
898 iref = (struct btrfs_inode_ref *)(ptr + cur);
899 name_len = btrfs_inode_ref_name_len(eb, iref);
900 name_off = (unsigned long)(iref + 1);
901 index = btrfs_inode_ref_index(eb, iref);
902 dir = found_key->offset;
903 } else {
904 extref = (struct btrfs_inode_extref *)(ptr + cur);
905 name_len = btrfs_inode_extref_name_len(eb, extref);
906 name_off = (unsigned long)&extref->name;
907 index = btrfs_inode_extref_index(eb, extref);
908 dir = btrfs_inode_extref_parent(eb, extref);
909 }
910
Alexander Block31db9f72012-07-25 23:19:24 +0200911 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000912 start = btrfs_ref_to_path(root, tmp_path, name_len,
913 name_off, eb, dir,
914 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200915 if (IS_ERR(start)) {
916 ret = PTR_ERR(start);
917 goto out;
918 }
919 if (start < p->buf) {
920 /* overflow , try again with larger buffer */
921 ret = fs_path_ensure_buf(p,
922 p->buf_len + p->buf - start);
923 if (ret < 0)
924 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000925 start = btrfs_ref_to_path(root, tmp_path,
926 name_len, name_off,
927 eb, dir,
928 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200929 if (IS_ERR(start)) {
930 ret = PTR_ERR(start);
931 goto out;
932 }
933 BUG_ON(start < p->buf);
934 }
935 p->start = start;
936 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000937 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
938 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200939 if (ret < 0)
940 goto out;
941 }
942
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000943 cur += elem_size + name_len;
944 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200945 if (ret)
946 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200947 num++;
948 }
949
950out:
951 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000952 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200953 return ret;
954}
955
956typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
957 const char *name, int name_len,
958 const char *data, int data_len,
959 u8 type, void *ctx);
960
961/*
962 * Helper function to iterate the entries in ONE btrfs_dir_item.
963 * The iterate callback may return a non zero value to stop iteration. This can
964 * be a negative value for error codes or 1 to simply stop it.
965 *
966 * path must point to the dir item when called.
967 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000968static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200969 struct btrfs_key *found_key,
970 iterate_dir_item_t iterate, void *ctx)
971{
972 int ret = 0;
973 struct extent_buffer *eb;
974 struct btrfs_item *item;
975 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200976 struct btrfs_key di_key;
977 char *buf = NULL;
David Sterbaace01052014-02-05 16:17:34 +0100978 const int buf_len = PATH_MAX;
Alexander Block31db9f72012-07-25 23:19:24 +0200979 u32 name_len;
980 u32 data_len;
981 u32 cur;
982 u32 len;
983 u32 total;
984 int slot;
985 int num;
986 u8 type;
987
Alexander Block31db9f72012-07-25 23:19:24 +0200988 buf = kmalloc(buf_len, GFP_NOFS);
989 if (!buf) {
990 ret = -ENOMEM;
991 goto out;
992 }
993
Alexander Block31db9f72012-07-25 23:19:24 +0200994 eb = path->nodes[0];
995 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +0100996 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +0200997 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
998 cur = 0;
999 len = 0;
1000 total = btrfs_item_size(eb, item);
1001
1002 num = 0;
1003 while (cur < total) {
1004 name_len = btrfs_dir_name_len(eb, di);
1005 data_len = btrfs_dir_data_len(eb, di);
1006 type = btrfs_dir_type(eb, di);
1007 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1008
David Sterbaace01052014-02-05 16:17:34 +01001009 /*
1010 * Path too long
1011 */
Alexander Block31db9f72012-07-25 23:19:24 +02001012 if (name_len + data_len > buf_len) {
David Sterbaace01052014-02-05 16:17:34 +01001013 ret = -ENAMETOOLONG;
1014 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02001015 }
1016
1017 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1018 name_len + data_len);
1019
1020 len = sizeof(*di) + name_len + data_len;
1021 di = (struct btrfs_dir_item *)((char *)di + len);
1022 cur += len;
1023
1024 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1025 data_len, type, ctx);
1026 if (ret < 0)
1027 goto out;
1028 if (ret) {
1029 ret = 0;
1030 goto out;
1031 }
1032
1033 num++;
1034 }
1035
1036out:
David Sterbaace01052014-02-05 16:17:34 +01001037 kfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001038 return ret;
1039}
1040
1041static int __copy_first_ref(int num, u64 dir, int index,
1042 struct fs_path *p, void *ctx)
1043{
1044 int ret;
1045 struct fs_path *pt = ctx;
1046
1047 ret = fs_path_copy(pt, p);
1048 if (ret < 0)
1049 return ret;
1050
1051 /* we want the first only */
1052 return 1;
1053}
1054
1055/*
1056 * Retrieve the first path of an inode. If an inode has more then one
1057 * ref/hardlink, this is ignored.
1058 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001059static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001060 u64 ino, struct fs_path *path)
1061{
1062 int ret;
1063 struct btrfs_key key, found_key;
1064 struct btrfs_path *p;
1065
1066 p = alloc_path_for_send();
1067 if (!p)
1068 return -ENOMEM;
1069
1070 fs_path_reset(path);
1071
1072 key.objectid = ino;
1073 key.type = BTRFS_INODE_REF_KEY;
1074 key.offset = 0;
1075
1076 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1077 if (ret < 0)
1078 goto out;
1079 if (ret) {
1080 ret = 1;
1081 goto out;
1082 }
1083 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1084 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001085 (found_key.type != BTRFS_INODE_REF_KEY &&
1086 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001087 ret = -ENOENT;
1088 goto out;
1089 }
1090
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001091 ret = iterate_inode_ref(root, p, &found_key, 1,
1092 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001093 if (ret < 0)
1094 goto out;
1095 ret = 0;
1096
1097out:
1098 btrfs_free_path(p);
1099 return ret;
1100}
1101
1102struct backref_ctx {
1103 struct send_ctx *sctx;
1104
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001105 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001106 /* number of total found references */
1107 u64 found;
1108
1109 /*
1110 * used for clones found in send_root. clones found behind cur_objectid
1111 * and cur_offset are not considered as allowed clones.
1112 */
1113 u64 cur_objectid;
1114 u64 cur_offset;
1115
1116 /* may be truncated in case it's the last extent in a file */
1117 u64 extent_len;
1118
1119 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001120 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001121};
1122
1123static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1124{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001125 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001126 struct clone_root *cr = (struct clone_root *)elt;
1127
1128 if (root < cr->root->objectid)
1129 return -1;
1130 if (root > cr->root->objectid)
1131 return 1;
1132 return 0;
1133}
1134
1135static int __clone_root_cmp_sort(const void *e1, const void *e2)
1136{
1137 struct clone_root *cr1 = (struct clone_root *)e1;
1138 struct clone_root *cr2 = (struct clone_root *)e2;
1139
1140 if (cr1->root->objectid < cr2->root->objectid)
1141 return -1;
1142 if (cr1->root->objectid > cr2->root->objectid)
1143 return 1;
1144 return 0;
1145}
1146
1147/*
1148 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001149 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001150 */
1151static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1152{
1153 struct backref_ctx *bctx = ctx_;
1154 struct clone_root *found;
1155 int ret;
1156 u64 i_size;
1157
1158 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001159 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001160 bctx->sctx->clone_roots_cnt,
1161 sizeof(struct clone_root),
1162 __clone_root_cmp_bsearch);
1163 if (!found)
1164 return 0;
1165
1166 if (found->root == bctx->sctx->send_root &&
1167 ino == bctx->cur_objectid &&
1168 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001169 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001170 }
1171
1172 /*
Alexander Block766702e2012-07-28 14:11:31 +02001173 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001174 * accept clones from these extents.
1175 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001176 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1177 NULL, NULL, NULL);
1178 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001179 if (ret < 0)
1180 return ret;
1181
1182 if (offset + bctx->extent_len > i_size)
1183 return 0;
1184
1185 /*
1186 * Make sure we don't consider clones from send_root that are
1187 * behind the current inode/offset.
1188 */
1189 if (found->root == bctx->sctx->send_root) {
1190 /*
1191 * TODO for the moment we don't accept clones from the inode
1192 * that is currently send. We may change this when
1193 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1194 * file.
1195 */
1196 if (ino >= bctx->cur_objectid)
1197 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001198#if 0
1199 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001200 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001201 if (offset + bctx->extent_len > bctx->cur_offset)
1202 return 0;
1203#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001204 }
1205
1206 bctx->found++;
1207 found->found_refs++;
1208 if (ino < found->ino) {
1209 found->ino = ino;
1210 found->offset = offset;
1211 } else if (found->ino == ino) {
1212 /*
1213 * same extent found more then once in the same file.
1214 */
1215 if (found->offset > offset + bctx->extent_len)
1216 found->offset = offset;
1217 }
1218
1219 return 0;
1220}
1221
1222/*
Alexander Block766702e2012-07-28 14:11:31 +02001223 * Given an inode, offset and extent item, it finds a good clone for a clone
1224 * instruction. Returns -ENOENT when none could be found. The function makes
1225 * sure that the returned clone is usable at the point where sending is at the
1226 * moment. This means, that no clones are accepted which lie behind the current
1227 * inode+offset.
1228 *
Alexander Block31db9f72012-07-25 23:19:24 +02001229 * path must point to the extent item when called.
1230 */
1231static int find_extent_clone(struct send_ctx *sctx,
1232 struct btrfs_path *path,
1233 u64 ino, u64 data_offset,
1234 u64 ino_size,
1235 struct clone_root **found)
1236{
1237 int ret;
1238 int extent_type;
1239 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001240 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001241 u64 num_bytes;
1242 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001243 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001244 struct btrfs_file_extent_item *fi;
1245 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001246 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001247 struct clone_root *cur_clone_root;
1248 struct btrfs_key found_key;
1249 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001250 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001251 u32 i;
1252
1253 tmp_path = alloc_path_for_send();
1254 if (!tmp_path)
1255 return -ENOMEM;
1256
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001257 /* We only use this path under the commit sem */
1258 tmp_path->need_commit_sem = 0;
1259
Alexander Block35075bb2012-07-28 12:44:34 +02001260 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1261 if (!backref_ctx) {
1262 ret = -ENOMEM;
1263 goto out;
1264 }
1265
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001266 backref_ctx->path = tmp_path;
1267
Alexander Block31db9f72012-07-25 23:19:24 +02001268 if (data_offset >= ino_size) {
1269 /*
1270 * There may be extents that lie behind the file's size.
1271 * I at least had this in combination with snapshotting while
1272 * writing large files.
1273 */
1274 ret = 0;
1275 goto out;
1276 }
1277
1278 fi = btrfs_item_ptr(eb, path->slots[0],
1279 struct btrfs_file_extent_item);
1280 extent_type = btrfs_file_extent_type(eb, fi);
1281 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1282 ret = -ENOENT;
1283 goto out;
1284 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001285 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001286
1287 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001288 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1289 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001290 ret = -ENOENT;
1291 goto out;
1292 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001293 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001294
Josef Bacik9e351cc2014-03-13 15:42:13 -04001295 down_read(&sctx->send_root->fs_info->commit_root_sem);
Liu Bo69917e42012-09-07 20:01:28 -06001296 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1297 &found_key, &flags);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001298 up_read(&sctx->send_root->fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001299 btrfs_release_path(tmp_path);
1300
1301 if (ret < 0)
1302 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001303 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001304 ret = -EIO;
1305 goto out;
1306 }
1307
1308 /*
1309 * Setup the clone roots.
1310 */
1311 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1312 cur_clone_root = sctx->clone_roots + i;
1313 cur_clone_root->ino = (u64)-1;
1314 cur_clone_root->offset = 0;
1315 cur_clone_root->found_refs = 0;
1316 }
1317
Alexander Block35075bb2012-07-28 12:44:34 +02001318 backref_ctx->sctx = sctx;
1319 backref_ctx->found = 0;
1320 backref_ctx->cur_objectid = ino;
1321 backref_ctx->cur_offset = data_offset;
1322 backref_ctx->found_itself = 0;
1323 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001324
1325 /*
1326 * The last extent of a file may be too large due to page alignment.
1327 * We need to adjust extent_len in this case so that the checks in
1328 * __iterate_backrefs work.
1329 */
1330 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001331 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001332
1333 /*
1334 * Now collect all backrefs.
1335 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001336 if (compressed == BTRFS_COMPRESS_NONE)
1337 extent_item_pos = logical - found_key.objectid;
1338 else
1339 extent_item_pos = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001340 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1341 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001342 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001343
Alexander Block31db9f72012-07-25 23:19:24 +02001344 if (ret < 0)
1345 goto out;
1346
Alexander Block35075bb2012-07-28 12:44:34 +02001347 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001348 /* found a bug in backref code? */
1349 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001350 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001351 "send_root. inode=%llu, offset=%llu, "
David Sterba351fd352014-05-15 16:48:20 +02001352 "disk_byte=%llu found extent=%llu",
Chris Mason74dd17f2012-08-07 16:25:13 -04001353 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001354 goto out;
1355 }
1356
1357verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1358 "ino=%llu, "
1359 "num_bytes=%llu, logical=%llu\n",
1360 data_offset, ino, num_bytes, logical);
1361
Alexander Block35075bb2012-07-28 12:44:34 +02001362 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001363 verbose_printk("btrfs: no clones found\n");
1364
1365 cur_clone_root = NULL;
1366 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1367 if (sctx->clone_roots[i].found_refs) {
1368 if (!cur_clone_root)
1369 cur_clone_root = sctx->clone_roots + i;
1370 else if (sctx->clone_roots[i].root == sctx->send_root)
1371 /* prefer clones from send_root over others */
1372 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001373 }
1374
1375 }
1376
1377 if (cur_clone_root) {
Filipe David Borba Manana93de4ba2014-02-15 15:53:16 +00001378 if (compressed != BTRFS_COMPRESS_NONE) {
1379 /*
1380 * Offsets given by iterate_extent_inodes() are relative
1381 * to the start of the extent, we need to add logical
1382 * offset from the file extent item.
1383 * (See why at backref.c:check_extent_in_eb())
1384 */
1385 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1386 fi);
1387 }
Alexander Block31db9f72012-07-25 23:19:24 +02001388 *found = cur_clone_root;
1389 ret = 0;
1390 } else {
1391 ret = -ENOENT;
1392 }
1393
1394out:
1395 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001396 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001397 return ret;
1398}
1399
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001400static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001401 u64 ino,
1402 struct fs_path *dest)
1403{
1404 int ret;
1405 struct btrfs_path *path;
1406 struct btrfs_key key;
1407 struct btrfs_file_extent_item *ei;
1408 u8 type;
1409 u8 compression;
1410 unsigned long off;
1411 int len;
1412
1413 path = alloc_path_for_send();
1414 if (!path)
1415 return -ENOMEM;
1416
1417 key.objectid = ino;
1418 key.type = BTRFS_EXTENT_DATA_KEY;
1419 key.offset = 0;
1420 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1421 if (ret < 0)
1422 goto out;
1423 BUG_ON(ret);
1424
1425 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1426 struct btrfs_file_extent_item);
1427 type = btrfs_file_extent_type(path->nodes[0], ei);
1428 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1429 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1430 BUG_ON(compression);
1431
1432 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001433 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001434
1435 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001436
1437out:
1438 btrfs_free_path(path);
1439 return ret;
1440}
1441
1442/*
1443 * Helper function to generate a file name that is unique in the root of
1444 * send_root and parent_root. This is used to generate names for orphan inodes.
1445 */
1446static int gen_unique_name(struct send_ctx *sctx,
1447 u64 ino, u64 gen,
1448 struct fs_path *dest)
1449{
1450 int ret = 0;
1451 struct btrfs_path *path;
1452 struct btrfs_dir_item *di;
1453 char tmp[64];
1454 int len;
1455 u64 idx = 0;
1456
1457 path = alloc_path_for_send();
1458 if (!path)
1459 return -ENOMEM;
1460
1461 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001462 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001463 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001464 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001465
1466 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1467 path, BTRFS_FIRST_FREE_OBJECTID,
1468 tmp, strlen(tmp), 0);
1469 btrfs_release_path(path);
1470 if (IS_ERR(di)) {
1471 ret = PTR_ERR(di);
1472 goto out;
1473 }
1474 if (di) {
1475 /* not unique, try again */
1476 idx++;
1477 continue;
1478 }
1479
1480 if (!sctx->parent_root) {
1481 /* unique */
1482 ret = 0;
1483 break;
1484 }
1485
1486 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1487 path, BTRFS_FIRST_FREE_OBJECTID,
1488 tmp, strlen(tmp), 0);
1489 btrfs_release_path(path);
1490 if (IS_ERR(di)) {
1491 ret = PTR_ERR(di);
1492 goto out;
1493 }
1494 if (di) {
1495 /* not unique, try again */
1496 idx++;
1497 continue;
1498 }
1499 /* unique */
1500 break;
1501 }
1502
1503 ret = fs_path_add(dest, tmp, strlen(tmp));
1504
1505out:
1506 btrfs_free_path(path);
1507 return ret;
1508}
1509
1510enum inode_state {
1511 inode_state_no_change,
1512 inode_state_will_create,
1513 inode_state_did_create,
1514 inode_state_will_delete,
1515 inode_state_did_delete,
1516};
1517
1518static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1519{
1520 int ret;
1521 int left_ret;
1522 int right_ret;
1523 u64 left_gen;
1524 u64 right_gen;
1525
1526 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001527 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001528 if (ret < 0 && ret != -ENOENT)
1529 goto out;
1530 left_ret = ret;
1531
1532 if (!sctx->parent_root) {
1533 right_ret = -ENOENT;
1534 } else {
1535 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001536 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001537 if (ret < 0 && ret != -ENOENT)
1538 goto out;
1539 right_ret = ret;
1540 }
1541
1542 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001543 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001544 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001545 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001546 if (ino < sctx->send_progress)
1547 ret = inode_state_did_create;
1548 else
1549 ret = inode_state_will_create;
1550 } else if (right_gen == gen) {
1551 if (ino < sctx->send_progress)
1552 ret = inode_state_did_delete;
1553 else
1554 ret = inode_state_will_delete;
1555 } else {
1556 ret = -ENOENT;
1557 }
1558 } else if (!left_ret) {
1559 if (left_gen == gen) {
1560 if (ino < sctx->send_progress)
1561 ret = inode_state_did_create;
1562 else
1563 ret = inode_state_will_create;
1564 } else {
1565 ret = -ENOENT;
1566 }
1567 } else if (!right_ret) {
1568 if (right_gen == gen) {
1569 if (ino < sctx->send_progress)
1570 ret = inode_state_did_delete;
1571 else
1572 ret = inode_state_will_delete;
1573 } else {
1574 ret = -ENOENT;
1575 }
1576 } else {
1577 ret = -ENOENT;
1578 }
1579
1580out:
1581 return ret;
1582}
1583
1584static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1585{
1586 int ret;
1587
1588 ret = get_cur_inode_state(sctx, ino, gen);
1589 if (ret < 0)
1590 goto out;
1591
1592 if (ret == inode_state_no_change ||
1593 ret == inode_state_did_create ||
1594 ret == inode_state_will_delete)
1595 ret = 1;
1596 else
1597 ret = 0;
1598
1599out:
1600 return ret;
1601}
1602
1603/*
1604 * Helper function to lookup a dir item in a dir.
1605 */
1606static int lookup_dir_item_inode(struct btrfs_root *root,
1607 u64 dir, const char *name, int name_len,
1608 u64 *found_inode,
1609 u8 *found_type)
1610{
1611 int ret = 0;
1612 struct btrfs_dir_item *di;
1613 struct btrfs_key key;
1614 struct btrfs_path *path;
1615
1616 path = alloc_path_for_send();
1617 if (!path)
1618 return -ENOMEM;
1619
1620 di = btrfs_lookup_dir_item(NULL, root, path,
1621 dir, name, name_len, 0);
1622 if (!di) {
1623 ret = -ENOENT;
1624 goto out;
1625 }
1626 if (IS_ERR(di)) {
1627 ret = PTR_ERR(di);
1628 goto out;
1629 }
1630 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001631 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1632 ret = -ENOENT;
1633 goto out;
1634 }
Alexander Block31db9f72012-07-25 23:19:24 +02001635 *found_inode = key.objectid;
1636 *found_type = btrfs_dir_type(path->nodes[0], di);
1637
1638out:
1639 btrfs_free_path(path);
1640 return ret;
1641}
1642
Alexander Block766702e2012-07-28 14:11:31 +02001643/*
1644 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1645 * generation of the parent dir and the name of the dir entry.
1646 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001647static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001648 u64 *dir, u64 *dir_gen, struct fs_path *name)
1649{
1650 int ret;
1651 struct btrfs_key key;
1652 struct btrfs_key found_key;
1653 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001654 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001655 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001656
1657 path = alloc_path_for_send();
1658 if (!path)
1659 return -ENOMEM;
1660
1661 key.objectid = ino;
1662 key.type = BTRFS_INODE_REF_KEY;
1663 key.offset = 0;
1664
1665 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1666 if (ret < 0)
1667 goto out;
1668 if (!ret)
1669 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1670 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001671 if (ret || found_key.objectid != ino ||
1672 (found_key.type != BTRFS_INODE_REF_KEY &&
1673 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001674 ret = -ENOENT;
1675 goto out;
1676 }
1677
Filipe Manana51a60252014-05-13 22:01:02 +01001678 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001679 struct btrfs_inode_ref *iref;
1680 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1681 struct btrfs_inode_ref);
1682 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1683 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1684 (unsigned long)(iref + 1),
1685 len);
1686 parent_dir = found_key.offset;
1687 } else {
1688 struct btrfs_inode_extref *extref;
1689 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1690 struct btrfs_inode_extref);
1691 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1692 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1693 (unsigned long)&extref->name, len);
1694 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1695 }
Alexander Block31db9f72012-07-25 23:19:24 +02001696 if (ret < 0)
1697 goto out;
1698 btrfs_release_path(path);
1699
Filipe Mananab46ab972014-03-21 12:46:54 +00001700 if (dir_gen) {
1701 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1702 NULL, NULL, NULL);
1703 if (ret < 0)
1704 goto out;
1705 }
Alexander Block31db9f72012-07-25 23:19:24 +02001706
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001707 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001708
1709out:
1710 btrfs_free_path(path);
1711 return ret;
1712}
1713
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001714static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001715 u64 ino, u64 dir,
1716 const char *name, int name_len)
1717{
1718 int ret;
1719 struct fs_path *tmp_name;
1720 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001721
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001722 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001723 if (!tmp_name)
1724 return -ENOMEM;
1725
Filipe Mananab46ab972014-03-21 12:46:54 +00001726 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001727 if (ret < 0)
1728 goto out;
1729
Alexander Blockb9291af2012-07-28 11:07:18 +02001730 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001731 ret = 0;
1732 goto out;
1733 }
1734
Alexander Blocke938c8a2012-07-28 16:33:49 +02001735 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001736
1737out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001738 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001739 return ret;
1740}
1741
Alexander Block766702e2012-07-28 14:11:31 +02001742/*
1743 * Used by process_recorded_refs to determine if a new ref would overwrite an
1744 * already existing ref. In case it detects an overwrite, it returns the
1745 * inode/gen in who_ino/who_gen.
1746 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1747 * to make sure later references to the overwritten inode are possible.
1748 * Orphanizing is however only required for the first ref of an inode.
1749 * process_recorded_refs does an additional is_first_ref check to see if
1750 * orphanizing is really required.
1751 */
Alexander Block31db9f72012-07-25 23:19:24 +02001752static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1753 const char *name, int name_len,
1754 u64 *who_ino, u64 *who_gen)
1755{
1756 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001757 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001758 u64 other_inode = 0;
1759 u8 other_type = 0;
1760
1761 if (!sctx->parent_root)
1762 goto out;
1763
1764 ret = is_inode_existent(sctx, dir, dir_gen);
1765 if (ret <= 0)
1766 goto out;
1767
Josef Bacikebdad912013-08-06 16:47:48 -04001768 /*
1769 * If we have a parent root we need to verify that the parent dir was
1770 * not delted and then re-created, if it was then we have no overwrite
1771 * and we can just unlink this entry.
1772 */
1773 if (sctx->parent_root) {
1774 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1775 NULL, NULL, NULL);
1776 if (ret < 0 && ret != -ENOENT)
1777 goto out;
1778 if (ret) {
1779 ret = 0;
1780 goto out;
1781 }
1782 if (gen != dir_gen)
1783 goto out;
1784 }
1785
Alexander Block31db9f72012-07-25 23:19:24 +02001786 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1787 &other_inode, &other_type);
1788 if (ret < 0 && ret != -ENOENT)
1789 goto out;
1790 if (ret) {
1791 ret = 0;
1792 goto out;
1793 }
1794
Alexander Block766702e2012-07-28 14:11:31 +02001795 /*
1796 * Check if the overwritten ref was already processed. If yes, the ref
1797 * was already unlinked/moved, so we can safely assume that we will not
1798 * overwrite anything at this point in time.
1799 */
Alexander Block31db9f72012-07-25 23:19:24 +02001800 if (other_inode > sctx->send_progress) {
1801 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001802 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001803 if (ret < 0)
1804 goto out;
1805
1806 ret = 1;
1807 *who_ino = other_inode;
1808 } else {
1809 ret = 0;
1810 }
1811
1812out:
1813 return ret;
1814}
1815
Alexander Block766702e2012-07-28 14:11:31 +02001816/*
1817 * Checks if the ref was overwritten by an already processed inode. This is
1818 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1819 * thus the orphan name needs be used.
1820 * process_recorded_refs also uses it to avoid unlinking of refs that were
1821 * overwritten.
1822 */
Alexander Block31db9f72012-07-25 23:19:24 +02001823static int did_overwrite_ref(struct send_ctx *sctx,
1824 u64 dir, u64 dir_gen,
1825 u64 ino, u64 ino_gen,
1826 const char *name, int name_len)
1827{
1828 int ret = 0;
1829 u64 gen;
1830 u64 ow_inode;
1831 u8 other_type;
1832
1833 if (!sctx->parent_root)
1834 goto out;
1835
1836 ret = is_inode_existent(sctx, dir, dir_gen);
1837 if (ret <= 0)
1838 goto out;
1839
1840 /* check if the ref was overwritten by another ref */
1841 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1842 &ow_inode, &other_type);
1843 if (ret < 0 && ret != -ENOENT)
1844 goto out;
1845 if (ret) {
1846 /* was never and will never be overwritten */
1847 ret = 0;
1848 goto out;
1849 }
1850
1851 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001852 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001853 if (ret < 0)
1854 goto out;
1855
1856 if (ow_inode == ino && gen == ino_gen) {
1857 ret = 0;
1858 goto out;
1859 }
1860
1861 /* we know that it is or will be overwritten. check this now */
1862 if (ow_inode < sctx->send_progress)
1863 ret = 1;
1864 else
1865 ret = 0;
1866
1867out:
1868 return ret;
1869}
1870
Alexander Block766702e2012-07-28 14:11:31 +02001871/*
1872 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1873 * that got overwritten. This is used by process_recorded_refs to determine
1874 * if it has to use the path as returned by get_cur_path or the orphan name.
1875 */
Alexander Block31db9f72012-07-25 23:19:24 +02001876static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1877{
1878 int ret = 0;
1879 struct fs_path *name = NULL;
1880 u64 dir;
1881 u64 dir_gen;
1882
1883 if (!sctx->parent_root)
1884 goto out;
1885
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001886 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001887 if (!name)
1888 return -ENOMEM;
1889
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001890 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001891 if (ret < 0)
1892 goto out;
1893
1894 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1895 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001896
1897out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001898 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001899 return ret;
1900}
1901
Alexander Block766702e2012-07-28 14:11:31 +02001902/*
1903 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1904 * so we need to do some special handling in case we have clashes. This function
1905 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001906 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001907 */
Alexander Block31db9f72012-07-25 23:19:24 +02001908static int name_cache_insert(struct send_ctx *sctx,
1909 struct name_cache_entry *nce)
1910{
1911 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001912 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001913
Alexander Block7e0926f2012-07-28 14:20:58 +02001914 nce_head = radix_tree_lookup(&sctx->name_cache,
1915 (unsigned long)nce->ino);
1916 if (!nce_head) {
1917 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001918 if (!nce_head) {
1919 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001920 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001921 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001922 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001923
Alexander Block7e0926f2012-07-28 14:20:58 +02001924 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001925 if (ret < 0) {
1926 kfree(nce_head);
1927 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001928 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001929 }
Alexander Block31db9f72012-07-25 23:19:24 +02001930 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001931 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001932 list_add_tail(&nce->list, &sctx->name_cache_list);
1933 sctx->name_cache_size++;
1934
1935 return ret;
1936}
1937
1938static void name_cache_delete(struct send_ctx *sctx,
1939 struct name_cache_entry *nce)
1940{
Alexander Block7e0926f2012-07-28 14:20:58 +02001941 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001942
Alexander Block7e0926f2012-07-28 14:20:58 +02001943 nce_head = radix_tree_lookup(&sctx->name_cache,
1944 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01001945 if (!nce_head) {
1946 btrfs_err(sctx->send_root->fs_info,
1947 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1948 nce->ino, sctx->name_cache_size);
1949 }
Alexander Block31db9f72012-07-25 23:19:24 +02001950
Alexander Block7e0926f2012-07-28 14:20:58 +02001951 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001952 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001953 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001954
David Sterba57fb8912014-02-03 19:24:40 +01001955 /*
1956 * We may not get to the final release of nce_head if the lookup fails
1957 */
1958 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02001959 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1960 kfree(nce_head);
1961 }
Alexander Block31db9f72012-07-25 23:19:24 +02001962}
1963
1964static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1965 u64 ino, u64 gen)
1966{
Alexander Block7e0926f2012-07-28 14:20:58 +02001967 struct list_head *nce_head;
1968 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001969
Alexander Block7e0926f2012-07-28 14:20:58 +02001970 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1971 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001972 return NULL;
1973
Alexander Block7e0926f2012-07-28 14:20:58 +02001974 list_for_each_entry(cur, nce_head, radix_list) {
1975 if (cur->ino == ino && cur->gen == gen)
1976 return cur;
1977 }
Alexander Block31db9f72012-07-25 23:19:24 +02001978 return NULL;
1979}
1980
Alexander Block766702e2012-07-28 14:11:31 +02001981/*
1982 * Removes the entry from the list and adds it back to the end. This marks the
1983 * entry as recently used so that name_cache_clean_unused does not remove it.
1984 */
Alexander Block31db9f72012-07-25 23:19:24 +02001985static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1986{
1987 list_del(&nce->list);
1988 list_add_tail(&nce->list, &sctx->name_cache_list);
1989}
1990
Alexander Block766702e2012-07-28 14:11:31 +02001991/*
1992 * Remove some entries from the beginning of name_cache_list.
1993 */
Alexander Block31db9f72012-07-25 23:19:24 +02001994static void name_cache_clean_unused(struct send_ctx *sctx)
1995{
1996 struct name_cache_entry *nce;
1997
1998 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1999 return;
2000
2001 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2002 nce = list_entry(sctx->name_cache_list.next,
2003 struct name_cache_entry, list);
2004 name_cache_delete(sctx, nce);
2005 kfree(nce);
2006 }
2007}
2008
2009static void name_cache_free(struct send_ctx *sctx)
2010{
2011 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002012
Alexander Blocke938c8a2012-07-28 16:33:49 +02002013 while (!list_empty(&sctx->name_cache_list)) {
2014 nce = list_entry(sctx->name_cache_list.next,
2015 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002016 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002017 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002018 }
2019}
2020
Alexander Block766702e2012-07-28 14:11:31 +02002021/*
2022 * Used by get_cur_path for each ref up to the root.
2023 * Returns 0 if it succeeded.
2024 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2025 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2026 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2027 * Returns <0 in case of error.
2028 */
Alexander Block31db9f72012-07-25 23:19:24 +02002029static int __get_cur_name_and_parent(struct send_ctx *sctx,
2030 u64 ino, u64 gen,
2031 u64 *parent_ino,
2032 u64 *parent_gen,
2033 struct fs_path *dest)
2034{
2035 int ret;
2036 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002037 struct name_cache_entry *nce = NULL;
2038
Alexander Block766702e2012-07-28 14:11:31 +02002039 /*
2040 * First check if we already did a call to this function with the same
2041 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2042 * return the cached result.
2043 */
Alexander Block31db9f72012-07-25 23:19:24 +02002044 nce = name_cache_search(sctx, ino, gen);
2045 if (nce) {
2046 if (ino < sctx->send_progress && nce->need_later_update) {
2047 name_cache_delete(sctx, nce);
2048 kfree(nce);
2049 nce = NULL;
2050 } else {
2051 name_cache_used(sctx, nce);
2052 *parent_ino = nce->parent_ino;
2053 *parent_gen = nce->parent_gen;
2054 ret = fs_path_add(dest, nce->name, nce->name_len);
2055 if (ret < 0)
2056 goto out;
2057 ret = nce->ret;
2058 goto out;
2059 }
2060 }
2061
Alexander Block766702e2012-07-28 14:11:31 +02002062 /*
2063 * If the inode is not existent yet, add the orphan name and return 1.
2064 * This should only happen for the parent dir that we determine in
2065 * __record_new_ref
2066 */
Alexander Block31db9f72012-07-25 23:19:24 +02002067 ret = is_inode_existent(sctx, ino, gen);
2068 if (ret < 0)
2069 goto out;
2070
2071 if (!ret) {
2072 ret = gen_unique_name(sctx, ino, gen, dest);
2073 if (ret < 0)
2074 goto out;
2075 ret = 1;
2076 goto out_cache;
2077 }
2078
Alexander Block766702e2012-07-28 14:11:31 +02002079 /*
2080 * Depending on whether the inode was already processed or not, use
2081 * send_root or parent_root for ref lookup.
2082 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002083 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002084 ret = get_first_ref(sctx->send_root, ino,
2085 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002086 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002087 ret = get_first_ref(sctx->parent_root, ino,
2088 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002089 if (ret < 0)
2090 goto out;
2091
Alexander Block766702e2012-07-28 14:11:31 +02002092 /*
2093 * Check if the ref was overwritten by an inode's ref that was processed
2094 * earlier. If yes, treat as orphan and return 1.
2095 */
Alexander Block31db9f72012-07-25 23:19:24 +02002096 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2097 dest->start, dest->end - dest->start);
2098 if (ret < 0)
2099 goto out;
2100 if (ret) {
2101 fs_path_reset(dest);
2102 ret = gen_unique_name(sctx, ino, gen, dest);
2103 if (ret < 0)
2104 goto out;
2105 ret = 1;
2106 }
2107
2108out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002109 /*
2110 * Store the result of the lookup in the name cache.
2111 */
Alexander Block31db9f72012-07-25 23:19:24 +02002112 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2113 if (!nce) {
2114 ret = -ENOMEM;
2115 goto out;
2116 }
2117
2118 nce->ino = ino;
2119 nce->gen = gen;
2120 nce->parent_ino = *parent_ino;
2121 nce->parent_gen = *parent_gen;
2122 nce->name_len = fs_path_len(dest);
2123 nce->ret = ret;
2124 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002125
2126 if (ino < sctx->send_progress)
2127 nce->need_later_update = 0;
2128 else
2129 nce->need_later_update = 1;
2130
2131 nce_ret = name_cache_insert(sctx, nce);
2132 if (nce_ret < 0)
2133 ret = nce_ret;
2134 name_cache_clean_unused(sctx);
2135
2136out:
Alexander Block31db9f72012-07-25 23:19:24 +02002137 return ret;
2138}
2139
2140/*
2141 * Magic happens here. This function returns the first ref to an inode as it
2142 * would look like while receiving the stream at this point in time.
2143 * We walk the path up to the root. For every inode in between, we check if it
2144 * was already processed/sent. If yes, we continue with the parent as found
2145 * in send_root. If not, we continue with the parent as found in parent_root.
2146 * If we encounter an inode that was deleted at this point in time, we use the
2147 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2148 * that were not created yet and overwritten inodes/refs.
2149 *
2150 * When do we have have orphan inodes:
2151 * 1. When an inode is freshly created and thus no valid refs are available yet
2152 * 2. When a directory lost all it's refs (deleted) but still has dir items
2153 * inside which were not processed yet (pending for move/delete). If anyone
2154 * tried to get the path to the dir items, it would get a path inside that
2155 * orphan directory.
2156 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2157 * of an unprocessed inode. If in that case the first ref would be
2158 * overwritten, the overwritten inode gets "orphanized". Later when we
2159 * process this overwritten inode, it is restored at a new place by moving
2160 * the orphan inode.
2161 *
2162 * sctx->send_progress tells this function at which point in time receiving
2163 * would be.
2164 */
2165static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2166 struct fs_path *dest)
2167{
2168 int ret = 0;
2169 struct fs_path *name = NULL;
2170 u64 parent_inode = 0;
2171 u64 parent_gen = 0;
2172 int stop = 0;
2173
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002174 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002175 if (!name) {
2176 ret = -ENOMEM;
2177 goto out;
2178 }
2179
2180 dest->reversed = 1;
2181 fs_path_reset(dest);
2182
2183 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2184 fs_path_reset(name);
2185
Filipe Manana9dc44212014-02-19 14:31:44 +00002186 if (is_waiting_for_rm(sctx, ino)) {
2187 ret = gen_unique_name(sctx, ino, gen, name);
2188 if (ret < 0)
2189 goto out;
2190 ret = fs_path_add_path(dest, name);
2191 break;
2192 }
2193
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002194 if (is_waiting_for_move(sctx, ino)) {
2195 ret = get_first_ref(sctx->parent_root, ino,
2196 &parent_inode, &parent_gen, name);
2197 } else {
2198 ret = __get_cur_name_and_parent(sctx, ino, gen,
2199 &parent_inode,
2200 &parent_gen, name);
2201 if (ret)
2202 stop = 1;
2203 }
2204
Alexander Block31db9f72012-07-25 23:19:24 +02002205 if (ret < 0)
2206 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002207
Alexander Block31db9f72012-07-25 23:19:24 +02002208 ret = fs_path_add_path(dest, name);
2209 if (ret < 0)
2210 goto out;
2211
2212 ino = parent_inode;
2213 gen = parent_gen;
2214 }
2215
2216out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002217 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002218 if (!ret)
2219 fs_path_unreverse(dest);
2220 return ret;
2221}
2222
2223/*
Alexander Block31db9f72012-07-25 23:19:24 +02002224 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2225 */
2226static int send_subvol_begin(struct send_ctx *sctx)
2227{
2228 int ret;
2229 struct btrfs_root *send_root = sctx->send_root;
2230 struct btrfs_root *parent_root = sctx->parent_root;
2231 struct btrfs_path *path;
2232 struct btrfs_key key;
2233 struct btrfs_root_ref *ref;
2234 struct extent_buffer *leaf;
2235 char *name = NULL;
2236 int namelen;
2237
Wang Shilongffcfaf82014-01-15 00:26:43 +08002238 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002239 if (!path)
2240 return -ENOMEM;
2241
2242 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2243 if (!name) {
2244 btrfs_free_path(path);
2245 return -ENOMEM;
2246 }
2247
2248 key.objectid = send_root->objectid;
2249 key.type = BTRFS_ROOT_BACKREF_KEY;
2250 key.offset = 0;
2251
2252 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2253 &key, path, 1, 0);
2254 if (ret < 0)
2255 goto out;
2256 if (ret) {
2257 ret = -ENOENT;
2258 goto out;
2259 }
2260
2261 leaf = path->nodes[0];
2262 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2263 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2264 key.objectid != send_root->objectid) {
2265 ret = -ENOENT;
2266 goto out;
2267 }
2268 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2269 namelen = btrfs_root_ref_name_len(leaf, ref);
2270 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2271 btrfs_release_path(path);
2272
Alexander Block31db9f72012-07-25 23:19:24 +02002273 if (parent_root) {
2274 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2275 if (ret < 0)
2276 goto out;
2277 } else {
2278 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2279 if (ret < 0)
2280 goto out;
2281 }
2282
2283 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2284 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2285 sctx->send_root->root_item.uuid);
2286 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002287 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002288 if (parent_root) {
2289 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2290 sctx->parent_root->root_item.uuid);
2291 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002292 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002293 }
2294
2295 ret = send_cmd(sctx);
2296
2297tlv_put_failure:
2298out:
2299 btrfs_free_path(path);
2300 kfree(name);
2301 return ret;
2302}
2303
2304static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2305{
2306 int ret = 0;
2307 struct fs_path *p;
2308
2309verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2310
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002311 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002312 if (!p)
2313 return -ENOMEM;
2314
2315 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2316 if (ret < 0)
2317 goto out;
2318
2319 ret = get_cur_path(sctx, ino, gen, p);
2320 if (ret < 0)
2321 goto out;
2322 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2323 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2324
2325 ret = send_cmd(sctx);
2326
2327tlv_put_failure:
2328out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002329 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002330 return ret;
2331}
2332
2333static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2334{
2335 int ret = 0;
2336 struct fs_path *p;
2337
2338verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2339
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002340 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002341 if (!p)
2342 return -ENOMEM;
2343
2344 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2345 if (ret < 0)
2346 goto out;
2347
2348 ret = get_cur_path(sctx, ino, gen, p);
2349 if (ret < 0)
2350 goto out;
2351 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2352 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2353
2354 ret = send_cmd(sctx);
2355
2356tlv_put_failure:
2357out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002358 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002359 return ret;
2360}
2361
2362static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2363{
2364 int ret = 0;
2365 struct fs_path *p;
2366
2367verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2368
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002369 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002370 if (!p)
2371 return -ENOMEM;
2372
2373 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2374 if (ret < 0)
2375 goto out;
2376
2377 ret = get_cur_path(sctx, ino, gen, p);
2378 if (ret < 0)
2379 goto out;
2380 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2381 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2382 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2383
2384 ret = send_cmd(sctx);
2385
2386tlv_put_failure:
2387out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002388 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002389 return ret;
2390}
2391
2392static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2393{
2394 int ret = 0;
2395 struct fs_path *p = NULL;
2396 struct btrfs_inode_item *ii;
2397 struct btrfs_path *path = NULL;
2398 struct extent_buffer *eb;
2399 struct btrfs_key key;
2400 int slot;
2401
2402verbose_printk("btrfs: send_utimes %llu\n", ino);
2403
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002404 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002405 if (!p)
2406 return -ENOMEM;
2407
2408 path = alloc_path_for_send();
2409 if (!path) {
2410 ret = -ENOMEM;
2411 goto out;
2412 }
2413
2414 key.objectid = ino;
2415 key.type = BTRFS_INODE_ITEM_KEY;
2416 key.offset = 0;
2417 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2418 if (ret < 0)
2419 goto out;
2420
2421 eb = path->nodes[0];
2422 slot = path->slots[0];
2423 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2424
2425 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2426 if (ret < 0)
2427 goto out;
2428
2429 ret = get_cur_path(sctx, ino, gen, p);
2430 if (ret < 0)
2431 goto out;
2432 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2433 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2434 btrfs_inode_atime(ii));
2435 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2436 btrfs_inode_mtime(ii));
2437 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2438 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002439 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002440
2441 ret = send_cmd(sctx);
2442
2443tlv_put_failure:
2444out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002445 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002446 btrfs_free_path(path);
2447 return ret;
2448}
2449
2450/*
2451 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2452 * a valid path yet because we did not process the refs yet. So, the inode
2453 * is created as orphan.
2454 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002455static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002456{
2457 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002458 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002459 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002460 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002461 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002462 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002463
Alexander Block1f4692d2012-07-28 10:42:24 +02002464verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002465
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002466 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002467 if (!p)
2468 return -ENOMEM;
2469
Liu Bo644d1942014-02-27 17:29:01 +08002470 if (ino != sctx->cur_ino) {
2471 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2472 NULL, NULL, &rdev);
2473 if (ret < 0)
2474 goto out;
2475 } else {
2476 gen = sctx->cur_inode_gen;
2477 mode = sctx->cur_inode_mode;
2478 rdev = sctx->cur_inode_rdev;
2479 }
Alexander Block31db9f72012-07-25 23:19:24 +02002480
Alexander Blocke938c8a2012-07-28 16:33:49 +02002481 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002482 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002483 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002484 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002485 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002486 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002487 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002488 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002489 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002490 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002491 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002492 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002493 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002494 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2495 (int)(mode & S_IFMT));
2496 ret = -ENOTSUPP;
2497 goto out;
2498 }
2499
2500 ret = begin_cmd(sctx, cmd);
2501 if (ret < 0)
2502 goto out;
2503
Alexander Block1f4692d2012-07-28 10:42:24 +02002504 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002505 if (ret < 0)
2506 goto out;
2507
2508 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002509 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002510
2511 if (S_ISLNK(mode)) {
2512 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002513 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002514 if (ret < 0)
2515 goto out;
2516 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2517 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2518 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002519 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2520 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002521 }
2522
2523 ret = send_cmd(sctx);
2524 if (ret < 0)
2525 goto out;
2526
2527
2528tlv_put_failure:
2529out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002530 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002531 return ret;
2532}
2533
Alexander Block1f4692d2012-07-28 10:42:24 +02002534/*
2535 * We need some special handling for inodes that get processed before the parent
2536 * directory got created. See process_recorded_refs for details.
2537 * This function does the check if we already created the dir out of order.
2538 */
2539static int did_create_dir(struct send_ctx *sctx, u64 dir)
2540{
2541 int ret = 0;
2542 struct btrfs_path *path = NULL;
2543 struct btrfs_key key;
2544 struct btrfs_key found_key;
2545 struct btrfs_key di_key;
2546 struct extent_buffer *eb;
2547 struct btrfs_dir_item *di;
2548 int slot;
2549
2550 path = alloc_path_for_send();
2551 if (!path) {
2552 ret = -ENOMEM;
2553 goto out;
2554 }
2555
2556 key.objectid = dir;
2557 key.type = BTRFS_DIR_INDEX_KEY;
2558 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002559 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2560 if (ret < 0)
2561 goto out;
2562
Alexander Block1f4692d2012-07-28 10:42:24 +02002563 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002564 eb = path->nodes[0];
2565 slot = path->slots[0];
2566 if (slot >= btrfs_header_nritems(eb)) {
2567 ret = btrfs_next_leaf(sctx->send_root, path);
2568 if (ret < 0) {
2569 goto out;
2570 } else if (ret > 0) {
2571 ret = 0;
2572 break;
2573 }
2574 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002575 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002576
2577 btrfs_item_key_to_cpu(eb, &found_key, slot);
2578 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002579 found_key.type != key.type) {
2580 ret = 0;
2581 goto out;
2582 }
2583
2584 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2585 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2586
Josef Bacika0525412013-08-12 10:56:14 -04002587 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2588 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002589 ret = 1;
2590 goto out;
2591 }
2592
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002593 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002594 }
2595
2596out:
2597 btrfs_free_path(path);
2598 return ret;
2599}
2600
2601/*
2602 * Only creates the inode if it is:
2603 * 1. Not a directory
2604 * 2. Or a directory which was not created already due to out of order
2605 * directories. See did_create_dir and process_recorded_refs for details.
2606 */
2607static int send_create_inode_if_needed(struct send_ctx *sctx)
2608{
2609 int ret;
2610
2611 if (S_ISDIR(sctx->cur_inode_mode)) {
2612 ret = did_create_dir(sctx, sctx->cur_ino);
2613 if (ret < 0)
2614 goto out;
2615 if (ret) {
2616 ret = 0;
2617 goto out;
2618 }
2619 }
2620
2621 ret = send_create_inode(sctx, sctx->cur_ino);
2622 if (ret < 0)
2623 goto out;
2624
2625out:
2626 return ret;
2627}
2628
Alexander Block31db9f72012-07-25 23:19:24 +02002629struct recorded_ref {
2630 struct list_head list;
2631 char *dir_path;
2632 char *name;
2633 struct fs_path *full_path;
2634 u64 dir;
2635 u64 dir_gen;
2636 int dir_path_len;
2637 int name_len;
2638};
2639
2640/*
2641 * We need to process new refs before deleted refs, but compare_tree gives us
2642 * everything mixed. So we first record all refs and later process them.
2643 * This function is a helper to record one ref.
2644 */
Liu Boa4d96d62014-03-03 21:31:03 +08002645static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002646 u64 dir_gen, struct fs_path *path)
2647{
2648 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002649
2650 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2651 if (!ref)
2652 return -ENOMEM;
2653
2654 ref->dir = dir;
2655 ref->dir_gen = dir_gen;
2656 ref->full_path = path;
2657
Andy Shevchenkoed848852013-08-21 10:32:13 +03002658 ref->name = (char *)kbasename(ref->full_path->start);
2659 ref->name_len = ref->full_path->end - ref->name;
2660 ref->dir_path = ref->full_path->start;
2661 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002662 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002663 else
Alexander Block31db9f72012-07-25 23:19:24 +02002664 ref->dir_path_len = ref->full_path->end -
2665 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002666
2667 list_add_tail(&ref->list, head);
2668 return 0;
2669}
2670
Josef Bacikba5e8f22013-08-16 16:52:55 -04002671static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2672{
2673 struct recorded_ref *new;
2674
2675 new = kmalloc(sizeof(*ref), GFP_NOFS);
2676 if (!new)
2677 return -ENOMEM;
2678
2679 new->dir = ref->dir;
2680 new->dir_gen = ref->dir_gen;
2681 new->full_path = NULL;
2682 INIT_LIST_HEAD(&new->list);
2683 list_add_tail(&new->list, list);
2684 return 0;
2685}
2686
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002687static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002688{
2689 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002690
Alexander Blocke938c8a2012-07-28 16:33:49 +02002691 while (!list_empty(head)) {
2692 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002693 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002694 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002695 kfree(cur);
2696 }
Alexander Block31db9f72012-07-25 23:19:24 +02002697}
2698
2699static void free_recorded_refs(struct send_ctx *sctx)
2700{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002701 __free_recorded_refs(&sctx->new_refs);
2702 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002703}
2704
2705/*
Alexander Block766702e2012-07-28 14:11:31 +02002706 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002707 * ref of an unprocessed inode gets overwritten and for all non empty
2708 * directories.
2709 */
2710static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2711 struct fs_path *path)
2712{
2713 int ret;
2714 struct fs_path *orphan;
2715
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002716 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002717 if (!orphan)
2718 return -ENOMEM;
2719
2720 ret = gen_unique_name(sctx, ino, gen, orphan);
2721 if (ret < 0)
2722 goto out;
2723
2724 ret = send_rename(sctx, path, orphan);
2725
2726out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002727 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002728 return ret;
2729}
2730
Filipe Manana9dc44212014-02-19 14:31:44 +00002731static struct orphan_dir_info *
2732add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2733{
2734 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2735 struct rb_node *parent = NULL;
2736 struct orphan_dir_info *entry, *odi;
2737
2738 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2739 if (!odi)
2740 return ERR_PTR(-ENOMEM);
2741 odi->ino = dir_ino;
2742 odi->gen = 0;
2743
2744 while (*p) {
2745 parent = *p;
2746 entry = rb_entry(parent, struct orphan_dir_info, node);
2747 if (dir_ino < entry->ino) {
2748 p = &(*p)->rb_left;
2749 } else if (dir_ino > entry->ino) {
2750 p = &(*p)->rb_right;
2751 } else {
2752 kfree(odi);
2753 return entry;
2754 }
2755 }
2756
2757 rb_link_node(&odi->node, parent, p);
2758 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2759 return odi;
2760}
2761
2762static struct orphan_dir_info *
2763get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2764{
2765 struct rb_node *n = sctx->orphan_dirs.rb_node;
2766 struct orphan_dir_info *entry;
2767
2768 while (n) {
2769 entry = rb_entry(n, struct orphan_dir_info, node);
2770 if (dir_ino < entry->ino)
2771 n = n->rb_left;
2772 else if (dir_ino > entry->ino)
2773 n = n->rb_right;
2774 else
2775 return entry;
2776 }
2777 return NULL;
2778}
2779
2780static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2781{
2782 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2783
2784 return odi != NULL;
2785}
2786
2787static void free_orphan_dir_info(struct send_ctx *sctx,
2788 struct orphan_dir_info *odi)
2789{
2790 if (!odi)
2791 return;
2792 rb_erase(&odi->node, &sctx->orphan_dirs);
2793 kfree(odi);
2794}
2795
Alexander Block31db9f72012-07-25 23:19:24 +02002796/*
2797 * Returns 1 if a directory can be removed at this point in time.
2798 * We check this by iterating all dir items and checking if the inode behind
2799 * the dir item was already processed.
2800 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002801static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2802 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002803{
2804 int ret = 0;
2805 struct btrfs_root *root = sctx->parent_root;
2806 struct btrfs_path *path;
2807 struct btrfs_key key;
2808 struct btrfs_key found_key;
2809 struct btrfs_key loc;
2810 struct btrfs_dir_item *di;
2811
Alexander Block6d85ed02012-08-01 14:48:59 +02002812 /*
2813 * Don't try to rmdir the top/root subvolume dir.
2814 */
2815 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2816 return 0;
2817
Alexander Block31db9f72012-07-25 23:19:24 +02002818 path = alloc_path_for_send();
2819 if (!path)
2820 return -ENOMEM;
2821
2822 key.objectid = dir;
2823 key.type = BTRFS_DIR_INDEX_KEY;
2824 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002825 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2826 if (ret < 0)
2827 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002828
2829 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002830 struct waiting_dir_move *dm;
2831
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002832 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2833 ret = btrfs_next_leaf(root, path);
2834 if (ret < 0)
2835 goto out;
2836 else if (ret > 0)
2837 break;
2838 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002839 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002840 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2841 path->slots[0]);
2842 if (found_key.objectid != key.objectid ||
2843 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002844 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002845
2846 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2847 struct btrfs_dir_item);
2848 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2849
Filipe Manana9dc44212014-02-19 14:31:44 +00002850 dm = get_waiting_dir_move(sctx, loc.objectid);
2851 if (dm) {
2852 struct orphan_dir_info *odi;
2853
2854 odi = add_orphan_dir_info(sctx, dir);
2855 if (IS_ERR(odi)) {
2856 ret = PTR_ERR(odi);
2857 goto out;
2858 }
2859 odi->gen = dir_gen;
2860 dm->rmdir_ino = dir;
2861 ret = 0;
2862 goto out;
2863 }
2864
Alexander Block31db9f72012-07-25 23:19:24 +02002865 if (loc.objectid > send_progress) {
2866 ret = 0;
2867 goto out;
2868 }
2869
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002870 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002871 }
2872
2873 ret = 1;
2874
2875out:
2876 btrfs_free_path(path);
2877 return ret;
2878}
2879
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002880static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2881{
Filipe Manana9dc44212014-02-19 14:31:44 +00002882 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002883
Filipe Manana9dc44212014-02-19 14:31:44 +00002884 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002885}
2886
2887static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2888{
2889 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2890 struct rb_node *parent = NULL;
2891 struct waiting_dir_move *entry, *dm;
2892
2893 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2894 if (!dm)
2895 return -ENOMEM;
2896 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00002897 dm->rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002898
2899 while (*p) {
2900 parent = *p;
2901 entry = rb_entry(parent, struct waiting_dir_move, node);
2902 if (ino < entry->ino) {
2903 p = &(*p)->rb_left;
2904 } else if (ino > entry->ino) {
2905 p = &(*p)->rb_right;
2906 } else {
2907 kfree(dm);
2908 return -EEXIST;
2909 }
2910 }
2911
2912 rb_link_node(&dm->node, parent, p);
2913 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2914 return 0;
2915}
2916
Filipe Manana9dc44212014-02-19 14:31:44 +00002917static struct waiting_dir_move *
2918get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002919{
2920 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2921 struct waiting_dir_move *entry;
2922
2923 while (n) {
2924 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00002925 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002926 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00002927 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002928 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002929 else
2930 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002931 }
Filipe Manana9dc44212014-02-19 14:31:44 +00002932 return NULL;
2933}
2934
2935static void free_waiting_dir_move(struct send_ctx *sctx,
2936 struct waiting_dir_move *dm)
2937{
2938 if (!dm)
2939 return;
2940 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2941 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002942}
2943
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00002944static int add_pending_dir_move(struct send_ctx *sctx,
2945 u64 ino,
2946 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00002947 u64 parent_ino,
2948 struct list_head *new_refs,
2949 struct list_head *deleted_refs)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002950{
2951 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2952 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07002953 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002954 struct recorded_ref *cur;
2955 int exists = 0;
2956 int ret;
2957
2958 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2959 if (!pm)
2960 return -ENOMEM;
2961 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00002962 pm->ino = ino;
2963 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002964 INIT_LIST_HEAD(&pm->list);
2965 INIT_LIST_HEAD(&pm->update_refs);
2966 RB_CLEAR_NODE(&pm->node);
2967
2968 while (*p) {
2969 parent = *p;
2970 entry = rb_entry(parent, struct pending_dir_move, node);
2971 if (parent_ino < entry->parent_ino) {
2972 p = &(*p)->rb_left;
2973 } else if (parent_ino > entry->parent_ino) {
2974 p = &(*p)->rb_right;
2975 } else {
2976 exists = 1;
2977 break;
2978 }
2979 }
2980
Filipe Mananaf9594922014-03-27 20:14:01 +00002981 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002982 ret = dup_ref(cur, &pm->update_refs);
2983 if (ret < 0)
2984 goto out;
2985 }
Filipe Mananaf9594922014-03-27 20:14:01 +00002986 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002987 ret = dup_ref(cur, &pm->update_refs);
2988 if (ret < 0)
2989 goto out;
2990 }
2991
2992 ret = add_waiting_dir_move(sctx, pm->ino);
2993 if (ret)
2994 goto out;
2995
2996 if (exists) {
2997 list_add_tail(&pm->list, &entry->list);
2998 } else {
2999 rb_link_node(&pm->node, parent, p);
3000 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3001 }
3002 ret = 0;
3003out:
3004 if (ret) {
3005 __free_recorded_refs(&pm->update_refs);
3006 kfree(pm);
3007 }
3008 return ret;
3009}
3010
3011static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3012 u64 parent_ino)
3013{
3014 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3015 struct pending_dir_move *entry;
3016
3017 while (n) {
3018 entry = rb_entry(n, struct pending_dir_move, node);
3019 if (parent_ino < entry->parent_ino)
3020 n = n->rb_left;
3021 else if (parent_ino > entry->parent_ino)
3022 n = n->rb_right;
3023 else
3024 return entry;
3025 }
3026 return NULL;
3027}
3028
Filipe Mananaf9594922014-03-27 20:14:01 +00003029static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3030 u64 ino, u64 gen, u64 *ancestor_ino)
3031{
3032 int ret = 0;
3033 u64 parent_inode = 0;
3034 u64 parent_gen = 0;
3035 u64 start_ino = ino;
3036
3037 *ancestor_ino = 0;
3038 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3039 fs_path_reset(name);
3040
3041 if (is_waiting_for_rm(sctx, ino))
3042 break;
3043 if (is_waiting_for_move(sctx, ino)) {
3044 if (*ancestor_ino == 0)
3045 *ancestor_ino = ino;
3046 ret = get_first_ref(sctx->parent_root, ino,
3047 &parent_inode, &parent_gen, name);
3048 } else {
3049 ret = __get_cur_name_and_parent(sctx, ino, gen,
3050 &parent_inode,
3051 &parent_gen, name);
3052 if (ret > 0) {
3053 ret = 0;
3054 break;
3055 }
3056 }
3057 if (ret < 0)
3058 break;
3059 if (parent_inode == start_ino) {
3060 ret = 1;
3061 if (*ancestor_ino == 0)
3062 *ancestor_ino = ino;
3063 break;
3064 }
3065 ino = parent_inode;
3066 gen = parent_gen;
3067 }
3068 return ret;
3069}
3070
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003071static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3072{
3073 struct fs_path *from_path = NULL;
3074 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003075 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003076 u64 orig_progress = sctx->send_progress;
3077 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003078 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003079 struct waiting_dir_move *dm = NULL;
3080 u64 rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003081 int ret;
Filipe Mananaf9594922014-03-27 20:14:01 +00003082 u64 ancestor = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003083
Filipe Manana2b863a12014-02-16 13:43:11 +00003084 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003085 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003086 if (!name || !from_path) {
3087 ret = -ENOMEM;
3088 goto out;
3089 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003090
Filipe Manana9dc44212014-02-19 14:31:44 +00003091 dm = get_waiting_dir_move(sctx, pm->ino);
3092 ASSERT(dm);
3093 rmdir_ino = dm->rmdir_ino;
3094 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003095
3096 ret = get_first_ref(sctx->parent_root, pm->ino,
3097 &parent_ino, &parent_gen, name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003098 if (ret < 0)
3099 goto out;
3100
Filipe Mananac992ec92014-03-22 17:15:24 +00003101 ret = get_cur_path(sctx, parent_ino, parent_gen,
3102 from_path);
3103 if (ret < 0)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003104 goto out;
Filipe Mananac992ec92014-03-22 17:15:24 +00003105 ret = fs_path_add_path(from_path, name);
3106 if (ret < 0)
3107 goto out;
3108
Filipe Mananaf9594922014-03-27 20:14:01 +00003109 sctx->send_progress = sctx->cur_ino + 1;
3110 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3111 if (ret) {
3112 LIST_HEAD(deleted_refs);
3113 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3114 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3115 &pm->update_refs, &deleted_refs);
3116 if (ret < 0)
3117 goto out;
3118 if (rmdir_ino) {
3119 dm = get_waiting_dir_move(sctx, pm->ino);
3120 ASSERT(dm);
3121 dm->rmdir_ino = rmdir_ino;
3122 }
3123 goto out;
3124 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003125 fs_path_reset(name);
3126 to_path = name;
3127 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003128 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3129 if (ret < 0)
3130 goto out;
3131
3132 ret = send_rename(sctx, from_path, to_path);
3133 if (ret < 0)
3134 goto out;
3135
Filipe Manana9dc44212014-02-19 14:31:44 +00003136 if (rmdir_ino) {
3137 struct orphan_dir_info *odi;
3138
3139 odi = get_orphan_dir_info(sctx, rmdir_ino);
3140 if (!odi) {
3141 /* already deleted */
3142 goto finish;
3143 }
3144 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3145 if (ret < 0)
3146 goto out;
3147 if (!ret)
3148 goto finish;
3149
3150 name = fs_path_alloc();
3151 if (!name) {
3152 ret = -ENOMEM;
3153 goto out;
3154 }
3155 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3156 if (ret < 0)
3157 goto out;
3158 ret = send_rmdir(sctx, name);
3159 if (ret < 0)
3160 goto out;
3161 free_orphan_dir_info(sctx, odi);
3162 }
3163
3164finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003165 ret = send_utimes(sctx, pm->ino, pm->gen);
3166 if (ret < 0)
3167 goto out;
3168
3169 /*
3170 * After rename/move, need to update the utimes of both new parent(s)
3171 * and old parent(s).
3172 */
3173 list_for_each_entry(cur, &pm->update_refs, list) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003174 if (cur->dir == rmdir_ino)
3175 continue;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003176 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3177 if (ret < 0)
3178 goto out;
3179 }
3180
3181out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003182 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003183 fs_path_free(from_path);
3184 fs_path_free(to_path);
3185 sctx->send_progress = orig_progress;
3186
3187 return ret;
3188}
3189
3190static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3191{
3192 if (!list_empty(&m->list))
3193 list_del(&m->list);
3194 if (!RB_EMPTY_NODE(&m->node))
3195 rb_erase(&m->node, &sctx->pending_dir_moves);
3196 __free_recorded_refs(&m->update_refs);
3197 kfree(m);
3198}
3199
3200static void tail_append_pending_moves(struct pending_dir_move *moves,
3201 struct list_head *stack)
3202{
3203 if (list_empty(&moves->list)) {
3204 list_add_tail(&moves->list, stack);
3205 } else {
3206 LIST_HEAD(list);
3207 list_splice_init(&moves->list, &list);
3208 list_add_tail(&moves->list, stack);
3209 list_splice_tail(&list, stack);
3210 }
3211}
3212
3213static int apply_children_dir_moves(struct send_ctx *sctx)
3214{
3215 struct pending_dir_move *pm;
3216 struct list_head stack;
3217 u64 parent_ino = sctx->cur_ino;
3218 int ret = 0;
3219
3220 pm = get_pending_dir_moves(sctx, parent_ino);
3221 if (!pm)
3222 return 0;
3223
3224 INIT_LIST_HEAD(&stack);
3225 tail_append_pending_moves(pm, &stack);
3226
3227 while (!list_empty(&stack)) {
3228 pm = list_first_entry(&stack, struct pending_dir_move, list);
3229 parent_ino = pm->ino;
3230 ret = apply_dir_move(sctx, pm);
3231 free_pending_move(sctx, pm);
3232 if (ret)
3233 goto out;
3234 pm = get_pending_dir_moves(sctx, parent_ino);
3235 if (pm)
3236 tail_append_pending_moves(pm, &stack);
3237 }
3238 return 0;
3239
3240out:
3241 while (!list_empty(&stack)) {
3242 pm = list_first_entry(&stack, struct pending_dir_move, list);
3243 free_pending_move(sctx, pm);
3244 }
3245 return ret;
3246}
3247
3248static int wait_for_parent_move(struct send_ctx *sctx,
3249 struct recorded_ref *parent_ref)
3250{
Filipe Mananaf9594922014-03-27 20:14:01 +00003251 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003252 u64 ino = parent_ref->dir;
3253 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003254 struct fs_path *path_before = NULL;
3255 struct fs_path *path_after = NULL;
3256 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003257
3258 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003259 path_before = fs_path_alloc();
3260 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003261 ret = -ENOMEM;
3262 goto out;
3263 }
3264
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003265 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003266 * Our current directory inode may not yet be renamed/moved because some
3267 * ancestor (immediate or not) has to be renamed/moved first. So find if
3268 * such ancestor exists and make sure our own rename/move happens after
3269 * that ancestor is processed.
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003270 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003271 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3272 if (is_waiting_for_move(sctx, ino)) {
3273 ret = 1;
3274 break;
3275 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003276
3277 fs_path_reset(path_before);
3278 fs_path_reset(path_after);
3279
3280 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananaf9594922014-03-27 20:14:01 +00003281 NULL, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003282 if (ret < 0)
3283 goto out;
3284 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3285 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003286 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003287 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003288 } else if (ret == -ENOENT) {
3289 ret = 1;
3290 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003291 }
3292
3293 len1 = fs_path_len(path_before);
3294 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003295 if (ino > sctx->cur_ino &&
3296 (parent_ino_before != parent_ino_after || len1 != len2 ||
3297 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003298 ret = 1;
Filipe Mananaf9594922014-03-27 20:14:01 +00003299 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003300 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003301 ino = parent_ino_after;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003302 }
3303
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003304out:
3305 fs_path_free(path_before);
3306 fs_path_free(path_after);
3307
Filipe Mananaf9594922014-03-27 20:14:01 +00003308 if (ret == 1) {
3309 ret = add_pending_dir_move(sctx,
3310 sctx->cur_ino,
3311 sctx->cur_inode_gen,
3312 ino,
3313 &sctx->new_refs,
3314 &sctx->deleted_refs);
3315 if (!ret)
3316 ret = 1;
3317 }
3318
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003319 return ret;
3320}
3321
Alexander Block31db9f72012-07-25 23:19:24 +02003322/*
3323 * This does all the move/link/unlink/rmdir magic.
3324 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003325static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003326{
3327 int ret = 0;
3328 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003329 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003330 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003331 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003332 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003333 u64 ow_gen;
3334 int did_overwrite = 0;
3335 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003336 u64 last_dir_ino_rm = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003337
3338verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3339
Alexander Block6d85ed02012-08-01 14:48:59 +02003340 /*
3341 * This should never happen as the root dir always has the same ref
3342 * which is always '..'
3343 */
3344 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003345 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003346
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003347 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003348 if (!valid_path) {
3349 ret = -ENOMEM;
3350 goto out;
3351 }
3352
Alexander Block31db9f72012-07-25 23:19:24 +02003353 /*
3354 * First, check if the first ref of the current inode was overwritten
3355 * before. If yes, we know that the current inode was already orphanized
3356 * and thus use the orphan name. If not, we can use get_cur_path to
3357 * get the path of the first ref as it would like while receiving at
3358 * this point in time.
3359 * New inodes are always orphan at the beginning, so force to use the
3360 * orphan name in this case.
3361 * The first ref is stored in valid_path and will be updated if it
3362 * gets moved around.
3363 */
3364 if (!sctx->cur_inode_new) {
3365 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3366 sctx->cur_inode_gen);
3367 if (ret < 0)
3368 goto out;
3369 if (ret)
3370 did_overwrite = 1;
3371 }
3372 if (sctx->cur_inode_new || did_overwrite) {
3373 ret = gen_unique_name(sctx, sctx->cur_ino,
3374 sctx->cur_inode_gen, valid_path);
3375 if (ret < 0)
3376 goto out;
3377 is_orphan = 1;
3378 } else {
3379 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3380 valid_path);
3381 if (ret < 0)
3382 goto out;
3383 }
3384
3385 list_for_each_entry(cur, &sctx->new_refs, list) {
3386 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003387 * We may have refs where the parent directory does not exist
3388 * yet. This happens if the parent directories inum is higher
3389 * the the current inum. To handle this case, we create the
3390 * parent directory out of order. But we need to check if this
3391 * did already happen before due to other refs in the same dir.
3392 */
3393 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3394 if (ret < 0)
3395 goto out;
3396 if (ret == inode_state_will_create) {
3397 ret = 0;
3398 /*
3399 * First check if any of the current inodes refs did
3400 * already create the dir.
3401 */
3402 list_for_each_entry(cur2, &sctx->new_refs, list) {
3403 if (cur == cur2)
3404 break;
3405 if (cur2->dir == cur->dir) {
3406 ret = 1;
3407 break;
3408 }
3409 }
3410
3411 /*
3412 * If that did not happen, check if a previous inode
3413 * did already create the dir.
3414 */
3415 if (!ret)
3416 ret = did_create_dir(sctx, cur->dir);
3417 if (ret < 0)
3418 goto out;
3419 if (!ret) {
3420 ret = send_create_inode(sctx, cur->dir);
3421 if (ret < 0)
3422 goto out;
3423 }
3424 }
3425
3426 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003427 * Check if this new ref would overwrite the first ref of
3428 * another unprocessed inode. If yes, orphanize the
3429 * overwritten inode. If we find an overwritten ref that is
3430 * not the first ref, simply unlink it.
3431 */
3432 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3433 cur->name, cur->name_len,
3434 &ow_inode, &ow_gen);
3435 if (ret < 0)
3436 goto out;
3437 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003438 ret = is_first_ref(sctx->parent_root,
3439 ow_inode, cur->dir, cur->name,
3440 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003441 if (ret < 0)
3442 goto out;
3443 if (ret) {
3444 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3445 cur->full_path);
3446 if (ret < 0)
3447 goto out;
3448 } else {
3449 ret = send_unlink(sctx, cur->full_path);
3450 if (ret < 0)
3451 goto out;
3452 }
3453 }
3454
3455 /*
3456 * link/move the ref to the new place. If we have an orphan
3457 * inode, move it and update valid_path. If not, link or move
3458 * it depending on the inode mode.
3459 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003460 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003461 ret = send_rename(sctx, valid_path, cur->full_path);
3462 if (ret < 0)
3463 goto out;
3464 is_orphan = 0;
3465 ret = fs_path_copy(valid_path, cur->full_path);
3466 if (ret < 0)
3467 goto out;
3468 } else {
3469 if (S_ISDIR(sctx->cur_inode_mode)) {
3470 /*
3471 * Dirs can't be linked, so move it. For moved
3472 * dirs, we always have one new and one deleted
3473 * ref. The deleted ref is ignored later.
3474 */
Filipe David Borba Mananad86477b2014-01-30 13:27:12 +00003475 ret = wait_for_parent_move(sctx, cur);
3476 if (ret < 0)
3477 goto out;
3478 if (ret) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003479 *pending_move = 1;
3480 } else {
3481 ret = send_rename(sctx, valid_path,
3482 cur->full_path);
3483 if (!ret)
3484 ret = fs_path_copy(valid_path,
3485 cur->full_path);
3486 }
Alexander Block31db9f72012-07-25 23:19:24 +02003487 if (ret < 0)
3488 goto out;
3489 } else {
3490 ret = send_link(sctx, cur->full_path,
3491 valid_path);
3492 if (ret < 0)
3493 goto out;
3494 }
3495 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003496 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003497 if (ret < 0)
3498 goto out;
3499 }
3500
3501 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3502 /*
3503 * Check if we can already rmdir the directory. If not,
3504 * orphanize it. For every dir item inside that gets deleted
3505 * later, we do this check again and rmdir it then if possible.
3506 * See the use of check_dirs for more details.
3507 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003508 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3509 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003510 if (ret < 0)
3511 goto out;
3512 if (ret) {
3513 ret = send_rmdir(sctx, valid_path);
3514 if (ret < 0)
3515 goto out;
3516 } else if (!is_orphan) {
3517 ret = orphanize_inode(sctx, sctx->cur_ino,
3518 sctx->cur_inode_gen, valid_path);
3519 if (ret < 0)
3520 goto out;
3521 is_orphan = 1;
3522 }
3523
3524 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003525 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003526 if (ret < 0)
3527 goto out;
3528 }
Alexander Blockccf16262012-07-28 11:46:29 +02003529 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3530 !list_empty(&sctx->deleted_refs)) {
3531 /*
3532 * We have a moved dir. Add the old parent to check_dirs
3533 */
3534 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3535 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003536 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003537 if (ret < 0)
3538 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003539 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3540 /*
3541 * We have a non dir inode. Go through all deleted refs and
3542 * unlink them if they were not already overwritten by other
3543 * inodes.
3544 */
3545 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3546 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3547 sctx->cur_ino, sctx->cur_inode_gen,
3548 cur->name, cur->name_len);
3549 if (ret < 0)
3550 goto out;
3551 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003552 ret = send_unlink(sctx, cur->full_path);
3553 if (ret < 0)
3554 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003555 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003556 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003557 if (ret < 0)
3558 goto out;
3559 }
Alexander Block31db9f72012-07-25 23:19:24 +02003560 /*
3561 * If the inode is still orphan, unlink the orphan. This may
3562 * happen when a previous inode did overwrite the first ref
3563 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003564 * inode. Unlinking does not mean that the inode is deleted in
3565 * all cases. There may still be links to this inode in other
3566 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003567 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003568 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003569 ret = send_unlink(sctx, valid_path);
3570 if (ret < 0)
3571 goto out;
3572 }
3573 }
3574
3575 /*
3576 * We did collect all parent dirs where cur_inode was once located. We
3577 * now go through all these dirs and check if they are pending for
3578 * deletion and if it's finally possible to perform the rmdir now.
3579 * We also update the inode stats of the parent dirs here.
3580 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003581 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003582 /*
3583 * In case we had refs into dirs that were not processed yet,
3584 * we don't need to do the utime and rmdir logic for these dirs.
3585 * The dir will be processed later.
3586 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003587 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003588 continue;
3589
Josef Bacikba5e8f22013-08-16 16:52:55 -04003590 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003591 if (ret < 0)
3592 goto out;
3593
3594 if (ret == inode_state_did_create ||
3595 ret == inode_state_no_change) {
3596 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003597 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003598 if (ret < 0)
3599 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003600 } else if (ret == inode_state_did_delete &&
3601 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003602 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3603 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003604 if (ret < 0)
3605 goto out;
3606 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003607 ret = get_cur_path(sctx, cur->dir,
3608 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003609 if (ret < 0)
3610 goto out;
3611 ret = send_rmdir(sctx, valid_path);
3612 if (ret < 0)
3613 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003614 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02003615 }
3616 }
3617 }
3618
Alexander Block31db9f72012-07-25 23:19:24 +02003619 ret = 0;
3620
3621out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04003622 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003623 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003624 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003625 return ret;
3626}
3627
Liu Boa4d96d62014-03-03 21:31:03 +08003628static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3629 struct fs_path *name, void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02003630{
3631 int ret = 0;
3632 struct send_ctx *sctx = ctx;
3633 struct fs_path *p;
3634 u64 gen;
3635
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003636 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003637 if (!p)
3638 return -ENOMEM;
3639
Liu Boa4d96d62014-03-03 21:31:03 +08003640 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003641 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003642 if (ret < 0)
3643 goto out;
3644
Alexander Block31db9f72012-07-25 23:19:24 +02003645 ret = get_cur_path(sctx, dir, gen, p);
3646 if (ret < 0)
3647 goto out;
3648 ret = fs_path_add_path(p, name);
3649 if (ret < 0)
3650 goto out;
3651
Liu Boa4d96d62014-03-03 21:31:03 +08003652 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003653
3654out:
3655 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003656 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003657 return ret;
3658}
3659
Liu Boa4d96d62014-03-03 21:31:03 +08003660static int __record_new_ref(int num, u64 dir, int index,
3661 struct fs_path *name,
3662 void *ctx)
3663{
3664 struct send_ctx *sctx = ctx;
3665 return record_ref(sctx->send_root, num, dir, index, name,
3666 ctx, &sctx->new_refs);
3667}
3668
3669
Alexander Block31db9f72012-07-25 23:19:24 +02003670static int __record_deleted_ref(int num, u64 dir, int index,
3671 struct fs_path *name,
3672 void *ctx)
3673{
Alexander Block31db9f72012-07-25 23:19:24 +02003674 struct send_ctx *sctx = ctx;
Liu Boa4d96d62014-03-03 21:31:03 +08003675 return record_ref(sctx->parent_root, num, dir, index, name,
3676 ctx, &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02003677}
3678
3679static int record_new_ref(struct send_ctx *sctx)
3680{
3681 int ret;
3682
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003683 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3684 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003685 if (ret < 0)
3686 goto out;
3687 ret = 0;
3688
3689out:
3690 return ret;
3691}
3692
3693static int record_deleted_ref(struct send_ctx *sctx)
3694{
3695 int ret;
3696
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003697 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3698 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003699 if (ret < 0)
3700 goto out;
3701 ret = 0;
3702
3703out:
3704 return ret;
3705}
3706
3707struct find_ref_ctx {
3708 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003709 u64 dir_gen;
3710 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003711 struct fs_path *name;
3712 int found_idx;
3713};
3714
3715static int __find_iref(int num, u64 dir, int index,
3716 struct fs_path *name,
3717 void *ctx_)
3718{
3719 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003720 u64 dir_gen;
3721 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003722
3723 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3724 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003725 /*
3726 * To avoid doing extra lookups we'll only do this if everything
3727 * else matches.
3728 */
3729 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3730 NULL, NULL, NULL);
3731 if (ret)
3732 return ret;
3733 if (dir_gen != ctx->dir_gen)
3734 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003735 ctx->found_idx = num;
3736 return 1;
3737 }
3738 return 0;
3739}
3740
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003741static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003742 struct btrfs_path *path,
3743 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003744 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003745{
3746 int ret;
3747 struct find_ref_ctx ctx;
3748
3749 ctx.dir = dir;
3750 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003751 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003752 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003753 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003754
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003755 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003756 if (ret < 0)
3757 return ret;
3758
3759 if (ctx.found_idx == -1)
3760 return -ENOENT;
3761
3762 return ctx.found_idx;
3763}
3764
3765static int __record_changed_new_ref(int num, u64 dir, int index,
3766 struct fs_path *name,
3767 void *ctx)
3768{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003769 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003770 int ret;
3771 struct send_ctx *sctx = ctx;
3772
Josef Bacikba5e8f22013-08-16 16:52:55 -04003773 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3774 NULL, NULL, NULL);
3775 if (ret)
3776 return ret;
3777
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003778 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003779 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003780 if (ret == -ENOENT)
3781 ret = __record_new_ref(num, dir, index, name, sctx);
3782 else if (ret > 0)
3783 ret = 0;
3784
3785 return ret;
3786}
3787
3788static int __record_changed_deleted_ref(int num, u64 dir, int index,
3789 struct fs_path *name,
3790 void *ctx)
3791{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003792 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003793 int ret;
3794 struct send_ctx *sctx = ctx;
3795
Josef Bacikba5e8f22013-08-16 16:52:55 -04003796 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3797 NULL, NULL, NULL);
3798 if (ret)
3799 return ret;
3800
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003801 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003802 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003803 if (ret == -ENOENT)
3804 ret = __record_deleted_ref(num, dir, index, name, sctx);
3805 else if (ret > 0)
3806 ret = 0;
3807
3808 return ret;
3809}
3810
3811static int record_changed_ref(struct send_ctx *sctx)
3812{
3813 int ret = 0;
3814
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003815 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003816 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3817 if (ret < 0)
3818 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003819 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003820 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3821 if (ret < 0)
3822 goto out;
3823 ret = 0;
3824
3825out:
3826 return ret;
3827}
3828
3829/*
3830 * Record and process all refs at once. Needed when an inode changes the
3831 * generation number, which means that it was deleted and recreated.
3832 */
3833static int process_all_refs(struct send_ctx *sctx,
3834 enum btrfs_compare_tree_result cmd)
3835{
3836 int ret;
3837 struct btrfs_root *root;
3838 struct btrfs_path *path;
3839 struct btrfs_key key;
3840 struct btrfs_key found_key;
3841 struct extent_buffer *eb;
3842 int slot;
3843 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003844 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003845
3846 path = alloc_path_for_send();
3847 if (!path)
3848 return -ENOMEM;
3849
3850 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3851 root = sctx->send_root;
3852 cb = __record_new_ref;
3853 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3854 root = sctx->parent_root;
3855 cb = __record_deleted_ref;
3856 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01003857 btrfs_err(sctx->send_root->fs_info,
3858 "Wrong command %d in process_all_refs", cmd);
3859 ret = -EINVAL;
3860 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003861 }
3862
3863 key.objectid = sctx->cmp_key->objectid;
3864 key.type = BTRFS_INODE_REF_KEY;
3865 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003866 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3867 if (ret < 0)
3868 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003869
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003870 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02003871 eb = path->nodes[0];
3872 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003873 if (slot >= btrfs_header_nritems(eb)) {
3874 ret = btrfs_next_leaf(root, path);
3875 if (ret < 0)
3876 goto out;
3877 else if (ret > 0)
3878 break;
3879 continue;
3880 }
3881
Alexander Block31db9f72012-07-25 23:19:24 +02003882 btrfs_item_key_to_cpu(eb, &found_key, slot);
3883
3884 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003885 (found_key.type != BTRFS_INODE_REF_KEY &&
3886 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003887 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003888
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003889 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003890 if (ret < 0)
3891 goto out;
3892
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003893 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003894 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003895 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003896
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003897 ret = process_recorded_refs(sctx, &pending_move);
3898 /* Only applicable to an incremental send. */
3899 ASSERT(pending_move == 0);
Alexander Block31db9f72012-07-25 23:19:24 +02003900
3901out:
3902 btrfs_free_path(path);
3903 return ret;
3904}
3905
3906static int send_set_xattr(struct send_ctx *sctx,
3907 struct fs_path *path,
3908 const char *name, int name_len,
3909 const char *data, int data_len)
3910{
3911 int ret = 0;
3912
3913 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3914 if (ret < 0)
3915 goto out;
3916
3917 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3918 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3919 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3920
3921 ret = send_cmd(sctx);
3922
3923tlv_put_failure:
3924out:
3925 return ret;
3926}
3927
3928static int send_remove_xattr(struct send_ctx *sctx,
3929 struct fs_path *path,
3930 const char *name, int name_len)
3931{
3932 int ret = 0;
3933
3934 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3935 if (ret < 0)
3936 goto out;
3937
3938 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3939 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3940
3941 ret = send_cmd(sctx);
3942
3943tlv_put_failure:
3944out:
3945 return ret;
3946}
3947
3948static int __process_new_xattr(int num, struct btrfs_key *di_key,
3949 const char *name, int name_len,
3950 const char *data, int data_len,
3951 u8 type, void *ctx)
3952{
3953 int ret;
3954 struct send_ctx *sctx = ctx;
3955 struct fs_path *p;
3956 posix_acl_xattr_header dummy_acl;
3957
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003958 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003959 if (!p)
3960 return -ENOMEM;
3961
3962 /*
3963 * This hack is needed because empty acl's are stored as zero byte
3964 * data in xattrs. Problem with that is, that receiving these zero byte
3965 * acl's will fail later. To fix this, we send a dummy acl list that
3966 * only contains the version number and no entries.
3967 */
3968 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3969 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3970 if (data_len == 0) {
3971 dummy_acl.a_version =
3972 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3973 data = (char *)&dummy_acl;
3974 data_len = sizeof(dummy_acl);
3975 }
3976 }
3977
3978 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3979 if (ret < 0)
3980 goto out;
3981
3982 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3983
3984out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003985 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003986 return ret;
3987}
3988
3989static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3990 const char *name, int name_len,
3991 const char *data, int data_len,
3992 u8 type, void *ctx)
3993{
3994 int ret;
3995 struct send_ctx *sctx = ctx;
3996 struct fs_path *p;
3997
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003998 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003999 if (!p)
4000 return -ENOMEM;
4001
4002 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4003 if (ret < 0)
4004 goto out;
4005
4006 ret = send_remove_xattr(sctx, p, name, name_len);
4007
4008out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004009 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004010 return ret;
4011}
4012
4013static int process_new_xattr(struct send_ctx *sctx)
4014{
4015 int ret = 0;
4016
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004017 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4018 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004019
4020 return ret;
4021}
4022
4023static int process_deleted_xattr(struct send_ctx *sctx)
4024{
4025 int ret;
4026
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004027 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4028 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004029
4030 return ret;
4031}
4032
4033struct find_xattr_ctx {
4034 const char *name;
4035 int name_len;
4036 int found_idx;
4037 char *found_data;
4038 int found_data_len;
4039};
4040
4041static int __find_xattr(int num, struct btrfs_key *di_key,
4042 const char *name, int name_len,
4043 const char *data, int data_len,
4044 u8 type, void *vctx)
4045{
4046 struct find_xattr_ctx *ctx = vctx;
4047
4048 if (name_len == ctx->name_len &&
4049 strncmp(name, ctx->name, name_len) == 0) {
4050 ctx->found_idx = num;
4051 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00004052 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02004053 if (!ctx->found_data)
4054 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004055 return 1;
4056 }
4057 return 0;
4058}
4059
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004060static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004061 struct btrfs_path *path,
4062 struct btrfs_key *key,
4063 const char *name, int name_len,
4064 char **data, int *data_len)
4065{
4066 int ret;
4067 struct find_xattr_ctx ctx;
4068
4069 ctx.name = name;
4070 ctx.name_len = name_len;
4071 ctx.found_idx = -1;
4072 ctx.found_data = NULL;
4073 ctx.found_data_len = 0;
4074
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004075 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004076 if (ret < 0)
4077 return ret;
4078
4079 if (ctx.found_idx == -1)
4080 return -ENOENT;
4081 if (data) {
4082 *data = ctx.found_data;
4083 *data_len = ctx.found_data_len;
4084 } else {
4085 kfree(ctx.found_data);
4086 }
4087 return ctx.found_idx;
4088}
4089
4090
4091static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4092 const char *name, int name_len,
4093 const char *data, int data_len,
4094 u8 type, void *ctx)
4095{
4096 int ret;
4097 struct send_ctx *sctx = ctx;
4098 char *found_data = NULL;
4099 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004100
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004101 ret = find_xattr(sctx->parent_root, sctx->right_path,
4102 sctx->cmp_key, name, name_len, &found_data,
4103 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004104 if (ret == -ENOENT) {
4105 ret = __process_new_xattr(num, di_key, name, name_len, data,
4106 data_len, type, ctx);
4107 } else if (ret >= 0) {
4108 if (data_len != found_data_len ||
4109 memcmp(data, found_data, data_len)) {
4110 ret = __process_new_xattr(num, di_key, name, name_len,
4111 data, data_len, type, ctx);
4112 } else {
4113 ret = 0;
4114 }
4115 }
4116
4117 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004118 return ret;
4119}
4120
4121static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4122 const char *name, int name_len,
4123 const char *data, int data_len,
4124 u8 type, void *ctx)
4125{
4126 int ret;
4127 struct send_ctx *sctx = ctx;
4128
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004129 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4130 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004131 if (ret == -ENOENT)
4132 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4133 data_len, type, ctx);
4134 else if (ret >= 0)
4135 ret = 0;
4136
4137 return ret;
4138}
4139
4140static int process_changed_xattr(struct send_ctx *sctx)
4141{
4142 int ret = 0;
4143
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004144 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004145 sctx->cmp_key, __process_changed_new_xattr, sctx);
4146 if (ret < 0)
4147 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004148 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004149 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4150
4151out:
4152 return ret;
4153}
4154
4155static int process_all_new_xattrs(struct send_ctx *sctx)
4156{
4157 int ret;
4158 struct btrfs_root *root;
4159 struct btrfs_path *path;
4160 struct btrfs_key key;
4161 struct btrfs_key found_key;
4162 struct extent_buffer *eb;
4163 int slot;
4164
4165 path = alloc_path_for_send();
4166 if (!path)
4167 return -ENOMEM;
4168
4169 root = sctx->send_root;
4170
4171 key.objectid = sctx->cmp_key->objectid;
4172 key.type = BTRFS_XATTR_ITEM_KEY;
4173 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004174 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4175 if (ret < 0)
4176 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004177
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004178 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004179 eb = path->nodes[0];
4180 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004181 if (slot >= btrfs_header_nritems(eb)) {
4182 ret = btrfs_next_leaf(root, path);
4183 if (ret < 0) {
4184 goto out;
4185 } else if (ret > 0) {
4186 ret = 0;
4187 break;
4188 }
4189 continue;
4190 }
Alexander Block31db9f72012-07-25 23:19:24 +02004191
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004192 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004193 if (found_key.objectid != key.objectid ||
4194 found_key.type != key.type) {
4195 ret = 0;
4196 goto out;
4197 }
4198
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004199 ret = iterate_dir_item(root, path, &found_key,
4200 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004201 if (ret < 0)
4202 goto out;
4203
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004204 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004205 }
4206
4207out:
4208 btrfs_free_path(path);
4209 return ret;
4210}
4211
Josef Baciked259092013-10-25 11:36:01 -04004212static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4213{
4214 struct btrfs_root *root = sctx->send_root;
4215 struct btrfs_fs_info *fs_info = root->fs_info;
4216 struct inode *inode;
4217 struct page *page;
4218 char *addr;
4219 struct btrfs_key key;
4220 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4221 pgoff_t last_index;
4222 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4223 ssize_t ret = 0;
4224
4225 key.objectid = sctx->cur_ino;
4226 key.type = BTRFS_INODE_ITEM_KEY;
4227 key.offset = 0;
4228
4229 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4230 if (IS_ERR(inode))
4231 return PTR_ERR(inode);
4232
4233 if (offset + len > i_size_read(inode)) {
4234 if (offset > i_size_read(inode))
4235 len = 0;
4236 else
4237 len = offset - i_size_read(inode);
4238 }
4239 if (len == 0)
4240 goto out;
4241
4242 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
Liu Bo2131bcd32014-03-05 10:07:35 +08004243
4244 /* initial readahead */
4245 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4246 file_ra_state_init(&sctx->ra, inode->i_mapping);
4247 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4248 last_index - index + 1);
4249
Josef Baciked259092013-10-25 11:36:01 -04004250 while (index <= last_index) {
4251 unsigned cur_len = min_t(unsigned, len,
4252 PAGE_CACHE_SIZE - pg_offset);
4253 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4254 if (!page) {
4255 ret = -ENOMEM;
4256 break;
4257 }
4258
4259 if (!PageUptodate(page)) {
4260 btrfs_readpage(NULL, page);
4261 lock_page(page);
4262 if (!PageUptodate(page)) {
4263 unlock_page(page);
4264 page_cache_release(page);
4265 ret = -EIO;
4266 break;
4267 }
4268 }
4269
4270 addr = kmap(page);
4271 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4272 kunmap(page);
4273 unlock_page(page);
4274 page_cache_release(page);
4275 index++;
4276 pg_offset = 0;
4277 len -= cur_len;
4278 ret += cur_len;
4279 }
4280out:
4281 iput(inode);
4282 return ret;
4283}
4284
Alexander Block31db9f72012-07-25 23:19:24 +02004285/*
4286 * Read some bytes from the current inode/file and send a write command to
4287 * user space.
4288 */
4289static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4290{
4291 int ret = 0;
4292 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004293 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004294
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004295 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004296 if (!p)
4297 return -ENOMEM;
4298
Alexander Block31db9f72012-07-25 23:19:24 +02004299verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4300
Josef Baciked259092013-10-25 11:36:01 -04004301 num_read = fill_read_buf(sctx, offset, len);
4302 if (num_read <= 0) {
4303 if (num_read < 0)
4304 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004305 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004306 }
Alexander Block31db9f72012-07-25 23:19:24 +02004307
4308 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4309 if (ret < 0)
4310 goto out;
4311
4312 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4313 if (ret < 0)
4314 goto out;
4315
4316 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4317 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004318 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004319
4320 ret = send_cmd(sctx);
4321
4322tlv_put_failure:
4323out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004324 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004325 if (ret < 0)
4326 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004327 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004328}
4329
4330/*
4331 * Send a clone command to user space.
4332 */
4333static int send_clone(struct send_ctx *sctx,
4334 u64 offset, u32 len,
4335 struct clone_root *clone_root)
4336{
4337 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004338 struct fs_path *p;
4339 u64 gen;
4340
4341verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4342 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4343 clone_root->root->objectid, clone_root->ino,
4344 clone_root->offset);
4345
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004346 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004347 if (!p)
4348 return -ENOMEM;
4349
4350 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4351 if (ret < 0)
4352 goto out;
4353
4354 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4355 if (ret < 0)
4356 goto out;
4357
4358 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4359 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4360 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4361
Alexander Blocke938c8a2012-07-28 16:33:49 +02004362 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004363 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004364 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004365 if (ret < 0)
4366 goto out;
4367 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4368 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004369 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004370 }
4371 if (ret < 0)
4372 goto out;
4373
4374 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02004375 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004376 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004377 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004378 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4379 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4380 clone_root->offset);
4381
4382 ret = send_cmd(sctx);
4383
4384tlv_put_failure:
4385out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004386 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004387 return ret;
4388}
4389
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004390/*
4391 * Send an update extent command to user space.
4392 */
4393static int send_update_extent(struct send_ctx *sctx,
4394 u64 offset, u32 len)
4395{
4396 int ret = 0;
4397 struct fs_path *p;
4398
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004399 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004400 if (!p)
4401 return -ENOMEM;
4402
4403 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4404 if (ret < 0)
4405 goto out;
4406
4407 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4408 if (ret < 0)
4409 goto out;
4410
4411 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4412 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4413 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4414
4415 ret = send_cmd(sctx);
4416
4417tlv_put_failure:
4418out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004419 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004420 return ret;
4421}
4422
Josef Bacik16e75492013-10-22 12:18:51 -04004423static int send_hole(struct send_ctx *sctx, u64 end)
4424{
4425 struct fs_path *p = NULL;
4426 u64 offset = sctx->cur_inode_last_extent;
4427 u64 len;
4428 int ret = 0;
4429
4430 p = fs_path_alloc();
4431 if (!p)
4432 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01004433 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4434 if (ret < 0)
4435 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04004436 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4437 while (offset < end) {
4438 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4439
4440 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4441 if (ret < 0)
4442 break;
Josef Bacik16e75492013-10-22 12:18:51 -04004443 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4444 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4445 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4446 ret = send_cmd(sctx);
4447 if (ret < 0)
4448 break;
4449 offset += len;
4450 }
4451tlv_put_failure:
4452 fs_path_free(p);
4453 return ret;
4454}
4455
Alexander Block31db9f72012-07-25 23:19:24 +02004456static int send_write_or_clone(struct send_ctx *sctx,
4457 struct btrfs_path *path,
4458 struct btrfs_key *key,
4459 struct clone_root *clone_root)
4460{
4461 int ret = 0;
4462 struct btrfs_file_extent_item *ei;
4463 u64 offset = key->offset;
4464 u64 pos = 0;
4465 u64 len;
4466 u32 l;
4467 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004468 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02004469
4470 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4471 struct btrfs_file_extent_item);
4472 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004473 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004474 len = btrfs_file_extent_inline_len(path->nodes[0],
4475 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004476 /*
4477 * it is possible the inline item won't cover the whole page,
4478 * but there may be items after this page. Make
4479 * sure to send the whole thing
4480 */
4481 len = PAGE_CACHE_ALIGN(len);
4482 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004483 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004484 }
Alexander Block31db9f72012-07-25 23:19:24 +02004485
4486 if (offset + len > sctx->cur_inode_size)
4487 len = sctx->cur_inode_size - offset;
4488 if (len == 0) {
4489 ret = 0;
4490 goto out;
4491 }
4492
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004493 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004494 ret = send_clone(sctx, offset, len, clone_root);
4495 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4496 ret = send_update_extent(sctx, offset, len);
4497 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004498 while (pos < len) {
4499 l = len - pos;
4500 if (l > BTRFS_SEND_READ_SIZE)
4501 l = BTRFS_SEND_READ_SIZE;
4502 ret = send_write(sctx, pos + offset, l);
4503 if (ret < 0)
4504 goto out;
4505 if (!ret)
4506 break;
4507 pos += ret;
4508 }
4509 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004510 }
Alexander Block31db9f72012-07-25 23:19:24 +02004511out:
4512 return ret;
4513}
4514
4515static int is_extent_unchanged(struct send_ctx *sctx,
4516 struct btrfs_path *left_path,
4517 struct btrfs_key *ekey)
4518{
4519 int ret = 0;
4520 struct btrfs_key key;
4521 struct btrfs_path *path = NULL;
4522 struct extent_buffer *eb;
4523 int slot;
4524 struct btrfs_key found_key;
4525 struct btrfs_file_extent_item *ei;
4526 u64 left_disknr;
4527 u64 right_disknr;
4528 u64 left_offset;
4529 u64 right_offset;
4530 u64 left_offset_fixed;
4531 u64 left_len;
4532 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04004533 u64 left_gen;
4534 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004535 u8 left_type;
4536 u8 right_type;
4537
4538 path = alloc_path_for_send();
4539 if (!path)
4540 return -ENOMEM;
4541
4542 eb = left_path->nodes[0];
4543 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02004544 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4545 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004546
4547 if (left_type != BTRFS_FILE_EXTENT_REG) {
4548 ret = 0;
4549 goto out;
4550 }
Chris Mason74dd17f2012-08-07 16:25:13 -04004551 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4552 left_len = btrfs_file_extent_num_bytes(eb, ei);
4553 left_offset = btrfs_file_extent_offset(eb, ei);
4554 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004555
4556 /*
4557 * Following comments will refer to these graphics. L is the left
4558 * extents which we are checking at the moment. 1-8 are the right
4559 * extents that we iterate.
4560 *
4561 * |-----L-----|
4562 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4563 *
4564 * |-----L-----|
4565 * |--1--|-2b-|...(same as above)
4566 *
4567 * Alternative situation. Happens on files where extents got split.
4568 * |-----L-----|
4569 * |-----------7-----------|-6-|
4570 *
4571 * Alternative situation. Happens on files which got larger.
4572 * |-----L-----|
4573 * |-8-|
4574 * Nothing follows after 8.
4575 */
4576
4577 key.objectid = ekey->objectid;
4578 key.type = BTRFS_EXTENT_DATA_KEY;
4579 key.offset = ekey->offset;
4580 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4581 if (ret < 0)
4582 goto out;
4583 if (ret) {
4584 ret = 0;
4585 goto out;
4586 }
4587
4588 /*
4589 * Handle special case where the right side has no extents at all.
4590 */
4591 eb = path->nodes[0];
4592 slot = path->slots[0];
4593 btrfs_item_key_to_cpu(eb, &found_key, slot);
4594 if (found_key.objectid != key.objectid ||
4595 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004596 /* If we're a hole then just pretend nothing changed */
4597 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004598 goto out;
4599 }
4600
4601 /*
4602 * We're now on 2a, 2b or 7.
4603 */
4604 key = found_key;
4605 while (key.offset < ekey->offset + left_len) {
4606 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4607 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004608 if (right_type != BTRFS_FILE_EXTENT_REG) {
4609 ret = 0;
4610 goto out;
4611 }
4612
Josef Bacik007d31f2013-10-31 16:49:02 -04004613 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4614 right_len = btrfs_file_extent_num_bytes(eb, ei);
4615 right_offset = btrfs_file_extent_offset(eb, ei);
4616 right_gen = btrfs_file_extent_generation(eb, ei);
4617
Alexander Block31db9f72012-07-25 23:19:24 +02004618 /*
4619 * Are we at extent 8? If yes, we know the extent is changed.
4620 * This may only happen on the first iteration.
4621 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02004622 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004623 /* If we're a hole just pretend nothing changed */
4624 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004625 goto out;
4626 }
4627
4628 left_offset_fixed = left_offset;
4629 if (key.offset < ekey->offset) {
4630 /* Fix the right offset for 2a and 7. */
4631 right_offset += ekey->offset - key.offset;
4632 } else {
4633 /* Fix the left offset for all behind 2a and 2b */
4634 left_offset_fixed += key.offset - ekey->offset;
4635 }
4636
4637 /*
4638 * Check if we have the same extent.
4639 */
Alexander Block39540962012-08-01 12:46:05 +02004640 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04004641 left_offset_fixed != right_offset ||
4642 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02004643 ret = 0;
4644 goto out;
4645 }
4646
4647 /*
4648 * Go to the next extent.
4649 */
4650 ret = btrfs_next_item(sctx->parent_root, path);
4651 if (ret < 0)
4652 goto out;
4653 if (!ret) {
4654 eb = path->nodes[0];
4655 slot = path->slots[0];
4656 btrfs_item_key_to_cpu(eb, &found_key, slot);
4657 }
4658 if (ret || found_key.objectid != key.objectid ||
4659 found_key.type != key.type) {
4660 key.offset += right_len;
4661 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00004662 }
4663 if (found_key.offset != key.offset + right_len) {
4664 ret = 0;
4665 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004666 }
4667 key = found_key;
4668 }
4669
4670 /*
4671 * We're now behind the left extent (treat as unchanged) or at the end
4672 * of the right side (treat as changed).
4673 */
4674 if (key.offset >= ekey->offset + left_len)
4675 ret = 1;
4676 else
4677 ret = 0;
4678
4679
4680out:
4681 btrfs_free_path(path);
4682 return ret;
4683}
4684
Josef Bacik16e75492013-10-22 12:18:51 -04004685static int get_last_extent(struct send_ctx *sctx, u64 offset)
4686{
4687 struct btrfs_path *path;
4688 struct btrfs_root *root = sctx->send_root;
4689 struct btrfs_file_extent_item *fi;
4690 struct btrfs_key key;
4691 u64 extent_end;
4692 u8 type;
4693 int ret;
4694
4695 path = alloc_path_for_send();
4696 if (!path)
4697 return -ENOMEM;
4698
4699 sctx->cur_inode_last_extent = 0;
4700
4701 key.objectid = sctx->cur_ino;
4702 key.type = BTRFS_EXTENT_DATA_KEY;
4703 key.offset = offset;
4704 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4705 if (ret < 0)
4706 goto out;
4707 ret = 0;
4708 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4709 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4710 goto out;
4711
4712 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4713 struct btrfs_file_extent_item);
4714 type = btrfs_file_extent_type(path->nodes[0], fi);
4715 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004716 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4717 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004718 extent_end = ALIGN(key.offset + size,
4719 sctx->send_root->sectorsize);
4720 } else {
4721 extent_end = key.offset +
4722 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4723 }
4724 sctx->cur_inode_last_extent = extent_end;
4725out:
4726 btrfs_free_path(path);
4727 return ret;
4728}
4729
4730static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4731 struct btrfs_key *key)
4732{
4733 struct btrfs_file_extent_item *fi;
4734 u64 extent_end;
4735 u8 type;
4736 int ret = 0;
4737
4738 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4739 return 0;
4740
4741 if (sctx->cur_inode_last_extent == (u64)-1) {
4742 ret = get_last_extent(sctx, key->offset - 1);
4743 if (ret)
4744 return ret;
4745 }
4746
4747 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4748 struct btrfs_file_extent_item);
4749 type = btrfs_file_extent_type(path->nodes[0], fi);
4750 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004751 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4752 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004753 extent_end = ALIGN(key->offset + size,
4754 sctx->send_root->sectorsize);
4755 } else {
4756 extent_end = key->offset +
4757 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4758 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00004759
4760 if (path->slots[0] == 0 &&
4761 sctx->cur_inode_last_extent < key->offset) {
4762 /*
4763 * We might have skipped entire leafs that contained only
4764 * file extent items for our current inode. These leafs have
4765 * a generation number smaller (older) than the one in the
4766 * current leaf and the leaf our last extent came from, and
4767 * are located between these 2 leafs.
4768 */
4769 ret = get_last_extent(sctx, key->offset - 1);
4770 if (ret)
4771 return ret;
4772 }
4773
Josef Bacik16e75492013-10-22 12:18:51 -04004774 if (sctx->cur_inode_last_extent < key->offset)
4775 ret = send_hole(sctx, key->offset);
4776 sctx->cur_inode_last_extent = extent_end;
4777 return ret;
4778}
4779
Alexander Block31db9f72012-07-25 23:19:24 +02004780static int process_extent(struct send_ctx *sctx,
4781 struct btrfs_path *path,
4782 struct btrfs_key *key)
4783{
Alexander Block31db9f72012-07-25 23:19:24 +02004784 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004785 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004786
4787 if (S_ISLNK(sctx->cur_inode_mode))
4788 return 0;
4789
4790 if (sctx->parent_root && !sctx->cur_inode_new) {
4791 ret = is_extent_unchanged(sctx, path, key);
4792 if (ret < 0)
4793 goto out;
4794 if (ret) {
4795 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004796 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004797 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004798 } else {
4799 struct btrfs_file_extent_item *ei;
4800 u8 type;
4801
4802 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4803 struct btrfs_file_extent_item);
4804 type = btrfs_file_extent_type(path->nodes[0], ei);
4805 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4806 type == BTRFS_FILE_EXTENT_REG) {
4807 /*
4808 * The send spec does not have a prealloc command yet,
4809 * so just leave a hole for prealloc'ed extents until
4810 * we have enough commands queued up to justify rev'ing
4811 * the send spec.
4812 */
4813 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4814 ret = 0;
4815 goto out;
4816 }
4817
4818 /* Have a hole, just skip it. */
4819 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4820 ret = 0;
4821 goto out;
4822 }
4823 }
Alexander Block31db9f72012-07-25 23:19:24 +02004824 }
4825
4826 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4827 sctx->cur_inode_size, &found_clone);
4828 if (ret != -ENOENT && ret < 0)
4829 goto out;
4830
4831 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004832 if (ret)
4833 goto out;
4834out_hole:
4835 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004836out:
4837 return ret;
4838}
4839
4840static int process_all_extents(struct send_ctx *sctx)
4841{
4842 int ret;
4843 struct btrfs_root *root;
4844 struct btrfs_path *path;
4845 struct btrfs_key key;
4846 struct btrfs_key found_key;
4847 struct extent_buffer *eb;
4848 int slot;
4849
4850 root = sctx->send_root;
4851 path = alloc_path_for_send();
4852 if (!path)
4853 return -ENOMEM;
4854
4855 key.objectid = sctx->cmp_key->objectid;
4856 key.type = BTRFS_EXTENT_DATA_KEY;
4857 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004858 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4859 if (ret < 0)
4860 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004861
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004862 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004863 eb = path->nodes[0];
4864 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004865
4866 if (slot >= btrfs_header_nritems(eb)) {
4867 ret = btrfs_next_leaf(root, path);
4868 if (ret < 0) {
4869 goto out;
4870 } else if (ret > 0) {
4871 ret = 0;
4872 break;
4873 }
4874 continue;
4875 }
4876
Alexander Block31db9f72012-07-25 23:19:24 +02004877 btrfs_item_key_to_cpu(eb, &found_key, slot);
4878
4879 if (found_key.objectid != key.objectid ||
4880 found_key.type != key.type) {
4881 ret = 0;
4882 goto out;
4883 }
4884
4885 ret = process_extent(sctx, path, &found_key);
4886 if (ret < 0)
4887 goto out;
4888
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004889 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004890 }
4891
4892out:
4893 btrfs_free_path(path);
4894 return ret;
4895}
4896
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004897static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4898 int *pending_move,
4899 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02004900{
4901 int ret = 0;
4902
4903 if (sctx->cur_ino == 0)
4904 goto out;
4905 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004906 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004907 goto out;
4908 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4909 goto out;
4910
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004911 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004912 if (ret < 0)
4913 goto out;
4914
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004915 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004916out:
4917 return ret;
4918}
4919
4920static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4921{
4922 int ret = 0;
4923 u64 left_mode;
4924 u64 left_uid;
4925 u64 left_gid;
4926 u64 right_mode;
4927 u64 right_uid;
4928 u64 right_gid;
4929 int need_chmod = 0;
4930 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004931 int pending_move = 0;
4932 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004933
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004934 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4935 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02004936 if (ret < 0)
4937 goto out;
4938
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004939 /*
4940 * We have processed the refs and thus need to advance send_progress.
4941 * Now, calls to get_cur_xxx will take the updated refs of the current
4942 * inode into account.
4943 *
4944 * On the other hand, if our current inode is a directory and couldn't
4945 * be moved/renamed because its parent was renamed/moved too and it has
4946 * a higher inode number, we can only move/rename our current inode
4947 * after we moved/renamed its parent. Therefore in this case operate on
4948 * the old path (pre move/rename) of our current inode, and the
4949 * move/rename will be performed later.
4950 */
4951 if (refs_processed && !pending_move)
4952 sctx->send_progress = sctx->cur_ino + 1;
4953
Alexander Block31db9f72012-07-25 23:19:24 +02004954 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4955 goto out;
4956 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4957 goto out;
4958
4959 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004960 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004961 if (ret < 0)
4962 goto out;
4963
Alex Lyakase2d044f2012-10-17 13:52:47 +00004964 if (!sctx->parent_root || sctx->cur_inode_new) {
4965 need_chown = 1;
4966 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004967 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004968 } else {
4969 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4970 NULL, NULL, &right_mode, &right_uid,
4971 &right_gid, NULL);
4972 if (ret < 0)
4973 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004974
Alex Lyakase2d044f2012-10-17 13:52:47 +00004975 if (left_uid != right_uid || left_gid != right_gid)
4976 need_chown = 1;
4977 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4978 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004979 }
4980
4981 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04004982 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01004983 if (sctx->cur_inode_last_extent == (u64)-1 ||
4984 sctx->cur_inode_last_extent <
4985 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04004986 ret = get_last_extent(sctx, (u64)-1);
4987 if (ret)
4988 goto out;
4989 }
4990 if (sctx->cur_inode_last_extent <
4991 sctx->cur_inode_size) {
4992 ret = send_hole(sctx, sctx->cur_inode_size);
4993 if (ret)
4994 goto out;
4995 }
4996 }
Alexander Block31db9f72012-07-25 23:19:24 +02004997 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4998 sctx->cur_inode_size);
4999 if (ret < 0)
5000 goto out;
5001 }
5002
5003 if (need_chown) {
5004 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5005 left_uid, left_gid);
5006 if (ret < 0)
5007 goto out;
5008 }
5009 if (need_chmod) {
5010 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5011 left_mode);
5012 if (ret < 0)
5013 goto out;
5014 }
5015
5016 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005017 * If other directory inodes depended on our current directory
5018 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005019 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005020 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5021 ret = apply_children_dir_moves(sctx);
5022 if (ret)
5023 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005024 /*
5025 * Need to send that every time, no matter if it actually
5026 * changed between the two trees as we have done changes to
5027 * the inode before. If our inode is a directory and it's
5028 * waiting to be moved/renamed, we will send its utimes when
5029 * it's moved/renamed, therefore we don't need to do it here.
5030 */
5031 sctx->send_progress = sctx->cur_ino + 1;
5032 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5033 if (ret < 0)
5034 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005035 }
5036
Alexander Block31db9f72012-07-25 23:19:24 +02005037out:
5038 return ret;
5039}
5040
5041static int changed_inode(struct send_ctx *sctx,
5042 enum btrfs_compare_tree_result result)
5043{
5044 int ret = 0;
5045 struct btrfs_key *key = sctx->cmp_key;
5046 struct btrfs_inode_item *left_ii = NULL;
5047 struct btrfs_inode_item *right_ii = NULL;
5048 u64 left_gen = 0;
5049 u64 right_gen = 0;
5050
Alexander Block31db9f72012-07-25 23:19:24 +02005051 sctx->cur_ino = key->objectid;
5052 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005053 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005054
5055 /*
5056 * Set send_progress to current inode. This will tell all get_cur_xxx
5057 * functions that the current inode's refs are not updated yet. Later,
5058 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5059 */
Alexander Block31db9f72012-07-25 23:19:24 +02005060 sctx->send_progress = sctx->cur_ino;
5061
5062 if (result == BTRFS_COMPARE_TREE_NEW ||
5063 result == BTRFS_COMPARE_TREE_CHANGED) {
5064 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5065 sctx->left_path->slots[0],
5066 struct btrfs_inode_item);
5067 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5068 left_ii);
5069 } else {
5070 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5071 sctx->right_path->slots[0],
5072 struct btrfs_inode_item);
5073 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5074 right_ii);
5075 }
5076 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5077 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5078 sctx->right_path->slots[0],
5079 struct btrfs_inode_item);
5080
5081 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5082 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005083
5084 /*
5085 * The cur_ino = root dir case is special here. We can't treat
5086 * the inode as deleted+reused because it would generate a
5087 * stream that tries to delete/mkdir the root dir.
5088 */
5089 if (left_gen != right_gen &&
5090 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005091 sctx->cur_inode_new_gen = 1;
5092 }
5093
5094 if (result == BTRFS_COMPARE_TREE_NEW) {
5095 sctx->cur_inode_gen = left_gen;
5096 sctx->cur_inode_new = 1;
5097 sctx->cur_inode_deleted = 0;
5098 sctx->cur_inode_size = btrfs_inode_size(
5099 sctx->left_path->nodes[0], left_ii);
5100 sctx->cur_inode_mode = btrfs_inode_mode(
5101 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005102 sctx->cur_inode_rdev = btrfs_inode_rdev(
5103 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005104 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005105 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005106 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5107 sctx->cur_inode_gen = right_gen;
5108 sctx->cur_inode_new = 0;
5109 sctx->cur_inode_deleted = 1;
5110 sctx->cur_inode_size = btrfs_inode_size(
5111 sctx->right_path->nodes[0], right_ii);
5112 sctx->cur_inode_mode = btrfs_inode_mode(
5113 sctx->right_path->nodes[0], right_ii);
5114 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005115 /*
5116 * We need to do some special handling in case the inode was
5117 * reported as changed with a changed generation number. This
5118 * means that the original inode was deleted and new inode
5119 * reused the same inum. So we have to treat the old inode as
5120 * deleted and the new one as new.
5121 */
Alexander Block31db9f72012-07-25 23:19:24 +02005122 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005123 /*
5124 * First, process the inode as if it was deleted.
5125 */
Alexander Block31db9f72012-07-25 23:19:24 +02005126 sctx->cur_inode_gen = right_gen;
5127 sctx->cur_inode_new = 0;
5128 sctx->cur_inode_deleted = 1;
5129 sctx->cur_inode_size = btrfs_inode_size(
5130 sctx->right_path->nodes[0], right_ii);
5131 sctx->cur_inode_mode = btrfs_inode_mode(
5132 sctx->right_path->nodes[0], right_ii);
5133 ret = process_all_refs(sctx,
5134 BTRFS_COMPARE_TREE_DELETED);
5135 if (ret < 0)
5136 goto out;
5137
Alexander Block766702e2012-07-28 14:11:31 +02005138 /*
5139 * Now process the inode as if it was new.
5140 */
Alexander Block31db9f72012-07-25 23:19:24 +02005141 sctx->cur_inode_gen = left_gen;
5142 sctx->cur_inode_new = 1;
5143 sctx->cur_inode_deleted = 0;
5144 sctx->cur_inode_size = btrfs_inode_size(
5145 sctx->left_path->nodes[0], left_ii);
5146 sctx->cur_inode_mode = btrfs_inode_mode(
5147 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005148 sctx->cur_inode_rdev = btrfs_inode_rdev(
5149 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005150 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005151 if (ret < 0)
5152 goto out;
5153
5154 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5155 if (ret < 0)
5156 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005157 /*
5158 * Advance send_progress now as we did not get into
5159 * process_recorded_refs_if_needed in the new_gen case.
5160 */
5161 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005162
5163 /*
5164 * Now process all extents and xattrs of the inode as if
5165 * they were all new.
5166 */
Alexander Block31db9f72012-07-25 23:19:24 +02005167 ret = process_all_extents(sctx);
5168 if (ret < 0)
5169 goto out;
5170 ret = process_all_new_xattrs(sctx);
5171 if (ret < 0)
5172 goto out;
5173 } else {
5174 sctx->cur_inode_gen = left_gen;
5175 sctx->cur_inode_new = 0;
5176 sctx->cur_inode_new_gen = 0;
5177 sctx->cur_inode_deleted = 0;
5178 sctx->cur_inode_size = btrfs_inode_size(
5179 sctx->left_path->nodes[0], left_ii);
5180 sctx->cur_inode_mode = btrfs_inode_mode(
5181 sctx->left_path->nodes[0], left_ii);
5182 }
5183 }
5184
5185out:
5186 return ret;
5187}
5188
Alexander Block766702e2012-07-28 14:11:31 +02005189/*
5190 * We have to process new refs before deleted refs, but compare_trees gives us
5191 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5192 * first and later process them in process_recorded_refs.
5193 * For the cur_inode_new_gen case, we skip recording completely because
5194 * changed_inode did already initiate processing of refs. The reason for this is
5195 * that in this case, compare_tree actually compares the refs of 2 different
5196 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5197 * refs of the right tree as deleted and all refs of the left tree as new.
5198 */
Alexander Block31db9f72012-07-25 23:19:24 +02005199static int changed_ref(struct send_ctx *sctx,
5200 enum btrfs_compare_tree_result result)
5201{
5202 int ret = 0;
5203
5204 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5205
5206 if (!sctx->cur_inode_new_gen &&
5207 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5208 if (result == BTRFS_COMPARE_TREE_NEW)
5209 ret = record_new_ref(sctx);
5210 else if (result == BTRFS_COMPARE_TREE_DELETED)
5211 ret = record_deleted_ref(sctx);
5212 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5213 ret = record_changed_ref(sctx);
5214 }
5215
5216 return ret;
5217}
5218
Alexander Block766702e2012-07-28 14:11:31 +02005219/*
5220 * Process new/deleted/changed xattrs. We skip processing in the
5221 * cur_inode_new_gen case because changed_inode did already initiate processing
5222 * of xattrs. The reason is the same as in changed_ref
5223 */
Alexander Block31db9f72012-07-25 23:19:24 +02005224static int changed_xattr(struct send_ctx *sctx,
5225 enum btrfs_compare_tree_result result)
5226{
5227 int ret = 0;
5228
5229 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5230
5231 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5232 if (result == BTRFS_COMPARE_TREE_NEW)
5233 ret = process_new_xattr(sctx);
5234 else if (result == BTRFS_COMPARE_TREE_DELETED)
5235 ret = process_deleted_xattr(sctx);
5236 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5237 ret = process_changed_xattr(sctx);
5238 }
5239
5240 return ret;
5241}
5242
Alexander Block766702e2012-07-28 14:11:31 +02005243/*
5244 * Process new/deleted/changed extents. We skip processing in the
5245 * cur_inode_new_gen case because changed_inode did already initiate processing
5246 * of extents. The reason is the same as in changed_ref
5247 */
Alexander Block31db9f72012-07-25 23:19:24 +02005248static int changed_extent(struct send_ctx *sctx,
5249 enum btrfs_compare_tree_result result)
5250{
5251 int ret = 0;
5252
5253 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5254
5255 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5256 if (result != BTRFS_COMPARE_TREE_DELETED)
5257 ret = process_extent(sctx, sctx->left_path,
5258 sctx->cmp_key);
5259 }
5260
5261 return ret;
5262}
5263
Josef Bacikba5e8f22013-08-16 16:52:55 -04005264static int dir_changed(struct send_ctx *sctx, u64 dir)
5265{
5266 u64 orig_gen, new_gen;
5267 int ret;
5268
5269 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5270 NULL, NULL);
5271 if (ret)
5272 return ret;
5273
5274 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5275 NULL, NULL, NULL);
5276 if (ret)
5277 return ret;
5278
5279 return (orig_gen != new_gen) ? 1 : 0;
5280}
5281
5282static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5283 struct btrfs_key *key)
5284{
5285 struct btrfs_inode_extref *extref;
5286 struct extent_buffer *leaf;
5287 u64 dirid = 0, last_dirid = 0;
5288 unsigned long ptr;
5289 u32 item_size;
5290 u32 cur_offset = 0;
5291 int ref_name_len;
5292 int ret = 0;
5293
5294 /* Easy case, just check this one dirid */
5295 if (key->type == BTRFS_INODE_REF_KEY) {
5296 dirid = key->offset;
5297
5298 ret = dir_changed(sctx, dirid);
5299 goto out;
5300 }
5301
5302 leaf = path->nodes[0];
5303 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5304 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5305 while (cur_offset < item_size) {
5306 extref = (struct btrfs_inode_extref *)(ptr +
5307 cur_offset);
5308 dirid = btrfs_inode_extref_parent(leaf, extref);
5309 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5310 cur_offset += ref_name_len + sizeof(*extref);
5311 if (dirid == last_dirid)
5312 continue;
5313 ret = dir_changed(sctx, dirid);
5314 if (ret)
5315 break;
5316 last_dirid = dirid;
5317 }
5318out:
5319 return ret;
5320}
5321
Alexander Block766702e2012-07-28 14:11:31 +02005322/*
5323 * Updates compare related fields in sctx and simply forwards to the actual
5324 * changed_xxx functions.
5325 */
Alexander Block31db9f72012-07-25 23:19:24 +02005326static int changed_cb(struct btrfs_root *left_root,
5327 struct btrfs_root *right_root,
5328 struct btrfs_path *left_path,
5329 struct btrfs_path *right_path,
5330 struct btrfs_key *key,
5331 enum btrfs_compare_tree_result result,
5332 void *ctx)
5333{
5334 int ret = 0;
5335 struct send_ctx *sctx = ctx;
5336
Josef Bacikba5e8f22013-08-16 16:52:55 -04005337 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005338 if (key->type == BTRFS_INODE_REF_KEY ||
5339 key->type == BTRFS_INODE_EXTREF_KEY) {
5340 ret = compare_refs(sctx, left_path, key);
5341 if (!ret)
5342 return 0;
5343 if (ret < 0)
5344 return ret;
5345 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5346 return maybe_send_hole(sctx, left_path, key);
5347 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005348 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005349 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005350 result = BTRFS_COMPARE_TREE_CHANGED;
5351 ret = 0;
5352 }
5353
Alexander Block31db9f72012-07-25 23:19:24 +02005354 sctx->left_path = left_path;
5355 sctx->right_path = right_path;
5356 sctx->cmp_key = key;
5357
5358 ret = finish_inode_if_needed(sctx, 0);
5359 if (ret < 0)
5360 goto out;
5361
Alexander Block2981e222012-08-01 14:47:03 +02005362 /* Ignore non-FS objects */
5363 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5364 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5365 goto out;
5366
Alexander Block31db9f72012-07-25 23:19:24 +02005367 if (key->type == BTRFS_INODE_ITEM_KEY)
5368 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005369 else if (key->type == BTRFS_INODE_REF_KEY ||
5370 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005371 ret = changed_ref(sctx, result);
5372 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5373 ret = changed_xattr(sctx, result);
5374 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5375 ret = changed_extent(sctx, result);
5376
5377out:
5378 return ret;
5379}
5380
5381static int full_send_tree(struct send_ctx *sctx)
5382{
5383 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005384 struct btrfs_root *send_root = sctx->send_root;
5385 struct btrfs_key key;
5386 struct btrfs_key found_key;
5387 struct btrfs_path *path;
5388 struct extent_buffer *eb;
5389 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02005390
5391 path = alloc_path_for_send();
5392 if (!path)
5393 return -ENOMEM;
5394
Alexander Block31db9f72012-07-25 23:19:24 +02005395 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5396 key.type = BTRFS_INODE_ITEM_KEY;
5397 key.offset = 0;
5398
Alexander Block31db9f72012-07-25 23:19:24 +02005399 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5400 if (ret < 0)
5401 goto out;
5402 if (ret)
5403 goto out_finish;
5404
5405 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005406 eb = path->nodes[0];
5407 slot = path->slots[0];
5408 btrfs_item_key_to_cpu(eb, &found_key, slot);
5409
5410 ret = changed_cb(send_root, NULL, path, NULL,
5411 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5412 if (ret < 0)
5413 goto out;
5414
5415 key.objectid = found_key.objectid;
5416 key.type = found_key.type;
5417 key.offset = found_key.offset + 1;
5418
5419 ret = btrfs_next_item(send_root, path);
5420 if (ret < 0)
5421 goto out;
5422 if (ret) {
5423 ret = 0;
5424 break;
5425 }
5426 }
5427
5428out_finish:
5429 ret = finish_inode_if_needed(sctx, 1);
5430
5431out:
5432 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02005433 return ret;
5434}
5435
5436static int send_subvol(struct send_ctx *sctx)
5437{
5438 int ret;
5439
Stefan Behrensc2c71322013-04-10 17:10:52 +00005440 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5441 ret = send_header(sctx);
5442 if (ret < 0)
5443 goto out;
5444 }
Alexander Block31db9f72012-07-25 23:19:24 +02005445
5446 ret = send_subvol_begin(sctx);
5447 if (ret < 0)
5448 goto out;
5449
5450 if (sctx->parent_root) {
5451 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5452 changed_cb, sctx);
5453 if (ret < 0)
5454 goto out;
5455 ret = finish_inode_if_needed(sctx, 1);
5456 if (ret < 0)
5457 goto out;
5458 } else {
5459 ret = full_send_tree(sctx);
5460 if (ret < 0)
5461 goto out;
5462 }
5463
5464out:
Alexander Block31db9f72012-07-25 23:19:24 +02005465 free_recorded_refs(sctx);
5466 return ret;
5467}
5468
David Sterba66ef7d62013-12-17 15:07:20 +01005469static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5470{
5471 spin_lock(&root->root_item_lock);
5472 root->send_in_progress--;
5473 /*
5474 * Not much left to do, we don't know why it's unbalanced and
5475 * can't blindly reset it to 0.
5476 */
5477 if (root->send_in_progress < 0)
5478 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02005479 "send_in_progres unbalanced %d root %llu",
David Sterba66ef7d62013-12-17 15:07:20 +01005480 root->send_in_progress, root->root_key.objectid);
5481 spin_unlock(&root->root_item_lock);
5482}
5483
Alexander Block31db9f72012-07-25 23:19:24 +02005484long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5485{
5486 int ret = 0;
5487 struct btrfs_root *send_root;
5488 struct btrfs_root *clone_root;
5489 struct btrfs_fs_info *fs_info;
5490 struct btrfs_ioctl_send_args *arg = NULL;
5491 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02005492 struct send_ctx *sctx = NULL;
5493 u32 i;
5494 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01005495 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08005496 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08005497 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02005498
5499 if (!capable(CAP_SYS_ADMIN))
5500 return -EPERM;
5501
Al Viro496ad9a2013-01-23 17:07:38 -05005502 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02005503 fs_info = send_root->fs_info;
5504
Josef Bacik139f8072013-05-20 11:26:50 -04005505 /*
David Sterba2c686532013-12-16 17:34:17 +01005506 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02005507 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01005508 */
5509 spin_lock(&send_root->root_item_lock);
5510 send_root->send_in_progress++;
5511 spin_unlock(&send_root->root_item_lock);
5512
5513 /*
Josef Bacik139f8072013-05-20 11:26:50 -04005514 * This is done when we lookup the root, it should already be complete
5515 * by the time we get here.
5516 */
5517 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5518
5519 /*
David Sterba2c686532013-12-16 17:34:17 +01005520 * Userspace tools do the checks and warn the user if it's
5521 * not RO.
5522 */
5523 if (!btrfs_root_readonly(send_root)) {
5524 ret = -EPERM;
5525 goto out;
5526 }
5527
Alexander Block31db9f72012-07-25 23:19:24 +02005528 arg = memdup_user(arg_, sizeof(*arg));
5529 if (IS_ERR(arg)) {
5530 ret = PTR_ERR(arg);
5531 arg = NULL;
5532 goto out;
5533 }
5534
5535 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05005536 sizeof(*arg->clone_sources) *
5537 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02005538 ret = -EFAULT;
5539 goto out;
5540 }
5541
Stefan Behrensc2c71322013-04-10 17:10:52 +00005542 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005543 ret = -EINVAL;
5544 goto out;
5545 }
5546
Alexander Block31db9f72012-07-25 23:19:24 +02005547 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5548 if (!sctx) {
5549 ret = -ENOMEM;
5550 goto out;
5551 }
5552
5553 INIT_LIST_HEAD(&sctx->new_refs);
5554 INIT_LIST_HEAD(&sctx->deleted_refs);
5555 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5556 INIT_LIST_HEAD(&sctx->name_cache_list);
5557
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005558 sctx->flags = arg->flags;
5559
Alexander Block31db9f72012-07-25 23:19:24 +02005560 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00005561 if (!sctx->send_filp) {
5562 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02005563 goto out;
5564 }
5565
Alexander Block31db9f72012-07-25 23:19:24 +02005566 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02005567 /*
5568 * Unlikely but possible, if the subvolume is marked for deletion but
5569 * is slow to remove the directory entry, send can still be started
5570 */
5571 if (btrfs_root_dead(sctx->send_root)) {
5572 ret = -EPERM;
5573 goto out;
5574 }
5575
Alexander Block31db9f72012-07-25 23:19:24 +02005576 sctx->clone_roots_cnt = arg->clone_sources_count;
5577
5578 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5579 sctx->send_buf = vmalloc(sctx->send_max_size);
5580 if (!sctx->send_buf) {
5581 ret = -ENOMEM;
5582 goto out;
5583 }
5584
5585 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5586 if (!sctx->read_buf) {
5587 ret = -ENOMEM;
5588 goto out;
5589 }
5590
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005591 sctx->pending_dir_moves = RB_ROOT;
5592 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00005593 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005594
Alexander Block31db9f72012-07-25 23:19:24 +02005595 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5596 (arg->clone_sources_count + 1));
5597 if (!sctx->clone_roots) {
5598 ret = -ENOMEM;
5599 goto out;
5600 }
5601
5602 if (arg->clone_sources_count) {
5603 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5604 sizeof(*arg->clone_sources));
5605 if (!clone_sources_tmp) {
5606 ret = -ENOMEM;
5607 goto out;
5608 }
5609
5610 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5611 arg->clone_sources_count *
5612 sizeof(*arg->clone_sources));
5613 if (ret) {
5614 ret = -EFAULT;
5615 goto out;
5616 }
5617
5618 for (i = 0; i < arg->clone_sources_count; i++) {
5619 key.objectid = clone_sources_tmp[i];
5620 key.type = BTRFS_ROOT_ITEM_KEY;
5621 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005622
5623 index = srcu_read_lock(&fs_info->subvol_srcu);
5624
Alexander Block31db9f72012-07-25 23:19:24 +02005625 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02005626 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005627 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005628 ret = PTR_ERR(clone_root);
5629 goto out;
5630 }
David Sterba2c686532013-12-16 17:34:17 +01005631 clone_sources_to_rollback = i + 1;
5632 spin_lock(&clone_root->root_item_lock);
5633 clone_root->send_in_progress++;
5634 if (!btrfs_root_readonly(clone_root)) {
5635 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005636 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005637 ret = -EPERM;
5638 goto out;
5639 }
5640 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005641 srcu_read_unlock(&fs_info->subvol_srcu, index);
5642
Alexander Block31db9f72012-07-25 23:19:24 +02005643 sctx->clone_roots[i].root = clone_root;
5644 }
5645 vfree(clone_sources_tmp);
5646 clone_sources_tmp = NULL;
5647 }
5648
5649 if (arg->parent_root) {
5650 key.objectid = arg->parent_root;
5651 key.type = BTRFS_ROOT_ITEM_KEY;
5652 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005653
5654 index = srcu_read_lock(&fs_info->subvol_srcu);
5655
Alexander Block31db9f72012-07-25 23:19:24 +02005656 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005657 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005658 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005659 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02005660 goto out;
5661 }
Wang Shilong18f687d2014-01-07 17:25:19 +08005662
David Sterba2c686532013-12-16 17:34:17 +01005663 spin_lock(&sctx->parent_root->root_item_lock);
5664 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02005665 if (!btrfs_root_readonly(sctx->parent_root) ||
5666 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01005667 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005668 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005669 ret = -EPERM;
5670 goto out;
5671 }
5672 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005673
5674 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005675 }
5676
5677 /*
5678 * Clones from send_root are allowed, but only if the clone source
5679 * is behind the current send position. This is checked while searching
5680 * for possible clone sources.
5681 */
5682 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5683
5684 /* We do a bsearch later */
5685 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5686 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5687 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08005688 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005689
Josef Bacika26e8c92014-03-28 17:07:27 -04005690 current->journal_info = (void *)BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02005691 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04005692 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02005693 if (ret < 0)
5694 goto out;
5695
Stefan Behrensc2c71322013-04-10 17:10:52 +00005696 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5697 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5698 if (ret < 0)
5699 goto out;
5700 ret = send_cmd(sctx);
5701 if (ret < 0)
5702 goto out;
5703 }
Alexander Block31db9f72012-07-25 23:19:24 +02005704
5705out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005706 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5707 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5708 struct rb_node *n;
5709 struct pending_dir_move *pm;
5710
5711 n = rb_first(&sctx->pending_dir_moves);
5712 pm = rb_entry(n, struct pending_dir_move, node);
5713 while (!list_empty(&pm->list)) {
5714 struct pending_dir_move *pm2;
5715
5716 pm2 = list_first_entry(&pm->list,
5717 struct pending_dir_move, list);
5718 free_pending_move(sctx, pm2);
5719 }
5720 free_pending_move(sctx, pm);
5721 }
5722
5723 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5724 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5725 struct rb_node *n;
5726 struct waiting_dir_move *dm;
5727
5728 n = rb_first(&sctx->waiting_dir_moves);
5729 dm = rb_entry(n, struct waiting_dir_move, node);
5730 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5731 kfree(dm);
5732 }
5733
Filipe Manana9dc44212014-02-19 14:31:44 +00005734 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5735 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5736 struct rb_node *n;
5737 struct orphan_dir_info *odi;
5738
5739 n = rb_first(&sctx->orphan_dirs);
5740 odi = rb_entry(n, struct orphan_dir_info, node);
5741 free_orphan_dir_info(sctx, odi);
5742 }
5743
Wang Shilong896c14f2014-01-07 17:25:18 +08005744 if (sort_clone_roots) {
5745 for (i = 0; i < sctx->clone_roots_cnt; i++)
5746 btrfs_root_dec_send_in_progress(
5747 sctx->clone_roots[i].root);
5748 } else {
5749 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5750 btrfs_root_dec_send_in_progress(
5751 sctx->clone_roots[i].root);
5752
5753 btrfs_root_dec_send_in_progress(send_root);
5754 }
David Sterba66ef7d62013-12-17 15:07:20 +01005755 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5756 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01005757
Alexander Block31db9f72012-07-25 23:19:24 +02005758 kfree(arg);
5759 vfree(clone_sources_tmp);
5760
5761 if (sctx) {
5762 if (sctx->send_filp)
5763 fput(sctx->send_filp);
5764
5765 vfree(sctx->clone_roots);
5766 vfree(sctx->send_buf);
5767 vfree(sctx->read_buf);
5768
5769 name_cache_free(sctx);
5770
5771 kfree(sctx);
5772 }
5773
5774 return ret;
5775}