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