blob: 298e25de13ab0aa4a2b41e832bb9b5755149fb85 [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;
Josef Bacik16e75492013-10-22 12:18:51 -0400115 u64 cur_inode_last_extent;
Alexander Block31db9f72012-07-25 23:19:24 +0200116
117 u64 send_progress;
118
119 struct list_head new_refs;
120 struct list_head deleted_refs;
121
122 struct radix_tree_root name_cache;
123 struct list_head name_cache_list;
124 int name_cache_size;
125
Alexander Block31db9f72012-07-25 23:19:24 +0200126 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000127
128 /*
129 * We process inodes by their increasing order, so if before an
130 * incremental send we reverse the parent/child relationship of
131 * directories such that a directory with a lower inode number was
132 * the parent of a directory with a higher inode number, and the one
133 * becoming the new parent got renamed too, we can't rename/move the
134 * directory with lower inode number when we finish processing it - we
135 * must process the directory with higher inode number first, then
136 * rename/move it and then rename/move the directory with lower inode
137 * number. Example follows.
138 *
139 * Tree state when the first send was performed:
140 *
141 * .
142 * |-- a (ino 257)
143 * |-- b (ino 258)
144 * |
145 * |
146 * |-- c (ino 259)
147 * | |-- d (ino 260)
148 * |
149 * |-- c2 (ino 261)
150 *
151 * Tree state when the second (incremental) send is performed:
152 *
153 * .
154 * |-- a (ino 257)
155 * |-- b (ino 258)
156 * |-- c2 (ino 261)
157 * |-- d2 (ino 260)
158 * |-- cc (ino 259)
159 *
160 * The sequence of steps that lead to the second state was:
161 *
162 * mv /a/b/c/d /a/b/c2/d2
163 * mv /a/b/c /a/b/c2/d2/cc
164 *
165 * "c" has lower inode number, but we can't move it (2nd mv operation)
166 * before we move "d", which has higher inode number.
167 *
168 * So we just memorize which move/rename operations must be performed
169 * later when their respective parent is processed and moved/renamed.
170 */
171
172 /* Indexed by parent directory inode number. */
173 struct rb_root pending_dir_moves;
174
175 /*
176 * Reverse index, indexed by the inode number of a directory that
177 * is waiting for the move/rename of its immediate parent before its
178 * own move/rename can be performed.
179 */
180 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000181
182 /*
183 * A directory that is going to be rm'ed might have a child directory
184 * which is in the pending directory moves index above. In this case,
185 * the directory can only be removed after the move/rename of its child
186 * is performed. Example:
187 *
188 * Parent snapshot:
189 *
190 * . (ino 256)
191 * |-- a/ (ino 257)
192 * |-- b/ (ino 258)
193 * |-- c/ (ino 259)
194 * | |-- x/ (ino 260)
195 * |
196 * |-- y/ (ino 261)
197 *
198 * Send snapshot:
199 *
200 * . (ino 256)
201 * |-- a/ (ino 257)
202 * |-- b/ (ino 258)
203 * |-- YY/ (ino 261)
204 * |-- x/ (ino 260)
205 *
206 * Sequence of steps that lead to the send snapshot:
207 * rm -f /a/b/c/foo.txt
208 * mv /a/b/y /a/b/YY
209 * mv /a/b/c/x /a/b/YY
210 * rmdir /a/b/c
211 *
212 * When the child is processed, its move/rename is delayed until its
213 * parent is processed (as explained above), but all other operations
214 * like update utimes, chown, chgrp, etc, are performed and the paths
215 * that it uses for those operations must use the orphanized name of
216 * its parent (the directory we're going to rm later), so we need to
217 * memorize that name.
218 *
219 * Indexed by the inode number of the directory to be deleted.
220 */
221 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000222};
223
224struct pending_dir_move {
225 struct rb_node node;
226 struct list_head list;
227 u64 parent_ino;
228 u64 ino;
229 u64 gen;
230 struct list_head update_refs;
231};
232
233struct waiting_dir_move {
234 struct rb_node node;
235 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000236 /*
237 * There might be some directory that could not be removed because it
238 * was waiting for this directory inode to be moved first. Therefore
239 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
240 */
241 u64 rmdir_ino;
242};
243
244struct orphan_dir_info {
245 struct rb_node node;
246 u64 ino;
247 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +0200248};
249
250struct name_cache_entry {
251 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200252 /*
253 * radix_tree has only 32bit entries but we need to handle 64bit inums.
254 * We use the lower 32bit of the 64bit inum to store it in the tree. If
255 * more then one inum would fall into the same entry, we use radix_list
256 * to store the additional entries. radix_list is also used to store
257 * entries where two entries have the same inum but different
258 * generations.
259 */
260 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200261 u64 ino;
262 u64 gen;
263 u64 parent_ino;
264 u64 parent_gen;
265 int ret;
266 int need_later_update;
267 int name_len;
268 char name[];
269};
270
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000271static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
272
Filipe Manana9dc44212014-02-19 14:31:44 +0000273static struct waiting_dir_move *
274get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
275
276static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
277
Josef Bacik16e75492013-10-22 12:18:51 -0400278static int need_send_hole(struct send_ctx *sctx)
279{
280 return (sctx->parent_root && !sctx->cur_inode_new &&
281 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
282 S_ISREG(sctx->cur_inode_mode));
283}
284
Alexander Block31db9f72012-07-25 23:19:24 +0200285static void fs_path_reset(struct fs_path *p)
286{
287 if (p->reversed) {
288 p->start = p->buf + p->buf_len - 1;
289 p->end = p->start;
290 *p->start = 0;
291 } else {
292 p->start = p->buf;
293 p->end = p->start;
294 *p->start = 0;
295 }
296}
297
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000298static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200299{
300 struct fs_path *p;
301
302 p = kmalloc(sizeof(*p), GFP_NOFS);
303 if (!p)
304 return NULL;
305 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200306 p->buf = p->inline_buf;
307 p->buf_len = FS_PATH_INLINE_SIZE;
308 fs_path_reset(p);
309 return p;
310}
311
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000312static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200313{
314 struct fs_path *p;
315
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000316 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200317 if (!p)
318 return NULL;
319 p->reversed = 1;
320 fs_path_reset(p);
321 return p;
322}
323
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000324static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200325{
326 if (!p)
327 return;
David Sterbaace01052014-02-05 16:17:34 +0100328 if (p->buf != p->inline_buf)
329 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200330 kfree(p);
331}
332
333static int fs_path_len(struct fs_path *p)
334{
335 return p->end - p->start;
336}
337
338static int fs_path_ensure_buf(struct fs_path *p, int len)
339{
340 char *tmp_buf;
341 int path_len;
342 int old_buf_len;
343
344 len++;
345
346 if (p->buf_len >= len)
347 return 0;
348
David Sterbaace01052014-02-05 16:17:34 +0100349 /*
350 * First time the inline_buf does not suffice
351 */
352 if (p->buf == p->inline_buf) {
353 p->buf = kmalloc(len, GFP_NOFS);
354 if (!p->buf)
355 return -ENOMEM;
356 /*
357 * The real size of the buffer is bigger, this will let the
358 * fast path happen most of the time
359 */
360 p->buf_len = ksize(p->buf);
361 } else {
362 char *tmp;
363
364 tmp = krealloc(p->buf, len, GFP_NOFS);
365 if (!tmp)
366 return -ENOMEM;
367 p->buf = tmp;
368 p->buf_len = ksize(p->buf);
369 }
370
Alexander Block31db9f72012-07-25 23:19:24 +0200371 path_len = p->end - p->start;
372 old_buf_len = p->buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +0200373
Alexander Block31db9f72012-07-25 23:19:24 +0200374 if (p->reversed) {
375 tmp_buf = p->buf + old_buf_len - path_len - 1;
376 p->end = p->buf + p->buf_len - 1;
377 p->start = p->end - path_len;
378 memmove(p->start, tmp_buf, path_len + 1);
379 } else {
380 p->start = p->buf;
381 p->end = p->start + path_len;
382 }
383 return 0;
384}
385
David Sterbab23ab572014-02-03 19:23:19 +0100386static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
387 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200388{
389 int ret;
390 int new_len;
391
392 new_len = p->end - p->start + name_len;
393 if (p->start != p->end)
394 new_len++;
395 ret = fs_path_ensure_buf(p, new_len);
396 if (ret < 0)
397 goto out;
398
399 if (p->reversed) {
400 if (p->start != p->end)
401 *--p->start = '/';
402 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100403 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200404 } else {
405 if (p->start != p->end)
406 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100407 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200408 p->end += name_len;
409 *p->end = 0;
410 }
411
412out:
413 return ret;
414}
415
416static int fs_path_add(struct fs_path *p, const char *name, int name_len)
417{
418 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100419 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200420
David Sterbab23ab572014-02-03 19:23:19 +0100421 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200422 if (ret < 0)
423 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100424 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200425
426out:
427 return ret;
428}
429
430static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
431{
432 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100433 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200434
David Sterbab23ab572014-02-03 19:23:19 +0100435 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200436 if (ret < 0)
437 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100438 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200439
440out:
441 return ret;
442}
443
444static int fs_path_add_from_extent_buffer(struct fs_path *p,
445 struct extent_buffer *eb,
446 unsigned long off, int len)
447{
448 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100449 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200450
David Sterbab23ab572014-02-03 19:23:19 +0100451 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200452 if (ret < 0)
453 goto out;
454
David Sterbab23ab572014-02-03 19:23:19 +0100455 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200456
457out:
458 return ret;
459}
460
Alexander Block31db9f72012-07-25 23:19:24 +0200461static int fs_path_copy(struct fs_path *p, struct fs_path *from)
462{
463 int ret;
464
465 p->reversed = from->reversed;
466 fs_path_reset(p);
467
468 ret = fs_path_add_path(p, from);
469
470 return ret;
471}
472
473
474static void fs_path_unreverse(struct fs_path *p)
475{
476 char *tmp;
477 int len;
478
479 if (!p->reversed)
480 return;
481
482 tmp = p->start;
483 len = p->end - p->start;
484 p->start = p->buf;
485 p->end = p->start + len;
486 memmove(p->start, tmp, len + 1);
487 p->reversed = 0;
488}
489
490static struct btrfs_path *alloc_path_for_send(void)
491{
492 struct btrfs_path *path;
493
494 path = btrfs_alloc_path();
495 if (!path)
496 return NULL;
497 path->search_commit_root = 1;
498 path->skip_locking = 1;
499 return path;
500}
501
Eric Sandeen48a3b632013-04-25 20:41:01 +0000502static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200503{
504 int ret;
505 mm_segment_t old_fs;
506 u32 pos = 0;
507
508 old_fs = get_fs();
509 set_fs(KERNEL_DS);
510
511 while (pos < len) {
Anand Jain1bcea352012-09-14 00:04:21 -0600512 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200513 /* TODO handle that correctly */
514 /*if (ret == -ERESTARTSYS) {
515 continue;
516 }*/
517 if (ret < 0)
518 goto out;
519 if (ret == 0) {
520 ret = -EIO;
521 goto out;
522 }
523 pos += ret;
524 }
525
526 ret = 0;
527
528out:
529 set_fs(old_fs);
530 return ret;
531}
532
533static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
534{
535 struct btrfs_tlv_header *hdr;
536 int total_len = sizeof(*hdr) + len;
537 int left = sctx->send_max_size - sctx->send_size;
538
539 if (unlikely(left < total_len))
540 return -EOVERFLOW;
541
542 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
543 hdr->tlv_type = cpu_to_le16(attr);
544 hdr->tlv_len = cpu_to_le16(len);
545 memcpy(hdr + 1, data, len);
546 sctx->send_size += total_len;
547
548 return 0;
549}
550
David Sterba95bc79d2013-12-16 17:34:10 +0100551#define TLV_PUT_DEFINE_INT(bits) \
552 static int tlv_put_u##bits(struct send_ctx *sctx, \
553 u##bits attr, u##bits value) \
554 { \
555 __le##bits __tmp = cpu_to_le##bits(value); \
556 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
557 }
Alexander Block31db9f72012-07-25 23:19:24 +0200558
David Sterba95bc79d2013-12-16 17:34:10 +0100559TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200560
561static int tlv_put_string(struct send_ctx *sctx, u16 attr,
562 const char *str, int len)
563{
564 if (len == -1)
565 len = strlen(str);
566 return tlv_put(sctx, attr, str, len);
567}
568
569static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
570 const u8 *uuid)
571{
572 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
573}
574
Alexander Block31db9f72012-07-25 23:19:24 +0200575static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
576 struct extent_buffer *eb,
577 struct btrfs_timespec *ts)
578{
579 struct btrfs_timespec bts;
580 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
581 return tlv_put(sctx, attr, &bts, sizeof(bts));
582}
583
584
585#define TLV_PUT(sctx, attrtype, attrlen, data) \
586 do { \
587 ret = tlv_put(sctx, attrtype, attrlen, data); \
588 if (ret < 0) \
589 goto tlv_put_failure; \
590 } while (0)
591
592#define TLV_PUT_INT(sctx, attrtype, bits, value) \
593 do { \
594 ret = tlv_put_u##bits(sctx, attrtype, value); \
595 if (ret < 0) \
596 goto tlv_put_failure; \
597 } while (0)
598
599#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
600#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
601#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
602#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
603#define TLV_PUT_STRING(sctx, attrtype, str, len) \
604 do { \
605 ret = tlv_put_string(sctx, attrtype, str, len); \
606 if (ret < 0) \
607 goto tlv_put_failure; \
608 } while (0)
609#define TLV_PUT_PATH(sctx, attrtype, p) \
610 do { \
611 ret = tlv_put_string(sctx, attrtype, p->start, \
612 p->end - p->start); \
613 if (ret < 0) \
614 goto tlv_put_failure; \
615 } while(0)
616#define TLV_PUT_UUID(sctx, attrtype, uuid) \
617 do { \
618 ret = tlv_put_uuid(sctx, attrtype, uuid); \
619 if (ret < 0) \
620 goto tlv_put_failure; \
621 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200622#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
623 do { \
624 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
625 if (ret < 0) \
626 goto tlv_put_failure; \
627 } while (0)
628
629static int send_header(struct send_ctx *sctx)
630{
631 struct btrfs_stream_header hdr;
632
633 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
634 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
635
Anand Jain1bcea352012-09-14 00:04:21 -0600636 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
637 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200638}
639
640/*
641 * For each command/item we want to send to userspace, we call this function.
642 */
643static int begin_cmd(struct send_ctx *sctx, int cmd)
644{
645 struct btrfs_cmd_header *hdr;
646
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530647 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200648 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200649
650 BUG_ON(sctx->send_size);
651
652 sctx->send_size += sizeof(*hdr);
653 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
654 hdr->cmd = cpu_to_le16(cmd);
655
656 return 0;
657}
658
659static int send_cmd(struct send_ctx *sctx)
660{
661 int ret;
662 struct btrfs_cmd_header *hdr;
663 u32 crc;
664
665 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
666 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
667 hdr->crc = 0;
668
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000669 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200670 hdr->crc = cpu_to_le32(crc);
671
Anand Jain1bcea352012-09-14 00:04:21 -0600672 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
673 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200674
675 sctx->total_send_size += sctx->send_size;
676 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
677 sctx->send_size = 0;
678
679 return ret;
680}
681
682/*
683 * Sends a move instruction to user space
684 */
685static int send_rename(struct send_ctx *sctx,
686 struct fs_path *from, struct fs_path *to)
687{
688 int ret;
689
690verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
691
692 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
693 if (ret < 0)
694 goto out;
695
696 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
697 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
698
699 ret = send_cmd(sctx);
700
701tlv_put_failure:
702out:
703 return ret;
704}
705
706/*
707 * Sends a link instruction to user space
708 */
709static int send_link(struct send_ctx *sctx,
710 struct fs_path *path, struct fs_path *lnk)
711{
712 int ret;
713
714verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
715
716 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
717 if (ret < 0)
718 goto out;
719
720 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
721 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
722
723 ret = send_cmd(sctx);
724
725tlv_put_failure:
726out:
727 return ret;
728}
729
730/*
731 * Sends an unlink instruction to user space
732 */
733static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
734{
735 int ret;
736
737verbose_printk("btrfs: send_unlink %s\n", path->start);
738
739 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
740 if (ret < 0)
741 goto out;
742
743 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
744
745 ret = send_cmd(sctx);
746
747tlv_put_failure:
748out:
749 return ret;
750}
751
752/*
753 * Sends a rmdir instruction to user space
754 */
755static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
756{
757 int ret;
758
759verbose_printk("btrfs: send_rmdir %s\n", path->start);
760
761 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
762 if (ret < 0)
763 goto out;
764
765 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
766
767 ret = send_cmd(sctx);
768
769tlv_put_failure:
770out:
771 return ret;
772}
773
774/*
775 * Helper function to retrieve some fields from an inode item.
776 */
777static int get_inode_info(struct btrfs_root *root,
778 u64 ino, u64 *size, u64 *gen,
Alexander Block85a7b332012-07-26 23:39:10 +0200779 u64 *mode, u64 *uid, u64 *gid,
780 u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200781{
782 int ret;
783 struct btrfs_inode_item *ii;
784 struct btrfs_key key;
785 struct btrfs_path *path;
786
787 path = alloc_path_for_send();
788 if (!path)
789 return -ENOMEM;
790
791 key.objectid = ino;
792 key.type = BTRFS_INODE_ITEM_KEY;
793 key.offset = 0;
794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
795 if (ret < 0)
796 goto out;
797 if (ret) {
798 ret = -ENOENT;
799 goto out;
800 }
801
802 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
803 struct btrfs_inode_item);
804 if (size)
805 *size = btrfs_inode_size(path->nodes[0], ii);
806 if (gen)
807 *gen = btrfs_inode_generation(path->nodes[0], ii);
808 if (mode)
809 *mode = btrfs_inode_mode(path->nodes[0], ii);
810 if (uid)
811 *uid = btrfs_inode_uid(path->nodes[0], ii);
812 if (gid)
813 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200814 if (rdev)
815 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200816
817out:
818 btrfs_free_path(path);
819 return ret;
820}
821
822typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
823 struct fs_path *p,
824 void *ctx);
825
826/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000827 * Helper function to iterate the entries in ONE btrfs_inode_ref or
828 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200829 * The iterate callback may return a non zero value to stop iteration. This can
830 * be a negative value for error codes or 1 to simply stop it.
831 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000832 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200833 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000834static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200835 struct btrfs_key *found_key, int resolve,
836 iterate_inode_ref_t iterate, void *ctx)
837{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000838 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200839 struct btrfs_item *item;
840 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000841 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200842 struct btrfs_path *tmp_path;
843 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000844 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200845 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000846 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200847 u32 name_len;
848 char *start;
849 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000850 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200851 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000852 u64 dir;
853 unsigned long name_off;
854 unsigned long elem_size;
855 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200856
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000857 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200858 if (!p)
859 return -ENOMEM;
860
861 tmp_path = alloc_path_for_send();
862 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000863 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200864 return -ENOMEM;
865 }
866
Alexander Block31db9f72012-07-25 23:19:24 +0200867
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000868 if (found_key->type == BTRFS_INODE_REF_KEY) {
869 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
870 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100871 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000872 total = btrfs_item_size(eb, item);
873 elem_size = sizeof(*iref);
874 } else {
875 ptr = btrfs_item_ptr_offset(eb, slot);
876 total = btrfs_item_size_nr(eb, slot);
877 elem_size = sizeof(*extref);
878 }
879
Alexander Block31db9f72012-07-25 23:19:24 +0200880 while (cur < total) {
881 fs_path_reset(p);
882
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000883 if (found_key->type == BTRFS_INODE_REF_KEY) {
884 iref = (struct btrfs_inode_ref *)(ptr + cur);
885 name_len = btrfs_inode_ref_name_len(eb, iref);
886 name_off = (unsigned long)(iref + 1);
887 index = btrfs_inode_ref_index(eb, iref);
888 dir = found_key->offset;
889 } else {
890 extref = (struct btrfs_inode_extref *)(ptr + cur);
891 name_len = btrfs_inode_extref_name_len(eb, extref);
892 name_off = (unsigned long)&extref->name;
893 index = btrfs_inode_extref_index(eb, extref);
894 dir = btrfs_inode_extref_parent(eb, extref);
895 }
896
Alexander Block31db9f72012-07-25 23:19:24 +0200897 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000898 start = btrfs_ref_to_path(root, tmp_path, name_len,
899 name_off, eb, dir,
900 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200901 if (IS_ERR(start)) {
902 ret = PTR_ERR(start);
903 goto out;
904 }
905 if (start < p->buf) {
906 /* overflow , try again with larger buffer */
907 ret = fs_path_ensure_buf(p,
908 p->buf_len + p->buf - start);
909 if (ret < 0)
910 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000911 start = btrfs_ref_to_path(root, tmp_path,
912 name_len, name_off,
913 eb, dir,
914 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200915 if (IS_ERR(start)) {
916 ret = PTR_ERR(start);
917 goto out;
918 }
919 BUG_ON(start < p->buf);
920 }
921 p->start = start;
922 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000923 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
924 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200925 if (ret < 0)
926 goto out;
927 }
928
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000929 cur += elem_size + name_len;
930 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200931 if (ret)
932 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200933 num++;
934 }
935
936out:
937 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000938 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200939 return ret;
940}
941
942typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
943 const char *name, int name_len,
944 const char *data, int data_len,
945 u8 type, void *ctx);
946
947/*
948 * Helper function to iterate the entries in ONE btrfs_dir_item.
949 * The iterate callback may return a non zero value to stop iteration. This can
950 * be a negative value for error codes or 1 to simply stop it.
951 *
952 * path must point to the dir item when called.
953 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000954static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200955 struct btrfs_key *found_key,
956 iterate_dir_item_t iterate, void *ctx)
957{
958 int ret = 0;
959 struct extent_buffer *eb;
960 struct btrfs_item *item;
961 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200962 struct btrfs_key di_key;
963 char *buf = NULL;
David Sterbaace01052014-02-05 16:17:34 +0100964 const int buf_len = PATH_MAX;
Alexander Block31db9f72012-07-25 23:19:24 +0200965 u32 name_len;
966 u32 data_len;
967 u32 cur;
968 u32 len;
969 u32 total;
970 int slot;
971 int num;
972 u8 type;
973
Alexander Block31db9f72012-07-25 23:19:24 +0200974 buf = kmalloc(buf_len, GFP_NOFS);
975 if (!buf) {
976 ret = -ENOMEM;
977 goto out;
978 }
979
Alexander Block31db9f72012-07-25 23:19:24 +0200980 eb = path->nodes[0];
981 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +0100982 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +0200983 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
984 cur = 0;
985 len = 0;
986 total = btrfs_item_size(eb, item);
987
988 num = 0;
989 while (cur < total) {
990 name_len = btrfs_dir_name_len(eb, di);
991 data_len = btrfs_dir_data_len(eb, di);
992 type = btrfs_dir_type(eb, di);
993 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
994
David Sterbaace01052014-02-05 16:17:34 +0100995 /*
996 * Path too long
997 */
Alexander Block31db9f72012-07-25 23:19:24 +0200998 if (name_len + data_len > buf_len) {
David Sterbaace01052014-02-05 16:17:34 +0100999 ret = -ENAMETOOLONG;
1000 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02001001 }
1002
1003 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1004 name_len + data_len);
1005
1006 len = sizeof(*di) + name_len + data_len;
1007 di = (struct btrfs_dir_item *)((char *)di + len);
1008 cur += len;
1009
1010 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1011 data_len, type, ctx);
1012 if (ret < 0)
1013 goto out;
1014 if (ret) {
1015 ret = 0;
1016 goto out;
1017 }
1018
1019 num++;
1020 }
1021
1022out:
David Sterbaace01052014-02-05 16:17:34 +01001023 kfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001024 return ret;
1025}
1026
1027static int __copy_first_ref(int num, u64 dir, int index,
1028 struct fs_path *p, void *ctx)
1029{
1030 int ret;
1031 struct fs_path *pt = ctx;
1032
1033 ret = fs_path_copy(pt, p);
1034 if (ret < 0)
1035 return ret;
1036
1037 /* we want the first only */
1038 return 1;
1039}
1040
1041/*
1042 * Retrieve the first path of an inode. If an inode has more then one
1043 * ref/hardlink, this is ignored.
1044 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001045static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001046 u64 ino, struct fs_path *path)
1047{
1048 int ret;
1049 struct btrfs_key key, found_key;
1050 struct btrfs_path *p;
1051
1052 p = alloc_path_for_send();
1053 if (!p)
1054 return -ENOMEM;
1055
1056 fs_path_reset(path);
1057
1058 key.objectid = ino;
1059 key.type = BTRFS_INODE_REF_KEY;
1060 key.offset = 0;
1061
1062 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1063 if (ret < 0)
1064 goto out;
1065 if (ret) {
1066 ret = 1;
1067 goto out;
1068 }
1069 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1070 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001071 (found_key.type != BTRFS_INODE_REF_KEY &&
1072 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001073 ret = -ENOENT;
1074 goto out;
1075 }
1076
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001077 ret = iterate_inode_ref(root, p, &found_key, 1,
1078 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001079 if (ret < 0)
1080 goto out;
1081 ret = 0;
1082
1083out:
1084 btrfs_free_path(p);
1085 return ret;
1086}
1087
1088struct backref_ctx {
1089 struct send_ctx *sctx;
1090
1091 /* number of total found references */
1092 u64 found;
1093
1094 /*
1095 * used for clones found in send_root. clones found behind cur_objectid
1096 * and cur_offset are not considered as allowed clones.
1097 */
1098 u64 cur_objectid;
1099 u64 cur_offset;
1100
1101 /* may be truncated in case it's the last extent in a file */
1102 u64 extent_len;
1103
1104 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001105 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001106};
1107
1108static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1109{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001110 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001111 struct clone_root *cr = (struct clone_root *)elt;
1112
1113 if (root < cr->root->objectid)
1114 return -1;
1115 if (root > cr->root->objectid)
1116 return 1;
1117 return 0;
1118}
1119
1120static int __clone_root_cmp_sort(const void *e1, const void *e2)
1121{
1122 struct clone_root *cr1 = (struct clone_root *)e1;
1123 struct clone_root *cr2 = (struct clone_root *)e2;
1124
1125 if (cr1->root->objectid < cr2->root->objectid)
1126 return -1;
1127 if (cr1->root->objectid > cr2->root->objectid)
1128 return 1;
1129 return 0;
1130}
1131
1132/*
1133 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001134 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001135 */
1136static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1137{
1138 struct backref_ctx *bctx = ctx_;
1139 struct clone_root *found;
1140 int ret;
1141 u64 i_size;
1142
1143 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001144 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001145 bctx->sctx->clone_roots_cnt,
1146 sizeof(struct clone_root),
1147 __clone_root_cmp_bsearch);
1148 if (!found)
1149 return 0;
1150
1151 if (found->root == bctx->sctx->send_root &&
1152 ino == bctx->cur_objectid &&
1153 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001154 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001155 }
1156
1157 /*
Alexander Block766702e2012-07-28 14:11:31 +02001158 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001159 * accept clones from these extents.
1160 */
Alexander Block85a7b332012-07-26 23:39:10 +02001161 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1162 NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001163 if (ret < 0)
1164 return ret;
1165
1166 if (offset + bctx->extent_len > i_size)
1167 return 0;
1168
1169 /*
1170 * Make sure we don't consider clones from send_root that are
1171 * behind the current inode/offset.
1172 */
1173 if (found->root == bctx->sctx->send_root) {
1174 /*
1175 * TODO for the moment we don't accept clones from the inode
1176 * that is currently send. We may change this when
1177 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1178 * file.
1179 */
1180 if (ino >= bctx->cur_objectid)
1181 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001182#if 0
1183 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001184 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001185 if (offset + bctx->extent_len > bctx->cur_offset)
1186 return 0;
1187#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001188 }
1189
1190 bctx->found++;
1191 found->found_refs++;
1192 if (ino < found->ino) {
1193 found->ino = ino;
1194 found->offset = offset;
1195 } else if (found->ino == ino) {
1196 /*
1197 * same extent found more then once in the same file.
1198 */
1199 if (found->offset > offset + bctx->extent_len)
1200 found->offset = offset;
1201 }
1202
1203 return 0;
1204}
1205
1206/*
Alexander Block766702e2012-07-28 14:11:31 +02001207 * Given an inode, offset and extent item, it finds a good clone for a clone
1208 * instruction. Returns -ENOENT when none could be found. The function makes
1209 * sure that the returned clone is usable at the point where sending is at the
1210 * moment. This means, that no clones are accepted which lie behind the current
1211 * inode+offset.
1212 *
Alexander Block31db9f72012-07-25 23:19:24 +02001213 * path must point to the extent item when called.
1214 */
1215static int find_extent_clone(struct send_ctx *sctx,
1216 struct btrfs_path *path,
1217 u64 ino, u64 data_offset,
1218 u64 ino_size,
1219 struct clone_root **found)
1220{
1221 int ret;
1222 int extent_type;
1223 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001224 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001225 u64 num_bytes;
1226 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001227 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001228 struct btrfs_file_extent_item *fi;
1229 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001230 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001231 struct clone_root *cur_clone_root;
1232 struct btrfs_key found_key;
1233 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001234 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001235 u32 i;
1236
1237 tmp_path = alloc_path_for_send();
1238 if (!tmp_path)
1239 return -ENOMEM;
1240
Alexander Block35075bb2012-07-28 12:44:34 +02001241 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1242 if (!backref_ctx) {
1243 ret = -ENOMEM;
1244 goto out;
1245 }
1246
Alexander Block31db9f72012-07-25 23:19:24 +02001247 if (data_offset >= ino_size) {
1248 /*
1249 * There may be extents that lie behind the file's size.
1250 * I at least had this in combination with snapshotting while
1251 * writing large files.
1252 */
1253 ret = 0;
1254 goto out;
1255 }
1256
1257 fi = btrfs_item_ptr(eb, path->slots[0],
1258 struct btrfs_file_extent_item);
1259 extent_type = btrfs_file_extent_type(eb, fi);
1260 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1261 ret = -ENOENT;
1262 goto out;
1263 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001264 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001265
1266 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001267 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1268 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001269 ret = -ENOENT;
1270 goto out;
1271 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001272 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001273
Liu Bo69917e42012-09-07 20:01:28 -06001274 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1275 &found_key, &flags);
Alexander Block31db9f72012-07-25 23:19:24 +02001276 btrfs_release_path(tmp_path);
1277
1278 if (ret < 0)
1279 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001280 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001281 ret = -EIO;
1282 goto out;
1283 }
1284
1285 /*
1286 * Setup the clone roots.
1287 */
1288 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1289 cur_clone_root = sctx->clone_roots + i;
1290 cur_clone_root->ino = (u64)-1;
1291 cur_clone_root->offset = 0;
1292 cur_clone_root->found_refs = 0;
1293 }
1294
Alexander Block35075bb2012-07-28 12:44:34 +02001295 backref_ctx->sctx = sctx;
1296 backref_ctx->found = 0;
1297 backref_ctx->cur_objectid = ino;
1298 backref_ctx->cur_offset = data_offset;
1299 backref_ctx->found_itself = 0;
1300 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001301
1302 /*
1303 * The last extent of a file may be too large due to page alignment.
1304 * We need to adjust extent_len in this case so that the checks in
1305 * __iterate_backrefs work.
1306 */
1307 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001308 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001309
1310 /*
1311 * Now collect all backrefs.
1312 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001313 if (compressed == BTRFS_COMPRESS_NONE)
1314 extent_item_pos = logical - found_key.objectid;
1315 else
1316 extent_item_pos = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001317 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1318 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001319 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001320
Alexander Block31db9f72012-07-25 23:19:24 +02001321 if (ret < 0)
1322 goto out;
1323
Alexander Block35075bb2012-07-28 12:44:34 +02001324 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001325 /* found a bug in backref code? */
1326 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001327 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001328 "send_root. inode=%llu, offset=%llu, "
Chris Mason74dd17f2012-08-07 16:25:13 -04001329 "disk_byte=%llu found extent=%llu\n",
1330 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001331 goto out;
1332 }
1333
1334verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1335 "ino=%llu, "
1336 "num_bytes=%llu, logical=%llu\n",
1337 data_offset, ino, num_bytes, logical);
1338
Alexander Block35075bb2012-07-28 12:44:34 +02001339 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001340 verbose_printk("btrfs: no clones found\n");
1341
1342 cur_clone_root = NULL;
1343 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1344 if (sctx->clone_roots[i].found_refs) {
1345 if (!cur_clone_root)
1346 cur_clone_root = sctx->clone_roots + i;
1347 else if (sctx->clone_roots[i].root == sctx->send_root)
1348 /* prefer clones from send_root over others */
1349 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001350 }
1351
1352 }
1353
1354 if (cur_clone_root) {
Filipe David Borba Manana93de4ba2014-02-15 15:53:16 +00001355 if (compressed != BTRFS_COMPRESS_NONE) {
1356 /*
1357 * Offsets given by iterate_extent_inodes() are relative
1358 * to the start of the extent, we need to add logical
1359 * offset from the file extent item.
1360 * (See why at backref.c:check_extent_in_eb())
1361 */
1362 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1363 fi);
1364 }
Alexander Block31db9f72012-07-25 23:19:24 +02001365 *found = cur_clone_root;
1366 ret = 0;
1367 } else {
1368 ret = -ENOENT;
1369 }
1370
1371out:
1372 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001373 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001374 return ret;
1375}
1376
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001377static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001378 u64 ino,
1379 struct fs_path *dest)
1380{
1381 int ret;
1382 struct btrfs_path *path;
1383 struct btrfs_key key;
1384 struct btrfs_file_extent_item *ei;
1385 u8 type;
1386 u8 compression;
1387 unsigned long off;
1388 int len;
1389
1390 path = alloc_path_for_send();
1391 if (!path)
1392 return -ENOMEM;
1393
1394 key.objectid = ino;
1395 key.type = BTRFS_EXTENT_DATA_KEY;
1396 key.offset = 0;
1397 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1398 if (ret < 0)
1399 goto out;
1400 BUG_ON(ret);
1401
1402 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1403 struct btrfs_file_extent_item);
1404 type = btrfs_file_extent_type(path->nodes[0], ei);
1405 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1406 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1407 BUG_ON(compression);
1408
1409 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001410 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001411
1412 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001413
1414out:
1415 btrfs_free_path(path);
1416 return ret;
1417}
1418
1419/*
1420 * Helper function to generate a file name that is unique in the root of
1421 * send_root and parent_root. This is used to generate names for orphan inodes.
1422 */
1423static int gen_unique_name(struct send_ctx *sctx,
1424 u64 ino, u64 gen,
1425 struct fs_path *dest)
1426{
1427 int ret = 0;
1428 struct btrfs_path *path;
1429 struct btrfs_dir_item *di;
1430 char tmp[64];
1431 int len;
1432 u64 idx = 0;
1433
1434 path = alloc_path_for_send();
1435 if (!path)
1436 return -ENOMEM;
1437
1438 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001439 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001440 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001441 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001442
1443 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1444 path, BTRFS_FIRST_FREE_OBJECTID,
1445 tmp, strlen(tmp), 0);
1446 btrfs_release_path(path);
1447 if (IS_ERR(di)) {
1448 ret = PTR_ERR(di);
1449 goto out;
1450 }
1451 if (di) {
1452 /* not unique, try again */
1453 idx++;
1454 continue;
1455 }
1456
1457 if (!sctx->parent_root) {
1458 /* unique */
1459 ret = 0;
1460 break;
1461 }
1462
1463 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1464 path, BTRFS_FIRST_FREE_OBJECTID,
1465 tmp, strlen(tmp), 0);
1466 btrfs_release_path(path);
1467 if (IS_ERR(di)) {
1468 ret = PTR_ERR(di);
1469 goto out;
1470 }
1471 if (di) {
1472 /* not unique, try again */
1473 idx++;
1474 continue;
1475 }
1476 /* unique */
1477 break;
1478 }
1479
1480 ret = fs_path_add(dest, tmp, strlen(tmp));
1481
1482out:
1483 btrfs_free_path(path);
1484 return ret;
1485}
1486
1487enum inode_state {
1488 inode_state_no_change,
1489 inode_state_will_create,
1490 inode_state_did_create,
1491 inode_state_will_delete,
1492 inode_state_did_delete,
1493};
1494
1495static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1496{
1497 int ret;
1498 int left_ret;
1499 int right_ret;
1500 u64 left_gen;
1501 u64 right_gen;
1502
1503 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001504 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001505 if (ret < 0 && ret != -ENOENT)
1506 goto out;
1507 left_ret = ret;
1508
1509 if (!sctx->parent_root) {
1510 right_ret = -ENOENT;
1511 } else {
1512 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001513 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001514 if (ret < 0 && ret != -ENOENT)
1515 goto out;
1516 right_ret = ret;
1517 }
1518
1519 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001520 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001521 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001522 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001523 if (ino < sctx->send_progress)
1524 ret = inode_state_did_create;
1525 else
1526 ret = inode_state_will_create;
1527 } else if (right_gen == gen) {
1528 if (ino < sctx->send_progress)
1529 ret = inode_state_did_delete;
1530 else
1531 ret = inode_state_will_delete;
1532 } else {
1533 ret = -ENOENT;
1534 }
1535 } else if (!left_ret) {
1536 if (left_gen == gen) {
1537 if (ino < sctx->send_progress)
1538 ret = inode_state_did_create;
1539 else
1540 ret = inode_state_will_create;
1541 } else {
1542 ret = -ENOENT;
1543 }
1544 } else if (!right_ret) {
1545 if (right_gen == gen) {
1546 if (ino < sctx->send_progress)
1547 ret = inode_state_did_delete;
1548 else
1549 ret = inode_state_will_delete;
1550 } else {
1551 ret = -ENOENT;
1552 }
1553 } else {
1554 ret = -ENOENT;
1555 }
1556
1557out:
1558 return ret;
1559}
1560
1561static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1562{
1563 int ret;
1564
1565 ret = get_cur_inode_state(sctx, ino, gen);
1566 if (ret < 0)
1567 goto out;
1568
1569 if (ret == inode_state_no_change ||
1570 ret == inode_state_did_create ||
1571 ret == inode_state_will_delete)
1572 ret = 1;
1573 else
1574 ret = 0;
1575
1576out:
1577 return ret;
1578}
1579
1580/*
1581 * Helper function to lookup a dir item in a dir.
1582 */
1583static int lookup_dir_item_inode(struct btrfs_root *root,
1584 u64 dir, const char *name, int name_len,
1585 u64 *found_inode,
1586 u8 *found_type)
1587{
1588 int ret = 0;
1589 struct btrfs_dir_item *di;
1590 struct btrfs_key key;
1591 struct btrfs_path *path;
1592
1593 path = alloc_path_for_send();
1594 if (!path)
1595 return -ENOMEM;
1596
1597 di = btrfs_lookup_dir_item(NULL, root, path,
1598 dir, name, name_len, 0);
1599 if (!di) {
1600 ret = -ENOENT;
1601 goto out;
1602 }
1603 if (IS_ERR(di)) {
1604 ret = PTR_ERR(di);
1605 goto out;
1606 }
1607 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1608 *found_inode = key.objectid;
1609 *found_type = btrfs_dir_type(path->nodes[0], di);
1610
1611out:
1612 btrfs_free_path(path);
1613 return ret;
1614}
1615
Alexander Block766702e2012-07-28 14:11:31 +02001616/*
1617 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1618 * generation of the parent dir and the name of the dir entry.
1619 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001620static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001621 u64 *dir, u64 *dir_gen, struct fs_path *name)
1622{
1623 int ret;
1624 struct btrfs_key key;
1625 struct btrfs_key found_key;
1626 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001627 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001628 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001629
1630 path = alloc_path_for_send();
1631 if (!path)
1632 return -ENOMEM;
1633
1634 key.objectid = ino;
1635 key.type = BTRFS_INODE_REF_KEY;
1636 key.offset = 0;
1637
1638 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1639 if (ret < 0)
1640 goto out;
1641 if (!ret)
1642 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1643 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001644 if (ret || found_key.objectid != ino ||
1645 (found_key.type != BTRFS_INODE_REF_KEY &&
1646 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001647 ret = -ENOENT;
1648 goto out;
1649 }
1650
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001651 if (key.type == BTRFS_INODE_REF_KEY) {
1652 struct btrfs_inode_ref *iref;
1653 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1654 struct btrfs_inode_ref);
1655 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1656 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1657 (unsigned long)(iref + 1),
1658 len);
1659 parent_dir = found_key.offset;
1660 } else {
1661 struct btrfs_inode_extref *extref;
1662 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1663 struct btrfs_inode_extref);
1664 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1665 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1666 (unsigned long)&extref->name, len);
1667 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1668 }
Alexander Block31db9f72012-07-25 23:19:24 +02001669 if (ret < 0)
1670 goto out;
1671 btrfs_release_path(path);
1672
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001673 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001674 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001675 if (ret < 0)
1676 goto out;
1677
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001678 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001679
1680out:
1681 btrfs_free_path(path);
1682 return ret;
1683}
1684
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001685static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001686 u64 ino, u64 dir,
1687 const char *name, int name_len)
1688{
1689 int ret;
1690 struct fs_path *tmp_name;
1691 u64 tmp_dir;
1692 u64 tmp_dir_gen;
1693
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001694 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001695 if (!tmp_name)
1696 return -ENOMEM;
1697
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001698 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001699 if (ret < 0)
1700 goto out;
1701
Alexander Blockb9291af2012-07-28 11:07:18 +02001702 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001703 ret = 0;
1704 goto out;
1705 }
1706
Alexander Blocke938c8a2012-07-28 16:33:49 +02001707 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001708
1709out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001710 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001711 return ret;
1712}
1713
Alexander Block766702e2012-07-28 14:11:31 +02001714/*
1715 * Used by process_recorded_refs to determine if a new ref would overwrite an
1716 * already existing ref. In case it detects an overwrite, it returns the
1717 * inode/gen in who_ino/who_gen.
1718 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1719 * to make sure later references to the overwritten inode are possible.
1720 * Orphanizing is however only required for the first ref of an inode.
1721 * process_recorded_refs does an additional is_first_ref check to see if
1722 * orphanizing is really required.
1723 */
Alexander Block31db9f72012-07-25 23:19:24 +02001724static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1725 const char *name, int name_len,
1726 u64 *who_ino, u64 *who_gen)
1727{
1728 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001729 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001730 u64 other_inode = 0;
1731 u8 other_type = 0;
1732
1733 if (!sctx->parent_root)
1734 goto out;
1735
1736 ret = is_inode_existent(sctx, dir, dir_gen);
1737 if (ret <= 0)
1738 goto out;
1739
Josef Bacikebdad912013-08-06 16:47:48 -04001740 /*
1741 * If we have a parent root we need to verify that the parent dir was
1742 * not delted and then re-created, if it was then we have no overwrite
1743 * and we can just unlink this entry.
1744 */
1745 if (sctx->parent_root) {
1746 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1747 NULL, NULL, NULL);
1748 if (ret < 0 && ret != -ENOENT)
1749 goto out;
1750 if (ret) {
1751 ret = 0;
1752 goto out;
1753 }
1754 if (gen != dir_gen)
1755 goto out;
1756 }
1757
Alexander Block31db9f72012-07-25 23:19:24 +02001758 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1759 &other_inode, &other_type);
1760 if (ret < 0 && ret != -ENOENT)
1761 goto out;
1762 if (ret) {
1763 ret = 0;
1764 goto out;
1765 }
1766
Alexander Block766702e2012-07-28 14:11:31 +02001767 /*
1768 * Check if the overwritten ref was already processed. If yes, the ref
1769 * was already unlinked/moved, so we can safely assume that we will not
1770 * overwrite anything at this point in time.
1771 */
Alexander Block31db9f72012-07-25 23:19:24 +02001772 if (other_inode > sctx->send_progress) {
1773 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001774 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001775 if (ret < 0)
1776 goto out;
1777
1778 ret = 1;
1779 *who_ino = other_inode;
1780 } else {
1781 ret = 0;
1782 }
1783
1784out:
1785 return ret;
1786}
1787
Alexander Block766702e2012-07-28 14:11:31 +02001788/*
1789 * Checks if the ref was overwritten by an already processed inode. This is
1790 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1791 * thus the orphan name needs be used.
1792 * process_recorded_refs also uses it to avoid unlinking of refs that were
1793 * overwritten.
1794 */
Alexander Block31db9f72012-07-25 23:19:24 +02001795static int did_overwrite_ref(struct send_ctx *sctx,
1796 u64 dir, u64 dir_gen,
1797 u64 ino, u64 ino_gen,
1798 const char *name, int name_len)
1799{
1800 int ret = 0;
1801 u64 gen;
1802 u64 ow_inode;
1803 u8 other_type;
1804
1805 if (!sctx->parent_root)
1806 goto out;
1807
1808 ret = is_inode_existent(sctx, dir, dir_gen);
1809 if (ret <= 0)
1810 goto out;
1811
1812 /* check if the ref was overwritten by another ref */
1813 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1814 &ow_inode, &other_type);
1815 if (ret < 0 && ret != -ENOENT)
1816 goto out;
1817 if (ret) {
1818 /* was never and will never be overwritten */
1819 ret = 0;
1820 goto out;
1821 }
1822
1823 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001824 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001825 if (ret < 0)
1826 goto out;
1827
1828 if (ow_inode == ino && gen == ino_gen) {
1829 ret = 0;
1830 goto out;
1831 }
1832
1833 /* we know that it is or will be overwritten. check this now */
1834 if (ow_inode < sctx->send_progress)
1835 ret = 1;
1836 else
1837 ret = 0;
1838
1839out:
1840 return ret;
1841}
1842
Alexander Block766702e2012-07-28 14:11:31 +02001843/*
1844 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1845 * that got overwritten. This is used by process_recorded_refs to determine
1846 * if it has to use the path as returned by get_cur_path or the orphan name.
1847 */
Alexander Block31db9f72012-07-25 23:19:24 +02001848static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1849{
1850 int ret = 0;
1851 struct fs_path *name = NULL;
1852 u64 dir;
1853 u64 dir_gen;
1854
1855 if (!sctx->parent_root)
1856 goto out;
1857
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001858 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001859 if (!name)
1860 return -ENOMEM;
1861
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001862 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001863 if (ret < 0)
1864 goto out;
1865
1866 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1867 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001868
1869out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001870 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001871 return ret;
1872}
1873
Alexander Block766702e2012-07-28 14:11:31 +02001874/*
1875 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1876 * so we need to do some special handling in case we have clashes. This function
1877 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001878 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001879 */
Alexander Block31db9f72012-07-25 23:19:24 +02001880static int name_cache_insert(struct send_ctx *sctx,
1881 struct name_cache_entry *nce)
1882{
1883 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001884 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001885
Alexander Block7e0926f2012-07-28 14:20:58 +02001886 nce_head = radix_tree_lookup(&sctx->name_cache,
1887 (unsigned long)nce->ino);
1888 if (!nce_head) {
1889 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001890 if (!nce_head) {
1891 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001892 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001893 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001894 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001895
Alexander Block7e0926f2012-07-28 14:20:58 +02001896 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001897 if (ret < 0) {
1898 kfree(nce_head);
1899 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001900 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001901 }
Alexander Block31db9f72012-07-25 23:19:24 +02001902 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001903 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001904 list_add_tail(&nce->list, &sctx->name_cache_list);
1905 sctx->name_cache_size++;
1906
1907 return ret;
1908}
1909
1910static void name_cache_delete(struct send_ctx *sctx,
1911 struct name_cache_entry *nce)
1912{
Alexander Block7e0926f2012-07-28 14:20:58 +02001913 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001914
Alexander Block7e0926f2012-07-28 14:20:58 +02001915 nce_head = radix_tree_lookup(&sctx->name_cache,
1916 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01001917 if (!nce_head) {
1918 btrfs_err(sctx->send_root->fs_info,
1919 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1920 nce->ino, sctx->name_cache_size);
1921 }
Alexander Block31db9f72012-07-25 23:19:24 +02001922
Alexander Block7e0926f2012-07-28 14:20:58 +02001923 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001924 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001925 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001926
David Sterba57fb8912014-02-03 19:24:40 +01001927 /*
1928 * We may not get to the final release of nce_head if the lookup fails
1929 */
1930 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02001931 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1932 kfree(nce_head);
1933 }
Alexander Block31db9f72012-07-25 23:19:24 +02001934}
1935
1936static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1937 u64 ino, u64 gen)
1938{
Alexander Block7e0926f2012-07-28 14:20:58 +02001939 struct list_head *nce_head;
1940 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001941
Alexander Block7e0926f2012-07-28 14:20:58 +02001942 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1943 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001944 return NULL;
1945
Alexander Block7e0926f2012-07-28 14:20:58 +02001946 list_for_each_entry(cur, nce_head, radix_list) {
1947 if (cur->ino == ino && cur->gen == gen)
1948 return cur;
1949 }
Alexander Block31db9f72012-07-25 23:19:24 +02001950 return NULL;
1951}
1952
Alexander Block766702e2012-07-28 14:11:31 +02001953/*
1954 * Removes the entry from the list and adds it back to the end. This marks the
1955 * entry as recently used so that name_cache_clean_unused does not remove it.
1956 */
Alexander Block31db9f72012-07-25 23:19:24 +02001957static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1958{
1959 list_del(&nce->list);
1960 list_add_tail(&nce->list, &sctx->name_cache_list);
1961}
1962
Alexander Block766702e2012-07-28 14:11:31 +02001963/*
1964 * Remove some entries from the beginning of name_cache_list.
1965 */
Alexander Block31db9f72012-07-25 23:19:24 +02001966static void name_cache_clean_unused(struct send_ctx *sctx)
1967{
1968 struct name_cache_entry *nce;
1969
1970 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1971 return;
1972
1973 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1974 nce = list_entry(sctx->name_cache_list.next,
1975 struct name_cache_entry, list);
1976 name_cache_delete(sctx, nce);
1977 kfree(nce);
1978 }
1979}
1980
1981static void name_cache_free(struct send_ctx *sctx)
1982{
1983 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02001984
Alexander Blocke938c8a2012-07-28 16:33:49 +02001985 while (!list_empty(&sctx->name_cache_list)) {
1986 nce = list_entry(sctx->name_cache_list.next,
1987 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02001988 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02001989 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001990 }
1991}
1992
Alexander Block766702e2012-07-28 14:11:31 +02001993/*
1994 * Used by get_cur_path for each ref up to the root.
1995 * Returns 0 if it succeeded.
1996 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1997 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1998 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1999 * Returns <0 in case of error.
2000 */
Alexander Block31db9f72012-07-25 23:19:24 +02002001static int __get_cur_name_and_parent(struct send_ctx *sctx,
2002 u64 ino, u64 gen,
2003 u64 *parent_ino,
2004 u64 *parent_gen,
2005 struct fs_path *dest)
2006{
2007 int ret;
2008 int nce_ret;
2009 struct btrfs_path *path = NULL;
2010 struct name_cache_entry *nce = NULL;
2011
Alexander Block766702e2012-07-28 14:11:31 +02002012 /*
2013 * First check if we already did a call to this function with the same
2014 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2015 * return the cached result.
2016 */
Alexander Block31db9f72012-07-25 23:19:24 +02002017 nce = name_cache_search(sctx, ino, gen);
2018 if (nce) {
2019 if (ino < sctx->send_progress && nce->need_later_update) {
2020 name_cache_delete(sctx, nce);
2021 kfree(nce);
2022 nce = NULL;
2023 } else {
2024 name_cache_used(sctx, nce);
2025 *parent_ino = nce->parent_ino;
2026 *parent_gen = nce->parent_gen;
2027 ret = fs_path_add(dest, nce->name, nce->name_len);
2028 if (ret < 0)
2029 goto out;
2030 ret = nce->ret;
2031 goto out;
2032 }
2033 }
2034
2035 path = alloc_path_for_send();
2036 if (!path)
2037 return -ENOMEM;
2038
Alexander Block766702e2012-07-28 14:11:31 +02002039 /*
2040 * If the inode is not existent yet, add the orphan name and return 1.
2041 * This should only happen for the parent dir that we determine in
2042 * __record_new_ref
2043 */
Alexander Block31db9f72012-07-25 23:19:24 +02002044 ret = is_inode_existent(sctx, ino, gen);
2045 if (ret < 0)
2046 goto out;
2047
2048 if (!ret) {
2049 ret = gen_unique_name(sctx, ino, gen, dest);
2050 if (ret < 0)
2051 goto out;
2052 ret = 1;
2053 goto out_cache;
2054 }
2055
Alexander Block766702e2012-07-28 14:11:31 +02002056 /*
2057 * Depending on whether the inode was already processed or not, use
2058 * send_root or parent_root for ref lookup.
2059 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002060 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002061 ret = get_first_ref(sctx->send_root, ino,
2062 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002063 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002064 ret = get_first_ref(sctx->parent_root, ino,
2065 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002066 if (ret < 0)
2067 goto out;
2068
Alexander Block766702e2012-07-28 14:11:31 +02002069 /*
2070 * Check if the ref was overwritten by an inode's ref that was processed
2071 * earlier. If yes, treat as orphan and return 1.
2072 */
Alexander Block31db9f72012-07-25 23:19:24 +02002073 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2074 dest->start, dest->end - dest->start);
2075 if (ret < 0)
2076 goto out;
2077 if (ret) {
2078 fs_path_reset(dest);
2079 ret = gen_unique_name(sctx, ino, gen, dest);
2080 if (ret < 0)
2081 goto out;
2082 ret = 1;
2083 }
2084
2085out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002086 /*
2087 * Store the result of the lookup in the name cache.
2088 */
Alexander Block31db9f72012-07-25 23:19:24 +02002089 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2090 if (!nce) {
2091 ret = -ENOMEM;
2092 goto out;
2093 }
2094
2095 nce->ino = ino;
2096 nce->gen = gen;
2097 nce->parent_ino = *parent_ino;
2098 nce->parent_gen = *parent_gen;
2099 nce->name_len = fs_path_len(dest);
2100 nce->ret = ret;
2101 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002102
2103 if (ino < sctx->send_progress)
2104 nce->need_later_update = 0;
2105 else
2106 nce->need_later_update = 1;
2107
2108 nce_ret = name_cache_insert(sctx, nce);
2109 if (nce_ret < 0)
2110 ret = nce_ret;
2111 name_cache_clean_unused(sctx);
2112
2113out:
2114 btrfs_free_path(path);
2115 return ret;
2116}
2117
2118/*
2119 * Magic happens here. This function returns the first ref to an inode as it
2120 * would look like while receiving the stream at this point in time.
2121 * We walk the path up to the root. For every inode in between, we check if it
2122 * was already processed/sent. If yes, we continue with the parent as found
2123 * in send_root. If not, we continue with the parent as found in parent_root.
2124 * If we encounter an inode that was deleted at this point in time, we use the
2125 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2126 * that were not created yet and overwritten inodes/refs.
2127 *
2128 * When do we have have orphan inodes:
2129 * 1. When an inode is freshly created and thus no valid refs are available yet
2130 * 2. When a directory lost all it's refs (deleted) but still has dir items
2131 * inside which were not processed yet (pending for move/delete). If anyone
2132 * tried to get the path to the dir items, it would get a path inside that
2133 * orphan directory.
2134 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2135 * of an unprocessed inode. If in that case the first ref would be
2136 * overwritten, the overwritten inode gets "orphanized". Later when we
2137 * process this overwritten inode, it is restored at a new place by moving
2138 * the orphan inode.
2139 *
2140 * sctx->send_progress tells this function at which point in time receiving
2141 * would be.
2142 */
2143static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2144 struct fs_path *dest)
2145{
2146 int ret = 0;
2147 struct fs_path *name = NULL;
2148 u64 parent_inode = 0;
2149 u64 parent_gen = 0;
2150 int stop = 0;
2151
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002152 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002153 if (!name) {
2154 ret = -ENOMEM;
2155 goto out;
2156 }
2157
2158 dest->reversed = 1;
2159 fs_path_reset(dest);
2160
2161 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2162 fs_path_reset(name);
2163
Filipe Manana9dc44212014-02-19 14:31:44 +00002164 if (is_waiting_for_rm(sctx, ino)) {
2165 ret = gen_unique_name(sctx, ino, gen, name);
2166 if (ret < 0)
2167 goto out;
2168 ret = fs_path_add_path(dest, name);
2169 break;
2170 }
2171
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002172 if (is_waiting_for_move(sctx, ino)) {
2173 ret = get_first_ref(sctx->parent_root, ino,
2174 &parent_inode, &parent_gen, name);
2175 } else {
2176 ret = __get_cur_name_and_parent(sctx, ino, gen,
2177 &parent_inode,
2178 &parent_gen, name);
2179 if (ret)
2180 stop = 1;
2181 }
2182
Alexander Block31db9f72012-07-25 23:19:24 +02002183 if (ret < 0)
2184 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002185
Alexander Block31db9f72012-07-25 23:19:24 +02002186 ret = fs_path_add_path(dest, name);
2187 if (ret < 0)
2188 goto out;
2189
2190 ino = parent_inode;
2191 gen = parent_gen;
2192 }
2193
2194out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002195 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002196 if (!ret)
2197 fs_path_unreverse(dest);
2198 return ret;
2199}
2200
2201/*
Alexander Block31db9f72012-07-25 23:19:24 +02002202 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2203 */
2204static int send_subvol_begin(struct send_ctx *sctx)
2205{
2206 int ret;
2207 struct btrfs_root *send_root = sctx->send_root;
2208 struct btrfs_root *parent_root = sctx->parent_root;
2209 struct btrfs_path *path;
2210 struct btrfs_key key;
2211 struct btrfs_root_ref *ref;
2212 struct extent_buffer *leaf;
2213 char *name = NULL;
2214 int namelen;
2215
Wang Shilongffcfaf82014-01-15 00:26:43 +08002216 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002217 if (!path)
2218 return -ENOMEM;
2219
2220 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2221 if (!name) {
2222 btrfs_free_path(path);
2223 return -ENOMEM;
2224 }
2225
2226 key.objectid = send_root->objectid;
2227 key.type = BTRFS_ROOT_BACKREF_KEY;
2228 key.offset = 0;
2229
2230 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2231 &key, path, 1, 0);
2232 if (ret < 0)
2233 goto out;
2234 if (ret) {
2235 ret = -ENOENT;
2236 goto out;
2237 }
2238
2239 leaf = path->nodes[0];
2240 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2241 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2242 key.objectid != send_root->objectid) {
2243 ret = -ENOENT;
2244 goto out;
2245 }
2246 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2247 namelen = btrfs_root_ref_name_len(leaf, ref);
2248 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2249 btrfs_release_path(path);
2250
Alexander Block31db9f72012-07-25 23:19:24 +02002251 if (parent_root) {
2252 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2253 if (ret < 0)
2254 goto out;
2255 } else {
2256 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2257 if (ret < 0)
2258 goto out;
2259 }
2260
2261 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2262 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2263 sctx->send_root->root_item.uuid);
2264 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002265 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002266 if (parent_root) {
2267 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2268 sctx->parent_root->root_item.uuid);
2269 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002270 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002271 }
2272
2273 ret = send_cmd(sctx);
2274
2275tlv_put_failure:
2276out:
2277 btrfs_free_path(path);
2278 kfree(name);
2279 return ret;
2280}
2281
2282static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2283{
2284 int ret = 0;
2285 struct fs_path *p;
2286
2287verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2288
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002289 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002290 if (!p)
2291 return -ENOMEM;
2292
2293 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2294 if (ret < 0)
2295 goto out;
2296
2297 ret = get_cur_path(sctx, ino, gen, p);
2298 if (ret < 0)
2299 goto out;
2300 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2301 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2302
2303 ret = send_cmd(sctx);
2304
2305tlv_put_failure:
2306out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002307 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002308 return ret;
2309}
2310
2311static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2312{
2313 int ret = 0;
2314 struct fs_path *p;
2315
2316verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2317
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002318 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002319 if (!p)
2320 return -ENOMEM;
2321
2322 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2323 if (ret < 0)
2324 goto out;
2325
2326 ret = get_cur_path(sctx, ino, gen, p);
2327 if (ret < 0)
2328 goto out;
2329 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2330 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2331
2332 ret = send_cmd(sctx);
2333
2334tlv_put_failure:
2335out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002336 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002337 return ret;
2338}
2339
2340static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2341{
2342 int ret = 0;
2343 struct fs_path *p;
2344
2345verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2346
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002347 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002348 if (!p)
2349 return -ENOMEM;
2350
2351 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2352 if (ret < 0)
2353 goto out;
2354
2355 ret = get_cur_path(sctx, ino, gen, p);
2356 if (ret < 0)
2357 goto out;
2358 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2359 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2360 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2361
2362 ret = send_cmd(sctx);
2363
2364tlv_put_failure:
2365out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002366 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002367 return ret;
2368}
2369
2370static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2371{
2372 int ret = 0;
2373 struct fs_path *p = NULL;
2374 struct btrfs_inode_item *ii;
2375 struct btrfs_path *path = NULL;
2376 struct extent_buffer *eb;
2377 struct btrfs_key key;
2378 int slot;
2379
2380verbose_printk("btrfs: send_utimes %llu\n", ino);
2381
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002382 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002383 if (!p)
2384 return -ENOMEM;
2385
2386 path = alloc_path_for_send();
2387 if (!path) {
2388 ret = -ENOMEM;
2389 goto out;
2390 }
2391
2392 key.objectid = ino;
2393 key.type = BTRFS_INODE_ITEM_KEY;
2394 key.offset = 0;
2395 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2396 if (ret < 0)
2397 goto out;
2398
2399 eb = path->nodes[0];
2400 slot = path->slots[0];
2401 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2402
2403 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2404 if (ret < 0)
2405 goto out;
2406
2407 ret = get_cur_path(sctx, ino, gen, p);
2408 if (ret < 0)
2409 goto out;
2410 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2411 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2412 btrfs_inode_atime(ii));
2413 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2414 btrfs_inode_mtime(ii));
2415 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2416 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002417 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002418
2419 ret = send_cmd(sctx);
2420
2421tlv_put_failure:
2422out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002423 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002424 btrfs_free_path(path);
2425 return ret;
2426}
2427
2428/*
2429 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2430 * a valid path yet because we did not process the refs yet. So, the inode
2431 * is created as orphan.
2432 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002433static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002434{
2435 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002436 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002437 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002438 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002439 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002440 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002441
Alexander Block1f4692d2012-07-28 10:42:24 +02002442verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002443
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002444 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002445 if (!p)
2446 return -ENOMEM;
2447
Alexander Block1f4692d2012-07-28 10:42:24 +02002448 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2449 NULL, &rdev);
2450 if (ret < 0)
2451 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002452
Alexander Blocke938c8a2012-07-28 16:33:49 +02002453 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002454 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002455 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002456 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002457 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002458 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002459 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002460 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002461 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002462 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002463 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002464 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002465 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002466 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2467 (int)(mode & S_IFMT));
2468 ret = -ENOTSUPP;
2469 goto out;
2470 }
2471
2472 ret = begin_cmd(sctx, cmd);
2473 if (ret < 0)
2474 goto out;
2475
Alexander Block1f4692d2012-07-28 10:42:24 +02002476 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002477 if (ret < 0)
2478 goto out;
2479
2480 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002481 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002482
2483 if (S_ISLNK(mode)) {
2484 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002485 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002486 if (ret < 0)
2487 goto out;
2488 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2489 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2490 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002491 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2492 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002493 }
2494
2495 ret = send_cmd(sctx);
2496 if (ret < 0)
2497 goto out;
2498
2499
2500tlv_put_failure:
2501out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002502 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002503 return ret;
2504}
2505
Alexander Block1f4692d2012-07-28 10:42:24 +02002506/*
2507 * We need some special handling for inodes that get processed before the parent
2508 * directory got created. See process_recorded_refs for details.
2509 * This function does the check if we already created the dir out of order.
2510 */
2511static int did_create_dir(struct send_ctx *sctx, u64 dir)
2512{
2513 int ret = 0;
2514 struct btrfs_path *path = NULL;
2515 struct btrfs_key key;
2516 struct btrfs_key found_key;
2517 struct btrfs_key di_key;
2518 struct extent_buffer *eb;
2519 struct btrfs_dir_item *di;
2520 int slot;
2521
2522 path = alloc_path_for_send();
2523 if (!path) {
2524 ret = -ENOMEM;
2525 goto out;
2526 }
2527
2528 key.objectid = dir;
2529 key.type = BTRFS_DIR_INDEX_KEY;
2530 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002531 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2532 if (ret < 0)
2533 goto out;
2534
Alexander Block1f4692d2012-07-28 10:42:24 +02002535 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002536 eb = path->nodes[0];
2537 slot = path->slots[0];
2538 if (slot >= btrfs_header_nritems(eb)) {
2539 ret = btrfs_next_leaf(sctx->send_root, path);
2540 if (ret < 0) {
2541 goto out;
2542 } else if (ret > 0) {
2543 ret = 0;
2544 break;
2545 }
2546 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002547 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002548
2549 btrfs_item_key_to_cpu(eb, &found_key, slot);
2550 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002551 found_key.type != key.type) {
2552 ret = 0;
2553 goto out;
2554 }
2555
2556 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2557 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2558
Josef Bacika0525412013-08-12 10:56:14 -04002559 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2560 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002561 ret = 1;
2562 goto out;
2563 }
2564
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002565 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002566 }
2567
2568out:
2569 btrfs_free_path(path);
2570 return ret;
2571}
2572
2573/*
2574 * Only creates the inode if it is:
2575 * 1. Not a directory
2576 * 2. Or a directory which was not created already due to out of order
2577 * directories. See did_create_dir and process_recorded_refs for details.
2578 */
2579static int send_create_inode_if_needed(struct send_ctx *sctx)
2580{
2581 int ret;
2582
2583 if (S_ISDIR(sctx->cur_inode_mode)) {
2584 ret = did_create_dir(sctx, sctx->cur_ino);
2585 if (ret < 0)
2586 goto out;
2587 if (ret) {
2588 ret = 0;
2589 goto out;
2590 }
2591 }
2592
2593 ret = send_create_inode(sctx, sctx->cur_ino);
2594 if (ret < 0)
2595 goto out;
2596
2597out:
2598 return ret;
2599}
2600
Alexander Block31db9f72012-07-25 23:19:24 +02002601struct recorded_ref {
2602 struct list_head list;
2603 char *dir_path;
2604 char *name;
2605 struct fs_path *full_path;
2606 u64 dir;
2607 u64 dir_gen;
2608 int dir_path_len;
2609 int name_len;
2610};
2611
2612/*
2613 * We need to process new refs before deleted refs, but compare_tree gives us
2614 * everything mixed. So we first record all refs and later process them.
2615 * This function is a helper to record one ref.
2616 */
2617static int record_ref(struct list_head *head, u64 dir,
2618 u64 dir_gen, struct fs_path *path)
2619{
2620 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002621
2622 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2623 if (!ref)
2624 return -ENOMEM;
2625
2626 ref->dir = dir;
2627 ref->dir_gen = dir_gen;
2628 ref->full_path = path;
2629
Andy Shevchenkoed848852013-08-21 10:32:13 +03002630 ref->name = (char *)kbasename(ref->full_path->start);
2631 ref->name_len = ref->full_path->end - ref->name;
2632 ref->dir_path = ref->full_path->start;
2633 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002634 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002635 else
Alexander Block31db9f72012-07-25 23:19:24 +02002636 ref->dir_path_len = ref->full_path->end -
2637 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002638
2639 list_add_tail(&ref->list, head);
2640 return 0;
2641}
2642
Josef Bacikba5e8f22013-08-16 16:52:55 -04002643static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2644{
2645 struct recorded_ref *new;
2646
2647 new = kmalloc(sizeof(*ref), GFP_NOFS);
2648 if (!new)
2649 return -ENOMEM;
2650
2651 new->dir = ref->dir;
2652 new->dir_gen = ref->dir_gen;
2653 new->full_path = NULL;
2654 INIT_LIST_HEAD(&new->list);
2655 list_add_tail(&new->list, list);
2656 return 0;
2657}
2658
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002659static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002660{
2661 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002662
Alexander Blocke938c8a2012-07-28 16:33:49 +02002663 while (!list_empty(head)) {
2664 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002665 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002666 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002667 kfree(cur);
2668 }
Alexander Block31db9f72012-07-25 23:19:24 +02002669}
2670
2671static void free_recorded_refs(struct send_ctx *sctx)
2672{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002673 __free_recorded_refs(&sctx->new_refs);
2674 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002675}
2676
2677/*
Alexander Block766702e2012-07-28 14:11:31 +02002678 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002679 * ref of an unprocessed inode gets overwritten and for all non empty
2680 * directories.
2681 */
2682static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2683 struct fs_path *path)
2684{
2685 int ret;
2686 struct fs_path *orphan;
2687
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002688 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002689 if (!orphan)
2690 return -ENOMEM;
2691
2692 ret = gen_unique_name(sctx, ino, gen, orphan);
2693 if (ret < 0)
2694 goto out;
2695
2696 ret = send_rename(sctx, path, orphan);
2697
2698out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002699 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002700 return ret;
2701}
2702
Filipe Manana9dc44212014-02-19 14:31:44 +00002703static struct orphan_dir_info *
2704add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2705{
2706 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2707 struct rb_node *parent = NULL;
2708 struct orphan_dir_info *entry, *odi;
2709
2710 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2711 if (!odi)
2712 return ERR_PTR(-ENOMEM);
2713 odi->ino = dir_ino;
2714 odi->gen = 0;
2715
2716 while (*p) {
2717 parent = *p;
2718 entry = rb_entry(parent, struct orphan_dir_info, node);
2719 if (dir_ino < entry->ino) {
2720 p = &(*p)->rb_left;
2721 } else if (dir_ino > entry->ino) {
2722 p = &(*p)->rb_right;
2723 } else {
2724 kfree(odi);
2725 return entry;
2726 }
2727 }
2728
2729 rb_link_node(&odi->node, parent, p);
2730 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2731 return odi;
2732}
2733
2734static struct orphan_dir_info *
2735get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2736{
2737 struct rb_node *n = sctx->orphan_dirs.rb_node;
2738 struct orphan_dir_info *entry;
2739
2740 while (n) {
2741 entry = rb_entry(n, struct orphan_dir_info, node);
2742 if (dir_ino < entry->ino)
2743 n = n->rb_left;
2744 else if (dir_ino > entry->ino)
2745 n = n->rb_right;
2746 else
2747 return entry;
2748 }
2749 return NULL;
2750}
2751
2752static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2753{
2754 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2755
2756 return odi != NULL;
2757}
2758
2759static void free_orphan_dir_info(struct send_ctx *sctx,
2760 struct orphan_dir_info *odi)
2761{
2762 if (!odi)
2763 return;
2764 rb_erase(&odi->node, &sctx->orphan_dirs);
2765 kfree(odi);
2766}
2767
Alexander Block31db9f72012-07-25 23:19:24 +02002768/*
2769 * Returns 1 if a directory can be removed at this point in time.
2770 * We check this by iterating all dir items and checking if the inode behind
2771 * the dir item was already processed.
2772 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002773static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2774 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002775{
2776 int ret = 0;
2777 struct btrfs_root *root = sctx->parent_root;
2778 struct btrfs_path *path;
2779 struct btrfs_key key;
2780 struct btrfs_key found_key;
2781 struct btrfs_key loc;
2782 struct btrfs_dir_item *di;
2783
Alexander Block6d85ed02012-08-01 14:48:59 +02002784 /*
2785 * Don't try to rmdir the top/root subvolume dir.
2786 */
2787 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2788 return 0;
2789
Alexander Block31db9f72012-07-25 23:19:24 +02002790 path = alloc_path_for_send();
2791 if (!path)
2792 return -ENOMEM;
2793
2794 key.objectid = dir;
2795 key.type = BTRFS_DIR_INDEX_KEY;
2796 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002797 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2798 if (ret < 0)
2799 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002800
2801 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002802 struct waiting_dir_move *dm;
2803
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002804 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2805 ret = btrfs_next_leaf(root, path);
2806 if (ret < 0)
2807 goto out;
2808 else if (ret > 0)
2809 break;
2810 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002811 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002812 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2813 path->slots[0]);
2814 if (found_key.objectid != key.objectid ||
2815 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002816 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002817
2818 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2819 struct btrfs_dir_item);
2820 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2821
Filipe Manana9dc44212014-02-19 14:31:44 +00002822 dm = get_waiting_dir_move(sctx, loc.objectid);
2823 if (dm) {
2824 struct orphan_dir_info *odi;
2825
2826 odi = add_orphan_dir_info(sctx, dir);
2827 if (IS_ERR(odi)) {
2828 ret = PTR_ERR(odi);
2829 goto out;
2830 }
2831 odi->gen = dir_gen;
2832 dm->rmdir_ino = dir;
2833 ret = 0;
2834 goto out;
2835 }
2836
Alexander Block31db9f72012-07-25 23:19:24 +02002837 if (loc.objectid > send_progress) {
2838 ret = 0;
2839 goto out;
2840 }
2841
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002842 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002843 }
2844
2845 ret = 1;
2846
2847out:
2848 btrfs_free_path(path);
2849 return ret;
2850}
2851
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002852static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2853{
Filipe Manana9dc44212014-02-19 14:31:44 +00002854 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002855
Filipe Manana9dc44212014-02-19 14:31:44 +00002856 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002857}
2858
2859static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2860{
2861 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2862 struct rb_node *parent = NULL;
2863 struct waiting_dir_move *entry, *dm;
2864
2865 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2866 if (!dm)
2867 return -ENOMEM;
2868 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00002869 dm->rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002870
2871 while (*p) {
2872 parent = *p;
2873 entry = rb_entry(parent, struct waiting_dir_move, node);
2874 if (ino < entry->ino) {
2875 p = &(*p)->rb_left;
2876 } else if (ino > entry->ino) {
2877 p = &(*p)->rb_right;
2878 } else {
2879 kfree(dm);
2880 return -EEXIST;
2881 }
2882 }
2883
2884 rb_link_node(&dm->node, parent, p);
2885 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2886 return 0;
2887}
2888
Filipe Manana9dc44212014-02-19 14:31:44 +00002889static struct waiting_dir_move *
2890get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002891{
2892 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2893 struct waiting_dir_move *entry;
2894
2895 while (n) {
2896 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00002897 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002898 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00002899 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002900 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002901 else
2902 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002903 }
Filipe Manana9dc44212014-02-19 14:31:44 +00002904 return NULL;
2905}
2906
2907static void free_waiting_dir_move(struct send_ctx *sctx,
2908 struct waiting_dir_move *dm)
2909{
2910 if (!dm)
2911 return;
2912 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2913 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002914}
2915
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002916static int add_pending_dir_move(struct send_ctx *sctx, u64 parent_ino)
2917{
2918 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2919 struct rb_node *parent = NULL;
2920 struct pending_dir_move *entry, *pm;
2921 struct recorded_ref *cur;
2922 int exists = 0;
2923 int ret;
2924
2925 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2926 if (!pm)
2927 return -ENOMEM;
2928 pm->parent_ino = parent_ino;
2929 pm->ino = sctx->cur_ino;
2930 pm->gen = sctx->cur_inode_gen;
2931 INIT_LIST_HEAD(&pm->list);
2932 INIT_LIST_HEAD(&pm->update_refs);
2933 RB_CLEAR_NODE(&pm->node);
2934
2935 while (*p) {
2936 parent = *p;
2937 entry = rb_entry(parent, struct pending_dir_move, node);
2938 if (parent_ino < entry->parent_ino) {
2939 p = &(*p)->rb_left;
2940 } else if (parent_ino > entry->parent_ino) {
2941 p = &(*p)->rb_right;
2942 } else {
2943 exists = 1;
2944 break;
2945 }
2946 }
2947
2948 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2949 ret = dup_ref(cur, &pm->update_refs);
2950 if (ret < 0)
2951 goto out;
2952 }
2953 list_for_each_entry(cur, &sctx->new_refs, list) {
2954 ret = dup_ref(cur, &pm->update_refs);
2955 if (ret < 0)
2956 goto out;
2957 }
2958
2959 ret = add_waiting_dir_move(sctx, pm->ino);
2960 if (ret)
2961 goto out;
2962
2963 if (exists) {
2964 list_add_tail(&pm->list, &entry->list);
2965 } else {
2966 rb_link_node(&pm->node, parent, p);
2967 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
2968 }
2969 ret = 0;
2970out:
2971 if (ret) {
2972 __free_recorded_refs(&pm->update_refs);
2973 kfree(pm);
2974 }
2975 return ret;
2976}
2977
2978static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
2979 u64 parent_ino)
2980{
2981 struct rb_node *n = sctx->pending_dir_moves.rb_node;
2982 struct pending_dir_move *entry;
2983
2984 while (n) {
2985 entry = rb_entry(n, struct pending_dir_move, node);
2986 if (parent_ino < entry->parent_ino)
2987 n = n->rb_left;
2988 else if (parent_ino > entry->parent_ino)
2989 n = n->rb_right;
2990 else
2991 return entry;
2992 }
2993 return NULL;
2994}
2995
2996static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
2997{
2998 struct fs_path *from_path = NULL;
2999 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003000 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003001 u64 orig_progress = sctx->send_progress;
3002 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003003 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003004 struct waiting_dir_move *dm = NULL;
3005 u64 rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003006 int ret;
3007
Filipe Manana2b863a12014-02-16 13:43:11 +00003008 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003009 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003010 if (!name || !from_path) {
3011 ret = -ENOMEM;
3012 goto out;
3013 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003014
Filipe Manana9dc44212014-02-19 14:31:44 +00003015 dm = get_waiting_dir_move(sctx, pm->ino);
3016 ASSERT(dm);
3017 rmdir_ino = dm->rmdir_ino;
3018 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003019
3020 ret = get_first_ref(sctx->parent_root, pm->ino,
3021 &parent_ino, &parent_gen, name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003022 if (ret < 0)
3023 goto out;
3024
Filipe Manana2b863a12014-02-16 13:43:11 +00003025 if (parent_ino == sctx->cur_ino) {
3026 /* child only renamed, not moved */
3027 ASSERT(parent_gen == sctx->cur_inode_gen);
3028 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3029 from_path);
3030 if (ret < 0)
3031 goto out;
3032 ret = fs_path_add_path(from_path, name);
3033 if (ret < 0)
3034 goto out;
3035 } else {
3036 /* child moved and maybe renamed too */
3037 sctx->send_progress = pm->ino;
3038 ret = get_cur_path(sctx, pm->ino, pm->gen, from_path);
3039 if (ret < 0)
3040 goto out;
3041 }
3042
3043 fs_path_free(name);
3044 name = NULL;
3045
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003046 to_path = fs_path_alloc();
3047 if (!to_path) {
3048 ret = -ENOMEM;
3049 goto out;
3050 }
3051
3052 sctx->send_progress = sctx->cur_ino + 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003053 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3054 if (ret < 0)
3055 goto out;
3056
3057 ret = send_rename(sctx, from_path, to_path);
3058 if (ret < 0)
3059 goto out;
3060
Filipe Manana9dc44212014-02-19 14:31:44 +00003061 if (rmdir_ino) {
3062 struct orphan_dir_info *odi;
3063
3064 odi = get_orphan_dir_info(sctx, rmdir_ino);
3065 if (!odi) {
3066 /* already deleted */
3067 goto finish;
3068 }
3069 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3070 if (ret < 0)
3071 goto out;
3072 if (!ret)
3073 goto finish;
3074
3075 name = fs_path_alloc();
3076 if (!name) {
3077 ret = -ENOMEM;
3078 goto out;
3079 }
3080 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3081 if (ret < 0)
3082 goto out;
3083 ret = send_rmdir(sctx, name);
3084 if (ret < 0)
3085 goto out;
3086 free_orphan_dir_info(sctx, odi);
3087 }
3088
3089finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003090 ret = send_utimes(sctx, pm->ino, pm->gen);
3091 if (ret < 0)
3092 goto out;
3093
3094 /*
3095 * After rename/move, need to update the utimes of both new parent(s)
3096 * and old parent(s).
3097 */
3098 list_for_each_entry(cur, &pm->update_refs, list) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003099 if (cur->dir == rmdir_ino)
3100 continue;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003101 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3102 if (ret < 0)
3103 goto out;
3104 }
3105
3106out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003107 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003108 fs_path_free(from_path);
3109 fs_path_free(to_path);
3110 sctx->send_progress = orig_progress;
3111
3112 return ret;
3113}
3114
3115static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3116{
3117 if (!list_empty(&m->list))
3118 list_del(&m->list);
3119 if (!RB_EMPTY_NODE(&m->node))
3120 rb_erase(&m->node, &sctx->pending_dir_moves);
3121 __free_recorded_refs(&m->update_refs);
3122 kfree(m);
3123}
3124
3125static void tail_append_pending_moves(struct pending_dir_move *moves,
3126 struct list_head *stack)
3127{
3128 if (list_empty(&moves->list)) {
3129 list_add_tail(&moves->list, stack);
3130 } else {
3131 LIST_HEAD(list);
3132 list_splice_init(&moves->list, &list);
3133 list_add_tail(&moves->list, stack);
3134 list_splice_tail(&list, stack);
3135 }
3136}
3137
3138static int apply_children_dir_moves(struct send_ctx *sctx)
3139{
3140 struct pending_dir_move *pm;
3141 struct list_head stack;
3142 u64 parent_ino = sctx->cur_ino;
3143 int ret = 0;
3144
3145 pm = get_pending_dir_moves(sctx, parent_ino);
3146 if (!pm)
3147 return 0;
3148
3149 INIT_LIST_HEAD(&stack);
3150 tail_append_pending_moves(pm, &stack);
3151
3152 while (!list_empty(&stack)) {
3153 pm = list_first_entry(&stack, struct pending_dir_move, list);
3154 parent_ino = pm->ino;
3155 ret = apply_dir_move(sctx, pm);
3156 free_pending_move(sctx, pm);
3157 if (ret)
3158 goto out;
3159 pm = get_pending_dir_moves(sctx, parent_ino);
3160 if (pm)
3161 tail_append_pending_moves(pm, &stack);
3162 }
3163 return 0;
3164
3165out:
3166 while (!list_empty(&stack)) {
3167 pm = list_first_entry(&stack, struct pending_dir_move, list);
3168 free_pending_move(sctx, pm);
3169 }
3170 return ret;
3171}
3172
3173static int wait_for_parent_move(struct send_ctx *sctx,
3174 struct recorded_ref *parent_ref)
3175{
3176 int ret;
3177 u64 ino = parent_ref->dir;
3178 u64 parent_ino_before, parent_ino_after;
3179 u64 new_gen, old_gen;
3180 struct fs_path *path_before = NULL;
3181 struct fs_path *path_after = NULL;
3182 int len1, len2;
3183
3184 if (parent_ref->dir <= sctx->cur_ino)
3185 return 0;
3186
3187 if (is_waiting_for_move(sctx, ino))
3188 return 1;
3189
3190 ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen,
3191 NULL, NULL, NULL, NULL);
3192 if (ret == -ENOENT)
3193 return 0;
3194 else if (ret < 0)
3195 return ret;
3196
3197 ret = get_inode_info(sctx->send_root, ino, NULL, &new_gen,
3198 NULL, NULL, NULL, NULL);
3199 if (ret < 0)
3200 return ret;
3201
3202 if (new_gen != old_gen)
3203 return 0;
3204
3205 path_before = fs_path_alloc();
3206 if (!path_before)
3207 return -ENOMEM;
3208
3209 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3210 NULL, path_before);
3211 if (ret == -ENOENT) {
3212 ret = 0;
3213 goto out;
3214 } else if (ret < 0) {
3215 goto out;
3216 }
3217
3218 path_after = fs_path_alloc();
3219 if (!path_after) {
3220 ret = -ENOMEM;
3221 goto out;
3222 }
3223
3224 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3225 NULL, path_after);
3226 if (ret == -ENOENT) {
3227 ret = 0;
3228 goto out;
3229 } else if (ret < 0) {
3230 goto out;
3231 }
3232
3233 len1 = fs_path_len(path_before);
3234 len2 = fs_path_len(path_after);
Filipe David Borba Manana5ed7f9f2014-02-01 02:02:16 +00003235 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3236 memcmp(path_before->start, path_after->start, len1)) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003237 ret = 1;
3238 goto out;
3239 }
3240 ret = 0;
3241
3242out:
3243 fs_path_free(path_before);
3244 fs_path_free(path_after);
3245
3246 return ret;
3247}
3248
Alexander Block31db9f72012-07-25 23:19:24 +02003249/*
3250 * This does all the move/link/unlink/rmdir magic.
3251 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003252static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003253{
3254 int ret = 0;
3255 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003256 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003257 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003258 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003259 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003260 u64 ow_gen;
3261 int did_overwrite = 0;
3262 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003263 u64 last_dir_ino_rm = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003264
3265verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3266
Alexander Block6d85ed02012-08-01 14:48:59 +02003267 /*
3268 * This should never happen as the root dir always has the same ref
3269 * which is always '..'
3270 */
3271 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003272 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003273
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003274 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003275 if (!valid_path) {
3276 ret = -ENOMEM;
3277 goto out;
3278 }
3279
Alexander Block31db9f72012-07-25 23:19:24 +02003280 /*
3281 * First, check if the first ref of the current inode was overwritten
3282 * before. If yes, we know that the current inode was already orphanized
3283 * and thus use the orphan name. If not, we can use get_cur_path to
3284 * get the path of the first ref as it would like while receiving at
3285 * this point in time.
3286 * New inodes are always orphan at the beginning, so force to use the
3287 * orphan name in this case.
3288 * The first ref is stored in valid_path and will be updated if it
3289 * gets moved around.
3290 */
3291 if (!sctx->cur_inode_new) {
3292 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3293 sctx->cur_inode_gen);
3294 if (ret < 0)
3295 goto out;
3296 if (ret)
3297 did_overwrite = 1;
3298 }
3299 if (sctx->cur_inode_new || did_overwrite) {
3300 ret = gen_unique_name(sctx, sctx->cur_ino,
3301 sctx->cur_inode_gen, valid_path);
3302 if (ret < 0)
3303 goto out;
3304 is_orphan = 1;
3305 } else {
3306 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3307 valid_path);
3308 if (ret < 0)
3309 goto out;
3310 }
3311
3312 list_for_each_entry(cur, &sctx->new_refs, list) {
3313 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003314 * We may have refs where the parent directory does not exist
3315 * yet. This happens if the parent directories inum is higher
3316 * the the current inum. To handle this case, we create the
3317 * parent directory out of order. But we need to check if this
3318 * did already happen before due to other refs in the same dir.
3319 */
3320 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3321 if (ret < 0)
3322 goto out;
3323 if (ret == inode_state_will_create) {
3324 ret = 0;
3325 /*
3326 * First check if any of the current inodes refs did
3327 * already create the dir.
3328 */
3329 list_for_each_entry(cur2, &sctx->new_refs, list) {
3330 if (cur == cur2)
3331 break;
3332 if (cur2->dir == cur->dir) {
3333 ret = 1;
3334 break;
3335 }
3336 }
3337
3338 /*
3339 * If that did not happen, check if a previous inode
3340 * did already create the dir.
3341 */
3342 if (!ret)
3343 ret = did_create_dir(sctx, cur->dir);
3344 if (ret < 0)
3345 goto out;
3346 if (!ret) {
3347 ret = send_create_inode(sctx, cur->dir);
3348 if (ret < 0)
3349 goto out;
3350 }
3351 }
3352
3353 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003354 * Check if this new ref would overwrite the first ref of
3355 * another unprocessed inode. If yes, orphanize the
3356 * overwritten inode. If we find an overwritten ref that is
3357 * not the first ref, simply unlink it.
3358 */
3359 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3360 cur->name, cur->name_len,
3361 &ow_inode, &ow_gen);
3362 if (ret < 0)
3363 goto out;
3364 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003365 ret = is_first_ref(sctx->parent_root,
3366 ow_inode, cur->dir, cur->name,
3367 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003368 if (ret < 0)
3369 goto out;
3370 if (ret) {
3371 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3372 cur->full_path);
3373 if (ret < 0)
3374 goto out;
3375 } else {
3376 ret = send_unlink(sctx, cur->full_path);
3377 if (ret < 0)
3378 goto out;
3379 }
3380 }
3381
3382 /*
3383 * link/move the ref to the new place. If we have an orphan
3384 * inode, move it and update valid_path. If not, link or move
3385 * it depending on the inode mode.
3386 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003387 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003388 ret = send_rename(sctx, valid_path, cur->full_path);
3389 if (ret < 0)
3390 goto out;
3391 is_orphan = 0;
3392 ret = fs_path_copy(valid_path, cur->full_path);
3393 if (ret < 0)
3394 goto out;
3395 } else {
3396 if (S_ISDIR(sctx->cur_inode_mode)) {
3397 /*
3398 * Dirs can't be linked, so move it. For moved
3399 * dirs, we always have one new and one deleted
3400 * ref. The deleted ref is ignored later.
3401 */
Filipe David Borba Mananad86477b2014-01-30 13:27:12 +00003402 ret = wait_for_parent_move(sctx, cur);
3403 if (ret < 0)
3404 goto out;
3405 if (ret) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003406 ret = add_pending_dir_move(sctx,
3407 cur->dir);
3408 *pending_move = 1;
3409 } else {
3410 ret = send_rename(sctx, valid_path,
3411 cur->full_path);
3412 if (!ret)
3413 ret = fs_path_copy(valid_path,
3414 cur->full_path);
3415 }
Alexander Block31db9f72012-07-25 23:19:24 +02003416 if (ret < 0)
3417 goto out;
3418 } else {
3419 ret = send_link(sctx, cur->full_path,
3420 valid_path);
3421 if (ret < 0)
3422 goto out;
3423 }
3424 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003425 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003426 if (ret < 0)
3427 goto out;
3428 }
3429
3430 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3431 /*
3432 * Check if we can already rmdir the directory. If not,
3433 * orphanize it. For every dir item inside that gets deleted
3434 * later, we do this check again and rmdir it then if possible.
3435 * See the use of check_dirs for more details.
3436 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003437 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3438 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003439 if (ret < 0)
3440 goto out;
3441 if (ret) {
3442 ret = send_rmdir(sctx, valid_path);
3443 if (ret < 0)
3444 goto out;
3445 } else if (!is_orphan) {
3446 ret = orphanize_inode(sctx, sctx->cur_ino,
3447 sctx->cur_inode_gen, valid_path);
3448 if (ret < 0)
3449 goto out;
3450 is_orphan = 1;
3451 }
3452
3453 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003454 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003455 if (ret < 0)
3456 goto out;
3457 }
Alexander Blockccf16262012-07-28 11:46:29 +02003458 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3459 !list_empty(&sctx->deleted_refs)) {
3460 /*
3461 * We have a moved dir. Add the old parent to check_dirs
3462 */
3463 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3464 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003465 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003466 if (ret < 0)
3467 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003468 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3469 /*
3470 * We have a non dir inode. Go through all deleted refs and
3471 * unlink them if they were not already overwritten by other
3472 * inodes.
3473 */
3474 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3475 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3476 sctx->cur_ino, sctx->cur_inode_gen,
3477 cur->name, cur->name_len);
3478 if (ret < 0)
3479 goto out;
3480 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003481 ret = send_unlink(sctx, cur->full_path);
3482 if (ret < 0)
3483 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003484 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003485 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003486 if (ret < 0)
3487 goto out;
3488 }
Alexander Block31db9f72012-07-25 23:19:24 +02003489 /*
3490 * If the inode is still orphan, unlink the orphan. This may
3491 * happen when a previous inode did overwrite the first ref
3492 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003493 * inode. Unlinking does not mean that the inode is deleted in
3494 * all cases. There may still be links to this inode in other
3495 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003496 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003497 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003498 ret = send_unlink(sctx, valid_path);
3499 if (ret < 0)
3500 goto out;
3501 }
3502 }
3503
3504 /*
3505 * We did collect all parent dirs where cur_inode was once located. We
3506 * now go through all these dirs and check if they are pending for
3507 * deletion and if it's finally possible to perform the rmdir now.
3508 * We also update the inode stats of the parent dirs here.
3509 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003510 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003511 /*
3512 * In case we had refs into dirs that were not processed yet,
3513 * we don't need to do the utime and rmdir logic for these dirs.
3514 * The dir will be processed later.
3515 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003516 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003517 continue;
3518
Josef Bacikba5e8f22013-08-16 16:52:55 -04003519 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003520 if (ret < 0)
3521 goto out;
3522
3523 if (ret == inode_state_did_create ||
3524 ret == inode_state_no_change) {
3525 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003526 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003527 if (ret < 0)
3528 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003529 } else if (ret == inode_state_did_delete &&
3530 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003531 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3532 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003533 if (ret < 0)
3534 goto out;
3535 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003536 ret = get_cur_path(sctx, cur->dir,
3537 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003538 if (ret < 0)
3539 goto out;
3540 ret = send_rmdir(sctx, valid_path);
3541 if (ret < 0)
3542 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003543 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02003544 }
3545 }
3546 }
3547
Alexander Block31db9f72012-07-25 23:19:24 +02003548 ret = 0;
3549
3550out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04003551 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003552 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003553 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003554 return ret;
3555}
3556
3557static int __record_new_ref(int num, u64 dir, int index,
3558 struct fs_path *name,
3559 void *ctx)
3560{
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
3570 ret = get_inode_info(sctx->send_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
3582 ret = record_ref(&sctx->new_refs, dir, gen, p);
3583
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
3590static int __record_deleted_ref(int num, u64 dir, int index,
3591 struct fs_path *name,
3592 void *ctx)
3593{
3594 int ret = 0;
3595 struct send_ctx *sctx = ctx;
3596 struct fs_path *p;
3597 u64 gen;
3598
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003599 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003600 if (!p)
3601 return -ENOMEM;
3602
3603 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003604 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003605 if (ret < 0)
3606 goto out;
3607
3608 ret = get_cur_path(sctx, dir, gen, p);
3609 if (ret < 0)
3610 goto out;
3611 ret = fs_path_add_path(p, name);
3612 if (ret < 0)
3613 goto out;
3614
3615 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3616
3617out:
3618 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003619 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003620 return ret;
3621}
3622
3623static int record_new_ref(struct send_ctx *sctx)
3624{
3625 int ret;
3626
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003627 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3628 sctx->cmp_key, 0, __record_new_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
3637static int record_deleted_ref(struct send_ctx *sctx)
3638{
3639 int ret;
3640
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003641 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3642 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003643 if (ret < 0)
3644 goto out;
3645 ret = 0;
3646
3647out:
3648 return ret;
3649}
3650
3651struct find_ref_ctx {
3652 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003653 u64 dir_gen;
3654 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003655 struct fs_path *name;
3656 int found_idx;
3657};
3658
3659static int __find_iref(int num, u64 dir, int index,
3660 struct fs_path *name,
3661 void *ctx_)
3662{
3663 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003664 u64 dir_gen;
3665 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003666
3667 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3668 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003669 /*
3670 * To avoid doing extra lookups we'll only do this if everything
3671 * else matches.
3672 */
3673 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3674 NULL, NULL, NULL);
3675 if (ret)
3676 return ret;
3677 if (dir_gen != ctx->dir_gen)
3678 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003679 ctx->found_idx = num;
3680 return 1;
3681 }
3682 return 0;
3683}
3684
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003685static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003686 struct btrfs_path *path,
3687 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003688 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003689{
3690 int ret;
3691 struct find_ref_ctx ctx;
3692
3693 ctx.dir = dir;
3694 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003695 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003696 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003697 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003698
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003699 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003700 if (ret < 0)
3701 return ret;
3702
3703 if (ctx.found_idx == -1)
3704 return -ENOENT;
3705
3706 return ctx.found_idx;
3707}
3708
3709static int __record_changed_new_ref(int num, u64 dir, int index,
3710 struct fs_path *name,
3711 void *ctx)
3712{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003713 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003714 int ret;
3715 struct send_ctx *sctx = ctx;
3716
Josef Bacikba5e8f22013-08-16 16:52:55 -04003717 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3718 NULL, NULL, NULL);
3719 if (ret)
3720 return ret;
3721
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003722 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003723 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003724 if (ret == -ENOENT)
3725 ret = __record_new_ref(num, dir, index, name, sctx);
3726 else if (ret > 0)
3727 ret = 0;
3728
3729 return ret;
3730}
3731
3732static int __record_changed_deleted_ref(int num, u64 dir, int index,
3733 struct fs_path *name,
3734 void *ctx)
3735{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003736 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003737 int ret;
3738 struct send_ctx *sctx = ctx;
3739
Josef Bacikba5e8f22013-08-16 16:52:55 -04003740 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3741 NULL, NULL, NULL);
3742 if (ret)
3743 return ret;
3744
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003745 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003746 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003747 if (ret == -ENOENT)
3748 ret = __record_deleted_ref(num, dir, index, name, sctx);
3749 else if (ret > 0)
3750 ret = 0;
3751
3752 return ret;
3753}
3754
3755static int record_changed_ref(struct send_ctx *sctx)
3756{
3757 int ret = 0;
3758
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003759 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003760 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3761 if (ret < 0)
3762 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003763 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003764 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3765 if (ret < 0)
3766 goto out;
3767 ret = 0;
3768
3769out:
3770 return ret;
3771}
3772
3773/*
3774 * Record and process all refs at once. Needed when an inode changes the
3775 * generation number, which means that it was deleted and recreated.
3776 */
3777static int process_all_refs(struct send_ctx *sctx,
3778 enum btrfs_compare_tree_result cmd)
3779{
3780 int ret;
3781 struct btrfs_root *root;
3782 struct btrfs_path *path;
3783 struct btrfs_key key;
3784 struct btrfs_key found_key;
3785 struct extent_buffer *eb;
3786 int slot;
3787 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003788 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003789
3790 path = alloc_path_for_send();
3791 if (!path)
3792 return -ENOMEM;
3793
3794 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3795 root = sctx->send_root;
3796 cb = __record_new_ref;
3797 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3798 root = sctx->parent_root;
3799 cb = __record_deleted_ref;
3800 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01003801 btrfs_err(sctx->send_root->fs_info,
3802 "Wrong command %d in process_all_refs", cmd);
3803 ret = -EINVAL;
3804 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003805 }
3806
3807 key.objectid = sctx->cmp_key->objectid;
3808 key.type = BTRFS_INODE_REF_KEY;
3809 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003810 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3811 if (ret < 0)
3812 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003813
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003814 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02003815 eb = path->nodes[0];
3816 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003817 if (slot >= btrfs_header_nritems(eb)) {
3818 ret = btrfs_next_leaf(root, path);
3819 if (ret < 0)
3820 goto out;
3821 else if (ret > 0)
3822 break;
3823 continue;
3824 }
3825
Alexander Block31db9f72012-07-25 23:19:24 +02003826 btrfs_item_key_to_cpu(eb, &found_key, slot);
3827
3828 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003829 (found_key.type != BTRFS_INODE_REF_KEY &&
3830 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003831 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003832
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003833 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003834 if (ret < 0)
3835 goto out;
3836
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003837 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003838 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003839 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003840
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003841 ret = process_recorded_refs(sctx, &pending_move);
3842 /* Only applicable to an incremental send. */
3843 ASSERT(pending_move == 0);
Alexander Block31db9f72012-07-25 23:19:24 +02003844
3845out:
3846 btrfs_free_path(path);
3847 return ret;
3848}
3849
3850static int send_set_xattr(struct send_ctx *sctx,
3851 struct fs_path *path,
3852 const char *name, int name_len,
3853 const char *data, int data_len)
3854{
3855 int ret = 0;
3856
3857 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3858 if (ret < 0)
3859 goto out;
3860
3861 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3862 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3863 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3864
3865 ret = send_cmd(sctx);
3866
3867tlv_put_failure:
3868out:
3869 return ret;
3870}
3871
3872static int send_remove_xattr(struct send_ctx *sctx,
3873 struct fs_path *path,
3874 const char *name, int name_len)
3875{
3876 int ret = 0;
3877
3878 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3879 if (ret < 0)
3880 goto out;
3881
3882 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3883 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3884
3885 ret = send_cmd(sctx);
3886
3887tlv_put_failure:
3888out:
3889 return ret;
3890}
3891
3892static int __process_new_xattr(int num, struct btrfs_key *di_key,
3893 const char *name, int name_len,
3894 const char *data, int data_len,
3895 u8 type, void *ctx)
3896{
3897 int ret;
3898 struct send_ctx *sctx = ctx;
3899 struct fs_path *p;
3900 posix_acl_xattr_header dummy_acl;
3901
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003902 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003903 if (!p)
3904 return -ENOMEM;
3905
3906 /*
3907 * This hack is needed because empty acl's are stored as zero byte
3908 * data in xattrs. Problem with that is, that receiving these zero byte
3909 * acl's will fail later. To fix this, we send a dummy acl list that
3910 * only contains the version number and no entries.
3911 */
3912 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3913 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3914 if (data_len == 0) {
3915 dummy_acl.a_version =
3916 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3917 data = (char *)&dummy_acl;
3918 data_len = sizeof(dummy_acl);
3919 }
3920 }
3921
3922 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3923 if (ret < 0)
3924 goto out;
3925
3926 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3927
3928out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003929 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003930 return ret;
3931}
3932
3933static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3934 const char *name, int name_len,
3935 const char *data, int data_len,
3936 u8 type, void *ctx)
3937{
3938 int ret;
3939 struct send_ctx *sctx = ctx;
3940 struct fs_path *p;
3941
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003942 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003943 if (!p)
3944 return -ENOMEM;
3945
3946 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3947 if (ret < 0)
3948 goto out;
3949
3950 ret = send_remove_xattr(sctx, p, name, name_len);
3951
3952out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003953 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003954 return ret;
3955}
3956
3957static int process_new_xattr(struct send_ctx *sctx)
3958{
3959 int ret = 0;
3960
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003961 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3962 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003963
3964 return ret;
3965}
3966
3967static int process_deleted_xattr(struct send_ctx *sctx)
3968{
3969 int ret;
3970
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003971 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3972 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003973
3974 return ret;
3975}
3976
3977struct find_xattr_ctx {
3978 const char *name;
3979 int name_len;
3980 int found_idx;
3981 char *found_data;
3982 int found_data_len;
3983};
3984
3985static int __find_xattr(int num, struct btrfs_key *di_key,
3986 const char *name, int name_len,
3987 const char *data, int data_len,
3988 u8 type, void *vctx)
3989{
3990 struct find_xattr_ctx *ctx = vctx;
3991
3992 if (name_len == ctx->name_len &&
3993 strncmp(name, ctx->name, name_len) == 0) {
3994 ctx->found_idx = num;
3995 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00003996 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02003997 if (!ctx->found_data)
3998 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02003999 return 1;
4000 }
4001 return 0;
4002}
4003
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004004static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004005 struct btrfs_path *path,
4006 struct btrfs_key *key,
4007 const char *name, int name_len,
4008 char **data, int *data_len)
4009{
4010 int ret;
4011 struct find_xattr_ctx ctx;
4012
4013 ctx.name = name;
4014 ctx.name_len = name_len;
4015 ctx.found_idx = -1;
4016 ctx.found_data = NULL;
4017 ctx.found_data_len = 0;
4018
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004019 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004020 if (ret < 0)
4021 return ret;
4022
4023 if (ctx.found_idx == -1)
4024 return -ENOENT;
4025 if (data) {
4026 *data = ctx.found_data;
4027 *data_len = ctx.found_data_len;
4028 } else {
4029 kfree(ctx.found_data);
4030 }
4031 return ctx.found_idx;
4032}
4033
4034
4035static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4036 const char *name, int name_len,
4037 const char *data, int data_len,
4038 u8 type, void *ctx)
4039{
4040 int ret;
4041 struct send_ctx *sctx = ctx;
4042 char *found_data = NULL;
4043 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004044
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004045 ret = find_xattr(sctx->parent_root, sctx->right_path,
4046 sctx->cmp_key, name, name_len, &found_data,
4047 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004048 if (ret == -ENOENT) {
4049 ret = __process_new_xattr(num, di_key, name, name_len, data,
4050 data_len, type, ctx);
4051 } else if (ret >= 0) {
4052 if (data_len != found_data_len ||
4053 memcmp(data, found_data, data_len)) {
4054 ret = __process_new_xattr(num, di_key, name, name_len,
4055 data, data_len, type, ctx);
4056 } else {
4057 ret = 0;
4058 }
4059 }
4060
4061 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004062 return ret;
4063}
4064
4065static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4066 const char *name, int name_len,
4067 const char *data, int data_len,
4068 u8 type, void *ctx)
4069{
4070 int ret;
4071 struct send_ctx *sctx = ctx;
4072
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004073 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4074 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004075 if (ret == -ENOENT)
4076 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4077 data_len, type, ctx);
4078 else if (ret >= 0)
4079 ret = 0;
4080
4081 return ret;
4082}
4083
4084static int process_changed_xattr(struct send_ctx *sctx)
4085{
4086 int ret = 0;
4087
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004088 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004089 sctx->cmp_key, __process_changed_new_xattr, sctx);
4090 if (ret < 0)
4091 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004092 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004093 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4094
4095out:
4096 return ret;
4097}
4098
4099static int process_all_new_xattrs(struct send_ctx *sctx)
4100{
4101 int ret;
4102 struct btrfs_root *root;
4103 struct btrfs_path *path;
4104 struct btrfs_key key;
4105 struct btrfs_key found_key;
4106 struct extent_buffer *eb;
4107 int slot;
4108
4109 path = alloc_path_for_send();
4110 if (!path)
4111 return -ENOMEM;
4112
4113 root = sctx->send_root;
4114
4115 key.objectid = sctx->cmp_key->objectid;
4116 key.type = BTRFS_XATTR_ITEM_KEY;
4117 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004118 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4119 if (ret < 0)
4120 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004121
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004122 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004123 eb = path->nodes[0];
4124 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004125 if (slot >= btrfs_header_nritems(eb)) {
4126 ret = btrfs_next_leaf(root, path);
4127 if (ret < 0) {
4128 goto out;
4129 } else if (ret > 0) {
4130 ret = 0;
4131 break;
4132 }
4133 continue;
4134 }
Alexander Block31db9f72012-07-25 23:19:24 +02004135
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004136 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004137 if (found_key.objectid != key.objectid ||
4138 found_key.type != key.type) {
4139 ret = 0;
4140 goto out;
4141 }
4142
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004143 ret = iterate_dir_item(root, path, &found_key,
4144 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004145 if (ret < 0)
4146 goto out;
4147
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004148 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004149 }
4150
4151out:
4152 btrfs_free_path(path);
4153 return ret;
4154}
4155
Josef Baciked259092013-10-25 11:36:01 -04004156static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4157{
4158 struct btrfs_root *root = sctx->send_root;
4159 struct btrfs_fs_info *fs_info = root->fs_info;
4160 struct inode *inode;
4161 struct page *page;
4162 char *addr;
4163 struct btrfs_key key;
4164 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4165 pgoff_t last_index;
4166 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4167 ssize_t ret = 0;
4168
4169 key.objectid = sctx->cur_ino;
4170 key.type = BTRFS_INODE_ITEM_KEY;
4171 key.offset = 0;
4172
4173 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4174 if (IS_ERR(inode))
4175 return PTR_ERR(inode);
4176
4177 if (offset + len > i_size_read(inode)) {
4178 if (offset > i_size_read(inode))
4179 len = 0;
4180 else
4181 len = offset - i_size_read(inode);
4182 }
4183 if (len == 0)
4184 goto out;
4185
4186 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
4187 while (index <= last_index) {
4188 unsigned cur_len = min_t(unsigned, len,
4189 PAGE_CACHE_SIZE - pg_offset);
4190 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4191 if (!page) {
4192 ret = -ENOMEM;
4193 break;
4194 }
4195
4196 if (!PageUptodate(page)) {
4197 btrfs_readpage(NULL, page);
4198 lock_page(page);
4199 if (!PageUptodate(page)) {
4200 unlock_page(page);
4201 page_cache_release(page);
4202 ret = -EIO;
4203 break;
4204 }
4205 }
4206
4207 addr = kmap(page);
4208 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4209 kunmap(page);
4210 unlock_page(page);
4211 page_cache_release(page);
4212 index++;
4213 pg_offset = 0;
4214 len -= cur_len;
4215 ret += cur_len;
4216 }
4217out:
4218 iput(inode);
4219 return ret;
4220}
4221
Alexander Block31db9f72012-07-25 23:19:24 +02004222/*
4223 * Read some bytes from the current inode/file and send a write command to
4224 * user space.
4225 */
4226static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4227{
4228 int ret = 0;
4229 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004230 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004231
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004232 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004233 if (!p)
4234 return -ENOMEM;
4235
Alexander Block31db9f72012-07-25 23:19:24 +02004236verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4237
Josef Baciked259092013-10-25 11:36:01 -04004238 num_read = fill_read_buf(sctx, offset, len);
4239 if (num_read <= 0) {
4240 if (num_read < 0)
4241 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004242 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004243 }
Alexander Block31db9f72012-07-25 23:19:24 +02004244
4245 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4246 if (ret < 0)
4247 goto out;
4248
4249 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4250 if (ret < 0)
4251 goto out;
4252
4253 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4254 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004255 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004256
4257 ret = send_cmd(sctx);
4258
4259tlv_put_failure:
4260out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004261 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004262 if (ret < 0)
4263 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004264 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004265}
4266
4267/*
4268 * Send a clone command to user space.
4269 */
4270static int send_clone(struct send_ctx *sctx,
4271 u64 offset, u32 len,
4272 struct clone_root *clone_root)
4273{
4274 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004275 struct fs_path *p;
4276 u64 gen;
4277
4278verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4279 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4280 clone_root->root->objectid, clone_root->ino,
4281 clone_root->offset);
4282
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004283 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004284 if (!p)
4285 return -ENOMEM;
4286
4287 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4288 if (ret < 0)
4289 goto out;
4290
4291 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4292 if (ret < 0)
4293 goto out;
4294
4295 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4296 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4297 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4298
Alexander Blocke938c8a2012-07-28 16:33:49 +02004299 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004300 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004301 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004302 if (ret < 0)
4303 goto out;
4304 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4305 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004306 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004307 }
4308 if (ret < 0)
4309 goto out;
4310
4311 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02004312 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004313 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004314 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004315 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4316 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4317 clone_root->offset);
4318
4319 ret = send_cmd(sctx);
4320
4321tlv_put_failure:
4322out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004323 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004324 return ret;
4325}
4326
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004327/*
4328 * Send an update extent command to user space.
4329 */
4330static int send_update_extent(struct send_ctx *sctx,
4331 u64 offset, u32 len)
4332{
4333 int ret = 0;
4334 struct fs_path *p;
4335
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004336 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004337 if (!p)
4338 return -ENOMEM;
4339
4340 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4341 if (ret < 0)
4342 goto out;
4343
4344 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4345 if (ret < 0)
4346 goto out;
4347
4348 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4349 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4350 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4351
4352 ret = send_cmd(sctx);
4353
4354tlv_put_failure:
4355out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004356 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004357 return ret;
4358}
4359
Josef Bacik16e75492013-10-22 12:18:51 -04004360static int send_hole(struct send_ctx *sctx, u64 end)
4361{
4362 struct fs_path *p = NULL;
4363 u64 offset = sctx->cur_inode_last_extent;
4364 u64 len;
4365 int ret = 0;
4366
4367 p = fs_path_alloc();
4368 if (!p)
4369 return -ENOMEM;
4370 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4371 while (offset < end) {
4372 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4373
4374 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4375 if (ret < 0)
4376 break;
4377 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4378 if (ret < 0)
4379 break;
4380 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4381 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4382 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4383 ret = send_cmd(sctx);
4384 if (ret < 0)
4385 break;
4386 offset += len;
4387 }
4388tlv_put_failure:
4389 fs_path_free(p);
4390 return ret;
4391}
4392
Alexander Block31db9f72012-07-25 23:19:24 +02004393static int send_write_or_clone(struct send_ctx *sctx,
4394 struct btrfs_path *path,
4395 struct btrfs_key *key,
4396 struct clone_root *clone_root)
4397{
4398 int ret = 0;
4399 struct btrfs_file_extent_item *ei;
4400 u64 offset = key->offset;
4401 u64 pos = 0;
4402 u64 len;
4403 u32 l;
4404 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004405 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02004406
4407 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4408 struct btrfs_file_extent_item);
4409 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004410 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004411 len = btrfs_file_extent_inline_len(path->nodes[0],
4412 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004413 /*
4414 * it is possible the inline item won't cover the whole page,
4415 * but there may be items after this page. Make
4416 * sure to send the whole thing
4417 */
4418 len = PAGE_CACHE_ALIGN(len);
4419 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004420 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004421 }
Alexander Block31db9f72012-07-25 23:19:24 +02004422
4423 if (offset + len > sctx->cur_inode_size)
4424 len = sctx->cur_inode_size - offset;
4425 if (len == 0) {
4426 ret = 0;
4427 goto out;
4428 }
4429
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004430 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004431 ret = send_clone(sctx, offset, len, clone_root);
4432 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4433 ret = send_update_extent(sctx, offset, len);
4434 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004435 while (pos < len) {
4436 l = len - pos;
4437 if (l > BTRFS_SEND_READ_SIZE)
4438 l = BTRFS_SEND_READ_SIZE;
4439 ret = send_write(sctx, pos + offset, l);
4440 if (ret < 0)
4441 goto out;
4442 if (!ret)
4443 break;
4444 pos += ret;
4445 }
4446 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004447 }
Alexander Block31db9f72012-07-25 23:19:24 +02004448out:
4449 return ret;
4450}
4451
4452static int is_extent_unchanged(struct send_ctx *sctx,
4453 struct btrfs_path *left_path,
4454 struct btrfs_key *ekey)
4455{
4456 int ret = 0;
4457 struct btrfs_key key;
4458 struct btrfs_path *path = NULL;
4459 struct extent_buffer *eb;
4460 int slot;
4461 struct btrfs_key found_key;
4462 struct btrfs_file_extent_item *ei;
4463 u64 left_disknr;
4464 u64 right_disknr;
4465 u64 left_offset;
4466 u64 right_offset;
4467 u64 left_offset_fixed;
4468 u64 left_len;
4469 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04004470 u64 left_gen;
4471 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004472 u8 left_type;
4473 u8 right_type;
4474
4475 path = alloc_path_for_send();
4476 if (!path)
4477 return -ENOMEM;
4478
4479 eb = left_path->nodes[0];
4480 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02004481 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4482 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004483
4484 if (left_type != BTRFS_FILE_EXTENT_REG) {
4485 ret = 0;
4486 goto out;
4487 }
Chris Mason74dd17f2012-08-07 16:25:13 -04004488 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4489 left_len = btrfs_file_extent_num_bytes(eb, ei);
4490 left_offset = btrfs_file_extent_offset(eb, ei);
4491 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004492
4493 /*
4494 * Following comments will refer to these graphics. L is the left
4495 * extents which we are checking at the moment. 1-8 are the right
4496 * extents that we iterate.
4497 *
4498 * |-----L-----|
4499 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4500 *
4501 * |-----L-----|
4502 * |--1--|-2b-|...(same as above)
4503 *
4504 * Alternative situation. Happens on files where extents got split.
4505 * |-----L-----|
4506 * |-----------7-----------|-6-|
4507 *
4508 * Alternative situation. Happens on files which got larger.
4509 * |-----L-----|
4510 * |-8-|
4511 * Nothing follows after 8.
4512 */
4513
4514 key.objectid = ekey->objectid;
4515 key.type = BTRFS_EXTENT_DATA_KEY;
4516 key.offset = ekey->offset;
4517 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4518 if (ret < 0)
4519 goto out;
4520 if (ret) {
4521 ret = 0;
4522 goto out;
4523 }
4524
4525 /*
4526 * Handle special case where the right side has no extents at all.
4527 */
4528 eb = path->nodes[0];
4529 slot = path->slots[0];
4530 btrfs_item_key_to_cpu(eb, &found_key, slot);
4531 if (found_key.objectid != key.objectid ||
4532 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004533 /* If we're a hole then just pretend nothing changed */
4534 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004535 goto out;
4536 }
4537
4538 /*
4539 * We're now on 2a, 2b or 7.
4540 */
4541 key = found_key;
4542 while (key.offset < ekey->offset + left_len) {
4543 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4544 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004545 if (right_type != BTRFS_FILE_EXTENT_REG) {
4546 ret = 0;
4547 goto out;
4548 }
4549
Josef Bacik007d31f2013-10-31 16:49:02 -04004550 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4551 right_len = btrfs_file_extent_num_bytes(eb, ei);
4552 right_offset = btrfs_file_extent_offset(eb, ei);
4553 right_gen = btrfs_file_extent_generation(eb, ei);
4554
Alexander Block31db9f72012-07-25 23:19:24 +02004555 /*
4556 * Are we at extent 8? If yes, we know the extent is changed.
4557 * This may only happen on the first iteration.
4558 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02004559 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004560 /* If we're a hole just pretend nothing changed */
4561 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004562 goto out;
4563 }
4564
4565 left_offset_fixed = left_offset;
4566 if (key.offset < ekey->offset) {
4567 /* Fix the right offset for 2a and 7. */
4568 right_offset += ekey->offset - key.offset;
4569 } else {
4570 /* Fix the left offset for all behind 2a and 2b */
4571 left_offset_fixed += key.offset - ekey->offset;
4572 }
4573
4574 /*
4575 * Check if we have the same extent.
4576 */
Alexander Block39540962012-08-01 12:46:05 +02004577 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04004578 left_offset_fixed != right_offset ||
4579 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02004580 ret = 0;
4581 goto out;
4582 }
4583
4584 /*
4585 * Go to the next extent.
4586 */
4587 ret = btrfs_next_item(sctx->parent_root, path);
4588 if (ret < 0)
4589 goto out;
4590 if (!ret) {
4591 eb = path->nodes[0];
4592 slot = path->slots[0];
4593 btrfs_item_key_to_cpu(eb, &found_key, slot);
4594 }
4595 if (ret || found_key.objectid != key.objectid ||
4596 found_key.type != key.type) {
4597 key.offset += right_len;
4598 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00004599 }
4600 if (found_key.offset != key.offset + right_len) {
4601 ret = 0;
4602 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004603 }
4604 key = found_key;
4605 }
4606
4607 /*
4608 * We're now behind the left extent (treat as unchanged) or at the end
4609 * of the right side (treat as changed).
4610 */
4611 if (key.offset >= ekey->offset + left_len)
4612 ret = 1;
4613 else
4614 ret = 0;
4615
4616
4617out:
4618 btrfs_free_path(path);
4619 return ret;
4620}
4621
Josef Bacik16e75492013-10-22 12:18:51 -04004622static int get_last_extent(struct send_ctx *sctx, u64 offset)
4623{
4624 struct btrfs_path *path;
4625 struct btrfs_root *root = sctx->send_root;
4626 struct btrfs_file_extent_item *fi;
4627 struct btrfs_key key;
4628 u64 extent_end;
4629 u8 type;
4630 int ret;
4631
4632 path = alloc_path_for_send();
4633 if (!path)
4634 return -ENOMEM;
4635
4636 sctx->cur_inode_last_extent = 0;
4637
4638 key.objectid = sctx->cur_ino;
4639 key.type = BTRFS_EXTENT_DATA_KEY;
4640 key.offset = offset;
4641 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4642 if (ret < 0)
4643 goto out;
4644 ret = 0;
4645 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4646 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4647 goto out;
4648
4649 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4650 struct btrfs_file_extent_item);
4651 type = btrfs_file_extent_type(path->nodes[0], fi);
4652 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004653 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4654 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004655 extent_end = ALIGN(key.offset + size,
4656 sctx->send_root->sectorsize);
4657 } else {
4658 extent_end = key.offset +
4659 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4660 }
4661 sctx->cur_inode_last_extent = extent_end;
4662out:
4663 btrfs_free_path(path);
4664 return ret;
4665}
4666
4667static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4668 struct btrfs_key *key)
4669{
4670 struct btrfs_file_extent_item *fi;
4671 u64 extent_end;
4672 u8 type;
4673 int ret = 0;
4674
4675 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4676 return 0;
4677
4678 if (sctx->cur_inode_last_extent == (u64)-1) {
4679 ret = get_last_extent(sctx, key->offset - 1);
4680 if (ret)
4681 return ret;
4682 }
4683
4684 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4685 struct btrfs_file_extent_item);
4686 type = btrfs_file_extent_type(path->nodes[0], fi);
4687 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004688 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4689 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004690 extent_end = ALIGN(key->offset + size,
4691 sctx->send_root->sectorsize);
4692 } else {
4693 extent_end = key->offset +
4694 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4695 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00004696
4697 if (path->slots[0] == 0 &&
4698 sctx->cur_inode_last_extent < key->offset) {
4699 /*
4700 * We might have skipped entire leafs that contained only
4701 * file extent items for our current inode. These leafs have
4702 * a generation number smaller (older) than the one in the
4703 * current leaf and the leaf our last extent came from, and
4704 * are located between these 2 leafs.
4705 */
4706 ret = get_last_extent(sctx, key->offset - 1);
4707 if (ret)
4708 return ret;
4709 }
4710
Josef Bacik16e75492013-10-22 12:18:51 -04004711 if (sctx->cur_inode_last_extent < key->offset)
4712 ret = send_hole(sctx, key->offset);
4713 sctx->cur_inode_last_extent = extent_end;
4714 return ret;
4715}
4716
Alexander Block31db9f72012-07-25 23:19:24 +02004717static int process_extent(struct send_ctx *sctx,
4718 struct btrfs_path *path,
4719 struct btrfs_key *key)
4720{
Alexander Block31db9f72012-07-25 23:19:24 +02004721 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004722 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004723
4724 if (S_ISLNK(sctx->cur_inode_mode))
4725 return 0;
4726
4727 if (sctx->parent_root && !sctx->cur_inode_new) {
4728 ret = is_extent_unchanged(sctx, path, key);
4729 if (ret < 0)
4730 goto out;
4731 if (ret) {
4732 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004733 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004734 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004735 } else {
4736 struct btrfs_file_extent_item *ei;
4737 u8 type;
4738
4739 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4740 struct btrfs_file_extent_item);
4741 type = btrfs_file_extent_type(path->nodes[0], ei);
4742 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4743 type == BTRFS_FILE_EXTENT_REG) {
4744 /*
4745 * The send spec does not have a prealloc command yet,
4746 * so just leave a hole for prealloc'ed extents until
4747 * we have enough commands queued up to justify rev'ing
4748 * the send spec.
4749 */
4750 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4751 ret = 0;
4752 goto out;
4753 }
4754
4755 /* Have a hole, just skip it. */
4756 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4757 ret = 0;
4758 goto out;
4759 }
4760 }
Alexander Block31db9f72012-07-25 23:19:24 +02004761 }
4762
4763 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4764 sctx->cur_inode_size, &found_clone);
4765 if (ret != -ENOENT && ret < 0)
4766 goto out;
4767
4768 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004769 if (ret)
4770 goto out;
4771out_hole:
4772 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004773out:
4774 return ret;
4775}
4776
4777static int process_all_extents(struct send_ctx *sctx)
4778{
4779 int ret;
4780 struct btrfs_root *root;
4781 struct btrfs_path *path;
4782 struct btrfs_key key;
4783 struct btrfs_key found_key;
4784 struct extent_buffer *eb;
4785 int slot;
4786
4787 root = sctx->send_root;
4788 path = alloc_path_for_send();
4789 if (!path)
4790 return -ENOMEM;
4791
4792 key.objectid = sctx->cmp_key->objectid;
4793 key.type = BTRFS_EXTENT_DATA_KEY;
4794 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004795 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4796 if (ret < 0)
4797 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004798
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004799 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004800 eb = path->nodes[0];
4801 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004802
4803 if (slot >= btrfs_header_nritems(eb)) {
4804 ret = btrfs_next_leaf(root, path);
4805 if (ret < 0) {
4806 goto out;
4807 } else if (ret > 0) {
4808 ret = 0;
4809 break;
4810 }
4811 continue;
4812 }
4813
Alexander Block31db9f72012-07-25 23:19:24 +02004814 btrfs_item_key_to_cpu(eb, &found_key, slot);
4815
4816 if (found_key.objectid != key.objectid ||
4817 found_key.type != key.type) {
4818 ret = 0;
4819 goto out;
4820 }
4821
4822 ret = process_extent(sctx, path, &found_key);
4823 if (ret < 0)
4824 goto out;
4825
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004826 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004827 }
4828
4829out:
4830 btrfs_free_path(path);
4831 return ret;
4832}
4833
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004834static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4835 int *pending_move,
4836 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02004837{
4838 int ret = 0;
4839
4840 if (sctx->cur_ino == 0)
4841 goto out;
4842 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004843 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004844 goto out;
4845 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4846 goto out;
4847
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004848 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004849 if (ret < 0)
4850 goto out;
4851
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004852 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004853out:
4854 return ret;
4855}
4856
4857static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4858{
4859 int ret = 0;
4860 u64 left_mode;
4861 u64 left_uid;
4862 u64 left_gid;
4863 u64 right_mode;
4864 u64 right_uid;
4865 u64 right_gid;
4866 int need_chmod = 0;
4867 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004868 int pending_move = 0;
4869 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004870
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004871 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4872 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02004873 if (ret < 0)
4874 goto out;
4875
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004876 /*
4877 * We have processed the refs and thus need to advance send_progress.
4878 * Now, calls to get_cur_xxx will take the updated refs of the current
4879 * inode into account.
4880 *
4881 * On the other hand, if our current inode is a directory and couldn't
4882 * be moved/renamed because its parent was renamed/moved too and it has
4883 * a higher inode number, we can only move/rename our current inode
4884 * after we moved/renamed its parent. Therefore in this case operate on
4885 * the old path (pre move/rename) of our current inode, and the
4886 * move/rename will be performed later.
4887 */
4888 if (refs_processed && !pending_move)
4889 sctx->send_progress = sctx->cur_ino + 1;
4890
Alexander Block31db9f72012-07-25 23:19:24 +02004891 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4892 goto out;
4893 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4894 goto out;
4895
4896 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004897 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004898 if (ret < 0)
4899 goto out;
4900
Alex Lyakase2d044f2012-10-17 13:52:47 +00004901 if (!sctx->parent_root || sctx->cur_inode_new) {
4902 need_chown = 1;
4903 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004904 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004905 } else {
4906 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4907 NULL, NULL, &right_mode, &right_uid,
4908 &right_gid, NULL);
4909 if (ret < 0)
4910 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004911
Alex Lyakase2d044f2012-10-17 13:52:47 +00004912 if (left_uid != right_uid || left_gid != right_gid)
4913 need_chown = 1;
4914 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4915 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004916 }
4917
4918 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04004919 if (need_send_hole(sctx)) {
4920 if (sctx->cur_inode_last_extent == (u64)-1) {
4921 ret = get_last_extent(sctx, (u64)-1);
4922 if (ret)
4923 goto out;
4924 }
4925 if (sctx->cur_inode_last_extent <
4926 sctx->cur_inode_size) {
4927 ret = send_hole(sctx, sctx->cur_inode_size);
4928 if (ret)
4929 goto out;
4930 }
4931 }
Alexander Block31db9f72012-07-25 23:19:24 +02004932 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4933 sctx->cur_inode_size);
4934 if (ret < 0)
4935 goto out;
4936 }
4937
4938 if (need_chown) {
4939 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4940 left_uid, left_gid);
4941 if (ret < 0)
4942 goto out;
4943 }
4944 if (need_chmod) {
4945 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4946 left_mode);
4947 if (ret < 0)
4948 goto out;
4949 }
4950
4951 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004952 * If other directory inodes depended on our current directory
4953 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02004954 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004955 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
4956 ret = apply_children_dir_moves(sctx);
4957 if (ret)
4958 goto out;
4959 }
4960
4961 /*
4962 * Need to send that every time, no matter if it actually
4963 * changed between the two trees as we have done changes to
4964 * the inode before.
4965 */
4966 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004967 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4968 if (ret < 0)
4969 goto out;
4970
4971out:
4972 return ret;
4973}
4974
4975static int changed_inode(struct send_ctx *sctx,
4976 enum btrfs_compare_tree_result result)
4977{
4978 int ret = 0;
4979 struct btrfs_key *key = sctx->cmp_key;
4980 struct btrfs_inode_item *left_ii = NULL;
4981 struct btrfs_inode_item *right_ii = NULL;
4982 u64 left_gen = 0;
4983 u64 right_gen = 0;
4984
Alexander Block31db9f72012-07-25 23:19:24 +02004985 sctx->cur_ino = key->objectid;
4986 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004987 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004988
4989 /*
4990 * Set send_progress to current inode. This will tell all get_cur_xxx
4991 * functions that the current inode's refs are not updated yet. Later,
4992 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4993 */
Alexander Block31db9f72012-07-25 23:19:24 +02004994 sctx->send_progress = sctx->cur_ino;
4995
4996 if (result == BTRFS_COMPARE_TREE_NEW ||
4997 result == BTRFS_COMPARE_TREE_CHANGED) {
4998 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4999 sctx->left_path->slots[0],
5000 struct btrfs_inode_item);
5001 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5002 left_ii);
5003 } else {
5004 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5005 sctx->right_path->slots[0],
5006 struct btrfs_inode_item);
5007 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5008 right_ii);
5009 }
5010 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5011 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5012 sctx->right_path->slots[0],
5013 struct btrfs_inode_item);
5014
5015 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5016 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005017
5018 /*
5019 * The cur_ino = root dir case is special here. We can't treat
5020 * the inode as deleted+reused because it would generate a
5021 * stream that tries to delete/mkdir the root dir.
5022 */
5023 if (left_gen != right_gen &&
5024 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005025 sctx->cur_inode_new_gen = 1;
5026 }
5027
5028 if (result == BTRFS_COMPARE_TREE_NEW) {
5029 sctx->cur_inode_gen = left_gen;
5030 sctx->cur_inode_new = 1;
5031 sctx->cur_inode_deleted = 0;
5032 sctx->cur_inode_size = btrfs_inode_size(
5033 sctx->left_path->nodes[0], left_ii);
5034 sctx->cur_inode_mode = btrfs_inode_mode(
5035 sctx->left_path->nodes[0], left_ii);
5036 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005037 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005038 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5039 sctx->cur_inode_gen = right_gen;
5040 sctx->cur_inode_new = 0;
5041 sctx->cur_inode_deleted = 1;
5042 sctx->cur_inode_size = btrfs_inode_size(
5043 sctx->right_path->nodes[0], right_ii);
5044 sctx->cur_inode_mode = btrfs_inode_mode(
5045 sctx->right_path->nodes[0], right_ii);
5046 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005047 /*
5048 * We need to do some special handling in case the inode was
5049 * reported as changed with a changed generation number. This
5050 * means that the original inode was deleted and new inode
5051 * reused the same inum. So we have to treat the old inode as
5052 * deleted and the new one as new.
5053 */
Alexander Block31db9f72012-07-25 23:19:24 +02005054 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005055 /*
5056 * First, process the inode as if it was deleted.
5057 */
Alexander Block31db9f72012-07-25 23:19:24 +02005058 sctx->cur_inode_gen = right_gen;
5059 sctx->cur_inode_new = 0;
5060 sctx->cur_inode_deleted = 1;
5061 sctx->cur_inode_size = btrfs_inode_size(
5062 sctx->right_path->nodes[0], right_ii);
5063 sctx->cur_inode_mode = btrfs_inode_mode(
5064 sctx->right_path->nodes[0], right_ii);
5065 ret = process_all_refs(sctx,
5066 BTRFS_COMPARE_TREE_DELETED);
5067 if (ret < 0)
5068 goto out;
5069
Alexander Block766702e2012-07-28 14:11:31 +02005070 /*
5071 * Now process the inode as if it was new.
5072 */
Alexander Block31db9f72012-07-25 23:19:24 +02005073 sctx->cur_inode_gen = left_gen;
5074 sctx->cur_inode_new = 1;
5075 sctx->cur_inode_deleted = 0;
5076 sctx->cur_inode_size = btrfs_inode_size(
5077 sctx->left_path->nodes[0], left_ii);
5078 sctx->cur_inode_mode = btrfs_inode_mode(
5079 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005080 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005081 if (ret < 0)
5082 goto out;
5083
5084 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5085 if (ret < 0)
5086 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005087 /*
5088 * Advance send_progress now as we did not get into
5089 * process_recorded_refs_if_needed in the new_gen case.
5090 */
5091 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005092
5093 /*
5094 * Now process all extents and xattrs of the inode as if
5095 * they were all new.
5096 */
Alexander Block31db9f72012-07-25 23:19:24 +02005097 ret = process_all_extents(sctx);
5098 if (ret < 0)
5099 goto out;
5100 ret = process_all_new_xattrs(sctx);
5101 if (ret < 0)
5102 goto out;
5103 } else {
5104 sctx->cur_inode_gen = left_gen;
5105 sctx->cur_inode_new = 0;
5106 sctx->cur_inode_new_gen = 0;
5107 sctx->cur_inode_deleted = 0;
5108 sctx->cur_inode_size = btrfs_inode_size(
5109 sctx->left_path->nodes[0], left_ii);
5110 sctx->cur_inode_mode = btrfs_inode_mode(
5111 sctx->left_path->nodes[0], left_ii);
5112 }
5113 }
5114
5115out:
5116 return ret;
5117}
5118
Alexander Block766702e2012-07-28 14:11:31 +02005119/*
5120 * We have to process new refs before deleted refs, but compare_trees gives us
5121 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5122 * first and later process them in process_recorded_refs.
5123 * For the cur_inode_new_gen case, we skip recording completely because
5124 * changed_inode did already initiate processing of refs. The reason for this is
5125 * that in this case, compare_tree actually compares the refs of 2 different
5126 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5127 * refs of the right tree as deleted and all refs of the left tree as new.
5128 */
Alexander Block31db9f72012-07-25 23:19:24 +02005129static int changed_ref(struct send_ctx *sctx,
5130 enum btrfs_compare_tree_result result)
5131{
5132 int ret = 0;
5133
5134 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5135
5136 if (!sctx->cur_inode_new_gen &&
5137 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5138 if (result == BTRFS_COMPARE_TREE_NEW)
5139 ret = record_new_ref(sctx);
5140 else if (result == BTRFS_COMPARE_TREE_DELETED)
5141 ret = record_deleted_ref(sctx);
5142 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5143 ret = record_changed_ref(sctx);
5144 }
5145
5146 return ret;
5147}
5148
Alexander Block766702e2012-07-28 14:11:31 +02005149/*
5150 * Process new/deleted/changed xattrs. We skip processing in the
5151 * cur_inode_new_gen case because changed_inode did already initiate processing
5152 * of xattrs. The reason is the same as in changed_ref
5153 */
Alexander Block31db9f72012-07-25 23:19:24 +02005154static int changed_xattr(struct send_ctx *sctx,
5155 enum btrfs_compare_tree_result result)
5156{
5157 int ret = 0;
5158
5159 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5160
5161 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5162 if (result == BTRFS_COMPARE_TREE_NEW)
5163 ret = process_new_xattr(sctx);
5164 else if (result == BTRFS_COMPARE_TREE_DELETED)
5165 ret = process_deleted_xattr(sctx);
5166 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5167 ret = process_changed_xattr(sctx);
5168 }
5169
5170 return ret;
5171}
5172
Alexander Block766702e2012-07-28 14:11:31 +02005173/*
5174 * Process new/deleted/changed extents. We skip processing in the
5175 * cur_inode_new_gen case because changed_inode did already initiate processing
5176 * of extents. The reason is the same as in changed_ref
5177 */
Alexander Block31db9f72012-07-25 23:19:24 +02005178static int changed_extent(struct send_ctx *sctx,
5179 enum btrfs_compare_tree_result result)
5180{
5181 int ret = 0;
5182
5183 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5184
5185 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5186 if (result != BTRFS_COMPARE_TREE_DELETED)
5187 ret = process_extent(sctx, sctx->left_path,
5188 sctx->cmp_key);
5189 }
5190
5191 return ret;
5192}
5193
Josef Bacikba5e8f22013-08-16 16:52:55 -04005194static int dir_changed(struct send_ctx *sctx, u64 dir)
5195{
5196 u64 orig_gen, new_gen;
5197 int ret;
5198
5199 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5200 NULL, NULL);
5201 if (ret)
5202 return ret;
5203
5204 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5205 NULL, NULL, NULL);
5206 if (ret)
5207 return ret;
5208
5209 return (orig_gen != new_gen) ? 1 : 0;
5210}
5211
5212static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5213 struct btrfs_key *key)
5214{
5215 struct btrfs_inode_extref *extref;
5216 struct extent_buffer *leaf;
5217 u64 dirid = 0, last_dirid = 0;
5218 unsigned long ptr;
5219 u32 item_size;
5220 u32 cur_offset = 0;
5221 int ref_name_len;
5222 int ret = 0;
5223
5224 /* Easy case, just check this one dirid */
5225 if (key->type == BTRFS_INODE_REF_KEY) {
5226 dirid = key->offset;
5227
5228 ret = dir_changed(sctx, dirid);
5229 goto out;
5230 }
5231
5232 leaf = path->nodes[0];
5233 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5234 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5235 while (cur_offset < item_size) {
5236 extref = (struct btrfs_inode_extref *)(ptr +
5237 cur_offset);
5238 dirid = btrfs_inode_extref_parent(leaf, extref);
5239 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5240 cur_offset += ref_name_len + sizeof(*extref);
5241 if (dirid == last_dirid)
5242 continue;
5243 ret = dir_changed(sctx, dirid);
5244 if (ret)
5245 break;
5246 last_dirid = dirid;
5247 }
5248out:
5249 return ret;
5250}
5251
Alexander Block766702e2012-07-28 14:11:31 +02005252/*
5253 * Updates compare related fields in sctx and simply forwards to the actual
5254 * changed_xxx functions.
5255 */
Alexander Block31db9f72012-07-25 23:19:24 +02005256static int changed_cb(struct btrfs_root *left_root,
5257 struct btrfs_root *right_root,
5258 struct btrfs_path *left_path,
5259 struct btrfs_path *right_path,
5260 struct btrfs_key *key,
5261 enum btrfs_compare_tree_result result,
5262 void *ctx)
5263{
5264 int ret = 0;
5265 struct send_ctx *sctx = ctx;
5266
Josef Bacikba5e8f22013-08-16 16:52:55 -04005267 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005268 if (key->type == BTRFS_INODE_REF_KEY ||
5269 key->type == BTRFS_INODE_EXTREF_KEY) {
5270 ret = compare_refs(sctx, left_path, key);
5271 if (!ret)
5272 return 0;
5273 if (ret < 0)
5274 return ret;
5275 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5276 return maybe_send_hole(sctx, left_path, key);
5277 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005278 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005279 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005280 result = BTRFS_COMPARE_TREE_CHANGED;
5281 ret = 0;
5282 }
5283
Alexander Block31db9f72012-07-25 23:19:24 +02005284 sctx->left_path = left_path;
5285 sctx->right_path = right_path;
5286 sctx->cmp_key = key;
5287
5288 ret = finish_inode_if_needed(sctx, 0);
5289 if (ret < 0)
5290 goto out;
5291
Alexander Block2981e222012-08-01 14:47:03 +02005292 /* Ignore non-FS objects */
5293 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5294 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5295 goto out;
5296
Alexander Block31db9f72012-07-25 23:19:24 +02005297 if (key->type == BTRFS_INODE_ITEM_KEY)
5298 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005299 else if (key->type == BTRFS_INODE_REF_KEY ||
5300 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005301 ret = changed_ref(sctx, result);
5302 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5303 ret = changed_xattr(sctx, result);
5304 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5305 ret = changed_extent(sctx, result);
5306
5307out:
5308 return ret;
5309}
5310
5311static int full_send_tree(struct send_ctx *sctx)
5312{
5313 int ret;
Wang Shilongdcfd5ad2014-02-08 23:46:36 +08005314 struct btrfs_trans_handle *trans = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02005315 struct btrfs_root *send_root = sctx->send_root;
5316 struct btrfs_key key;
5317 struct btrfs_key found_key;
5318 struct btrfs_path *path;
5319 struct extent_buffer *eb;
5320 int slot;
5321 u64 start_ctransid;
5322 u64 ctransid;
5323
5324 path = alloc_path_for_send();
5325 if (!path)
5326 return -ENOMEM;
5327
Anand Jain5f3ab902012-12-07 09:28:54 +00005328 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005329 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005330 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005331
5332 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5333 key.type = BTRFS_INODE_ITEM_KEY;
5334 key.offset = 0;
5335
Wang Shilongdcfd5ad2014-02-08 23:46:36 +08005336join_trans:
5337 /*
5338 * We need to make sure the transaction does not get committed
5339 * while we do anything on commit roots. Join a transaction to prevent
5340 * this.
5341 */
5342 trans = btrfs_join_transaction(send_root);
5343 if (IS_ERR(trans)) {
5344 ret = PTR_ERR(trans);
5345 trans = NULL;
5346 goto out;
5347 }
5348
Alexander Block31db9f72012-07-25 23:19:24 +02005349 /*
Alexander Block766702e2012-07-28 14:11:31 +02005350 * Make sure the tree has not changed after re-joining. We detect this
5351 * by comparing start_ctransid and ctransid. They should always match.
Alexander Block31db9f72012-07-25 23:19:24 +02005352 */
Anand Jain5f3ab902012-12-07 09:28:54 +00005353 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005354 ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005355 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005356
5357 if (ctransid != start_ctransid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005358 WARN(1, KERN_WARNING "BTRFS: the root that you're trying to "
Alexander Block31db9f72012-07-25 23:19:24 +02005359 "send was modified in between. This is "
5360 "probably a bug.\n");
5361 ret = -EIO;
5362 goto out;
5363 }
5364
5365 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5366 if (ret < 0)
5367 goto out;
5368 if (ret)
5369 goto out_finish;
5370
5371 while (1) {
Wang Shilongdcfd5ad2014-02-08 23:46:36 +08005372 /*
5373 * When someone want to commit while we iterate, end the
5374 * joined transaction and rejoin.
5375 */
5376 if (btrfs_should_end_transaction(trans, send_root)) {
5377 ret = btrfs_end_transaction(trans, send_root);
5378 trans = NULL;
5379 if (ret < 0)
5380 goto out;
5381 btrfs_release_path(path);
5382 goto join_trans;
5383 }
5384
Alexander Block31db9f72012-07-25 23:19:24 +02005385 eb = path->nodes[0];
5386 slot = path->slots[0];
5387 btrfs_item_key_to_cpu(eb, &found_key, slot);
5388
5389 ret = changed_cb(send_root, NULL, path, NULL,
5390 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5391 if (ret < 0)
5392 goto out;
5393
5394 key.objectid = found_key.objectid;
5395 key.type = found_key.type;
5396 key.offset = found_key.offset + 1;
5397
5398 ret = btrfs_next_item(send_root, path);
5399 if (ret < 0)
5400 goto out;
5401 if (ret) {
5402 ret = 0;
5403 break;
5404 }
5405 }
5406
5407out_finish:
5408 ret = finish_inode_if_needed(sctx, 1);
5409
5410out:
5411 btrfs_free_path(path);
Wang Shilongdcfd5ad2014-02-08 23:46:36 +08005412 if (trans) {
5413 if (!ret)
5414 ret = btrfs_end_transaction(trans, send_root);
5415 else
5416 btrfs_end_transaction(trans, send_root);
5417 }
Alexander Block31db9f72012-07-25 23:19:24 +02005418 return ret;
5419}
5420
5421static int send_subvol(struct send_ctx *sctx)
5422{
5423 int ret;
5424
Stefan Behrensc2c71322013-04-10 17:10:52 +00005425 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5426 ret = send_header(sctx);
5427 if (ret < 0)
5428 goto out;
5429 }
Alexander Block31db9f72012-07-25 23:19:24 +02005430
5431 ret = send_subvol_begin(sctx);
5432 if (ret < 0)
5433 goto out;
5434
5435 if (sctx->parent_root) {
5436 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5437 changed_cb, sctx);
5438 if (ret < 0)
5439 goto out;
5440 ret = finish_inode_if_needed(sctx, 1);
5441 if (ret < 0)
5442 goto out;
5443 } else {
5444 ret = full_send_tree(sctx);
5445 if (ret < 0)
5446 goto out;
5447 }
5448
5449out:
Alexander Block31db9f72012-07-25 23:19:24 +02005450 free_recorded_refs(sctx);
5451 return ret;
5452}
5453
David Sterba66ef7d62013-12-17 15:07:20 +01005454static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5455{
5456 spin_lock(&root->root_item_lock);
5457 root->send_in_progress--;
5458 /*
5459 * Not much left to do, we don't know why it's unbalanced and
5460 * can't blindly reset it to 0.
5461 */
5462 if (root->send_in_progress < 0)
5463 btrfs_err(root->fs_info,
5464 "send_in_progres unbalanced %d root %llu\n",
5465 root->send_in_progress, root->root_key.objectid);
5466 spin_unlock(&root->root_item_lock);
5467}
5468
Alexander Block31db9f72012-07-25 23:19:24 +02005469long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5470{
5471 int ret = 0;
5472 struct btrfs_root *send_root;
5473 struct btrfs_root *clone_root;
5474 struct btrfs_fs_info *fs_info;
5475 struct btrfs_ioctl_send_args *arg = NULL;
5476 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02005477 struct send_ctx *sctx = NULL;
5478 u32 i;
5479 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01005480 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08005481 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08005482 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02005483
5484 if (!capable(CAP_SYS_ADMIN))
5485 return -EPERM;
5486
Al Viro496ad9a2013-01-23 17:07:38 -05005487 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02005488 fs_info = send_root->fs_info;
5489
Josef Bacik139f8072013-05-20 11:26:50 -04005490 /*
David Sterba2c686532013-12-16 17:34:17 +01005491 * The subvolume must remain read-only during send, protect against
5492 * making it RW.
5493 */
5494 spin_lock(&send_root->root_item_lock);
5495 send_root->send_in_progress++;
5496 spin_unlock(&send_root->root_item_lock);
5497
5498 /*
Josef Bacik139f8072013-05-20 11:26:50 -04005499 * This is done when we lookup the root, it should already be complete
5500 * by the time we get here.
5501 */
5502 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5503
5504 /*
David Sterba2c686532013-12-16 17:34:17 +01005505 * Userspace tools do the checks and warn the user if it's
5506 * not RO.
5507 */
5508 if (!btrfs_root_readonly(send_root)) {
5509 ret = -EPERM;
5510 goto out;
5511 }
5512
Alexander Block31db9f72012-07-25 23:19:24 +02005513 arg = memdup_user(arg_, sizeof(*arg));
5514 if (IS_ERR(arg)) {
5515 ret = PTR_ERR(arg);
5516 arg = NULL;
5517 goto out;
5518 }
5519
5520 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05005521 sizeof(*arg->clone_sources) *
5522 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02005523 ret = -EFAULT;
5524 goto out;
5525 }
5526
Stefan Behrensc2c71322013-04-10 17:10:52 +00005527 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005528 ret = -EINVAL;
5529 goto out;
5530 }
5531
Alexander Block31db9f72012-07-25 23:19:24 +02005532 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5533 if (!sctx) {
5534 ret = -ENOMEM;
5535 goto out;
5536 }
5537
5538 INIT_LIST_HEAD(&sctx->new_refs);
5539 INIT_LIST_HEAD(&sctx->deleted_refs);
5540 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5541 INIT_LIST_HEAD(&sctx->name_cache_list);
5542
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005543 sctx->flags = arg->flags;
5544
Alexander Block31db9f72012-07-25 23:19:24 +02005545 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00005546 if (!sctx->send_filp) {
5547 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02005548 goto out;
5549 }
5550
Alexander Block31db9f72012-07-25 23:19:24 +02005551 sctx->send_root = send_root;
5552 sctx->clone_roots_cnt = arg->clone_sources_count;
5553
5554 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5555 sctx->send_buf = vmalloc(sctx->send_max_size);
5556 if (!sctx->send_buf) {
5557 ret = -ENOMEM;
5558 goto out;
5559 }
5560
5561 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5562 if (!sctx->read_buf) {
5563 ret = -ENOMEM;
5564 goto out;
5565 }
5566
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005567 sctx->pending_dir_moves = RB_ROOT;
5568 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00005569 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005570
Alexander Block31db9f72012-07-25 23:19:24 +02005571 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5572 (arg->clone_sources_count + 1));
5573 if (!sctx->clone_roots) {
5574 ret = -ENOMEM;
5575 goto out;
5576 }
5577
5578 if (arg->clone_sources_count) {
5579 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5580 sizeof(*arg->clone_sources));
5581 if (!clone_sources_tmp) {
5582 ret = -ENOMEM;
5583 goto out;
5584 }
5585
5586 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5587 arg->clone_sources_count *
5588 sizeof(*arg->clone_sources));
5589 if (ret) {
5590 ret = -EFAULT;
5591 goto out;
5592 }
5593
5594 for (i = 0; i < arg->clone_sources_count; i++) {
5595 key.objectid = clone_sources_tmp[i];
5596 key.type = BTRFS_ROOT_ITEM_KEY;
5597 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005598
5599 index = srcu_read_lock(&fs_info->subvol_srcu);
5600
Alexander Block31db9f72012-07-25 23:19:24 +02005601 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02005602 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005603 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005604 ret = PTR_ERR(clone_root);
5605 goto out;
5606 }
David Sterba2c686532013-12-16 17:34:17 +01005607 clone_sources_to_rollback = i + 1;
5608 spin_lock(&clone_root->root_item_lock);
5609 clone_root->send_in_progress++;
5610 if (!btrfs_root_readonly(clone_root)) {
5611 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005612 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005613 ret = -EPERM;
5614 goto out;
5615 }
5616 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005617 srcu_read_unlock(&fs_info->subvol_srcu, index);
5618
Alexander Block31db9f72012-07-25 23:19:24 +02005619 sctx->clone_roots[i].root = clone_root;
5620 }
5621 vfree(clone_sources_tmp);
5622 clone_sources_tmp = NULL;
5623 }
5624
5625 if (arg->parent_root) {
5626 key.objectid = arg->parent_root;
5627 key.type = BTRFS_ROOT_ITEM_KEY;
5628 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005629
5630 index = srcu_read_lock(&fs_info->subvol_srcu);
5631
Alexander Block31db9f72012-07-25 23:19:24 +02005632 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005633 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005634 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005635 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02005636 goto out;
5637 }
Wang Shilong18f687d2014-01-07 17:25:19 +08005638
David Sterba2c686532013-12-16 17:34:17 +01005639 spin_lock(&sctx->parent_root->root_item_lock);
5640 sctx->parent_root->send_in_progress++;
5641 if (!btrfs_root_readonly(sctx->parent_root)) {
5642 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005643 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005644 ret = -EPERM;
5645 goto out;
5646 }
5647 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005648
5649 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005650 }
5651
5652 /*
5653 * Clones from send_root are allowed, but only if the clone source
5654 * is behind the current send position. This is checked while searching
5655 * for possible clone sources.
5656 */
5657 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5658
5659 /* We do a bsearch later */
5660 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5661 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5662 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08005663 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005664
5665 ret = send_subvol(sctx);
5666 if (ret < 0)
5667 goto out;
5668
Stefan Behrensc2c71322013-04-10 17:10:52 +00005669 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5670 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5671 if (ret < 0)
5672 goto out;
5673 ret = send_cmd(sctx);
5674 if (ret < 0)
5675 goto out;
5676 }
Alexander Block31db9f72012-07-25 23:19:24 +02005677
5678out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005679 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5680 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5681 struct rb_node *n;
5682 struct pending_dir_move *pm;
5683
5684 n = rb_first(&sctx->pending_dir_moves);
5685 pm = rb_entry(n, struct pending_dir_move, node);
5686 while (!list_empty(&pm->list)) {
5687 struct pending_dir_move *pm2;
5688
5689 pm2 = list_first_entry(&pm->list,
5690 struct pending_dir_move, list);
5691 free_pending_move(sctx, pm2);
5692 }
5693 free_pending_move(sctx, pm);
5694 }
5695
5696 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5697 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5698 struct rb_node *n;
5699 struct waiting_dir_move *dm;
5700
5701 n = rb_first(&sctx->waiting_dir_moves);
5702 dm = rb_entry(n, struct waiting_dir_move, node);
5703 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5704 kfree(dm);
5705 }
5706
Filipe Manana9dc44212014-02-19 14:31:44 +00005707 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5708 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5709 struct rb_node *n;
5710 struct orphan_dir_info *odi;
5711
5712 n = rb_first(&sctx->orphan_dirs);
5713 odi = rb_entry(n, struct orphan_dir_info, node);
5714 free_orphan_dir_info(sctx, odi);
5715 }
5716
Wang Shilong896c14f2014-01-07 17:25:18 +08005717 if (sort_clone_roots) {
5718 for (i = 0; i < sctx->clone_roots_cnt; i++)
5719 btrfs_root_dec_send_in_progress(
5720 sctx->clone_roots[i].root);
5721 } else {
5722 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5723 btrfs_root_dec_send_in_progress(
5724 sctx->clone_roots[i].root);
5725
5726 btrfs_root_dec_send_in_progress(send_root);
5727 }
David Sterba66ef7d62013-12-17 15:07:20 +01005728 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5729 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01005730
Alexander Block31db9f72012-07-25 23:19:24 +02005731 kfree(arg);
5732 vfree(clone_sources_tmp);
5733
5734 if (sctx) {
5735 if (sctx->send_filp)
5736 fput(sctx->send_filp);
5737
5738 vfree(sctx->clone_roots);
5739 vfree(sctx->send_buf);
5740 vfree(sctx->read_buf);
5741
5742 name_cache_free(sctx);
5743
5744 kfree(sctx);
5745 }
5746
5747 return ret;
5748}