blob: 112eb647b5cd98391b66ab6207078463f773a8fa [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
Alexander Block31db9f72012-07-25 23:19:24 +0200127 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000128
129 /*
130 * We process inodes by their increasing order, so if before an
131 * incremental send we reverse the parent/child relationship of
132 * directories such that a directory with a lower inode number was
133 * the parent of a directory with a higher inode number, and the one
134 * becoming the new parent got renamed too, we can't rename/move the
135 * directory with lower inode number when we finish processing it - we
136 * must process the directory with higher inode number first, then
137 * rename/move it and then rename/move the directory with lower inode
138 * number. Example follows.
139 *
140 * Tree state when the first send was performed:
141 *
142 * .
143 * |-- a (ino 257)
144 * |-- b (ino 258)
145 * |
146 * |
147 * |-- c (ino 259)
148 * | |-- d (ino 260)
149 * |
150 * |-- c2 (ino 261)
151 *
152 * Tree state when the second (incremental) send is performed:
153 *
154 * .
155 * |-- a (ino 257)
156 * |-- b (ino 258)
157 * |-- c2 (ino 261)
158 * |-- d2 (ino 260)
159 * |-- cc (ino 259)
160 *
161 * The sequence of steps that lead to the second state was:
162 *
163 * mv /a/b/c/d /a/b/c2/d2
164 * mv /a/b/c /a/b/c2/d2/cc
165 *
166 * "c" has lower inode number, but we can't move it (2nd mv operation)
167 * before we move "d", which has higher inode number.
168 *
169 * So we just memorize which move/rename operations must be performed
170 * later when their respective parent is processed and moved/renamed.
171 */
172
173 /* Indexed by parent directory inode number. */
174 struct rb_root pending_dir_moves;
175
176 /*
177 * Reverse index, indexed by the inode number of a directory that
178 * is waiting for the move/rename of its immediate parent before its
179 * own move/rename can be performed.
180 */
181 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000182
183 /*
184 * A directory that is going to be rm'ed might have a child directory
185 * which is in the pending directory moves index above. In this case,
186 * the directory can only be removed after the move/rename of its child
187 * is performed. Example:
188 *
189 * Parent snapshot:
190 *
191 * . (ino 256)
192 * |-- a/ (ino 257)
193 * |-- b/ (ino 258)
194 * |-- c/ (ino 259)
195 * | |-- x/ (ino 260)
196 * |
197 * |-- y/ (ino 261)
198 *
199 * Send snapshot:
200 *
201 * . (ino 256)
202 * |-- a/ (ino 257)
203 * |-- b/ (ino 258)
204 * |-- YY/ (ino 261)
205 * |-- x/ (ino 260)
206 *
207 * Sequence of steps that lead to the send snapshot:
208 * rm -f /a/b/c/foo.txt
209 * mv /a/b/y /a/b/YY
210 * mv /a/b/c/x /a/b/YY
211 * rmdir /a/b/c
212 *
213 * When the child is processed, its move/rename is delayed until its
214 * parent is processed (as explained above), but all other operations
215 * like update utimes, chown, chgrp, etc, are performed and the paths
216 * that it uses for those operations must use the orphanized name of
217 * its parent (the directory we're going to rm later), so we need to
218 * memorize that name.
219 *
220 * Indexed by the inode number of the directory to be deleted.
221 */
222 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000223};
224
225struct pending_dir_move {
226 struct rb_node node;
227 struct list_head list;
228 u64 parent_ino;
229 u64 ino;
230 u64 gen;
231 struct list_head update_refs;
232};
233
234struct waiting_dir_move {
235 struct rb_node node;
236 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000237 /*
238 * There might be some directory that could not be removed because it
239 * was waiting for this directory inode to be moved first. Therefore
240 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
241 */
242 u64 rmdir_ino;
243};
244
245struct orphan_dir_info {
246 struct rb_node node;
247 u64 ino;
248 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +0200249};
250
251struct name_cache_entry {
252 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200253 /*
254 * radix_tree has only 32bit entries but we need to handle 64bit inums.
255 * We use the lower 32bit of the 64bit inum to store it in the tree. If
256 * more then one inum would fall into the same entry, we use radix_list
257 * to store the additional entries. radix_list is also used to store
258 * entries where two entries have the same inum but different
259 * generations.
260 */
261 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200262 u64 ino;
263 u64 gen;
264 u64 parent_ino;
265 u64 parent_gen;
266 int ret;
267 int need_later_update;
268 int name_len;
269 char name[];
270};
271
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000272static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
273
Filipe Manana9dc44212014-02-19 14:31:44 +0000274static struct waiting_dir_move *
275get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
276
277static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
278
Josef Bacik16e75492013-10-22 12:18:51 -0400279static int need_send_hole(struct send_ctx *sctx)
280{
281 return (sctx->parent_root && !sctx->cur_inode_new &&
282 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
283 S_ISREG(sctx->cur_inode_mode));
284}
285
Alexander Block31db9f72012-07-25 23:19:24 +0200286static void fs_path_reset(struct fs_path *p)
287{
288 if (p->reversed) {
289 p->start = p->buf + p->buf_len - 1;
290 p->end = p->start;
291 *p->start = 0;
292 } else {
293 p->start = p->buf;
294 p->end = p->start;
295 *p->start = 0;
296 }
297}
298
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000299static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200300{
301 struct fs_path *p;
302
303 p = kmalloc(sizeof(*p), GFP_NOFS);
304 if (!p)
305 return NULL;
306 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200307 p->buf = p->inline_buf;
308 p->buf_len = FS_PATH_INLINE_SIZE;
309 fs_path_reset(p);
310 return p;
311}
312
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000313static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200314{
315 struct fs_path *p;
316
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000317 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200318 if (!p)
319 return NULL;
320 p->reversed = 1;
321 fs_path_reset(p);
322 return p;
323}
324
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000325static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200326{
327 if (!p)
328 return;
David Sterbaace01052014-02-05 16:17:34 +0100329 if (p->buf != p->inline_buf)
330 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200331 kfree(p);
332}
333
334static int fs_path_len(struct fs_path *p)
335{
336 return p->end - p->start;
337}
338
339static int fs_path_ensure_buf(struct fs_path *p, int len)
340{
341 char *tmp_buf;
342 int path_len;
343 int old_buf_len;
344
345 len++;
346
347 if (p->buf_len >= len)
348 return 0;
349
David Sterba1b2782c2014-02-25 19:32:59 +0100350 path_len = p->end - p->start;
351 old_buf_len = p->buf_len;
352
David Sterbaace01052014-02-05 16:17:34 +0100353 /*
354 * First time the inline_buf does not suffice
355 */
David Sterba9c9ca002014-02-25 19:33:08 +0100356 if (p->buf == p->inline_buf)
357 tmp_buf = kmalloc(len, GFP_NOFS);
358 else
359 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
360 if (!tmp_buf)
361 return -ENOMEM;
362 p->buf = tmp_buf;
363 /*
364 * The real size of the buffer is bigger, this will let the fast path
365 * happen most of the time
366 */
367 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100368
Alexander Block31db9f72012-07-25 23:19:24 +0200369 if (p->reversed) {
370 tmp_buf = p->buf + old_buf_len - path_len - 1;
371 p->end = p->buf + p->buf_len - 1;
372 p->start = p->end - path_len;
373 memmove(p->start, tmp_buf, path_len + 1);
374 } else {
375 p->start = p->buf;
376 p->end = p->start + path_len;
377 }
378 return 0;
379}
380
David Sterbab23ab572014-02-03 19:23:19 +0100381static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
382 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200383{
384 int ret;
385 int new_len;
386
387 new_len = p->end - p->start + name_len;
388 if (p->start != p->end)
389 new_len++;
390 ret = fs_path_ensure_buf(p, new_len);
391 if (ret < 0)
392 goto out;
393
394 if (p->reversed) {
395 if (p->start != p->end)
396 *--p->start = '/';
397 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100398 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200399 } else {
400 if (p->start != p->end)
401 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100402 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200403 p->end += name_len;
404 *p->end = 0;
405 }
406
407out:
408 return ret;
409}
410
411static int fs_path_add(struct fs_path *p, const char *name, int name_len)
412{
413 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100414 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200415
David Sterbab23ab572014-02-03 19:23:19 +0100416 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200417 if (ret < 0)
418 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100419 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200420
421out:
422 return ret;
423}
424
425static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
426{
427 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100428 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200429
David Sterbab23ab572014-02-03 19:23:19 +0100430 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200431 if (ret < 0)
432 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100433 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200434
435out:
436 return ret;
437}
438
439static int fs_path_add_from_extent_buffer(struct fs_path *p,
440 struct extent_buffer *eb,
441 unsigned long off, int len)
442{
443 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100444 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200445
David Sterbab23ab572014-02-03 19:23:19 +0100446 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200447 if (ret < 0)
448 goto out;
449
David Sterbab23ab572014-02-03 19:23:19 +0100450 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200451
452out:
453 return ret;
454}
455
Alexander Block31db9f72012-07-25 23:19:24 +0200456static int fs_path_copy(struct fs_path *p, struct fs_path *from)
457{
458 int ret;
459
460 p->reversed = from->reversed;
461 fs_path_reset(p);
462
463 ret = fs_path_add_path(p, from);
464
465 return ret;
466}
467
468
469static void fs_path_unreverse(struct fs_path *p)
470{
471 char *tmp;
472 int len;
473
474 if (!p->reversed)
475 return;
476
477 tmp = p->start;
478 len = p->end - p->start;
479 p->start = p->buf;
480 p->end = p->start + len;
481 memmove(p->start, tmp, len + 1);
482 p->reversed = 0;
483}
484
485static struct btrfs_path *alloc_path_for_send(void)
486{
487 struct btrfs_path *path;
488
489 path = btrfs_alloc_path();
490 if (!path)
491 return NULL;
492 path->search_commit_root = 1;
493 path->skip_locking = 1;
494 return path;
495}
496
Eric Sandeen48a3b632013-04-25 20:41:01 +0000497static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200498{
499 int ret;
500 mm_segment_t old_fs;
501 u32 pos = 0;
502
503 old_fs = get_fs();
504 set_fs(KERNEL_DS);
505
506 while (pos < len) {
Anand Jain1bcea352012-09-14 00:04:21 -0600507 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200508 /* TODO handle that correctly */
509 /*if (ret == -ERESTARTSYS) {
510 continue;
511 }*/
512 if (ret < 0)
513 goto out;
514 if (ret == 0) {
515 ret = -EIO;
516 goto out;
517 }
518 pos += ret;
519 }
520
521 ret = 0;
522
523out:
524 set_fs(old_fs);
525 return ret;
526}
527
528static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
529{
530 struct btrfs_tlv_header *hdr;
531 int total_len = sizeof(*hdr) + len;
532 int left = sctx->send_max_size - sctx->send_size;
533
534 if (unlikely(left < total_len))
535 return -EOVERFLOW;
536
537 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
538 hdr->tlv_type = cpu_to_le16(attr);
539 hdr->tlv_len = cpu_to_le16(len);
540 memcpy(hdr + 1, data, len);
541 sctx->send_size += total_len;
542
543 return 0;
544}
545
David Sterba95bc79d2013-12-16 17:34:10 +0100546#define TLV_PUT_DEFINE_INT(bits) \
547 static int tlv_put_u##bits(struct send_ctx *sctx, \
548 u##bits attr, u##bits value) \
549 { \
550 __le##bits __tmp = cpu_to_le##bits(value); \
551 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
552 }
Alexander Block31db9f72012-07-25 23:19:24 +0200553
David Sterba95bc79d2013-12-16 17:34:10 +0100554TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200555
556static int tlv_put_string(struct send_ctx *sctx, u16 attr,
557 const char *str, int len)
558{
559 if (len == -1)
560 len = strlen(str);
561 return tlv_put(sctx, attr, str, len);
562}
563
564static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
565 const u8 *uuid)
566{
567 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
568}
569
Alexander Block31db9f72012-07-25 23:19:24 +0200570static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
571 struct extent_buffer *eb,
572 struct btrfs_timespec *ts)
573{
574 struct btrfs_timespec bts;
575 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
576 return tlv_put(sctx, attr, &bts, sizeof(bts));
577}
578
579
580#define TLV_PUT(sctx, attrtype, attrlen, data) \
581 do { \
582 ret = tlv_put(sctx, attrtype, attrlen, data); \
583 if (ret < 0) \
584 goto tlv_put_failure; \
585 } while (0)
586
587#define TLV_PUT_INT(sctx, attrtype, bits, value) \
588 do { \
589 ret = tlv_put_u##bits(sctx, attrtype, value); \
590 if (ret < 0) \
591 goto tlv_put_failure; \
592 } while (0)
593
594#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
595#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
596#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
597#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
598#define TLV_PUT_STRING(sctx, attrtype, str, len) \
599 do { \
600 ret = tlv_put_string(sctx, attrtype, str, len); \
601 if (ret < 0) \
602 goto tlv_put_failure; \
603 } while (0)
604#define TLV_PUT_PATH(sctx, attrtype, p) \
605 do { \
606 ret = tlv_put_string(sctx, attrtype, p->start, \
607 p->end - p->start); \
608 if (ret < 0) \
609 goto tlv_put_failure; \
610 } while(0)
611#define TLV_PUT_UUID(sctx, attrtype, uuid) \
612 do { \
613 ret = tlv_put_uuid(sctx, attrtype, uuid); \
614 if (ret < 0) \
615 goto tlv_put_failure; \
616 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200617#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
618 do { \
619 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
620 if (ret < 0) \
621 goto tlv_put_failure; \
622 } while (0)
623
624static int send_header(struct send_ctx *sctx)
625{
626 struct btrfs_stream_header hdr;
627
628 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
629 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
630
Anand Jain1bcea352012-09-14 00:04:21 -0600631 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
632 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200633}
634
635/*
636 * For each command/item we want to send to userspace, we call this function.
637 */
638static int begin_cmd(struct send_ctx *sctx, int cmd)
639{
640 struct btrfs_cmd_header *hdr;
641
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530642 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200643 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200644
645 BUG_ON(sctx->send_size);
646
647 sctx->send_size += sizeof(*hdr);
648 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
649 hdr->cmd = cpu_to_le16(cmd);
650
651 return 0;
652}
653
654static int send_cmd(struct send_ctx *sctx)
655{
656 int ret;
657 struct btrfs_cmd_header *hdr;
658 u32 crc;
659
660 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
661 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
662 hdr->crc = 0;
663
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000664 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200665 hdr->crc = cpu_to_le32(crc);
666
Anand Jain1bcea352012-09-14 00:04:21 -0600667 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
668 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200669
670 sctx->total_send_size += sctx->send_size;
671 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
672 sctx->send_size = 0;
673
674 return ret;
675}
676
677/*
678 * Sends a move instruction to user space
679 */
680static int send_rename(struct send_ctx *sctx,
681 struct fs_path *from, struct fs_path *to)
682{
683 int ret;
684
685verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
686
687 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
688 if (ret < 0)
689 goto out;
690
691 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
692 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
693
694 ret = send_cmd(sctx);
695
696tlv_put_failure:
697out:
698 return ret;
699}
700
701/*
702 * Sends a link instruction to user space
703 */
704static int send_link(struct send_ctx *sctx,
705 struct fs_path *path, struct fs_path *lnk)
706{
707 int ret;
708
709verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
710
711 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
712 if (ret < 0)
713 goto out;
714
715 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
716 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
717
718 ret = send_cmd(sctx);
719
720tlv_put_failure:
721out:
722 return ret;
723}
724
725/*
726 * Sends an unlink instruction to user space
727 */
728static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
729{
730 int ret;
731
732verbose_printk("btrfs: send_unlink %s\n", path->start);
733
734 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
735 if (ret < 0)
736 goto out;
737
738 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
739
740 ret = send_cmd(sctx);
741
742tlv_put_failure:
743out:
744 return ret;
745}
746
747/*
748 * Sends a rmdir instruction to user space
749 */
750static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
751{
752 int ret;
753
754verbose_printk("btrfs: send_rmdir %s\n", path->start);
755
756 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
757 if (ret < 0)
758 goto out;
759
760 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
761
762 ret = send_cmd(sctx);
763
764tlv_put_failure:
765out:
766 return ret;
767}
768
769/*
770 * Helper function to retrieve some fields from an inode item.
771 */
772static int get_inode_info(struct btrfs_root *root,
773 u64 ino, u64 *size, u64 *gen,
Alexander Block85a7b332012-07-26 23:39:10 +0200774 u64 *mode, u64 *uid, u64 *gid,
775 u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200776{
777 int ret;
778 struct btrfs_inode_item *ii;
779 struct btrfs_key key;
780 struct btrfs_path *path;
781
782 path = alloc_path_for_send();
783 if (!path)
784 return -ENOMEM;
785
786 key.objectid = ino;
787 key.type = BTRFS_INODE_ITEM_KEY;
788 key.offset = 0;
789 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
790 if (ret < 0)
791 goto out;
792 if (ret) {
793 ret = -ENOENT;
794 goto out;
795 }
796
797 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
798 struct btrfs_inode_item);
799 if (size)
800 *size = btrfs_inode_size(path->nodes[0], ii);
801 if (gen)
802 *gen = btrfs_inode_generation(path->nodes[0], ii);
803 if (mode)
804 *mode = btrfs_inode_mode(path->nodes[0], ii);
805 if (uid)
806 *uid = btrfs_inode_uid(path->nodes[0], ii);
807 if (gid)
808 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200809 if (rdev)
810 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200811
812out:
813 btrfs_free_path(path);
814 return ret;
815}
816
817typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
818 struct fs_path *p,
819 void *ctx);
820
821/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000822 * Helper function to iterate the entries in ONE btrfs_inode_ref or
823 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200824 * The iterate callback may return a non zero value to stop iteration. This can
825 * be a negative value for error codes or 1 to simply stop it.
826 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000827 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200828 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000829static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200830 struct btrfs_key *found_key, int resolve,
831 iterate_inode_ref_t iterate, void *ctx)
832{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000833 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200834 struct btrfs_item *item;
835 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000836 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200837 struct btrfs_path *tmp_path;
838 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000839 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200840 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000841 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200842 u32 name_len;
843 char *start;
844 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000845 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200846 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000847 u64 dir;
848 unsigned long name_off;
849 unsigned long elem_size;
850 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200851
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000852 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200853 if (!p)
854 return -ENOMEM;
855
856 tmp_path = alloc_path_for_send();
857 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000858 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200859 return -ENOMEM;
860 }
861
Alexander Block31db9f72012-07-25 23:19:24 +0200862
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000863 if (found_key->type == BTRFS_INODE_REF_KEY) {
864 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
865 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100866 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000867 total = btrfs_item_size(eb, item);
868 elem_size = sizeof(*iref);
869 } else {
870 ptr = btrfs_item_ptr_offset(eb, slot);
871 total = btrfs_item_size_nr(eb, slot);
872 elem_size = sizeof(*extref);
873 }
874
Alexander Block31db9f72012-07-25 23:19:24 +0200875 while (cur < total) {
876 fs_path_reset(p);
877
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000878 if (found_key->type == BTRFS_INODE_REF_KEY) {
879 iref = (struct btrfs_inode_ref *)(ptr + cur);
880 name_len = btrfs_inode_ref_name_len(eb, iref);
881 name_off = (unsigned long)(iref + 1);
882 index = btrfs_inode_ref_index(eb, iref);
883 dir = found_key->offset;
884 } else {
885 extref = (struct btrfs_inode_extref *)(ptr + cur);
886 name_len = btrfs_inode_extref_name_len(eb, extref);
887 name_off = (unsigned long)&extref->name;
888 index = btrfs_inode_extref_index(eb, extref);
889 dir = btrfs_inode_extref_parent(eb, extref);
890 }
891
Alexander Block31db9f72012-07-25 23:19:24 +0200892 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000893 start = btrfs_ref_to_path(root, tmp_path, name_len,
894 name_off, eb, dir,
895 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200896 if (IS_ERR(start)) {
897 ret = PTR_ERR(start);
898 goto out;
899 }
900 if (start < p->buf) {
901 /* overflow , try again with larger buffer */
902 ret = fs_path_ensure_buf(p,
903 p->buf_len + p->buf - start);
904 if (ret < 0)
905 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000906 start = btrfs_ref_to_path(root, tmp_path,
907 name_len, name_off,
908 eb, dir,
909 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200910 if (IS_ERR(start)) {
911 ret = PTR_ERR(start);
912 goto out;
913 }
914 BUG_ON(start < p->buf);
915 }
916 p->start = start;
917 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000918 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
919 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200920 if (ret < 0)
921 goto out;
922 }
923
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000924 cur += elem_size + name_len;
925 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200926 if (ret)
927 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200928 num++;
929 }
930
931out:
932 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000933 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200934 return ret;
935}
936
937typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
938 const char *name, int name_len,
939 const char *data, int data_len,
940 u8 type, void *ctx);
941
942/*
943 * Helper function to iterate the entries in ONE btrfs_dir_item.
944 * The iterate callback may return a non zero value to stop iteration. This can
945 * be a negative value for error codes or 1 to simply stop it.
946 *
947 * path must point to the dir item when called.
948 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000949static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200950 struct btrfs_key *found_key,
951 iterate_dir_item_t iterate, void *ctx)
952{
953 int ret = 0;
954 struct extent_buffer *eb;
955 struct btrfs_item *item;
956 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200957 struct btrfs_key di_key;
958 char *buf = NULL;
David Sterbaace01052014-02-05 16:17:34 +0100959 const int buf_len = PATH_MAX;
Alexander Block31db9f72012-07-25 23:19:24 +0200960 u32 name_len;
961 u32 data_len;
962 u32 cur;
963 u32 len;
964 u32 total;
965 int slot;
966 int num;
967 u8 type;
968
Alexander Block31db9f72012-07-25 23:19:24 +0200969 buf = kmalloc(buf_len, GFP_NOFS);
970 if (!buf) {
971 ret = -ENOMEM;
972 goto out;
973 }
974
Alexander Block31db9f72012-07-25 23:19:24 +0200975 eb = path->nodes[0];
976 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +0100977 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +0200978 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
979 cur = 0;
980 len = 0;
981 total = btrfs_item_size(eb, item);
982
983 num = 0;
984 while (cur < total) {
985 name_len = btrfs_dir_name_len(eb, di);
986 data_len = btrfs_dir_data_len(eb, di);
987 type = btrfs_dir_type(eb, di);
988 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
989
David Sterbaace01052014-02-05 16:17:34 +0100990 /*
991 * Path too long
992 */
Alexander Block31db9f72012-07-25 23:19:24 +0200993 if (name_len + data_len > buf_len) {
David Sterbaace01052014-02-05 16:17:34 +0100994 ret = -ENAMETOOLONG;
995 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200996 }
997
998 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
999 name_len + data_len);
1000
1001 len = sizeof(*di) + name_len + data_len;
1002 di = (struct btrfs_dir_item *)((char *)di + len);
1003 cur += len;
1004
1005 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1006 data_len, type, ctx);
1007 if (ret < 0)
1008 goto out;
1009 if (ret) {
1010 ret = 0;
1011 goto out;
1012 }
1013
1014 num++;
1015 }
1016
1017out:
David Sterbaace01052014-02-05 16:17:34 +01001018 kfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001019 return ret;
1020}
1021
1022static int __copy_first_ref(int num, u64 dir, int index,
1023 struct fs_path *p, void *ctx)
1024{
1025 int ret;
1026 struct fs_path *pt = ctx;
1027
1028 ret = fs_path_copy(pt, p);
1029 if (ret < 0)
1030 return ret;
1031
1032 /* we want the first only */
1033 return 1;
1034}
1035
1036/*
1037 * Retrieve the first path of an inode. If an inode has more then one
1038 * ref/hardlink, this is ignored.
1039 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001040static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001041 u64 ino, struct fs_path *path)
1042{
1043 int ret;
1044 struct btrfs_key key, found_key;
1045 struct btrfs_path *p;
1046
1047 p = alloc_path_for_send();
1048 if (!p)
1049 return -ENOMEM;
1050
1051 fs_path_reset(path);
1052
1053 key.objectid = ino;
1054 key.type = BTRFS_INODE_REF_KEY;
1055 key.offset = 0;
1056
1057 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1058 if (ret < 0)
1059 goto out;
1060 if (ret) {
1061 ret = 1;
1062 goto out;
1063 }
1064 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1065 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001066 (found_key.type != BTRFS_INODE_REF_KEY &&
1067 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001068 ret = -ENOENT;
1069 goto out;
1070 }
1071
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001072 ret = iterate_inode_ref(root, p, &found_key, 1,
1073 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001074 if (ret < 0)
1075 goto out;
1076 ret = 0;
1077
1078out:
1079 btrfs_free_path(p);
1080 return ret;
1081}
1082
1083struct backref_ctx {
1084 struct send_ctx *sctx;
1085
1086 /* number of total found references */
1087 u64 found;
1088
1089 /*
1090 * used for clones found in send_root. clones found behind cur_objectid
1091 * and cur_offset are not considered as allowed clones.
1092 */
1093 u64 cur_objectid;
1094 u64 cur_offset;
1095
1096 /* may be truncated in case it's the last extent in a file */
1097 u64 extent_len;
1098
1099 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001100 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001101};
1102
1103static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1104{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001105 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001106 struct clone_root *cr = (struct clone_root *)elt;
1107
1108 if (root < cr->root->objectid)
1109 return -1;
1110 if (root > cr->root->objectid)
1111 return 1;
1112 return 0;
1113}
1114
1115static int __clone_root_cmp_sort(const void *e1, const void *e2)
1116{
1117 struct clone_root *cr1 = (struct clone_root *)e1;
1118 struct clone_root *cr2 = (struct clone_root *)e2;
1119
1120 if (cr1->root->objectid < cr2->root->objectid)
1121 return -1;
1122 if (cr1->root->objectid > cr2->root->objectid)
1123 return 1;
1124 return 0;
1125}
1126
1127/*
1128 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001129 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001130 */
1131static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1132{
1133 struct backref_ctx *bctx = ctx_;
1134 struct clone_root *found;
1135 int ret;
1136 u64 i_size;
1137
1138 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001139 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001140 bctx->sctx->clone_roots_cnt,
1141 sizeof(struct clone_root),
1142 __clone_root_cmp_bsearch);
1143 if (!found)
1144 return 0;
1145
1146 if (found->root == bctx->sctx->send_root &&
1147 ino == bctx->cur_objectid &&
1148 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001149 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001150 }
1151
1152 /*
Alexander Block766702e2012-07-28 14:11:31 +02001153 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001154 * accept clones from these extents.
1155 */
Alexander Block85a7b332012-07-26 23:39:10 +02001156 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1157 NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001158 if (ret < 0)
1159 return ret;
1160
1161 if (offset + bctx->extent_len > i_size)
1162 return 0;
1163
1164 /*
1165 * Make sure we don't consider clones from send_root that are
1166 * behind the current inode/offset.
1167 */
1168 if (found->root == bctx->sctx->send_root) {
1169 /*
1170 * TODO for the moment we don't accept clones from the inode
1171 * that is currently send. We may change this when
1172 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1173 * file.
1174 */
1175 if (ino >= bctx->cur_objectid)
1176 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001177#if 0
1178 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001179 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001180 if (offset + bctx->extent_len > bctx->cur_offset)
1181 return 0;
1182#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001183 }
1184
1185 bctx->found++;
1186 found->found_refs++;
1187 if (ino < found->ino) {
1188 found->ino = ino;
1189 found->offset = offset;
1190 } else if (found->ino == ino) {
1191 /*
1192 * same extent found more then once in the same file.
1193 */
1194 if (found->offset > offset + bctx->extent_len)
1195 found->offset = offset;
1196 }
1197
1198 return 0;
1199}
1200
1201/*
Alexander Block766702e2012-07-28 14:11:31 +02001202 * Given an inode, offset and extent item, it finds a good clone for a clone
1203 * instruction. Returns -ENOENT when none could be found. The function makes
1204 * sure that the returned clone is usable at the point where sending is at the
1205 * moment. This means, that no clones are accepted which lie behind the current
1206 * inode+offset.
1207 *
Alexander Block31db9f72012-07-25 23:19:24 +02001208 * path must point to the extent item when called.
1209 */
1210static int find_extent_clone(struct send_ctx *sctx,
1211 struct btrfs_path *path,
1212 u64 ino, u64 data_offset,
1213 u64 ino_size,
1214 struct clone_root **found)
1215{
1216 int ret;
1217 int extent_type;
1218 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001219 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001220 u64 num_bytes;
1221 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001222 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001223 struct btrfs_file_extent_item *fi;
1224 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001225 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001226 struct clone_root *cur_clone_root;
1227 struct btrfs_key found_key;
1228 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001229 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001230 u32 i;
1231
1232 tmp_path = alloc_path_for_send();
1233 if (!tmp_path)
1234 return -ENOMEM;
1235
Alexander Block35075bb2012-07-28 12:44:34 +02001236 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1237 if (!backref_ctx) {
1238 ret = -ENOMEM;
1239 goto out;
1240 }
1241
Alexander Block31db9f72012-07-25 23:19:24 +02001242 if (data_offset >= ino_size) {
1243 /*
1244 * There may be extents that lie behind the file's size.
1245 * I at least had this in combination with snapshotting while
1246 * writing large files.
1247 */
1248 ret = 0;
1249 goto out;
1250 }
1251
1252 fi = btrfs_item_ptr(eb, path->slots[0],
1253 struct btrfs_file_extent_item);
1254 extent_type = btrfs_file_extent_type(eb, fi);
1255 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1256 ret = -ENOENT;
1257 goto out;
1258 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001259 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001260
1261 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001262 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1263 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001264 ret = -ENOENT;
1265 goto out;
1266 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001267 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001268
Liu Bo69917e42012-09-07 20:01:28 -06001269 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1270 &found_key, &flags);
Alexander Block31db9f72012-07-25 23:19:24 +02001271 btrfs_release_path(tmp_path);
1272
1273 if (ret < 0)
1274 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001275 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001276 ret = -EIO;
1277 goto out;
1278 }
1279
1280 /*
1281 * Setup the clone roots.
1282 */
1283 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1284 cur_clone_root = sctx->clone_roots + i;
1285 cur_clone_root->ino = (u64)-1;
1286 cur_clone_root->offset = 0;
1287 cur_clone_root->found_refs = 0;
1288 }
1289
Alexander Block35075bb2012-07-28 12:44:34 +02001290 backref_ctx->sctx = sctx;
1291 backref_ctx->found = 0;
1292 backref_ctx->cur_objectid = ino;
1293 backref_ctx->cur_offset = data_offset;
1294 backref_ctx->found_itself = 0;
1295 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001296
1297 /*
1298 * The last extent of a file may be too large due to page alignment.
1299 * We need to adjust extent_len in this case so that the checks in
1300 * __iterate_backrefs work.
1301 */
1302 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001303 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001304
1305 /*
1306 * Now collect all backrefs.
1307 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001308 if (compressed == BTRFS_COMPRESS_NONE)
1309 extent_item_pos = logical - found_key.objectid;
1310 else
1311 extent_item_pos = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001312 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1313 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001314 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001315
Alexander Block31db9f72012-07-25 23:19:24 +02001316 if (ret < 0)
1317 goto out;
1318
Alexander Block35075bb2012-07-28 12:44:34 +02001319 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001320 /* found a bug in backref code? */
1321 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001322 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001323 "send_root. inode=%llu, offset=%llu, "
Chris Mason74dd17f2012-08-07 16:25:13 -04001324 "disk_byte=%llu found extent=%llu\n",
1325 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001326 goto out;
1327 }
1328
1329verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1330 "ino=%llu, "
1331 "num_bytes=%llu, logical=%llu\n",
1332 data_offset, ino, num_bytes, logical);
1333
Alexander Block35075bb2012-07-28 12:44:34 +02001334 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001335 verbose_printk("btrfs: no clones found\n");
1336
1337 cur_clone_root = NULL;
1338 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1339 if (sctx->clone_roots[i].found_refs) {
1340 if (!cur_clone_root)
1341 cur_clone_root = sctx->clone_roots + i;
1342 else if (sctx->clone_roots[i].root == sctx->send_root)
1343 /* prefer clones from send_root over others */
1344 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001345 }
1346
1347 }
1348
1349 if (cur_clone_root) {
Filipe David Borba Manana93de4ba2014-02-15 15:53:16 +00001350 if (compressed != BTRFS_COMPRESS_NONE) {
1351 /*
1352 * Offsets given by iterate_extent_inodes() are relative
1353 * to the start of the extent, we need to add logical
1354 * offset from the file extent item.
1355 * (See why at backref.c:check_extent_in_eb())
1356 */
1357 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1358 fi);
1359 }
Alexander Block31db9f72012-07-25 23:19:24 +02001360 *found = cur_clone_root;
1361 ret = 0;
1362 } else {
1363 ret = -ENOENT;
1364 }
1365
1366out:
1367 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001368 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001369 return ret;
1370}
1371
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001372static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001373 u64 ino,
1374 struct fs_path *dest)
1375{
1376 int ret;
1377 struct btrfs_path *path;
1378 struct btrfs_key key;
1379 struct btrfs_file_extent_item *ei;
1380 u8 type;
1381 u8 compression;
1382 unsigned long off;
1383 int len;
1384
1385 path = alloc_path_for_send();
1386 if (!path)
1387 return -ENOMEM;
1388
1389 key.objectid = ino;
1390 key.type = BTRFS_EXTENT_DATA_KEY;
1391 key.offset = 0;
1392 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1393 if (ret < 0)
1394 goto out;
1395 BUG_ON(ret);
1396
1397 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1398 struct btrfs_file_extent_item);
1399 type = btrfs_file_extent_type(path->nodes[0], ei);
1400 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1401 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1402 BUG_ON(compression);
1403
1404 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001405 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001406
1407 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001408
1409out:
1410 btrfs_free_path(path);
1411 return ret;
1412}
1413
1414/*
1415 * Helper function to generate a file name that is unique in the root of
1416 * send_root and parent_root. This is used to generate names for orphan inodes.
1417 */
1418static int gen_unique_name(struct send_ctx *sctx,
1419 u64 ino, u64 gen,
1420 struct fs_path *dest)
1421{
1422 int ret = 0;
1423 struct btrfs_path *path;
1424 struct btrfs_dir_item *di;
1425 char tmp[64];
1426 int len;
1427 u64 idx = 0;
1428
1429 path = alloc_path_for_send();
1430 if (!path)
1431 return -ENOMEM;
1432
1433 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001434 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001435 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001436 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001437
1438 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1439 path, BTRFS_FIRST_FREE_OBJECTID,
1440 tmp, strlen(tmp), 0);
1441 btrfs_release_path(path);
1442 if (IS_ERR(di)) {
1443 ret = PTR_ERR(di);
1444 goto out;
1445 }
1446 if (di) {
1447 /* not unique, try again */
1448 idx++;
1449 continue;
1450 }
1451
1452 if (!sctx->parent_root) {
1453 /* unique */
1454 ret = 0;
1455 break;
1456 }
1457
1458 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1459 path, BTRFS_FIRST_FREE_OBJECTID,
1460 tmp, strlen(tmp), 0);
1461 btrfs_release_path(path);
1462 if (IS_ERR(di)) {
1463 ret = PTR_ERR(di);
1464 goto out;
1465 }
1466 if (di) {
1467 /* not unique, try again */
1468 idx++;
1469 continue;
1470 }
1471 /* unique */
1472 break;
1473 }
1474
1475 ret = fs_path_add(dest, tmp, strlen(tmp));
1476
1477out:
1478 btrfs_free_path(path);
1479 return ret;
1480}
1481
1482enum inode_state {
1483 inode_state_no_change,
1484 inode_state_will_create,
1485 inode_state_did_create,
1486 inode_state_will_delete,
1487 inode_state_did_delete,
1488};
1489
1490static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1491{
1492 int ret;
1493 int left_ret;
1494 int right_ret;
1495 u64 left_gen;
1496 u64 right_gen;
1497
1498 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001499 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001500 if (ret < 0 && ret != -ENOENT)
1501 goto out;
1502 left_ret = ret;
1503
1504 if (!sctx->parent_root) {
1505 right_ret = -ENOENT;
1506 } else {
1507 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001508 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001509 if (ret < 0 && ret != -ENOENT)
1510 goto out;
1511 right_ret = ret;
1512 }
1513
1514 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001515 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001516 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001517 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001518 if (ino < sctx->send_progress)
1519 ret = inode_state_did_create;
1520 else
1521 ret = inode_state_will_create;
1522 } else if (right_gen == gen) {
1523 if (ino < sctx->send_progress)
1524 ret = inode_state_did_delete;
1525 else
1526 ret = inode_state_will_delete;
1527 } else {
1528 ret = -ENOENT;
1529 }
1530 } else if (!left_ret) {
1531 if (left_gen == gen) {
1532 if (ino < sctx->send_progress)
1533 ret = inode_state_did_create;
1534 else
1535 ret = inode_state_will_create;
1536 } else {
1537 ret = -ENOENT;
1538 }
1539 } else if (!right_ret) {
1540 if (right_gen == gen) {
1541 if (ino < sctx->send_progress)
1542 ret = inode_state_did_delete;
1543 else
1544 ret = inode_state_will_delete;
1545 } else {
1546 ret = -ENOENT;
1547 }
1548 } else {
1549 ret = -ENOENT;
1550 }
1551
1552out:
1553 return ret;
1554}
1555
1556static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1557{
1558 int ret;
1559
1560 ret = get_cur_inode_state(sctx, ino, gen);
1561 if (ret < 0)
1562 goto out;
1563
1564 if (ret == inode_state_no_change ||
1565 ret == inode_state_did_create ||
1566 ret == inode_state_will_delete)
1567 ret = 1;
1568 else
1569 ret = 0;
1570
1571out:
1572 return ret;
1573}
1574
1575/*
1576 * Helper function to lookup a dir item in a dir.
1577 */
1578static int lookup_dir_item_inode(struct btrfs_root *root,
1579 u64 dir, const char *name, int name_len,
1580 u64 *found_inode,
1581 u8 *found_type)
1582{
1583 int ret = 0;
1584 struct btrfs_dir_item *di;
1585 struct btrfs_key key;
1586 struct btrfs_path *path;
1587
1588 path = alloc_path_for_send();
1589 if (!path)
1590 return -ENOMEM;
1591
1592 di = btrfs_lookup_dir_item(NULL, root, path,
1593 dir, name, name_len, 0);
1594 if (!di) {
1595 ret = -ENOENT;
1596 goto out;
1597 }
1598 if (IS_ERR(di)) {
1599 ret = PTR_ERR(di);
1600 goto out;
1601 }
1602 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1603 *found_inode = key.objectid;
1604 *found_type = btrfs_dir_type(path->nodes[0], di);
1605
1606out:
1607 btrfs_free_path(path);
1608 return ret;
1609}
1610
Alexander Block766702e2012-07-28 14:11:31 +02001611/*
1612 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1613 * generation of the parent dir and the name of the dir entry.
1614 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001615static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001616 u64 *dir, u64 *dir_gen, struct fs_path *name)
1617{
1618 int ret;
1619 struct btrfs_key key;
1620 struct btrfs_key found_key;
1621 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001622 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001623 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001624
1625 path = alloc_path_for_send();
1626 if (!path)
1627 return -ENOMEM;
1628
1629 key.objectid = ino;
1630 key.type = BTRFS_INODE_REF_KEY;
1631 key.offset = 0;
1632
1633 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1634 if (ret < 0)
1635 goto out;
1636 if (!ret)
1637 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1638 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001639 if (ret || found_key.objectid != ino ||
1640 (found_key.type != BTRFS_INODE_REF_KEY &&
1641 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001642 ret = -ENOENT;
1643 goto out;
1644 }
1645
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001646 if (key.type == BTRFS_INODE_REF_KEY) {
1647 struct btrfs_inode_ref *iref;
1648 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1649 struct btrfs_inode_ref);
1650 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1651 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1652 (unsigned long)(iref + 1),
1653 len);
1654 parent_dir = found_key.offset;
1655 } else {
1656 struct btrfs_inode_extref *extref;
1657 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1658 struct btrfs_inode_extref);
1659 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1660 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1661 (unsigned long)&extref->name, len);
1662 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1663 }
Alexander Block31db9f72012-07-25 23:19:24 +02001664 if (ret < 0)
1665 goto out;
1666 btrfs_release_path(path);
1667
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001668 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001669 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001670 if (ret < 0)
1671 goto out;
1672
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001673 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001674
1675out:
1676 btrfs_free_path(path);
1677 return ret;
1678}
1679
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001680static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001681 u64 ino, u64 dir,
1682 const char *name, int name_len)
1683{
1684 int ret;
1685 struct fs_path *tmp_name;
1686 u64 tmp_dir;
1687 u64 tmp_dir_gen;
1688
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001689 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001690 if (!tmp_name)
1691 return -ENOMEM;
1692
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001693 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001694 if (ret < 0)
1695 goto out;
1696
Alexander Blockb9291af2012-07-28 11:07:18 +02001697 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001698 ret = 0;
1699 goto out;
1700 }
1701
Alexander Blocke938c8a2012-07-28 16:33:49 +02001702 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001703
1704out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001705 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001706 return ret;
1707}
1708
Alexander Block766702e2012-07-28 14:11:31 +02001709/*
1710 * Used by process_recorded_refs to determine if a new ref would overwrite an
1711 * already existing ref. In case it detects an overwrite, it returns the
1712 * inode/gen in who_ino/who_gen.
1713 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1714 * to make sure later references to the overwritten inode are possible.
1715 * Orphanizing is however only required for the first ref of an inode.
1716 * process_recorded_refs does an additional is_first_ref check to see if
1717 * orphanizing is really required.
1718 */
Alexander Block31db9f72012-07-25 23:19:24 +02001719static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1720 const char *name, int name_len,
1721 u64 *who_ino, u64 *who_gen)
1722{
1723 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001724 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001725 u64 other_inode = 0;
1726 u8 other_type = 0;
1727
1728 if (!sctx->parent_root)
1729 goto out;
1730
1731 ret = is_inode_existent(sctx, dir, dir_gen);
1732 if (ret <= 0)
1733 goto out;
1734
Josef Bacikebdad912013-08-06 16:47:48 -04001735 /*
1736 * If we have a parent root we need to verify that the parent dir was
1737 * not delted and then re-created, if it was then we have no overwrite
1738 * and we can just unlink this entry.
1739 */
1740 if (sctx->parent_root) {
1741 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1742 NULL, NULL, NULL);
1743 if (ret < 0 && ret != -ENOENT)
1744 goto out;
1745 if (ret) {
1746 ret = 0;
1747 goto out;
1748 }
1749 if (gen != dir_gen)
1750 goto out;
1751 }
1752
Alexander Block31db9f72012-07-25 23:19:24 +02001753 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1754 &other_inode, &other_type);
1755 if (ret < 0 && ret != -ENOENT)
1756 goto out;
1757 if (ret) {
1758 ret = 0;
1759 goto out;
1760 }
1761
Alexander Block766702e2012-07-28 14:11:31 +02001762 /*
1763 * Check if the overwritten ref was already processed. If yes, the ref
1764 * was already unlinked/moved, so we can safely assume that we will not
1765 * overwrite anything at this point in time.
1766 */
Alexander Block31db9f72012-07-25 23:19:24 +02001767 if (other_inode > sctx->send_progress) {
1768 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001769 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001770 if (ret < 0)
1771 goto out;
1772
1773 ret = 1;
1774 *who_ino = other_inode;
1775 } else {
1776 ret = 0;
1777 }
1778
1779out:
1780 return ret;
1781}
1782
Alexander Block766702e2012-07-28 14:11:31 +02001783/*
1784 * Checks if the ref was overwritten by an already processed inode. This is
1785 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1786 * thus the orphan name needs be used.
1787 * process_recorded_refs also uses it to avoid unlinking of refs that were
1788 * overwritten.
1789 */
Alexander Block31db9f72012-07-25 23:19:24 +02001790static int did_overwrite_ref(struct send_ctx *sctx,
1791 u64 dir, u64 dir_gen,
1792 u64 ino, u64 ino_gen,
1793 const char *name, int name_len)
1794{
1795 int ret = 0;
1796 u64 gen;
1797 u64 ow_inode;
1798 u8 other_type;
1799
1800 if (!sctx->parent_root)
1801 goto out;
1802
1803 ret = is_inode_existent(sctx, dir, dir_gen);
1804 if (ret <= 0)
1805 goto out;
1806
1807 /* check if the ref was overwritten by another ref */
1808 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1809 &ow_inode, &other_type);
1810 if (ret < 0 && ret != -ENOENT)
1811 goto out;
1812 if (ret) {
1813 /* was never and will never be overwritten */
1814 ret = 0;
1815 goto out;
1816 }
1817
1818 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001819 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001820 if (ret < 0)
1821 goto out;
1822
1823 if (ow_inode == ino && gen == ino_gen) {
1824 ret = 0;
1825 goto out;
1826 }
1827
1828 /* we know that it is or will be overwritten. check this now */
1829 if (ow_inode < sctx->send_progress)
1830 ret = 1;
1831 else
1832 ret = 0;
1833
1834out:
1835 return ret;
1836}
1837
Alexander Block766702e2012-07-28 14:11:31 +02001838/*
1839 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1840 * that got overwritten. This is used by process_recorded_refs to determine
1841 * if it has to use the path as returned by get_cur_path or the orphan name.
1842 */
Alexander Block31db9f72012-07-25 23:19:24 +02001843static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1844{
1845 int ret = 0;
1846 struct fs_path *name = NULL;
1847 u64 dir;
1848 u64 dir_gen;
1849
1850 if (!sctx->parent_root)
1851 goto out;
1852
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001853 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001854 if (!name)
1855 return -ENOMEM;
1856
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001857 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001858 if (ret < 0)
1859 goto out;
1860
1861 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1862 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001863
1864out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001865 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001866 return ret;
1867}
1868
Alexander Block766702e2012-07-28 14:11:31 +02001869/*
1870 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1871 * so we need to do some special handling in case we have clashes. This function
1872 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001873 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001874 */
Alexander Block31db9f72012-07-25 23:19:24 +02001875static int name_cache_insert(struct send_ctx *sctx,
1876 struct name_cache_entry *nce)
1877{
1878 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001879 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001880
Alexander Block7e0926f2012-07-28 14:20:58 +02001881 nce_head = radix_tree_lookup(&sctx->name_cache,
1882 (unsigned long)nce->ino);
1883 if (!nce_head) {
1884 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001885 if (!nce_head) {
1886 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001887 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001888 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001889 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001890
Alexander Block7e0926f2012-07-28 14:20:58 +02001891 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001892 if (ret < 0) {
1893 kfree(nce_head);
1894 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001895 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001896 }
Alexander Block31db9f72012-07-25 23:19:24 +02001897 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001898 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001899 list_add_tail(&nce->list, &sctx->name_cache_list);
1900 sctx->name_cache_size++;
1901
1902 return ret;
1903}
1904
1905static void name_cache_delete(struct send_ctx *sctx,
1906 struct name_cache_entry *nce)
1907{
Alexander Block7e0926f2012-07-28 14:20:58 +02001908 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001909
Alexander Block7e0926f2012-07-28 14:20:58 +02001910 nce_head = radix_tree_lookup(&sctx->name_cache,
1911 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01001912 if (!nce_head) {
1913 btrfs_err(sctx->send_root->fs_info,
1914 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1915 nce->ino, sctx->name_cache_size);
1916 }
Alexander Block31db9f72012-07-25 23:19:24 +02001917
Alexander Block7e0926f2012-07-28 14:20:58 +02001918 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001919 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001920 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001921
David Sterba57fb8912014-02-03 19:24:40 +01001922 /*
1923 * We may not get to the final release of nce_head if the lookup fails
1924 */
1925 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02001926 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1927 kfree(nce_head);
1928 }
Alexander Block31db9f72012-07-25 23:19:24 +02001929}
1930
1931static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1932 u64 ino, u64 gen)
1933{
Alexander Block7e0926f2012-07-28 14:20:58 +02001934 struct list_head *nce_head;
1935 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001936
Alexander Block7e0926f2012-07-28 14:20:58 +02001937 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1938 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001939 return NULL;
1940
Alexander Block7e0926f2012-07-28 14:20:58 +02001941 list_for_each_entry(cur, nce_head, radix_list) {
1942 if (cur->ino == ino && cur->gen == gen)
1943 return cur;
1944 }
Alexander Block31db9f72012-07-25 23:19:24 +02001945 return NULL;
1946}
1947
Alexander Block766702e2012-07-28 14:11:31 +02001948/*
1949 * Removes the entry from the list and adds it back to the end. This marks the
1950 * entry as recently used so that name_cache_clean_unused does not remove it.
1951 */
Alexander Block31db9f72012-07-25 23:19:24 +02001952static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1953{
1954 list_del(&nce->list);
1955 list_add_tail(&nce->list, &sctx->name_cache_list);
1956}
1957
Alexander Block766702e2012-07-28 14:11:31 +02001958/*
1959 * Remove some entries from the beginning of name_cache_list.
1960 */
Alexander Block31db9f72012-07-25 23:19:24 +02001961static void name_cache_clean_unused(struct send_ctx *sctx)
1962{
1963 struct name_cache_entry *nce;
1964
1965 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1966 return;
1967
1968 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1969 nce = list_entry(sctx->name_cache_list.next,
1970 struct name_cache_entry, list);
1971 name_cache_delete(sctx, nce);
1972 kfree(nce);
1973 }
1974}
1975
1976static void name_cache_free(struct send_ctx *sctx)
1977{
1978 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02001979
Alexander Blocke938c8a2012-07-28 16:33:49 +02001980 while (!list_empty(&sctx->name_cache_list)) {
1981 nce = list_entry(sctx->name_cache_list.next,
1982 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02001983 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02001984 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001985 }
1986}
1987
Alexander Block766702e2012-07-28 14:11:31 +02001988/*
1989 * Used by get_cur_path for each ref up to the root.
1990 * Returns 0 if it succeeded.
1991 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1992 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1993 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1994 * Returns <0 in case of error.
1995 */
Alexander Block31db9f72012-07-25 23:19:24 +02001996static int __get_cur_name_and_parent(struct send_ctx *sctx,
1997 u64 ino, u64 gen,
1998 u64 *parent_ino,
1999 u64 *parent_gen,
2000 struct fs_path *dest)
2001{
2002 int ret;
2003 int nce_ret;
2004 struct btrfs_path *path = NULL;
2005 struct name_cache_entry *nce = NULL;
2006
Alexander Block766702e2012-07-28 14:11:31 +02002007 /*
2008 * First check if we already did a call to this function with the same
2009 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2010 * return the cached result.
2011 */
Alexander Block31db9f72012-07-25 23:19:24 +02002012 nce = name_cache_search(sctx, ino, gen);
2013 if (nce) {
2014 if (ino < sctx->send_progress && nce->need_later_update) {
2015 name_cache_delete(sctx, nce);
2016 kfree(nce);
2017 nce = NULL;
2018 } else {
2019 name_cache_used(sctx, nce);
2020 *parent_ino = nce->parent_ino;
2021 *parent_gen = nce->parent_gen;
2022 ret = fs_path_add(dest, nce->name, nce->name_len);
2023 if (ret < 0)
2024 goto out;
2025 ret = nce->ret;
2026 goto out;
2027 }
2028 }
2029
2030 path = alloc_path_for_send();
2031 if (!path)
2032 return -ENOMEM;
2033
Alexander Block766702e2012-07-28 14:11:31 +02002034 /*
2035 * If the inode is not existent yet, add the orphan name and return 1.
2036 * This should only happen for the parent dir that we determine in
2037 * __record_new_ref
2038 */
Alexander Block31db9f72012-07-25 23:19:24 +02002039 ret = is_inode_existent(sctx, ino, gen);
2040 if (ret < 0)
2041 goto out;
2042
2043 if (!ret) {
2044 ret = gen_unique_name(sctx, ino, gen, dest);
2045 if (ret < 0)
2046 goto out;
2047 ret = 1;
2048 goto out_cache;
2049 }
2050
Alexander Block766702e2012-07-28 14:11:31 +02002051 /*
2052 * Depending on whether the inode was already processed or not, use
2053 * send_root or parent_root for ref lookup.
2054 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002055 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002056 ret = get_first_ref(sctx->send_root, ino,
2057 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002058 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002059 ret = get_first_ref(sctx->parent_root, ino,
2060 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002061 if (ret < 0)
2062 goto out;
2063
Alexander Block766702e2012-07-28 14:11:31 +02002064 /*
2065 * Check if the ref was overwritten by an inode's ref that was processed
2066 * earlier. If yes, treat as orphan and return 1.
2067 */
Alexander Block31db9f72012-07-25 23:19:24 +02002068 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2069 dest->start, dest->end - dest->start);
2070 if (ret < 0)
2071 goto out;
2072 if (ret) {
2073 fs_path_reset(dest);
2074 ret = gen_unique_name(sctx, ino, gen, dest);
2075 if (ret < 0)
2076 goto out;
2077 ret = 1;
2078 }
2079
2080out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002081 /*
2082 * Store the result of the lookup in the name cache.
2083 */
Alexander Block31db9f72012-07-25 23:19:24 +02002084 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2085 if (!nce) {
2086 ret = -ENOMEM;
2087 goto out;
2088 }
2089
2090 nce->ino = ino;
2091 nce->gen = gen;
2092 nce->parent_ino = *parent_ino;
2093 nce->parent_gen = *parent_gen;
2094 nce->name_len = fs_path_len(dest);
2095 nce->ret = ret;
2096 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002097
2098 if (ino < sctx->send_progress)
2099 nce->need_later_update = 0;
2100 else
2101 nce->need_later_update = 1;
2102
2103 nce_ret = name_cache_insert(sctx, nce);
2104 if (nce_ret < 0)
2105 ret = nce_ret;
2106 name_cache_clean_unused(sctx);
2107
2108out:
2109 btrfs_free_path(path);
2110 return ret;
2111}
2112
2113/*
2114 * Magic happens here. This function returns the first ref to an inode as it
2115 * would look like while receiving the stream at this point in time.
2116 * We walk the path up to the root. For every inode in between, we check if it
2117 * was already processed/sent. If yes, we continue with the parent as found
2118 * in send_root. If not, we continue with the parent as found in parent_root.
2119 * If we encounter an inode that was deleted at this point in time, we use the
2120 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2121 * that were not created yet and overwritten inodes/refs.
2122 *
2123 * When do we have have orphan inodes:
2124 * 1. When an inode is freshly created and thus no valid refs are available yet
2125 * 2. When a directory lost all it's refs (deleted) but still has dir items
2126 * inside which were not processed yet (pending for move/delete). If anyone
2127 * tried to get the path to the dir items, it would get a path inside that
2128 * orphan directory.
2129 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2130 * of an unprocessed inode. If in that case the first ref would be
2131 * overwritten, the overwritten inode gets "orphanized". Later when we
2132 * process this overwritten inode, it is restored at a new place by moving
2133 * the orphan inode.
2134 *
2135 * sctx->send_progress tells this function at which point in time receiving
2136 * would be.
2137 */
2138static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2139 struct fs_path *dest)
2140{
2141 int ret = 0;
2142 struct fs_path *name = NULL;
2143 u64 parent_inode = 0;
2144 u64 parent_gen = 0;
2145 int stop = 0;
2146
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002147 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002148 if (!name) {
2149 ret = -ENOMEM;
2150 goto out;
2151 }
2152
2153 dest->reversed = 1;
2154 fs_path_reset(dest);
2155
2156 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2157 fs_path_reset(name);
2158
Filipe Manana9dc44212014-02-19 14:31:44 +00002159 if (is_waiting_for_rm(sctx, ino)) {
2160 ret = gen_unique_name(sctx, ino, gen, name);
2161 if (ret < 0)
2162 goto out;
2163 ret = fs_path_add_path(dest, name);
2164 break;
2165 }
2166
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002167 if (is_waiting_for_move(sctx, ino)) {
2168 ret = get_first_ref(sctx->parent_root, ino,
2169 &parent_inode, &parent_gen, name);
2170 } else {
2171 ret = __get_cur_name_and_parent(sctx, ino, gen,
2172 &parent_inode,
2173 &parent_gen, name);
2174 if (ret)
2175 stop = 1;
2176 }
2177
Alexander Block31db9f72012-07-25 23:19:24 +02002178 if (ret < 0)
2179 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002180
Alexander Block31db9f72012-07-25 23:19:24 +02002181 ret = fs_path_add_path(dest, name);
2182 if (ret < 0)
2183 goto out;
2184
2185 ino = parent_inode;
2186 gen = parent_gen;
2187 }
2188
2189out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002190 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002191 if (!ret)
2192 fs_path_unreverse(dest);
2193 return ret;
2194}
2195
2196/*
Alexander Block31db9f72012-07-25 23:19:24 +02002197 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2198 */
2199static int send_subvol_begin(struct send_ctx *sctx)
2200{
2201 int ret;
2202 struct btrfs_root *send_root = sctx->send_root;
2203 struct btrfs_root *parent_root = sctx->parent_root;
2204 struct btrfs_path *path;
2205 struct btrfs_key key;
2206 struct btrfs_root_ref *ref;
2207 struct extent_buffer *leaf;
2208 char *name = NULL;
2209 int namelen;
2210
Wang Shilongffcfaf82014-01-15 00:26:43 +08002211 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002212 if (!path)
2213 return -ENOMEM;
2214
2215 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2216 if (!name) {
2217 btrfs_free_path(path);
2218 return -ENOMEM;
2219 }
2220
2221 key.objectid = send_root->objectid;
2222 key.type = BTRFS_ROOT_BACKREF_KEY;
2223 key.offset = 0;
2224
2225 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2226 &key, path, 1, 0);
2227 if (ret < 0)
2228 goto out;
2229 if (ret) {
2230 ret = -ENOENT;
2231 goto out;
2232 }
2233
2234 leaf = path->nodes[0];
2235 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2236 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2237 key.objectid != send_root->objectid) {
2238 ret = -ENOENT;
2239 goto out;
2240 }
2241 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2242 namelen = btrfs_root_ref_name_len(leaf, ref);
2243 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2244 btrfs_release_path(path);
2245
Alexander Block31db9f72012-07-25 23:19:24 +02002246 if (parent_root) {
2247 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2248 if (ret < 0)
2249 goto out;
2250 } else {
2251 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2252 if (ret < 0)
2253 goto out;
2254 }
2255
2256 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2257 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2258 sctx->send_root->root_item.uuid);
2259 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002260 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002261 if (parent_root) {
2262 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2263 sctx->parent_root->root_item.uuid);
2264 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002265 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002266 }
2267
2268 ret = send_cmd(sctx);
2269
2270tlv_put_failure:
2271out:
2272 btrfs_free_path(path);
2273 kfree(name);
2274 return ret;
2275}
2276
2277static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2278{
2279 int ret = 0;
2280 struct fs_path *p;
2281
2282verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2283
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002284 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002285 if (!p)
2286 return -ENOMEM;
2287
2288 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2289 if (ret < 0)
2290 goto out;
2291
2292 ret = get_cur_path(sctx, ino, gen, p);
2293 if (ret < 0)
2294 goto out;
2295 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2296 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2297
2298 ret = send_cmd(sctx);
2299
2300tlv_put_failure:
2301out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002302 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002303 return ret;
2304}
2305
2306static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2307{
2308 int ret = 0;
2309 struct fs_path *p;
2310
2311verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2312
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002313 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002314 if (!p)
2315 return -ENOMEM;
2316
2317 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2318 if (ret < 0)
2319 goto out;
2320
2321 ret = get_cur_path(sctx, ino, gen, p);
2322 if (ret < 0)
2323 goto out;
2324 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2325 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2326
2327 ret = send_cmd(sctx);
2328
2329tlv_put_failure:
2330out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002331 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002332 return ret;
2333}
2334
2335static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2336{
2337 int ret = 0;
2338 struct fs_path *p;
2339
2340verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2341
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002342 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002343 if (!p)
2344 return -ENOMEM;
2345
2346 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2347 if (ret < 0)
2348 goto out;
2349
2350 ret = get_cur_path(sctx, ino, gen, p);
2351 if (ret < 0)
2352 goto out;
2353 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2354 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2355 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2356
2357 ret = send_cmd(sctx);
2358
2359tlv_put_failure:
2360out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002361 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002362 return ret;
2363}
2364
2365static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2366{
2367 int ret = 0;
2368 struct fs_path *p = NULL;
2369 struct btrfs_inode_item *ii;
2370 struct btrfs_path *path = NULL;
2371 struct extent_buffer *eb;
2372 struct btrfs_key key;
2373 int slot;
2374
2375verbose_printk("btrfs: send_utimes %llu\n", ino);
2376
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002377 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002378 if (!p)
2379 return -ENOMEM;
2380
2381 path = alloc_path_for_send();
2382 if (!path) {
2383 ret = -ENOMEM;
2384 goto out;
2385 }
2386
2387 key.objectid = ino;
2388 key.type = BTRFS_INODE_ITEM_KEY;
2389 key.offset = 0;
2390 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2391 if (ret < 0)
2392 goto out;
2393
2394 eb = path->nodes[0];
2395 slot = path->slots[0];
2396 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2397
2398 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2399 if (ret < 0)
2400 goto out;
2401
2402 ret = get_cur_path(sctx, ino, gen, p);
2403 if (ret < 0)
2404 goto out;
2405 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2406 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2407 btrfs_inode_atime(ii));
2408 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2409 btrfs_inode_mtime(ii));
2410 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2411 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002412 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002413
2414 ret = send_cmd(sctx);
2415
2416tlv_put_failure:
2417out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002418 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002419 btrfs_free_path(path);
2420 return ret;
2421}
2422
2423/*
2424 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2425 * a valid path yet because we did not process the refs yet. So, the inode
2426 * is created as orphan.
2427 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002428static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002429{
2430 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002431 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002432 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002433 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002434 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002435 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002436
Alexander Block1f4692d2012-07-28 10:42:24 +02002437verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002438
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002439 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002440 if (!p)
2441 return -ENOMEM;
2442
Liu Bo644d1942014-02-27 17:29:01 +08002443 if (ino != sctx->cur_ino) {
2444 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2445 NULL, NULL, &rdev);
2446 if (ret < 0)
2447 goto out;
2448 } else {
2449 gen = sctx->cur_inode_gen;
2450 mode = sctx->cur_inode_mode;
2451 rdev = sctx->cur_inode_rdev;
2452 }
Alexander Block31db9f72012-07-25 23:19:24 +02002453
Alexander Blocke938c8a2012-07-28 16:33:49 +02002454 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002455 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002456 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002457 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002458 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002459 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002460 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002461 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002462 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002463 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002464 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002465 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002466 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002467 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2468 (int)(mode & S_IFMT));
2469 ret = -ENOTSUPP;
2470 goto out;
2471 }
2472
2473 ret = begin_cmd(sctx, cmd);
2474 if (ret < 0)
2475 goto out;
2476
Alexander Block1f4692d2012-07-28 10:42:24 +02002477 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002478 if (ret < 0)
2479 goto out;
2480
2481 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002482 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002483
2484 if (S_ISLNK(mode)) {
2485 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002486 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002487 if (ret < 0)
2488 goto out;
2489 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2490 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2491 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002492 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2493 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002494 }
2495
2496 ret = send_cmd(sctx);
2497 if (ret < 0)
2498 goto out;
2499
2500
2501tlv_put_failure:
2502out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002503 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002504 return ret;
2505}
2506
Alexander Block1f4692d2012-07-28 10:42:24 +02002507/*
2508 * We need some special handling for inodes that get processed before the parent
2509 * directory got created. See process_recorded_refs for details.
2510 * This function does the check if we already created the dir out of order.
2511 */
2512static int did_create_dir(struct send_ctx *sctx, u64 dir)
2513{
2514 int ret = 0;
2515 struct btrfs_path *path = NULL;
2516 struct btrfs_key key;
2517 struct btrfs_key found_key;
2518 struct btrfs_key di_key;
2519 struct extent_buffer *eb;
2520 struct btrfs_dir_item *di;
2521 int slot;
2522
2523 path = alloc_path_for_send();
2524 if (!path) {
2525 ret = -ENOMEM;
2526 goto out;
2527 }
2528
2529 key.objectid = dir;
2530 key.type = BTRFS_DIR_INDEX_KEY;
2531 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002532 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2533 if (ret < 0)
2534 goto out;
2535
Alexander Block1f4692d2012-07-28 10:42:24 +02002536 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002537 eb = path->nodes[0];
2538 slot = path->slots[0];
2539 if (slot >= btrfs_header_nritems(eb)) {
2540 ret = btrfs_next_leaf(sctx->send_root, path);
2541 if (ret < 0) {
2542 goto out;
2543 } else if (ret > 0) {
2544 ret = 0;
2545 break;
2546 }
2547 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002548 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002549
2550 btrfs_item_key_to_cpu(eb, &found_key, slot);
2551 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002552 found_key.type != key.type) {
2553 ret = 0;
2554 goto out;
2555 }
2556
2557 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2558 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2559
Josef Bacika0525412013-08-12 10:56:14 -04002560 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2561 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002562 ret = 1;
2563 goto out;
2564 }
2565
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002566 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002567 }
2568
2569out:
2570 btrfs_free_path(path);
2571 return ret;
2572}
2573
2574/*
2575 * Only creates the inode if it is:
2576 * 1. Not a directory
2577 * 2. Or a directory which was not created already due to out of order
2578 * directories. See did_create_dir and process_recorded_refs for details.
2579 */
2580static int send_create_inode_if_needed(struct send_ctx *sctx)
2581{
2582 int ret;
2583
2584 if (S_ISDIR(sctx->cur_inode_mode)) {
2585 ret = did_create_dir(sctx, sctx->cur_ino);
2586 if (ret < 0)
2587 goto out;
2588 if (ret) {
2589 ret = 0;
2590 goto out;
2591 }
2592 }
2593
2594 ret = send_create_inode(sctx, sctx->cur_ino);
2595 if (ret < 0)
2596 goto out;
2597
2598out:
2599 return ret;
2600}
2601
Alexander Block31db9f72012-07-25 23:19:24 +02002602struct recorded_ref {
2603 struct list_head list;
2604 char *dir_path;
2605 char *name;
2606 struct fs_path *full_path;
2607 u64 dir;
2608 u64 dir_gen;
2609 int dir_path_len;
2610 int name_len;
2611};
2612
2613/*
2614 * We need to process new refs before deleted refs, but compare_tree gives us
2615 * everything mixed. So we first record all refs and later process them.
2616 * This function is a helper to record one ref.
2617 */
Liu Boa4d96d62014-03-03 21:31:03 +08002618static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002619 u64 dir_gen, struct fs_path *path)
2620{
2621 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002622
2623 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2624 if (!ref)
2625 return -ENOMEM;
2626
2627 ref->dir = dir;
2628 ref->dir_gen = dir_gen;
2629 ref->full_path = path;
2630
Andy Shevchenkoed848852013-08-21 10:32:13 +03002631 ref->name = (char *)kbasename(ref->full_path->start);
2632 ref->name_len = ref->full_path->end - ref->name;
2633 ref->dir_path = ref->full_path->start;
2634 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002635 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002636 else
Alexander Block31db9f72012-07-25 23:19:24 +02002637 ref->dir_path_len = ref->full_path->end -
2638 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002639
2640 list_add_tail(&ref->list, head);
2641 return 0;
2642}
2643
Josef Bacikba5e8f22013-08-16 16:52:55 -04002644static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2645{
2646 struct recorded_ref *new;
2647
2648 new = kmalloc(sizeof(*ref), GFP_NOFS);
2649 if (!new)
2650 return -ENOMEM;
2651
2652 new->dir = ref->dir;
2653 new->dir_gen = ref->dir_gen;
2654 new->full_path = NULL;
2655 INIT_LIST_HEAD(&new->list);
2656 list_add_tail(&new->list, list);
2657 return 0;
2658}
2659
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002660static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002661{
2662 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002663
Alexander Blocke938c8a2012-07-28 16:33:49 +02002664 while (!list_empty(head)) {
2665 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002666 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002667 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002668 kfree(cur);
2669 }
Alexander Block31db9f72012-07-25 23:19:24 +02002670}
2671
2672static void free_recorded_refs(struct send_ctx *sctx)
2673{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002674 __free_recorded_refs(&sctx->new_refs);
2675 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002676}
2677
2678/*
Alexander Block766702e2012-07-28 14:11:31 +02002679 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002680 * ref of an unprocessed inode gets overwritten and for all non empty
2681 * directories.
2682 */
2683static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2684 struct fs_path *path)
2685{
2686 int ret;
2687 struct fs_path *orphan;
2688
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002689 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002690 if (!orphan)
2691 return -ENOMEM;
2692
2693 ret = gen_unique_name(sctx, ino, gen, orphan);
2694 if (ret < 0)
2695 goto out;
2696
2697 ret = send_rename(sctx, path, orphan);
2698
2699out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002700 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002701 return ret;
2702}
2703
Filipe Manana9dc44212014-02-19 14:31:44 +00002704static struct orphan_dir_info *
2705add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2706{
2707 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2708 struct rb_node *parent = NULL;
2709 struct orphan_dir_info *entry, *odi;
2710
2711 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2712 if (!odi)
2713 return ERR_PTR(-ENOMEM);
2714 odi->ino = dir_ino;
2715 odi->gen = 0;
2716
2717 while (*p) {
2718 parent = *p;
2719 entry = rb_entry(parent, struct orphan_dir_info, node);
2720 if (dir_ino < entry->ino) {
2721 p = &(*p)->rb_left;
2722 } else if (dir_ino > entry->ino) {
2723 p = &(*p)->rb_right;
2724 } else {
2725 kfree(odi);
2726 return entry;
2727 }
2728 }
2729
2730 rb_link_node(&odi->node, parent, p);
2731 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2732 return odi;
2733}
2734
2735static struct orphan_dir_info *
2736get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2737{
2738 struct rb_node *n = sctx->orphan_dirs.rb_node;
2739 struct orphan_dir_info *entry;
2740
2741 while (n) {
2742 entry = rb_entry(n, struct orphan_dir_info, node);
2743 if (dir_ino < entry->ino)
2744 n = n->rb_left;
2745 else if (dir_ino > entry->ino)
2746 n = n->rb_right;
2747 else
2748 return entry;
2749 }
2750 return NULL;
2751}
2752
2753static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2754{
2755 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2756
2757 return odi != NULL;
2758}
2759
2760static void free_orphan_dir_info(struct send_ctx *sctx,
2761 struct orphan_dir_info *odi)
2762{
2763 if (!odi)
2764 return;
2765 rb_erase(&odi->node, &sctx->orphan_dirs);
2766 kfree(odi);
2767}
2768
Alexander Block31db9f72012-07-25 23:19:24 +02002769/*
2770 * Returns 1 if a directory can be removed at this point in time.
2771 * We check this by iterating all dir items and checking if the inode behind
2772 * the dir item was already processed.
2773 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002774static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2775 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002776{
2777 int ret = 0;
2778 struct btrfs_root *root = sctx->parent_root;
2779 struct btrfs_path *path;
2780 struct btrfs_key key;
2781 struct btrfs_key found_key;
2782 struct btrfs_key loc;
2783 struct btrfs_dir_item *di;
2784
Alexander Block6d85ed02012-08-01 14:48:59 +02002785 /*
2786 * Don't try to rmdir the top/root subvolume dir.
2787 */
2788 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2789 return 0;
2790
Alexander Block31db9f72012-07-25 23:19:24 +02002791 path = alloc_path_for_send();
2792 if (!path)
2793 return -ENOMEM;
2794
2795 key.objectid = dir;
2796 key.type = BTRFS_DIR_INDEX_KEY;
2797 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002798 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2799 if (ret < 0)
2800 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002801
2802 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002803 struct waiting_dir_move *dm;
2804
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002805 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2806 ret = btrfs_next_leaf(root, path);
2807 if (ret < 0)
2808 goto out;
2809 else if (ret > 0)
2810 break;
2811 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002812 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002813 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2814 path->slots[0]);
2815 if (found_key.objectid != key.objectid ||
2816 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002817 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002818
2819 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2820 struct btrfs_dir_item);
2821 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2822
Filipe Manana9dc44212014-02-19 14:31:44 +00002823 dm = get_waiting_dir_move(sctx, loc.objectid);
2824 if (dm) {
2825 struct orphan_dir_info *odi;
2826
2827 odi = add_orphan_dir_info(sctx, dir);
2828 if (IS_ERR(odi)) {
2829 ret = PTR_ERR(odi);
2830 goto out;
2831 }
2832 odi->gen = dir_gen;
2833 dm->rmdir_ino = dir;
2834 ret = 0;
2835 goto out;
2836 }
2837
Alexander Block31db9f72012-07-25 23:19:24 +02002838 if (loc.objectid > send_progress) {
2839 ret = 0;
2840 goto out;
2841 }
2842
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002843 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002844 }
2845
2846 ret = 1;
2847
2848out:
2849 btrfs_free_path(path);
2850 return ret;
2851}
2852
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002853static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2854{
Filipe Manana9dc44212014-02-19 14:31:44 +00002855 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002856
Filipe Manana9dc44212014-02-19 14:31:44 +00002857 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002858}
2859
2860static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2861{
2862 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2863 struct rb_node *parent = NULL;
2864 struct waiting_dir_move *entry, *dm;
2865
2866 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2867 if (!dm)
2868 return -ENOMEM;
2869 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00002870 dm->rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002871
2872 while (*p) {
2873 parent = *p;
2874 entry = rb_entry(parent, struct waiting_dir_move, node);
2875 if (ino < entry->ino) {
2876 p = &(*p)->rb_left;
2877 } else if (ino > entry->ino) {
2878 p = &(*p)->rb_right;
2879 } else {
2880 kfree(dm);
2881 return -EEXIST;
2882 }
2883 }
2884
2885 rb_link_node(&dm->node, parent, p);
2886 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2887 return 0;
2888}
2889
Filipe Manana9dc44212014-02-19 14:31:44 +00002890static struct waiting_dir_move *
2891get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002892{
2893 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2894 struct waiting_dir_move *entry;
2895
2896 while (n) {
2897 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00002898 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002899 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00002900 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002901 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002902 else
2903 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002904 }
Filipe Manana9dc44212014-02-19 14:31:44 +00002905 return NULL;
2906}
2907
2908static void free_waiting_dir_move(struct send_ctx *sctx,
2909 struct waiting_dir_move *dm)
2910{
2911 if (!dm)
2912 return;
2913 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2914 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002915}
2916
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002917static int add_pending_dir_move(struct send_ctx *sctx, u64 parent_ino)
2918{
2919 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2920 struct rb_node *parent = NULL;
2921 struct pending_dir_move *entry, *pm;
2922 struct recorded_ref *cur;
2923 int exists = 0;
2924 int ret;
2925
2926 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2927 if (!pm)
2928 return -ENOMEM;
2929 pm->parent_ino = parent_ino;
2930 pm->ino = sctx->cur_ino;
2931 pm->gen = sctx->cur_inode_gen;
2932 INIT_LIST_HEAD(&pm->list);
2933 INIT_LIST_HEAD(&pm->update_refs);
2934 RB_CLEAR_NODE(&pm->node);
2935
2936 while (*p) {
2937 parent = *p;
2938 entry = rb_entry(parent, struct pending_dir_move, node);
2939 if (parent_ino < entry->parent_ino) {
2940 p = &(*p)->rb_left;
2941 } else if (parent_ino > entry->parent_ino) {
2942 p = &(*p)->rb_right;
2943 } else {
2944 exists = 1;
2945 break;
2946 }
2947 }
2948
2949 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2950 ret = dup_ref(cur, &pm->update_refs);
2951 if (ret < 0)
2952 goto out;
2953 }
2954 list_for_each_entry(cur, &sctx->new_refs, list) {
2955 ret = dup_ref(cur, &pm->update_refs);
2956 if (ret < 0)
2957 goto out;
2958 }
2959
2960 ret = add_waiting_dir_move(sctx, pm->ino);
2961 if (ret)
2962 goto out;
2963
2964 if (exists) {
2965 list_add_tail(&pm->list, &entry->list);
2966 } else {
2967 rb_link_node(&pm->node, parent, p);
2968 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
2969 }
2970 ret = 0;
2971out:
2972 if (ret) {
2973 __free_recorded_refs(&pm->update_refs);
2974 kfree(pm);
2975 }
2976 return ret;
2977}
2978
2979static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
2980 u64 parent_ino)
2981{
2982 struct rb_node *n = sctx->pending_dir_moves.rb_node;
2983 struct pending_dir_move *entry;
2984
2985 while (n) {
2986 entry = rb_entry(n, struct pending_dir_move, node);
2987 if (parent_ino < entry->parent_ino)
2988 n = n->rb_left;
2989 else if (parent_ino > entry->parent_ino)
2990 n = n->rb_right;
2991 else
2992 return entry;
2993 }
2994 return NULL;
2995}
2996
2997static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
2998{
2999 struct fs_path *from_path = NULL;
3000 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003001 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003002 u64 orig_progress = sctx->send_progress;
3003 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003004 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003005 struct waiting_dir_move *dm = NULL;
3006 u64 rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003007 int ret;
3008
Filipe Manana2b863a12014-02-16 13:43:11 +00003009 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003010 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003011 if (!name || !from_path) {
3012 ret = -ENOMEM;
3013 goto out;
3014 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003015
Filipe Manana9dc44212014-02-19 14:31:44 +00003016 dm = get_waiting_dir_move(sctx, pm->ino);
3017 ASSERT(dm);
3018 rmdir_ino = dm->rmdir_ino;
3019 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003020
3021 ret = get_first_ref(sctx->parent_root, pm->ino,
3022 &parent_ino, &parent_gen, name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003023 if (ret < 0)
3024 goto out;
3025
Filipe Manana2b863a12014-02-16 13:43:11 +00003026 if (parent_ino == sctx->cur_ino) {
3027 /* child only renamed, not moved */
3028 ASSERT(parent_gen == sctx->cur_inode_gen);
3029 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3030 from_path);
3031 if (ret < 0)
3032 goto out;
3033 ret = fs_path_add_path(from_path, name);
3034 if (ret < 0)
3035 goto out;
3036 } else {
3037 /* child moved and maybe renamed too */
3038 sctx->send_progress = pm->ino;
3039 ret = get_cur_path(sctx, pm->ino, pm->gen, from_path);
3040 if (ret < 0)
3041 goto out;
3042 }
3043
3044 fs_path_free(name);
3045 name = NULL;
3046
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003047 to_path = fs_path_alloc();
3048 if (!to_path) {
3049 ret = -ENOMEM;
3050 goto out;
3051 }
3052
3053 sctx->send_progress = sctx->cur_ino + 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003054 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3055 if (ret < 0)
3056 goto out;
3057
3058 ret = send_rename(sctx, from_path, to_path);
3059 if (ret < 0)
3060 goto out;
3061
Filipe Manana9dc44212014-02-19 14:31:44 +00003062 if (rmdir_ino) {
3063 struct orphan_dir_info *odi;
3064
3065 odi = get_orphan_dir_info(sctx, rmdir_ino);
3066 if (!odi) {
3067 /* already deleted */
3068 goto finish;
3069 }
3070 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3071 if (ret < 0)
3072 goto out;
3073 if (!ret)
3074 goto finish;
3075
3076 name = fs_path_alloc();
3077 if (!name) {
3078 ret = -ENOMEM;
3079 goto out;
3080 }
3081 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3082 if (ret < 0)
3083 goto out;
3084 ret = send_rmdir(sctx, name);
3085 if (ret < 0)
3086 goto out;
3087 free_orphan_dir_info(sctx, odi);
3088 }
3089
3090finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003091 ret = send_utimes(sctx, pm->ino, pm->gen);
3092 if (ret < 0)
3093 goto out;
3094
3095 /*
3096 * After rename/move, need to update the utimes of both new parent(s)
3097 * and old parent(s).
3098 */
3099 list_for_each_entry(cur, &pm->update_refs, list) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003100 if (cur->dir == rmdir_ino)
3101 continue;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003102 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3103 if (ret < 0)
3104 goto out;
3105 }
3106
3107out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003108 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003109 fs_path_free(from_path);
3110 fs_path_free(to_path);
3111 sctx->send_progress = orig_progress;
3112
3113 return ret;
3114}
3115
3116static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3117{
3118 if (!list_empty(&m->list))
3119 list_del(&m->list);
3120 if (!RB_EMPTY_NODE(&m->node))
3121 rb_erase(&m->node, &sctx->pending_dir_moves);
3122 __free_recorded_refs(&m->update_refs);
3123 kfree(m);
3124}
3125
3126static void tail_append_pending_moves(struct pending_dir_move *moves,
3127 struct list_head *stack)
3128{
3129 if (list_empty(&moves->list)) {
3130 list_add_tail(&moves->list, stack);
3131 } else {
3132 LIST_HEAD(list);
3133 list_splice_init(&moves->list, &list);
3134 list_add_tail(&moves->list, stack);
3135 list_splice_tail(&list, stack);
3136 }
3137}
3138
3139static int apply_children_dir_moves(struct send_ctx *sctx)
3140{
3141 struct pending_dir_move *pm;
3142 struct list_head stack;
3143 u64 parent_ino = sctx->cur_ino;
3144 int ret = 0;
3145
3146 pm = get_pending_dir_moves(sctx, parent_ino);
3147 if (!pm)
3148 return 0;
3149
3150 INIT_LIST_HEAD(&stack);
3151 tail_append_pending_moves(pm, &stack);
3152
3153 while (!list_empty(&stack)) {
3154 pm = list_first_entry(&stack, struct pending_dir_move, list);
3155 parent_ino = pm->ino;
3156 ret = apply_dir_move(sctx, pm);
3157 free_pending_move(sctx, pm);
3158 if (ret)
3159 goto out;
3160 pm = get_pending_dir_moves(sctx, parent_ino);
3161 if (pm)
3162 tail_append_pending_moves(pm, &stack);
3163 }
3164 return 0;
3165
3166out:
3167 while (!list_empty(&stack)) {
3168 pm = list_first_entry(&stack, struct pending_dir_move, list);
3169 free_pending_move(sctx, pm);
3170 }
3171 return ret;
3172}
3173
3174static int wait_for_parent_move(struct send_ctx *sctx,
3175 struct recorded_ref *parent_ref)
3176{
3177 int ret;
3178 u64 ino = parent_ref->dir;
3179 u64 parent_ino_before, parent_ino_after;
3180 u64 new_gen, old_gen;
3181 struct fs_path *path_before = NULL;
3182 struct fs_path *path_after = NULL;
3183 int len1, len2;
3184
3185 if (parent_ref->dir <= sctx->cur_ino)
3186 return 0;
3187
3188 if (is_waiting_for_move(sctx, ino))
3189 return 1;
3190
3191 ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen,
3192 NULL, NULL, NULL, NULL);
3193 if (ret == -ENOENT)
3194 return 0;
3195 else if (ret < 0)
3196 return ret;
3197
3198 ret = get_inode_info(sctx->send_root, ino, NULL, &new_gen,
3199 NULL, NULL, NULL, NULL);
3200 if (ret < 0)
3201 return ret;
3202
3203 if (new_gen != old_gen)
3204 return 0;
3205
3206 path_before = fs_path_alloc();
3207 if (!path_before)
3208 return -ENOMEM;
3209
3210 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3211 NULL, path_before);
3212 if (ret == -ENOENT) {
3213 ret = 0;
3214 goto out;
3215 } else if (ret < 0) {
3216 goto out;
3217 }
3218
3219 path_after = fs_path_alloc();
3220 if (!path_after) {
3221 ret = -ENOMEM;
3222 goto out;
3223 }
3224
3225 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3226 NULL, path_after);
3227 if (ret == -ENOENT) {
3228 ret = 0;
3229 goto out;
3230 } else if (ret < 0) {
3231 goto out;
3232 }
3233
3234 len1 = fs_path_len(path_before);
3235 len2 = fs_path_len(path_after);
Filipe David Borba Manana5ed7f9f2014-02-01 02:02:16 +00003236 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3237 memcmp(path_before->start, path_after->start, len1)) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003238 ret = 1;
3239 goto out;
3240 }
3241 ret = 0;
3242
3243out:
3244 fs_path_free(path_before);
3245 fs_path_free(path_after);
3246
3247 return ret;
3248}
3249
Alexander Block31db9f72012-07-25 23:19:24 +02003250/*
3251 * This does all the move/link/unlink/rmdir magic.
3252 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003253static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003254{
3255 int ret = 0;
3256 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003257 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003258 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003259 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003260 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003261 u64 ow_gen;
3262 int did_overwrite = 0;
3263 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003264 u64 last_dir_ino_rm = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003265
3266verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3267
Alexander Block6d85ed02012-08-01 14:48:59 +02003268 /*
3269 * This should never happen as the root dir always has the same ref
3270 * which is always '..'
3271 */
3272 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003273 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003274
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003275 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003276 if (!valid_path) {
3277 ret = -ENOMEM;
3278 goto out;
3279 }
3280
Alexander Block31db9f72012-07-25 23:19:24 +02003281 /*
3282 * First, check if the first ref of the current inode was overwritten
3283 * before. If yes, we know that the current inode was already orphanized
3284 * and thus use the orphan name. If not, we can use get_cur_path to
3285 * get the path of the first ref as it would like while receiving at
3286 * this point in time.
3287 * New inodes are always orphan at the beginning, so force to use the
3288 * orphan name in this case.
3289 * The first ref is stored in valid_path and will be updated if it
3290 * gets moved around.
3291 */
3292 if (!sctx->cur_inode_new) {
3293 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3294 sctx->cur_inode_gen);
3295 if (ret < 0)
3296 goto out;
3297 if (ret)
3298 did_overwrite = 1;
3299 }
3300 if (sctx->cur_inode_new || did_overwrite) {
3301 ret = gen_unique_name(sctx, sctx->cur_ino,
3302 sctx->cur_inode_gen, valid_path);
3303 if (ret < 0)
3304 goto out;
3305 is_orphan = 1;
3306 } else {
3307 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3308 valid_path);
3309 if (ret < 0)
3310 goto out;
3311 }
3312
3313 list_for_each_entry(cur, &sctx->new_refs, list) {
3314 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003315 * We may have refs where the parent directory does not exist
3316 * yet. This happens if the parent directories inum is higher
3317 * the the current inum. To handle this case, we create the
3318 * parent directory out of order. But we need to check if this
3319 * did already happen before due to other refs in the same dir.
3320 */
3321 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3322 if (ret < 0)
3323 goto out;
3324 if (ret == inode_state_will_create) {
3325 ret = 0;
3326 /*
3327 * First check if any of the current inodes refs did
3328 * already create the dir.
3329 */
3330 list_for_each_entry(cur2, &sctx->new_refs, list) {
3331 if (cur == cur2)
3332 break;
3333 if (cur2->dir == cur->dir) {
3334 ret = 1;
3335 break;
3336 }
3337 }
3338
3339 /*
3340 * If that did not happen, check if a previous inode
3341 * did already create the dir.
3342 */
3343 if (!ret)
3344 ret = did_create_dir(sctx, cur->dir);
3345 if (ret < 0)
3346 goto out;
3347 if (!ret) {
3348 ret = send_create_inode(sctx, cur->dir);
3349 if (ret < 0)
3350 goto out;
3351 }
3352 }
3353
3354 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003355 * Check if this new ref would overwrite the first ref of
3356 * another unprocessed inode. If yes, orphanize the
3357 * overwritten inode. If we find an overwritten ref that is
3358 * not the first ref, simply unlink it.
3359 */
3360 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3361 cur->name, cur->name_len,
3362 &ow_inode, &ow_gen);
3363 if (ret < 0)
3364 goto out;
3365 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003366 ret = is_first_ref(sctx->parent_root,
3367 ow_inode, cur->dir, cur->name,
3368 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003369 if (ret < 0)
3370 goto out;
3371 if (ret) {
3372 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3373 cur->full_path);
3374 if (ret < 0)
3375 goto out;
3376 } else {
3377 ret = send_unlink(sctx, cur->full_path);
3378 if (ret < 0)
3379 goto out;
3380 }
3381 }
3382
3383 /*
3384 * link/move the ref to the new place. If we have an orphan
3385 * inode, move it and update valid_path. If not, link or move
3386 * it depending on the inode mode.
3387 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003388 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003389 ret = send_rename(sctx, valid_path, cur->full_path);
3390 if (ret < 0)
3391 goto out;
3392 is_orphan = 0;
3393 ret = fs_path_copy(valid_path, cur->full_path);
3394 if (ret < 0)
3395 goto out;
3396 } else {
3397 if (S_ISDIR(sctx->cur_inode_mode)) {
3398 /*
3399 * Dirs can't be linked, so move it. For moved
3400 * dirs, we always have one new and one deleted
3401 * ref. The deleted ref is ignored later.
3402 */
Filipe David Borba Mananad86477b2014-01-30 13:27:12 +00003403 ret = wait_for_parent_move(sctx, cur);
3404 if (ret < 0)
3405 goto out;
3406 if (ret) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003407 ret = add_pending_dir_move(sctx,
3408 cur->dir);
3409 *pending_move = 1;
3410 } else {
3411 ret = send_rename(sctx, valid_path,
3412 cur->full_path);
3413 if (!ret)
3414 ret = fs_path_copy(valid_path,
3415 cur->full_path);
3416 }
Alexander Block31db9f72012-07-25 23:19:24 +02003417 if (ret < 0)
3418 goto out;
3419 } else {
3420 ret = send_link(sctx, cur->full_path,
3421 valid_path);
3422 if (ret < 0)
3423 goto out;
3424 }
3425 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003426 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003427 if (ret < 0)
3428 goto out;
3429 }
3430
3431 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3432 /*
3433 * Check if we can already rmdir the directory. If not,
3434 * orphanize it. For every dir item inside that gets deleted
3435 * later, we do this check again and rmdir it then if possible.
3436 * See the use of check_dirs for more details.
3437 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003438 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3439 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003440 if (ret < 0)
3441 goto out;
3442 if (ret) {
3443 ret = send_rmdir(sctx, valid_path);
3444 if (ret < 0)
3445 goto out;
3446 } else if (!is_orphan) {
3447 ret = orphanize_inode(sctx, sctx->cur_ino,
3448 sctx->cur_inode_gen, valid_path);
3449 if (ret < 0)
3450 goto out;
3451 is_orphan = 1;
3452 }
3453
3454 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003455 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003456 if (ret < 0)
3457 goto out;
3458 }
Alexander Blockccf16262012-07-28 11:46:29 +02003459 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3460 !list_empty(&sctx->deleted_refs)) {
3461 /*
3462 * We have a moved dir. Add the old parent to check_dirs
3463 */
3464 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3465 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003466 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003467 if (ret < 0)
3468 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003469 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3470 /*
3471 * We have a non dir inode. Go through all deleted refs and
3472 * unlink them if they were not already overwritten by other
3473 * inodes.
3474 */
3475 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3476 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3477 sctx->cur_ino, sctx->cur_inode_gen,
3478 cur->name, cur->name_len);
3479 if (ret < 0)
3480 goto out;
3481 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003482 ret = send_unlink(sctx, cur->full_path);
3483 if (ret < 0)
3484 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003485 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003486 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003487 if (ret < 0)
3488 goto out;
3489 }
Alexander Block31db9f72012-07-25 23:19:24 +02003490 /*
3491 * If the inode is still orphan, unlink the orphan. This may
3492 * happen when a previous inode did overwrite the first ref
3493 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003494 * inode. Unlinking does not mean that the inode is deleted in
3495 * all cases. There may still be links to this inode in other
3496 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003497 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003498 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003499 ret = send_unlink(sctx, valid_path);
3500 if (ret < 0)
3501 goto out;
3502 }
3503 }
3504
3505 /*
3506 * We did collect all parent dirs where cur_inode was once located. We
3507 * now go through all these dirs and check if they are pending for
3508 * deletion and if it's finally possible to perform the rmdir now.
3509 * We also update the inode stats of the parent dirs here.
3510 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003511 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003512 /*
3513 * In case we had refs into dirs that were not processed yet,
3514 * we don't need to do the utime and rmdir logic for these dirs.
3515 * The dir will be processed later.
3516 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003517 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003518 continue;
3519
Josef Bacikba5e8f22013-08-16 16:52:55 -04003520 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003521 if (ret < 0)
3522 goto out;
3523
3524 if (ret == inode_state_did_create ||
3525 ret == inode_state_no_change) {
3526 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003527 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003528 if (ret < 0)
3529 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003530 } else if (ret == inode_state_did_delete &&
3531 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003532 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3533 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003534 if (ret < 0)
3535 goto out;
3536 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003537 ret = get_cur_path(sctx, cur->dir,
3538 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003539 if (ret < 0)
3540 goto out;
3541 ret = send_rmdir(sctx, valid_path);
3542 if (ret < 0)
3543 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003544 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02003545 }
3546 }
3547 }
3548
Alexander Block31db9f72012-07-25 23:19:24 +02003549 ret = 0;
3550
3551out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04003552 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003553 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003554 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003555 return ret;
3556}
3557
Liu Boa4d96d62014-03-03 21:31:03 +08003558static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3559 struct fs_path *name, void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02003560{
3561 int ret = 0;
3562 struct send_ctx *sctx = ctx;
3563 struct fs_path *p;
3564 u64 gen;
3565
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003566 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003567 if (!p)
3568 return -ENOMEM;
3569
Liu Boa4d96d62014-03-03 21:31:03 +08003570 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003571 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003572 if (ret < 0)
3573 goto out;
3574
Alexander Block31db9f72012-07-25 23:19:24 +02003575 ret = get_cur_path(sctx, dir, gen, p);
3576 if (ret < 0)
3577 goto out;
3578 ret = fs_path_add_path(p, name);
3579 if (ret < 0)
3580 goto out;
3581
Liu Boa4d96d62014-03-03 21:31:03 +08003582 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003583
3584out:
3585 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003586 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003587 return ret;
3588}
3589
Liu Boa4d96d62014-03-03 21:31:03 +08003590static int __record_new_ref(int num, u64 dir, int index,
3591 struct fs_path *name,
3592 void *ctx)
3593{
3594 struct send_ctx *sctx = ctx;
3595 return record_ref(sctx->send_root, num, dir, index, name,
3596 ctx, &sctx->new_refs);
3597}
3598
3599
Alexander Block31db9f72012-07-25 23:19:24 +02003600static int __record_deleted_ref(int num, u64 dir, int index,
3601 struct fs_path *name,
3602 void *ctx)
3603{
Alexander Block31db9f72012-07-25 23:19:24 +02003604 struct send_ctx *sctx = ctx;
Liu Boa4d96d62014-03-03 21:31:03 +08003605 return record_ref(sctx->parent_root, num, dir, index, name,
3606 ctx, &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02003607}
3608
3609static int record_new_ref(struct send_ctx *sctx)
3610{
3611 int ret;
3612
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003613 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3614 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003615 if (ret < 0)
3616 goto out;
3617 ret = 0;
3618
3619out:
3620 return ret;
3621}
3622
3623static int record_deleted_ref(struct send_ctx *sctx)
3624{
3625 int ret;
3626
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003627 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3628 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003629 if (ret < 0)
3630 goto out;
3631 ret = 0;
3632
3633out:
3634 return ret;
3635}
3636
3637struct find_ref_ctx {
3638 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003639 u64 dir_gen;
3640 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003641 struct fs_path *name;
3642 int found_idx;
3643};
3644
3645static int __find_iref(int num, u64 dir, int index,
3646 struct fs_path *name,
3647 void *ctx_)
3648{
3649 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003650 u64 dir_gen;
3651 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003652
3653 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3654 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003655 /*
3656 * To avoid doing extra lookups we'll only do this if everything
3657 * else matches.
3658 */
3659 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3660 NULL, NULL, NULL);
3661 if (ret)
3662 return ret;
3663 if (dir_gen != ctx->dir_gen)
3664 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003665 ctx->found_idx = num;
3666 return 1;
3667 }
3668 return 0;
3669}
3670
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003671static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003672 struct btrfs_path *path,
3673 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003674 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003675{
3676 int ret;
3677 struct find_ref_ctx ctx;
3678
3679 ctx.dir = dir;
3680 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003681 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003682 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003683 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003684
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003685 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003686 if (ret < 0)
3687 return ret;
3688
3689 if (ctx.found_idx == -1)
3690 return -ENOENT;
3691
3692 return ctx.found_idx;
3693}
3694
3695static int __record_changed_new_ref(int num, u64 dir, int index,
3696 struct fs_path *name,
3697 void *ctx)
3698{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003699 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003700 int ret;
3701 struct send_ctx *sctx = ctx;
3702
Josef Bacikba5e8f22013-08-16 16:52:55 -04003703 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3704 NULL, NULL, NULL);
3705 if (ret)
3706 return ret;
3707
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003708 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003709 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003710 if (ret == -ENOENT)
3711 ret = __record_new_ref(num, dir, index, name, sctx);
3712 else if (ret > 0)
3713 ret = 0;
3714
3715 return ret;
3716}
3717
3718static int __record_changed_deleted_ref(int num, u64 dir, int index,
3719 struct fs_path *name,
3720 void *ctx)
3721{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003722 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003723 int ret;
3724 struct send_ctx *sctx = ctx;
3725
Josef Bacikba5e8f22013-08-16 16:52:55 -04003726 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3727 NULL, NULL, NULL);
3728 if (ret)
3729 return ret;
3730
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003731 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003732 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003733 if (ret == -ENOENT)
3734 ret = __record_deleted_ref(num, dir, index, name, sctx);
3735 else if (ret > 0)
3736 ret = 0;
3737
3738 return ret;
3739}
3740
3741static int record_changed_ref(struct send_ctx *sctx)
3742{
3743 int ret = 0;
3744
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003745 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003746 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3747 if (ret < 0)
3748 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003749 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003750 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3751 if (ret < 0)
3752 goto out;
3753 ret = 0;
3754
3755out:
3756 return ret;
3757}
3758
3759/*
3760 * Record and process all refs at once. Needed when an inode changes the
3761 * generation number, which means that it was deleted and recreated.
3762 */
3763static int process_all_refs(struct send_ctx *sctx,
3764 enum btrfs_compare_tree_result cmd)
3765{
3766 int ret;
3767 struct btrfs_root *root;
3768 struct btrfs_path *path;
3769 struct btrfs_key key;
3770 struct btrfs_key found_key;
3771 struct extent_buffer *eb;
3772 int slot;
3773 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003774 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003775
3776 path = alloc_path_for_send();
3777 if (!path)
3778 return -ENOMEM;
3779
3780 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3781 root = sctx->send_root;
3782 cb = __record_new_ref;
3783 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3784 root = sctx->parent_root;
3785 cb = __record_deleted_ref;
3786 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01003787 btrfs_err(sctx->send_root->fs_info,
3788 "Wrong command %d in process_all_refs", cmd);
3789 ret = -EINVAL;
3790 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003791 }
3792
3793 key.objectid = sctx->cmp_key->objectid;
3794 key.type = BTRFS_INODE_REF_KEY;
3795 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003796 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3797 if (ret < 0)
3798 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003799
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003800 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02003801 eb = path->nodes[0];
3802 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003803 if (slot >= btrfs_header_nritems(eb)) {
3804 ret = btrfs_next_leaf(root, path);
3805 if (ret < 0)
3806 goto out;
3807 else if (ret > 0)
3808 break;
3809 continue;
3810 }
3811
Alexander Block31db9f72012-07-25 23:19:24 +02003812 btrfs_item_key_to_cpu(eb, &found_key, slot);
3813
3814 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003815 (found_key.type != BTRFS_INODE_REF_KEY &&
3816 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003817 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003818
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003819 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003820 if (ret < 0)
3821 goto out;
3822
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003823 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003824 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003825 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003826
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003827 ret = process_recorded_refs(sctx, &pending_move);
3828 /* Only applicable to an incremental send. */
3829 ASSERT(pending_move == 0);
Alexander Block31db9f72012-07-25 23:19:24 +02003830
3831out:
3832 btrfs_free_path(path);
3833 return ret;
3834}
3835
3836static int send_set_xattr(struct send_ctx *sctx,
3837 struct fs_path *path,
3838 const char *name, int name_len,
3839 const char *data, int data_len)
3840{
3841 int ret = 0;
3842
3843 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3844 if (ret < 0)
3845 goto out;
3846
3847 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3848 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3849 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3850
3851 ret = send_cmd(sctx);
3852
3853tlv_put_failure:
3854out:
3855 return ret;
3856}
3857
3858static int send_remove_xattr(struct send_ctx *sctx,
3859 struct fs_path *path,
3860 const char *name, int name_len)
3861{
3862 int ret = 0;
3863
3864 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3865 if (ret < 0)
3866 goto out;
3867
3868 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3869 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3870
3871 ret = send_cmd(sctx);
3872
3873tlv_put_failure:
3874out:
3875 return ret;
3876}
3877
3878static int __process_new_xattr(int num, struct btrfs_key *di_key,
3879 const char *name, int name_len,
3880 const char *data, int data_len,
3881 u8 type, void *ctx)
3882{
3883 int ret;
3884 struct send_ctx *sctx = ctx;
3885 struct fs_path *p;
3886 posix_acl_xattr_header dummy_acl;
3887
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003888 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003889 if (!p)
3890 return -ENOMEM;
3891
3892 /*
3893 * This hack is needed because empty acl's are stored as zero byte
3894 * data in xattrs. Problem with that is, that receiving these zero byte
3895 * acl's will fail later. To fix this, we send a dummy acl list that
3896 * only contains the version number and no entries.
3897 */
3898 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3899 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3900 if (data_len == 0) {
3901 dummy_acl.a_version =
3902 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3903 data = (char *)&dummy_acl;
3904 data_len = sizeof(dummy_acl);
3905 }
3906 }
3907
3908 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3909 if (ret < 0)
3910 goto out;
3911
3912 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3913
3914out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003915 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003916 return ret;
3917}
3918
3919static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3920 const char *name, int name_len,
3921 const char *data, int data_len,
3922 u8 type, void *ctx)
3923{
3924 int ret;
3925 struct send_ctx *sctx = ctx;
3926 struct fs_path *p;
3927
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003928 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003929 if (!p)
3930 return -ENOMEM;
3931
3932 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3933 if (ret < 0)
3934 goto out;
3935
3936 ret = send_remove_xattr(sctx, p, name, name_len);
3937
3938out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003939 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003940 return ret;
3941}
3942
3943static int process_new_xattr(struct send_ctx *sctx)
3944{
3945 int ret = 0;
3946
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003947 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3948 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003949
3950 return ret;
3951}
3952
3953static int process_deleted_xattr(struct send_ctx *sctx)
3954{
3955 int ret;
3956
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003957 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3958 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003959
3960 return ret;
3961}
3962
3963struct find_xattr_ctx {
3964 const char *name;
3965 int name_len;
3966 int found_idx;
3967 char *found_data;
3968 int found_data_len;
3969};
3970
3971static int __find_xattr(int num, struct btrfs_key *di_key,
3972 const char *name, int name_len,
3973 const char *data, int data_len,
3974 u8 type, void *vctx)
3975{
3976 struct find_xattr_ctx *ctx = vctx;
3977
3978 if (name_len == ctx->name_len &&
3979 strncmp(name, ctx->name, name_len) == 0) {
3980 ctx->found_idx = num;
3981 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00003982 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02003983 if (!ctx->found_data)
3984 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02003985 return 1;
3986 }
3987 return 0;
3988}
3989
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003990static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003991 struct btrfs_path *path,
3992 struct btrfs_key *key,
3993 const char *name, int name_len,
3994 char **data, int *data_len)
3995{
3996 int ret;
3997 struct find_xattr_ctx ctx;
3998
3999 ctx.name = name;
4000 ctx.name_len = name_len;
4001 ctx.found_idx = -1;
4002 ctx.found_data = NULL;
4003 ctx.found_data_len = 0;
4004
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004005 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004006 if (ret < 0)
4007 return ret;
4008
4009 if (ctx.found_idx == -1)
4010 return -ENOENT;
4011 if (data) {
4012 *data = ctx.found_data;
4013 *data_len = ctx.found_data_len;
4014 } else {
4015 kfree(ctx.found_data);
4016 }
4017 return ctx.found_idx;
4018}
4019
4020
4021static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4022 const char *name, int name_len,
4023 const char *data, int data_len,
4024 u8 type, void *ctx)
4025{
4026 int ret;
4027 struct send_ctx *sctx = ctx;
4028 char *found_data = NULL;
4029 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004030
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004031 ret = find_xattr(sctx->parent_root, sctx->right_path,
4032 sctx->cmp_key, name, name_len, &found_data,
4033 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004034 if (ret == -ENOENT) {
4035 ret = __process_new_xattr(num, di_key, name, name_len, data,
4036 data_len, type, ctx);
4037 } else if (ret >= 0) {
4038 if (data_len != found_data_len ||
4039 memcmp(data, found_data, data_len)) {
4040 ret = __process_new_xattr(num, di_key, name, name_len,
4041 data, data_len, type, ctx);
4042 } else {
4043 ret = 0;
4044 }
4045 }
4046
4047 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004048 return ret;
4049}
4050
4051static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4052 const char *name, int name_len,
4053 const char *data, int data_len,
4054 u8 type, void *ctx)
4055{
4056 int ret;
4057 struct send_ctx *sctx = ctx;
4058
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004059 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4060 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004061 if (ret == -ENOENT)
4062 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4063 data_len, type, ctx);
4064 else if (ret >= 0)
4065 ret = 0;
4066
4067 return ret;
4068}
4069
4070static int process_changed_xattr(struct send_ctx *sctx)
4071{
4072 int ret = 0;
4073
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004074 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004075 sctx->cmp_key, __process_changed_new_xattr, sctx);
4076 if (ret < 0)
4077 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004078 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004079 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4080
4081out:
4082 return ret;
4083}
4084
4085static int process_all_new_xattrs(struct send_ctx *sctx)
4086{
4087 int ret;
4088 struct btrfs_root *root;
4089 struct btrfs_path *path;
4090 struct btrfs_key key;
4091 struct btrfs_key found_key;
4092 struct extent_buffer *eb;
4093 int slot;
4094
4095 path = alloc_path_for_send();
4096 if (!path)
4097 return -ENOMEM;
4098
4099 root = sctx->send_root;
4100
4101 key.objectid = sctx->cmp_key->objectid;
4102 key.type = BTRFS_XATTR_ITEM_KEY;
4103 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004104 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4105 if (ret < 0)
4106 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004107
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004108 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004109 eb = path->nodes[0];
4110 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004111 if (slot >= btrfs_header_nritems(eb)) {
4112 ret = btrfs_next_leaf(root, path);
4113 if (ret < 0) {
4114 goto out;
4115 } else if (ret > 0) {
4116 ret = 0;
4117 break;
4118 }
4119 continue;
4120 }
Alexander Block31db9f72012-07-25 23:19:24 +02004121
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004122 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004123 if (found_key.objectid != key.objectid ||
4124 found_key.type != key.type) {
4125 ret = 0;
4126 goto out;
4127 }
4128
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004129 ret = iterate_dir_item(root, path, &found_key,
4130 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004131 if (ret < 0)
4132 goto out;
4133
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004134 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004135 }
4136
4137out:
4138 btrfs_free_path(path);
4139 return ret;
4140}
4141
Josef Baciked259092013-10-25 11:36:01 -04004142static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4143{
4144 struct btrfs_root *root = sctx->send_root;
4145 struct btrfs_fs_info *fs_info = root->fs_info;
4146 struct inode *inode;
4147 struct page *page;
4148 char *addr;
4149 struct btrfs_key key;
4150 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4151 pgoff_t last_index;
4152 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4153 ssize_t ret = 0;
4154
4155 key.objectid = sctx->cur_ino;
4156 key.type = BTRFS_INODE_ITEM_KEY;
4157 key.offset = 0;
4158
4159 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4160 if (IS_ERR(inode))
4161 return PTR_ERR(inode);
4162
4163 if (offset + len > i_size_read(inode)) {
4164 if (offset > i_size_read(inode))
4165 len = 0;
4166 else
4167 len = offset - i_size_read(inode);
4168 }
4169 if (len == 0)
4170 goto out;
4171
4172 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
4173 while (index <= last_index) {
4174 unsigned cur_len = min_t(unsigned, len,
4175 PAGE_CACHE_SIZE - pg_offset);
4176 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4177 if (!page) {
4178 ret = -ENOMEM;
4179 break;
4180 }
4181
4182 if (!PageUptodate(page)) {
4183 btrfs_readpage(NULL, page);
4184 lock_page(page);
4185 if (!PageUptodate(page)) {
4186 unlock_page(page);
4187 page_cache_release(page);
4188 ret = -EIO;
4189 break;
4190 }
4191 }
4192
4193 addr = kmap(page);
4194 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4195 kunmap(page);
4196 unlock_page(page);
4197 page_cache_release(page);
4198 index++;
4199 pg_offset = 0;
4200 len -= cur_len;
4201 ret += cur_len;
4202 }
4203out:
4204 iput(inode);
4205 return ret;
4206}
4207
Alexander Block31db9f72012-07-25 23:19:24 +02004208/*
4209 * Read some bytes from the current inode/file and send a write command to
4210 * user space.
4211 */
4212static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4213{
4214 int ret = 0;
4215 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004216 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004217
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004218 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004219 if (!p)
4220 return -ENOMEM;
4221
Alexander Block31db9f72012-07-25 23:19:24 +02004222verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4223
Josef Baciked259092013-10-25 11:36:01 -04004224 num_read = fill_read_buf(sctx, offset, len);
4225 if (num_read <= 0) {
4226 if (num_read < 0)
4227 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004228 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004229 }
Alexander Block31db9f72012-07-25 23:19:24 +02004230
4231 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4232 if (ret < 0)
4233 goto out;
4234
4235 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4236 if (ret < 0)
4237 goto out;
4238
4239 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4240 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004241 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004242
4243 ret = send_cmd(sctx);
4244
4245tlv_put_failure:
4246out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004247 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004248 if (ret < 0)
4249 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004250 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004251}
4252
4253/*
4254 * Send a clone command to user space.
4255 */
4256static int send_clone(struct send_ctx *sctx,
4257 u64 offset, u32 len,
4258 struct clone_root *clone_root)
4259{
4260 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004261 struct fs_path *p;
4262 u64 gen;
4263
4264verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4265 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4266 clone_root->root->objectid, clone_root->ino,
4267 clone_root->offset);
4268
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004269 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004270 if (!p)
4271 return -ENOMEM;
4272
4273 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4274 if (ret < 0)
4275 goto out;
4276
4277 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4278 if (ret < 0)
4279 goto out;
4280
4281 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4282 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4283 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4284
Alexander Blocke938c8a2012-07-28 16:33:49 +02004285 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004286 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004287 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004288 if (ret < 0)
4289 goto out;
4290 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4291 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004292 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004293 }
4294 if (ret < 0)
4295 goto out;
4296
4297 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02004298 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004299 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004300 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004301 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4302 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4303 clone_root->offset);
4304
4305 ret = send_cmd(sctx);
4306
4307tlv_put_failure:
4308out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004309 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004310 return ret;
4311}
4312
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004313/*
4314 * Send an update extent command to user space.
4315 */
4316static int send_update_extent(struct send_ctx *sctx,
4317 u64 offset, u32 len)
4318{
4319 int ret = 0;
4320 struct fs_path *p;
4321
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004322 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004323 if (!p)
4324 return -ENOMEM;
4325
4326 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4327 if (ret < 0)
4328 goto out;
4329
4330 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4331 if (ret < 0)
4332 goto out;
4333
4334 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4335 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4336 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4337
4338 ret = send_cmd(sctx);
4339
4340tlv_put_failure:
4341out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004342 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004343 return ret;
4344}
4345
Josef Bacik16e75492013-10-22 12:18:51 -04004346static int send_hole(struct send_ctx *sctx, u64 end)
4347{
4348 struct fs_path *p = NULL;
4349 u64 offset = sctx->cur_inode_last_extent;
4350 u64 len;
4351 int ret = 0;
4352
4353 p = fs_path_alloc();
4354 if (!p)
4355 return -ENOMEM;
4356 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4357 while (offset < end) {
4358 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4359
4360 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4361 if (ret < 0)
4362 break;
4363 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4364 if (ret < 0)
4365 break;
4366 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4367 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4368 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4369 ret = send_cmd(sctx);
4370 if (ret < 0)
4371 break;
4372 offset += len;
4373 }
4374tlv_put_failure:
4375 fs_path_free(p);
4376 return ret;
4377}
4378
Alexander Block31db9f72012-07-25 23:19:24 +02004379static int send_write_or_clone(struct send_ctx *sctx,
4380 struct btrfs_path *path,
4381 struct btrfs_key *key,
4382 struct clone_root *clone_root)
4383{
4384 int ret = 0;
4385 struct btrfs_file_extent_item *ei;
4386 u64 offset = key->offset;
4387 u64 pos = 0;
4388 u64 len;
4389 u32 l;
4390 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004391 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02004392
4393 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4394 struct btrfs_file_extent_item);
4395 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004396 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004397 len = btrfs_file_extent_inline_len(path->nodes[0],
4398 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004399 /*
4400 * it is possible the inline item won't cover the whole page,
4401 * but there may be items after this page. Make
4402 * sure to send the whole thing
4403 */
4404 len = PAGE_CACHE_ALIGN(len);
4405 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004406 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004407 }
Alexander Block31db9f72012-07-25 23:19:24 +02004408
4409 if (offset + len > sctx->cur_inode_size)
4410 len = sctx->cur_inode_size - offset;
4411 if (len == 0) {
4412 ret = 0;
4413 goto out;
4414 }
4415
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004416 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004417 ret = send_clone(sctx, offset, len, clone_root);
4418 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4419 ret = send_update_extent(sctx, offset, len);
4420 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004421 while (pos < len) {
4422 l = len - pos;
4423 if (l > BTRFS_SEND_READ_SIZE)
4424 l = BTRFS_SEND_READ_SIZE;
4425 ret = send_write(sctx, pos + offset, l);
4426 if (ret < 0)
4427 goto out;
4428 if (!ret)
4429 break;
4430 pos += ret;
4431 }
4432 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004433 }
Alexander Block31db9f72012-07-25 23:19:24 +02004434out:
4435 return ret;
4436}
4437
4438static int is_extent_unchanged(struct send_ctx *sctx,
4439 struct btrfs_path *left_path,
4440 struct btrfs_key *ekey)
4441{
4442 int ret = 0;
4443 struct btrfs_key key;
4444 struct btrfs_path *path = NULL;
4445 struct extent_buffer *eb;
4446 int slot;
4447 struct btrfs_key found_key;
4448 struct btrfs_file_extent_item *ei;
4449 u64 left_disknr;
4450 u64 right_disknr;
4451 u64 left_offset;
4452 u64 right_offset;
4453 u64 left_offset_fixed;
4454 u64 left_len;
4455 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04004456 u64 left_gen;
4457 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004458 u8 left_type;
4459 u8 right_type;
4460
4461 path = alloc_path_for_send();
4462 if (!path)
4463 return -ENOMEM;
4464
4465 eb = left_path->nodes[0];
4466 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02004467 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4468 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004469
4470 if (left_type != BTRFS_FILE_EXTENT_REG) {
4471 ret = 0;
4472 goto out;
4473 }
Chris Mason74dd17f2012-08-07 16:25:13 -04004474 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4475 left_len = btrfs_file_extent_num_bytes(eb, ei);
4476 left_offset = btrfs_file_extent_offset(eb, ei);
4477 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004478
4479 /*
4480 * Following comments will refer to these graphics. L is the left
4481 * extents which we are checking at the moment. 1-8 are the right
4482 * extents that we iterate.
4483 *
4484 * |-----L-----|
4485 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4486 *
4487 * |-----L-----|
4488 * |--1--|-2b-|...(same as above)
4489 *
4490 * Alternative situation. Happens on files where extents got split.
4491 * |-----L-----|
4492 * |-----------7-----------|-6-|
4493 *
4494 * Alternative situation. Happens on files which got larger.
4495 * |-----L-----|
4496 * |-8-|
4497 * Nothing follows after 8.
4498 */
4499
4500 key.objectid = ekey->objectid;
4501 key.type = BTRFS_EXTENT_DATA_KEY;
4502 key.offset = ekey->offset;
4503 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4504 if (ret < 0)
4505 goto out;
4506 if (ret) {
4507 ret = 0;
4508 goto out;
4509 }
4510
4511 /*
4512 * Handle special case where the right side has no extents at all.
4513 */
4514 eb = path->nodes[0];
4515 slot = path->slots[0];
4516 btrfs_item_key_to_cpu(eb, &found_key, slot);
4517 if (found_key.objectid != key.objectid ||
4518 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004519 /* If we're a hole then just pretend nothing changed */
4520 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004521 goto out;
4522 }
4523
4524 /*
4525 * We're now on 2a, 2b or 7.
4526 */
4527 key = found_key;
4528 while (key.offset < ekey->offset + left_len) {
4529 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4530 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004531 if (right_type != BTRFS_FILE_EXTENT_REG) {
4532 ret = 0;
4533 goto out;
4534 }
4535
Josef Bacik007d31f2013-10-31 16:49:02 -04004536 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4537 right_len = btrfs_file_extent_num_bytes(eb, ei);
4538 right_offset = btrfs_file_extent_offset(eb, ei);
4539 right_gen = btrfs_file_extent_generation(eb, ei);
4540
Alexander Block31db9f72012-07-25 23:19:24 +02004541 /*
4542 * Are we at extent 8? If yes, we know the extent is changed.
4543 * This may only happen on the first iteration.
4544 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02004545 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004546 /* If we're a hole just pretend nothing changed */
4547 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004548 goto out;
4549 }
4550
4551 left_offset_fixed = left_offset;
4552 if (key.offset < ekey->offset) {
4553 /* Fix the right offset for 2a and 7. */
4554 right_offset += ekey->offset - key.offset;
4555 } else {
4556 /* Fix the left offset for all behind 2a and 2b */
4557 left_offset_fixed += key.offset - ekey->offset;
4558 }
4559
4560 /*
4561 * Check if we have the same extent.
4562 */
Alexander Block39540962012-08-01 12:46:05 +02004563 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04004564 left_offset_fixed != right_offset ||
4565 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02004566 ret = 0;
4567 goto out;
4568 }
4569
4570 /*
4571 * Go to the next extent.
4572 */
4573 ret = btrfs_next_item(sctx->parent_root, path);
4574 if (ret < 0)
4575 goto out;
4576 if (!ret) {
4577 eb = path->nodes[0];
4578 slot = path->slots[0];
4579 btrfs_item_key_to_cpu(eb, &found_key, slot);
4580 }
4581 if (ret || found_key.objectid != key.objectid ||
4582 found_key.type != key.type) {
4583 key.offset += right_len;
4584 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00004585 }
4586 if (found_key.offset != key.offset + right_len) {
4587 ret = 0;
4588 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004589 }
4590 key = found_key;
4591 }
4592
4593 /*
4594 * We're now behind the left extent (treat as unchanged) or at the end
4595 * of the right side (treat as changed).
4596 */
4597 if (key.offset >= ekey->offset + left_len)
4598 ret = 1;
4599 else
4600 ret = 0;
4601
4602
4603out:
4604 btrfs_free_path(path);
4605 return ret;
4606}
4607
Josef Bacik16e75492013-10-22 12:18:51 -04004608static int get_last_extent(struct send_ctx *sctx, u64 offset)
4609{
4610 struct btrfs_path *path;
4611 struct btrfs_root *root = sctx->send_root;
4612 struct btrfs_file_extent_item *fi;
4613 struct btrfs_key key;
4614 u64 extent_end;
4615 u8 type;
4616 int ret;
4617
4618 path = alloc_path_for_send();
4619 if (!path)
4620 return -ENOMEM;
4621
4622 sctx->cur_inode_last_extent = 0;
4623
4624 key.objectid = sctx->cur_ino;
4625 key.type = BTRFS_EXTENT_DATA_KEY;
4626 key.offset = offset;
4627 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4628 if (ret < 0)
4629 goto out;
4630 ret = 0;
4631 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4632 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4633 goto out;
4634
4635 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4636 struct btrfs_file_extent_item);
4637 type = btrfs_file_extent_type(path->nodes[0], fi);
4638 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004639 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4640 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004641 extent_end = ALIGN(key.offset + size,
4642 sctx->send_root->sectorsize);
4643 } else {
4644 extent_end = key.offset +
4645 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4646 }
4647 sctx->cur_inode_last_extent = extent_end;
4648out:
4649 btrfs_free_path(path);
4650 return ret;
4651}
4652
4653static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4654 struct btrfs_key *key)
4655{
4656 struct btrfs_file_extent_item *fi;
4657 u64 extent_end;
4658 u8 type;
4659 int ret = 0;
4660
4661 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4662 return 0;
4663
4664 if (sctx->cur_inode_last_extent == (u64)-1) {
4665 ret = get_last_extent(sctx, key->offset - 1);
4666 if (ret)
4667 return ret;
4668 }
4669
4670 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4671 struct btrfs_file_extent_item);
4672 type = btrfs_file_extent_type(path->nodes[0], fi);
4673 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004674 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4675 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004676 extent_end = ALIGN(key->offset + size,
4677 sctx->send_root->sectorsize);
4678 } else {
4679 extent_end = key->offset +
4680 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4681 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00004682
4683 if (path->slots[0] == 0 &&
4684 sctx->cur_inode_last_extent < key->offset) {
4685 /*
4686 * We might have skipped entire leafs that contained only
4687 * file extent items for our current inode. These leafs have
4688 * a generation number smaller (older) than the one in the
4689 * current leaf and the leaf our last extent came from, and
4690 * are located between these 2 leafs.
4691 */
4692 ret = get_last_extent(sctx, key->offset - 1);
4693 if (ret)
4694 return ret;
4695 }
4696
Josef Bacik16e75492013-10-22 12:18:51 -04004697 if (sctx->cur_inode_last_extent < key->offset)
4698 ret = send_hole(sctx, key->offset);
4699 sctx->cur_inode_last_extent = extent_end;
4700 return ret;
4701}
4702
Alexander Block31db9f72012-07-25 23:19:24 +02004703static int process_extent(struct send_ctx *sctx,
4704 struct btrfs_path *path,
4705 struct btrfs_key *key)
4706{
Alexander Block31db9f72012-07-25 23:19:24 +02004707 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004708 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004709
4710 if (S_ISLNK(sctx->cur_inode_mode))
4711 return 0;
4712
4713 if (sctx->parent_root && !sctx->cur_inode_new) {
4714 ret = is_extent_unchanged(sctx, path, key);
4715 if (ret < 0)
4716 goto out;
4717 if (ret) {
4718 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004719 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004720 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004721 } else {
4722 struct btrfs_file_extent_item *ei;
4723 u8 type;
4724
4725 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4726 struct btrfs_file_extent_item);
4727 type = btrfs_file_extent_type(path->nodes[0], ei);
4728 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4729 type == BTRFS_FILE_EXTENT_REG) {
4730 /*
4731 * The send spec does not have a prealloc command yet,
4732 * so just leave a hole for prealloc'ed extents until
4733 * we have enough commands queued up to justify rev'ing
4734 * the send spec.
4735 */
4736 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4737 ret = 0;
4738 goto out;
4739 }
4740
4741 /* Have a hole, just skip it. */
4742 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4743 ret = 0;
4744 goto out;
4745 }
4746 }
Alexander Block31db9f72012-07-25 23:19:24 +02004747 }
4748
4749 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4750 sctx->cur_inode_size, &found_clone);
4751 if (ret != -ENOENT && ret < 0)
4752 goto out;
4753
4754 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004755 if (ret)
4756 goto out;
4757out_hole:
4758 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004759out:
4760 return ret;
4761}
4762
4763static int process_all_extents(struct send_ctx *sctx)
4764{
4765 int ret;
4766 struct btrfs_root *root;
4767 struct btrfs_path *path;
4768 struct btrfs_key key;
4769 struct btrfs_key found_key;
4770 struct extent_buffer *eb;
4771 int slot;
4772
4773 root = sctx->send_root;
4774 path = alloc_path_for_send();
4775 if (!path)
4776 return -ENOMEM;
4777
4778 key.objectid = sctx->cmp_key->objectid;
4779 key.type = BTRFS_EXTENT_DATA_KEY;
4780 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004781 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4782 if (ret < 0)
4783 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004784
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004785 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004786 eb = path->nodes[0];
4787 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004788
4789 if (slot >= btrfs_header_nritems(eb)) {
4790 ret = btrfs_next_leaf(root, path);
4791 if (ret < 0) {
4792 goto out;
4793 } else if (ret > 0) {
4794 ret = 0;
4795 break;
4796 }
4797 continue;
4798 }
4799
Alexander Block31db9f72012-07-25 23:19:24 +02004800 btrfs_item_key_to_cpu(eb, &found_key, slot);
4801
4802 if (found_key.objectid != key.objectid ||
4803 found_key.type != key.type) {
4804 ret = 0;
4805 goto out;
4806 }
4807
4808 ret = process_extent(sctx, path, &found_key);
4809 if (ret < 0)
4810 goto out;
4811
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004812 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004813 }
4814
4815out:
4816 btrfs_free_path(path);
4817 return ret;
4818}
4819
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004820static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4821 int *pending_move,
4822 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02004823{
4824 int ret = 0;
4825
4826 if (sctx->cur_ino == 0)
4827 goto out;
4828 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004829 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004830 goto out;
4831 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4832 goto out;
4833
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004834 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004835 if (ret < 0)
4836 goto out;
4837
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004838 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004839out:
4840 return ret;
4841}
4842
4843static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4844{
4845 int ret = 0;
4846 u64 left_mode;
4847 u64 left_uid;
4848 u64 left_gid;
4849 u64 right_mode;
4850 u64 right_uid;
4851 u64 right_gid;
4852 int need_chmod = 0;
4853 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004854 int pending_move = 0;
4855 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004856
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004857 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4858 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02004859 if (ret < 0)
4860 goto out;
4861
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004862 /*
4863 * We have processed the refs and thus need to advance send_progress.
4864 * Now, calls to get_cur_xxx will take the updated refs of the current
4865 * inode into account.
4866 *
4867 * On the other hand, if our current inode is a directory and couldn't
4868 * be moved/renamed because its parent was renamed/moved too and it has
4869 * a higher inode number, we can only move/rename our current inode
4870 * after we moved/renamed its parent. Therefore in this case operate on
4871 * the old path (pre move/rename) of our current inode, and the
4872 * move/rename will be performed later.
4873 */
4874 if (refs_processed && !pending_move)
4875 sctx->send_progress = sctx->cur_ino + 1;
4876
Alexander Block31db9f72012-07-25 23:19:24 +02004877 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4878 goto out;
4879 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4880 goto out;
4881
4882 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004883 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004884 if (ret < 0)
4885 goto out;
4886
Alex Lyakase2d044f2012-10-17 13:52:47 +00004887 if (!sctx->parent_root || sctx->cur_inode_new) {
4888 need_chown = 1;
4889 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004890 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004891 } else {
4892 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4893 NULL, NULL, &right_mode, &right_uid,
4894 &right_gid, NULL);
4895 if (ret < 0)
4896 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004897
Alex Lyakase2d044f2012-10-17 13:52:47 +00004898 if (left_uid != right_uid || left_gid != right_gid)
4899 need_chown = 1;
4900 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4901 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004902 }
4903
4904 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04004905 if (need_send_hole(sctx)) {
4906 if (sctx->cur_inode_last_extent == (u64)-1) {
4907 ret = get_last_extent(sctx, (u64)-1);
4908 if (ret)
4909 goto out;
4910 }
4911 if (sctx->cur_inode_last_extent <
4912 sctx->cur_inode_size) {
4913 ret = send_hole(sctx, sctx->cur_inode_size);
4914 if (ret)
4915 goto out;
4916 }
4917 }
Alexander Block31db9f72012-07-25 23:19:24 +02004918 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4919 sctx->cur_inode_size);
4920 if (ret < 0)
4921 goto out;
4922 }
4923
4924 if (need_chown) {
4925 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4926 left_uid, left_gid);
4927 if (ret < 0)
4928 goto out;
4929 }
4930 if (need_chmod) {
4931 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4932 left_mode);
4933 if (ret < 0)
4934 goto out;
4935 }
4936
4937 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004938 * If other directory inodes depended on our current directory
4939 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02004940 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004941 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
4942 ret = apply_children_dir_moves(sctx);
4943 if (ret)
4944 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00004945 /*
4946 * Need to send that every time, no matter if it actually
4947 * changed between the two trees as we have done changes to
4948 * the inode before. If our inode is a directory and it's
4949 * waiting to be moved/renamed, we will send its utimes when
4950 * it's moved/renamed, therefore we don't need to do it here.
4951 */
4952 sctx->send_progress = sctx->cur_ino + 1;
4953 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4954 if (ret < 0)
4955 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004956 }
4957
Alexander Block31db9f72012-07-25 23:19:24 +02004958out:
4959 return ret;
4960}
4961
4962static int changed_inode(struct send_ctx *sctx,
4963 enum btrfs_compare_tree_result result)
4964{
4965 int ret = 0;
4966 struct btrfs_key *key = sctx->cmp_key;
4967 struct btrfs_inode_item *left_ii = NULL;
4968 struct btrfs_inode_item *right_ii = NULL;
4969 u64 left_gen = 0;
4970 u64 right_gen = 0;
4971
Alexander Block31db9f72012-07-25 23:19:24 +02004972 sctx->cur_ino = key->objectid;
4973 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004974 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004975
4976 /*
4977 * Set send_progress to current inode. This will tell all get_cur_xxx
4978 * functions that the current inode's refs are not updated yet. Later,
4979 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4980 */
Alexander Block31db9f72012-07-25 23:19:24 +02004981 sctx->send_progress = sctx->cur_ino;
4982
4983 if (result == BTRFS_COMPARE_TREE_NEW ||
4984 result == BTRFS_COMPARE_TREE_CHANGED) {
4985 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4986 sctx->left_path->slots[0],
4987 struct btrfs_inode_item);
4988 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4989 left_ii);
4990 } else {
4991 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4992 sctx->right_path->slots[0],
4993 struct btrfs_inode_item);
4994 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4995 right_ii);
4996 }
4997 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4998 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4999 sctx->right_path->slots[0],
5000 struct btrfs_inode_item);
5001
5002 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5003 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005004
5005 /*
5006 * The cur_ino = root dir case is special here. We can't treat
5007 * the inode as deleted+reused because it would generate a
5008 * stream that tries to delete/mkdir the root dir.
5009 */
5010 if (left_gen != right_gen &&
5011 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005012 sctx->cur_inode_new_gen = 1;
5013 }
5014
5015 if (result == BTRFS_COMPARE_TREE_NEW) {
5016 sctx->cur_inode_gen = left_gen;
5017 sctx->cur_inode_new = 1;
5018 sctx->cur_inode_deleted = 0;
5019 sctx->cur_inode_size = btrfs_inode_size(
5020 sctx->left_path->nodes[0], left_ii);
5021 sctx->cur_inode_mode = btrfs_inode_mode(
5022 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005023 sctx->cur_inode_rdev = btrfs_inode_rdev(
5024 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005025 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005026 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005027 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5028 sctx->cur_inode_gen = right_gen;
5029 sctx->cur_inode_new = 0;
5030 sctx->cur_inode_deleted = 1;
5031 sctx->cur_inode_size = btrfs_inode_size(
5032 sctx->right_path->nodes[0], right_ii);
5033 sctx->cur_inode_mode = btrfs_inode_mode(
5034 sctx->right_path->nodes[0], right_ii);
5035 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005036 /*
5037 * We need to do some special handling in case the inode was
5038 * reported as changed with a changed generation number. This
5039 * means that the original inode was deleted and new inode
5040 * reused the same inum. So we have to treat the old inode as
5041 * deleted and the new one as new.
5042 */
Alexander Block31db9f72012-07-25 23:19:24 +02005043 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005044 /*
5045 * First, process the inode as if it was deleted.
5046 */
Alexander Block31db9f72012-07-25 23:19:24 +02005047 sctx->cur_inode_gen = right_gen;
5048 sctx->cur_inode_new = 0;
5049 sctx->cur_inode_deleted = 1;
5050 sctx->cur_inode_size = btrfs_inode_size(
5051 sctx->right_path->nodes[0], right_ii);
5052 sctx->cur_inode_mode = btrfs_inode_mode(
5053 sctx->right_path->nodes[0], right_ii);
5054 ret = process_all_refs(sctx,
5055 BTRFS_COMPARE_TREE_DELETED);
5056 if (ret < 0)
5057 goto out;
5058
Alexander Block766702e2012-07-28 14:11:31 +02005059 /*
5060 * Now process the inode as if it was new.
5061 */
Alexander Block31db9f72012-07-25 23:19:24 +02005062 sctx->cur_inode_gen = left_gen;
5063 sctx->cur_inode_new = 1;
5064 sctx->cur_inode_deleted = 0;
5065 sctx->cur_inode_size = btrfs_inode_size(
5066 sctx->left_path->nodes[0], left_ii);
5067 sctx->cur_inode_mode = btrfs_inode_mode(
5068 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005069 sctx->cur_inode_rdev = btrfs_inode_rdev(
5070 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005071 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005072 if (ret < 0)
5073 goto out;
5074
5075 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5076 if (ret < 0)
5077 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005078 /*
5079 * Advance send_progress now as we did not get into
5080 * process_recorded_refs_if_needed in the new_gen case.
5081 */
5082 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005083
5084 /*
5085 * Now process all extents and xattrs of the inode as if
5086 * they were all new.
5087 */
Alexander Block31db9f72012-07-25 23:19:24 +02005088 ret = process_all_extents(sctx);
5089 if (ret < 0)
5090 goto out;
5091 ret = process_all_new_xattrs(sctx);
5092 if (ret < 0)
5093 goto out;
5094 } else {
5095 sctx->cur_inode_gen = left_gen;
5096 sctx->cur_inode_new = 0;
5097 sctx->cur_inode_new_gen = 0;
5098 sctx->cur_inode_deleted = 0;
5099 sctx->cur_inode_size = btrfs_inode_size(
5100 sctx->left_path->nodes[0], left_ii);
5101 sctx->cur_inode_mode = btrfs_inode_mode(
5102 sctx->left_path->nodes[0], left_ii);
5103 }
5104 }
5105
5106out:
5107 return ret;
5108}
5109
Alexander Block766702e2012-07-28 14:11:31 +02005110/*
5111 * We have to process new refs before deleted refs, but compare_trees gives us
5112 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5113 * first and later process them in process_recorded_refs.
5114 * For the cur_inode_new_gen case, we skip recording completely because
5115 * changed_inode did already initiate processing of refs. The reason for this is
5116 * that in this case, compare_tree actually compares the refs of 2 different
5117 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5118 * refs of the right tree as deleted and all refs of the left tree as new.
5119 */
Alexander Block31db9f72012-07-25 23:19:24 +02005120static int changed_ref(struct send_ctx *sctx,
5121 enum btrfs_compare_tree_result result)
5122{
5123 int ret = 0;
5124
5125 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5126
5127 if (!sctx->cur_inode_new_gen &&
5128 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5129 if (result == BTRFS_COMPARE_TREE_NEW)
5130 ret = record_new_ref(sctx);
5131 else if (result == BTRFS_COMPARE_TREE_DELETED)
5132 ret = record_deleted_ref(sctx);
5133 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5134 ret = record_changed_ref(sctx);
5135 }
5136
5137 return ret;
5138}
5139
Alexander Block766702e2012-07-28 14:11:31 +02005140/*
5141 * Process new/deleted/changed xattrs. We skip processing in the
5142 * cur_inode_new_gen case because changed_inode did already initiate processing
5143 * of xattrs. The reason is the same as in changed_ref
5144 */
Alexander Block31db9f72012-07-25 23:19:24 +02005145static int changed_xattr(struct send_ctx *sctx,
5146 enum btrfs_compare_tree_result result)
5147{
5148 int ret = 0;
5149
5150 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5151
5152 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5153 if (result == BTRFS_COMPARE_TREE_NEW)
5154 ret = process_new_xattr(sctx);
5155 else if (result == BTRFS_COMPARE_TREE_DELETED)
5156 ret = process_deleted_xattr(sctx);
5157 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5158 ret = process_changed_xattr(sctx);
5159 }
5160
5161 return ret;
5162}
5163
Alexander Block766702e2012-07-28 14:11:31 +02005164/*
5165 * Process new/deleted/changed extents. We skip processing in the
5166 * cur_inode_new_gen case because changed_inode did already initiate processing
5167 * of extents. The reason is the same as in changed_ref
5168 */
Alexander Block31db9f72012-07-25 23:19:24 +02005169static int changed_extent(struct send_ctx *sctx,
5170 enum btrfs_compare_tree_result result)
5171{
5172 int ret = 0;
5173
5174 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5175
5176 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5177 if (result != BTRFS_COMPARE_TREE_DELETED)
5178 ret = process_extent(sctx, sctx->left_path,
5179 sctx->cmp_key);
5180 }
5181
5182 return ret;
5183}
5184
Josef Bacikba5e8f22013-08-16 16:52:55 -04005185static int dir_changed(struct send_ctx *sctx, u64 dir)
5186{
5187 u64 orig_gen, new_gen;
5188 int ret;
5189
5190 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5191 NULL, NULL);
5192 if (ret)
5193 return ret;
5194
5195 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5196 NULL, NULL, NULL);
5197 if (ret)
5198 return ret;
5199
5200 return (orig_gen != new_gen) ? 1 : 0;
5201}
5202
5203static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5204 struct btrfs_key *key)
5205{
5206 struct btrfs_inode_extref *extref;
5207 struct extent_buffer *leaf;
5208 u64 dirid = 0, last_dirid = 0;
5209 unsigned long ptr;
5210 u32 item_size;
5211 u32 cur_offset = 0;
5212 int ref_name_len;
5213 int ret = 0;
5214
5215 /* Easy case, just check this one dirid */
5216 if (key->type == BTRFS_INODE_REF_KEY) {
5217 dirid = key->offset;
5218
5219 ret = dir_changed(sctx, dirid);
5220 goto out;
5221 }
5222
5223 leaf = path->nodes[0];
5224 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5225 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5226 while (cur_offset < item_size) {
5227 extref = (struct btrfs_inode_extref *)(ptr +
5228 cur_offset);
5229 dirid = btrfs_inode_extref_parent(leaf, extref);
5230 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5231 cur_offset += ref_name_len + sizeof(*extref);
5232 if (dirid == last_dirid)
5233 continue;
5234 ret = dir_changed(sctx, dirid);
5235 if (ret)
5236 break;
5237 last_dirid = dirid;
5238 }
5239out:
5240 return ret;
5241}
5242
Alexander Block766702e2012-07-28 14:11:31 +02005243/*
5244 * Updates compare related fields in sctx and simply forwards to the actual
5245 * changed_xxx functions.
5246 */
Alexander Block31db9f72012-07-25 23:19:24 +02005247static int changed_cb(struct btrfs_root *left_root,
5248 struct btrfs_root *right_root,
5249 struct btrfs_path *left_path,
5250 struct btrfs_path *right_path,
5251 struct btrfs_key *key,
5252 enum btrfs_compare_tree_result result,
5253 void *ctx)
5254{
5255 int ret = 0;
5256 struct send_ctx *sctx = ctx;
5257
Josef Bacikba5e8f22013-08-16 16:52:55 -04005258 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005259 if (key->type == BTRFS_INODE_REF_KEY ||
5260 key->type == BTRFS_INODE_EXTREF_KEY) {
5261 ret = compare_refs(sctx, left_path, key);
5262 if (!ret)
5263 return 0;
5264 if (ret < 0)
5265 return ret;
5266 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5267 return maybe_send_hole(sctx, left_path, key);
5268 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005269 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005270 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005271 result = BTRFS_COMPARE_TREE_CHANGED;
5272 ret = 0;
5273 }
5274
Alexander Block31db9f72012-07-25 23:19:24 +02005275 sctx->left_path = left_path;
5276 sctx->right_path = right_path;
5277 sctx->cmp_key = key;
5278
5279 ret = finish_inode_if_needed(sctx, 0);
5280 if (ret < 0)
5281 goto out;
5282
Alexander Block2981e222012-08-01 14:47:03 +02005283 /* Ignore non-FS objects */
5284 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5285 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5286 goto out;
5287
Alexander Block31db9f72012-07-25 23:19:24 +02005288 if (key->type == BTRFS_INODE_ITEM_KEY)
5289 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005290 else if (key->type == BTRFS_INODE_REF_KEY ||
5291 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005292 ret = changed_ref(sctx, result);
5293 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5294 ret = changed_xattr(sctx, result);
5295 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5296 ret = changed_extent(sctx, result);
5297
5298out:
5299 return ret;
5300}
5301
5302static int full_send_tree(struct send_ctx *sctx)
5303{
5304 int ret;
Wang Shilongdcfd5ad2014-02-08 23:46:36 +08005305 struct btrfs_trans_handle *trans = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02005306 struct btrfs_root *send_root = sctx->send_root;
5307 struct btrfs_key key;
5308 struct btrfs_key found_key;
5309 struct btrfs_path *path;
5310 struct extent_buffer *eb;
5311 int slot;
5312 u64 start_ctransid;
5313 u64 ctransid;
5314
5315 path = alloc_path_for_send();
5316 if (!path)
5317 return -ENOMEM;
5318
Anand Jain5f3ab902012-12-07 09:28:54 +00005319 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005320 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005321 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005322
5323 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5324 key.type = BTRFS_INODE_ITEM_KEY;
5325 key.offset = 0;
5326
Wang Shilongdcfd5ad2014-02-08 23:46:36 +08005327join_trans:
5328 /*
5329 * We need to make sure the transaction does not get committed
5330 * while we do anything on commit roots. Join a transaction to prevent
5331 * this.
5332 */
5333 trans = btrfs_join_transaction(send_root);
5334 if (IS_ERR(trans)) {
5335 ret = PTR_ERR(trans);
5336 trans = NULL;
5337 goto out;
5338 }
5339
Alexander Block31db9f72012-07-25 23:19:24 +02005340 /*
Alexander Block766702e2012-07-28 14:11:31 +02005341 * Make sure the tree has not changed after re-joining. We detect this
5342 * by comparing start_ctransid and ctransid. They should always match.
Alexander Block31db9f72012-07-25 23:19:24 +02005343 */
Anand Jain5f3ab902012-12-07 09:28:54 +00005344 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005345 ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005346 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005347
5348 if (ctransid != start_ctransid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005349 WARN(1, KERN_WARNING "BTRFS: the root that you're trying to "
Alexander Block31db9f72012-07-25 23:19:24 +02005350 "send was modified in between. This is "
5351 "probably a bug.\n");
5352 ret = -EIO;
5353 goto out;
5354 }
5355
5356 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5357 if (ret < 0)
5358 goto out;
5359 if (ret)
5360 goto out_finish;
5361
5362 while (1) {
Wang Shilongdcfd5ad2014-02-08 23:46:36 +08005363 /*
5364 * When someone want to commit while we iterate, end the
5365 * joined transaction and rejoin.
5366 */
5367 if (btrfs_should_end_transaction(trans, send_root)) {
5368 ret = btrfs_end_transaction(trans, send_root);
5369 trans = NULL;
5370 if (ret < 0)
5371 goto out;
5372 btrfs_release_path(path);
5373 goto join_trans;
5374 }
5375
Alexander Block31db9f72012-07-25 23:19:24 +02005376 eb = path->nodes[0];
5377 slot = path->slots[0];
5378 btrfs_item_key_to_cpu(eb, &found_key, slot);
5379
5380 ret = changed_cb(send_root, NULL, path, NULL,
5381 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5382 if (ret < 0)
5383 goto out;
5384
5385 key.objectid = found_key.objectid;
5386 key.type = found_key.type;
5387 key.offset = found_key.offset + 1;
5388
5389 ret = btrfs_next_item(send_root, path);
5390 if (ret < 0)
5391 goto out;
5392 if (ret) {
5393 ret = 0;
5394 break;
5395 }
5396 }
5397
5398out_finish:
5399 ret = finish_inode_if_needed(sctx, 1);
5400
5401out:
5402 btrfs_free_path(path);
Wang Shilongdcfd5ad2014-02-08 23:46:36 +08005403 if (trans) {
5404 if (!ret)
5405 ret = btrfs_end_transaction(trans, send_root);
5406 else
5407 btrfs_end_transaction(trans, send_root);
5408 }
Alexander Block31db9f72012-07-25 23:19:24 +02005409 return ret;
5410}
5411
5412static int send_subvol(struct send_ctx *sctx)
5413{
5414 int ret;
5415
Stefan Behrensc2c71322013-04-10 17:10:52 +00005416 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5417 ret = send_header(sctx);
5418 if (ret < 0)
5419 goto out;
5420 }
Alexander Block31db9f72012-07-25 23:19:24 +02005421
5422 ret = send_subvol_begin(sctx);
5423 if (ret < 0)
5424 goto out;
5425
5426 if (sctx->parent_root) {
5427 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5428 changed_cb, sctx);
5429 if (ret < 0)
5430 goto out;
5431 ret = finish_inode_if_needed(sctx, 1);
5432 if (ret < 0)
5433 goto out;
5434 } else {
5435 ret = full_send_tree(sctx);
5436 if (ret < 0)
5437 goto out;
5438 }
5439
5440out:
Alexander Block31db9f72012-07-25 23:19:24 +02005441 free_recorded_refs(sctx);
5442 return ret;
5443}
5444
David Sterba66ef7d62013-12-17 15:07:20 +01005445static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5446{
5447 spin_lock(&root->root_item_lock);
5448 root->send_in_progress--;
5449 /*
5450 * Not much left to do, we don't know why it's unbalanced and
5451 * can't blindly reset it to 0.
5452 */
5453 if (root->send_in_progress < 0)
5454 btrfs_err(root->fs_info,
5455 "send_in_progres unbalanced %d root %llu\n",
5456 root->send_in_progress, root->root_key.objectid);
5457 spin_unlock(&root->root_item_lock);
5458}
5459
Alexander Block31db9f72012-07-25 23:19:24 +02005460long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5461{
5462 int ret = 0;
5463 struct btrfs_root *send_root;
5464 struct btrfs_root *clone_root;
5465 struct btrfs_fs_info *fs_info;
5466 struct btrfs_ioctl_send_args *arg = NULL;
5467 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02005468 struct send_ctx *sctx = NULL;
5469 u32 i;
5470 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01005471 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08005472 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08005473 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02005474
5475 if (!capable(CAP_SYS_ADMIN))
5476 return -EPERM;
5477
Al Viro496ad9a2013-01-23 17:07:38 -05005478 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02005479 fs_info = send_root->fs_info;
5480
Josef Bacik139f8072013-05-20 11:26:50 -04005481 /*
David Sterba2c686532013-12-16 17:34:17 +01005482 * The subvolume must remain read-only during send, protect against
5483 * making it RW.
5484 */
5485 spin_lock(&send_root->root_item_lock);
5486 send_root->send_in_progress++;
5487 spin_unlock(&send_root->root_item_lock);
5488
5489 /*
Josef Bacik139f8072013-05-20 11:26:50 -04005490 * This is done when we lookup the root, it should already be complete
5491 * by the time we get here.
5492 */
5493 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5494
5495 /*
David Sterba2c686532013-12-16 17:34:17 +01005496 * Userspace tools do the checks and warn the user if it's
5497 * not RO.
5498 */
5499 if (!btrfs_root_readonly(send_root)) {
5500 ret = -EPERM;
5501 goto out;
5502 }
5503
Alexander Block31db9f72012-07-25 23:19:24 +02005504 arg = memdup_user(arg_, sizeof(*arg));
5505 if (IS_ERR(arg)) {
5506 ret = PTR_ERR(arg);
5507 arg = NULL;
5508 goto out;
5509 }
5510
5511 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05005512 sizeof(*arg->clone_sources) *
5513 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02005514 ret = -EFAULT;
5515 goto out;
5516 }
5517
Stefan Behrensc2c71322013-04-10 17:10:52 +00005518 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005519 ret = -EINVAL;
5520 goto out;
5521 }
5522
Alexander Block31db9f72012-07-25 23:19:24 +02005523 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5524 if (!sctx) {
5525 ret = -ENOMEM;
5526 goto out;
5527 }
5528
5529 INIT_LIST_HEAD(&sctx->new_refs);
5530 INIT_LIST_HEAD(&sctx->deleted_refs);
5531 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5532 INIT_LIST_HEAD(&sctx->name_cache_list);
5533
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005534 sctx->flags = arg->flags;
5535
Alexander Block31db9f72012-07-25 23:19:24 +02005536 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00005537 if (!sctx->send_filp) {
5538 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02005539 goto out;
5540 }
5541
Alexander Block31db9f72012-07-25 23:19:24 +02005542 sctx->send_root = send_root;
5543 sctx->clone_roots_cnt = arg->clone_sources_count;
5544
5545 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5546 sctx->send_buf = vmalloc(sctx->send_max_size);
5547 if (!sctx->send_buf) {
5548 ret = -ENOMEM;
5549 goto out;
5550 }
5551
5552 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5553 if (!sctx->read_buf) {
5554 ret = -ENOMEM;
5555 goto out;
5556 }
5557
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005558 sctx->pending_dir_moves = RB_ROOT;
5559 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00005560 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005561
Alexander Block31db9f72012-07-25 23:19:24 +02005562 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5563 (arg->clone_sources_count + 1));
5564 if (!sctx->clone_roots) {
5565 ret = -ENOMEM;
5566 goto out;
5567 }
5568
5569 if (arg->clone_sources_count) {
5570 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5571 sizeof(*arg->clone_sources));
5572 if (!clone_sources_tmp) {
5573 ret = -ENOMEM;
5574 goto out;
5575 }
5576
5577 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5578 arg->clone_sources_count *
5579 sizeof(*arg->clone_sources));
5580 if (ret) {
5581 ret = -EFAULT;
5582 goto out;
5583 }
5584
5585 for (i = 0; i < arg->clone_sources_count; i++) {
5586 key.objectid = clone_sources_tmp[i];
5587 key.type = BTRFS_ROOT_ITEM_KEY;
5588 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005589
5590 index = srcu_read_lock(&fs_info->subvol_srcu);
5591
Alexander Block31db9f72012-07-25 23:19:24 +02005592 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02005593 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005594 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005595 ret = PTR_ERR(clone_root);
5596 goto out;
5597 }
David Sterba2c686532013-12-16 17:34:17 +01005598 clone_sources_to_rollback = i + 1;
5599 spin_lock(&clone_root->root_item_lock);
5600 clone_root->send_in_progress++;
5601 if (!btrfs_root_readonly(clone_root)) {
5602 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005603 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005604 ret = -EPERM;
5605 goto out;
5606 }
5607 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005608 srcu_read_unlock(&fs_info->subvol_srcu, index);
5609
Alexander Block31db9f72012-07-25 23:19:24 +02005610 sctx->clone_roots[i].root = clone_root;
5611 }
5612 vfree(clone_sources_tmp);
5613 clone_sources_tmp = NULL;
5614 }
5615
5616 if (arg->parent_root) {
5617 key.objectid = arg->parent_root;
5618 key.type = BTRFS_ROOT_ITEM_KEY;
5619 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005620
5621 index = srcu_read_lock(&fs_info->subvol_srcu);
5622
Alexander Block31db9f72012-07-25 23:19:24 +02005623 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005624 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005625 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005626 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02005627 goto out;
5628 }
Wang Shilong18f687d2014-01-07 17:25:19 +08005629
David Sterba2c686532013-12-16 17:34:17 +01005630 spin_lock(&sctx->parent_root->root_item_lock);
5631 sctx->parent_root->send_in_progress++;
5632 if (!btrfs_root_readonly(sctx->parent_root)) {
5633 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005634 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005635 ret = -EPERM;
5636 goto out;
5637 }
5638 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005639
5640 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005641 }
5642
5643 /*
5644 * Clones from send_root are allowed, but only if the clone source
5645 * is behind the current send position. This is checked while searching
5646 * for possible clone sources.
5647 */
5648 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5649
5650 /* We do a bsearch later */
5651 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5652 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5653 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08005654 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005655
5656 ret = send_subvol(sctx);
5657 if (ret < 0)
5658 goto out;
5659
Stefan Behrensc2c71322013-04-10 17:10:52 +00005660 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5661 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5662 if (ret < 0)
5663 goto out;
5664 ret = send_cmd(sctx);
5665 if (ret < 0)
5666 goto out;
5667 }
Alexander Block31db9f72012-07-25 23:19:24 +02005668
5669out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005670 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5671 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5672 struct rb_node *n;
5673 struct pending_dir_move *pm;
5674
5675 n = rb_first(&sctx->pending_dir_moves);
5676 pm = rb_entry(n, struct pending_dir_move, node);
5677 while (!list_empty(&pm->list)) {
5678 struct pending_dir_move *pm2;
5679
5680 pm2 = list_first_entry(&pm->list,
5681 struct pending_dir_move, list);
5682 free_pending_move(sctx, pm2);
5683 }
5684 free_pending_move(sctx, pm);
5685 }
5686
5687 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5688 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5689 struct rb_node *n;
5690 struct waiting_dir_move *dm;
5691
5692 n = rb_first(&sctx->waiting_dir_moves);
5693 dm = rb_entry(n, struct waiting_dir_move, node);
5694 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5695 kfree(dm);
5696 }
5697
Filipe Manana9dc44212014-02-19 14:31:44 +00005698 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5699 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5700 struct rb_node *n;
5701 struct orphan_dir_info *odi;
5702
5703 n = rb_first(&sctx->orphan_dirs);
5704 odi = rb_entry(n, struct orphan_dir_info, node);
5705 free_orphan_dir_info(sctx, odi);
5706 }
5707
Wang Shilong896c14f2014-01-07 17:25:18 +08005708 if (sort_clone_roots) {
5709 for (i = 0; i < sctx->clone_roots_cnt; i++)
5710 btrfs_root_dec_send_in_progress(
5711 sctx->clone_roots[i].root);
5712 } else {
5713 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5714 btrfs_root_dec_send_in_progress(
5715 sctx->clone_roots[i].root);
5716
5717 btrfs_root_dec_send_in_progress(send_root);
5718 }
David Sterba66ef7d62013-12-17 15:07:20 +01005719 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5720 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01005721
Alexander Block31db9f72012-07-25 23:19:24 +02005722 kfree(arg);
5723 vfree(clone_sources_tmp);
5724
5725 if (sctx) {
5726 if (sctx->send_filp)
5727 fput(sctx->send_filp);
5728
5729 vfree(sctx->clone_roots);
5730 vfree(sctx->send_buf);
5731 vfree(sctx->read_buf);
5732
5733 name_cache_free(sctx);
5734
5735 kfree(sctx);
5736 }
5737
5738 return ret;
5739}