blob: 6b5f13659317b100c4f3126a24390026839d301b [file] [log] [blame]
Alexander Block31db9f72012-07-25 23:19:24 +02001/*
2 * Copyright (C) 2012 Alexander Block. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/bsearch.h>
20#include <linux/fs.h>
21#include <linux/file.h>
22#include <linux/sort.h>
23#include <linux/mount.h>
24#include <linux/xattr.h>
25#include <linux/posix_acl_xattr.h>
26#include <linux/radix-tree.h>
Stephen Rothwella1857eb2012-07-27 10:11:13 +100027#include <linux/vmalloc.h>
Andy Shevchenkoed848852013-08-21 10:32:13 +030028#include <linux/string.h>
Alexander Block31db9f72012-07-25 23:19:24 +020029
30#include "send.h"
31#include "backref.h"
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +000032#include "hash.h"
Alexander Block31db9f72012-07-25 23:19:24 +020033#include "locking.h"
34#include "disk-io.h"
35#include "btrfs_inode.h"
36#include "transaction.h"
37
38static int g_verbose = 0;
39
40#define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
41
42/*
43 * A fs_path is a helper to dynamically build path names with unknown size.
44 * It reallocates the internal buffer on demand.
45 * It allows fast adding of path elements on the right side (normal path) and
46 * fast adding to the left side (reversed path). A reversed path can also be
47 * unreversed if needed.
48 */
49struct fs_path {
50 union {
51 struct {
52 char *start;
53 char *end;
Alexander Block31db9f72012-07-25 23:19:24 +020054
55 char *buf;
David Sterba1f5a7ff2014-02-03 19:23:47 +010056 unsigned short buf_len:15;
57 unsigned short reversed:1;
Alexander Block31db9f72012-07-25 23:19:24 +020058 char inline_buf[];
59 };
David Sterbaace01052014-02-05 16:17:34 +010060 /*
61 * Average path length does not exceed 200 bytes, we'll have
62 * better packing in the slab and higher chance to satisfy
63 * a allocation later during send.
64 */
65 char pad[256];
Alexander Block31db9f72012-07-25 23:19:24 +020066 };
67};
68#define FS_PATH_INLINE_SIZE \
69 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
70
71
72/* reused for each extent */
73struct clone_root {
74 struct btrfs_root *root;
75 u64 ino;
76 u64 offset;
77
78 u64 found_refs;
79};
80
81#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
82#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
83
84struct send_ctx {
85 struct file *send_filp;
86 loff_t send_off;
87 char *send_buf;
88 u32 send_size;
89 u32 send_max_size;
90 u64 total_send_size;
91 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000092 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020093
Alexander Block31db9f72012-07-25 23:19:24 +020094 struct btrfs_root *send_root;
95 struct btrfs_root *parent_root;
96 struct clone_root *clone_roots;
97 int clone_roots_cnt;
98
99 /* current state of the compare_tree call */
100 struct btrfs_path *left_path;
101 struct btrfs_path *right_path;
102 struct btrfs_key *cmp_key;
103
104 /*
105 * infos of the currently processed inode. In case of deleted inodes,
106 * these are the values from the deleted inode.
107 */
108 u64 cur_ino;
109 u64 cur_inode_gen;
110 int cur_inode_new;
111 int cur_inode_new_gen;
112 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200113 u64 cur_inode_size;
114 u64 cur_inode_mode;
Liu Bo644d1942014-02-27 17:29:01 +0800115 u64 cur_inode_rdev;
Josef Bacik16e75492013-10-22 12:18:51 -0400116 u64 cur_inode_last_extent;
Alexander Block31db9f72012-07-25 23:19:24 +0200117
118 u64 send_progress;
119
120 struct list_head new_refs;
121 struct list_head deleted_refs;
122
123 struct radix_tree_root name_cache;
124 struct list_head name_cache_list;
125 int name_cache_size;
126
Liu Bo2131bcd2014-03-05 10:07:35 +0800127 struct file_ra_state ra;
128
Alexander Block31db9f72012-07-25 23:19:24 +0200129 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000130
131 /*
132 * We process inodes by their increasing order, so if before an
133 * incremental send we reverse the parent/child relationship of
134 * directories such that a directory with a lower inode number was
135 * the parent of a directory with a higher inode number, and the one
136 * becoming the new parent got renamed too, we can't rename/move the
137 * directory with lower inode number when we finish processing it - we
138 * must process the directory with higher inode number first, then
139 * rename/move it and then rename/move the directory with lower inode
140 * number. Example follows.
141 *
142 * Tree state when the first send was performed:
143 *
144 * .
145 * |-- a (ino 257)
146 * |-- b (ino 258)
147 * |
148 * |
149 * |-- c (ino 259)
150 * | |-- d (ino 260)
151 * |
152 * |-- c2 (ino 261)
153 *
154 * Tree state when the second (incremental) send is performed:
155 *
156 * .
157 * |-- a (ino 257)
158 * |-- b (ino 258)
159 * |-- c2 (ino 261)
160 * |-- d2 (ino 260)
161 * |-- cc (ino 259)
162 *
163 * The sequence of steps that lead to the second state was:
164 *
165 * mv /a/b/c/d /a/b/c2/d2
166 * mv /a/b/c /a/b/c2/d2/cc
167 *
168 * "c" has lower inode number, but we can't move it (2nd mv operation)
169 * before we move "d", which has higher inode number.
170 *
171 * So we just memorize which move/rename operations must be performed
172 * later when their respective parent is processed and moved/renamed.
173 */
174
175 /* Indexed by parent directory inode number. */
176 struct rb_root pending_dir_moves;
177
178 /*
179 * Reverse index, indexed by the inode number of a directory that
180 * is waiting for the move/rename of its immediate parent before its
181 * own move/rename can be performed.
182 */
183 struct rb_root waiting_dir_moves;
Filipe Manana9dc44212014-02-19 14:31:44 +0000184
185 /*
186 * A directory that is going to be rm'ed might have a child directory
187 * which is in the pending directory moves index above. In this case,
188 * the directory can only be removed after the move/rename of its child
189 * is performed. Example:
190 *
191 * Parent snapshot:
192 *
193 * . (ino 256)
194 * |-- a/ (ino 257)
195 * |-- b/ (ino 258)
196 * |-- c/ (ino 259)
197 * | |-- x/ (ino 260)
198 * |
199 * |-- y/ (ino 261)
200 *
201 * Send snapshot:
202 *
203 * . (ino 256)
204 * |-- a/ (ino 257)
205 * |-- b/ (ino 258)
206 * |-- YY/ (ino 261)
207 * |-- x/ (ino 260)
208 *
209 * Sequence of steps that lead to the send snapshot:
210 * rm -f /a/b/c/foo.txt
211 * mv /a/b/y /a/b/YY
212 * mv /a/b/c/x /a/b/YY
213 * rmdir /a/b/c
214 *
215 * When the child is processed, its move/rename is delayed until its
216 * parent is processed (as explained above), but all other operations
217 * like update utimes, chown, chgrp, etc, are performed and the paths
218 * that it uses for those operations must use the orphanized name of
219 * its parent (the directory we're going to rm later), so we need to
220 * memorize that name.
221 *
222 * Indexed by the inode number of the directory to be deleted.
223 */
224 struct rb_root orphan_dirs;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000225};
226
227struct pending_dir_move {
228 struct rb_node node;
229 struct list_head list;
230 u64 parent_ino;
231 u64 ino;
232 u64 gen;
233 struct list_head update_refs;
234};
235
236struct waiting_dir_move {
237 struct rb_node node;
238 u64 ino;
Filipe Manana9dc44212014-02-19 14:31:44 +0000239 /*
240 * There might be some directory that could not be removed because it
241 * was waiting for this directory inode to be moved first. Therefore
242 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
243 */
244 u64 rmdir_ino;
245};
246
247struct orphan_dir_info {
248 struct rb_node node;
249 u64 ino;
250 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +0200251};
252
253struct name_cache_entry {
254 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200255 /*
256 * radix_tree has only 32bit entries but we need to handle 64bit inums.
257 * We use the lower 32bit of the 64bit inum to store it in the tree. If
258 * more then one inum would fall into the same entry, we use radix_list
259 * to store the additional entries. radix_list is also used to store
260 * entries where two entries have the same inum but different
261 * generations.
262 */
263 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200264 u64 ino;
265 u64 gen;
266 u64 parent_ino;
267 u64 parent_gen;
268 int ret;
269 int need_later_update;
270 int name_len;
271 char name[];
272};
273
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000274static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
275
Filipe Manana9dc44212014-02-19 14:31:44 +0000276static struct waiting_dir_move *
277get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
278
279static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
280
Josef Bacik16e75492013-10-22 12:18:51 -0400281static int need_send_hole(struct send_ctx *sctx)
282{
283 return (sctx->parent_root && !sctx->cur_inode_new &&
284 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
285 S_ISREG(sctx->cur_inode_mode));
286}
287
Alexander Block31db9f72012-07-25 23:19:24 +0200288static void fs_path_reset(struct fs_path *p)
289{
290 if (p->reversed) {
291 p->start = p->buf + p->buf_len - 1;
292 p->end = p->start;
293 *p->start = 0;
294 } else {
295 p->start = p->buf;
296 p->end = p->start;
297 *p->start = 0;
298 }
299}
300
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000301static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200302{
303 struct fs_path *p;
304
305 p = kmalloc(sizeof(*p), GFP_NOFS);
306 if (!p)
307 return NULL;
308 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200309 p->buf = p->inline_buf;
310 p->buf_len = FS_PATH_INLINE_SIZE;
311 fs_path_reset(p);
312 return p;
313}
314
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000315static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200316{
317 struct fs_path *p;
318
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000319 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200320 if (!p)
321 return NULL;
322 p->reversed = 1;
323 fs_path_reset(p);
324 return p;
325}
326
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000327static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200328{
329 if (!p)
330 return;
David Sterbaace01052014-02-05 16:17:34 +0100331 if (p->buf != p->inline_buf)
332 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200333 kfree(p);
334}
335
336static int fs_path_len(struct fs_path *p)
337{
338 return p->end - p->start;
339}
340
341static int fs_path_ensure_buf(struct fs_path *p, int len)
342{
343 char *tmp_buf;
344 int path_len;
345 int old_buf_len;
346
347 len++;
348
349 if (p->buf_len >= len)
350 return 0;
351
David Sterba1b2782c2014-02-25 19:32:59 +0100352 path_len = p->end - p->start;
353 old_buf_len = p->buf_len;
354
David Sterbaace01052014-02-05 16:17:34 +0100355 /*
356 * First time the inline_buf does not suffice
357 */
David Sterba9c9ca002014-02-25 19:33:08 +0100358 if (p->buf == p->inline_buf)
359 tmp_buf = kmalloc(len, GFP_NOFS);
360 else
361 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
362 if (!tmp_buf)
363 return -ENOMEM;
364 p->buf = tmp_buf;
365 /*
366 * The real size of the buffer is bigger, this will let the fast path
367 * happen most of the time
368 */
369 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100370
Alexander Block31db9f72012-07-25 23:19:24 +0200371 if (p->reversed) {
372 tmp_buf = p->buf + old_buf_len - path_len - 1;
373 p->end = p->buf + p->buf_len - 1;
374 p->start = p->end - path_len;
375 memmove(p->start, tmp_buf, path_len + 1);
376 } else {
377 p->start = p->buf;
378 p->end = p->start + path_len;
379 }
380 return 0;
381}
382
David Sterbab23ab572014-02-03 19:23:19 +0100383static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
384 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200385{
386 int ret;
387 int new_len;
388
389 new_len = p->end - p->start + name_len;
390 if (p->start != p->end)
391 new_len++;
392 ret = fs_path_ensure_buf(p, new_len);
393 if (ret < 0)
394 goto out;
395
396 if (p->reversed) {
397 if (p->start != p->end)
398 *--p->start = '/';
399 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100400 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200401 } else {
402 if (p->start != p->end)
403 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100404 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200405 p->end += name_len;
406 *p->end = 0;
407 }
408
409out:
410 return ret;
411}
412
413static int fs_path_add(struct fs_path *p, const char *name, int name_len)
414{
415 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100416 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200417
David Sterbab23ab572014-02-03 19:23:19 +0100418 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200419 if (ret < 0)
420 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100421 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200422
423out:
424 return ret;
425}
426
427static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
428{
429 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100430 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200431
David Sterbab23ab572014-02-03 19:23:19 +0100432 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200433 if (ret < 0)
434 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100435 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200436
437out:
438 return ret;
439}
440
441static int fs_path_add_from_extent_buffer(struct fs_path *p,
442 struct extent_buffer *eb,
443 unsigned long off, int len)
444{
445 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100446 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200447
David Sterbab23ab572014-02-03 19:23:19 +0100448 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200449 if (ret < 0)
450 goto out;
451
David Sterbab23ab572014-02-03 19:23:19 +0100452 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200453
454out:
455 return ret;
456}
457
Alexander Block31db9f72012-07-25 23:19:24 +0200458static int fs_path_copy(struct fs_path *p, struct fs_path *from)
459{
460 int ret;
461
462 p->reversed = from->reversed;
463 fs_path_reset(p);
464
465 ret = fs_path_add_path(p, from);
466
467 return ret;
468}
469
470
471static void fs_path_unreverse(struct fs_path *p)
472{
473 char *tmp;
474 int len;
475
476 if (!p->reversed)
477 return;
478
479 tmp = p->start;
480 len = p->end - p->start;
481 p->start = p->buf;
482 p->end = p->start + len;
483 memmove(p->start, tmp, len + 1);
484 p->reversed = 0;
485}
486
487static struct btrfs_path *alloc_path_for_send(void)
488{
489 struct btrfs_path *path;
490
491 path = btrfs_alloc_path();
492 if (!path)
493 return NULL;
494 path->search_commit_root = 1;
495 path->skip_locking = 1;
496 return path;
497}
498
Eric Sandeen48a3b632013-04-25 20:41:01 +0000499static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200500{
501 int ret;
502 mm_segment_t old_fs;
503 u32 pos = 0;
504
505 old_fs = get_fs();
506 set_fs(KERNEL_DS);
507
508 while (pos < len) {
Anand Jain1bcea352012-09-14 00:04:21 -0600509 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200510 /* TODO handle that correctly */
511 /*if (ret == -ERESTARTSYS) {
512 continue;
513 }*/
514 if (ret < 0)
515 goto out;
516 if (ret == 0) {
517 ret = -EIO;
518 goto out;
519 }
520 pos += ret;
521 }
522
523 ret = 0;
524
525out:
526 set_fs(old_fs);
527 return ret;
528}
529
530static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
531{
532 struct btrfs_tlv_header *hdr;
533 int total_len = sizeof(*hdr) + len;
534 int left = sctx->send_max_size - sctx->send_size;
535
536 if (unlikely(left < total_len))
537 return -EOVERFLOW;
538
539 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
540 hdr->tlv_type = cpu_to_le16(attr);
541 hdr->tlv_len = cpu_to_le16(len);
542 memcpy(hdr + 1, data, len);
543 sctx->send_size += total_len;
544
545 return 0;
546}
547
David Sterba95bc79d2013-12-16 17:34:10 +0100548#define TLV_PUT_DEFINE_INT(bits) \
549 static int tlv_put_u##bits(struct send_ctx *sctx, \
550 u##bits attr, u##bits value) \
551 { \
552 __le##bits __tmp = cpu_to_le##bits(value); \
553 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
554 }
Alexander Block31db9f72012-07-25 23:19:24 +0200555
David Sterba95bc79d2013-12-16 17:34:10 +0100556TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200557
558static int tlv_put_string(struct send_ctx *sctx, u16 attr,
559 const char *str, int len)
560{
561 if (len == -1)
562 len = strlen(str);
563 return tlv_put(sctx, attr, str, len);
564}
565
566static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
567 const u8 *uuid)
568{
569 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
570}
571
Alexander Block31db9f72012-07-25 23:19:24 +0200572static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
573 struct extent_buffer *eb,
574 struct btrfs_timespec *ts)
575{
576 struct btrfs_timespec bts;
577 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
578 return tlv_put(sctx, attr, &bts, sizeof(bts));
579}
580
581
582#define TLV_PUT(sctx, attrtype, attrlen, data) \
583 do { \
584 ret = tlv_put(sctx, attrtype, attrlen, data); \
585 if (ret < 0) \
586 goto tlv_put_failure; \
587 } while (0)
588
589#define TLV_PUT_INT(sctx, attrtype, bits, value) \
590 do { \
591 ret = tlv_put_u##bits(sctx, attrtype, value); \
592 if (ret < 0) \
593 goto tlv_put_failure; \
594 } while (0)
595
596#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
597#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
598#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
599#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
600#define TLV_PUT_STRING(sctx, attrtype, str, len) \
601 do { \
602 ret = tlv_put_string(sctx, attrtype, str, len); \
603 if (ret < 0) \
604 goto tlv_put_failure; \
605 } while (0)
606#define TLV_PUT_PATH(sctx, attrtype, p) \
607 do { \
608 ret = tlv_put_string(sctx, attrtype, p->start, \
609 p->end - p->start); \
610 if (ret < 0) \
611 goto tlv_put_failure; \
612 } while(0)
613#define TLV_PUT_UUID(sctx, attrtype, uuid) \
614 do { \
615 ret = tlv_put_uuid(sctx, attrtype, uuid); \
616 if (ret < 0) \
617 goto tlv_put_failure; \
618 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200619#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
620 do { \
621 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
622 if (ret < 0) \
623 goto tlv_put_failure; \
624 } while (0)
625
626static int send_header(struct send_ctx *sctx)
627{
628 struct btrfs_stream_header hdr;
629
630 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
631 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
632
Anand Jain1bcea352012-09-14 00:04:21 -0600633 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
634 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200635}
636
637/*
638 * For each command/item we want to send to userspace, we call this function.
639 */
640static int begin_cmd(struct send_ctx *sctx, int cmd)
641{
642 struct btrfs_cmd_header *hdr;
643
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530644 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200645 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200646
647 BUG_ON(sctx->send_size);
648
649 sctx->send_size += sizeof(*hdr);
650 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
651 hdr->cmd = cpu_to_le16(cmd);
652
653 return 0;
654}
655
656static int send_cmd(struct send_ctx *sctx)
657{
658 int ret;
659 struct btrfs_cmd_header *hdr;
660 u32 crc;
661
662 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
663 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
664 hdr->crc = 0;
665
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000666 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200667 hdr->crc = cpu_to_le32(crc);
668
Anand Jain1bcea352012-09-14 00:04:21 -0600669 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
670 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200671
672 sctx->total_send_size += sctx->send_size;
673 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
674 sctx->send_size = 0;
675
676 return ret;
677}
678
679/*
680 * Sends a move instruction to user space
681 */
682static int send_rename(struct send_ctx *sctx,
683 struct fs_path *from, struct fs_path *to)
684{
685 int ret;
686
687verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
688
689 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
690 if (ret < 0)
691 goto out;
692
693 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
694 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
695
696 ret = send_cmd(sctx);
697
698tlv_put_failure:
699out:
700 return ret;
701}
702
703/*
704 * Sends a link instruction to user space
705 */
706static int send_link(struct send_ctx *sctx,
707 struct fs_path *path, struct fs_path *lnk)
708{
709 int ret;
710
711verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
712
713 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
714 if (ret < 0)
715 goto out;
716
717 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
718 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
719
720 ret = send_cmd(sctx);
721
722tlv_put_failure:
723out:
724 return ret;
725}
726
727/*
728 * Sends an unlink instruction to user space
729 */
730static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
731{
732 int ret;
733
734verbose_printk("btrfs: send_unlink %s\n", path->start);
735
736 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
737 if (ret < 0)
738 goto out;
739
740 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
741
742 ret = send_cmd(sctx);
743
744tlv_put_failure:
745out:
746 return ret;
747}
748
749/*
750 * Sends a rmdir instruction to user space
751 */
752static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
753{
754 int ret;
755
756verbose_printk("btrfs: send_rmdir %s\n", path->start);
757
758 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
759 if (ret < 0)
760 goto out;
761
762 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
763
764 ret = send_cmd(sctx);
765
766tlv_put_failure:
767out:
768 return ret;
769}
770
771/*
772 * Helper function to retrieve some fields from an inode item.
773 */
774static int get_inode_info(struct btrfs_root *root,
775 u64 ino, u64 *size, u64 *gen,
Alexander Block85a7b332012-07-26 23:39:10 +0200776 u64 *mode, u64 *uid, u64 *gid,
777 u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200778{
779 int ret;
780 struct btrfs_inode_item *ii;
781 struct btrfs_key key;
782 struct btrfs_path *path;
783
784 path = alloc_path_for_send();
785 if (!path)
786 return -ENOMEM;
787
788 key.objectid = ino;
789 key.type = BTRFS_INODE_ITEM_KEY;
790 key.offset = 0;
791 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
792 if (ret < 0)
793 goto out;
794 if (ret) {
795 ret = -ENOENT;
796 goto out;
797 }
798
799 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
800 struct btrfs_inode_item);
801 if (size)
802 *size = btrfs_inode_size(path->nodes[0], ii);
803 if (gen)
804 *gen = btrfs_inode_generation(path->nodes[0], ii);
805 if (mode)
806 *mode = btrfs_inode_mode(path->nodes[0], ii);
807 if (uid)
808 *uid = btrfs_inode_uid(path->nodes[0], ii);
809 if (gid)
810 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200811 if (rdev)
812 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200813
814out:
815 btrfs_free_path(path);
816 return ret;
817}
818
819typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
820 struct fs_path *p,
821 void *ctx);
822
823/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000824 * Helper function to iterate the entries in ONE btrfs_inode_ref or
825 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200826 * The iterate callback may return a non zero value to stop iteration. This can
827 * be a negative value for error codes or 1 to simply stop it.
828 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000829 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200830 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000831static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200832 struct btrfs_key *found_key, int resolve,
833 iterate_inode_ref_t iterate, void *ctx)
834{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000835 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200836 struct btrfs_item *item;
837 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000838 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200839 struct btrfs_path *tmp_path;
840 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000841 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200842 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000843 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200844 u32 name_len;
845 char *start;
846 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000847 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200848 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000849 u64 dir;
850 unsigned long name_off;
851 unsigned long elem_size;
852 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200853
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000854 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200855 if (!p)
856 return -ENOMEM;
857
858 tmp_path = alloc_path_for_send();
859 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000860 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200861 return -ENOMEM;
862 }
863
Alexander Block31db9f72012-07-25 23:19:24 +0200864
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000865 if (found_key->type == BTRFS_INODE_REF_KEY) {
866 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
867 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100868 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000869 total = btrfs_item_size(eb, item);
870 elem_size = sizeof(*iref);
871 } else {
872 ptr = btrfs_item_ptr_offset(eb, slot);
873 total = btrfs_item_size_nr(eb, slot);
874 elem_size = sizeof(*extref);
875 }
876
Alexander Block31db9f72012-07-25 23:19:24 +0200877 while (cur < total) {
878 fs_path_reset(p);
879
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000880 if (found_key->type == BTRFS_INODE_REF_KEY) {
881 iref = (struct btrfs_inode_ref *)(ptr + cur);
882 name_len = btrfs_inode_ref_name_len(eb, iref);
883 name_off = (unsigned long)(iref + 1);
884 index = btrfs_inode_ref_index(eb, iref);
885 dir = found_key->offset;
886 } else {
887 extref = (struct btrfs_inode_extref *)(ptr + cur);
888 name_len = btrfs_inode_extref_name_len(eb, extref);
889 name_off = (unsigned long)&extref->name;
890 index = btrfs_inode_extref_index(eb, extref);
891 dir = btrfs_inode_extref_parent(eb, extref);
892 }
893
Alexander Block31db9f72012-07-25 23:19:24 +0200894 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000895 start = btrfs_ref_to_path(root, tmp_path, name_len,
896 name_off, eb, dir,
897 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200898 if (IS_ERR(start)) {
899 ret = PTR_ERR(start);
900 goto out;
901 }
902 if (start < p->buf) {
903 /* overflow , try again with larger buffer */
904 ret = fs_path_ensure_buf(p,
905 p->buf_len + p->buf - start);
906 if (ret < 0)
907 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000908 start = btrfs_ref_to_path(root, tmp_path,
909 name_len, name_off,
910 eb, dir,
911 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200912 if (IS_ERR(start)) {
913 ret = PTR_ERR(start);
914 goto out;
915 }
916 BUG_ON(start < p->buf);
917 }
918 p->start = start;
919 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000920 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
921 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200922 if (ret < 0)
923 goto out;
924 }
925
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000926 cur += elem_size + name_len;
927 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200928 if (ret)
929 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200930 num++;
931 }
932
933out:
934 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000935 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200936 return ret;
937}
938
939typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
940 const char *name, int name_len,
941 const char *data, int data_len,
942 u8 type, void *ctx);
943
944/*
945 * Helper function to iterate the entries in ONE btrfs_dir_item.
946 * The iterate callback may return a non zero value to stop iteration. This can
947 * be a negative value for error codes or 1 to simply stop it.
948 *
949 * path must point to the dir item when called.
950 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000951static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200952 struct btrfs_key *found_key,
953 iterate_dir_item_t iterate, void *ctx)
954{
955 int ret = 0;
956 struct extent_buffer *eb;
957 struct btrfs_item *item;
958 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200959 struct btrfs_key di_key;
960 char *buf = NULL;
David Sterbaace01052014-02-05 16:17:34 +0100961 const int buf_len = PATH_MAX;
Alexander Block31db9f72012-07-25 23:19:24 +0200962 u32 name_len;
963 u32 data_len;
964 u32 cur;
965 u32 len;
966 u32 total;
967 int slot;
968 int num;
969 u8 type;
970
Alexander Block31db9f72012-07-25 23:19:24 +0200971 buf = kmalloc(buf_len, GFP_NOFS);
972 if (!buf) {
973 ret = -ENOMEM;
974 goto out;
975 }
976
Alexander Block31db9f72012-07-25 23:19:24 +0200977 eb = path->nodes[0];
978 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +0100979 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +0200980 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
981 cur = 0;
982 len = 0;
983 total = btrfs_item_size(eb, item);
984
985 num = 0;
986 while (cur < total) {
987 name_len = btrfs_dir_name_len(eb, di);
988 data_len = btrfs_dir_data_len(eb, di);
989 type = btrfs_dir_type(eb, di);
990 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
991
David Sterbaace01052014-02-05 16:17:34 +0100992 /*
993 * Path too long
994 */
Alexander Block31db9f72012-07-25 23:19:24 +0200995 if (name_len + data_len > buf_len) {
David Sterbaace01052014-02-05 16:17:34 +0100996 ret = -ENAMETOOLONG;
997 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200998 }
999
1000 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1001 name_len + data_len);
1002
1003 len = sizeof(*di) + name_len + data_len;
1004 di = (struct btrfs_dir_item *)((char *)di + len);
1005 cur += len;
1006
1007 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1008 data_len, type, ctx);
1009 if (ret < 0)
1010 goto out;
1011 if (ret) {
1012 ret = 0;
1013 goto out;
1014 }
1015
1016 num++;
1017 }
1018
1019out:
David Sterbaace01052014-02-05 16:17:34 +01001020 kfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001021 return ret;
1022}
1023
1024static int __copy_first_ref(int num, u64 dir, int index,
1025 struct fs_path *p, void *ctx)
1026{
1027 int ret;
1028 struct fs_path *pt = ctx;
1029
1030 ret = fs_path_copy(pt, p);
1031 if (ret < 0)
1032 return ret;
1033
1034 /* we want the first only */
1035 return 1;
1036}
1037
1038/*
1039 * Retrieve the first path of an inode. If an inode has more then one
1040 * ref/hardlink, this is ignored.
1041 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001042static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001043 u64 ino, struct fs_path *path)
1044{
1045 int ret;
1046 struct btrfs_key key, found_key;
1047 struct btrfs_path *p;
1048
1049 p = alloc_path_for_send();
1050 if (!p)
1051 return -ENOMEM;
1052
1053 fs_path_reset(path);
1054
1055 key.objectid = ino;
1056 key.type = BTRFS_INODE_REF_KEY;
1057 key.offset = 0;
1058
1059 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1060 if (ret < 0)
1061 goto out;
1062 if (ret) {
1063 ret = 1;
1064 goto out;
1065 }
1066 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1067 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001068 (found_key.type != BTRFS_INODE_REF_KEY &&
1069 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001070 ret = -ENOENT;
1071 goto out;
1072 }
1073
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001074 ret = iterate_inode_ref(root, p, &found_key, 1,
1075 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001076 if (ret < 0)
1077 goto out;
1078 ret = 0;
1079
1080out:
1081 btrfs_free_path(p);
1082 return ret;
1083}
1084
1085struct backref_ctx {
1086 struct send_ctx *sctx;
1087
1088 /* number of total found references */
1089 u64 found;
1090
1091 /*
1092 * used for clones found in send_root. clones found behind cur_objectid
1093 * and cur_offset are not considered as allowed clones.
1094 */
1095 u64 cur_objectid;
1096 u64 cur_offset;
1097
1098 /* may be truncated in case it's the last extent in a file */
1099 u64 extent_len;
1100
1101 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001102 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001103};
1104
1105static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1106{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001107 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001108 struct clone_root *cr = (struct clone_root *)elt;
1109
1110 if (root < cr->root->objectid)
1111 return -1;
1112 if (root > cr->root->objectid)
1113 return 1;
1114 return 0;
1115}
1116
1117static int __clone_root_cmp_sort(const void *e1, const void *e2)
1118{
1119 struct clone_root *cr1 = (struct clone_root *)e1;
1120 struct clone_root *cr2 = (struct clone_root *)e2;
1121
1122 if (cr1->root->objectid < cr2->root->objectid)
1123 return -1;
1124 if (cr1->root->objectid > cr2->root->objectid)
1125 return 1;
1126 return 0;
1127}
1128
1129/*
1130 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001131 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001132 */
1133static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1134{
1135 struct backref_ctx *bctx = ctx_;
1136 struct clone_root *found;
1137 int ret;
1138 u64 i_size;
1139
1140 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001141 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001142 bctx->sctx->clone_roots_cnt,
1143 sizeof(struct clone_root),
1144 __clone_root_cmp_bsearch);
1145 if (!found)
1146 return 0;
1147
1148 if (found->root == bctx->sctx->send_root &&
1149 ino == bctx->cur_objectid &&
1150 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001151 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001152 }
1153
1154 /*
Alexander Block766702e2012-07-28 14:11:31 +02001155 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001156 * accept clones from these extents.
1157 */
Alexander Block85a7b332012-07-26 23:39:10 +02001158 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1159 NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001160 if (ret < 0)
1161 return ret;
1162
1163 if (offset + bctx->extent_len > i_size)
1164 return 0;
1165
1166 /*
1167 * Make sure we don't consider clones from send_root that are
1168 * behind the current inode/offset.
1169 */
1170 if (found->root == bctx->sctx->send_root) {
1171 /*
1172 * TODO for the moment we don't accept clones from the inode
1173 * that is currently send. We may change this when
1174 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1175 * file.
1176 */
1177 if (ino >= bctx->cur_objectid)
1178 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001179#if 0
1180 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001181 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001182 if (offset + bctx->extent_len > bctx->cur_offset)
1183 return 0;
1184#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001185 }
1186
1187 bctx->found++;
1188 found->found_refs++;
1189 if (ino < found->ino) {
1190 found->ino = ino;
1191 found->offset = offset;
1192 } else if (found->ino == ino) {
1193 /*
1194 * same extent found more then once in the same file.
1195 */
1196 if (found->offset > offset + bctx->extent_len)
1197 found->offset = offset;
1198 }
1199
1200 return 0;
1201}
1202
1203/*
Alexander Block766702e2012-07-28 14:11:31 +02001204 * Given an inode, offset and extent item, it finds a good clone for a clone
1205 * instruction. Returns -ENOENT when none could be found. The function makes
1206 * sure that the returned clone is usable at the point where sending is at the
1207 * moment. This means, that no clones are accepted which lie behind the current
1208 * inode+offset.
1209 *
Alexander Block31db9f72012-07-25 23:19:24 +02001210 * path must point to the extent item when called.
1211 */
1212static int find_extent_clone(struct send_ctx *sctx,
1213 struct btrfs_path *path,
1214 u64 ino, u64 data_offset,
1215 u64 ino_size,
1216 struct clone_root **found)
1217{
1218 int ret;
1219 int extent_type;
1220 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001221 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001222 u64 num_bytes;
1223 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001224 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001225 struct btrfs_file_extent_item *fi;
1226 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001227 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001228 struct clone_root *cur_clone_root;
1229 struct btrfs_key found_key;
1230 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001231 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001232 u32 i;
1233
1234 tmp_path = alloc_path_for_send();
1235 if (!tmp_path)
1236 return -ENOMEM;
1237
Alexander Block35075bb2012-07-28 12:44:34 +02001238 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1239 if (!backref_ctx) {
1240 ret = -ENOMEM;
1241 goto out;
1242 }
1243
Alexander Block31db9f72012-07-25 23:19:24 +02001244 if (data_offset >= ino_size) {
1245 /*
1246 * There may be extents that lie behind the file's size.
1247 * I at least had this in combination with snapshotting while
1248 * writing large files.
1249 */
1250 ret = 0;
1251 goto out;
1252 }
1253
1254 fi = btrfs_item_ptr(eb, path->slots[0],
1255 struct btrfs_file_extent_item);
1256 extent_type = btrfs_file_extent_type(eb, fi);
1257 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1258 ret = -ENOENT;
1259 goto out;
1260 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001261 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001262
1263 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001264 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1265 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001266 ret = -ENOENT;
1267 goto out;
1268 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001269 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001270
Josef Bacik9e351cc2014-03-13 15:42:13 -04001271 down_read(&sctx->send_root->fs_info->commit_root_sem);
Liu Bo69917e42012-09-07 20:01:28 -06001272 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1273 &found_key, &flags);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001274 up_read(&sctx->send_root->fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001275 btrfs_release_path(tmp_path);
1276
1277 if (ret < 0)
1278 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001279 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001280 ret = -EIO;
1281 goto out;
1282 }
1283
1284 /*
1285 * Setup the clone roots.
1286 */
1287 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1288 cur_clone_root = sctx->clone_roots + i;
1289 cur_clone_root->ino = (u64)-1;
1290 cur_clone_root->offset = 0;
1291 cur_clone_root->found_refs = 0;
1292 }
1293
Alexander Block35075bb2012-07-28 12:44:34 +02001294 backref_ctx->sctx = sctx;
1295 backref_ctx->found = 0;
1296 backref_ctx->cur_objectid = ino;
1297 backref_ctx->cur_offset = data_offset;
1298 backref_ctx->found_itself = 0;
1299 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001300
1301 /*
1302 * The last extent of a file may be too large due to page alignment.
1303 * We need to adjust extent_len in this case so that the checks in
1304 * __iterate_backrefs work.
1305 */
1306 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001307 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001308
1309 /*
1310 * Now collect all backrefs.
1311 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001312 if (compressed == BTRFS_COMPRESS_NONE)
1313 extent_item_pos = logical - found_key.objectid;
1314 else
1315 extent_item_pos = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001316 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1317 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001318 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001319
Alexander Block31db9f72012-07-25 23:19:24 +02001320 if (ret < 0)
1321 goto out;
1322
Alexander Block35075bb2012-07-28 12:44:34 +02001323 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001324 /* found a bug in backref code? */
1325 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001326 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001327 "send_root. inode=%llu, offset=%llu, "
Chris Mason74dd17f2012-08-07 16:25:13 -04001328 "disk_byte=%llu found extent=%llu\n",
1329 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001330 goto out;
1331 }
1332
1333verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1334 "ino=%llu, "
1335 "num_bytes=%llu, logical=%llu\n",
1336 data_offset, ino, num_bytes, logical);
1337
Alexander Block35075bb2012-07-28 12:44:34 +02001338 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001339 verbose_printk("btrfs: no clones found\n");
1340
1341 cur_clone_root = NULL;
1342 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1343 if (sctx->clone_roots[i].found_refs) {
1344 if (!cur_clone_root)
1345 cur_clone_root = sctx->clone_roots + i;
1346 else if (sctx->clone_roots[i].root == sctx->send_root)
1347 /* prefer clones from send_root over others */
1348 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001349 }
1350
1351 }
1352
1353 if (cur_clone_root) {
Filipe David Borba Manana93de4ba2014-02-15 15:53:16 +00001354 if (compressed != BTRFS_COMPRESS_NONE) {
1355 /*
1356 * Offsets given by iterate_extent_inodes() are relative
1357 * to the start of the extent, we need to add logical
1358 * offset from the file extent item.
1359 * (See why at backref.c:check_extent_in_eb())
1360 */
1361 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1362 fi);
1363 }
Alexander Block31db9f72012-07-25 23:19:24 +02001364 *found = cur_clone_root;
1365 ret = 0;
1366 } else {
1367 ret = -ENOENT;
1368 }
1369
1370out:
1371 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001372 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001373 return ret;
1374}
1375
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001376static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001377 u64 ino,
1378 struct fs_path *dest)
1379{
1380 int ret;
1381 struct btrfs_path *path;
1382 struct btrfs_key key;
1383 struct btrfs_file_extent_item *ei;
1384 u8 type;
1385 u8 compression;
1386 unsigned long off;
1387 int len;
1388
1389 path = alloc_path_for_send();
1390 if (!path)
1391 return -ENOMEM;
1392
1393 key.objectid = ino;
1394 key.type = BTRFS_EXTENT_DATA_KEY;
1395 key.offset = 0;
1396 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1397 if (ret < 0)
1398 goto out;
1399 BUG_ON(ret);
1400
1401 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1402 struct btrfs_file_extent_item);
1403 type = btrfs_file_extent_type(path->nodes[0], ei);
1404 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1405 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1406 BUG_ON(compression);
1407
1408 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001409 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001410
1411 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001412
1413out:
1414 btrfs_free_path(path);
1415 return ret;
1416}
1417
1418/*
1419 * Helper function to generate a file name that is unique in the root of
1420 * send_root and parent_root. This is used to generate names for orphan inodes.
1421 */
1422static int gen_unique_name(struct send_ctx *sctx,
1423 u64 ino, u64 gen,
1424 struct fs_path *dest)
1425{
1426 int ret = 0;
1427 struct btrfs_path *path;
1428 struct btrfs_dir_item *di;
1429 char tmp[64];
1430 int len;
1431 u64 idx = 0;
1432
1433 path = alloc_path_for_send();
1434 if (!path)
1435 return -ENOMEM;
1436
1437 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001438 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001439 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001440 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001441
1442 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1443 path, BTRFS_FIRST_FREE_OBJECTID,
1444 tmp, strlen(tmp), 0);
1445 btrfs_release_path(path);
1446 if (IS_ERR(di)) {
1447 ret = PTR_ERR(di);
1448 goto out;
1449 }
1450 if (di) {
1451 /* not unique, try again */
1452 idx++;
1453 continue;
1454 }
1455
1456 if (!sctx->parent_root) {
1457 /* unique */
1458 ret = 0;
1459 break;
1460 }
1461
1462 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1463 path, BTRFS_FIRST_FREE_OBJECTID,
1464 tmp, strlen(tmp), 0);
1465 btrfs_release_path(path);
1466 if (IS_ERR(di)) {
1467 ret = PTR_ERR(di);
1468 goto out;
1469 }
1470 if (di) {
1471 /* not unique, try again */
1472 idx++;
1473 continue;
1474 }
1475 /* unique */
1476 break;
1477 }
1478
1479 ret = fs_path_add(dest, tmp, strlen(tmp));
1480
1481out:
1482 btrfs_free_path(path);
1483 return ret;
1484}
1485
1486enum inode_state {
1487 inode_state_no_change,
1488 inode_state_will_create,
1489 inode_state_did_create,
1490 inode_state_will_delete,
1491 inode_state_did_delete,
1492};
1493
1494static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1495{
1496 int ret;
1497 int left_ret;
1498 int right_ret;
1499 u64 left_gen;
1500 u64 right_gen;
1501
1502 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001503 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001504 if (ret < 0 && ret != -ENOENT)
1505 goto out;
1506 left_ret = ret;
1507
1508 if (!sctx->parent_root) {
1509 right_ret = -ENOENT;
1510 } else {
1511 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001512 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001513 if (ret < 0 && ret != -ENOENT)
1514 goto out;
1515 right_ret = ret;
1516 }
1517
1518 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001519 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001520 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001521 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001522 if (ino < sctx->send_progress)
1523 ret = inode_state_did_create;
1524 else
1525 ret = inode_state_will_create;
1526 } else if (right_gen == gen) {
1527 if (ino < sctx->send_progress)
1528 ret = inode_state_did_delete;
1529 else
1530 ret = inode_state_will_delete;
1531 } else {
1532 ret = -ENOENT;
1533 }
1534 } else if (!left_ret) {
1535 if (left_gen == gen) {
1536 if (ino < sctx->send_progress)
1537 ret = inode_state_did_create;
1538 else
1539 ret = inode_state_will_create;
1540 } else {
1541 ret = -ENOENT;
1542 }
1543 } else if (!right_ret) {
1544 if (right_gen == gen) {
1545 if (ino < sctx->send_progress)
1546 ret = inode_state_did_delete;
1547 else
1548 ret = inode_state_will_delete;
1549 } else {
1550 ret = -ENOENT;
1551 }
1552 } else {
1553 ret = -ENOENT;
1554 }
1555
1556out:
1557 return ret;
1558}
1559
1560static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1561{
1562 int ret;
1563
1564 ret = get_cur_inode_state(sctx, ino, gen);
1565 if (ret < 0)
1566 goto out;
1567
1568 if (ret == inode_state_no_change ||
1569 ret == inode_state_did_create ||
1570 ret == inode_state_will_delete)
1571 ret = 1;
1572 else
1573 ret = 0;
1574
1575out:
1576 return ret;
1577}
1578
1579/*
1580 * Helper function to lookup a dir item in a dir.
1581 */
1582static int lookup_dir_item_inode(struct btrfs_root *root,
1583 u64 dir, const char *name, int name_len,
1584 u64 *found_inode,
1585 u8 *found_type)
1586{
1587 int ret = 0;
1588 struct btrfs_dir_item *di;
1589 struct btrfs_key key;
1590 struct btrfs_path *path;
1591
1592 path = alloc_path_for_send();
1593 if (!path)
1594 return -ENOMEM;
1595
1596 di = btrfs_lookup_dir_item(NULL, root, path,
1597 dir, name, name_len, 0);
1598 if (!di) {
1599 ret = -ENOENT;
1600 goto out;
1601 }
1602 if (IS_ERR(di)) {
1603 ret = PTR_ERR(di);
1604 goto out;
1605 }
1606 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1607 *found_inode = key.objectid;
1608 *found_type = btrfs_dir_type(path->nodes[0], di);
1609
1610out:
1611 btrfs_free_path(path);
1612 return ret;
1613}
1614
Alexander Block766702e2012-07-28 14:11:31 +02001615/*
1616 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1617 * generation of the parent dir and the name of the dir entry.
1618 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001619static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001620 u64 *dir, u64 *dir_gen, struct fs_path *name)
1621{
1622 int ret;
1623 struct btrfs_key key;
1624 struct btrfs_key found_key;
1625 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001626 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001627 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001628
1629 path = alloc_path_for_send();
1630 if (!path)
1631 return -ENOMEM;
1632
1633 key.objectid = ino;
1634 key.type = BTRFS_INODE_REF_KEY;
1635 key.offset = 0;
1636
1637 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1638 if (ret < 0)
1639 goto out;
1640 if (!ret)
1641 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1642 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001643 if (ret || found_key.objectid != ino ||
1644 (found_key.type != BTRFS_INODE_REF_KEY &&
1645 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001646 ret = -ENOENT;
1647 goto out;
1648 }
1649
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001650 if (key.type == BTRFS_INODE_REF_KEY) {
1651 struct btrfs_inode_ref *iref;
1652 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1653 struct btrfs_inode_ref);
1654 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1655 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1656 (unsigned long)(iref + 1),
1657 len);
1658 parent_dir = found_key.offset;
1659 } else {
1660 struct btrfs_inode_extref *extref;
1661 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1662 struct btrfs_inode_extref);
1663 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1664 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1665 (unsigned long)&extref->name, len);
1666 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1667 }
Alexander Block31db9f72012-07-25 23:19:24 +02001668 if (ret < 0)
1669 goto out;
1670 btrfs_release_path(path);
1671
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001672 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001673 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001674 if (ret < 0)
1675 goto out;
1676
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001677 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001678
1679out:
1680 btrfs_free_path(path);
1681 return ret;
1682}
1683
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001684static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001685 u64 ino, u64 dir,
1686 const char *name, int name_len)
1687{
1688 int ret;
1689 struct fs_path *tmp_name;
1690 u64 tmp_dir;
1691 u64 tmp_dir_gen;
1692
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001693 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001694 if (!tmp_name)
1695 return -ENOMEM;
1696
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001697 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001698 if (ret < 0)
1699 goto out;
1700
Alexander Blockb9291af2012-07-28 11:07:18 +02001701 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001702 ret = 0;
1703 goto out;
1704 }
1705
Alexander Blocke938c8a2012-07-28 16:33:49 +02001706 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001707
1708out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001709 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001710 return ret;
1711}
1712
Alexander Block766702e2012-07-28 14:11:31 +02001713/*
1714 * Used by process_recorded_refs to determine if a new ref would overwrite an
1715 * already existing ref. In case it detects an overwrite, it returns the
1716 * inode/gen in who_ino/who_gen.
1717 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1718 * to make sure later references to the overwritten inode are possible.
1719 * Orphanizing is however only required for the first ref of an inode.
1720 * process_recorded_refs does an additional is_first_ref check to see if
1721 * orphanizing is really required.
1722 */
Alexander Block31db9f72012-07-25 23:19:24 +02001723static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1724 const char *name, int name_len,
1725 u64 *who_ino, u64 *who_gen)
1726{
1727 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001728 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001729 u64 other_inode = 0;
1730 u8 other_type = 0;
1731
1732 if (!sctx->parent_root)
1733 goto out;
1734
1735 ret = is_inode_existent(sctx, dir, dir_gen);
1736 if (ret <= 0)
1737 goto out;
1738
Josef Bacikebdad912013-08-06 16:47:48 -04001739 /*
1740 * If we have a parent root we need to verify that the parent dir was
1741 * not delted and then re-created, if it was then we have no overwrite
1742 * and we can just unlink this entry.
1743 */
1744 if (sctx->parent_root) {
1745 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1746 NULL, NULL, NULL);
1747 if (ret < 0 && ret != -ENOENT)
1748 goto out;
1749 if (ret) {
1750 ret = 0;
1751 goto out;
1752 }
1753 if (gen != dir_gen)
1754 goto out;
1755 }
1756
Alexander Block31db9f72012-07-25 23:19:24 +02001757 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1758 &other_inode, &other_type);
1759 if (ret < 0 && ret != -ENOENT)
1760 goto out;
1761 if (ret) {
1762 ret = 0;
1763 goto out;
1764 }
1765
Alexander Block766702e2012-07-28 14:11:31 +02001766 /*
1767 * Check if the overwritten ref was already processed. If yes, the ref
1768 * was already unlinked/moved, so we can safely assume that we will not
1769 * overwrite anything at this point in time.
1770 */
Alexander Block31db9f72012-07-25 23:19:24 +02001771 if (other_inode > sctx->send_progress) {
1772 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001773 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001774 if (ret < 0)
1775 goto out;
1776
1777 ret = 1;
1778 *who_ino = other_inode;
1779 } else {
1780 ret = 0;
1781 }
1782
1783out:
1784 return ret;
1785}
1786
Alexander Block766702e2012-07-28 14:11:31 +02001787/*
1788 * Checks if the ref was overwritten by an already processed inode. This is
1789 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1790 * thus the orphan name needs be used.
1791 * process_recorded_refs also uses it to avoid unlinking of refs that were
1792 * overwritten.
1793 */
Alexander Block31db9f72012-07-25 23:19:24 +02001794static int did_overwrite_ref(struct send_ctx *sctx,
1795 u64 dir, u64 dir_gen,
1796 u64 ino, u64 ino_gen,
1797 const char *name, int name_len)
1798{
1799 int ret = 0;
1800 u64 gen;
1801 u64 ow_inode;
1802 u8 other_type;
1803
1804 if (!sctx->parent_root)
1805 goto out;
1806
1807 ret = is_inode_existent(sctx, dir, dir_gen);
1808 if (ret <= 0)
1809 goto out;
1810
1811 /* check if the ref was overwritten by another ref */
1812 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1813 &ow_inode, &other_type);
1814 if (ret < 0 && ret != -ENOENT)
1815 goto out;
1816 if (ret) {
1817 /* was never and will never be overwritten */
1818 ret = 0;
1819 goto out;
1820 }
1821
1822 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001823 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001824 if (ret < 0)
1825 goto out;
1826
1827 if (ow_inode == ino && gen == ino_gen) {
1828 ret = 0;
1829 goto out;
1830 }
1831
1832 /* we know that it is or will be overwritten. check this now */
1833 if (ow_inode < sctx->send_progress)
1834 ret = 1;
1835 else
1836 ret = 0;
1837
1838out:
1839 return ret;
1840}
1841
Alexander Block766702e2012-07-28 14:11:31 +02001842/*
1843 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1844 * that got overwritten. This is used by process_recorded_refs to determine
1845 * if it has to use the path as returned by get_cur_path or the orphan name.
1846 */
Alexander Block31db9f72012-07-25 23:19:24 +02001847static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1848{
1849 int ret = 0;
1850 struct fs_path *name = NULL;
1851 u64 dir;
1852 u64 dir_gen;
1853
1854 if (!sctx->parent_root)
1855 goto out;
1856
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001857 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001858 if (!name)
1859 return -ENOMEM;
1860
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001861 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001862 if (ret < 0)
1863 goto out;
1864
1865 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1866 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001867
1868out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001869 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001870 return ret;
1871}
1872
Alexander Block766702e2012-07-28 14:11:31 +02001873/*
1874 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1875 * so we need to do some special handling in case we have clashes. This function
1876 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001877 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001878 */
Alexander Block31db9f72012-07-25 23:19:24 +02001879static int name_cache_insert(struct send_ctx *sctx,
1880 struct name_cache_entry *nce)
1881{
1882 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001883 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001884
Alexander Block7e0926f2012-07-28 14:20:58 +02001885 nce_head = radix_tree_lookup(&sctx->name_cache,
1886 (unsigned long)nce->ino);
1887 if (!nce_head) {
1888 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001889 if (!nce_head) {
1890 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001891 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001892 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001893 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001894
Alexander Block7e0926f2012-07-28 14:20:58 +02001895 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001896 if (ret < 0) {
1897 kfree(nce_head);
1898 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001899 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001900 }
Alexander Block31db9f72012-07-25 23:19:24 +02001901 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001902 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001903 list_add_tail(&nce->list, &sctx->name_cache_list);
1904 sctx->name_cache_size++;
1905
1906 return ret;
1907}
1908
1909static void name_cache_delete(struct send_ctx *sctx,
1910 struct name_cache_entry *nce)
1911{
Alexander Block7e0926f2012-07-28 14:20:58 +02001912 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001913
Alexander Block7e0926f2012-07-28 14:20:58 +02001914 nce_head = radix_tree_lookup(&sctx->name_cache,
1915 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01001916 if (!nce_head) {
1917 btrfs_err(sctx->send_root->fs_info,
1918 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1919 nce->ino, sctx->name_cache_size);
1920 }
Alexander Block31db9f72012-07-25 23:19:24 +02001921
Alexander Block7e0926f2012-07-28 14:20:58 +02001922 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001923 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001924 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001925
David Sterba57fb8912014-02-03 19:24:40 +01001926 /*
1927 * We may not get to the final release of nce_head if the lookup fails
1928 */
1929 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02001930 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1931 kfree(nce_head);
1932 }
Alexander Block31db9f72012-07-25 23:19:24 +02001933}
1934
1935static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1936 u64 ino, u64 gen)
1937{
Alexander Block7e0926f2012-07-28 14:20:58 +02001938 struct list_head *nce_head;
1939 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001940
Alexander Block7e0926f2012-07-28 14:20:58 +02001941 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1942 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001943 return NULL;
1944
Alexander Block7e0926f2012-07-28 14:20:58 +02001945 list_for_each_entry(cur, nce_head, radix_list) {
1946 if (cur->ino == ino && cur->gen == gen)
1947 return cur;
1948 }
Alexander Block31db9f72012-07-25 23:19:24 +02001949 return NULL;
1950}
1951
Alexander Block766702e2012-07-28 14:11:31 +02001952/*
1953 * Removes the entry from the list and adds it back to the end. This marks the
1954 * entry as recently used so that name_cache_clean_unused does not remove it.
1955 */
Alexander Block31db9f72012-07-25 23:19:24 +02001956static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1957{
1958 list_del(&nce->list);
1959 list_add_tail(&nce->list, &sctx->name_cache_list);
1960}
1961
Alexander Block766702e2012-07-28 14:11:31 +02001962/*
1963 * Remove some entries from the beginning of name_cache_list.
1964 */
Alexander Block31db9f72012-07-25 23:19:24 +02001965static void name_cache_clean_unused(struct send_ctx *sctx)
1966{
1967 struct name_cache_entry *nce;
1968
1969 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1970 return;
1971
1972 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1973 nce = list_entry(sctx->name_cache_list.next,
1974 struct name_cache_entry, list);
1975 name_cache_delete(sctx, nce);
1976 kfree(nce);
1977 }
1978}
1979
1980static void name_cache_free(struct send_ctx *sctx)
1981{
1982 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02001983
Alexander Blocke938c8a2012-07-28 16:33:49 +02001984 while (!list_empty(&sctx->name_cache_list)) {
1985 nce = list_entry(sctx->name_cache_list.next,
1986 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02001987 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02001988 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001989 }
1990}
1991
Alexander Block766702e2012-07-28 14:11:31 +02001992/*
1993 * Used by get_cur_path for each ref up to the root.
1994 * Returns 0 if it succeeded.
1995 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1996 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1997 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1998 * Returns <0 in case of error.
1999 */
Alexander Block31db9f72012-07-25 23:19:24 +02002000static int __get_cur_name_and_parent(struct send_ctx *sctx,
2001 u64 ino, u64 gen,
2002 u64 *parent_ino,
2003 u64 *parent_gen,
2004 struct fs_path *dest)
2005{
2006 int ret;
2007 int nce_ret;
2008 struct btrfs_path *path = NULL;
2009 struct name_cache_entry *nce = NULL;
2010
Alexander Block766702e2012-07-28 14:11:31 +02002011 /*
2012 * First check if we already did a call to this function with the same
2013 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2014 * return the cached result.
2015 */
Alexander Block31db9f72012-07-25 23:19:24 +02002016 nce = name_cache_search(sctx, ino, gen);
2017 if (nce) {
2018 if (ino < sctx->send_progress && nce->need_later_update) {
2019 name_cache_delete(sctx, nce);
2020 kfree(nce);
2021 nce = NULL;
2022 } else {
2023 name_cache_used(sctx, nce);
2024 *parent_ino = nce->parent_ino;
2025 *parent_gen = nce->parent_gen;
2026 ret = fs_path_add(dest, nce->name, nce->name_len);
2027 if (ret < 0)
2028 goto out;
2029 ret = nce->ret;
2030 goto out;
2031 }
2032 }
2033
2034 path = alloc_path_for_send();
2035 if (!path)
2036 return -ENOMEM;
2037
Alexander Block766702e2012-07-28 14:11:31 +02002038 /*
2039 * If the inode is not existent yet, add the orphan name and return 1.
2040 * This should only happen for the parent dir that we determine in
2041 * __record_new_ref
2042 */
Alexander Block31db9f72012-07-25 23:19:24 +02002043 ret = is_inode_existent(sctx, ino, gen);
2044 if (ret < 0)
2045 goto out;
2046
2047 if (!ret) {
2048 ret = gen_unique_name(sctx, ino, gen, dest);
2049 if (ret < 0)
2050 goto out;
2051 ret = 1;
2052 goto out_cache;
2053 }
2054
Alexander Block766702e2012-07-28 14:11:31 +02002055 /*
2056 * Depending on whether the inode was already processed or not, use
2057 * send_root or parent_root for ref lookup.
2058 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002059 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002060 ret = get_first_ref(sctx->send_root, ino,
2061 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002062 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002063 ret = get_first_ref(sctx->parent_root, ino,
2064 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002065 if (ret < 0)
2066 goto out;
2067
Alexander Block766702e2012-07-28 14:11:31 +02002068 /*
2069 * Check if the ref was overwritten by an inode's ref that was processed
2070 * earlier. If yes, treat as orphan and return 1.
2071 */
Alexander Block31db9f72012-07-25 23:19:24 +02002072 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2073 dest->start, dest->end - dest->start);
2074 if (ret < 0)
2075 goto out;
2076 if (ret) {
2077 fs_path_reset(dest);
2078 ret = gen_unique_name(sctx, ino, gen, dest);
2079 if (ret < 0)
2080 goto out;
2081 ret = 1;
2082 }
2083
2084out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002085 /*
2086 * Store the result of the lookup in the name cache.
2087 */
Alexander Block31db9f72012-07-25 23:19:24 +02002088 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2089 if (!nce) {
2090 ret = -ENOMEM;
2091 goto out;
2092 }
2093
2094 nce->ino = ino;
2095 nce->gen = gen;
2096 nce->parent_ino = *parent_ino;
2097 nce->parent_gen = *parent_gen;
2098 nce->name_len = fs_path_len(dest);
2099 nce->ret = ret;
2100 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002101
2102 if (ino < sctx->send_progress)
2103 nce->need_later_update = 0;
2104 else
2105 nce->need_later_update = 1;
2106
2107 nce_ret = name_cache_insert(sctx, nce);
2108 if (nce_ret < 0)
2109 ret = nce_ret;
2110 name_cache_clean_unused(sctx);
2111
2112out:
2113 btrfs_free_path(path);
2114 return ret;
2115}
2116
2117/*
2118 * Magic happens here. This function returns the first ref to an inode as it
2119 * would look like while receiving the stream at this point in time.
2120 * We walk the path up to the root. For every inode in between, we check if it
2121 * was already processed/sent. If yes, we continue with the parent as found
2122 * in send_root. If not, we continue with the parent as found in parent_root.
2123 * If we encounter an inode that was deleted at this point in time, we use the
2124 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2125 * that were not created yet and overwritten inodes/refs.
2126 *
2127 * When do we have have orphan inodes:
2128 * 1. When an inode is freshly created and thus no valid refs are available yet
2129 * 2. When a directory lost all it's refs (deleted) but still has dir items
2130 * inside which were not processed yet (pending for move/delete). If anyone
2131 * tried to get the path to the dir items, it would get a path inside that
2132 * orphan directory.
2133 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2134 * of an unprocessed inode. If in that case the first ref would be
2135 * overwritten, the overwritten inode gets "orphanized". Later when we
2136 * process this overwritten inode, it is restored at a new place by moving
2137 * the orphan inode.
2138 *
2139 * sctx->send_progress tells this function at which point in time receiving
2140 * would be.
2141 */
2142static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2143 struct fs_path *dest)
2144{
2145 int ret = 0;
2146 struct fs_path *name = NULL;
2147 u64 parent_inode = 0;
2148 u64 parent_gen = 0;
2149 int stop = 0;
2150
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002151 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002152 if (!name) {
2153 ret = -ENOMEM;
2154 goto out;
2155 }
2156
2157 dest->reversed = 1;
2158 fs_path_reset(dest);
2159
2160 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2161 fs_path_reset(name);
2162
Filipe Manana9dc44212014-02-19 14:31:44 +00002163 if (is_waiting_for_rm(sctx, ino)) {
2164 ret = gen_unique_name(sctx, ino, gen, name);
2165 if (ret < 0)
2166 goto out;
2167 ret = fs_path_add_path(dest, name);
2168 break;
2169 }
2170
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002171 if (is_waiting_for_move(sctx, ino)) {
2172 ret = get_first_ref(sctx->parent_root, ino,
2173 &parent_inode, &parent_gen, name);
2174 } else {
2175 ret = __get_cur_name_and_parent(sctx, ino, gen,
2176 &parent_inode,
2177 &parent_gen, name);
2178 if (ret)
2179 stop = 1;
2180 }
2181
Alexander Block31db9f72012-07-25 23:19:24 +02002182 if (ret < 0)
2183 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002184
Alexander Block31db9f72012-07-25 23:19:24 +02002185 ret = fs_path_add_path(dest, name);
2186 if (ret < 0)
2187 goto out;
2188
2189 ino = parent_inode;
2190 gen = parent_gen;
2191 }
2192
2193out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002194 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002195 if (!ret)
2196 fs_path_unreverse(dest);
2197 return ret;
2198}
2199
2200/*
Alexander Block31db9f72012-07-25 23:19:24 +02002201 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2202 */
2203static int send_subvol_begin(struct send_ctx *sctx)
2204{
2205 int ret;
2206 struct btrfs_root *send_root = sctx->send_root;
2207 struct btrfs_root *parent_root = sctx->parent_root;
2208 struct btrfs_path *path;
2209 struct btrfs_key key;
2210 struct btrfs_root_ref *ref;
2211 struct extent_buffer *leaf;
2212 char *name = NULL;
2213 int namelen;
2214
Wang Shilongffcfaf82014-01-15 00:26:43 +08002215 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002216 if (!path)
2217 return -ENOMEM;
2218
2219 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2220 if (!name) {
2221 btrfs_free_path(path);
2222 return -ENOMEM;
2223 }
2224
2225 key.objectid = send_root->objectid;
2226 key.type = BTRFS_ROOT_BACKREF_KEY;
2227 key.offset = 0;
2228
2229 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2230 &key, path, 1, 0);
2231 if (ret < 0)
2232 goto out;
2233 if (ret) {
2234 ret = -ENOENT;
2235 goto out;
2236 }
2237
2238 leaf = path->nodes[0];
2239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2240 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2241 key.objectid != send_root->objectid) {
2242 ret = -ENOENT;
2243 goto out;
2244 }
2245 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2246 namelen = btrfs_root_ref_name_len(leaf, ref);
2247 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2248 btrfs_release_path(path);
2249
Alexander Block31db9f72012-07-25 23:19:24 +02002250 if (parent_root) {
2251 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2252 if (ret < 0)
2253 goto out;
2254 } else {
2255 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2256 if (ret < 0)
2257 goto out;
2258 }
2259
2260 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2261 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2262 sctx->send_root->root_item.uuid);
2263 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002264 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002265 if (parent_root) {
2266 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2267 sctx->parent_root->root_item.uuid);
2268 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002269 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002270 }
2271
2272 ret = send_cmd(sctx);
2273
2274tlv_put_failure:
2275out:
2276 btrfs_free_path(path);
2277 kfree(name);
2278 return ret;
2279}
2280
2281static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2282{
2283 int ret = 0;
2284 struct fs_path *p;
2285
2286verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2287
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002288 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002289 if (!p)
2290 return -ENOMEM;
2291
2292 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2293 if (ret < 0)
2294 goto out;
2295
2296 ret = get_cur_path(sctx, ino, gen, p);
2297 if (ret < 0)
2298 goto out;
2299 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2300 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2301
2302 ret = send_cmd(sctx);
2303
2304tlv_put_failure:
2305out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002306 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002307 return ret;
2308}
2309
2310static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2311{
2312 int ret = 0;
2313 struct fs_path *p;
2314
2315verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2316
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002317 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002318 if (!p)
2319 return -ENOMEM;
2320
2321 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2322 if (ret < 0)
2323 goto out;
2324
2325 ret = get_cur_path(sctx, ino, gen, p);
2326 if (ret < 0)
2327 goto out;
2328 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2329 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2330
2331 ret = send_cmd(sctx);
2332
2333tlv_put_failure:
2334out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002335 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002336 return ret;
2337}
2338
2339static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2340{
2341 int ret = 0;
2342 struct fs_path *p;
2343
2344verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2345
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002346 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002347 if (!p)
2348 return -ENOMEM;
2349
2350 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2351 if (ret < 0)
2352 goto out;
2353
2354 ret = get_cur_path(sctx, ino, gen, p);
2355 if (ret < 0)
2356 goto out;
2357 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2358 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2359 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2360
2361 ret = send_cmd(sctx);
2362
2363tlv_put_failure:
2364out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002365 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002366 return ret;
2367}
2368
2369static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2370{
2371 int ret = 0;
2372 struct fs_path *p = NULL;
2373 struct btrfs_inode_item *ii;
2374 struct btrfs_path *path = NULL;
2375 struct extent_buffer *eb;
2376 struct btrfs_key key;
2377 int slot;
2378
2379verbose_printk("btrfs: send_utimes %llu\n", ino);
2380
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002381 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002382 if (!p)
2383 return -ENOMEM;
2384
2385 path = alloc_path_for_send();
2386 if (!path) {
2387 ret = -ENOMEM;
2388 goto out;
2389 }
2390
2391 key.objectid = ino;
2392 key.type = BTRFS_INODE_ITEM_KEY;
2393 key.offset = 0;
2394 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2395 if (ret < 0)
2396 goto out;
2397
2398 eb = path->nodes[0];
2399 slot = path->slots[0];
2400 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2401
2402 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2403 if (ret < 0)
2404 goto out;
2405
2406 ret = get_cur_path(sctx, ino, gen, p);
2407 if (ret < 0)
2408 goto out;
2409 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2410 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2411 btrfs_inode_atime(ii));
2412 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2413 btrfs_inode_mtime(ii));
2414 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2415 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002416 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002417
2418 ret = send_cmd(sctx);
2419
2420tlv_put_failure:
2421out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002422 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002423 btrfs_free_path(path);
2424 return ret;
2425}
2426
2427/*
2428 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2429 * a valid path yet because we did not process the refs yet. So, the inode
2430 * is created as orphan.
2431 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002432static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002433{
2434 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002435 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002436 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002437 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002438 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002439 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002440
Alexander Block1f4692d2012-07-28 10:42:24 +02002441verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002442
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002443 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002444 if (!p)
2445 return -ENOMEM;
2446
Liu Bo644d1942014-02-27 17:29:01 +08002447 if (ino != sctx->cur_ino) {
2448 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2449 NULL, NULL, &rdev);
2450 if (ret < 0)
2451 goto out;
2452 } else {
2453 gen = sctx->cur_inode_gen;
2454 mode = sctx->cur_inode_mode;
2455 rdev = sctx->cur_inode_rdev;
2456 }
Alexander Block31db9f72012-07-25 23:19:24 +02002457
Alexander Blocke938c8a2012-07-28 16:33:49 +02002458 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002459 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002460 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002461 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002462 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002463 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002464 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002465 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002466 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002467 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002468 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002469 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002470 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002471 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2472 (int)(mode & S_IFMT));
2473 ret = -ENOTSUPP;
2474 goto out;
2475 }
2476
2477 ret = begin_cmd(sctx, cmd);
2478 if (ret < 0)
2479 goto out;
2480
Alexander Block1f4692d2012-07-28 10:42:24 +02002481 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002482 if (ret < 0)
2483 goto out;
2484
2485 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002486 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002487
2488 if (S_ISLNK(mode)) {
2489 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002490 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002491 if (ret < 0)
2492 goto out;
2493 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2494 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2495 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002496 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2497 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002498 }
2499
2500 ret = send_cmd(sctx);
2501 if (ret < 0)
2502 goto out;
2503
2504
2505tlv_put_failure:
2506out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002507 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002508 return ret;
2509}
2510
Alexander Block1f4692d2012-07-28 10:42:24 +02002511/*
2512 * We need some special handling for inodes that get processed before the parent
2513 * directory got created. See process_recorded_refs for details.
2514 * This function does the check if we already created the dir out of order.
2515 */
2516static int did_create_dir(struct send_ctx *sctx, u64 dir)
2517{
2518 int ret = 0;
2519 struct btrfs_path *path = NULL;
2520 struct btrfs_key key;
2521 struct btrfs_key found_key;
2522 struct btrfs_key di_key;
2523 struct extent_buffer *eb;
2524 struct btrfs_dir_item *di;
2525 int slot;
2526
2527 path = alloc_path_for_send();
2528 if (!path) {
2529 ret = -ENOMEM;
2530 goto out;
2531 }
2532
2533 key.objectid = dir;
2534 key.type = BTRFS_DIR_INDEX_KEY;
2535 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002536 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2537 if (ret < 0)
2538 goto out;
2539
Alexander Block1f4692d2012-07-28 10:42:24 +02002540 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002541 eb = path->nodes[0];
2542 slot = path->slots[0];
2543 if (slot >= btrfs_header_nritems(eb)) {
2544 ret = btrfs_next_leaf(sctx->send_root, path);
2545 if (ret < 0) {
2546 goto out;
2547 } else if (ret > 0) {
2548 ret = 0;
2549 break;
2550 }
2551 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002552 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002553
2554 btrfs_item_key_to_cpu(eb, &found_key, slot);
2555 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002556 found_key.type != key.type) {
2557 ret = 0;
2558 goto out;
2559 }
2560
2561 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2562 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2563
Josef Bacika0525412013-08-12 10:56:14 -04002564 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2565 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002566 ret = 1;
2567 goto out;
2568 }
2569
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002570 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002571 }
2572
2573out:
2574 btrfs_free_path(path);
2575 return ret;
2576}
2577
2578/*
2579 * Only creates the inode if it is:
2580 * 1. Not a directory
2581 * 2. Or a directory which was not created already due to out of order
2582 * directories. See did_create_dir and process_recorded_refs for details.
2583 */
2584static int send_create_inode_if_needed(struct send_ctx *sctx)
2585{
2586 int ret;
2587
2588 if (S_ISDIR(sctx->cur_inode_mode)) {
2589 ret = did_create_dir(sctx, sctx->cur_ino);
2590 if (ret < 0)
2591 goto out;
2592 if (ret) {
2593 ret = 0;
2594 goto out;
2595 }
2596 }
2597
2598 ret = send_create_inode(sctx, sctx->cur_ino);
2599 if (ret < 0)
2600 goto out;
2601
2602out:
2603 return ret;
2604}
2605
Alexander Block31db9f72012-07-25 23:19:24 +02002606struct recorded_ref {
2607 struct list_head list;
2608 char *dir_path;
2609 char *name;
2610 struct fs_path *full_path;
2611 u64 dir;
2612 u64 dir_gen;
2613 int dir_path_len;
2614 int name_len;
2615};
2616
2617/*
2618 * We need to process new refs before deleted refs, but compare_tree gives us
2619 * everything mixed. So we first record all refs and later process them.
2620 * This function is a helper to record one ref.
2621 */
Liu Boa4d96d62014-03-03 21:31:03 +08002622static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002623 u64 dir_gen, struct fs_path *path)
2624{
2625 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002626
2627 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2628 if (!ref)
2629 return -ENOMEM;
2630
2631 ref->dir = dir;
2632 ref->dir_gen = dir_gen;
2633 ref->full_path = path;
2634
Andy Shevchenkoed848852013-08-21 10:32:13 +03002635 ref->name = (char *)kbasename(ref->full_path->start);
2636 ref->name_len = ref->full_path->end - ref->name;
2637 ref->dir_path = ref->full_path->start;
2638 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002639 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002640 else
Alexander Block31db9f72012-07-25 23:19:24 +02002641 ref->dir_path_len = ref->full_path->end -
2642 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002643
2644 list_add_tail(&ref->list, head);
2645 return 0;
2646}
2647
Josef Bacikba5e8f22013-08-16 16:52:55 -04002648static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2649{
2650 struct recorded_ref *new;
2651
2652 new = kmalloc(sizeof(*ref), GFP_NOFS);
2653 if (!new)
2654 return -ENOMEM;
2655
2656 new->dir = ref->dir;
2657 new->dir_gen = ref->dir_gen;
2658 new->full_path = NULL;
2659 INIT_LIST_HEAD(&new->list);
2660 list_add_tail(&new->list, list);
2661 return 0;
2662}
2663
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002664static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002665{
2666 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002667
Alexander Blocke938c8a2012-07-28 16:33:49 +02002668 while (!list_empty(head)) {
2669 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002670 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002671 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002672 kfree(cur);
2673 }
Alexander Block31db9f72012-07-25 23:19:24 +02002674}
2675
2676static void free_recorded_refs(struct send_ctx *sctx)
2677{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002678 __free_recorded_refs(&sctx->new_refs);
2679 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002680}
2681
2682/*
Alexander Block766702e2012-07-28 14:11:31 +02002683 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002684 * ref of an unprocessed inode gets overwritten and for all non empty
2685 * directories.
2686 */
2687static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2688 struct fs_path *path)
2689{
2690 int ret;
2691 struct fs_path *orphan;
2692
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002693 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002694 if (!orphan)
2695 return -ENOMEM;
2696
2697 ret = gen_unique_name(sctx, ino, gen, orphan);
2698 if (ret < 0)
2699 goto out;
2700
2701 ret = send_rename(sctx, path, orphan);
2702
2703out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002704 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002705 return ret;
2706}
2707
Filipe Manana9dc44212014-02-19 14:31:44 +00002708static struct orphan_dir_info *
2709add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2710{
2711 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2712 struct rb_node *parent = NULL;
2713 struct orphan_dir_info *entry, *odi;
2714
2715 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2716 if (!odi)
2717 return ERR_PTR(-ENOMEM);
2718 odi->ino = dir_ino;
2719 odi->gen = 0;
2720
2721 while (*p) {
2722 parent = *p;
2723 entry = rb_entry(parent, struct orphan_dir_info, node);
2724 if (dir_ino < entry->ino) {
2725 p = &(*p)->rb_left;
2726 } else if (dir_ino > entry->ino) {
2727 p = &(*p)->rb_right;
2728 } else {
2729 kfree(odi);
2730 return entry;
2731 }
2732 }
2733
2734 rb_link_node(&odi->node, parent, p);
2735 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2736 return odi;
2737}
2738
2739static struct orphan_dir_info *
2740get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2741{
2742 struct rb_node *n = sctx->orphan_dirs.rb_node;
2743 struct orphan_dir_info *entry;
2744
2745 while (n) {
2746 entry = rb_entry(n, struct orphan_dir_info, node);
2747 if (dir_ino < entry->ino)
2748 n = n->rb_left;
2749 else if (dir_ino > entry->ino)
2750 n = n->rb_right;
2751 else
2752 return entry;
2753 }
2754 return NULL;
2755}
2756
2757static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2758{
2759 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2760
2761 return odi != NULL;
2762}
2763
2764static void free_orphan_dir_info(struct send_ctx *sctx,
2765 struct orphan_dir_info *odi)
2766{
2767 if (!odi)
2768 return;
2769 rb_erase(&odi->node, &sctx->orphan_dirs);
2770 kfree(odi);
2771}
2772
Alexander Block31db9f72012-07-25 23:19:24 +02002773/*
2774 * Returns 1 if a directory can be removed at this point in time.
2775 * We check this by iterating all dir items and checking if the inode behind
2776 * the dir item was already processed.
2777 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002778static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2779 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002780{
2781 int ret = 0;
2782 struct btrfs_root *root = sctx->parent_root;
2783 struct btrfs_path *path;
2784 struct btrfs_key key;
2785 struct btrfs_key found_key;
2786 struct btrfs_key loc;
2787 struct btrfs_dir_item *di;
2788
Alexander Block6d85ed02012-08-01 14:48:59 +02002789 /*
2790 * Don't try to rmdir the top/root subvolume dir.
2791 */
2792 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2793 return 0;
2794
Alexander Block31db9f72012-07-25 23:19:24 +02002795 path = alloc_path_for_send();
2796 if (!path)
2797 return -ENOMEM;
2798
2799 key.objectid = dir;
2800 key.type = BTRFS_DIR_INDEX_KEY;
2801 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002802 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2803 if (ret < 0)
2804 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002805
2806 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002807 struct waiting_dir_move *dm;
2808
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002809 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2810 ret = btrfs_next_leaf(root, path);
2811 if (ret < 0)
2812 goto out;
2813 else if (ret > 0)
2814 break;
2815 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002816 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002817 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2818 path->slots[0]);
2819 if (found_key.objectid != key.objectid ||
2820 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002821 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002822
2823 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2824 struct btrfs_dir_item);
2825 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2826
Filipe Manana9dc44212014-02-19 14:31:44 +00002827 dm = get_waiting_dir_move(sctx, loc.objectid);
2828 if (dm) {
2829 struct orphan_dir_info *odi;
2830
2831 odi = add_orphan_dir_info(sctx, dir);
2832 if (IS_ERR(odi)) {
2833 ret = PTR_ERR(odi);
2834 goto out;
2835 }
2836 odi->gen = dir_gen;
2837 dm->rmdir_ino = dir;
2838 ret = 0;
2839 goto out;
2840 }
2841
Alexander Block31db9f72012-07-25 23:19:24 +02002842 if (loc.objectid > send_progress) {
2843 ret = 0;
2844 goto out;
2845 }
2846
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002847 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002848 }
2849
2850 ret = 1;
2851
2852out:
2853 btrfs_free_path(path);
2854 return ret;
2855}
2856
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002857static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2858{
Filipe Manana9dc44212014-02-19 14:31:44 +00002859 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002860
Filipe Manana9dc44212014-02-19 14:31:44 +00002861 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002862}
2863
2864static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2865{
2866 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2867 struct rb_node *parent = NULL;
2868 struct waiting_dir_move *entry, *dm;
2869
2870 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2871 if (!dm)
2872 return -ENOMEM;
2873 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00002874 dm->rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002875
2876 while (*p) {
2877 parent = *p;
2878 entry = rb_entry(parent, struct waiting_dir_move, node);
2879 if (ino < entry->ino) {
2880 p = &(*p)->rb_left;
2881 } else if (ino > entry->ino) {
2882 p = &(*p)->rb_right;
2883 } else {
2884 kfree(dm);
2885 return -EEXIST;
2886 }
2887 }
2888
2889 rb_link_node(&dm->node, parent, p);
2890 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2891 return 0;
2892}
2893
Filipe Manana9dc44212014-02-19 14:31:44 +00002894static struct waiting_dir_move *
2895get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002896{
2897 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2898 struct waiting_dir_move *entry;
2899
2900 while (n) {
2901 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00002902 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002903 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00002904 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002905 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002906 else
2907 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002908 }
Filipe Manana9dc44212014-02-19 14:31:44 +00002909 return NULL;
2910}
2911
2912static void free_waiting_dir_move(struct send_ctx *sctx,
2913 struct waiting_dir_move *dm)
2914{
2915 if (!dm)
2916 return;
2917 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2918 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002919}
2920
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00002921static int add_pending_dir_move(struct send_ctx *sctx,
2922 u64 ino,
2923 u64 ino_gen,
2924 u64 parent_ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002925{
2926 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2927 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07002928 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002929 struct recorded_ref *cur;
2930 int exists = 0;
2931 int ret;
2932
2933 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2934 if (!pm)
2935 return -ENOMEM;
2936 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00002937 pm->ino = ino;
2938 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002939 INIT_LIST_HEAD(&pm->list);
2940 INIT_LIST_HEAD(&pm->update_refs);
2941 RB_CLEAR_NODE(&pm->node);
2942
2943 while (*p) {
2944 parent = *p;
2945 entry = rb_entry(parent, struct pending_dir_move, node);
2946 if (parent_ino < entry->parent_ino) {
2947 p = &(*p)->rb_left;
2948 } else if (parent_ino > entry->parent_ino) {
2949 p = &(*p)->rb_right;
2950 } else {
2951 exists = 1;
2952 break;
2953 }
2954 }
2955
2956 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2957 ret = dup_ref(cur, &pm->update_refs);
2958 if (ret < 0)
2959 goto out;
2960 }
2961 list_for_each_entry(cur, &sctx->new_refs, list) {
2962 ret = dup_ref(cur, &pm->update_refs);
2963 if (ret < 0)
2964 goto out;
2965 }
2966
2967 ret = add_waiting_dir_move(sctx, pm->ino);
2968 if (ret)
2969 goto out;
2970
2971 if (exists) {
2972 list_add_tail(&pm->list, &entry->list);
2973 } else {
2974 rb_link_node(&pm->node, parent, p);
2975 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
2976 }
2977 ret = 0;
2978out:
2979 if (ret) {
2980 __free_recorded_refs(&pm->update_refs);
2981 kfree(pm);
2982 }
2983 return ret;
2984}
2985
2986static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
2987 u64 parent_ino)
2988{
2989 struct rb_node *n = sctx->pending_dir_moves.rb_node;
2990 struct pending_dir_move *entry;
2991
2992 while (n) {
2993 entry = rb_entry(n, struct pending_dir_move, node);
2994 if (parent_ino < entry->parent_ino)
2995 n = n->rb_left;
2996 else if (parent_ino > entry->parent_ino)
2997 n = n->rb_right;
2998 else
2999 return entry;
3000 }
3001 return NULL;
3002}
3003
3004static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3005{
3006 struct fs_path *from_path = NULL;
3007 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003008 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003009 u64 orig_progress = sctx->send_progress;
3010 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003011 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003012 struct waiting_dir_move *dm = NULL;
3013 u64 rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003014 int ret;
3015
Filipe Manana2b863a12014-02-16 13:43:11 +00003016 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003017 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003018 if (!name || !from_path) {
3019 ret = -ENOMEM;
3020 goto out;
3021 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003022
Filipe Manana9dc44212014-02-19 14:31:44 +00003023 dm = get_waiting_dir_move(sctx, pm->ino);
3024 ASSERT(dm);
3025 rmdir_ino = dm->rmdir_ino;
3026 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003027
3028 ret = get_first_ref(sctx->parent_root, pm->ino,
3029 &parent_ino, &parent_gen, name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003030 if (ret < 0)
3031 goto out;
3032
Filipe Manana2b863a12014-02-16 13:43:11 +00003033 if (parent_ino == sctx->cur_ino) {
3034 /* child only renamed, not moved */
3035 ASSERT(parent_gen == sctx->cur_inode_gen);
3036 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3037 from_path);
3038 if (ret < 0)
3039 goto out;
3040 ret = fs_path_add_path(from_path, name);
3041 if (ret < 0)
3042 goto out;
3043 } else {
3044 /* child moved and maybe renamed too */
3045 sctx->send_progress = pm->ino;
3046 ret = get_cur_path(sctx, pm->ino, pm->gen, from_path);
3047 if (ret < 0)
3048 goto out;
3049 }
3050
3051 fs_path_free(name);
3052 name = NULL;
3053
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003054 to_path = fs_path_alloc();
3055 if (!to_path) {
3056 ret = -ENOMEM;
3057 goto out;
3058 }
3059
3060 sctx->send_progress = sctx->cur_ino + 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003061 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3062 if (ret < 0)
3063 goto out;
3064
3065 ret = send_rename(sctx, from_path, to_path);
3066 if (ret < 0)
3067 goto out;
3068
Filipe Manana9dc44212014-02-19 14:31:44 +00003069 if (rmdir_ino) {
3070 struct orphan_dir_info *odi;
3071
3072 odi = get_orphan_dir_info(sctx, rmdir_ino);
3073 if (!odi) {
3074 /* already deleted */
3075 goto finish;
3076 }
3077 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3078 if (ret < 0)
3079 goto out;
3080 if (!ret)
3081 goto finish;
3082
3083 name = fs_path_alloc();
3084 if (!name) {
3085 ret = -ENOMEM;
3086 goto out;
3087 }
3088 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3089 if (ret < 0)
3090 goto out;
3091 ret = send_rmdir(sctx, name);
3092 if (ret < 0)
3093 goto out;
3094 free_orphan_dir_info(sctx, odi);
3095 }
3096
3097finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003098 ret = send_utimes(sctx, pm->ino, pm->gen);
3099 if (ret < 0)
3100 goto out;
3101
3102 /*
3103 * After rename/move, need to update the utimes of both new parent(s)
3104 * and old parent(s).
3105 */
3106 list_for_each_entry(cur, &pm->update_refs, list) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003107 if (cur->dir == rmdir_ino)
3108 continue;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003109 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3110 if (ret < 0)
3111 goto out;
3112 }
3113
3114out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003115 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003116 fs_path_free(from_path);
3117 fs_path_free(to_path);
3118 sctx->send_progress = orig_progress;
3119
3120 return ret;
3121}
3122
3123static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3124{
3125 if (!list_empty(&m->list))
3126 list_del(&m->list);
3127 if (!RB_EMPTY_NODE(&m->node))
3128 rb_erase(&m->node, &sctx->pending_dir_moves);
3129 __free_recorded_refs(&m->update_refs);
3130 kfree(m);
3131}
3132
3133static void tail_append_pending_moves(struct pending_dir_move *moves,
3134 struct list_head *stack)
3135{
3136 if (list_empty(&moves->list)) {
3137 list_add_tail(&moves->list, stack);
3138 } else {
3139 LIST_HEAD(list);
3140 list_splice_init(&moves->list, &list);
3141 list_add_tail(&moves->list, stack);
3142 list_splice_tail(&list, stack);
3143 }
3144}
3145
3146static int apply_children_dir_moves(struct send_ctx *sctx)
3147{
3148 struct pending_dir_move *pm;
3149 struct list_head stack;
3150 u64 parent_ino = sctx->cur_ino;
3151 int ret = 0;
3152
3153 pm = get_pending_dir_moves(sctx, parent_ino);
3154 if (!pm)
3155 return 0;
3156
3157 INIT_LIST_HEAD(&stack);
3158 tail_append_pending_moves(pm, &stack);
3159
3160 while (!list_empty(&stack)) {
3161 pm = list_first_entry(&stack, struct pending_dir_move, list);
3162 parent_ino = pm->ino;
3163 ret = apply_dir_move(sctx, pm);
3164 free_pending_move(sctx, pm);
3165 if (ret)
3166 goto out;
3167 pm = get_pending_dir_moves(sctx, parent_ino);
3168 if (pm)
3169 tail_append_pending_moves(pm, &stack);
3170 }
3171 return 0;
3172
3173out:
3174 while (!list_empty(&stack)) {
3175 pm = list_first_entry(&stack, struct pending_dir_move, list);
3176 free_pending_move(sctx, pm);
3177 }
3178 return ret;
3179}
3180
3181static int wait_for_parent_move(struct send_ctx *sctx,
3182 struct recorded_ref *parent_ref)
3183{
3184 int ret;
3185 u64 ino = parent_ref->dir;
3186 u64 parent_ino_before, parent_ino_after;
Filipe Manana425b5da2014-03-18 17:56:06 +00003187 u64 old_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003188 struct fs_path *path_before = NULL;
3189 struct fs_path *path_after = NULL;
3190 int len1, len2;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003191 int register_upper_dirs;
3192 u64 gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003193
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003194 if (is_waiting_for_move(sctx, ino))
3195 return 1;
3196
Filipe Manana7b119a82014-03-16 20:37:26 +00003197 if (parent_ref->dir <= sctx->cur_ino)
3198 return 0;
3199
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003200 ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen,
3201 NULL, NULL, NULL, NULL);
3202 if (ret == -ENOENT)
3203 return 0;
3204 else if (ret < 0)
3205 return ret;
3206
Filipe Manana425b5da2014-03-18 17:56:06 +00003207 if (parent_ref->dir_gen != old_gen)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003208 return 0;
3209
3210 path_before = fs_path_alloc();
3211 if (!path_before)
3212 return -ENOMEM;
3213
3214 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3215 NULL, path_before);
3216 if (ret == -ENOENT) {
3217 ret = 0;
3218 goto out;
3219 } else if (ret < 0) {
3220 goto out;
3221 }
3222
3223 path_after = fs_path_alloc();
3224 if (!path_after) {
3225 ret = -ENOMEM;
3226 goto out;
3227 }
3228
3229 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003230 &gen, path_after);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003231 if (ret == -ENOENT) {
3232 ret = 0;
3233 goto out;
3234 } else if (ret < 0) {
3235 goto out;
3236 }
3237
3238 len1 = fs_path_len(path_before);
3239 len2 = fs_path_len(path_after);
Filipe David Borba Manana5ed7f9f2014-02-01 02:02:16 +00003240 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3241 memcmp(path_before->start, path_after->start, len1)) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003242 ret = 1;
3243 goto out;
3244 }
3245 ret = 0;
3246
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003247 /*
3248 * Ok, our new most direct ancestor has a higher inode number but
3249 * wasn't moved/renamed. So maybe some of the new ancestors higher in
3250 * the hierarchy have an higher inode number too *and* were renamed
3251 * or moved - in this case we need to wait for the ancestor's rename
3252 * or move operation before we can do the move/rename for the current
3253 * inode.
3254 */
3255 register_upper_dirs = 0;
3256 ino = parent_ino_after;
3257again:
3258 while ((ret == 0 || register_upper_dirs) && ino > sctx->cur_ino) {
3259 u64 parent_gen;
3260
3261 fs_path_reset(path_before);
3262 fs_path_reset(path_after);
3263
3264 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3265 &parent_gen, path_after);
3266 if (ret < 0)
3267 goto out;
3268 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3269 NULL, path_before);
3270 if (ret == -ENOENT) {
3271 ret = 0;
3272 break;
3273 } else if (ret < 0) {
3274 goto out;
3275 }
3276
3277 len1 = fs_path_len(path_before);
3278 len2 = fs_path_len(path_after);
3279 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3280 memcmp(path_before->start, path_after->start, len1)) {
3281 ret = 1;
3282 if (register_upper_dirs) {
3283 break;
3284 } else {
3285 register_upper_dirs = 1;
3286 ino = parent_ref->dir;
3287 gen = parent_ref->dir_gen;
3288 goto again;
3289 }
3290 } else if (register_upper_dirs) {
3291 ret = add_pending_dir_move(sctx, ino, gen,
3292 parent_ino_after);
3293 if (ret < 0 && ret != -EEXIST)
3294 goto out;
3295 }
3296
3297 ino = parent_ino_after;
3298 gen = parent_gen;
3299 }
3300
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003301out:
3302 fs_path_free(path_before);
3303 fs_path_free(path_after);
3304
3305 return ret;
3306}
3307
Alexander Block31db9f72012-07-25 23:19:24 +02003308/*
3309 * This does all the move/link/unlink/rmdir magic.
3310 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003311static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003312{
3313 int ret = 0;
3314 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003315 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003316 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003317 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003318 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003319 u64 ow_gen;
3320 int did_overwrite = 0;
3321 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003322 u64 last_dir_ino_rm = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003323
3324verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3325
Alexander Block6d85ed02012-08-01 14:48:59 +02003326 /*
3327 * This should never happen as the root dir always has the same ref
3328 * which is always '..'
3329 */
3330 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003331 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003332
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003333 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003334 if (!valid_path) {
3335 ret = -ENOMEM;
3336 goto out;
3337 }
3338
Alexander Block31db9f72012-07-25 23:19:24 +02003339 /*
3340 * First, check if the first ref of the current inode was overwritten
3341 * before. If yes, we know that the current inode was already orphanized
3342 * and thus use the orphan name. If not, we can use get_cur_path to
3343 * get the path of the first ref as it would like while receiving at
3344 * this point in time.
3345 * New inodes are always orphan at the beginning, so force to use the
3346 * orphan name in this case.
3347 * The first ref is stored in valid_path and will be updated if it
3348 * gets moved around.
3349 */
3350 if (!sctx->cur_inode_new) {
3351 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3352 sctx->cur_inode_gen);
3353 if (ret < 0)
3354 goto out;
3355 if (ret)
3356 did_overwrite = 1;
3357 }
3358 if (sctx->cur_inode_new || did_overwrite) {
3359 ret = gen_unique_name(sctx, sctx->cur_ino,
3360 sctx->cur_inode_gen, valid_path);
3361 if (ret < 0)
3362 goto out;
3363 is_orphan = 1;
3364 } else {
3365 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3366 valid_path);
3367 if (ret < 0)
3368 goto out;
3369 }
3370
3371 list_for_each_entry(cur, &sctx->new_refs, list) {
3372 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003373 * We may have refs where the parent directory does not exist
3374 * yet. This happens if the parent directories inum is higher
3375 * the the current inum. To handle this case, we create the
3376 * parent directory out of order. But we need to check if this
3377 * did already happen before due to other refs in the same dir.
3378 */
3379 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3380 if (ret < 0)
3381 goto out;
3382 if (ret == inode_state_will_create) {
3383 ret = 0;
3384 /*
3385 * First check if any of the current inodes refs did
3386 * already create the dir.
3387 */
3388 list_for_each_entry(cur2, &sctx->new_refs, list) {
3389 if (cur == cur2)
3390 break;
3391 if (cur2->dir == cur->dir) {
3392 ret = 1;
3393 break;
3394 }
3395 }
3396
3397 /*
3398 * If that did not happen, check if a previous inode
3399 * did already create the dir.
3400 */
3401 if (!ret)
3402 ret = did_create_dir(sctx, cur->dir);
3403 if (ret < 0)
3404 goto out;
3405 if (!ret) {
3406 ret = send_create_inode(sctx, cur->dir);
3407 if (ret < 0)
3408 goto out;
3409 }
3410 }
3411
3412 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003413 * Check if this new ref would overwrite the first ref of
3414 * another unprocessed inode. If yes, orphanize the
3415 * overwritten inode. If we find an overwritten ref that is
3416 * not the first ref, simply unlink it.
3417 */
3418 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3419 cur->name, cur->name_len,
3420 &ow_inode, &ow_gen);
3421 if (ret < 0)
3422 goto out;
3423 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003424 ret = is_first_ref(sctx->parent_root,
3425 ow_inode, cur->dir, cur->name,
3426 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003427 if (ret < 0)
3428 goto out;
3429 if (ret) {
3430 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3431 cur->full_path);
3432 if (ret < 0)
3433 goto out;
3434 } else {
3435 ret = send_unlink(sctx, cur->full_path);
3436 if (ret < 0)
3437 goto out;
3438 }
3439 }
3440
3441 /*
3442 * link/move the ref to the new place. If we have an orphan
3443 * inode, move it and update valid_path. If not, link or move
3444 * it depending on the inode mode.
3445 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003446 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003447 ret = send_rename(sctx, valid_path, cur->full_path);
3448 if (ret < 0)
3449 goto out;
3450 is_orphan = 0;
3451 ret = fs_path_copy(valid_path, cur->full_path);
3452 if (ret < 0)
3453 goto out;
3454 } else {
3455 if (S_ISDIR(sctx->cur_inode_mode)) {
3456 /*
3457 * Dirs can't be linked, so move it. For moved
3458 * dirs, we always have one new and one deleted
3459 * ref. The deleted ref is ignored later.
3460 */
Filipe David Borba Mananad86477b2014-01-30 13:27:12 +00003461 ret = wait_for_parent_move(sctx, cur);
3462 if (ret < 0)
3463 goto out;
3464 if (ret) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003465 ret = add_pending_dir_move(sctx,
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003466 sctx->cur_ino,
3467 sctx->cur_inode_gen,
3468 cur->dir);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003469 *pending_move = 1;
3470 } else {
3471 ret = send_rename(sctx, valid_path,
3472 cur->full_path);
3473 if (!ret)
3474 ret = fs_path_copy(valid_path,
3475 cur->full_path);
3476 }
Alexander Block31db9f72012-07-25 23:19:24 +02003477 if (ret < 0)
3478 goto out;
3479 } else {
3480 ret = send_link(sctx, cur->full_path,
3481 valid_path);
3482 if (ret < 0)
3483 goto out;
3484 }
3485 }
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 }
3490
3491 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3492 /*
3493 * Check if we can already rmdir the directory. If not,
3494 * orphanize it. For every dir item inside that gets deleted
3495 * later, we do this check again and rmdir it then if possible.
3496 * See the use of check_dirs for more details.
3497 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003498 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3499 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003500 if (ret < 0)
3501 goto out;
3502 if (ret) {
3503 ret = send_rmdir(sctx, valid_path);
3504 if (ret < 0)
3505 goto out;
3506 } else if (!is_orphan) {
3507 ret = orphanize_inode(sctx, sctx->cur_ino,
3508 sctx->cur_inode_gen, valid_path);
3509 if (ret < 0)
3510 goto out;
3511 is_orphan = 1;
3512 }
3513
3514 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003515 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003516 if (ret < 0)
3517 goto out;
3518 }
Alexander Blockccf16262012-07-28 11:46:29 +02003519 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3520 !list_empty(&sctx->deleted_refs)) {
3521 /*
3522 * We have a moved dir. Add the old parent to check_dirs
3523 */
3524 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3525 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003526 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003527 if (ret < 0)
3528 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003529 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3530 /*
3531 * We have a non dir inode. Go through all deleted refs and
3532 * unlink them if they were not already overwritten by other
3533 * inodes.
3534 */
3535 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3536 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3537 sctx->cur_ino, sctx->cur_inode_gen,
3538 cur->name, cur->name_len);
3539 if (ret < 0)
3540 goto out;
3541 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003542 ret = send_unlink(sctx, cur->full_path);
3543 if (ret < 0)
3544 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003545 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003546 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003547 if (ret < 0)
3548 goto out;
3549 }
Alexander Block31db9f72012-07-25 23:19:24 +02003550 /*
3551 * If the inode is still orphan, unlink the orphan. This may
3552 * happen when a previous inode did overwrite the first ref
3553 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003554 * inode. Unlinking does not mean that the inode is deleted in
3555 * all cases. There may still be links to this inode in other
3556 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003557 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003558 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003559 ret = send_unlink(sctx, valid_path);
3560 if (ret < 0)
3561 goto out;
3562 }
3563 }
3564
3565 /*
3566 * We did collect all parent dirs where cur_inode was once located. We
3567 * now go through all these dirs and check if they are pending for
3568 * deletion and if it's finally possible to perform the rmdir now.
3569 * We also update the inode stats of the parent dirs here.
3570 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003571 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003572 /*
3573 * In case we had refs into dirs that were not processed yet,
3574 * we don't need to do the utime and rmdir logic for these dirs.
3575 * The dir will be processed later.
3576 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003577 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003578 continue;
3579
Josef Bacikba5e8f22013-08-16 16:52:55 -04003580 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003581 if (ret < 0)
3582 goto out;
3583
3584 if (ret == inode_state_did_create ||
3585 ret == inode_state_no_change) {
3586 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003587 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003588 if (ret < 0)
3589 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003590 } else if (ret == inode_state_did_delete &&
3591 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003592 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3593 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003594 if (ret < 0)
3595 goto out;
3596 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003597 ret = get_cur_path(sctx, cur->dir,
3598 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003599 if (ret < 0)
3600 goto out;
3601 ret = send_rmdir(sctx, valid_path);
3602 if (ret < 0)
3603 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003604 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02003605 }
3606 }
3607 }
3608
Alexander Block31db9f72012-07-25 23:19:24 +02003609 ret = 0;
3610
3611out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04003612 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003613 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003614 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003615 return ret;
3616}
3617
Liu Boa4d96d62014-03-03 21:31:03 +08003618static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3619 struct fs_path *name, void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02003620{
3621 int ret = 0;
3622 struct send_ctx *sctx = ctx;
3623 struct fs_path *p;
3624 u64 gen;
3625
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003626 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003627 if (!p)
3628 return -ENOMEM;
3629
Liu Boa4d96d62014-03-03 21:31:03 +08003630 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003631 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003632 if (ret < 0)
3633 goto out;
3634
Alexander Block31db9f72012-07-25 23:19:24 +02003635 ret = get_cur_path(sctx, dir, gen, p);
3636 if (ret < 0)
3637 goto out;
3638 ret = fs_path_add_path(p, name);
3639 if (ret < 0)
3640 goto out;
3641
Liu Boa4d96d62014-03-03 21:31:03 +08003642 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003643
3644out:
3645 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003646 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003647 return ret;
3648}
3649
Liu Boa4d96d62014-03-03 21:31:03 +08003650static int __record_new_ref(int num, u64 dir, int index,
3651 struct fs_path *name,
3652 void *ctx)
3653{
3654 struct send_ctx *sctx = ctx;
3655 return record_ref(sctx->send_root, num, dir, index, name,
3656 ctx, &sctx->new_refs);
3657}
3658
3659
Alexander Block31db9f72012-07-25 23:19:24 +02003660static int __record_deleted_ref(int num, u64 dir, int index,
3661 struct fs_path *name,
3662 void *ctx)
3663{
Alexander Block31db9f72012-07-25 23:19:24 +02003664 struct send_ctx *sctx = ctx;
Liu Boa4d96d62014-03-03 21:31:03 +08003665 return record_ref(sctx->parent_root, num, dir, index, name,
3666 ctx, &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02003667}
3668
3669static int record_new_ref(struct send_ctx *sctx)
3670{
3671 int ret;
3672
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003673 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3674 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003675 if (ret < 0)
3676 goto out;
3677 ret = 0;
3678
3679out:
3680 return ret;
3681}
3682
3683static int record_deleted_ref(struct send_ctx *sctx)
3684{
3685 int ret;
3686
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003687 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3688 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003689 if (ret < 0)
3690 goto out;
3691 ret = 0;
3692
3693out:
3694 return ret;
3695}
3696
3697struct find_ref_ctx {
3698 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003699 u64 dir_gen;
3700 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003701 struct fs_path *name;
3702 int found_idx;
3703};
3704
3705static int __find_iref(int num, u64 dir, int index,
3706 struct fs_path *name,
3707 void *ctx_)
3708{
3709 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003710 u64 dir_gen;
3711 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003712
3713 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3714 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003715 /*
3716 * To avoid doing extra lookups we'll only do this if everything
3717 * else matches.
3718 */
3719 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3720 NULL, NULL, NULL);
3721 if (ret)
3722 return ret;
3723 if (dir_gen != ctx->dir_gen)
3724 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003725 ctx->found_idx = num;
3726 return 1;
3727 }
3728 return 0;
3729}
3730
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003731static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003732 struct btrfs_path *path,
3733 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003734 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003735{
3736 int ret;
3737 struct find_ref_ctx ctx;
3738
3739 ctx.dir = dir;
3740 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003741 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003742 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003743 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003744
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003745 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003746 if (ret < 0)
3747 return ret;
3748
3749 if (ctx.found_idx == -1)
3750 return -ENOENT;
3751
3752 return ctx.found_idx;
3753}
3754
3755static int __record_changed_new_ref(int num, u64 dir, int index,
3756 struct fs_path *name,
3757 void *ctx)
3758{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003759 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003760 int ret;
3761 struct send_ctx *sctx = ctx;
3762
Josef Bacikba5e8f22013-08-16 16:52:55 -04003763 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3764 NULL, NULL, NULL);
3765 if (ret)
3766 return ret;
3767
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003768 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003769 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003770 if (ret == -ENOENT)
3771 ret = __record_new_ref(num, dir, index, name, sctx);
3772 else if (ret > 0)
3773 ret = 0;
3774
3775 return ret;
3776}
3777
3778static int __record_changed_deleted_ref(int num, u64 dir, int index,
3779 struct fs_path *name,
3780 void *ctx)
3781{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003782 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003783 int ret;
3784 struct send_ctx *sctx = ctx;
3785
Josef Bacikba5e8f22013-08-16 16:52:55 -04003786 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3787 NULL, NULL, NULL);
3788 if (ret)
3789 return ret;
3790
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003791 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003792 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003793 if (ret == -ENOENT)
3794 ret = __record_deleted_ref(num, dir, index, name, sctx);
3795 else if (ret > 0)
3796 ret = 0;
3797
3798 return ret;
3799}
3800
3801static int record_changed_ref(struct send_ctx *sctx)
3802{
3803 int ret = 0;
3804
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003805 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003806 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3807 if (ret < 0)
3808 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003809 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003810 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3811 if (ret < 0)
3812 goto out;
3813 ret = 0;
3814
3815out:
3816 return ret;
3817}
3818
3819/*
3820 * Record and process all refs at once. Needed when an inode changes the
3821 * generation number, which means that it was deleted and recreated.
3822 */
3823static int process_all_refs(struct send_ctx *sctx,
3824 enum btrfs_compare_tree_result cmd)
3825{
3826 int ret;
3827 struct btrfs_root *root;
3828 struct btrfs_path *path;
3829 struct btrfs_key key;
3830 struct btrfs_key found_key;
3831 struct extent_buffer *eb;
3832 int slot;
3833 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003834 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003835
3836 path = alloc_path_for_send();
3837 if (!path)
3838 return -ENOMEM;
3839
3840 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3841 root = sctx->send_root;
3842 cb = __record_new_ref;
3843 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3844 root = sctx->parent_root;
3845 cb = __record_deleted_ref;
3846 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01003847 btrfs_err(sctx->send_root->fs_info,
3848 "Wrong command %d in process_all_refs", cmd);
3849 ret = -EINVAL;
3850 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003851 }
3852
3853 key.objectid = sctx->cmp_key->objectid;
3854 key.type = BTRFS_INODE_REF_KEY;
3855 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003856 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3857 if (ret < 0)
3858 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003859
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003860 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02003861 eb = path->nodes[0];
3862 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003863 if (slot >= btrfs_header_nritems(eb)) {
3864 ret = btrfs_next_leaf(root, path);
3865 if (ret < 0)
3866 goto out;
3867 else if (ret > 0)
3868 break;
3869 continue;
3870 }
3871
Alexander Block31db9f72012-07-25 23:19:24 +02003872 btrfs_item_key_to_cpu(eb, &found_key, slot);
3873
3874 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003875 (found_key.type != BTRFS_INODE_REF_KEY &&
3876 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003877 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003878
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003879 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003880 if (ret < 0)
3881 goto out;
3882
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003883 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003884 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003885 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003886
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003887 ret = process_recorded_refs(sctx, &pending_move);
3888 /* Only applicable to an incremental send. */
3889 ASSERT(pending_move == 0);
Alexander Block31db9f72012-07-25 23:19:24 +02003890
3891out:
3892 btrfs_free_path(path);
3893 return ret;
3894}
3895
3896static int send_set_xattr(struct send_ctx *sctx,
3897 struct fs_path *path,
3898 const char *name, int name_len,
3899 const char *data, int data_len)
3900{
3901 int ret = 0;
3902
3903 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3904 if (ret < 0)
3905 goto out;
3906
3907 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3908 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3909 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3910
3911 ret = send_cmd(sctx);
3912
3913tlv_put_failure:
3914out:
3915 return ret;
3916}
3917
3918static int send_remove_xattr(struct send_ctx *sctx,
3919 struct fs_path *path,
3920 const char *name, int name_len)
3921{
3922 int ret = 0;
3923
3924 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3925 if (ret < 0)
3926 goto out;
3927
3928 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3929 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3930
3931 ret = send_cmd(sctx);
3932
3933tlv_put_failure:
3934out:
3935 return ret;
3936}
3937
3938static int __process_new_xattr(int num, struct btrfs_key *di_key,
3939 const char *name, int name_len,
3940 const char *data, int data_len,
3941 u8 type, void *ctx)
3942{
3943 int ret;
3944 struct send_ctx *sctx = ctx;
3945 struct fs_path *p;
3946 posix_acl_xattr_header dummy_acl;
3947
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003948 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003949 if (!p)
3950 return -ENOMEM;
3951
3952 /*
3953 * This hack is needed because empty acl's are stored as zero byte
3954 * data in xattrs. Problem with that is, that receiving these zero byte
3955 * acl's will fail later. To fix this, we send a dummy acl list that
3956 * only contains the version number and no entries.
3957 */
3958 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3959 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3960 if (data_len == 0) {
3961 dummy_acl.a_version =
3962 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3963 data = (char *)&dummy_acl;
3964 data_len = sizeof(dummy_acl);
3965 }
3966 }
3967
3968 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3969 if (ret < 0)
3970 goto out;
3971
3972 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3973
3974out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003975 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003976 return ret;
3977}
3978
3979static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3980 const char *name, int name_len,
3981 const char *data, int data_len,
3982 u8 type, void *ctx)
3983{
3984 int ret;
3985 struct send_ctx *sctx = ctx;
3986 struct fs_path *p;
3987
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003988 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003989 if (!p)
3990 return -ENOMEM;
3991
3992 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3993 if (ret < 0)
3994 goto out;
3995
3996 ret = send_remove_xattr(sctx, p, name, name_len);
3997
3998out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003999 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004000 return ret;
4001}
4002
4003static int process_new_xattr(struct send_ctx *sctx)
4004{
4005 int ret = 0;
4006
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004007 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4008 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004009
4010 return ret;
4011}
4012
4013static int process_deleted_xattr(struct send_ctx *sctx)
4014{
4015 int ret;
4016
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004017 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4018 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004019
4020 return ret;
4021}
4022
4023struct find_xattr_ctx {
4024 const char *name;
4025 int name_len;
4026 int found_idx;
4027 char *found_data;
4028 int found_data_len;
4029};
4030
4031static int __find_xattr(int num, struct btrfs_key *di_key,
4032 const char *name, int name_len,
4033 const char *data, int data_len,
4034 u8 type, void *vctx)
4035{
4036 struct find_xattr_ctx *ctx = vctx;
4037
4038 if (name_len == ctx->name_len &&
4039 strncmp(name, ctx->name, name_len) == 0) {
4040 ctx->found_idx = num;
4041 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00004042 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02004043 if (!ctx->found_data)
4044 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004045 return 1;
4046 }
4047 return 0;
4048}
4049
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004050static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004051 struct btrfs_path *path,
4052 struct btrfs_key *key,
4053 const char *name, int name_len,
4054 char **data, int *data_len)
4055{
4056 int ret;
4057 struct find_xattr_ctx ctx;
4058
4059 ctx.name = name;
4060 ctx.name_len = name_len;
4061 ctx.found_idx = -1;
4062 ctx.found_data = NULL;
4063 ctx.found_data_len = 0;
4064
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004065 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004066 if (ret < 0)
4067 return ret;
4068
4069 if (ctx.found_idx == -1)
4070 return -ENOENT;
4071 if (data) {
4072 *data = ctx.found_data;
4073 *data_len = ctx.found_data_len;
4074 } else {
4075 kfree(ctx.found_data);
4076 }
4077 return ctx.found_idx;
4078}
4079
4080
4081static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4082 const char *name, int name_len,
4083 const char *data, int data_len,
4084 u8 type, void *ctx)
4085{
4086 int ret;
4087 struct send_ctx *sctx = ctx;
4088 char *found_data = NULL;
4089 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004090
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004091 ret = find_xattr(sctx->parent_root, sctx->right_path,
4092 sctx->cmp_key, name, name_len, &found_data,
4093 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004094 if (ret == -ENOENT) {
4095 ret = __process_new_xattr(num, di_key, name, name_len, data,
4096 data_len, type, ctx);
4097 } else if (ret >= 0) {
4098 if (data_len != found_data_len ||
4099 memcmp(data, found_data, data_len)) {
4100 ret = __process_new_xattr(num, di_key, name, name_len,
4101 data, data_len, type, ctx);
4102 } else {
4103 ret = 0;
4104 }
4105 }
4106
4107 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004108 return ret;
4109}
4110
4111static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4112 const char *name, int name_len,
4113 const char *data, int data_len,
4114 u8 type, void *ctx)
4115{
4116 int ret;
4117 struct send_ctx *sctx = ctx;
4118
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004119 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4120 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004121 if (ret == -ENOENT)
4122 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4123 data_len, type, ctx);
4124 else if (ret >= 0)
4125 ret = 0;
4126
4127 return ret;
4128}
4129
4130static int process_changed_xattr(struct send_ctx *sctx)
4131{
4132 int ret = 0;
4133
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004134 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004135 sctx->cmp_key, __process_changed_new_xattr, sctx);
4136 if (ret < 0)
4137 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004138 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004139 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4140
4141out:
4142 return ret;
4143}
4144
4145static int process_all_new_xattrs(struct send_ctx *sctx)
4146{
4147 int ret;
4148 struct btrfs_root *root;
4149 struct btrfs_path *path;
4150 struct btrfs_key key;
4151 struct btrfs_key found_key;
4152 struct extent_buffer *eb;
4153 int slot;
4154
4155 path = alloc_path_for_send();
4156 if (!path)
4157 return -ENOMEM;
4158
4159 root = sctx->send_root;
4160
4161 key.objectid = sctx->cmp_key->objectid;
4162 key.type = BTRFS_XATTR_ITEM_KEY;
4163 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004164 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4165 if (ret < 0)
4166 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004167
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004168 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004169 eb = path->nodes[0];
4170 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004171 if (slot >= btrfs_header_nritems(eb)) {
4172 ret = btrfs_next_leaf(root, path);
4173 if (ret < 0) {
4174 goto out;
4175 } else if (ret > 0) {
4176 ret = 0;
4177 break;
4178 }
4179 continue;
4180 }
Alexander Block31db9f72012-07-25 23:19:24 +02004181
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004182 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004183 if (found_key.objectid != key.objectid ||
4184 found_key.type != key.type) {
4185 ret = 0;
4186 goto out;
4187 }
4188
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004189 ret = iterate_dir_item(root, path, &found_key,
4190 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004191 if (ret < 0)
4192 goto out;
4193
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004194 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004195 }
4196
4197out:
4198 btrfs_free_path(path);
4199 return ret;
4200}
4201
Josef Baciked259092013-10-25 11:36:01 -04004202static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4203{
4204 struct btrfs_root *root = sctx->send_root;
4205 struct btrfs_fs_info *fs_info = root->fs_info;
4206 struct inode *inode;
4207 struct page *page;
4208 char *addr;
4209 struct btrfs_key key;
4210 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4211 pgoff_t last_index;
4212 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4213 ssize_t ret = 0;
4214
4215 key.objectid = sctx->cur_ino;
4216 key.type = BTRFS_INODE_ITEM_KEY;
4217 key.offset = 0;
4218
4219 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4220 if (IS_ERR(inode))
4221 return PTR_ERR(inode);
4222
4223 if (offset + len > i_size_read(inode)) {
4224 if (offset > i_size_read(inode))
4225 len = 0;
4226 else
4227 len = offset - i_size_read(inode);
4228 }
4229 if (len == 0)
4230 goto out;
4231
4232 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004233
4234 /* initial readahead */
4235 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4236 file_ra_state_init(&sctx->ra, inode->i_mapping);
4237 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4238 last_index - index + 1);
4239
Josef Baciked259092013-10-25 11:36:01 -04004240 while (index <= last_index) {
4241 unsigned cur_len = min_t(unsigned, len,
4242 PAGE_CACHE_SIZE - pg_offset);
4243 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4244 if (!page) {
4245 ret = -ENOMEM;
4246 break;
4247 }
4248
4249 if (!PageUptodate(page)) {
4250 btrfs_readpage(NULL, page);
4251 lock_page(page);
4252 if (!PageUptodate(page)) {
4253 unlock_page(page);
4254 page_cache_release(page);
4255 ret = -EIO;
4256 break;
4257 }
4258 }
4259
4260 addr = kmap(page);
4261 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4262 kunmap(page);
4263 unlock_page(page);
4264 page_cache_release(page);
4265 index++;
4266 pg_offset = 0;
4267 len -= cur_len;
4268 ret += cur_len;
4269 }
4270out:
4271 iput(inode);
4272 return ret;
4273}
4274
Alexander Block31db9f72012-07-25 23:19:24 +02004275/*
4276 * Read some bytes from the current inode/file and send a write command to
4277 * user space.
4278 */
4279static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4280{
4281 int ret = 0;
4282 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004283 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004284
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004285 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004286 if (!p)
4287 return -ENOMEM;
4288
Alexander Block31db9f72012-07-25 23:19:24 +02004289verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4290
Josef Baciked259092013-10-25 11:36:01 -04004291 num_read = fill_read_buf(sctx, offset, len);
4292 if (num_read <= 0) {
4293 if (num_read < 0)
4294 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004295 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004296 }
Alexander Block31db9f72012-07-25 23:19:24 +02004297
4298 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4299 if (ret < 0)
4300 goto out;
4301
4302 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4303 if (ret < 0)
4304 goto out;
4305
4306 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4307 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004308 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004309
4310 ret = send_cmd(sctx);
4311
4312tlv_put_failure:
4313out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004314 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004315 if (ret < 0)
4316 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004317 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004318}
4319
4320/*
4321 * Send a clone command to user space.
4322 */
4323static int send_clone(struct send_ctx *sctx,
4324 u64 offset, u32 len,
4325 struct clone_root *clone_root)
4326{
4327 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004328 struct fs_path *p;
4329 u64 gen;
4330
4331verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4332 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4333 clone_root->root->objectid, clone_root->ino,
4334 clone_root->offset);
4335
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004336 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004337 if (!p)
4338 return -ENOMEM;
4339
4340 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4341 if (ret < 0)
4342 goto out;
4343
4344 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4345 if (ret < 0)
4346 goto out;
4347
4348 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4349 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4350 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4351
Alexander Blocke938c8a2012-07-28 16:33:49 +02004352 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004353 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004354 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004355 if (ret < 0)
4356 goto out;
4357 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4358 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004359 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004360 }
4361 if (ret < 0)
4362 goto out;
4363
4364 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02004365 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004366 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004367 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004368 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4369 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4370 clone_root->offset);
4371
4372 ret = send_cmd(sctx);
4373
4374tlv_put_failure:
4375out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004376 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004377 return ret;
4378}
4379
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004380/*
4381 * Send an update extent command to user space.
4382 */
4383static int send_update_extent(struct send_ctx *sctx,
4384 u64 offset, u32 len)
4385{
4386 int ret = 0;
4387 struct fs_path *p;
4388
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004389 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004390 if (!p)
4391 return -ENOMEM;
4392
4393 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4394 if (ret < 0)
4395 goto out;
4396
4397 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4398 if (ret < 0)
4399 goto out;
4400
4401 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4402 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4403 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4404
4405 ret = send_cmd(sctx);
4406
4407tlv_put_failure:
4408out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004409 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004410 return ret;
4411}
4412
Josef Bacik16e75492013-10-22 12:18:51 -04004413static int send_hole(struct send_ctx *sctx, u64 end)
4414{
4415 struct fs_path *p = NULL;
4416 u64 offset = sctx->cur_inode_last_extent;
4417 u64 len;
4418 int ret = 0;
4419
4420 p = fs_path_alloc();
4421 if (!p)
4422 return -ENOMEM;
4423 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4424 while (offset < end) {
4425 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4426
4427 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4428 if (ret < 0)
4429 break;
4430 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4431 if (ret < 0)
4432 break;
4433 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4434 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4435 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4436 ret = send_cmd(sctx);
4437 if (ret < 0)
4438 break;
4439 offset += len;
4440 }
4441tlv_put_failure:
4442 fs_path_free(p);
4443 return ret;
4444}
4445
Alexander Block31db9f72012-07-25 23:19:24 +02004446static int send_write_or_clone(struct send_ctx *sctx,
4447 struct btrfs_path *path,
4448 struct btrfs_key *key,
4449 struct clone_root *clone_root)
4450{
4451 int ret = 0;
4452 struct btrfs_file_extent_item *ei;
4453 u64 offset = key->offset;
4454 u64 pos = 0;
4455 u64 len;
4456 u32 l;
4457 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004458 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02004459
4460 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4461 struct btrfs_file_extent_item);
4462 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004463 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004464 len = btrfs_file_extent_inline_len(path->nodes[0],
4465 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004466 /*
4467 * it is possible the inline item won't cover the whole page,
4468 * but there may be items after this page. Make
4469 * sure to send the whole thing
4470 */
4471 len = PAGE_CACHE_ALIGN(len);
4472 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004473 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004474 }
Alexander Block31db9f72012-07-25 23:19:24 +02004475
4476 if (offset + len > sctx->cur_inode_size)
4477 len = sctx->cur_inode_size - offset;
4478 if (len == 0) {
4479 ret = 0;
4480 goto out;
4481 }
4482
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004483 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004484 ret = send_clone(sctx, offset, len, clone_root);
4485 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4486 ret = send_update_extent(sctx, offset, len);
4487 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004488 while (pos < len) {
4489 l = len - pos;
4490 if (l > BTRFS_SEND_READ_SIZE)
4491 l = BTRFS_SEND_READ_SIZE;
4492 ret = send_write(sctx, pos + offset, l);
4493 if (ret < 0)
4494 goto out;
4495 if (!ret)
4496 break;
4497 pos += ret;
4498 }
4499 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004500 }
Alexander Block31db9f72012-07-25 23:19:24 +02004501out:
4502 return ret;
4503}
4504
4505static int is_extent_unchanged(struct send_ctx *sctx,
4506 struct btrfs_path *left_path,
4507 struct btrfs_key *ekey)
4508{
4509 int ret = 0;
4510 struct btrfs_key key;
4511 struct btrfs_path *path = NULL;
4512 struct extent_buffer *eb;
4513 int slot;
4514 struct btrfs_key found_key;
4515 struct btrfs_file_extent_item *ei;
4516 u64 left_disknr;
4517 u64 right_disknr;
4518 u64 left_offset;
4519 u64 right_offset;
4520 u64 left_offset_fixed;
4521 u64 left_len;
4522 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04004523 u64 left_gen;
4524 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004525 u8 left_type;
4526 u8 right_type;
4527
4528 path = alloc_path_for_send();
4529 if (!path)
4530 return -ENOMEM;
4531
4532 eb = left_path->nodes[0];
4533 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02004534 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4535 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004536
4537 if (left_type != BTRFS_FILE_EXTENT_REG) {
4538 ret = 0;
4539 goto out;
4540 }
Chris Mason74dd17f2012-08-07 16:25:13 -04004541 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4542 left_len = btrfs_file_extent_num_bytes(eb, ei);
4543 left_offset = btrfs_file_extent_offset(eb, ei);
4544 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004545
4546 /*
4547 * Following comments will refer to these graphics. L is the left
4548 * extents which we are checking at the moment. 1-8 are the right
4549 * extents that we iterate.
4550 *
4551 * |-----L-----|
4552 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4553 *
4554 * |-----L-----|
4555 * |--1--|-2b-|...(same as above)
4556 *
4557 * Alternative situation. Happens on files where extents got split.
4558 * |-----L-----|
4559 * |-----------7-----------|-6-|
4560 *
4561 * Alternative situation. Happens on files which got larger.
4562 * |-----L-----|
4563 * |-8-|
4564 * Nothing follows after 8.
4565 */
4566
4567 key.objectid = ekey->objectid;
4568 key.type = BTRFS_EXTENT_DATA_KEY;
4569 key.offset = ekey->offset;
4570 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4571 if (ret < 0)
4572 goto out;
4573 if (ret) {
4574 ret = 0;
4575 goto out;
4576 }
4577
4578 /*
4579 * Handle special case where the right side has no extents at all.
4580 */
4581 eb = path->nodes[0];
4582 slot = path->slots[0];
4583 btrfs_item_key_to_cpu(eb, &found_key, slot);
4584 if (found_key.objectid != key.objectid ||
4585 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004586 /* If we're a hole then just pretend nothing changed */
4587 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004588 goto out;
4589 }
4590
4591 /*
4592 * We're now on 2a, 2b or 7.
4593 */
4594 key = found_key;
4595 while (key.offset < ekey->offset + left_len) {
4596 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4597 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004598 if (right_type != BTRFS_FILE_EXTENT_REG) {
4599 ret = 0;
4600 goto out;
4601 }
4602
Josef Bacik007d31f2013-10-31 16:49:02 -04004603 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4604 right_len = btrfs_file_extent_num_bytes(eb, ei);
4605 right_offset = btrfs_file_extent_offset(eb, ei);
4606 right_gen = btrfs_file_extent_generation(eb, ei);
4607
Alexander Block31db9f72012-07-25 23:19:24 +02004608 /*
4609 * Are we at extent 8? If yes, we know the extent is changed.
4610 * This may only happen on the first iteration.
4611 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02004612 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004613 /* If we're a hole just pretend nothing changed */
4614 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004615 goto out;
4616 }
4617
4618 left_offset_fixed = left_offset;
4619 if (key.offset < ekey->offset) {
4620 /* Fix the right offset for 2a and 7. */
4621 right_offset += ekey->offset - key.offset;
4622 } else {
4623 /* Fix the left offset for all behind 2a and 2b */
4624 left_offset_fixed += key.offset - ekey->offset;
4625 }
4626
4627 /*
4628 * Check if we have the same extent.
4629 */
Alexander Block39540962012-08-01 12:46:05 +02004630 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04004631 left_offset_fixed != right_offset ||
4632 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02004633 ret = 0;
4634 goto out;
4635 }
4636
4637 /*
4638 * Go to the next extent.
4639 */
4640 ret = btrfs_next_item(sctx->parent_root, path);
4641 if (ret < 0)
4642 goto out;
4643 if (!ret) {
4644 eb = path->nodes[0];
4645 slot = path->slots[0];
4646 btrfs_item_key_to_cpu(eb, &found_key, slot);
4647 }
4648 if (ret || found_key.objectid != key.objectid ||
4649 found_key.type != key.type) {
4650 key.offset += right_len;
4651 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00004652 }
4653 if (found_key.offset != key.offset + right_len) {
4654 ret = 0;
4655 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004656 }
4657 key = found_key;
4658 }
4659
4660 /*
4661 * We're now behind the left extent (treat as unchanged) or at the end
4662 * of the right side (treat as changed).
4663 */
4664 if (key.offset >= ekey->offset + left_len)
4665 ret = 1;
4666 else
4667 ret = 0;
4668
4669
4670out:
4671 btrfs_free_path(path);
4672 return ret;
4673}
4674
Josef Bacik16e75492013-10-22 12:18:51 -04004675static int get_last_extent(struct send_ctx *sctx, u64 offset)
4676{
4677 struct btrfs_path *path;
4678 struct btrfs_root *root = sctx->send_root;
4679 struct btrfs_file_extent_item *fi;
4680 struct btrfs_key key;
4681 u64 extent_end;
4682 u8 type;
4683 int ret;
4684
4685 path = alloc_path_for_send();
4686 if (!path)
4687 return -ENOMEM;
4688
4689 sctx->cur_inode_last_extent = 0;
4690
4691 key.objectid = sctx->cur_ino;
4692 key.type = BTRFS_EXTENT_DATA_KEY;
4693 key.offset = offset;
4694 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4695 if (ret < 0)
4696 goto out;
4697 ret = 0;
4698 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4699 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4700 goto out;
4701
4702 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4703 struct btrfs_file_extent_item);
4704 type = btrfs_file_extent_type(path->nodes[0], fi);
4705 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004706 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4707 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004708 extent_end = ALIGN(key.offset + size,
4709 sctx->send_root->sectorsize);
4710 } else {
4711 extent_end = key.offset +
4712 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4713 }
4714 sctx->cur_inode_last_extent = extent_end;
4715out:
4716 btrfs_free_path(path);
4717 return ret;
4718}
4719
4720static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4721 struct btrfs_key *key)
4722{
4723 struct btrfs_file_extent_item *fi;
4724 u64 extent_end;
4725 u8 type;
4726 int ret = 0;
4727
4728 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4729 return 0;
4730
4731 if (sctx->cur_inode_last_extent == (u64)-1) {
4732 ret = get_last_extent(sctx, key->offset - 1);
4733 if (ret)
4734 return ret;
4735 }
4736
4737 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4738 struct btrfs_file_extent_item);
4739 type = btrfs_file_extent_type(path->nodes[0], fi);
4740 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004741 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4742 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004743 extent_end = ALIGN(key->offset + size,
4744 sctx->send_root->sectorsize);
4745 } else {
4746 extent_end = key->offset +
4747 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4748 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00004749
4750 if (path->slots[0] == 0 &&
4751 sctx->cur_inode_last_extent < key->offset) {
4752 /*
4753 * We might have skipped entire leafs that contained only
4754 * file extent items for our current inode. These leafs have
4755 * a generation number smaller (older) than the one in the
4756 * current leaf and the leaf our last extent came from, and
4757 * are located between these 2 leafs.
4758 */
4759 ret = get_last_extent(sctx, key->offset - 1);
4760 if (ret)
4761 return ret;
4762 }
4763
Josef Bacik16e75492013-10-22 12:18:51 -04004764 if (sctx->cur_inode_last_extent < key->offset)
4765 ret = send_hole(sctx, key->offset);
4766 sctx->cur_inode_last_extent = extent_end;
4767 return ret;
4768}
4769
Alexander Block31db9f72012-07-25 23:19:24 +02004770static int process_extent(struct send_ctx *sctx,
4771 struct btrfs_path *path,
4772 struct btrfs_key *key)
4773{
Alexander Block31db9f72012-07-25 23:19:24 +02004774 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004775 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004776
4777 if (S_ISLNK(sctx->cur_inode_mode))
4778 return 0;
4779
4780 if (sctx->parent_root && !sctx->cur_inode_new) {
4781 ret = is_extent_unchanged(sctx, path, key);
4782 if (ret < 0)
4783 goto out;
4784 if (ret) {
4785 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004786 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004787 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004788 } else {
4789 struct btrfs_file_extent_item *ei;
4790 u8 type;
4791
4792 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4793 struct btrfs_file_extent_item);
4794 type = btrfs_file_extent_type(path->nodes[0], ei);
4795 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4796 type == BTRFS_FILE_EXTENT_REG) {
4797 /*
4798 * The send spec does not have a prealloc command yet,
4799 * so just leave a hole for prealloc'ed extents until
4800 * we have enough commands queued up to justify rev'ing
4801 * the send spec.
4802 */
4803 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4804 ret = 0;
4805 goto out;
4806 }
4807
4808 /* Have a hole, just skip it. */
4809 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4810 ret = 0;
4811 goto out;
4812 }
4813 }
Alexander Block31db9f72012-07-25 23:19:24 +02004814 }
4815
4816 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4817 sctx->cur_inode_size, &found_clone);
4818 if (ret != -ENOENT && ret < 0)
4819 goto out;
4820
4821 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004822 if (ret)
4823 goto out;
4824out_hole:
4825 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004826out:
4827 return ret;
4828}
4829
4830static int process_all_extents(struct send_ctx *sctx)
4831{
4832 int ret;
4833 struct btrfs_root *root;
4834 struct btrfs_path *path;
4835 struct btrfs_key key;
4836 struct btrfs_key found_key;
4837 struct extent_buffer *eb;
4838 int slot;
4839
4840 root = sctx->send_root;
4841 path = alloc_path_for_send();
4842 if (!path)
4843 return -ENOMEM;
4844
4845 key.objectid = sctx->cmp_key->objectid;
4846 key.type = BTRFS_EXTENT_DATA_KEY;
4847 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004848 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4849 if (ret < 0)
4850 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004851
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004852 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004853 eb = path->nodes[0];
4854 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004855
4856 if (slot >= btrfs_header_nritems(eb)) {
4857 ret = btrfs_next_leaf(root, path);
4858 if (ret < 0) {
4859 goto out;
4860 } else if (ret > 0) {
4861 ret = 0;
4862 break;
4863 }
4864 continue;
4865 }
4866
Alexander Block31db9f72012-07-25 23:19:24 +02004867 btrfs_item_key_to_cpu(eb, &found_key, slot);
4868
4869 if (found_key.objectid != key.objectid ||
4870 found_key.type != key.type) {
4871 ret = 0;
4872 goto out;
4873 }
4874
4875 ret = process_extent(sctx, path, &found_key);
4876 if (ret < 0)
4877 goto out;
4878
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004879 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004880 }
4881
4882out:
4883 btrfs_free_path(path);
4884 return ret;
4885}
4886
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004887static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4888 int *pending_move,
4889 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02004890{
4891 int ret = 0;
4892
4893 if (sctx->cur_ino == 0)
4894 goto out;
4895 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004896 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004897 goto out;
4898 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4899 goto out;
4900
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004901 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004902 if (ret < 0)
4903 goto out;
4904
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004905 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004906out:
4907 return ret;
4908}
4909
4910static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4911{
4912 int ret = 0;
4913 u64 left_mode;
4914 u64 left_uid;
4915 u64 left_gid;
4916 u64 right_mode;
4917 u64 right_uid;
4918 u64 right_gid;
4919 int need_chmod = 0;
4920 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004921 int pending_move = 0;
4922 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004923
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004924 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4925 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02004926 if (ret < 0)
4927 goto out;
4928
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004929 /*
4930 * We have processed the refs and thus need to advance send_progress.
4931 * Now, calls to get_cur_xxx will take the updated refs of the current
4932 * inode into account.
4933 *
4934 * On the other hand, if our current inode is a directory and couldn't
4935 * be moved/renamed because its parent was renamed/moved too and it has
4936 * a higher inode number, we can only move/rename our current inode
4937 * after we moved/renamed its parent. Therefore in this case operate on
4938 * the old path (pre move/rename) of our current inode, and the
4939 * move/rename will be performed later.
4940 */
4941 if (refs_processed && !pending_move)
4942 sctx->send_progress = sctx->cur_ino + 1;
4943
Alexander Block31db9f72012-07-25 23:19:24 +02004944 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4945 goto out;
4946 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4947 goto out;
4948
4949 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004950 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004951 if (ret < 0)
4952 goto out;
4953
Alex Lyakase2d044f2012-10-17 13:52:47 +00004954 if (!sctx->parent_root || sctx->cur_inode_new) {
4955 need_chown = 1;
4956 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004957 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004958 } else {
4959 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4960 NULL, NULL, &right_mode, &right_uid,
4961 &right_gid, NULL);
4962 if (ret < 0)
4963 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004964
Alex Lyakase2d044f2012-10-17 13:52:47 +00004965 if (left_uid != right_uid || left_gid != right_gid)
4966 need_chown = 1;
4967 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4968 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004969 }
4970
4971 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04004972 if (need_send_hole(sctx)) {
4973 if (sctx->cur_inode_last_extent == (u64)-1) {
4974 ret = get_last_extent(sctx, (u64)-1);
4975 if (ret)
4976 goto out;
4977 }
4978 if (sctx->cur_inode_last_extent <
4979 sctx->cur_inode_size) {
4980 ret = send_hole(sctx, sctx->cur_inode_size);
4981 if (ret)
4982 goto out;
4983 }
4984 }
Alexander Block31db9f72012-07-25 23:19:24 +02004985 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4986 sctx->cur_inode_size);
4987 if (ret < 0)
4988 goto out;
4989 }
4990
4991 if (need_chown) {
4992 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4993 left_uid, left_gid);
4994 if (ret < 0)
4995 goto out;
4996 }
4997 if (need_chmod) {
4998 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4999 left_mode);
5000 if (ret < 0)
5001 goto out;
5002 }
5003
5004 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005005 * If other directory inodes depended on our current directory
5006 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005007 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005008 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5009 ret = apply_children_dir_moves(sctx);
5010 if (ret)
5011 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005012 /*
5013 * Need to send that every time, no matter if it actually
5014 * changed between the two trees as we have done changes to
5015 * the inode before. If our inode is a directory and it's
5016 * waiting to be moved/renamed, we will send its utimes when
5017 * it's moved/renamed, therefore we don't need to do it here.
5018 */
5019 sctx->send_progress = sctx->cur_ino + 1;
5020 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5021 if (ret < 0)
5022 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005023 }
5024
Alexander Block31db9f72012-07-25 23:19:24 +02005025out:
5026 return ret;
5027}
5028
5029static int changed_inode(struct send_ctx *sctx,
5030 enum btrfs_compare_tree_result result)
5031{
5032 int ret = 0;
5033 struct btrfs_key *key = sctx->cmp_key;
5034 struct btrfs_inode_item *left_ii = NULL;
5035 struct btrfs_inode_item *right_ii = NULL;
5036 u64 left_gen = 0;
5037 u64 right_gen = 0;
5038
Alexander Block31db9f72012-07-25 23:19:24 +02005039 sctx->cur_ino = key->objectid;
5040 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005041 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005042
5043 /*
5044 * Set send_progress to current inode. This will tell all get_cur_xxx
5045 * functions that the current inode's refs are not updated yet. Later,
5046 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5047 */
Alexander Block31db9f72012-07-25 23:19:24 +02005048 sctx->send_progress = sctx->cur_ino;
5049
5050 if (result == BTRFS_COMPARE_TREE_NEW ||
5051 result == BTRFS_COMPARE_TREE_CHANGED) {
5052 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5053 sctx->left_path->slots[0],
5054 struct btrfs_inode_item);
5055 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5056 left_ii);
5057 } else {
5058 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5059 sctx->right_path->slots[0],
5060 struct btrfs_inode_item);
5061 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5062 right_ii);
5063 }
5064 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5065 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5066 sctx->right_path->slots[0],
5067 struct btrfs_inode_item);
5068
5069 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5070 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005071
5072 /*
5073 * The cur_ino = root dir case is special here. We can't treat
5074 * the inode as deleted+reused because it would generate a
5075 * stream that tries to delete/mkdir the root dir.
5076 */
5077 if (left_gen != right_gen &&
5078 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005079 sctx->cur_inode_new_gen = 1;
5080 }
5081
5082 if (result == BTRFS_COMPARE_TREE_NEW) {
5083 sctx->cur_inode_gen = left_gen;
5084 sctx->cur_inode_new = 1;
5085 sctx->cur_inode_deleted = 0;
5086 sctx->cur_inode_size = btrfs_inode_size(
5087 sctx->left_path->nodes[0], left_ii);
5088 sctx->cur_inode_mode = btrfs_inode_mode(
5089 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005090 sctx->cur_inode_rdev = btrfs_inode_rdev(
5091 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005092 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005093 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005094 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5095 sctx->cur_inode_gen = right_gen;
5096 sctx->cur_inode_new = 0;
5097 sctx->cur_inode_deleted = 1;
5098 sctx->cur_inode_size = btrfs_inode_size(
5099 sctx->right_path->nodes[0], right_ii);
5100 sctx->cur_inode_mode = btrfs_inode_mode(
5101 sctx->right_path->nodes[0], right_ii);
5102 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005103 /*
5104 * We need to do some special handling in case the inode was
5105 * reported as changed with a changed generation number. This
5106 * means that the original inode was deleted and new inode
5107 * reused the same inum. So we have to treat the old inode as
5108 * deleted and the new one as new.
5109 */
Alexander Block31db9f72012-07-25 23:19:24 +02005110 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005111 /*
5112 * First, process the inode as if it was deleted.
5113 */
Alexander Block31db9f72012-07-25 23:19:24 +02005114 sctx->cur_inode_gen = right_gen;
5115 sctx->cur_inode_new = 0;
5116 sctx->cur_inode_deleted = 1;
5117 sctx->cur_inode_size = btrfs_inode_size(
5118 sctx->right_path->nodes[0], right_ii);
5119 sctx->cur_inode_mode = btrfs_inode_mode(
5120 sctx->right_path->nodes[0], right_ii);
5121 ret = process_all_refs(sctx,
5122 BTRFS_COMPARE_TREE_DELETED);
5123 if (ret < 0)
5124 goto out;
5125
Alexander Block766702e2012-07-28 14:11:31 +02005126 /*
5127 * Now process the inode as if it was new.
5128 */
Alexander Block31db9f72012-07-25 23:19:24 +02005129 sctx->cur_inode_gen = left_gen;
5130 sctx->cur_inode_new = 1;
5131 sctx->cur_inode_deleted = 0;
5132 sctx->cur_inode_size = btrfs_inode_size(
5133 sctx->left_path->nodes[0], left_ii);
5134 sctx->cur_inode_mode = btrfs_inode_mode(
5135 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005136 sctx->cur_inode_rdev = btrfs_inode_rdev(
5137 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005138 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005139 if (ret < 0)
5140 goto out;
5141
5142 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5143 if (ret < 0)
5144 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005145 /*
5146 * Advance send_progress now as we did not get into
5147 * process_recorded_refs_if_needed in the new_gen case.
5148 */
5149 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005150
5151 /*
5152 * Now process all extents and xattrs of the inode as if
5153 * they were all new.
5154 */
Alexander Block31db9f72012-07-25 23:19:24 +02005155 ret = process_all_extents(sctx);
5156 if (ret < 0)
5157 goto out;
5158 ret = process_all_new_xattrs(sctx);
5159 if (ret < 0)
5160 goto out;
5161 } else {
5162 sctx->cur_inode_gen = left_gen;
5163 sctx->cur_inode_new = 0;
5164 sctx->cur_inode_new_gen = 0;
5165 sctx->cur_inode_deleted = 0;
5166 sctx->cur_inode_size = btrfs_inode_size(
5167 sctx->left_path->nodes[0], left_ii);
5168 sctx->cur_inode_mode = btrfs_inode_mode(
5169 sctx->left_path->nodes[0], left_ii);
5170 }
5171 }
5172
5173out:
5174 return ret;
5175}
5176
Alexander Block766702e2012-07-28 14:11:31 +02005177/*
5178 * We have to process new refs before deleted refs, but compare_trees gives us
5179 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5180 * first and later process them in process_recorded_refs.
5181 * For the cur_inode_new_gen case, we skip recording completely because
5182 * changed_inode did already initiate processing of refs. The reason for this is
5183 * that in this case, compare_tree actually compares the refs of 2 different
5184 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5185 * refs of the right tree as deleted and all refs of the left tree as new.
5186 */
Alexander Block31db9f72012-07-25 23:19:24 +02005187static int changed_ref(struct send_ctx *sctx,
5188 enum btrfs_compare_tree_result result)
5189{
5190 int ret = 0;
5191
5192 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5193
5194 if (!sctx->cur_inode_new_gen &&
5195 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5196 if (result == BTRFS_COMPARE_TREE_NEW)
5197 ret = record_new_ref(sctx);
5198 else if (result == BTRFS_COMPARE_TREE_DELETED)
5199 ret = record_deleted_ref(sctx);
5200 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5201 ret = record_changed_ref(sctx);
5202 }
5203
5204 return ret;
5205}
5206
Alexander Block766702e2012-07-28 14:11:31 +02005207/*
5208 * Process new/deleted/changed xattrs. We skip processing in the
5209 * cur_inode_new_gen case because changed_inode did already initiate processing
5210 * of xattrs. The reason is the same as in changed_ref
5211 */
Alexander Block31db9f72012-07-25 23:19:24 +02005212static int changed_xattr(struct send_ctx *sctx,
5213 enum btrfs_compare_tree_result result)
5214{
5215 int ret = 0;
5216
5217 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5218
5219 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5220 if (result == BTRFS_COMPARE_TREE_NEW)
5221 ret = process_new_xattr(sctx);
5222 else if (result == BTRFS_COMPARE_TREE_DELETED)
5223 ret = process_deleted_xattr(sctx);
5224 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5225 ret = process_changed_xattr(sctx);
5226 }
5227
5228 return ret;
5229}
5230
Alexander Block766702e2012-07-28 14:11:31 +02005231/*
5232 * Process new/deleted/changed extents. We skip processing in the
5233 * cur_inode_new_gen case because changed_inode did already initiate processing
5234 * of extents. The reason is the same as in changed_ref
5235 */
Alexander Block31db9f72012-07-25 23:19:24 +02005236static int changed_extent(struct send_ctx *sctx,
5237 enum btrfs_compare_tree_result result)
5238{
5239 int ret = 0;
5240
5241 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5242
5243 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5244 if (result != BTRFS_COMPARE_TREE_DELETED)
5245 ret = process_extent(sctx, sctx->left_path,
5246 sctx->cmp_key);
5247 }
5248
5249 return ret;
5250}
5251
Josef Bacikba5e8f22013-08-16 16:52:55 -04005252static int dir_changed(struct send_ctx *sctx, u64 dir)
5253{
5254 u64 orig_gen, new_gen;
5255 int ret;
5256
5257 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5258 NULL, NULL);
5259 if (ret)
5260 return ret;
5261
5262 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5263 NULL, NULL, NULL);
5264 if (ret)
5265 return ret;
5266
5267 return (orig_gen != new_gen) ? 1 : 0;
5268}
5269
5270static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5271 struct btrfs_key *key)
5272{
5273 struct btrfs_inode_extref *extref;
5274 struct extent_buffer *leaf;
5275 u64 dirid = 0, last_dirid = 0;
5276 unsigned long ptr;
5277 u32 item_size;
5278 u32 cur_offset = 0;
5279 int ref_name_len;
5280 int ret = 0;
5281
5282 /* Easy case, just check this one dirid */
5283 if (key->type == BTRFS_INODE_REF_KEY) {
5284 dirid = key->offset;
5285
5286 ret = dir_changed(sctx, dirid);
5287 goto out;
5288 }
5289
5290 leaf = path->nodes[0];
5291 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5292 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5293 while (cur_offset < item_size) {
5294 extref = (struct btrfs_inode_extref *)(ptr +
5295 cur_offset);
5296 dirid = btrfs_inode_extref_parent(leaf, extref);
5297 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5298 cur_offset += ref_name_len + sizeof(*extref);
5299 if (dirid == last_dirid)
5300 continue;
5301 ret = dir_changed(sctx, dirid);
5302 if (ret)
5303 break;
5304 last_dirid = dirid;
5305 }
5306out:
5307 return ret;
5308}
5309
Alexander Block766702e2012-07-28 14:11:31 +02005310/*
5311 * Updates compare related fields in sctx and simply forwards to the actual
5312 * changed_xxx functions.
5313 */
Alexander Block31db9f72012-07-25 23:19:24 +02005314static int changed_cb(struct btrfs_root *left_root,
5315 struct btrfs_root *right_root,
5316 struct btrfs_path *left_path,
5317 struct btrfs_path *right_path,
5318 struct btrfs_key *key,
5319 enum btrfs_compare_tree_result result,
5320 void *ctx)
5321{
5322 int ret = 0;
5323 struct send_ctx *sctx = ctx;
5324
Josef Bacikba5e8f22013-08-16 16:52:55 -04005325 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005326 if (key->type == BTRFS_INODE_REF_KEY ||
5327 key->type == BTRFS_INODE_EXTREF_KEY) {
5328 ret = compare_refs(sctx, left_path, key);
5329 if (!ret)
5330 return 0;
5331 if (ret < 0)
5332 return ret;
5333 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5334 return maybe_send_hole(sctx, left_path, key);
5335 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005336 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005337 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005338 result = BTRFS_COMPARE_TREE_CHANGED;
5339 ret = 0;
5340 }
5341
Alexander Block31db9f72012-07-25 23:19:24 +02005342 sctx->left_path = left_path;
5343 sctx->right_path = right_path;
5344 sctx->cmp_key = key;
5345
5346 ret = finish_inode_if_needed(sctx, 0);
5347 if (ret < 0)
5348 goto out;
5349
Alexander Block2981e222012-08-01 14:47:03 +02005350 /* Ignore non-FS objects */
5351 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5352 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5353 goto out;
5354
Alexander Block31db9f72012-07-25 23:19:24 +02005355 if (key->type == BTRFS_INODE_ITEM_KEY)
5356 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005357 else if (key->type == BTRFS_INODE_REF_KEY ||
5358 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005359 ret = changed_ref(sctx, result);
5360 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5361 ret = changed_xattr(sctx, result);
5362 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5363 ret = changed_extent(sctx, result);
5364
5365out:
5366 return ret;
5367}
5368
5369static int full_send_tree(struct send_ctx *sctx)
5370{
5371 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005372 struct btrfs_root *send_root = sctx->send_root;
5373 struct btrfs_key key;
5374 struct btrfs_key found_key;
5375 struct btrfs_path *path;
5376 struct extent_buffer *eb;
5377 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02005378
5379 path = alloc_path_for_send();
5380 if (!path)
5381 return -ENOMEM;
5382
Alexander Block31db9f72012-07-25 23:19:24 +02005383 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5384 key.type = BTRFS_INODE_ITEM_KEY;
5385 key.offset = 0;
5386
Alexander Block31db9f72012-07-25 23:19:24 +02005387 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5388 if (ret < 0)
5389 goto out;
5390 if (ret)
5391 goto out_finish;
5392
5393 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005394 eb = path->nodes[0];
5395 slot = path->slots[0];
5396 btrfs_item_key_to_cpu(eb, &found_key, slot);
5397
5398 ret = changed_cb(send_root, NULL, path, NULL,
5399 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5400 if (ret < 0)
5401 goto out;
5402
5403 key.objectid = found_key.objectid;
5404 key.type = found_key.type;
5405 key.offset = found_key.offset + 1;
5406
5407 ret = btrfs_next_item(send_root, path);
5408 if (ret < 0)
5409 goto out;
5410 if (ret) {
5411 ret = 0;
5412 break;
5413 }
5414 }
5415
5416out_finish:
5417 ret = finish_inode_if_needed(sctx, 1);
5418
5419out:
5420 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02005421 return ret;
5422}
5423
5424static int send_subvol(struct send_ctx *sctx)
5425{
5426 int ret;
5427
Stefan Behrensc2c71322013-04-10 17:10:52 +00005428 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5429 ret = send_header(sctx);
5430 if (ret < 0)
5431 goto out;
5432 }
Alexander Block31db9f72012-07-25 23:19:24 +02005433
5434 ret = send_subvol_begin(sctx);
5435 if (ret < 0)
5436 goto out;
5437
5438 if (sctx->parent_root) {
5439 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5440 changed_cb, sctx);
5441 if (ret < 0)
5442 goto out;
5443 ret = finish_inode_if_needed(sctx, 1);
5444 if (ret < 0)
5445 goto out;
5446 } else {
5447 ret = full_send_tree(sctx);
5448 if (ret < 0)
5449 goto out;
5450 }
5451
5452out:
Alexander Block31db9f72012-07-25 23:19:24 +02005453 free_recorded_refs(sctx);
5454 return ret;
5455}
5456
David Sterba66ef7d62013-12-17 15:07:20 +01005457static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5458{
5459 spin_lock(&root->root_item_lock);
5460 root->send_in_progress--;
5461 /*
5462 * Not much left to do, we don't know why it's unbalanced and
5463 * can't blindly reset it to 0.
5464 */
5465 if (root->send_in_progress < 0)
5466 btrfs_err(root->fs_info,
5467 "send_in_progres unbalanced %d root %llu\n",
5468 root->send_in_progress, root->root_key.objectid);
5469 spin_unlock(&root->root_item_lock);
5470}
5471
Alexander Block31db9f72012-07-25 23:19:24 +02005472long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5473{
5474 int ret = 0;
5475 struct btrfs_root *send_root;
5476 struct btrfs_root *clone_root;
5477 struct btrfs_fs_info *fs_info;
5478 struct btrfs_ioctl_send_args *arg = NULL;
5479 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02005480 struct send_ctx *sctx = NULL;
5481 u32 i;
5482 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01005483 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08005484 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08005485 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02005486
5487 if (!capable(CAP_SYS_ADMIN))
5488 return -EPERM;
5489
Al Viro496ad9a2013-01-23 17:07:38 -05005490 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02005491 fs_info = send_root->fs_info;
5492
Josef Bacik139f8072013-05-20 11:26:50 -04005493 /*
David Sterba2c686532013-12-16 17:34:17 +01005494 * The subvolume must remain read-only during send, protect against
5495 * making it RW.
5496 */
5497 spin_lock(&send_root->root_item_lock);
5498 send_root->send_in_progress++;
5499 spin_unlock(&send_root->root_item_lock);
5500
5501 /*
Josef Bacik139f8072013-05-20 11:26:50 -04005502 * This is done when we lookup the root, it should already be complete
5503 * by the time we get here.
5504 */
5505 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5506
5507 /*
David Sterba2c686532013-12-16 17:34:17 +01005508 * Userspace tools do the checks and warn the user if it's
5509 * not RO.
5510 */
5511 if (!btrfs_root_readonly(send_root)) {
5512 ret = -EPERM;
5513 goto out;
5514 }
5515
Alexander Block31db9f72012-07-25 23:19:24 +02005516 arg = memdup_user(arg_, sizeof(*arg));
5517 if (IS_ERR(arg)) {
5518 ret = PTR_ERR(arg);
5519 arg = NULL;
5520 goto out;
5521 }
5522
5523 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05005524 sizeof(*arg->clone_sources) *
5525 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02005526 ret = -EFAULT;
5527 goto out;
5528 }
5529
Stefan Behrensc2c71322013-04-10 17:10:52 +00005530 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005531 ret = -EINVAL;
5532 goto out;
5533 }
5534
Alexander Block31db9f72012-07-25 23:19:24 +02005535 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5536 if (!sctx) {
5537 ret = -ENOMEM;
5538 goto out;
5539 }
5540
5541 INIT_LIST_HEAD(&sctx->new_refs);
5542 INIT_LIST_HEAD(&sctx->deleted_refs);
5543 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5544 INIT_LIST_HEAD(&sctx->name_cache_list);
5545
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005546 sctx->flags = arg->flags;
5547
Alexander Block31db9f72012-07-25 23:19:24 +02005548 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00005549 if (!sctx->send_filp) {
5550 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02005551 goto out;
5552 }
5553
Alexander Block31db9f72012-07-25 23:19:24 +02005554 sctx->send_root = send_root;
5555 sctx->clone_roots_cnt = arg->clone_sources_count;
5556
5557 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5558 sctx->send_buf = vmalloc(sctx->send_max_size);
5559 if (!sctx->send_buf) {
5560 ret = -ENOMEM;
5561 goto out;
5562 }
5563
5564 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5565 if (!sctx->read_buf) {
5566 ret = -ENOMEM;
5567 goto out;
5568 }
5569
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005570 sctx->pending_dir_moves = RB_ROOT;
5571 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00005572 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005573
Alexander Block31db9f72012-07-25 23:19:24 +02005574 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5575 (arg->clone_sources_count + 1));
5576 if (!sctx->clone_roots) {
5577 ret = -ENOMEM;
5578 goto out;
5579 }
5580
5581 if (arg->clone_sources_count) {
5582 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5583 sizeof(*arg->clone_sources));
5584 if (!clone_sources_tmp) {
5585 ret = -ENOMEM;
5586 goto out;
5587 }
5588
5589 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5590 arg->clone_sources_count *
5591 sizeof(*arg->clone_sources));
5592 if (ret) {
5593 ret = -EFAULT;
5594 goto out;
5595 }
5596
5597 for (i = 0; i < arg->clone_sources_count; i++) {
5598 key.objectid = clone_sources_tmp[i];
5599 key.type = BTRFS_ROOT_ITEM_KEY;
5600 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005601
5602 index = srcu_read_lock(&fs_info->subvol_srcu);
5603
Alexander Block31db9f72012-07-25 23:19:24 +02005604 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02005605 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005606 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005607 ret = PTR_ERR(clone_root);
5608 goto out;
5609 }
David Sterba2c686532013-12-16 17:34:17 +01005610 clone_sources_to_rollback = i + 1;
5611 spin_lock(&clone_root->root_item_lock);
5612 clone_root->send_in_progress++;
5613 if (!btrfs_root_readonly(clone_root)) {
5614 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005615 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005616 ret = -EPERM;
5617 goto out;
5618 }
5619 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005620 srcu_read_unlock(&fs_info->subvol_srcu, index);
5621
Alexander Block31db9f72012-07-25 23:19:24 +02005622 sctx->clone_roots[i].root = clone_root;
5623 }
5624 vfree(clone_sources_tmp);
5625 clone_sources_tmp = NULL;
5626 }
5627
5628 if (arg->parent_root) {
5629 key.objectid = arg->parent_root;
5630 key.type = BTRFS_ROOT_ITEM_KEY;
5631 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005632
5633 index = srcu_read_lock(&fs_info->subvol_srcu);
5634
Alexander Block31db9f72012-07-25 23:19:24 +02005635 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005636 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005637 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005638 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02005639 goto out;
5640 }
Wang Shilong18f687d2014-01-07 17:25:19 +08005641
David Sterba2c686532013-12-16 17:34:17 +01005642 spin_lock(&sctx->parent_root->root_item_lock);
5643 sctx->parent_root->send_in_progress++;
5644 if (!btrfs_root_readonly(sctx->parent_root)) {
5645 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005646 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005647 ret = -EPERM;
5648 goto out;
5649 }
5650 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005651
5652 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005653 }
5654
5655 /*
5656 * Clones from send_root are allowed, but only if the clone source
5657 * is behind the current send position. This is checked while searching
5658 * for possible clone sources.
5659 */
5660 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5661
5662 /* We do a bsearch later */
5663 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5664 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5665 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08005666 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005667
Josef Bacika26e8c92014-03-28 17:07:27 -04005668 current->journal_info = (void *)BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02005669 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04005670 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02005671 if (ret < 0)
5672 goto out;
5673
Stefan Behrensc2c71322013-04-10 17:10:52 +00005674 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5675 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5676 if (ret < 0)
5677 goto out;
5678 ret = send_cmd(sctx);
5679 if (ret < 0)
5680 goto out;
5681 }
Alexander Block31db9f72012-07-25 23:19:24 +02005682
5683out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005684 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5685 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5686 struct rb_node *n;
5687 struct pending_dir_move *pm;
5688
5689 n = rb_first(&sctx->pending_dir_moves);
5690 pm = rb_entry(n, struct pending_dir_move, node);
5691 while (!list_empty(&pm->list)) {
5692 struct pending_dir_move *pm2;
5693
5694 pm2 = list_first_entry(&pm->list,
5695 struct pending_dir_move, list);
5696 free_pending_move(sctx, pm2);
5697 }
5698 free_pending_move(sctx, pm);
5699 }
5700
5701 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5702 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5703 struct rb_node *n;
5704 struct waiting_dir_move *dm;
5705
5706 n = rb_first(&sctx->waiting_dir_moves);
5707 dm = rb_entry(n, struct waiting_dir_move, node);
5708 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5709 kfree(dm);
5710 }
5711
Filipe Manana9dc44212014-02-19 14:31:44 +00005712 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5713 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5714 struct rb_node *n;
5715 struct orphan_dir_info *odi;
5716
5717 n = rb_first(&sctx->orphan_dirs);
5718 odi = rb_entry(n, struct orphan_dir_info, node);
5719 free_orphan_dir_info(sctx, odi);
5720 }
5721
Wang Shilong896c14f2014-01-07 17:25:18 +08005722 if (sort_clone_roots) {
5723 for (i = 0; i < sctx->clone_roots_cnt; i++)
5724 btrfs_root_dec_send_in_progress(
5725 sctx->clone_roots[i].root);
5726 } else {
5727 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5728 btrfs_root_dec_send_in_progress(
5729 sctx->clone_roots[i].root);
5730
5731 btrfs_root_dec_send_in_progress(send_root);
5732 }
David Sterba66ef7d62013-12-17 15:07:20 +01005733 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5734 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01005735
Alexander Block31db9f72012-07-25 23:19:24 +02005736 kfree(arg);
5737 vfree(clone_sources_tmp);
5738
5739 if (sctx) {
5740 if (sctx->send_filp)
5741 fput(sctx->send_filp);
5742
5743 vfree(sctx->clone_roots);
5744 vfree(sctx->send_buf);
5745 vfree(sctx->read_buf);
5746
5747 name_cache_free(sctx);
5748
5749 kfree(sctx);
5750 }
5751
5752 return ret;
5753}