blob: fe5857223515d14753c1f75ce60c2c12e16a3e67 [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
Chris Masoncfd4a532014-04-26 05:02:03 -0700352 if (len > PATH_MAX) {
353 WARN_ON(1);
354 return -ENOMEM;
355 }
356
David Sterba1b2782c2014-02-25 19:32:59 +0100357 path_len = p->end - p->start;
358 old_buf_len = p->buf_len;
359
David Sterbaace01052014-02-05 16:17:34 +0100360 /*
361 * First time the inline_buf does not suffice
362 */
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100363 if (p->buf == p->inline_buf) {
David Sterba9c9ca002014-02-25 19:33:08 +0100364 tmp_buf = kmalloc(len, GFP_NOFS);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100365 if (tmp_buf)
366 memcpy(tmp_buf, p->buf, old_buf_len);
367 } else {
David Sterba9c9ca002014-02-25 19:33:08 +0100368 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
Filipe Manana01a9a8a2014-05-21 17:38:13 +0100369 }
David Sterba9c9ca002014-02-25 19:33:08 +0100370 if (!tmp_buf)
371 return -ENOMEM;
372 p->buf = tmp_buf;
373 /*
374 * The real size of the buffer is bigger, this will let the fast path
375 * happen most of the time
376 */
377 p->buf_len = ksize(p->buf);
David Sterbaace01052014-02-05 16:17:34 +0100378
Alexander Block31db9f72012-07-25 23:19:24 +0200379 if (p->reversed) {
380 tmp_buf = p->buf + old_buf_len - path_len - 1;
381 p->end = p->buf + p->buf_len - 1;
382 p->start = p->end - path_len;
383 memmove(p->start, tmp_buf, path_len + 1);
384 } else {
385 p->start = p->buf;
386 p->end = p->start + path_len;
387 }
388 return 0;
389}
390
David Sterbab23ab572014-02-03 19:23:19 +0100391static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
392 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200393{
394 int ret;
395 int new_len;
396
397 new_len = p->end - p->start + name_len;
398 if (p->start != p->end)
399 new_len++;
400 ret = fs_path_ensure_buf(p, new_len);
401 if (ret < 0)
402 goto out;
403
404 if (p->reversed) {
405 if (p->start != p->end)
406 *--p->start = '/';
407 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100408 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200409 } else {
410 if (p->start != p->end)
411 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100412 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200413 p->end += name_len;
414 *p->end = 0;
415 }
416
417out:
418 return ret;
419}
420
421static int fs_path_add(struct fs_path *p, const char *name, int name_len)
422{
423 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100424 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200425
David Sterbab23ab572014-02-03 19:23:19 +0100426 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200427 if (ret < 0)
428 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100429 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200430
431out:
432 return ret;
433}
434
435static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
436{
437 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100438 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200439
David Sterbab23ab572014-02-03 19:23:19 +0100440 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200441 if (ret < 0)
442 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100443 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200444
445out:
446 return ret;
447}
448
449static int fs_path_add_from_extent_buffer(struct fs_path *p,
450 struct extent_buffer *eb,
451 unsigned long off, int len)
452{
453 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100454 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200455
David Sterbab23ab572014-02-03 19:23:19 +0100456 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200457 if (ret < 0)
458 goto out;
459
David Sterbab23ab572014-02-03 19:23:19 +0100460 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200461
462out:
463 return ret;
464}
465
Alexander Block31db9f72012-07-25 23:19:24 +0200466static int fs_path_copy(struct fs_path *p, struct fs_path *from)
467{
468 int ret;
469
470 p->reversed = from->reversed;
471 fs_path_reset(p);
472
473 ret = fs_path_add_path(p, from);
474
475 return ret;
476}
477
478
479static void fs_path_unreverse(struct fs_path *p)
480{
481 char *tmp;
482 int len;
483
484 if (!p->reversed)
485 return;
486
487 tmp = p->start;
488 len = p->end - p->start;
489 p->start = p->buf;
490 p->end = p->start + len;
491 memmove(p->start, tmp, len + 1);
492 p->reversed = 0;
493}
494
495static struct btrfs_path *alloc_path_for_send(void)
496{
497 struct btrfs_path *path;
498
499 path = btrfs_alloc_path();
500 if (!path)
501 return NULL;
502 path->search_commit_root = 1;
503 path->skip_locking = 1;
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400504 path->need_commit_sem = 1;
Alexander Block31db9f72012-07-25 23:19:24 +0200505 return path;
506}
507
Eric Sandeen48a3b632013-04-25 20:41:01 +0000508static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200509{
510 int ret;
511 mm_segment_t old_fs;
512 u32 pos = 0;
513
514 old_fs = get_fs();
515 set_fs(KERNEL_DS);
516
517 while (pos < len) {
Fabian Frederickd447d0d2014-07-15 21:17:17 +0200518 ret = vfs_write(filp, (__force const char __user *)buf + pos,
519 len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200520 /* TODO handle that correctly */
521 /*if (ret == -ERESTARTSYS) {
522 continue;
523 }*/
524 if (ret < 0)
525 goto out;
526 if (ret == 0) {
527 ret = -EIO;
528 goto out;
529 }
530 pos += ret;
531 }
532
533 ret = 0;
534
535out:
536 set_fs(old_fs);
537 return ret;
538}
539
540static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
541{
542 struct btrfs_tlv_header *hdr;
543 int total_len = sizeof(*hdr) + len;
544 int left = sctx->send_max_size - sctx->send_size;
545
546 if (unlikely(left < total_len))
547 return -EOVERFLOW;
548
549 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
550 hdr->tlv_type = cpu_to_le16(attr);
551 hdr->tlv_len = cpu_to_le16(len);
552 memcpy(hdr + 1, data, len);
553 sctx->send_size += total_len;
554
555 return 0;
556}
557
David Sterba95bc79d2013-12-16 17:34:10 +0100558#define TLV_PUT_DEFINE_INT(bits) \
559 static int tlv_put_u##bits(struct send_ctx *sctx, \
560 u##bits attr, u##bits value) \
561 { \
562 __le##bits __tmp = cpu_to_le##bits(value); \
563 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
564 }
Alexander Block31db9f72012-07-25 23:19:24 +0200565
David Sterba95bc79d2013-12-16 17:34:10 +0100566TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200567
568static int tlv_put_string(struct send_ctx *sctx, u16 attr,
569 const char *str, int len)
570{
571 if (len == -1)
572 len = strlen(str);
573 return tlv_put(sctx, attr, str, len);
574}
575
576static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
577 const u8 *uuid)
578{
579 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
580}
581
Alexander Block31db9f72012-07-25 23:19:24 +0200582static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
583 struct extent_buffer *eb,
584 struct btrfs_timespec *ts)
585{
586 struct btrfs_timespec bts;
587 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
588 return tlv_put(sctx, attr, &bts, sizeof(bts));
589}
590
591
592#define TLV_PUT(sctx, attrtype, attrlen, data) \
593 do { \
594 ret = tlv_put(sctx, attrtype, attrlen, data); \
595 if (ret < 0) \
596 goto tlv_put_failure; \
597 } while (0)
598
599#define TLV_PUT_INT(sctx, attrtype, bits, value) \
600 do { \
601 ret = tlv_put_u##bits(sctx, attrtype, value); \
602 if (ret < 0) \
603 goto tlv_put_failure; \
604 } while (0)
605
606#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
607#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
608#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
609#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
610#define TLV_PUT_STRING(sctx, attrtype, str, len) \
611 do { \
612 ret = tlv_put_string(sctx, attrtype, str, len); \
613 if (ret < 0) \
614 goto tlv_put_failure; \
615 } while (0)
616#define TLV_PUT_PATH(sctx, attrtype, p) \
617 do { \
618 ret = tlv_put_string(sctx, attrtype, p->start, \
619 p->end - p->start); \
620 if (ret < 0) \
621 goto tlv_put_failure; \
622 } while(0)
623#define TLV_PUT_UUID(sctx, attrtype, uuid) \
624 do { \
625 ret = tlv_put_uuid(sctx, attrtype, uuid); \
626 if (ret < 0) \
627 goto tlv_put_failure; \
628 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200629#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
630 do { \
631 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
632 if (ret < 0) \
633 goto tlv_put_failure; \
634 } while (0)
635
636static int send_header(struct send_ctx *sctx)
637{
638 struct btrfs_stream_header hdr;
639
640 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
641 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
642
Anand Jain1bcea352012-09-14 00:04:21 -0600643 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
644 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200645}
646
647/*
648 * For each command/item we want to send to userspace, we call this function.
649 */
650static int begin_cmd(struct send_ctx *sctx, int cmd)
651{
652 struct btrfs_cmd_header *hdr;
653
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530654 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200655 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200656
657 BUG_ON(sctx->send_size);
658
659 sctx->send_size += sizeof(*hdr);
660 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
661 hdr->cmd = cpu_to_le16(cmd);
662
663 return 0;
664}
665
666static int send_cmd(struct send_ctx *sctx)
667{
668 int ret;
669 struct btrfs_cmd_header *hdr;
670 u32 crc;
671
672 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
673 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
674 hdr->crc = 0;
675
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000676 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200677 hdr->crc = cpu_to_le32(crc);
678
Anand Jain1bcea352012-09-14 00:04:21 -0600679 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
680 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200681
682 sctx->total_send_size += sctx->send_size;
683 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
684 sctx->send_size = 0;
685
686 return ret;
687}
688
689/*
690 * Sends a move instruction to user space
691 */
692static int send_rename(struct send_ctx *sctx,
693 struct fs_path *from, struct fs_path *to)
694{
695 int ret;
696
697verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
698
699 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
700 if (ret < 0)
701 goto out;
702
703 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
704 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
705
706 ret = send_cmd(sctx);
707
708tlv_put_failure:
709out:
710 return ret;
711}
712
713/*
714 * Sends a link instruction to user space
715 */
716static int send_link(struct send_ctx *sctx,
717 struct fs_path *path, struct fs_path *lnk)
718{
719 int ret;
720
721verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
722
723 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
724 if (ret < 0)
725 goto out;
726
727 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
728 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
729
730 ret = send_cmd(sctx);
731
732tlv_put_failure:
733out:
734 return ret;
735}
736
737/*
738 * Sends an unlink instruction to user space
739 */
740static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
741{
742 int ret;
743
744verbose_printk("btrfs: send_unlink %s\n", path->start);
745
746 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
747 if (ret < 0)
748 goto out;
749
750 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
751
752 ret = send_cmd(sctx);
753
754tlv_put_failure:
755out:
756 return ret;
757}
758
759/*
760 * Sends a rmdir instruction to user space
761 */
762static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
763{
764 int ret;
765
766verbose_printk("btrfs: send_rmdir %s\n", path->start);
767
768 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
769 if (ret < 0)
770 goto out;
771
772 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
773
774 ret = send_cmd(sctx);
775
776tlv_put_failure:
777out:
778 return ret;
779}
780
781/*
782 * Helper function to retrieve some fields from an inode item.
783 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400784static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
785 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
786 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200787{
788 int ret;
789 struct btrfs_inode_item *ii;
790 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200791
792 key.objectid = ino;
793 key.type = BTRFS_INODE_ITEM_KEY;
794 key.offset = 0;
795 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200796 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400797 if (ret > 0)
798 ret = -ENOENT;
799 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200800 }
801
802 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
803 struct btrfs_inode_item);
804 if (size)
805 *size = btrfs_inode_size(path->nodes[0], ii);
806 if (gen)
807 *gen = btrfs_inode_generation(path->nodes[0], ii);
808 if (mode)
809 *mode = btrfs_inode_mode(path->nodes[0], ii);
810 if (uid)
811 *uid = btrfs_inode_uid(path->nodes[0], ii);
812 if (gid)
813 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200814 if (rdev)
815 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200816
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400817 return ret;
818}
819
820static int get_inode_info(struct btrfs_root *root,
821 u64 ino, u64 *size, u64 *gen,
822 u64 *mode, u64 *uid, u64 *gid,
823 u64 *rdev)
824{
825 struct btrfs_path *path;
826 int ret;
827
828 path = alloc_path_for_send();
829 if (!path)
830 return -ENOMEM;
831 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
832 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200833 btrfs_free_path(path);
834 return ret;
835}
836
837typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
838 struct fs_path *p,
839 void *ctx);
840
841/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000842 * Helper function to iterate the entries in ONE btrfs_inode_ref or
843 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200844 * The iterate callback may return a non zero value to stop iteration. This can
845 * be a negative value for error codes or 1 to simply stop it.
846 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000847 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200848 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000849static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200850 struct btrfs_key *found_key, int resolve,
851 iterate_inode_ref_t iterate, void *ctx)
852{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000853 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200854 struct btrfs_item *item;
855 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000856 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200857 struct btrfs_path *tmp_path;
858 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000859 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200860 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000861 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200862 u32 name_len;
863 char *start;
864 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000865 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200866 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000867 u64 dir;
868 unsigned long name_off;
869 unsigned long elem_size;
870 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200871
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000872 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200873 if (!p)
874 return -ENOMEM;
875
876 tmp_path = alloc_path_for_send();
877 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000878 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200879 return -ENOMEM;
880 }
881
Alexander Block31db9f72012-07-25 23:19:24 +0200882
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000883 if (found_key->type == BTRFS_INODE_REF_KEY) {
884 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
885 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100886 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000887 total = btrfs_item_size(eb, item);
888 elem_size = sizeof(*iref);
889 } else {
890 ptr = btrfs_item_ptr_offset(eb, slot);
891 total = btrfs_item_size_nr(eb, slot);
892 elem_size = sizeof(*extref);
893 }
894
Alexander Block31db9f72012-07-25 23:19:24 +0200895 while (cur < total) {
896 fs_path_reset(p);
897
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000898 if (found_key->type == BTRFS_INODE_REF_KEY) {
899 iref = (struct btrfs_inode_ref *)(ptr + cur);
900 name_len = btrfs_inode_ref_name_len(eb, iref);
901 name_off = (unsigned long)(iref + 1);
902 index = btrfs_inode_ref_index(eb, iref);
903 dir = found_key->offset;
904 } else {
905 extref = (struct btrfs_inode_extref *)(ptr + cur);
906 name_len = btrfs_inode_extref_name_len(eb, extref);
907 name_off = (unsigned long)&extref->name;
908 index = btrfs_inode_extref_index(eb, extref);
909 dir = btrfs_inode_extref_parent(eb, extref);
910 }
911
Alexander Block31db9f72012-07-25 23:19:24 +0200912 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000913 start = btrfs_ref_to_path(root, tmp_path, name_len,
914 name_off, eb, dir,
915 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200916 if (IS_ERR(start)) {
917 ret = PTR_ERR(start);
918 goto out;
919 }
920 if (start < p->buf) {
921 /* overflow , try again with larger buffer */
922 ret = fs_path_ensure_buf(p,
923 p->buf_len + p->buf - start);
924 if (ret < 0)
925 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000926 start = btrfs_ref_to_path(root, tmp_path,
927 name_len, name_off,
928 eb, dir,
929 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200930 if (IS_ERR(start)) {
931 ret = PTR_ERR(start);
932 goto out;
933 }
934 BUG_ON(start < p->buf);
935 }
936 p->start = start;
937 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000938 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
939 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200940 if (ret < 0)
941 goto out;
942 }
943
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000944 cur += elem_size + name_len;
945 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200946 if (ret)
947 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200948 num++;
949 }
950
951out:
952 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000953 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200954 return ret;
955}
956
957typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
958 const char *name, int name_len,
959 const char *data, int data_len,
960 u8 type, void *ctx);
961
962/*
963 * Helper function to iterate the entries in ONE btrfs_dir_item.
964 * The iterate callback may return a non zero value to stop iteration. This can
965 * be a negative value for error codes or 1 to simply stop it.
966 *
967 * path must point to the dir item when called.
968 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000969static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200970 struct btrfs_key *found_key,
971 iterate_dir_item_t iterate, void *ctx)
972{
973 int ret = 0;
974 struct extent_buffer *eb;
975 struct btrfs_item *item;
976 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200977 struct btrfs_key di_key;
978 char *buf = NULL;
Filipe Manana7e3ae332014-05-23 20:15:16 +0100979 int buf_len;
Alexander Block31db9f72012-07-25 23:19:24 +0200980 u32 name_len;
981 u32 data_len;
982 u32 cur;
983 u32 len;
984 u32 total;
985 int slot;
986 int num;
987 u8 type;
988
Filipe Manana4395e0c2014-08-20 10:45:45 +0100989 /*
990 * Start with a small buffer (1 page). If later we end up needing more
991 * space, which can happen for xattrs on a fs with a leaf size greater
992 * then the page size, attempt to increase the buffer. Typically xattr
993 * values are small.
994 */
995 buf_len = PATH_MAX;
Alexander Block31db9f72012-07-25 23:19:24 +0200996 buf = kmalloc(buf_len, GFP_NOFS);
997 if (!buf) {
998 ret = -ENOMEM;
999 goto out;
1000 }
1001
Alexander Block31db9f72012-07-25 23:19:24 +02001002 eb = path->nodes[0];
1003 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +01001004 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +02001005 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1006 cur = 0;
1007 len = 0;
1008 total = btrfs_item_size(eb, item);
1009
1010 num = 0;
1011 while (cur < total) {
1012 name_len = btrfs_dir_name_len(eb, di);
1013 data_len = btrfs_dir_data_len(eb, di);
1014 type = btrfs_dir_type(eb, di);
1015 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1016
Filipe Manana7e3ae332014-05-23 20:15:16 +01001017 if (type == BTRFS_FT_XATTR) {
1018 if (name_len > XATTR_NAME_MAX) {
1019 ret = -ENAMETOOLONG;
1020 goto out;
1021 }
Filipe Manana4395e0c2014-08-20 10:45:45 +01001022 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root)) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001023 ret = -E2BIG;
1024 goto out;
1025 }
1026 } else {
1027 /*
1028 * Path too long
1029 */
Filipe Manana4395e0c2014-08-20 10:45:45 +01001030 if (name_len + data_len > PATH_MAX) {
Filipe Manana7e3ae332014-05-23 20:15:16 +01001031 ret = -ENAMETOOLONG;
1032 goto out;
1033 }
Alexander Block31db9f72012-07-25 23:19:24 +02001034 }
1035
Filipe Manana4395e0c2014-08-20 10:45:45 +01001036 if (name_len + data_len > buf_len) {
1037 buf_len = name_len + data_len;
1038 if (is_vmalloc_addr(buf)) {
1039 vfree(buf);
1040 buf = NULL;
1041 } else {
1042 char *tmp = krealloc(buf, buf_len,
1043 GFP_NOFS | __GFP_NOWARN);
1044
1045 if (!tmp)
1046 kfree(buf);
1047 buf = tmp;
1048 }
1049 if (!buf) {
1050 buf = vmalloc(buf_len);
1051 if (!buf) {
1052 ret = -ENOMEM;
1053 goto out;
1054 }
1055 }
1056 }
1057
Alexander Block31db9f72012-07-25 23:19:24 +02001058 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1059 name_len + data_len);
1060
1061 len = sizeof(*di) + name_len + data_len;
1062 di = (struct btrfs_dir_item *)((char *)di + len);
1063 cur += len;
1064
1065 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1066 data_len, type, ctx);
1067 if (ret < 0)
1068 goto out;
1069 if (ret) {
1070 ret = 0;
1071 goto out;
1072 }
1073
1074 num++;
1075 }
1076
1077out:
Filipe Manana4395e0c2014-08-20 10:45:45 +01001078 kvfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001079 return ret;
1080}
1081
1082static int __copy_first_ref(int num, u64 dir, int index,
1083 struct fs_path *p, void *ctx)
1084{
1085 int ret;
1086 struct fs_path *pt = ctx;
1087
1088 ret = fs_path_copy(pt, p);
1089 if (ret < 0)
1090 return ret;
1091
1092 /* we want the first only */
1093 return 1;
1094}
1095
1096/*
1097 * Retrieve the first path of an inode. If an inode has more then one
1098 * ref/hardlink, this is ignored.
1099 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001100static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001101 u64 ino, struct fs_path *path)
1102{
1103 int ret;
1104 struct btrfs_key key, found_key;
1105 struct btrfs_path *p;
1106
1107 p = alloc_path_for_send();
1108 if (!p)
1109 return -ENOMEM;
1110
1111 fs_path_reset(path);
1112
1113 key.objectid = ino;
1114 key.type = BTRFS_INODE_REF_KEY;
1115 key.offset = 0;
1116
1117 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1118 if (ret < 0)
1119 goto out;
1120 if (ret) {
1121 ret = 1;
1122 goto out;
1123 }
1124 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1125 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001126 (found_key.type != BTRFS_INODE_REF_KEY &&
1127 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001128 ret = -ENOENT;
1129 goto out;
1130 }
1131
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001132 ret = iterate_inode_ref(root, p, &found_key, 1,
1133 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001134 if (ret < 0)
1135 goto out;
1136 ret = 0;
1137
1138out:
1139 btrfs_free_path(p);
1140 return ret;
1141}
1142
1143struct backref_ctx {
1144 struct send_ctx *sctx;
1145
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001146 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001147 /* number of total found references */
1148 u64 found;
1149
1150 /*
1151 * used for clones found in send_root. clones found behind cur_objectid
1152 * and cur_offset are not considered as allowed clones.
1153 */
1154 u64 cur_objectid;
1155 u64 cur_offset;
1156
1157 /* may be truncated in case it's the last extent in a file */
1158 u64 extent_len;
1159
1160 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001161 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001162};
1163
1164static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1165{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001166 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001167 struct clone_root *cr = (struct clone_root *)elt;
1168
1169 if (root < cr->root->objectid)
1170 return -1;
1171 if (root > cr->root->objectid)
1172 return 1;
1173 return 0;
1174}
1175
1176static int __clone_root_cmp_sort(const void *e1, const void *e2)
1177{
1178 struct clone_root *cr1 = (struct clone_root *)e1;
1179 struct clone_root *cr2 = (struct clone_root *)e2;
1180
1181 if (cr1->root->objectid < cr2->root->objectid)
1182 return -1;
1183 if (cr1->root->objectid > cr2->root->objectid)
1184 return 1;
1185 return 0;
1186}
1187
1188/*
1189 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001190 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001191 */
1192static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1193{
1194 struct backref_ctx *bctx = ctx_;
1195 struct clone_root *found;
1196 int ret;
1197 u64 i_size;
1198
1199 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001200 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001201 bctx->sctx->clone_roots_cnt,
1202 sizeof(struct clone_root),
1203 __clone_root_cmp_bsearch);
1204 if (!found)
1205 return 0;
1206
1207 if (found->root == bctx->sctx->send_root &&
1208 ino == bctx->cur_objectid &&
1209 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001210 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001211 }
1212
1213 /*
Alexander Block766702e2012-07-28 14:11:31 +02001214 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001215 * accept clones from these extents.
1216 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001217 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1218 NULL, NULL, NULL);
1219 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001220 if (ret < 0)
1221 return ret;
1222
1223 if (offset + bctx->extent_len > i_size)
1224 return 0;
1225
1226 /*
1227 * Make sure we don't consider clones from send_root that are
1228 * behind the current inode/offset.
1229 */
1230 if (found->root == bctx->sctx->send_root) {
1231 /*
1232 * TODO for the moment we don't accept clones from the inode
1233 * that is currently send. We may change this when
1234 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1235 * file.
1236 */
1237 if (ino >= bctx->cur_objectid)
1238 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001239#if 0
1240 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001241 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001242 if (offset + bctx->extent_len > bctx->cur_offset)
1243 return 0;
1244#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001245 }
1246
1247 bctx->found++;
1248 found->found_refs++;
1249 if (ino < found->ino) {
1250 found->ino = ino;
1251 found->offset = offset;
1252 } else if (found->ino == ino) {
1253 /*
1254 * same extent found more then once in the same file.
1255 */
1256 if (found->offset > offset + bctx->extent_len)
1257 found->offset = offset;
1258 }
1259
1260 return 0;
1261}
1262
1263/*
Alexander Block766702e2012-07-28 14:11:31 +02001264 * Given an inode, offset and extent item, it finds a good clone for a clone
1265 * instruction. Returns -ENOENT when none could be found. The function makes
1266 * sure that the returned clone is usable at the point where sending is at the
1267 * moment. This means, that no clones are accepted which lie behind the current
1268 * inode+offset.
1269 *
Alexander Block31db9f72012-07-25 23:19:24 +02001270 * path must point to the extent item when called.
1271 */
1272static int find_extent_clone(struct send_ctx *sctx,
1273 struct btrfs_path *path,
1274 u64 ino, u64 data_offset,
1275 u64 ino_size,
1276 struct clone_root **found)
1277{
1278 int ret;
1279 int extent_type;
1280 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001281 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001282 u64 num_bytes;
1283 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001284 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001285 struct btrfs_file_extent_item *fi;
1286 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001287 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001288 struct clone_root *cur_clone_root;
1289 struct btrfs_key found_key;
1290 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001291 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001292 u32 i;
1293
1294 tmp_path = alloc_path_for_send();
1295 if (!tmp_path)
1296 return -ENOMEM;
1297
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001298 /* We only use this path under the commit sem */
1299 tmp_path->need_commit_sem = 0;
1300
Alexander Block35075bb2012-07-28 12:44:34 +02001301 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1302 if (!backref_ctx) {
1303 ret = -ENOMEM;
1304 goto out;
1305 }
1306
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001307 backref_ctx->path = tmp_path;
1308
Alexander Block31db9f72012-07-25 23:19:24 +02001309 if (data_offset >= ino_size) {
1310 /*
1311 * There may be extents that lie behind the file's size.
1312 * I at least had this in combination with snapshotting while
1313 * writing large files.
1314 */
1315 ret = 0;
1316 goto out;
1317 }
1318
1319 fi = btrfs_item_ptr(eb, path->slots[0],
1320 struct btrfs_file_extent_item);
1321 extent_type = btrfs_file_extent_type(eb, fi);
1322 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1323 ret = -ENOENT;
1324 goto out;
1325 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001326 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001327
1328 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001329 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1330 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001331 ret = -ENOENT;
1332 goto out;
1333 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001334 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001335
Josef Bacik9e351cc2014-03-13 15:42:13 -04001336 down_read(&sctx->send_root->fs_info->commit_root_sem);
Liu Bo69917e42012-09-07 20:01:28 -06001337 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1338 &found_key, &flags);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001339 up_read(&sctx->send_root->fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001340 btrfs_release_path(tmp_path);
1341
1342 if (ret < 0)
1343 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001344 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001345 ret = -EIO;
1346 goto out;
1347 }
1348
1349 /*
1350 * Setup the clone roots.
1351 */
1352 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1353 cur_clone_root = sctx->clone_roots + i;
1354 cur_clone_root->ino = (u64)-1;
1355 cur_clone_root->offset = 0;
1356 cur_clone_root->found_refs = 0;
1357 }
1358
Alexander Block35075bb2012-07-28 12:44:34 +02001359 backref_ctx->sctx = sctx;
1360 backref_ctx->found = 0;
1361 backref_ctx->cur_objectid = ino;
1362 backref_ctx->cur_offset = data_offset;
1363 backref_ctx->found_itself = 0;
1364 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001365
1366 /*
1367 * The last extent of a file may be too large due to page alignment.
1368 * We need to adjust extent_len in this case so that the checks in
1369 * __iterate_backrefs work.
1370 */
1371 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001372 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001373
1374 /*
1375 * Now collect all backrefs.
1376 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001377 if (compressed == BTRFS_COMPRESS_NONE)
1378 extent_item_pos = logical - found_key.objectid;
1379 else
1380 extent_item_pos = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001381 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1382 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001383 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001384
Alexander Block31db9f72012-07-25 23:19:24 +02001385 if (ret < 0)
1386 goto out;
1387
Alexander Block35075bb2012-07-28 12:44:34 +02001388 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001389 /* found a bug in backref code? */
1390 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001391 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001392 "send_root. inode=%llu, offset=%llu, "
David Sterba351fd352014-05-15 16:48:20 +02001393 "disk_byte=%llu found extent=%llu",
Chris Mason74dd17f2012-08-07 16:25:13 -04001394 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001395 goto out;
1396 }
1397
1398verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1399 "ino=%llu, "
1400 "num_bytes=%llu, logical=%llu\n",
1401 data_offset, ino, num_bytes, logical);
1402
Alexander Block35075bb2012-07-28 12:44:34 +02001403 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001404 verbose_printk("btrfs: no clones found\n");
1405
1406 cur_clone_root = NULL;
1407 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1408 if (sctx->clone_roots[i].found_refs) {
1409 if (!cur_clone_root)
1410 cur_clone_root = sctx->clone_roots + i;
1411 else if (sctx->clone_roots[i].root == sctx->send_root)
1412 /* prefer clones from send_root over others */
1413 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001414 }
1415
1416 }
1417
1418 if (cur_clone_root) {
Filipe David Borba Manana93de4ba2014-02-15 15:53:16 +00001419 if (compressed != BTRFS_COMPRESS_NONE) {
1420 /*
1421 * Offsets given by iterate_extent_inodes() are relative
1422 * to the start of the extent, we need to add logical
1423 * offset from the file extent item.
1424 * (See why at backref.c:check_extent_in_eb())
1425 */
1426 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1427 fi);
1428 }
Alexander Block31db9f72012-07-25 23:19:24 +02001429 *found = cur_clone_root;
1430 ret = 0;
1431 } else {
1432 ret = -ENOENT;
1433 }
1434
1435out:
1436 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001437 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001438 return ret;
1439}
1440
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001441static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001442 u64 ino,
1443 struct fs_path *dest)
1444{
1445 int ret;
1446 struct btrfs_path *path;
1447 struct btrfs_key key;
1448 struct btrfs_file_extent_item *ei;
1449 u8 type;
1450 u8 compression;
1451 unsigned long off;
1452 int len;
1453
1454 path = alloc_path_for_send();
1455 if (!path)
1456 return -ENOMEM;
1457
1458 key.objectid = ino;
1459 key.type = BTRFS_EXTENT_DATA_KEY;
1460 key.offset = 0;
1461 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1462 if (ret < 0)
1463 goto out;
1464 BUG_ON(ret);
1465
1466 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1467 struct btrfs_file_extent_item);
1468 type = btrfs_file_extent_type(path->nodes[0], ei);
1469 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1470 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1471 BUG_ON(compression);
1472
1473 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001474 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001475
1476 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001477
1478out:
1479 btrfs_free_path(path);
1480 return ret;
1481}
1482
1483/*
1484 * Helper function to generate a file name that is unique in the root of
1485 * send_root and parent_root. This is used to generate names for orphan inodes.
1486 */
1487static int gen_unique_name(struct send_ctx *sctx,
1488 u64 ino, u64 gen,
1489 struct fs_path *dest)
1490{
1491 int ret = 0;
1492 struct btrfs_path *path;
1493 struct btrfs_dir_item *di;
1494 char tmp[64];
1495 int len;
1496 u64 idx = 0;
1497
1498 path = alloc_path_for_send();
1499 if (!path)
1500 return -ENOMEM;
1501
1502 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001503 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001504 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001505 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001506
1507 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1508 path, BTRFS_FIRST_FREE_OBJECTID,
1509 tmp, strlen(tmp), 0);
1510 btrfs_release_path(path);
1511 if (IS_ERR(di)) {
1512 ret = PTR_ERR(di);
1513 goto out;
1514 }
1515 if (di) {
1516 /* not unique, try again */
1517 idx++;
1518 continue;
1519 }
1520
1521 if (!sctx->parent_root) {
1522 /* unique */
1523 ret = 0;
1524 break;
1525 }
1526
1527 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1528 path, BTRFS_FIRST_FREE_OBJECTID,
1529 tmp, strlen(tmp), 0);
1530 btrfs_release_path(path);
1531 if (IS_ERR(di)) {
1532 ret = PTR_ERR(di);
1533 goto out;
1534 }
1535 if (di) {
1536 /* not unique, try again */
1537 idx++;
1538 continue;
1539 }
1540 /* unique */
1541 break;
1542 }
1543
1544 ret = fs_path_add(dest, tmp, strlen(tmp));
1545
1546out:
1547 btrfs_free_path(path);
1548 return ret;
1549}
1550
1551enum inode_state {
1552 inode_state_no_change,
1553 inode_state_will_create,
1554 inode_state_did_create,
1555 inode_state_will_delete,
1556 inode_state_did_delete,
1557};
1558
1559static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1560{
1561 int ret;
1562 int left_ret;
1563 int right_ret;
1564 u64 left_gen;
1565 u64 right_gen;
1566
1567 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001568 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001569 if (ret < 0 && ret != -ENOENT)
1570 goto out;
1571 left_ret = ret;
1572
1573 if (!sctx->parent_root) {
1574 right_ret = -ENOENT;
1575 } else {
1576 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001577 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001578 if (ret < 0 && ret != -ENOENT)
1579 goto out;
1580 right_ret = ret;
1581 }
1582
1583 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001584 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001585 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001586 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001587 if (ino < sctx->send_progress)
1588 ret = inode_state_did_create;
1589 else
1590 ret = inode_state_will_create;
1591 } else if (right_gen == gen) {
1592 if (ino < sctx->send_progress)
1593 ret = inode_state_did_delete;
1594 else
1595 ret = inode_state_will_delete;
1596 } else {
1597 ret = -ENOENT;
1598 }
1599 } else if (!left_ret) {
1600 if (left_gen == gen) {
1601 if (ino < sctx->send_progress)
1602 ret = inode_state_did_create;
1603 else
1604 ret = inode_state_will_create;
1605 } else {
1606 ret = -ENOENT;
1607 }
1608 } else if (!right_ret) {
1609 if (right_gen == gen) {
1610 if (ino < sctx->send_progress)
1611 ret = inode_state_did_delete;
1612 else
1613 ret = inode_state_will_delete;
1614 } else {
1615 ret = -ENOENT;
1616 }
1617 } else {
1618 ret = -ENOENT;
1619 }
1620
1621out:
1622 return ret;
1623}
1624
1625static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1626{
1627 int ret;
1628
1629 ret = get_cur_inode_state(sctx, ino, gen);
1630 if (ret < 0)
1631 goto out;
1632
1633 if (ret == inode_state_no_change ||
1634 ret == inode_state_did_create ||
1635 ret == inode_state_will_delete)
1636 ret = 1;
1637 else
1638 ret = 0;
1639
1640out:
1641 return ret;
1642}
1643
1644/*
1645 * Helper function to lookup a dir item in a dir.
1646 */
1647static int lookup_dir_item_inode(struct btrfs_root *root,
1648 u64 dir, const char *name, int name_len,
1649 u64 *found_inode,
1650 u8 *found_type)
1651{
1652 int ret = 0;
1653 struct btrfs_dir_item *di;
1654 struct btrfs_key key;
1655 struct btrfs_path *path;
1656
1657 path = alloc_path_for_send();
1658 if (!path)
1659 return -ENOMEM;
1660
1661 di = btrfs_lookup_dir_item(NULL, root, path,
1662 dir, name, name_len, 0);
1663 if (!di) {
1664 ret = -ENOENT;
1665 goto out;
1666 }
1667 if (IS_ERR(di)) {
1668 ret = PTR_ERR(di);
1669 goto out;
1670 }
1671 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
Filipe Manana1af56072014-05-25 04:49:24 +01001672 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1673 ret = -ENOENT;
1674 goto out;
1675 }
Alexander Block31db9f72012-07-25 23:19:24 +02001676 *found_inode = key.objectid;
1677 *found_type = btrfs_dir_type(path->nodes[0], di);
1678
1679out:
1680 btrfs_free_path(path);
1681 return ret;
1682}
1683
Alexander Block766702e2012-07-28 14:11:31 +02001684/*
1685 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1686 * generation of the parent dir and the name of the dir entry.
1687 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001688static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001689 u64 *dir, u64 *dir_gen, struct fs_path *name)
1690{
1691 int ret;
1692 struct btrfs_key key;
1693 struct btrfs_key found_key;
1694 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001695 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001696 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001697
1698 path = alloc_path_for_send();
1699 if (!path)
1700 return -ENOMEM;
1701
1702 key.objectid = ino;
1703 key.type = BTRFS_INODE_REF_KEY;
1704 key.offset = 0;
1705
1706 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1707 if (ret < 0)
1708 goto out;
1709 if (!ret)
1710 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1711 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001712 if (ret || found_key.objectid != ino ||
1713 (found_key.type != BTRFS_INODE_REF_KEY &&
1714 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001715 ret = -ENOENT;
1716 goto out;
1717 }
1718
Filipe Manana51a60252014-05-13 22:01:02 +01001719 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001720 struct btrfs_inode_ref *iref;
1721 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1722 struct btrfs_inode_ref);
1723 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1724 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1725 (unsigned long)(iref + 1),
1726 len);
1727 parent_dir = found_key.offset;
1728 } else {
1729 struct btrfs_inode_extref *extref;
1730 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1731 struct btrfs_inode_extref);
1732 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1733 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1734 (unsigned long)&extref->name, len);
1735 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1736 }
Alexander Block31db9f72012-07-25 23:19:24 +02001737 if (ret < 0)
1738 goto out;
1739 btrfs_release_path(path);
1740
Filipe Mananab46ab972014-03-21 12:46:54 +00001741 if (dir_gen) {
1742 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1743 NULL, NULL, NULL);
1744 if (ret < 0)
1745 goto out;
1746 }
Alexander Block31db9f72012-07-25 23:19:24 +02001747
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001748 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001749
1750out:
1751 btrfs_free_path(path);
1752 return ret;
1753}
1754
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001755static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001756 u64 ino, u64 dir,
1757 const char *name, int name_len)
1758{
1759 int ret;
1760 struct fs_path *tmp_name;
1761 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001762
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001763 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001764 if (!tmp_name)
1765 return -ENOMEM;
1766
Filipe Mananab46ab972014-03-21 12:46:54 +00001767 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001768 if (ret < 0)
1769 goto out;
1770
Alexander Blockb9291af2012-07-28 11:07:18 +02001771 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001772 ret = 0;
1773 goto out;
1774 }
1775
Alexander Blocke938c8a2012-07-28 16:33:49 +02001776 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001777
1778out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001779 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001780 return ret;
1781}
1782
Alexander Block766702e2012-07-28 14:11:31 +02001783/*
1784 * Used by process_recorded_refs to determine if a new ref would overwrite an
1785 * already existing ref. In case it detects an overwrite, it returns the
1786 * inode/gen in who_ino/who_gen.
1787 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1788 * to make sure later references to the overwritten inode are possible.
1789 * Orphanizing is however only required for the first ref of an inode.
1790 * process_recorded_refs does an additional is_first_ref check to see if
1791 * orphanizing is really required.
1792 */
Alexander Block31db9f72012-07-25 23:19:24 +02001793static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1794 const char *name, int name_len,
1795 u64 *who_ino, u64 *who_gen)
1796{
1797 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001798 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001799 u64 other_inode = 0;
1800 u8 other_type = 0;
1801
1802 if (!sctx->parent_root)
1803 goto out;
1804
1805 ret = is_inode_existent(sctx, dir, dir_gen);
1806 if (ret <= 0)
1807 goto out;
1808
Josef Bacikebdad912013-08-06 16:47:48 -04001809 /*
1810 * If we have a parent root we need to verify that the parent dir was
1811 * not delted and then re-created, if it was then we have no overwrite
1812 * and we can just unlink this entry.
1813 */
1814 if (sctx->parent_root) {
1815 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1816 NULL, NULL, NULL);
1817 if (ret < 0 && ret != -ENOENT)
1818 goto out;
1819 if (ret) {
1820 ret = 0;
1821 goto out;
1822 }
1823 if (gen != dir_gen)
1824 goto out;
1825 }
1826
Alexander Block31db9f72012-07-25 23:19:24 +02001827 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1828 &other_inode, &other_type);
1829 if (ret < 0 && ret != -ENOENT)
1830 goto out;
1831 if (ret) {
1832 ret = 0;
1833 goto out;
1834 }
1835
Alexander Block766702e2012-07-28 14:11:31 +02001836 /*
1837 * Check if the overwritten ref was already processed. If yes, the ref
1838 * was already unlinked/moved, so we can safely assume that we will not
1839 * overwrite anything at this point in time.
1840 */
Alexander Block31db9f72012-07-25 23:19:24 +02001841 if (other_inode > sctx->send_progress) {
1842 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001843 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001844 if (ret < 0)
1845 goto out;
1846
1847 ret = 1;
1848 *who_ino = other_inode;
1849 } else {
1850 ret = 0;
1851 }
1852
1853out:
1854 return ret;
1855}
1856
Alexander Block766702e2012-07-28 14:11:31 +02001857/*
1858 * Checks if the ref was overwritten by an already processed inode. This is
1859 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1860 * thus the orphan name needs be used.
1861 * process_recorded_refs also uses it to avoid unlinking of refs that were
1862 * overwritten.
1863 */
Alexander Block31db9f72012-07-25 23:19:24 +02001864static int did_overwrite_ref(struct send_ctx *sctx,
1865 u64 dir, u64 dir_gen,
1866 u64 ino, u64 ino_gen,
1867 const char *name, int name_len)
1868{
1869 int ret = 0;
1870 u64 gen;
1871 u64 ow_inode;
1872 u8 other_type;
1873
1874 if (!sctx->parent_root)
1875 goto out;
1876
1877 ret = is_inode_existent(sctx, dir, dir_gen);
1878 if (ret <= 0)
1879 goto out;
1880
1881 /* check if the ref was overwritten by another ref */
1882 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1883 &ow_inode, &other_type);
1884 if (ret < 0 && ret != -ENOENT)
1885 goto out;
1886 if (ret) {
1887 /* was never and will never be overwritten */
1888 ret = 0;
1889 goto out;
1890 }
1891
1892 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001893 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001894 if (ret < 0)
1895 goto out;
1896
1897 if (ow_inode == ino && gen == ino_gen) {
1898 ret = 0;
1899 goto out;
1900 }
1901
1902 /* we know that it is or will be overwritten. check this now */
1903 if (ow_inode < sctx->send_progress)
1904 ret = 1;
1905 else
1906 ret = 0;
1907
1908out:
1909 return ret;
1910}
1911
Alexander Block766702e2012-07-28 14:11:31 +02001912/*
1913 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1914 * that got overwritten. This is used by process_recorded_refs to determine
1915 * if it has to use the path as returned by get_cur_path or the orphan name.
1916 */
Alexander Block31db9f72012-07-25 23:19:24 +02001917static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1918{
1919 int ret = 0;
1920 struct fs_path *name = NULL;
1921 u64 dir;
1922 u64 dir_gen;
1923
1924 if (!sctx->parent_root)
1925 goto out;
1926
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001927 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001928 if (!name)
1929 return -ENOMEM;
1930
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001931 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001932 if (ret < 0)
1933 goto out;
1934
1935 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1936 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001937
1938out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001939 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001940 return ret;
1941}
1942
Alexander Block766702e2012-07-28 14:11:31 +02001943/*
1944 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1945 * so we need to do some special handling in case we have clashes. This function
1946 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001947 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001948 */
Alexander Block31db9f72012-07-25 23:19:24 +02001949static int name_cache_insert(struct send_ctx *sctx,
1950 struct name_cache_entry *nce)
1951{
1952 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001953 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001954
Alexander Block7e0926f2012-07-28 14:20:58 +02001955 nce_head = radix_tree_lookup(&sctx->name_cache,
1956 (unsigned long)nce->ino);
1957 if (!nce_head) {
1958 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001959 if (!nce_head) {
1960 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001961 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001962 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001963 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001964
Alexander Block7e0926f2012-07-28 14:20:58 +02001965 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001966 if (ret < 0) {
1967 kfree(nce_head);
1968 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001969 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001970 }
Alexander Block31db9f72012-07-25 23:19:24 +02001971 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001972 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001973 list_add_tail(&nce->list, &sctx->name_cache_list);
1974 sctx->name_cache_size++;
1975
1976 return ret;
1977}
1978
1979static void name_cache_delete(struct send_ctx *sctx,
1980 struct name_cache_entry *nce)
1981{
Alexander Block7e0926f2012-07-28 14:20:58 +02001982 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001983
Alexander Block7e0926f2012-07-28 14:20:58 +02001984 nce_head = radix_tree_lookup(&sctx->name_cache,
1985 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01001986 if (!nce_head) {
1987 btrfs_err(sctx->send_root->fs_info,
1988 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1989 nce->ino, sctx->name_cache_size);
1990 }
Alexander Block31db9f72012-07-25 23:19:24 +02001991
Alexander Block7e0926f2012-07-28 14:20:58 +02001992 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001993 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001994 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001995
David Sterba57fb8912014-02-03 19:24:40 +01001996 /*
1997 * We may not get to the final release of nce_head if the lookup fails
1998 */
1999 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02002000 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2001 kfree(nce_head);
2002 }
Alexander Block31db9f72012-07-25 23:19:24 +02002003}
2004
2005static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2006 u64 ino, u64 gen)
2007{
Alexander Block7e0926f2012-07-28 14:20:58 +02002008 struct list_head *nce_head;
2009 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002010
Alexander Block7e0926f2012-07-28 14:20:58 +02002011 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2012 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02002013 return NULL;
2014
Alexander Block7e0926f2012-07-28 14:20:58 +02002015 list_for_each_entry(cur, nce_head, radix_list) {
2016 if (cur->ino == ino && cur->gen == gen)
2017 return cur;
2018 }
Alexander Block31db9f72012-07-25 23:19:24 +02002019 return NULL;
2020}
2021
Alexander Block766702e2012-07-28 14:11:31 +02002022/*
2023 * Removes the entry from the list and adds it back to the end. This marks the
2024 * entry as recently used so that name_cache_clean_unused does not remove it.
2025 */
Alexander Block31db9f72012-07-25 23:19:24 +02002026static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2027{
2028 list_del(&nce->list);
2029 list_add_tail(&nce->list, &sctx->name_cache_list);
2030}
2031
Alexander Block766702e2012-07-28 14:11:31 +02002032/*
2033 * Remove some entries from the beginning of name_cache_list.
2034 */
Alexander Block31db9f72012-07-25 23:19:24 +02002035static void name_cache_clean_unused(struct send_ctx *sctx)
2036{
2037 struct name_cache_entry *nce;
2038
2039 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2040 return;
2041
2042 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2043 nce = list_entry(sctx->name_cache_list.next,
2044 struct name_cache_entry, list);
2045 name_cache_delete(sctx, nce);
2046 kfree(nce);
2047 }
2048}
2049
2050static void name_cache_free(struct send_ctx *sctx)
2051{
2052 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002053
Alexander Blocke938c8a2012-07-28 16:33:49 +02002054 while (!list_empty(&sctx->name_cache_list)) {
2055 nce = list_entry(sctx->name_cache_list.next,
2056 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002057 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002058 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002059 }
2060}
2061
Alexander Block766702e2012-07-28 14:11:31 +02002062/*
2063 * Used by get_cur_path for each ref up to the root.
2064 * Returns 0 if it succeeded.
2065 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2066 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2067 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2068 * Returns <0 in case of error.
2069 */
Alexander Block31db9f72012-07-25 23:19:24 +02002070static int __get_cur_name_and_parent(struct send_ctx *sctx,
2071 u64 ino, u64 gen,
2072 u64 *parent_ino,
2073 u64 *parent_gen,
2074 struct fs_path *dest)
2075{
2076 int ret;
2077 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002078 struct name_cache_entry *nce = NULL;
2079
Alexander Block766702e2012-07-28 14:11:31 +02002080 /*
2081 * First check if we already did a call to this function with the same
2082 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2083 * return the cached result.
2084 */
Alexander Block31db9f72012-07-25 23:19:24 +02002085 nce = name_cache_search(sctx, ino, gen);
2086 if (nce) {
2087 if (ino < sctx->send_progress && nce->need_later_update) {
2088 name_cache_delete(sctx, nce);
2089 kfree(nce);
2090 nce = NULL;
2091 } else {
2092 name_cache_used(sctx, nce);
2093 *parent_ino = nce->parent_ino;
2094 *parent_gen = nce->parent_gen;
2095 ret = fs_path_add(dest, nce->name, nce->name_len);
2096 if (ret < 0)
2097 goto out;
2098 ret = nce->ret;
2099 goto out;
2100 }
2101 }
2102
Alexander Block766702e2012-07-28 14:11:31 +02002103 /*
2104 * If the inode is not existent yet, add the orphan name and return 1.
2105 * This should only happen for the parent dir that we determine in
2106 * __record_new_ref
2107 */
Alexander Block31db9f72012-07-25 23:19:24 +02002108 ret = is_inode_existent(sctx, ino, gen);
2109 if (ret < 0)
2110 goto out;
2111
2112 if (!ret) {
2113 ret = gen_unique_name(sctx, ino, gen, dest);
2114 if (ret < 0)
2115 goto out;
2116 ret = 1;
2117 goto out_cache;
2118 }
2119
Alexander Block766702e2012-07-28 14:11:31 +02002120 /*
2121 * Depending on whether the inode was already processed or not, use
2122 * send_root or parent_root for ref lookup.
2123 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002124 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002125 ret = get_first_ref(sctx->send_root, ino,
2126 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002127 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002128 ret = get_first_ref(sctx->parent_root, ino,
2129 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002130 if (ret < 0)
2131 goto out;
2132
Alexander Block766702e2012-07-28 14:11:31 +02002133 /*
2134 * Check if the ref was overwritten by an inode's ref that was processed
2135 * earlier. If yes, treat as orphan and return 1.
2136 */
Alexander Block31db9f72012-07-25 23:19:24 +02002137 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2138 dest->start, dest->end - dest->start);
2139 if (ret < 0)
2140 goto out;
2141 if (ret) {
2142 fs_path_reset(dest);
2143 ret = gen_unique_name(sctx, ino, gen, dest);
2144 if (ret < 0)
2145 goto out;
2146 ret = 1;
2147 }
2148
2149out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002150 /*
2151 * Store the result of the lookup in the name cache.
2152 */
Alexander Block31db9f72012-07-25 23:19:24 +02002153 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2154 if (!nce) {
2155 ret = -ENOMEM;
2156 goto out;
2157 }
2158
2159 nce->ino = ino;
2160 nce->gen = gen;
2161 nce->parent_ino = *parent_ino;
2162 nce->parent_gen = *parent_gen;
2163 nce->name_len = fs_path_len(dest);
2164 nce->ret = ret;
2165 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002166
2167 if (ino < sctx->send_progress)
2168 nce->need_later_update = 0;
2169 else
2170 nce->need_later_update = 1;
2171
2172 nce_ret = name_cache_insert(sctx, nce);
2173 if (nce_ret < 0)
2174 ret = nce_ret;
2175 name_cache_clean_unused(sctx);
2176
2177out:
Alexander Block31db9f72012-07-25 23:19:24 +02002178 return ret;
2179}
2180
2181/*
2182 * Magic happens here. This function returns the first ref to an inode as it
2183 * would look like while receiving the stream at this point in time.
2184 * We walk the path up to the root. For every inode in between, we check if it
2185 * was already processed/sent. If yes, we continue with the parent as found
2186 * in send_root. If not, we continue with the parent as found in parent_root.
2187 * If we encounter an inode that was deleted at this point in time, we use the
2188 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2189 * that were not created yet and overwritten inodes/refs.
2190 *
2191 * When do we have have orphan inodes:
2192 * 1. When an inode is freshly created and thus no valid refs are available yet
2193 * 2. When a directory lost all it's refs (deleted) but still has dir items
2194 * inside which were not processed yet (pending for move/delete). If anyone
2195 * tried to get the path to the dir items, it would get a path inside that
2196 * orphan directory.
2197 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2198 * of an unprocessed inode. If in that case the first ref would be
2199 * overwritten, the overwritten inode gets "orphanized". Later when we
2200 * process this overwritten inode, it is restored at a new place by moving
2201 * the orphan inode.
2202 *
2203 * sctx->send_progress tells this function at which point in time receiving
2204 * would be.
2205 */
2206static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2207 struct fs_path *dest)
2208{
2209 int ret = 0;
2210 struct fs_path *name = NULL;
2211 u64 parent_inode = 0;
2212 u64 parent_gen = 0;
2213 int stop = 0;
2214
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002215 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002216 if (!name) {
2217 ret = -ENOMEM;
2218 goto out;
2219 }
2220
2221 dest->reversed = 1;
2222 fs_path_reset(dest);
2223
2224 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2225 fs_path_reset(name);
2226
Filipe Manana9dc44212014-02-19 14:31:44 +00002227 if (is_waiting_for_rm(sctx, ino)) {
2228 ret = gen_unique_name(sctx, ino, gen, name);
2229 if (ret < 0)
2230 goto out;
2231 ret = fs_path_add_path(dest, name);
2232 break;
2233 }
2234
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002235 if (is_waiting_for_move(sctx, ino)) {
2236 ret = get_first_ref(sctx->parent_root, ino,
2237 &parent_inode, &parent_gen, name);
2238 } else {
2239 ret = __get_cur_name_and_parent(sctx, ino, gen,
2240 &parent_inode,
2241 &parent_gen, name);
2242 if (ret)
2243 stop = 1;
2244 }
2245
Alexander Block31db9f72012-07-25 23:19:24 +02002246 if (ret < 0)
2247 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002248
Alexander Block31db9f72012-07-25 23:19:24 +02002249 ret = fs_path_add_path(dest, name);
2250 if (ret < 0)
2251 goto out;
2252
2253 ino = parent_inode;
2254 gen = parent_gen;
2255 }
2256
2257out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002258 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002259 if (!ret)
2260 fs_path_unreverse(dest);
2261 return ret;
2262}
2263
2264/*
Alexander Block31db9f72012-07-25 23:19:24 +02002265 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2266 */
2267static int send_subvol_begin(struct send_ctx *sctx)
2268{
2269 int ret;
2270 struct btrfs_root *send_root = sctx->send_root;
2271 struct btrfs_root *parent_root = sctx->parent_root;
2272 struct btrfs_path *path;
2273 struct btrfs_key key;
2274 struct btrfs_root_ref *ref;
2275 struct extent_buffer *leaf;
2276 char *name = NULL;
2277 int namelen;
2278
Wang Shilongffcfaf82014-01-15 00:26:43 +08002279 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002280 if (!path)
2281 return -ENOMEM;
2282
2283 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2284 if (!name) {
2285 btrfs_free_path(path);
2286 return -ENOMEM;
2287 }
2288
2289 key.objectid = send_root->objectid;
2290 key.type = BTRFS_ROOT_BACKREF_KEY;
2291 key.offset = 0;
2292
2293 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2294 &key, path, 1, 0);
2295 if (ret < 0)
2296 goto out;
2297 if (ret) {
2298 ret = -ENOENT;
2299 goto out;
2300 }
2301
2302 leaf = path->nodes[0];
2303 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2304 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2305 key.objectid != send_root->objectid) {
2306 ret = -ENOENT;
2307 goto out;
2308 }
2309 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2310 namelen = btrfs_root_ref_name_len(leaf, ref);
2311 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2312 btrfs_release_path(path);
2313
Alexander Block31db9f72012-07-25 23:19:24 +02002314 if (parent_root) {
2315 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2316 if (ret < 0)
2317 goto out;
2318 } else {
2319 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2320 if (ret < 0)
2321 goto out;
2322 }
2323
2324 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2325 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2326 sctx->send_root->root_item.uuid);
2327 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002328 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002329 if (parent_root) {
2330 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2331 sctx->parent_root->root_item.uuid);
2332 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002333 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002334 }
2335
2336 ret = send_cmd(sctx);
2337
2338tlv_put_failure:
2339out:
2340 btrfs_free_path(path);
2341 kfree(name);
2342 return ret;
2343}
2344
2345static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2346{
2347 int ret = 0;
2348 struct fs_path *p;
2349
2350verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2351
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002352 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002353 if (!p)
2354 return -ENOMEM;
2355
2356 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2357 if (ret < 0)
2358 goto out;
2359
2360 ret = get_cur_path(sctx, ino, gen, p);
2361 if (ret < 0)
2362 goto out;
2363 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2364 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2365
2366 ret = send_cmd(sctx);
2367
2368tlv_put_failure:
2369out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002370 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002371 return ret;
2372}
2373
2374static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2375{
2376 int ret = 0;
2377 struct fs_path *p;
2378
2379verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
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 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2386 if (ret < 0)
2387 goto out;
2388
2389 ret = get_cur_path(sctx, ino, gen, p);
2390 if (ret < 0)
2391 goto out;
2392 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2393 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2394
2395 ret = send_cmd(sctx);
2396
2397tlv_put_failure:
2398out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002399 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002400 return ret;
2401}
2402
2403static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2404{
2405 int ret = 0;
2406 struct fs_path *p;
2407
2408verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2409
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002410 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002411 if (!p)
2412 return -ENOMEM;
2413
2414 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2415 if (ret < 0)
2416 goto out;
2417
2418 ret = get_cur_path(sctx, ino, gen, p);
2419 if (ret < 0)
2420 goto out;
2421 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2422 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2423 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2424
2425 ret = send_cmd(sctx);
2426
2427tlv_put_failure:
2428out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002429 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002430 return ret;
2431}
2432
2433static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2434{
2435 int ret = 0;
2436 struct fs_path *p = NULL;
2437 struct btrfs_inode_item *ii;
2438 struct btrfs_path *path = NULL;
2439 struct extent_buffer *eb;
2440 struct btrfs_key key;
2441 int slot;
2442
2443verbose_printk("btrfs: send_utimes %llu\n", ino);
2444
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002445 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002446 if (!p)
2447 return -ENOMEM;
2448
2449 path = alloc_path_for_send();
2450 if (!path) {
2451 ret = -ENOMEM;
2452 goto out;
2453 }
2454
2455 key.objectid = ino;
2456 key.type = BTRFS_INODE_ITEM_KEY;
2457 key.offset = 0;
2458 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2459 if (ret < 0)
2460 goto out;
2461
2462 eb = path->nodes[0];
2463 slot = path->slots[0];
2464 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2465
2466 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2467 if (ret < 0)
2468 goto out;
2469
2470 ret = get_cur_path(sctx, ino, gen, p);
2471 if (ret < 0)
2472 goto out;
2473 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
David Sterbaa937b972014-12-12 17:39:12 +01002474 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2475 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2476 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
Alexander Block766702e2012-07-28 14:11:31 +02002477 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002478
2479 ret = send_cmd(sctx);
2480
2481tlv_put_failure:
2482out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002483 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002484 btrfs_free_path(path);
2485 return ret;
2486}
2487
2488/*
2489 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2490 * a valid path yet because we did not process the refs yet. So, the inode
2491 * is created as orphan.
2492 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002493static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002494{
2495 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002496 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002497 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002498 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002499 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002500 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002501
Alexander Block1f4692d2012-07-28 10:42:24 +02002502verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002503
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002504 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002505 if (!p)
2506 return -ENOMEM;
2507
Liu Bo644d1942014-02-27 17:29:01 +08002508 if (ino != sctx->cur_ino) {
2509 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2510 NULL, NULL, &rdev);
2511 if (ret < 0)
2512 goto out;
2513 } else {
2514 gen = sctx->cur_inode_gen;
2515 mode = sctx->cur_inode_mode;
2516 rdev = sctx->cur_inode_rdev;
2517 }
Alexander Block31db9f72012-07-25 23:19:24 +02002518
Alexander Blocke938c8a2012-07-28 16:33:49 +02002519 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002520 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002521 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002522 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002523 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002524 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002525 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002526 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002527 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002528 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002529 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002530 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002531 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002532 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2533 (int)(mode & S_IFMT));
2534 ret = -ENOTSUPP;
2535 goto out;
2536 }
2537
2538 ret = begin_cmd(sctx, cmd);
2539 if (ret < 0)
2540 goto out;
2541
Alexander Block1f4692d2012-07-28 10:42:24 +02002542 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002543 if (ret < 0)
2544 goto out;
2545
2546 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002547 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002548
2549 if (S_ISLNK(mode)) {
2550 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002551 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002552 if (ret < 0)
2553 goto out;
2554 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2555 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2556 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002557 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2558 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002559 }
2560
2561 ret = send_cmd(sctx);
2562 if (ret < 0)
2563 goto out;
2564
2565
2566tlv_put_failure:
2567out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002568 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002569 return ret;
2570}
2571
Alexander Block1f4692d2012-07-28 10:42:24 +02002572/*
2573 * We need some special handling for inodes that get processed before the parent
2574 * directory got created. See process_recorded_refs for details.
2575 * This function does the check if we already created the dir out of order.
2576 */
2577static int did_create_dir(struct send_ctx *sctx, u64 dir)
2578{
2579 int ret = 0;
2580 struct btrfs_path *path = NULL;
2581 struct btrfs_key key;
2582 struct btrfs_key found_key;
2583 struct btrfs_key di_key;
2584 struct extent_buffer *eb;
2585 struct btrfs_dir_item *di;
2586 int slot;
2587
2588 path = alloc_path_for_send();
2589 if (!path) {
2590 ret = -ENOMEM;
2591 goto out;
2592 }
2593
2594 key.objectid = dir;
2595 key.type = BTRFS_DIR_INDEX_KEY;
2596 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002597 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2598 if (ret < 0)
2599 goto out;
2600
Alexander Block1f4692d2012-07-28 10:42:24 +02002601 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002602 eb = path->nodes[0];
2603 slot = path->slots[0];
2604 if (slot >= btrfs_header_nritems(eb)) {
2605 ret = btrfs_next_leaf(sctx->send_root, path);
2606 if (ret < 0) {
2607 goto out;
2608 } else if (ret > 0) {
2609 ret = 0;
2610 break;
2611 }
2612 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002613 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002614
2615 btrfs_item_key_to_cpu(eb, &found_key, slot);
2616 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002617 found_key.type != key.type) {
2618 ret = 0;
2619 goto out;
2620 }
2621
2622 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2623 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2624
Josef Bacika0525412013-08-12 10:56:14 -04002625 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2626 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002627 ret = 1;
2628 goto out;
2629 }
2630
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002631 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002632 }
2633
2634out:
2635 btrfs_free_path(path);
2636 return ret;
2637}
2638
2639/*
2640 * Only creates the inode if it is:
2641 * 1. Not a directory
2642 * 2. Or a directory which was not created already due to out of order
2643 * directories. See did_create_dir and process_recorded_refs for details.
2644 */
2645static int send_create_inode_if_needed(struct send_ctx *sctx)
2646{
2647 int ret;
2648
2649 if (S_ISDIR(sctx->cur_inode_mode)) {
2650 ret = did_create_dir(sctx, sctx->cur_ino);
2651 if (ret < 0)
2652 goto out;
2653 if (ret) {
2654 ret = 0;
2655 goto out;
2656 }
2657 }
2658
2659 ret = send_create_inode(sctx, sctx->cur_ino);
2660 if (ret < 0)
2661 goto out;
2662
2663out:
2664 return ret;
2665}
2666
Alexander Block31db9f72012-07-25 23:19:24 +02002667struct recorded_ref {
2668 struct list_head list;
2669 char *dir_path;
2670 char *name;
2671 struct fs_path *full_path;
2672 u64 dir;
2673 u64 dir_gen;
2674 int dir_path_len;
2675 int name_len;
2676};
2677
2678/*
2679 * We need to process new refs before deleted refs, but compare_tree gives us
2680 * everything mixed. So we first record all refs and later process them.
2681 * This function is a helper to record one ref.
2682 */
Liu Boa4d96d62014-03-03 21:31:03 +08002683static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002684 u64 dir_gen, struct fs_path *path)
2685{
2686 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002687
2688 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2689 if (!ref)
2690 return -ENOMEM;
2691
2692 ref->dir = dir;
2693 ref->dir_gen = dir_gen;
2694 ref->full_path = path;
2695
Andy Shevchenkoed848852013-08-21 10:32:13 +03002696 ref->name = (char *)kbasename(ref->full_path->start);
2697 ref->name_len = ref->full_path->end - ref->name;
2698 ref->dir_path = ref->full_path->start;
2699 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002700 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002701 else
Alexander Block31db9f72012-07-25 23:19:24 +02002702 ref->dir_path_len = ref->full_path->end -
2703 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002704
2705 list_add_tail(&ref->list, head);
2706 return 0;
2707}
2708
Josef Bacikba5e8f22013-08-16 16:52:55 -04002709static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2710{
2711 struct recorded_ref *new;
2712
2713 new = kmalloc(sizeof(*ref), GFP_NOFS);
2714 if (!new)
2715 return -ENOMEM;
2716
2717 new->dir = ref->dir;
2718 new->dir_gen = ref->dir_gen;
2719 new->full_path = NULL;
2720 INIT_LIST_HEAD(&new->list);
2721 list_add_tail(&new->list, list);
2722 return 0;
2723}
2724
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002725static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002726{
2727 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002728
Alexander Blocke938c8a2012-07-28 16:33:49 +02002729 while (!list_empty(head)) {
2730 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002731 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002732 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002733 kfree(cur);
2734 }
Alexander Block31db9f72012-07-25 23:19:24 +02002735}
2736
2737static void free_recorded_refs(struct send_ctx *sctx)
2738{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002739 __free_recorded_refs(&sctx->new_refs);
2740 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002741}
2742
2743/*
Alexander Block766702e2012-07-28 14:11:31 +02002744 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002745 * ref of an unprocessed inode gets overwritten and for all non empty
2746 * directories.
2747 */
2748static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2749 struct fs_path *path)
2750{
2751 int ret;
2752 struct fs_path *orphan;
2753
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002754 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002755 if (!orphan)
2756 return -ENOMEM;
2757
2758 ret = gen_unique_name(sctx, ino, gen, orphan);
2759 if (ret < 0)
2760 goto out;
2761
2762 ret = send_rename(sctx, path, orphan);
2763
2764out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002765 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002766 return ret;
2767}
2768
Filipe Manana9dc44212014-02-19 14:31:44 +00002769static struct orphan_dir_info *
2770add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2771{
2772 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2773 struct rb_node *parent = NULL;
2774 struct orphan_dir_info *entry, *odi;
2775
2776 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2777 if (!odi)
2778 return ERR_PTR(-ENOMEM);
2779 odi->ino = dir_ino;
2780 odi->gen = 0;
2781
2782 while (*p) {
2783 parent = *p;
2784 entry = rb_entry(parent, struct orphan_dir_info, node);
2785 if (dir_ino < entry->ino) {
2786 p = &(*p)->rb_left;
2787 } else if (dir_ino > entry->ino) {
2788 p = &(*p)->rb_right;
2789 } else {
2790 kfree(odi);
2791 return entry;
2792 }
2793 }
2794
2795 rb_link_node(&odi->node, parent, p);
2796 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2797 return odi;
2798}
2799
2800static struct orphan_dir_info *
2801get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2802{
2803 struct rb_node *n = sctx->orphan_dirs.rb_node;
2804 struct orphan_dir_info *entry;
2805
2806 while (n) {
2807 entry = rb_entry(n, struct orphan_dir_info, node);
2808 if (dir_ino < entry->ino)
2809 n = n->rb_left;
2810 else if (dir_ino > entry->ino)
2811 n = n->rb_right;
2812 else
2813 return entry;
2814 }
2815 return NULL;
2816}
2817
2818static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2819{
2820 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2821
2822 return odi != NULL;
2823}
2824
2825static void free_orphan_dir_info(struct send_ctx *sctx,
2826 struct orphan_dir_info *odi)
2827{
2828 if (!odi)
2829 return;
2830 rb_erase(&odi->node, &sctx->orphan_dirs);
2831 kfree(odi);
2832}
2833
Alexander Block31db9f72012-07-25 23:19:24 +02002834/*
2835 * Returns 1 if a directory can be removed at this point in time.
2836 * We check this by iterating all dir items and checking if the inode behind
2837 * the dir item was already processed.
2838 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002839static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2840 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002841{
2842 int ret = 0;
2843 struct btrfs_root *root = sctx->parent_root;
2844 struct btrfs_path *path;
2845 struct btrfs_key key;
2846 struct btrfs_key found_key;
2847 struct btrfs_key loc;
2848 struct btrfs_dir_item *di;
2849
Alexander Block6d85ed052012-08-01 14:48:59 +02002850 /*
2851 * Don't try to rmdir the top/root subvolume dir.
2852 */
2853 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2854 return 0;
2855
Alexander Block31db9f72012-07-25 23:19:24 +02002856 path = alloc_path_for_send();
2857 if (!path)
2858 return -ENOMEM;
2859
2860 key.objectid = dir;
2861 key.type = BTRFS_DIR_INDEX_KEY;
2862 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002863 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2864 if (ret < 0)
2865 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002866
2867 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002868 struct waiting_dir_move *dm;
2869
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002870 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2871 ret = btrfs_next_leaf(root, path);
2872 if (ret < 0)
2873 goto out;
2874 else if (ret > 0)
2875 break;
2876 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002877 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002878 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2879 path->slots[0]);
2880 if (found_key.objectid != key.objectid ||
2881 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002882 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002883
2884 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2885 struct btrfs_dir_item);
2886 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2887
Filipe Manana9dc44212014-02-19 14:31:44 +00002888 dm = get_waiting_dir_move(sctx, loc.objectid);
2889 if (dm) {
2890 struct orphan_dir_info *odi;
2891
2892 odi = add_orphan_dir_info(sctx, dir);
2893 if (IS_ERR(odi)) {
2894 ret = PTR_ERR(odi);
2895 goto out;
2896 }
2897 odi->gen = dir_gen;
2898 dm->rmdir_ino = dir;
2899 ret = 0;
2900 goto out;
2901 }
2902
Alexander Block31db9f72012-07-25 23:19:24 +02002903 if (loc.objectid > send_progress) {
2904 ret = 0;
2905 goto out;
2906 }
2907
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002908 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002909 }
2910
2911 ret = 1;
2912
2913out:
2914 btrfs_free_path(path);
2915 return ret;
2916}
2917
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002918static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2919{
Filipe Manana9dc44212014-02-19 14:31:44 +00002920 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002921
Filipe Manana9dc44212014-02-19 14:31:44 +00002922 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002923}
2924
2925static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2926{
2927 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2928 struct rb_node *parent = NULL;
2929 struct waiting_dir_move *entry, *dm;
2930
2931 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2932 if (!dm)
2933 return -ENOMEM;
2934 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00002935 dm->rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002936
2937 while (*p) {
2938 parent = *p;
2939 entry = rb_entry(parent, struct waiting_dir_move, node);
2940 if (ino < entry->ino) {
2941 p = &(*p)->rb_left;
2942 } else if (ino > entry->ino) {
2943 p = &(*p)->rb_right;
2944 } else {
2945 kfree(dm);
2946 return -EEXIST;
2947 }
2948 }
2949
2950 rb_link_node(&dm->node, parent, p);
2951 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2952 return 0;
2953}
2954
Filipe Manana9dc44212014-02-19 14:31:44 +00002955static struct waiting_dir_move *
2956get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002957{
2958 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2959 struct waiting_dir_move *entry;
2960
2961 while (n) {
2962 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00002963 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002964 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00002965 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002966 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002967 else
2968 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002969 }
Filipe Manana9dc44212014-02-19 14:31:44 +00002970 return NULL;
2971}
2972
2973static void free_waiting_dir_move(struct send_ctx *sctx,
2974 struct waiting_dir_move *dm)
2975{
2976 if (!dm)
2977 return;
2978 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2979 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002980}
2981
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00002982static int add_pending_dir_move(struct send_ctx *sctx,
2983 u64 ino,
2984 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00002985 u64 parent_ino,
2986 struct list_head *new_refs,
2987 struct list_head *deleted_refs)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002988{
2989 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2990 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07002991 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002992 struct recorded_ref *cur;
2993 int exists = 0;
2994 int ret;
2995
2996 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2997 if (!pm)
2998 return -ENOMEM;
2999 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003000 pm->ino = ino;
3001 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003002 INIT_LIST_HEAD(&pm->list);
3003 INIT_LIST_HEAD(&pm->update_refs);
3004 RB_CLEAR_NODE(&pm->node);
3005
3006 while (*p) {
3007 parent = *p;
3008 entry = rb_entry(parent, struct pending_dir_move, node);
3009 if (parent_ino < entry->parent_ino) {
3010 p = &(*p)->rb_left;
3011 } else if (parent_ino > entry->parent_ino) {
3012 p = &(*p)->rb_right;
3013 } else {
3014 exists = 1;
3015 break;
3016 }
3017 }
3018
Filipe Mananaf9594922014-03-27 20:14:01 +00003019 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003020 ret = dup_ref(cur, &pm->update_refs);
3021 if (ret < 0)
3022 goto out;
3023 }
Filipe Mananaf9594922014-03-27 20:14:01 +00003024 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003025 ret = dup_ref(cur, &pm->update_refs);
3026 if (ret < 0)
3027 goto out;
3028 }
3029
3030 ret = add_waiting_dir_move(sctx, pm->ino);
3031 if (ret)
3032 goto out;
3033
3034 if (exists) {
3035 list_add_tail(&pm->list, &entry->list);
3036 } else {
3037 rb_link_node(&pm->node, parent, p);
3038 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3039 }
3040 ret = 0;
3041out:
3042 if (ret) {
3043 __free_recorded_refs(&pm->update_refs);
3044 kfree(pm);
3045 }
3046 return ret;
3047}
3048
3049static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3050 u64 parent_ino)
3051{
3052 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3053 struct pending_dir_move *entry;
3054
3055 while (n) {
3056 entry = rb_entry(n, struct pending_dir_move, node);
3057 if (parent_ino < entry->parent_ino)
3058 n = n->rb_left;
3059 else if (parent_ino > entry->parent_ino)
3060 n = n->rb_right;
3061 else
3062 return entry;
3063 }
3064 return NULL;
3065}
3066
Filipe Mananaf9594922014-03-27 20:14:01 +00003067static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3068 u64 ino, u64 gen, u64 *ancestor_ino)
3069{
3070 int ret = 0;
3071 u64 parent_inode = 0;
3072 u64 parent_gen = 0;
3073 u64 start_ino = ino;
3074
3075 *ancestor_ino = 0;
3076 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3077 fs_path_reset(name);
3078
3079 if (is_waiting_for_rm(sctx, ino))
3080 break;
3081 if (is_waiting_for_move(sctx, ino)) {
3082 if (*ancestor_ino == 0)
3083 *ancestor_ino = ino;
3084 ret = get_first_ref(sctx->parent_root, ino,
3085 &parent_inode, &parent_gen, name);
3086 } else {
3087 ret = __get_cur_name_and_parent(sctx, ino, gen,
3088 &parent_inode,
3089 &parent_gen, name);
3090 if (ret > 0) {
3091 ret = 0;
3092 break;
3093 }
3094 }
3095 if (ret < 0)
3096 break;
3097 if (parent_inode == start_ino) {
3098 ret = 1;
3099 if (*ancestor_ino == 0)
3100 *ancestor_ino = ino;
3101 break;
3102 }
3103 ino = parent_inode;
3104 gen = parent_gen;
3105 }
3106 return ret;
3107}
3108
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003109static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3110{
3111 struct fs_path *from_path = NULL;
3112 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003113 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003114 u64 orig_progress = sctx->send_progress;
3115 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003116 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003117 struct waiting_dir_move *dm = NULL;
3118 u64 rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003119 int ret;
Filipe Mananaf9594922014-03-27 20:14:01 +00003120 u64 ancestor = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003121
Filipe Manana2b863a12014-02-16 13:43:11 +00003122 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003123 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003124 if (!name || !from_path) {
3125 ret = -ENOMEM;
3126 goto out;
3127 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003128
Filipe Manana9dc44212014-02-19 14:31:44 +00003129 dm = get_waiting_dir_move(sctx, pm->ino);
3130 ASSERT(dm);
3131 rmdir_ino = dm->rmdir_ino;
3132 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003133
3134 ret = get_first_ref(sctx->parent_root, pm->ino,
3135 &parent_ino, &parent_gen, name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003136 if (ret < 0)
3137 goto out;
3138
Filipe Mananac992ec92014-03-22 17:15:24 +00003139 ret = get_cur_path(sctx, parent_ino, parent_gen,
3140 from_path);
3141 if (ret < 0)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003142 goto out;
Filipe Mananac992ec92014-03-22 17:15:24 +00003143 ret = fs_path_add_path(from_path, name);
3144 if (ret < 0)
3145 goto out;
3146
Filipe Mananaf9594922014-03-27 20:14:01 +00003147 sctx->send_progress = sctx->cur_ino + 1;
3148 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3149 if (ret) {
3150 LIST_HEAD(deleted_refs);
3151 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3152 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3153 &pm->update_refs, &deleted_refs);
3154 if (ret < 0)
3155 goto out;
3156 if (rmdir_ino) {
3157 dm = get_waiting_dir_move(sctx, pm->ino);
3158 ASSERT(dm);
3159 dm->rmdir_ino = rmdir_ino;
3160 }
3161 goto out;
3162 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003163 fs_path_reset(name);
3164 to_path = name;
3165 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003166 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3167 if (ret < 0)
3168 goto out;
3169
3170 ret = send_rename(sctx, from_path, to_path);
3171 if (ret < 0)
3172 goto out;
3173
Filipe Manana9dc44212014-02-19 14:31:44 +00003174 if (rmdir_ino) {
3175 struct orphan_dir_info *odi;
3176
3177 odi = get_orphan_dir_info(sctx, rmdir_ino);
3178 if (!odi) {
3179 /* already deleted */
3180 goto finish;
3181 }
3182 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3183 if (ret < 0)
3184 goto out;
3185 if (!ret)
3186 goto finish;
3187
3188 name = fs_path_alloc();
3189 if (!name) {
3190 ret = -ENOMEM;
3191 goto out;
3192 }
3193 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3194 if (ret < 0)
3195 goto out;
3196 ret = send_rmdir(sctx, name);
3197 if (ret < 0)
3198 goto out;
3199 free_orphan_dir_info(sctx, odi);
3200 }
3201
3202finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003203 ret = send_utimes(sctx, pm->ino, pm->gen);
3204 if (ret < 0)
3205 goto out;
3206
3207 /*
3208 * After rename/move, need to update the utimes of both new parent(s)
3209 * and old parent(s).
3210 */
3211 list_for_each_entry(cur, &pm->update_refs, list) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003212 if (cur->dir == rmdir_ino)
3213 continue;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003214 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3215 if (ret < 0)
3216 goto out;
3217 }
3218
3219out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003220 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003221 fs_path_free(from_path);
3222 fs_path_free(to_path);
3223 sctx->send_progress = orig_progress;
3224
3225 return ret;
3226}
3227
3228static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3229{
3230 if (!list_empty(&m->list))
3231 list_del(&m->list);
3232 if (!RB_EMPTY_NODE(&m->node))
3233 rb_erase(&m->node, &sctx->pending_dir_moves);
3234 __free_recorded_refs(&m->update_refs);
3235 kfree(m);
3236}
3237
3238static void tail_append_pending_moves(struct pending_dir_move *moves,
3239 struct list_head *stack)
3240{
3241 if (list_empty(&moves->list)) {
3242 list_add_tail(&moves->list, stack);
3243 } else {
3244 LIST_HEAD(list);
3245 list_splice_init(&moves->list, &list);
3246 list_add_tail(&moves->list, stack);
3247 list_splice_tail(&list, stack);
3248 }
3249}
3250
3251static int apply_children_dir_moves(struct send_ctx *sctx)
3252{
3253 struct pending_dir_move *pm;
3254 struct list_head stack;
3255 u64 parent_ino = sctx->cur_ino;
3256 int ret = 0;
3257
3258 pm = get_pending_dir_moves(sctx, parent_ino);
3259 if (!pm)
3260 return 0;
3261
3262 INIT_LIST_HEAD(&stack);
3263 tail_append_pending_moves(pm, &stack);
3264
3265 while (!list_empty(&stack)) {
3266 pm = list_first_entry(&stack, struct pending_dir_move, list);
3267 parent_ino = pm->ino;
3268 ret = apply_dir_move(sctx, pm);
3269 free_pending_move(sctx, pm);
3270 if (ret)
3271 goto out;
3272 pm = get_pending_dir_moves(sctx, parent_ino);
3273 if (pm)
3274 tail_append_pending_moves(pm, &stack);
3275 }
3276 return 0;
3277
3278out:
3279 while (!list_empty(&stack)) {
3280 pm = list_first_entry(&stack, struct pending_dir_move, list);
3281 free_pending_move(sctx, pm);
3282 }
3283 return ret;
3284}
3285
3286static int wait_for_parent_move(struct send_ctx *sctx,
3287 struct recorded_ref *parent_ref)
3288{
Filipe Mananaf9594922014-03-27 20:14:01 +00003289 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003290 u64 ino = parent_ref->dir;
3291 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003292 struct fs_path *path_before = NULL;
3293 struct fs_path *path_after = NULL;
3294 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003295
3296 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003297 path_before = fs_path_alloc();
3298 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003299 ret = -ENOMEM;
3300 goto out;
3301 }
3302
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003303 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003304 * Our current directory inode may not yet be renamed/moved because some
3305 * ancestor (immediate or not) has to be renamed/moved first. So find if
3306 * such ancestor exists and make sure our own rename/move happens after
3307 * that ancestor is processed.
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003308 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003309 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3310 if (is_waiting_for_move(sctx, ino)) {
3311 ret = 1;
3312 break;
3313 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003314
3315 fs_path_reset(path_before);
3316 fs_path_reset(path_after);
3317
3318 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananaf9594922014-03-27 20:14:01 +00003319 NULL, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003320 if (ret < 0)
3321 goto out;
3322 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3323 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003324 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003325 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003326 } else if (ret == -ENOENT) {
Filipe Mananabf8e8ca2014-10-02 19:17:32 +01003327 ret = 0;
Filipe Mananaf9594922014-03-27 20:14:01 +00003328 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003329 }
3330
3331 len1 = fs_path_len(path_before);
3332 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003333 if (ino > sctx->cur_ino &&
3334 (parent_ino_before != parent_ino_after || len1 != len2 ||
3335 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003336 ret = 1;
Filipe Mananaf9594922014-03-27 20:14:01 +00003337 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003338 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003339 ino = parent_ino_after;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003340 }
3341
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003342out:
3343 fs_path_free(path_before);
3344 fs_path_free(path_after);
3345
Filipe Mananaf9594922014-03-27 20:14:01 +00003346 if (ret == 1) {
3347 ret = add_pending_dir_move(sctx,
3348 sctx->cur_ino,
3349 sctx->cur_inode_gen,
3350 ino,
3351 &sctx->new_refs,
3352 &sctx->deleted_refs);
3353 if (!ret)
3354 ret = 1;
3355 }
3356
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003357 return ret;
3358}
3359
Alexander Block31db9f72012-07-25 23:19:24 +02003360/*
3361 * This does all the move/link/unlink/rmdir magic.
3362 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003363static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003364{
3365 int ret = 0;
3366 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003367 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003368 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003369 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003370 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003371 u64 ow_gen;
3372 int did_overwrite = 0;
3373 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003374 u64 last_dir_ino_rm = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003375
3376verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3377
Alexander Block6d85ed052012-08-01 14:48:59 +02003378 /*
3379 * This should never happen as the root dir always has the same ref
3380 * which is always '..'
3381 */
3382 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003383 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed052012-08-01 14:48:59 +02003384
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003385 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003386 if (!valid_path) {
3387 ret = -ENOMEM;
3388 goto out;
3389 }
3390
Alexander Block31db9f72012-07-25 23:19:24 +02003391 /*
3392 * First, check if the first ref of the current inode was overwritten
3393 * before. If yes, we know that the current inode was already orphanized
3394 * and thus use the orphan name. If not, we can use get_cur_path to
3395 * get the path of the first ref as it would like while receiving at
3396 * this point in time.
3397 * New inodes are always orphan at the beginning, so force to use the
3398 * orphan name in this case.
3399 * The first ref is stored in valid_path and will be updated if it
3400 * gets moved around.
3401 */
3402 if (!sctx->cur_inode_new) {
3403 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3404 sctx->cur_inode_gen);
3405 if (ret < 0)
3406 goto out;
3407 if (ret)
3408 did_overwrite = 1;
3409 }
3410 if (sctx->cur_inode_new || did_overwrite) {
3411 ret = gen_unique_name(sctx, sctx->cur_ino,
3412 sctx->cur_inode_gen, valid_path);
3413 if (ret < 0)
3414 goto out;
3415 is_orphan = 1;
3416 } else {
3417 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3418 valid_path);
3419 if (ret < 0)
3420 goto out;
3421 }
3422
3423 list_for_each_entry(cur, &sctx->new_refs, list) {
3424 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003425 * We may have refs where the parent directory does not exist
3426 * yet. This happens if the parent directories inum is higher
3427 * the the current inum. To handle this case, we create the
3428 * parent directory out of order. But we need to check if this
3429 * did already happen before due to other refs in the same dir.
3430 */
3431 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3432 if (ret < 0)
3433 goto out;
3434 if (ret == inode_state_will_create) {
3435 ret = 0;
3436 /*
3437 * First check if any of the current inodes refs did
3438 * already create the dir.
3439 */
3440 list_for_each_entry(cur2, &sctx->new_refs, list) {
3441 if (cur == cur2)
3442 break;
3443 if (cur2->dir == cur->dir) {
3444 ret = 1;
3445 break;
3446 }
3447 }
3448
3449 /*
3450 * If that did not happen, check if a previous inode
3451 * did already create the dir.
3452 */
3453 if (!ret)
3454 ret = did_create_dir(sctx, cur->dir);
3455 if (ret < 0)
3456 goto out;
3457 if (!ret) {
3458 ret = send_create_inode(sctx, cur->dir);
3459 if (ret < 0)
3460 goto out;
3461 }
3462 }
3463
3464 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003465 * Check if this new ref would overwrite the first ref of
3466 * another unprocessed inode. If yes, orphanize the
3467 * overwritten inode. If we find an overwritten ref that is
3468 * not the first ref, simply unlink it.
3469 */
3470 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3471 cur->name, cur->name_len,
3472 &ow_inode, &ow_gen);
3473 if (ret < 0)
3474 goto out;
3475 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003476 ret = is_first_ref(sctx->parent_root,
3477 ow_inode, cur->dir, cur->name,
3478 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003479 if (ret < 0)
3480 goto out;
3481 if (ret) {
3482 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3483 cur->full_path);
3484 if (ret < 0)
3485 goto out;
3486 } else {
3487 ret = send_unlink(sctx, cur->full_path);
3488 if (ret < 0)
3489 goto out;
3490 }
3491 }
3492
3493 /*
3494 * link/move the ref to the new place. If we have an orphan
3495 * inode, move it and update valid_path. If not, link or move
3496 * it depending on the inode mode.
3497 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003498 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003499 ret = send_rename(sctx, valid_path, cur->full_path);
3500 if (ret < 0)
3501 goto out;
3502 is_orphan = 0;
3503 ret = fs_path_copy(valid_path, cur->full_path);
3504 if (ret < 0)
3505 goto out;
3506 } else {
3507 if (S_ISDIR(sctx->cur_inode_mode)) {
3508 /*
3509 * Dirs can't be linked, so move it. For moved
3510 * dirs, we always have one new and one deleted
3511 * ref. The deleted ref is ignored later.
3512 */
Filipe David Borba Mananad86477b2014-01-30 13:27:12 +00003513 ret = wait_for_parent_move(sctx, cur);
3514 if (ret < 0)
3515 goto out;
3516 if (ret) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003517 *pending_move = 1;
3518 } else {
3519 ret = send_rename(sctx, valid_path,
3520 cur->full_path);
3521 if (!ret)
3522 ret = fs_path_copy(valid_path,
3523 cur->full_path);
3524 }
Alexander Block31db9f72012-07-25 23:19:24 +02003525 if (ret < 0)
3526 goto out;
3527 } else {
3528 ret = send_link(sctx, cur->full_path,
3529 valid_path);
3530 if (ret < 0)
3531 goto out;
3532 }
3533 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003534 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003535 if (ret < 0)
3536 goto out;
3537 }
3538
3539 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3540 /*
3541 * Check if we can already rmdir the directory. If not,
3542 * orphanize it. For every dir item inside that gets deleted
3543 * later, we do this check again and rmdir it then if possible.
3544 * See the use of check_dirs for more details.
3545 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003546 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3547 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003548 if (ret < 0)
3549 goto out;
3550 if (ret) {
3551 ret = send_rmdir(sctx, valid_path);
3552 if (ret < 0)
3553 goto out;
3554 } else if (!is_orphan) {
3555 ret = orphanize_inode(sctx, sctx->cur_ino,
3556 sctx->cur_inode_gen, valid_path);
3557 if (ret < 0)
3558 goto out;
3559 is_orphan = 1;
3560 }
3561
3562 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003563 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003564 if (ret < 0)
3565 goto out;
3566 }
Alexander Blockccf16262012-07-28 11:46:29 +02003567 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3568 !list_empty(&sctx->deleted_refs)) {
3569 /*
3570 * We have a moved dir. Add the old parent to check_dirs
3571 */
3572 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3573 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003574 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003575 if (ret < 0)
3576 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003577 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3578 /*
3579 * We have a non dir inode. Go through all deleted refs and
3580 * unlink them if they were not already overwritten by other
3581 * inodes.
3582 */
3583 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3584 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3585 sctx->cur_ino, sctx->cur_inode_gen,
3586 cur->name, cur->name_len);
3587 if (ret < 0)
3588 goto out;
3589 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003590 ret = send_unlink(sctx, cur->full_path);
3591 if (ret < 0)
3592 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003593 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003594 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003595 if (ret < 0)
3596 goto out;
3597 }
Alexander Block31db9f72012-07-25 23:19:24 +02003598 /*
3599 * If the inode is still orphan, unlink the orphan. This may
3600 * happen when a previous inode did overwrite the first ref
3601 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003602 * inode. Unlinking does not mean that the inode is deleted in
3603 * all cases. There may still be links to this inode in other
3604 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003605 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003606 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003607 ret = send_unlink(sctx, valid_path);
3608 if (ret < 0)
3609 goto out;
3610 }
3611 }
3612
3613 /*
3614 * We did collect all parent dirs where cur_inode was once located. We
3615 * now go through all these dirs and check if they are pending for
3616 * deletion and if it's finally possible to perform the rmdir now.
3617 * We also update the inode stats of the parent dirs here.
3618 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003619 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003620 /*
3621 * In case we had refs into dirs that were not processed yet,
3622 * we don't need to do the utime and rmdir logic for these dirs.
3623 * The dir will be processed later.
3624 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003625 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003626 continue;
3627
Josef Bacikba5e8f22013-08-16 16:52:55 -04003628 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003629 if (ret < 0)
3630 goto out;
3631
3632 if (ret == inode_state_did_create ||
3633 ret == inode_state_no_change) {
3634 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003635 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003636 if (ret < 0)
3637 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003638 } else if (ret == inode_state_did_delete &&
3639 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003640 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3641 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003642 if (ret < 0)
3643 goto out;
3644 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003645 ret = get_cur_path(sctx, cur->dir,
3646 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003647 if (ret < 0)
3648 goto out;
3649 ret = send_rmdir(sctx, valid_path);
3650 if (ret < 0)
3651 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003652 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02003653 }
3654 }
3655 }
3656
Alexander Block31db9f72012-07-25 23:19:24 +02003657 ret = 0;
3658
3659out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04003660 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003661 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003662 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003663 return ret;
3664}
3665
Liu Boa4d96d62014-03-03 21:31:03 +08003666static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3667 struct fs_path *name, void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02003668{
3669 int ret = 0;
3670 struct send_ctx *sctx = ctx;
3671 struct fs_path *p;
3672 u64 gen;
3673
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003674 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003675 if (!p)
3676 return -ENOMEM;
3677
Liu Boa4d96d62014-03-03 21:31:03 +08003678 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003679 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003680 if (ret < 0)
3681 goto out;
3682
Alexander Block31db9f72012-07-25 23:19:24 +02003683 ret = get_cur_path(sctx, dir, gen, p);
3684 if (ret < 0)
3685 goto out;
3686 ret = fs_path_add_path(p, name);
3687 if (ret < 0)
3688 goto out;
3689
Liu Boa4d96d62014-03-03 21:31:03 +08003690 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003691
3692out:
3693 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003694 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003695 return ret;
3696}
3697
Liu Boa4d96d62014-03-03 21:31:03 +08003698static int __record_new_ref(int num, u64 dir, int index,
3699 struct fs_path *name,
3700 void *ctx)
3701{
3702 struct send_ctx *sctx = ctx;
3703 return record_ref(sctx->send_root, num, dir, index, name,
3704 ctx, &sctx->new_refs);
3705}
3706
3707
Alexander Block31db9f72012-07-25 23:19:24 +02003708static int __record_deleted_ref(int num, u64 dir, int index,
3709 struct fs_path *name,
3710 void *ctx)
3711{
Alexander Block31db9f72012-07-25 23:19:24 +02003712 struct send_ctx *sctx = ctx;
Liu Boa4d96d62014-03-03 21:31:03 +08003713 return record_ref(sctx->parent_root, num, dir, index, name,
3714 ctx, &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02003715}
3716
3717static int record_new_ref(struct send_ctx *sctx)
3718{
3719 int ret;
3720
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003721 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3722 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003723 if (ret < 0)
3724 goto out;
3725 ret = 0;
3726
3727out:
3728 return ret;
3729}
3730
3731static int record_deleted_ref(struct send_ctx *sctx)
3732{
3733 int ret;
3734
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003735 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3736 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003737 if (ret < 0)
3738 goto out;
3739 ret = 0;
3740
3741out:
3742 return ret;
3743}
3744
3745struct find_ref_ctx {
3746 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003747 u64 dir_gen;
3748 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003749 struct fs_path *name;
3750 int found_idx;
3751};
3752
3753static int __find_iref(int num, u64 dir, int index,
3754 struct fs_path *name,
3755 void *ctx_)
3756{
3757 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003758 u64 dir_gen;
3759 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003760
3761 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3762 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003763 /*
3764 * To avoid doing extra lookups we'll only do this if everything
3765 * else matches.
3766 */
3767 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3768 NULL, NULL, NULL);
3769 if (ret)
3770 return ret;
3771 if (dir_gen != ctx->dir_gen)
3772 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003773 ctx->found_idx = num;
3774 return 1;
3775 }
3776 return 0;
3777}
3778
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003779static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003780 struct btrfs_path *path,
3781 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003782 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003783{
3784 int ret;
3785 struct find_ref_ctx ctx;
3786
3787 ctx.dir = dir;
3788 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003789 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003790 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003791 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003792
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003793 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003794 if (ret < 0)
3795 return ret;
3796
3797 if (ctx.found_idx == -1)
3798 return -ENOENT;
3799
3800 return ctx.found_idx;
3801}
3802
3803static int __record_changed_new_ref(int num, u64 dir, int index,
3804 struct fs_path *name,
3805 void *ctx)
3806{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003807 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003808 int ret;
3809 struct send_ctx *sctx = ctx;
3810
Josef Bacikba5e8f22013-08-16 16:52:55 -04003811 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3812 NULL, NULL, NULL);
3813 if (ret)
3814 return ret;
3815
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003816 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003817 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003818 if (ret == -ENOENT)
3819 ret = __record_new_ref(num, dir, index, name, sctx);
3820 else if (ret > 0)
3821 ret = 0;
3822
3823 return ret;
3824}
3825
3826static int __record_changed_deleted_ref(int num, u64 dir, int index,
3827 struct fs_path *name,
3828 void *ctx)
3829{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003830 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003831 int ret;
3832 struct send_ctx *sctx = ctx;
3833
Josef Bacikba5e8f22013-08-16 16:52:55 -04003834 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3835 NULL, NULL, NULL);
3836 if (ret)
3837 return ret;
3838
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003839 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003840 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003841 if (ret == -ENOENT)
3842 ret = __record_deleted_ref(num, dir, index, name, sctx);
3843 else if (ret > 0)
3844 ret = 0;
3845
3846 return ret;
3847}
3848
3849static int record_changed_ref(struct send_ctx *sctx)
3850{
3851 int ret = 0;
3852
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003853 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003854 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3855 if (ret < 0)
3856 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003857 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003858 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3859 if (ret < 0)
3860 goto out;
3861 ret = 0;
3862
3863out:
3864 return ret;
3865}
3866
3867/*
3868 * Record and process all refs at once. Needed when an inode changes the
3869 * generation number, which means that it was deleted and recreated.
3870 */
3871static int process_all_refs(struct send_ctx *sctx,
3872 enum btrfs_compare_tree_result cmd)
3873{
3874 int ret;
3875 struct btrfs_root *root;
3876 struct btrfs_path *path;
3877 struct btrfs_key key;
3878 struct btrfs_key found_key;
3879 struct extent_buffer *eb;
3880 int slot;
3881 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003882 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003883
3884 path = alloc_path_for_send();
3885 if (!path)
3886 return -ENOMEM;
3887
3888 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3889 root = sctx->send_root;
3890 cb = __record_new_ref;
3891 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3892 root = sctx->parent_root;
3893 cb = __record_deleted_ref;
3894 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01003895 btrfs_err(sctx->send_root->fs_info,
3896 "Wrong command %d in process_all_refs", cmd);
3897 ret = -EINVAL;
3898 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003899 }
3900
3901 key.objectid = sctx->cmp_key->objectid;
3902 key.type = BTRFS_INODE_REF_KEY;
3903 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003904 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3905 if (ret < 0)
3906 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003907
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003908 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02003909 eb = path->nodes[0];
3910 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003911 if (slot >= btrfs_header_nritems(eb)) {
3912 ret = btrfs_next_leaf(root, path);
3913 if (ret < 0)
3914 goto out;
3915 else if (ret > 0)
3916 break;
3917 continue;
3918 }
3919
Alexander Block31db9f72012-07-25 23:19:24 +02003920 btrfs_item_key_to_cpu(eb, &found_key, slot);
3921
3922 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003923 (found_key.type != BTRFS_INODE_REF_KEY &&
3924 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003925 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003926
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003927 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003928 if (ret < 0)
3929 goto out;
3930
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003931 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003932 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003933 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003934
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003935 ret = process_recorded_refs(sctx, &pending_move);
3936 /* Only applicable to an incremental send. */
3937 ASSERT(pending_move == 0);
Alexander Block31db9f72012-07-25 23:19:24 +02003938
3939out:
3940 btrfs_free_path(path);
3941 return ret;
3942}
3943
3944static int send_set_xattr(struct send_ctx *sctx,
3945 struct fs_path *path,
3946 const char *name, int name_len,
3947 const char *data, int data_len)
3948{
3949 int ret = 0;
3950
3951 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3952 if (ret < 0)
3953 goto out;
3954
3955 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3956 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3957 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3958
3959 ret = send_cmd(sctx);
3960
3961tlv_put_failure:
3962out:
3963 return ret;
3964}
3965
3966static int send_remove_xattr(struct send_ctx *sctx,
3967 struct fs_path *path,
3968 const char *name, int name_len)
3969{
3970 int ret = 0;
3971
3972 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3973 if (ret < 0)
3974 goto out;
3975
3976 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3977 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3978
3979 ret = send_cmd(sctx);
3980
3981tlv_put_failure:
3982out:
3983 return ret;
3984}
3985
3986static int __process_new_xattr(int num, struct btrfs_key *di_key,
3987 const char *name, int name_len,
3988 const char *data, int data_len,
3989 u8 type, void *ctx)
3990{
3991 int ret;
3992 struct send_ctx *sctx = ctx;
3993 struct fs_path *p;
3994 posix_acl_xattr_header dummy_acl;
3995
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003996 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003997 if (!p)
3998 return -ENOMEM;
3999
4000 /*
4001 * This hack is needed because empty acl's are stored as zero byte
4002 * data in xattrs. Problem with that is, that receiving these zero byte
4003 * acl's will fail later. To fix this, we send a dummy acl list that
4004 * only contains the version number and no entries.
4005 */
4006 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4007 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4008 if (data_len == 0) {
4009 dummy_acl.a_version =
4010 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4011 data = (char *)&dummy_acl;
4012 data_len = sizeof(dummy_acl);
4013 }
4014 }
4015
4016 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4017 if (ret < 0)
4018 goto out;
4019
4020 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4021
4022out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004023 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004024 return ret;
4025}
4026
4027static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4028 const char *name, int name_len,
4029 const char *data, int data_len,
4030 u8 type, void *ctx)
4031{
4032 int ret;
4033 struct send_ctx *sctx = ctx;
4034 struct fs_path *p;
4035
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004036 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004037 if (!p)
4038 return -ENOMEM;
4039
4040 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4041 if (ret < 0)
4042 goto out;
4043
4044 ret = send_remove_xattr(sctx, p, name, name_len);
4045
4046out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004047 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004048 return ret;
4049}
4050
4051static int process_new_xattr(struct send_ctx *sctx)
4052{
4053 int ret = 0;
4054
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004055 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4056 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004057
4058 return ret;
4059}
4060
4061static int process_deleted_xattr(struct send_ctx *sctx)
4062{
4063 int ret;
4064
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004065 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4066 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004067
4068 return ret;
4069}
4070
4071struct find_xattr_ctx {
4072 const char *name;
4073 int name_len;
4074 int found_idx;
4075 char *found_data;
4076 int found_data_len;
4077};
4078
4079static int __find_xattr(int num, struct btrfs_key *di_key,
4080 const char *name, int name_len,
4081 const char *data, int data_len,
4082 u8 type, void *vctx)
4083{
4084 struct find_xattr_ctx *ctx = vctx;
4085
4086 if (name_len == ctx->name_len &&
4087 strncmp(name, ctx->name, name_len) == 0) {
4088 ctx->found_idx = num;
4089 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00004090 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02004091 if (!ctx->found_data)
4092 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004093 return 1;
4094 }
4095 return 0;
4096}
4097
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004098static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004099 struct btrfs_path *path,
4100 struct btrfs_key *key,
4101 const char *name, int name_len,
4102 char **data, int *data_len)
4103{
4104 int ret;
4105 struct find_xattr_ctx ctx;
4106
4107 ctx.name = name;
4108 ctx.name_len = name_len;
4109 ctx.found_idx = -1;
4110 ctx.found_data = NULL;
4111 ctx.found_data_len = 0;
4112
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004113 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004114 if (ret < 0)
4115 return ret;
4116
4117 if (ctx.found_idx == -1)
4118 return -ENOENT;
4119 if (data) {
4120 *data = ctx.found_data;
4121 *data_len = ctx.found_data_len;
4122 } else {
4123 kfree(ctx.found_data);
4124 }
4125 return ctx.found_idx;
4126}
4127
4128
4129static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4130 const char *name, int name_len,
4131 const char *data, int data_len,
4132 u8 type, void *ctx)
4133{
4134 int ret;
4135 struct send_ctx *sctx = ctx;
4136 char *found_data = NULL;
4137 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004138
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004139 ret = find_xattr(sctx->parent_root, sctx->right_path,
4140 sctx->cmp_key, name, name_len, &found_data,
4141 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004142 if (ret == -ENOENT) {
4143 ret = __process_new_xattr(num, di_key, name, name_len, data,
4144 data_len, type, ctx);
4145 } else if (ret >= 0) {
4146 if (data_len != found_data_len ||
4147 memcmp(data, found_data, data_len)) {
4148 ret = __process_new_xattr(num, di_key, name, name_len,
4149 data, data_len, type, ctx);
4150 } else {
4151 ret = 0;
4152 }
4153 }
4154
4155 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004156 return ret;
4157}
4158
4159static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4160 const char *name, int name_len,
4161 const char *data, int data_len,
4162 u8 type, void *ctx)
4163{
4164 int ret;
4165 struct send_ctx *sctx = ctx;
4166
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004167 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4168 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004169 if (ret == -ENOENT)
4170 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4171 data_len, type, ctx);
4172 else if (ret >= 0)
4173 ret = 0;
4174
4175 return ret;
4176}
4177
4178static int process_changed_xattr(struct send_ctx *sctx)
4179{
4180 int ret = 0;
4181
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004182 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004183 sctx->cmp_key, __process_changed_new_xattr, sctx);
4184 if (ret < 0)
4185 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004186 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004187 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4188
4189out:
4190 return ret;
4191}
4192
4193static int process_all_new_xattrs(struct send_ctx *sctx)
4194{
4195 int ret;
4196 struct btrfs_root *root;
4197 struct btrfs_path *path;
4198 struct btrfs_key key;
4199 struct btrfs_key found_key;
4200 struct extent_buffer *eb;
4201 int slot;
4202
4203 path = alloc_path_for_send();
4204 if (!path)
4205 return -ENOMEM;
4206
4207 root = sctx->send_root;
4208
4209 key.objectid = sctx->cmp_key->objectid;
4210 key.type = BTRFS_XATTR_ITEM_KEY;
4211 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4213 if (ret < 0)
4214 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004215
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004216 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004217 eb = path->nodes[0];
4218 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004219 if (slot >= btrfs_header_nritems(eb)) {
4220 ret = btrfs_next_leaf(root, path);
4221 if (ret < 0) {
4222 goto out;
4223 } else if (ret > 0) {
4224 ret = 0;
4225 break;
4226 }
4227 continue;
4228 }
Alexander Block31db9f72012-07-25 23:19:24 +02004229
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004230 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004231 if (found_key.objectid != key.objectid ||
4232 found_key.type != key.type) {
4233 ret = 0;
4234 goto out;
4235 }
4236
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004237 ret = iterate_dir_item(root, path, &found_key,
4238 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004239 if (ret < 0)
4240 goto out;
4241
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004242 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004243 }
4244
4245out:
4246 btrfs_free_path(path);
4247 return ret;
4248}
4249
Josef Baciked259092013-10-25 11:36:01 -04004250static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4251{
4252 struct btrfs_root *root = sctx->send_root;
4253 struct btrfs_fs_info *fs_info = root->fs_info;
4254 struct inode *inode;
4255 struct page *page;
4256 char *addr;
4257 struct btrfs_key key;
4258 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4259 pgoff_t last_index;
4260 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4261 ssize_t ret = 0;
4262
4263 key.objectid = sctx->cur_ino;
4264 key.type = BTRFS_INODE_ITEM_KEY;
4265 key.offset = 0;
4266
4267 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4268 if (IS_ERR(inode))
4269 return PTR_ERR(inode);
4270
4271 if (offset + len > i_size_read(inode)) {
4272 if (offset > i_size_read(inode))
4273 len = 0;
4274 else
4275 len = offset - i_size_read(inode);
4276 }
4277 if (len == 0)
4278 goto out;
4279
4280 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
Liu Bo2131bcd2014-03-05 10:07:35 +08004281
4282 /* initial readahead */
4283 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4284 file_ra_state_init(&sctx->ra, inode->i_mapping);
4285 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4286 last_index - index + 1);
4287
Josef Baciked259092013-10-25 11:36:01 -04004288 while (index <= last_index) {
4289 unsigned cur_len = min_t(unsigned, len,
4290 PAGE_CACHE_SIZE - pg_offset);
4291 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4292 if (!page) {
4293 ret = -ENOMEM;
4294 break;
4295 }
4296
4297 if (!PageUptodate(page)) {
4298 btrfs_readpage(NULL, page);
4299 lock_page(page);
4300 if (!PageUptodate(page)) {
4301 unlock_page(page);
4302 page_cache_release(page);
4303 ret = -EIO;
4304 break;
4305 }
4306 }
4307
4308 addr = kmap(page);
4309 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4310 kunmap(page);
4311 unlock_page(page);
4312 page_cache_release(page);
4313 index++;
4314 pg_offset = 0;
4315 len -= cur_len;
4316 ret += cur_len;
4317 }
4318out:
4319 iput(inode);
4320 return ret;
4321}
4322
Alexander Block31db9f72012-07-25 23:19:24 +02004323/*
4324 * Read some bytes from the current inode/file and send a write command to
4325 * user space.
4326 */
4327static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4328{
4329 int ret = 0;
4330 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004331 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004332
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004333 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004334 if (!p)
4335 return -ENOMEM;
4336
Alexander Block31db9f72012-07-25 23:19:24 +02004337verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4338
Josef Baciked259092013-10-25 11:36:01 -04004339 num_read = fill_read_buf(sctx, offset, len);
4340 if (num_read <= 0) {
4341 if (num_read < 0)
4342 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004343 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004344 }
Alexander Block31db9f72012-07-25 23:19:24 +02004345
4346 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4347 if (ret < 0)
4348 goto out;
4349
4350 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4351 if (ret < 0)
4352 goto out;
4353
4354 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4355 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004356 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004357
4358 ret = send_cmd(sctx);
4359
4360tlv_put_failure:
4361out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004362 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004363 if (ret < 0)
4364 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004365 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004366}
4367
4368/*
4369 * Send a clone command to user space.
4370 */
4371static int send_clone(struct send_ctx *sctx,
4372 u64 offset, u32 len,
4373 struct clone_root *clone_root)
4374{
4375 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004376 struct fs_path *p;
4377 u64 gen;
4378
4379verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4380 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4381 clone_root->root->objectid, clone_root->ino,
4382 clone_root->offset);
4383
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004384 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004385 if (!p)
4386 return -ENOMEM;
4387
4388 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4389 if (ret < 0)
4390 goto out;
4391
4392 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4393 if (ret < 0)
4394 goto out;
4395
4396 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4397 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4398 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4399
Alexander Blocke938c8a2012-07-28 16:33:49 +02004400 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004401 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004402 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004403 if (ret < 0)
4404 goto out;
4405 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4406 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004407 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004408 }
4409 if (ret < 0)
4410 goto out;
4411
4412 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02004413 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004414 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004415 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004416 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4417 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4418 clone_root->offset);
4419
4420 ret = send_cmd(sctx);
4421
4422tlv_put_failure:
4423out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004424 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004425 return ret;
4426}
4427
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004428/*
4429 * Send an update extent command to user space.
4430 */
4431static int send_update_extent(struct send_ctx *sctx,
4432 u64 offset, u32 len)
4433{
4434 int ret = 0;
4435 struct fs_path *p;
4436
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004437 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004438 if (!p)
4439 return -ENOMEM;
4440
4441 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4442 if (ret < 0)
4443 goto out;
4444
4445 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4446 if (ret < 0)
4447 goto out;
4448
4449 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4450 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4451 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4452
4453 ret = send_cmd(sctx);
4454
4455tlv_put_failure:
4456out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004457 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004458 return ret;
4459}
4460
Josef Bacik16e75492013-10-22 12:18:51 -04004461static int send_hole(struct send_ctx *sctx, u64 end)
4462{
4463 struct fs_path *p = NULL;
4464 u64 offset = sctx->cur_inode_last_extent;
4465 u64 len;
4466 int ret = 0;
4467
4468 p = fs_path_alloc();
4469 if (!p)
4470 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01004471 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4472 if (ret < 0)
4473 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04004474 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4475 while (offset < end) {
4476 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4477
4478 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4479 if (ret < 0)
4480 break;
Josef Bacik16e75492013-10-22 12:18:51 -04004481 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4482 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4483 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4484 ret = send_cmd(sctx);
4485 if (ret < 0)
4486 break;
4487 offset += len;
4488 }
4489tlv_put_failure:
4490 fs_path_free(p);
4491 return ret;
4492}
4493
Alexander Block31db9f72012-07-25 23:19:24 +02004494static int send_write_or_clone(struct send_ctx *sctx,
4495 struct btrfs_path *path,
4496 struct btrfs_key *key,
4497 struct clone_root *clone_root)
4498{
4499 int ret = 0;
4500 struct btrfs_file_extent_item *ei;
4501 u64 offset = key->offset;
4502 u64 pos = 0;
4503 u64 len;
4504 u32 l;
4505 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004506 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02004507
4508 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4509 struct btrfs_file_extent_item);
4510 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004511 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004512 len = btrfs_file_extent_inline_len(path->nodes[0],
4513 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004514 /*
4515 * it is possible the inline item won't cover the whole page,
4516 * but there may be items after this page. Make
4517 * sure to send the whole thing
4518 */
4519 len = PAGE_CACHE_ALIGN(len);
4520 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004521 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004522 }
Alexander Block31db9f72012-07-25 23:19:24 +02004523
4524 if (offset + len > sctx->cur_inode_size)
4525 len = sctx->cur_inode_size - offset;
4526 if (len == 0) {
4527 ret = 0;
4528 goto out;
4529 }
4530
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004531 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004532 ret = send_clone(sctx, offset, len, clone_root);
4533 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4534 ret = send_update_extent(sctx, offset, len);
4535 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004536 while (pos < len) {
4537 l = len - pos;
4538 if (l > BTRFS_SEND_READ_SIZE)
4539 l = BTRFS_SEND_READ_SIZE;
4540 ret = send_write(sctx, pos + offset, l);
4541 if (ret < 0)
4542 goto out;
4543 if (!ret)
4544 break;
4545 pos += ret;
4546 }
4547 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004548 }
Alexander Block31db9f72012-07-25 23:19:24 +02004549out:
4550 return ret;
4551}
4552
4553static int is_extent_unchanged(struct send_ctx *sctx,
4554 struct btrfs_path *left_path,
4555 struct btrfs_key *ekey)
4556{
4557 int ret = 0;
4558 struct btrfs_key key;
4559 struct btrfs_path *path = NULL;
4560 struct extent_buffer *eb;
4561 int slot;
4562 struct btrfs_key found_key;
4563 struct btrfs_file_extent_item *ei;
4564 u64 left_disknr;
4565 u64 right_disknr;
4566 u64 left_offset;
4567 u64 right_offset;
4568 u64 left_offset_fixed;
4569 u64 left_len;
4570 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04004571 u64 left_gen;
4572 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004573 u8 left_type;
4574 u8 right_type;
4575
4576 path = alloc_path_for_send();
4577 if (!path)
4578 return -ENOMEM;
4579
4580 eb = left_path->nodes[0];
4581 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02004582 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4583 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004584
4585 if (left_type != BTRFS_FILE_EXTENT_REG) {
4586 ret = 0;
4587 goto out;
4588 }
Chris Mason74dd17f2012-08-07 16:25:13 -04004589 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4590 left_len = btrfs_file_extent_num_bytes(eb, ei);
4591 left_offset = btrfs_file_extent_offset(eb, ei);
4592 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004593
4594 /*
4595 * Following comments will refer to these graphics. L is the left
4596 * extents which we are checking at the moment. 1-8 are the right
4597 * extents that we iterate.
4598 *
4599 * |-----L-----|
4600 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4601 *
4602 * |-----L-----|
4603 * |--1--|-2b-|...(same as above)
4604 *
4605 * Alternative situation. Happens on files where extents got split.
4606 * |-----L-----|
4607 * |-----------7-----------|-6-|
4608 *
4609 * Alternative situation. Happens on files which got larger.
4610 * |-----L-----|
4611 * |-8-|
4612 * Nothing follows after 8.
4613 */
4614
4615 key.objectid = ekey->objectid;
4616 key.type = BTRFS_EXTENT_DATA_KEY;
4617 key.offset = ekey->offset;
4618 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4619 if (ret < 0)
4620 goto out;
4621 if (ret) {
4622 ret = 0;
4623 goto out;
4624 }
4625
4626 /*
4627 * Handle special case where the right side has no extents at all.
4628 */
4629 eb = path->nodes[0];
4630 slot = path->slots[0];
4631 btrfs_item_key_to_cpu(eb, &found_key, slot);
4632 if (found_key.objectid != key.objectid ||
4633 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004634 /* If we're a hole then just pretend nothing changed */
4635 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004636 goto out;
4637 }
4638
4639 /*
4640 * We're now on 2a, 2b or 7.
4641 */
4642 key = found_key;
4643 while (key.offset < ekey->offset + left_len) {
4644 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4645 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004646 if (right_type != BTRFS_FILE_EXTENT_REG) {
4647 ret = 0;
4648 goto out;
4649 }
4650
Josef Bacik007d31f2013-10-31 16:49:02 -04004651 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4652 right_len = btrfs_file_extent_num_bytes(eb, ei);
4653 right_offset = btrfs_file_extent_offset(eb, ei);
4654 right_gen = btrfs_file_extent_generation(eb, ei);
4655
Alexander Block31db9f72012-07-25 23:19:24 +02004656 /*
4657 * Are we at extent 8? If yes, we know the extent is changed.
4658 * This may only happen on the first iteration.
4659 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02004660 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004661 /* If we're a hole just pretend nothing changed */
4662 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004663 goto out;
4664 }
4665
4666 left_offset_fixed = left_offset;
4667 if (key.offset < ekey->offset) {
4668 /* Fix the right offset for 2a and 7. */
4669 right_offset += ekey->offset - key.offset;
4670 } else {
4671 /* Fix the left offset for all behind 2a and 2b */
4672 left_offset_fixed += key.offset - ekey->offset;
4673 }
4674
4675 /*
4676 * Check if we have the same extent.
4677 */
Alexander Block39540962012-08-01 12:46:05 +02004678 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04004679 left_offset_fixed != right_offset ||
4680 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02004681 ret = 0;
4682 goto out;
4683 }
4684
4685 /*
4686 * Go to the next extent.
4687 */
4688 ret = btrfs_next_item(sctx->parent_root, path);
4689 if (ret < 0)
4690 goto out;
4691 if (!ret) {
4692 eb = path->nodes[0];
4693 slot = path->slots[0];
4694 btrfs_item_key_to_cpu(eb, &found_key, slot);
4695 }
4696 if (ret || found_key.objectid != key.objectid ||
4697 found_key.type != key.type) {
4698 key.offset += right_len;
4699 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00004700 }
4701 if (found_key.offset != key.offset + right_len) {
4702 ret = 0;
4703 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004704 }
4705 key = found_key;
4706 }
4707
4708 /*
4709 * We're now behind the left extent (treat as unchanged) or at the end
4710 * of the right side (treat as changed).
4711 */
4712 if (key.offset >= ekey->offset + left_len)
4713 ret = 1;
4714 else
4715 ret = 0;
4716
4717
4718out:
4719 btrfs_free_path(path);
4720 return ret;
4721}
4722
Josef Bacik16e75492013-10-22 12:18:51 -04004723static int get_last_extent(struct send_ctx *sctx, u64 offset)
4724{
4725 struct btrfs_path *path;
4726 struct btrfs_root *root = sctx->send_root;
4727 struct btrfs_file_extent_item *fi;
4728 struct btrfs_key key;
4729 u64 extent_end;
4730 u8 type;
4731 int ret;
4732
4733 path = alloc_path_for_send();
4734 if (!path)
4735 return -ENOMEM;
4736
4737 sctx->cur_inode_last_extent = 0;
4738
4739 key.objectid = sctx->cur_ino;
4740 key.type = BTRFS_EXTENT_DATA_KEY;
4741 key.offset = offset;
4742 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4743 if (ret < 0)
4744 goto out;
4745 ret = 0;
4746 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4747 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4748 goto out;
4749
4750 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4751 struct btrfs_file_extent_item);
4752 type = btrfs_file_extent_type(path->nodes[0], fi);
4753 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004754 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4755 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004756 extent_end = ALIGN(key.offset + size,
4757 sctx->send_root->sectorsize);
4758 } else {
4759 extent_end = key.offset +
4760 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4761 }
4762 sctx->cur_inode_last_extent = extent_end;
4763out:
4764 btrfs_free_path(path);
4765 return ret;
4766}
4767
4768static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4769 struct btrfs_key *key)
4770{
4771 struct btrfs_file_extent_item *fi;
4772 u64 extent_end;
4773 u8 type;
4774 int ret = 0;
4775
4776 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4777 return 0;
4778
4779 if (sctx->cur_inode_last_extent == (u64)-1) {
4780 ret = get_last_extent(sctx, key->offset - 1);
4781 if (ret)
4782 return ret;
4783 }
4784
4785 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4786 struct btrfs_file_extent_item);
4787 type = btrfs_file_extent_type(path->nodes[0], fi);
4788 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004789 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4790 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004791 extent_end = ALIGN(key->offset + size,
4792 sctx->send_root->sectorsize);
4793 } else {
4794 extent_end = key->offset +
4795 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4796 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00004797
4798 if (path->slots[0] == 0 &&
4799 sctx->cur_inode_last_extent < key->offset) {
4800 /*
4801 * We might have skipped entire leafs that contained only
4802 * file extent items for our current inode. These leafs have
4803 * a generation number smaller (older) than the one in the
4804 * current leaf and the leaf our last extent came from, and
4805 * are located between these 2 leafs.
4806 */
4807 ret = get_last_extent(sctx, key->offset - 1);
4808 if (ret)
4809 return ret;
4810 }
4811
Josef Bacik16e75492013-10-22 12:18:51 -04004812 if (sctx->cur_inode_last_extent < key->offset)
4813 ret = send_hole(sctx, key->offset);
4814 sctx->cur_inode_last_extent = extent_end;
4815 return ret;
4816}
4817
Alexander Block31db9f72012-07-25 23:19:24 +02004818static int process_extent(struct send_ctx *sctx,
4819 struct btrfs_path *path,
4820 struct btrfs_key *key)
4821{
Alexander Block31db9f72012-07-25 23:19:24 +02004822 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004823 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004824
4825 if (S_ISLNK(sctx->cur_inode_mode))
4826 return 0;
4827
4828 if (sctx->parent_root && !sctx->cur_inode_new) {
4829 ret = is_extent_unchanged(sctx, path, key);
4830 if (ret < 0)
4831 goto out;
4832 if (ret) {
4833 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004834 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004835 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004836 } else {
4837 struct btrfs_file_extent_item *ei;
4838 u8 type;
4839
4840 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4841 struct btrfs_file_extent_item);
4842 type = btrfs_file_extent_type(path->nodes[0], ei);
4843 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4844 type == BTRFS_FILE_EXTENT_REG) {
4845 /*
4846 * The send spec does not have a prealloc command yet,
4847 * so just leave a hole for prealloc'ed extents until
4848 * we have enough commands queued up to justify rev'ing
4849 * the send spec.
4850 */
4851 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4852 ret = 0;
4853 goto out;
4854 }
4855
4856 /* Have a hole, just skip it. */
4857 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4858 ret = 0;
4859 goto out;
4860 }
4861 }
Alexander Block31db9f72012-07-25 23:19:24 +02004862 }
4863
4864 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4865 sctx->cur_inode_size, &found_clone);
4866 if (ret != -ENOENT && ret < 0)
4867 goto out;
4868
4869 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004870 if (ret)
4871 goto out;
4872out_hole:
4873 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004874out:
4875 return ret;
4876}
4877
4878static int process_all_extents(struct send_ctx *sctx)
4879{
4880 int ret;
4881 struct btrfs_root *root;
4882 struct btrfs_path *path;
4883 struct btrfs_key key;
4884 struct btrfs_key found_key;
4885 struct extent_buffer *eb;
4886 int slot;
4887
4888 root = sctx->send_root;
4889 path = alloc_path_for_send();
4890 if (!path)
4891 return -ENOMEM;
4892
4893 key.objectid = sctx->cmp_key->objectid;
4894 key.type = BTRFS_EXTENT_DATA_KEY;
4895 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004896 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4897 if (ret < 0)
4898 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004899
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004900 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004901 eb = path->nodes[0];
4902 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004903
4904 if (slot >= btrfs_header_nritems(eb)) {
4905 ret = btrfs_next_leaf(root, path);
4906 if (ret < 0) {
4907 goto out;
4908 } else if (ret > 0) {
4909 ret = 0;
4910 break;
4911 }
4912 continue;
4913 }
4914
Alexander Block31db9f72012-07-25 23:19:24 +02004915 btrfs_item_key_to_cpu(eb, &found_key, slot);
4916
4917 if (found_key.objectid != key.objectid ||
4918 found_key.type != key.type) {
4919 ret = 0;
4920 goto out;
4921 }
4922
4923 ret = process_extent(sctx, path, &found_key);
4924 if (ret < 0)
4925 goto out;
4926
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004927 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004928 }
4929
4930out:
4931 btrfs_free_path(path);
4932 return ret;
4933}
4934
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004935static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4936 int *pending_move,
4937 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02004938{
4939 int ret = 0;
4940
4941 if (sctx->cur_ino == 0)
4942 goto out;
4943 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004944 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004945 goto out;
4946 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4947 goto out;
4948
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004949 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004950 if (ret < 0)
4951 goto out;
4952
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004953 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004954out:
4955 return ret;
4956}
4957
4958static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4959{
4960 int ret = 0;
4961 u64 left_mode;
4962 u64 left_uid;
4963 u64 left_gid;
4964 u64 right_mode;
4965 u64 right_uid;
4966 u64 right_gid;
4967 int need_chmod = 0;
4968 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004969 int pending_move = 0;
4970 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004971
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004972 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4973 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02004974 if (ret < 0)
4975 goto out;
4976
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004977 /*
4978 * We have processed the refs and thus need to advance send_progress.
4979 * Now, calls to get_cur_xxx will take the updated refs of the current
4980 * inode into account.
4981 *
4982 * On the other hand, if our current inode is a directory and couldn't
4983 * be moved/renamed because its parent was renamed/moved too and it has
4984 * a higher inode number, we can only move/rename our current inode
4985 * after we moved/renamed its parent. Therefore in this case operate on
4986 * the old path (pre move/rename) of our current inode, and the
4987 * move/rename will be performed later.
4988 */
4989 if (refs_processed && !pending_move)
4990 sctx->send_progress = sctx->cur_ino + 1;
4991
Alexander Block31db9f72012-07-25 23:19:24 +02004992 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4993 goto out;
4994 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4995 goto out;
4996
4997 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004998 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004999 if (ret < 0)
5000 goto out;
5001
Alex Lyakase2d044f2012-10-17 13:52:47 +00005002 if (!sctx->parent_root || sctx->cur_inode_new) {
5003 need_chown = 1;
5004 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02005005 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00005006 } else {
5007 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5008 NULL, NULL, &right_mode, &right_uid,
5009 &right_gid, NULL);
5010 if (ret < 0)
5011 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02005012
Alex Lyakase2d044f2012-10-17 13:52:47 +00005013 if (left_uid != right_uid || left_gid != right_gid)
5014 need_chown = 1;
5015 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5016 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005017 }
5018
5019 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04005020 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01005021 if (sctx->cur_inode_last_extent == (u64)-1 ||
5022 sctx->cur_inode_last_extent <
5023 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04005024 ret = get_last_extent(sctx, (u64)-1);
5025 if (ret)
5026 goto out;
5027 }
5028 if (sctx->cur_inode_last_extent <
5029 sctx->cur_inode_size) {
5030 ret = send_hole(sctx, sctx->cur_inode_size);
5031 if (ret)
5032 goto out;
5033 }
5034 }
Alexander Block31db9f72012-07-25 23:19:24 +02005035 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5036 sctx->cur_inode_size);
5037 if (ret < 0)
5038 goto out;
5039 }
5040
5041 if (need_chown) {
5042 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5043 left_uid, left_gid);
5044 if (ret < 0)
5045 goto out;
5046 }
5047 if (need_chmod) {
5048 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5049 left_mode);
5050 if (ret < 0)
5051 goto out;
5052 }
5053
5054 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005055 * If other directory inodes depended on our current directory
5056 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005057 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005058 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5059 ret = apply_children_dir_moves(sctx);
5060 if (ret)
5061 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005062 /*
5063 * Need to send that every time, no matter if it actually
5064 * changed between the two trees as we have done changes to
5065 * the inode before. If our inode is a directory and it's
5066 * waiting to be moved/renamed, we will send its utimes when
5067 * it's moved/renamed, therefore we don't need to do it here.
5068 */
5069 sctx->send_progress = sctx->cur_ino + 1;
5070 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5071 if (ret < 0)
5072 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005073 }
5074
Alexander Block31db9f72012-07-25 23:19:24 +02005075out:
5076 return ret;
5077}
5078
5079static int changed_inode(struct send_ctx *sctx,
5080 enum btrfs_compare_tree_result result)
5081{
5082 int ret = 0;
5083 struct btrfs_key *key = sctx->cmp_key;
5084 struct btrfs_inode_item *left_ii = NULL;
5085 struct btrfs_inode_item *right_ii = NULL;
5086 u64 left_gen = 0;
5087 u64 right_gen = 0;
5088
Alexander Block31db9f72012-07-25 23:19:24 +02005089 sctx->cur_ino = key->objectid;
5090 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005091 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005092
5093 /*
5094 * Set send_progress to current inode. This will tell all get_cur_xxx
5095 * functions that the current inode's refs are not updated yet. Later,
5096 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5097 */
Alexander Block31db9f72012-07-25 23:19:24 +02005098 sctx->send_progress = sctx->cur_ino;
5099
5100 if (result == BTRFS_COMPARE_TREE_NEW ||
5101 result == BTRFS_COMPARE_TREE_CHANGED) {
5102 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5103 sctx->left_path->slots[0],
5104 struct btrfs_inode_item);
5105 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5106 left_ii);
5107 } else {
5108 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5109 sctx->right_path->slots[0],
5110 struct btrfs_inode_item);
5111 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5112 right_ii);
5113 }
5114 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5115 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5116 sctx->right_path->slots[0],
5117 struct btrfs_inode_item);
5118
5119 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5120 right_ii);
Alexander Block6d85ed052012-08-01 14:48:59 +02005121
5122 /*
5123 * The cur_ino = root dir case is special here. We can't treat
5124 * the inode as deleted+reused because it would generate a
5125 * stream that tries to delete/mkdir the root dir.
5126 */
5127 if (left_gen != right_gen &&
5128 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005129 sctx->cur_inode_new_gen = 1;
5130 }
5131
5132 if (result == BTRFS_COMPARE_TREE_NEW) {
5133 sctx->cur_inode_gen = left_gen;
5134 sctx->cur_inode_new = 1;
5135 sctx->cur_inode_deleted = 0;
5136 sctx->cur_inode_size = btrfs_inode_size(
5137 sctx->left_path->nodes[0], left_ii);
5138 sctx->cur_inode_mode = btrfs_inode_mode(
5139 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005140 sctx->cur_inode_rdev = btrfs_inode_rdev(
5141 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005142 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005143 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005144 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5145 sctx->cur_inode_gen = right_gen;
5146 sctx->cur_inode_new = 0;
5147 sctx->cur_inode_deleted = 1;
5148 sctx->cur_inode_size = btrfs_inode_size(
5149 sctx->right_path->nodes[0], right_ii);
5150 sctx->cur_inode_mode = btrfs_inode_mode(
5151 sctx->right_path->nodes[0], right_ii);
5152 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005153 /*
5154 * We need to do some special handling in case the inode was
5155 * reported as changed with a changed generation number. This
5156 * means that the original inode was deleted and new inode
5157 * reused the same inum. So we have to treat the old inode as
5158 * deleted and the new one as new.
5159 */
Alexander Block31db9f72012-07-25 23:19:24 +02005160 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005161 /*
5162 * First, process the inode as if it was deleted.
5163 */
Alexander Block31db9f72012-07-25 23:19:24 +02005164 sctx->cur_inode_gen = right_gen;
5165 sctx->cur_inode_new = 0;
5166 sctx->cur_inode_deleted = 1;
5167 sctx->cur_inode_size = btrfs_inode_size(
5168 sctx->right_path->nodes[0], right_ii);
5169 sctx->cur_inode_mode = btrfs_inode_mode(
5170 sctx->right_path->nodes[0], right_ii);
5171 ret = process_all_refs(sctx,
5172 BTRFS_COMPARE_TREE_DELETED);
5173 if (ret < 0)
5174 goto out;
5175
Alexander Block766702e2012-07-28 14:11:31 +02005176 /*
5177 * Now process the inode as if it was new.
5178 */
Alexander Block31db9f72012-07-25 23:19:24 +02005179 sctx->cur_inode_gen = left_gen;
5180 sctx->cur_inode_new = 1;
5181 sctx->cur_inode_deleted = 0;
5182 sctx->cur_inode_size = btrfs_inode_size(
5183 sctx->left_path->nodes[0], left_ii);
5184 sctx->cur_inode_mode = btrfs_inode_mode(
5185 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005186 sctx->cur_inode_rdev = btrfs_inode_rdev(
5187 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005188 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005189 if (ret < 0)
5190 goto out;
5191
5192 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5193 if (ret < 0)
5194 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005195 /*
5196 * Advance send_progress now as we did not get into
5197 * process_recorded_refs_if_needed in the new_gen case.
5198 */
5199 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005200
5201 /*
5202 * Now process all extents and xattrs of the inode as if
5203 * they were all new.
5204 */
Alexander Block31db9f72012-07-25 23:19:24 +02005205 ret = process_all_extents(sctx);
5206 if (ret < 0)
5207 goto out;
5208 ret = process_all_new_xattrs(sctx);
5209 if (ret < 0)
5210 goto out;
5211 } else {
5212 sctx->cur_inode_gen = left_gen;
5213 sctx->cur_inode_new = 0;
5214 sctx->cur_inode_new_gen = 0;
5215 sctx->cur_inode_deleted = 0;
5216 sctx->cur_inode_size = btrfs_inode_size(
5217 sctx->left_path->nodes[0], left_ii);
5218 sctx->cur_inode_mode = btrfs_inode_mode(
5219 sctx->left_path->nodes[0], left_ii);
5220 }
5221 }
5222
5223out:
5224 return ret;
5225}
5226
Alexander Block766702e2012-07-28 14:11:31 +02005227/*
5228 * We have to process new refs before deleted refs, but compare_trees gives us
5229 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5230 * first and later process them in process_recorded_refs.
5231 * For the cur_inode_new_gen case, we skip recording completely because
5232 * changed_inode did already initiate processing of refs. The reason for this is
5233 * that in this case, compare_tree actually compares the refs of 2 different
5234 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5235 * refs of the right tree as deleted and all refs of the left tree as new.
5236 */
Alexander Block31db9f72012-07-25 23:19:24 +02005237static int changed_ref(struct send_ctx *sctx,
5238 enum btrfs_compare_tree_result result)
5239{
5240 int ret = 0;
5241
5242 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5243
5244 if (!sctx->cur_inode_new_gen &&
5245 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5246 if (result == BTRFS_COMPARE_TREE_NEW)
5247 ret = record_new_ref(sctx);
5248 else if (result == BTRFS_COMPARE_TREE_DELETED)
5249 ret = record_deleted_ref(sctx);
5250 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5251 ret = record_changed_ref(sctx);
5252 }
5253
5254 return ret;
5255}
5256
Alexander Block766702e2012-07-28 14:11:31 +02005257/*
5258 * Process new/deleted/changed xattrs. We skip processing in the
5259 * cur_inode_new_gen case because changed_inode did already initiate processing
5260 * of xattrs. The reason is the same as in changed_ref
5261 */
Alexander Block31db9f72012-07-25 23:19:24 +02005262static int changed_xattr(struct send_ctx *sctx,
5263 enum btrfs_compare_tree_result result)
5264{
5265 int ret = 0;
5266
5267 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5268
5269 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5270 if (result == BTRFS_COMPARE_TREE_NEW)
5271 ret = process_new_xattr(sctx);
5272 else if (result == BTRFS_COMPARE_TREE_DELETED)
5273 ret = process_deleted_xattr(sctx);
5274 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5275 ret = process_changed_xattr(sctx);
5276 }
5277
5278 return ret;
5279}
5280
Alexander Block766702e2012-07-28 14:11:31 +02005281/*
5282 * Process new/deleted/changed extents. We skip processing in the
5283 * cur_inode_new_gen case because changed_inode did already initiate processing
5284 * of extents. The reason is the same as in changed_ref
5285 */
Alexander Block31db9f72012-07-25 23:19:24 +02005286static int changed_extent(struct send_ctx *sctx,
5287 enum btrfs_compare_tree_result result)
5288{
5289 int ret = 0;
5290
5291 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5292
5293 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5294 if (result != BTRFS_COMPARE_TREE_DELETED)
5295 ret = process_extent(sctx, sctx->left_path,
5296 sctx->cmp_key);
5297 }
5298
5299 return ret;
5300}
5301
Josef Bacikba5e8f22013-08-16 16:52:55 -04005302static int dir_changed(struct send_ctx *sctx, u64 dir)
5303{
5304 u64 orig_gen, new_gen;
5305 int ret;
5306
5307 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5308 NULL, NULL);
5309 if (ret)
5310 return ret;
5311
5312 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5313 NULL, NULL, NULL);
5314 if (ret)
5315 return ret;
5316
5317 return (orig_gen != new_gen) ? 1 : 0;
5318}
5319
5320static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5321 struct btrfs_key *key)
5322{
5323 struct btrfs_inode_extref *extref;
5324 struct extent_buffer *leaf;
5325 u64 dirid = 0, last_dirid = 0;
5326 unsigned long ptr;
5327 u32 item_size;
5328 u32 cur_offset = 0;
5329 int ref_name_len;
5330 int ret = 0;
5331
5332 /* Easy case, just check this one dirid */
5333 if (key->type == BTRFS_INODE_REF_KEY) {
5334 dirid = key->offset;
5335
5336 ret = dir_changed(sctx, dirid);
5337 goto out;
5338 }
5339
5340 leaf = path->nodes[0];
5341 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5342 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5343 while (cur_offset < item_size) {
5344 extref = (struct btrfs_inode_extref *)(ptr +
5345 cur_offset);
5346 dirid = btrfs_inode_extref_parent(leaf, extref);
5347 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5348 cur_offset += ref_name_len + sizeof(*extref);
5349 if (dirid == last_dirid)
5350 continue;
5351 ret = dir_changed(sctx, dirid);
5352 if (ret)
5353 break;
5354 last_dirid = dirid;
5355 }
5356out:
5357 return ret;
5358}
5359
Alexander Block766702e2012-07-28 14:11:31 +02005360/*
5361 * Updates compare related fields in sctx and simply forwards to the actual
5362 * changed_xxx functions.
5363 */
Alexander Block31db9f72012-07-25 23:19:24 +02005364static int changed_cb(struct btrfs_root *left_root,
5365 struct btrfs_root *right_root,
5366 struct btrfs_path *left_path,
5367 struct btrfs_path *right_path,
5368 struct btrfs_key *key,
5369 enum btrfs_compare_tree_result result,
5370 void *ctx)
5371{
5372 int ret = 0;
5373 struct send_ctx *sctx = ctx;
5374
Josef Bacikba5e8f22013-08-16 16:52:55 -04005375 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005376 if (key->type == BTRFS_INODE_REF_KEY ||
5377 key->type == BTRFS_INODE_EXTREF_KEY) {
5378 ret = compare_refs(sctx, left_path, key);
5379 if (!ret)
5380 return 0;
5381 if (ret < 0)
5382 return ret;
5383 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5384 return maybe_send_hole(sctx, left_path, key);
5385 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005386 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005387 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005388 result = BTRFS_COMPARE_TREE_CHANGED;
5389 ret = 0;
5390 }
5391
Alexander Block31db9f72012-07-25 23:19:24 +02005392 sctx->left_path = left_path;
5393 sctx->right_path = right_path;
5394 sctx->cmp_key = key;
5395
5396 ret = finish_inode_if_needed(sctx, 0);
5397 if (ret < 0)
5398 goto out;
5399
Alexander Block2981e222012-08-01 14:47:03 +02005400 /* Ignore non-FS objects */
5401 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5402 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5403 goto out;
5404
Alexander Block31db9f72012-07-25 23:19:24 +02005405 if (key->type == BTRFS_INODE_ITEM_KEY)
5406 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005407 else if (key->type == BTRFS_INODE_REF_KEY ||
5408 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005409 ret = changed_ref(sctx, result);
5410 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5411 ret = changed_xattr(sctx, result);
5412 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5413 ret = changed_extent(sctx, result);
5414
5415out:
5416 return ret;
5417}
5418
5419static int full_send_tree(struct send_ctx *sctx)
5420{
5421 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005422 struct btrfs_root *send_root = sctx->send_root;
5423 struct btrfs_key key;
5424 struct btrfs_key found_key;
5425 struct btrfs_path *path;
5426 struct extent_buffer *eb;
5427 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02005428
5429 path = alloc_path_for_send();
5430 if (!path)
5431 return -ENOMEM;
5432
Alexander Block31db9f72012-07-25 23:19:24 +02005433 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5434 key.type = BTRFS_INODE_ITEM_KEY;
5435 key.offset = 0;
5436
Alexander Block31db9f72012-07-25 23:19:24 +02005437 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5438 if (ret < 0)
5439 goto out;
5440 if (ret)
5441 goto out_finish;
5442
5443 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005444 eb = path->nodes[0];
5445 slot = path->slots[0];
5446 btrfs_item_key_to_cpu(eb, &found_key, slot);
5447
5448 ret = changed_cb(send_root, NULL, path, NULL,
5449 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5450 if (ret < 0)
5451 goto out;
5452
5453 key.objectid = found_key.objectid;
5454 key.type = found_key.type;
5455 key.offset = found_key.offset + 1;
5456
5457 ret = btrfs_next_item(send_root, path);
5458 if (ret < 0)
5459 goto out;
5460 if (ret) {
5461 ret = 0;
5462 break;
5463 }
5464 }
5465
5466out_finish:
5467 ret = finish_inode_if_needed(sctx, 1);
5468
5469out:
5470 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02005471 return ret;
5472}
5473
5474static int send_subvol(struct send_ctx *sctx)
5475{
5476 int ret;
5477
Stefan Behrensc2c71322013-04-10 17:10:52 +00005478 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5479 ret = send_header(sctx);
5480 if (ret < 0)
5481 goto out;
5482 }
Alexander Block31db9f72012-07-25 23:19:24 +02005483
5484 ret = send_subvol_begin(sctx);
5485 if (ret < 0)
5486 goto out;
5487
5488 if (sctx->parent_root) {
5489 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5490 changed_cb, sctx);
5491 if (ret < 0)
5492 goto out;
5493 ret = finish_inode_if_needed(sctx, 1);
5494 if (ret < 0)
5495 goto out;
5496 } else {
5497 ret = full_send_tree(sctx);
5498 if (ret < 0)
5499 goto out;
5500 }
5501
5502out:
Alexander Block31db9f72012-07-25 23:19:24 +02005503 free_recorded_refs(sctx);
5504 return ret;
5505}
5506
Filipe Mananae5fa8f82014-10-21 11:11:41 +01005507/*
5508 * If orphan cleanup did remove any orphans from a root, it means the tree
5509 * was modified and therefore the commit root is not the same as the current
5510 * root anymore. This is a problem, because send uses the commit root and
5511 * therefore can see inode items that don't exist in the current root anymore,
5512 * and for example make calls to btrfs_iget, which will do tree lookups based
5513 * on the current root and not on the commit root. Those lookups will fail,
5514 * returning a -ESTALE error, and making send fail with that error. So make
5515 * sure a send does not see any orphans we have just removed, and that it will
5516 * see the same inodes regardless of whether a transaction commit happened
5517 * before it started (meaning that the commit root will be the same as the
5518 * current root) or not.
5519 */
5520static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
5521{
5522 int i;
5523 struct btrfs_trans_handle *trans = NULL;
5524
5525again:
5526 if (sctx->parent_root &&
5527 sctx->parent_root->node != sctx->parent_root->commit_root)
5528 goto commit_trans;
5529
5530 for (i = 0; i < sctx->clone_roots_cnt; i++)
5531 if (sctx->clone_roots[i].root->node !=
5532 sctx->clone_roots[i].root->commit_root)
5533 goto commit_trans;
5534
5535 if (trans)
5536 return btrfs_end_transaction(trans, sctx->send_root);
5537
5538 return 0;
5539
5540commit_trans:
5541 /* Use any root, all fs roots will get their commit roots updated. */
5542 if (!trans) {
5543 trans = btrfs_join_transaction(sctx->send_root);
5544 if (IS_ERR(trans))
5545 return PTR_ERR(trans);
5546 goto again;
5547 }
5548
5549 return btrfs_commit_transaction(trans, sctx->send_root);
5550}
5551
David Sterba66ef7d62013-12-17 15:07:20 +01005552static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5553{
5554 spin_lock(&root->root_item_lock);
5555 root->send_in_progress--;
5556 /*
5557 * Not much left to do, we don't know why it's unbalanced and
5558 * can't blindly reset it to 0.
5559 */
5560 if (root->send_in_progress < 0)
5561 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02005562 "send_in_progres unbalanced %d root %llu",
David Sterba66ef7d62013-12-17 15:07:20 +01005563 root->send_in_progress, root->root_key.objectid);
5564 spin_unlock(&root->root_item_lock);
5565}
5566
Alexander Block31db9f72012-07-25 23:19:24 +02005567long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5568{
5569 int ret = 0;
5570 struct btrfs_root *send_root;
5571 struct btrfs_root *clone_root;
5572 struct btrfs_fs_info *fs_info;
5573 struct btrfs_ioctl_send_args *arg = NULL;
5574 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02005575 struct send_ctx *sctx = NULL;
5576 u32 i;
5577 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01005578 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08005579 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08005580 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02005581
5582 if (!capable(CAP_SYS_ADMIN))
5583 return -EPERM;
5584
Al Viro496ad9a2013-01-23 17:07:38 -05005585 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02005586 fs_info = send_root->fs_info;
5587
Josef Bacik139f8072013-05-20 11:26:50 -04005588 /*
David Sterba2c686532013-12-16 17:34:17 +01005589 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02005590 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01005591 */
5592 spin_lock(&send_root->root_item_lock);
5593 send_root->send_in_progress++;
5594 spin_unlock(&send_root->root_item_lock);
5595
5596 /*
Josef Bacik139f8072013-05-20 11:26:50 -04005597 * This is done when we lookup the root, it should already be complete
5598 * by the time we get here.
5599 */
5600 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5601
5602 /*
David Sterba2c686532013-12-16 17:34:17 +01005603 * Userspace tools do the checks and warn the user if it's
5604 * not RO.
5605 */
5606 if (!btrfs_root_readonly(send_root)) {
5607 ret = -EPERM;
5608 goto out;
5609 }
5610
Alexander Block31db9f72012-07-25 23:19:24 +02005611 arg = memdup_user(arg_, sizeof(*arg));
5612 if (IS_ERR(arg)) {
5613 ret = PTR_ERR(arg);
5614 arg = NULL;
5615 goto out;
5616 }
5617
5618 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05005619 sizeof(*arg->clone_sources) *
5620 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02005621 ret = -EFAULT;
5622 goto out;
5623 }
5624
Stefan Behrensc2c71322013-04-10 17:10:52 +00005625 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005626 ret = -EINVAL;
5627 goto out;
5628 }
5629
Alexander Block31db9f72012-07-25 23:19:24 +02005630 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5631 if (!sctx) {
5632 ret = -ENOMEM;
5633 goto out;
5634 }
5635
5636 INIT_LIST_HEAD(&sctx->new_refs);
5637 INIT_LIST_HEAD(&sctx->deleted_refs);
5638 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5639 INIT_LIST_HEAD(&sctx->name_cache_list);
5640
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005641 sctx->flags = arg->flags;
5642
Alexander Block31db9f72012-07-25 23:19:24 +02005643 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00005644 if (!sctx->send_filp) {
5645 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02005646 goto out;
5647 }
5648
Alexander Block31db9f72012-07-25 23:19:24 +02005649 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02005650 /*
5651 * Unlikely but possible, if the subvolume is marked for deletion but
5652 * is slow to remove the directory entry, send can still be started
5653 */
5654 if (btrfs_root_dead(sctx->send_root)) {
5655 ret = -EPERM;
5656 goto out;
5657 }
5658
Alexander Block31db9f72012-07-25 23:19:24 +02005659 sctx->clone_roots_cnt = arg->clone_sources_count;
5660
5661 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5662 sctx->send_buf = vmalloc(sctx->send_max_size);
5663 if (!sctx->send_buf) {
5664 ret = -ENOMEM;
5665 goto out;
5666 }
5667
5668 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5669 if (!sctx->read_buf) {
5670 ret = -ENOMEM;
5671 goto out;
5672 }
5673
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005674 sctx->pending_dir_moves = RB_ROOT;
5675 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00005676 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005677
Alexander Block31db9f72012-07-25 23:19:24 +02005678 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5679 (arg->clone_sources_count + 1));
5680 if (!sctx->clone_roots) {
5681 ret = -ENOMEM;
5682 goto out;
5683 }
5684
5685 if (arg->clone_sources_count) {
5686 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5687 sizeof(*arg->clone_sources));
5688 if (!clone_sources_tmp) {
5689 ret = -ENOMEM;
5690 goto out;
5691 }
5692
5693 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5694 arg->clone_sources_count *
5695 sizeof(*arg->clone_sources));
5696 if (ret) {
5697 ret = -EFAULT;
5698 goto out;
5699 }
5700
5701 for (i = 0; i < arg->clone_sources_count; i++) {
5702 key.objectid = clone_sources_tmp[i];
5703 key.type = BTRFS_ROOT_ITEM_KEY;
5704 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005705
5706 index = srcu_read_lock(&fs_info->subvol_srcu);
5707
Alexander Block31db9f72012-07-25 23:19:24 +02005708 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02005709 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005710 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005711 ret = PTR_ERR(clone_root);
5712 goto out;
5713 }
David Sterba2c686532013-12-16 17:34:17 +01005714 clone_sources_to_rollback = i + 1;
5715 spin_lock(&clone_root->root_item_lock);
5716 clone_root->send_in_progress++;
5717 if (!btrfs_root_readonly(clone_root)) {
5718 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005719 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005720 ret = -EPERM;
5721 goto out;
5722 }
5723 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005724 srcu_read_unlock(&fs_info->subvol_srcu, index);
5725
Alexander Block31db9f72012-07-25 23:19:24 +02005726 sctx->clone_roots[i].root = clone_root;
5727 }
5728 vfree(clone_sources_tmp);
5729 clone_sources_tmp = NULL;
5730 }
5731
5732 if (arg->parent_root) {
5733 key.objectid = arg->parent_root;
5734 key.type = BTRFS_ROOT_ITEM_KEY;
5735 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005736
5737 index = srcu_read_lock(&fs_info->subvol_srcu);
5738
Alexander Block31db9f72012-07-25 23:19:24 +02005739 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005740 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005741 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005742 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02005743 goto out;
5744 }
Wang Shilong18f687d2014-01-07 17:25:19 +08005745
David Sterba2c686532013-12-16 17:34:17 +01005746 spin_lock(&sctx->parent_root->root_item_lock);
5747 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02005748 if (!btrfs_root_readonly(sctx->parent_root) ||
5749 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01005750 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005751 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005752 ret = -EPERM;
5753 goto out;
5754 }
5755 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005756
5757 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005758 }
5759
5760 /*
5761 * Clones from send_root are allowed, but only if the clone source
5762 * is behind the current send position. This is checked while searching
5763 * for possible clone sources.
5764 */
5765 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5766
5767 /* We do a bsearch later */
5768 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5769 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5770 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08005771 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005772
Filipe Mananae5fa8f82014-10-21 11:11:41 +01005773 ret = ensure_commit_roots_uptodate(sctx);
5774 if (ret)
5775 goto out;
5776
David Sterba2755a0d2014-07-31 00:43:18 +02005777 current->journal_info = BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02005778 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04005779 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02005780 if (ret < 0)
5781 goto out;
5782
Stefan Behrensc2c71322013-04-10 17:10:52 +00005783 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5784 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5785 if (ret < 0)
5786 goto out;
5787 ret = send_cmd(sctx);
5788 if (ret < 0)
5789 goto out;
5790 }
Alexander Block31db9f72012-07-25 23:19:24 +02005791
5792out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005793 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5794 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5795 struct rb_node *n;
5796 struct pending_dir_move *pm;
5797
5798 n = rb_first(&sctx->pending_dir_moves);
5799 pm = rb_entry(n, struct pending_dir_move, node);
5800 while (!list_empty(&pm->list)) {
5801 struct pending_dir_move *pm2;
5802
5803 pm2 = list_first_entry(&pm->list,
5804 struct pending_dir_move, list);
5805 free_pending_move(sctx, pm2);
5806 }
5807 free_pending_move(sctx, pm);
5808 }
5809
5810 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5811 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5812 struct rb_node *n;
5813 struct waiting_dir_move *dm;
5814
5815 n = rb_first(&sctx->waiting_dir_moves);
5816 dm = rb_entry(n, struct waiting_dir_move, node);
5817 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5818 kfree(dm);
5819 }
5820
Filipe Manana9dc44212014-02-19 14:31:44 +00005821 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5822 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5823 struct rb_node *n;
5824 struct orphan_dir_info *odi;
5825
5826 n = rb_first(&sctx->orphan_dirs);
5827 odi = rb_entry(n, struct orphan_dir_info, node);
5828 free_orphan_dir_info(sctx, odi);
5829 }
5830
Wang Shilong896c14f2014-01-07 17:25:18 +08005831 if (sort_clone_roots) {
5832 for (i = 0; i < sctx->clone_roots_cnt; i++)
5833 btrfs_root_dec_send_in_progress(
5834 sctx->clone_roots[i].root);
5835 } else {
5836 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5837 btrfs_root_dec_send_in_progress(
5838 sctx->clone_roots[i].root);
5839
5840 btrfs_root_dec_send_in_progress(send_root);
5841 }
David Sterba66ef7d62013-12-17 15:07:20 +01005842 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5843 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01005844
Alexander Block31db9f72012-07-25 23:19:24 +02005845 kfree(arg);
5846 vfree(clone_sources_tmp);
5847
5848 if (sctx) {
5849 if (sctx->send_filp)
5850 fput(sctx->send_filp);
5851
5852 vfree(sctx->clone_roots);
5853 vfree(sctx->send_buf);
5854 vfree(sctx->read_buf);
5855
5856 name_cache_free(sctx);
5857
5858 kfree(sctx);
5859 }
5860
5861 return ret;
5862}