blob: 3f14b31c0c961ca5751faf4238918f6d720dcdb7 [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 Bo2131bcd32014-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) {
Anand Jain1bcea352012-09-14 00:04:21 -0600518 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200519 /* TODO handle that correctly */
520 /*if (ret == -ERESTARTSYS) {
521 continue;
522 }*/
523 if (ret < 0)
524 goto out;
525 if (ret == 0) {
526 ret = -EIO;
527 goto out;
528 }
529 pos += ret;
530 }
531
532 ret = 0;
533
534out:
535 set_fs(old_fs);
536 return ret;
537}
538
539static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
540{
541 struct btrfs_tlv_header *hdr;
542 int total_len = sizeof(*hdr) + len;
543 int left = sctx->send_max_size - sctx->send_size;
544
545 if (unlikely(left < total_len))
546 return -EOVERFLOW;
547
548 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
549 hdr->tlv_type = cpu_to_le16(attr);
550 hdr->tlv_len = cpu_to_le16(len);
551 memcpy(hdr + 1, data, len);
552 sctx->send_size += total_len;
553
554 return 0;
555}
556
David Sterba95bc79d2013-12-16 17:34:10 +0100557#define TLV_PUT_DEFINE_INT(bits) \
558 static int tlv_put_u##bits(struct send_ctx *sctx, \
559 u##bits attr, u##bits value) \
560 { \
561 __le##bits __tmp = cpu_to_le##bits(value); \
562 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
563 }
Alexander Block31db9f72012-07-25 23:19:24 +0200564
David Sterba95bc79d2013-12-16 17:34:10 +0100565TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200566
567static int tlv_put_string(struct send_ctx *sctx, u16 attr,
568 const char *str, int len)
569{
570 if (len == -1)
571 len = strlen(str);
572 return tlv_put(sctx, attr, str, len);
573}
574
575static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
576 const u8 *uuid)
577{
578 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
579}
580
Alexander Block31db9f72012-07-25 23:19:24 +0200581static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
582 struct extent_buffer *eb,
583 struct btrfs_timespec *ts)
584{
585 struct btrfs_timespec bts;
586 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
587 return tlv_put(sctx, attr, &bts, sizeof(bts));
588}
589
590
591#define TLV_PUT(sctx, attrtype, attrlen, data) \
592 do { \
593 ret = tlv_put(sctx, attrtype, attrlen, data); \
594 if (ret < 0) \
595 goto tlv_put_failure; \
596 } while (0)
597
598#define TLV_PUT_INT(sctx, attrtype, bits, value) \
599 do { \
600 ret = tlv_put_u##bits(sctx, attrtype, value); \
601 if (ret < 0) \
602 goto tlv_put_failure; \
603 } while (0)
604
605#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
606#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
607#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
608#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
609#define TLV_PUT_STRING(sctx, attrtype, str, len) \
610 do { \
611 ret = tlv_put_string(sctx, attrtype, str, len); \
612 if (ret < 0) \
613 goto tlv_put_failure; \
614 } while (0)
615#define TLV_PUT_PATH(sctx, attrtype, p) \
616 do { \
617 ret = tlv_put_string(sctx, attrtype, p->start, \
618 p->end - p->start); \
619 if (ret < 0) \
620 goto tlv_put_failure; \
621 } while(0)
622#define TLV_PUT_UUID(sctx, attrtype, uuid) \
623 do { \
624 ret = tlv_put_uuid(sctx, attrtype, uuid); \
625 if (ret < 0) \
626 goto tlv_put_failure; \
627 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200628#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
629 do { \
630 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
631 if (ret < 0) \
632 goto tlv_put_failure; \
633 } while (0)
634
635static int send_header(struct send_ctx *sctx)
636{
637 struct btrfs_stream_header hdr;
638
639 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
640 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
641
Anand Jain1bcea352012-09-14 00:04:21 -0600642 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
643 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200644}
645
646/*
647 * For each command/item we want to send to userspace, we call this function.
648 */
649static int begin_cmd(struct send_ctx *sctx, int cmd)
650{
651 struct btrfs_cmd_header *hdr;
652
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530653 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200654 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200655
656 BUG_ON(sctx->send_size);
657
658 sctx->send_size += sizeof(*hdr);
659 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
660 hdr->cmd = cpu_to_le16(cmd);
661
662 return 0;
663}
664
665static int send_cmd(struct send_ctx *sctx)
666{
667 int ret;
668 struct btrfs_cmd_header *hdr;
669 u32 crc;
670
671 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
672 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
673 hdr->crc = 0;
674
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000675 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200676 hdr->crc = cpu_to_le32(crc);
677
Anand Jain1bcea352012-09-14 00:04:21 -0600678 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
679 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200680
681 sctx->total_send_size += sctx->send_size;
682 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
683 sctx->send_size = 0;
684
685 return ret;
686}
687
688/*
689 * Sends a move instruction to user space
690 */
691static int send_rename(struct send_ctx *sctx,
692 struct fs_path *from, struct fs_path *to)
693{
694 int ret;
695
696verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
697
698 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
699 if (ret < 0)
700 goto out;
701
702 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
703 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
704
705 ret = send_cmd(sctx);
706
707tlv_put_failure:
708out:
709 return ret;
710}
711
712/*
713 * Sends a link instruction to user space
714 */
715static int send_link(struct send_ctx *sctx,
716 struct fs_path *path, struct fs_path *lnk)
717{
718 int ret;
719
720verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
721
722 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
723 if (ret < 0)
724 goto out;
725
726 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
727 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
728
729 ret = send_cmd(sctx);
730
731tlv_put_failure:
732out:
733 return ret;
734}
735
736/*
737 * Sends an unlink instruction to user space
738 */
739static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
740{
741 int ret;
742
743verbose_printk("btrfs: send_unlink %s\n", path->start);
744
745 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
746 if (ret < 0)
747 goto out;
748
749 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
750
751 ret = send_cmd(sctx);
752
753tlv_put_failure:
754out:
755 return ret;
756}
757
758/*
759 * Sends a rmdir instruction to user space
760 */
761static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
762{
763 int ret;
764
765verbose_printk("btrfs: send_rmdir %s\n", path->start);
766
767 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
768 if (ret < 0)
769 goto out;
770
771 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
772
773 ret = send_cmd(sctx);
774
775tlv_put_failure:
776out:
777 return ret;
778}
779
780/*
781 * Helper function to retrieve some fields from an inode item.
782 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400783static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
784 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
785 u64 *gid, u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200786{
787 int ret;
788 struct btrfs_inode_item *ii;
789 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +0200790
791 key.objectid = ino;
792 key.type = BTRFS_INODE_ITEM_KEY;
793 key.offset = 0;
794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Alexander Block31db9f72012-07-25 23:19:24 +0200795 if (ret) {
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400796 if (ret > 0)
797 ret = -ENOENT;
798 return ret;
Alexander Block31db9f72012-07-25 23:19:24 +0200799 }
800
801 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
802 struct btrfs_inode_item);
803 if (size)
804 *size = btrfs_inode_size(path->nodes[0], ii);
805 if (gen)
806 *gen = btrfs_inode_generation(path->nodes[0], ii);
807 if (mode)
808 *mode = btrfs_inode_mode(path->nodes[0], ii);
809 if (uid)
810 *uid = btrfs_inode_uid(path->nodes[0], ii);
811 if (gid)
812 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200813 if (rdev)
814 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200815
Josef Bacik3f8a18c2014-03-28 17:16:01 -0400816 return ret;
817}
818
819static int get_inode_info(struct btrfs_root *root,
820 u64 ino, u64 *size, u64 *gen,
821 u64 *mode, u64 *uid, u64 *gid,
822 u64 *rdev)
823{
824 struct btrfs_path *path;
825 int ret;
826
827 path = alloc_path_for_send();
828 if (!path)
829 return -ENOMEM;
830 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
831 rdev);
Alexander Block31db9f72012-07-25 23:19:24 +0200832 btrfs_free_path(path);
833 return ret;
834}
835
836typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
837 struct fs_path *p,
838 void *ctx);
839
840/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000841 * Helper function to iterate the entries in ONE btrfs_inode_ref or
842 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200843 * The iterate callback may return a non zero value to stop iteration. This can
844 * be a negative value for error codes or 1 to simply stop it.
845 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000846 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200847 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000848static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200849 struct btrfs_key *found_key, int resolve,
850 iterate_inode_ref_t iterate, void *ctx)
851{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000852 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200853 struct btrfs_item *item;
854 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000855 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200856 struct btrfs_path *tmp_path;
857 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000858 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200859 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000860 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200861 u32 name_len;
862 char *start;
863 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000864 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200865 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000866 u64 dir;
867 unsigned long name_off;
868 unsigned long elem_size;
869 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200870
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000871 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200872 if (!p)
873 return -ENOMEM;
874
875 tmp_path = alloc_path_for_send();
876 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000877 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200878 return -ENOMEM;
879 }
880
Alexander Block31db9f72012-07-25 23:19:24 +0200881
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000882 if (found_key->type == BTRFS_INODE_REF_KEY) {
883 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
884 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100885 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000886 total = btrfs_item_size(eb, item);
887 elem_size = sizeof(*iref);
888 } else {
889 ptr = btrfs_item_ptr_offset(eb, slot);
890 total = btrfs_item_size_nr(eb, slot);
891 elem_size = sizeof(*extref);
892 }
893
Alexander Block31db9f72012-07-25 23:19:24 +0200894 while (cur < total) {
895 fs_path_reset(p);
896
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000897 if (found_key->type == BTRFS_INODE_REF_KEY) {
898 iref = (struct btrfs_inode_ref *)(ptr + cur);
899 name_len = btrfs_inode_ref_name_len(eb, iref);
900 name_off = (unsigned long)(iref + 1);
901 index = btrfs_inode_ref_index(eb, iref);
902 dir = found_key->offset;
903 } else {
904 extref = (struct btrfs_inode_extref *)(ptr + cur);
905 name_len = btrfs_inode_extref_name_len(eb, extref);
906 name_off = (unsigned long)&extref->name;
907 index = btrfs_inode_extref_index(eb, extref);
908 dir = btrfs_inode_extref_parent(eb, extref);
909 }
910
Alexander Block31db9f72012-07-25 23:19:24 +0200911 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000912 start = btrfs_ref_to_path(root, tmp_path, name_len,
913 name_off, eb, dir,
914 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200915 if (IS_ERR(start)) {
916 ret = PTR_ERR(start);
917 goto out;
918 }
919 if (start < p->buf) {
920 /* overflow , try again with larger buffer */
921 ret = fs_path_ensure_buf(p,
922 p->buf_len + p->buf - start);
923 if (ret < 0)
924 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000925 start = btrfs_ref_to_path(root, tmp_path,
926 name_len, name_off,
927 eb, dir,
928 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200929 if (IS_ERR(start)) {
930 ret = PTR_ERR(start);
931 goto out;
932 }
933 BUG_ON(start < p->buf);
934 }
935 p->start = start;
936 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000937 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
938 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200939 if (ret < 0)
940 goto out;
941 }
942
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000943 cur += elem_size + name_len;
944 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200945 if (ret)
946 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200947 num++;
948 }
949
950out:
951 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000952 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200953 return ret;
954}
955
956typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
957 const char *name, int name_len,
958 const char *data, int data_len,
959 u8 type, void *ctx);
960
961/*
962 * Helper function to iterate the entries in ONE btrfs_dir_item.
963 * The iterate callback may return a non zero value to stop iteration. This can
964 * be a negative value for error codes or 1 to simply stop it.
965 *
966 * path must point to the dir item when called.
967 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000968static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200969 struct btrfs_key *found_key,
970 iterate_dir_item_t iterate, void *ctx)
971{
972 int ret = 0;
973 struct extent_buffer *eb;
974 struct btrfs_item *item;
975 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200976 struct btrfs_key di_key;
977 char *buf = NULL;
David Sterbaace01052014-02-05 16:17:34 +0100978 const int buf_len = PATH_MAX;
Alexander Block31db9f72012-07-25 23:19:24 +0200979 u32 name_len;
980 u32 data_len;
981 u32 cur;
982 u32 len;
983 u32 total;
984 int slot;
985 int num;
986 u8 type;
987
Alexander Block31db9f72012-07-25 23:19:24 +0200988 buf = kmalloc(buf_len, GFP_NOFS);
989 if (!buf) {
990 ret = -ENOMEM;
991 goto out;
992 }
993
Alexander Block31db9f72012-07-25 23:19:24 +0200994 eb = path->nodes[0];
995 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +0100996 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +0200997 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
998 cur = 0;
999 len = 0;
1000 total = btrfs_item_size(eb, item);
1001
1002 num = 0;
1003 while (cur < total) {
1004 name_len = btrfs_dir_name_len(eb, di);
1005 data_len = btrfs_dir_data_len(eb, di);
1006 type = btrfs_dir_type(eb, di);
1007 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1008
David Sterbaace01052014-02-05 16:17:34 +01001009 /*
1010 * Path too long
1011 */
Alexander Block31db9f72012-07-25 23:19:24 +02001012 if (name_len + data_len > buf_len) {
David Sterbaace01052014-02-05 16:17:34 +01001013 ret = -ENAMETOOLONG;
1014 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02001015 }
1016
1017 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1018 name_len + data_len);
1019
1020 len = sizeof(*di) + name_len + data_len;
1021 di = (struct btrfs_dir_item *)((char *)di + len);
1022 cur += len;
1023
1024 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1025 data_len, type, ctx);
1026 if (ret < 0)
1027 goto out;
1028 if (ret) {
1029 ret = 0;
1030 goto out;
1031 }
1032
1033 num++;
1034 }
1035
1036out:
David Sterbaace01052014-02-05 16:17:34 +01001037 kfree(buf);
Alexander Block31db9f72012-07-25 23:19:24 +02001038 return ret;
1039}
1040
1041static int __copy_first_ref(int num, u64 dir, int index,
1042 struct fs_path *p, void *ctx)
1043{
1044 int ret;
1045 struct fs_path *pt = ctx;
1046
1047 ret = fs_path_copy(pt, p);
1048 if (ret < 0)
1049 return ret;
1050
1051 /* we want the first only */
1052 return 1;
1053}
1054
1055/*
1056 * Retrieve the first path of an inode. If an inode has more then one
1057 * ref/hardlink, this is ignored.
1058 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001059static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001060 u64 ino, struct fs_path *path)
1061{
1062 int ret;
1063 struct btrfs_key key, found_key;
1064 struct btrfs_path *p;
1065
1066 p = alloc_path_for_send();
1067 if (!p)
1068 return -ENOMEM;
1069
1070 fs_path_reset(path);
1071
1072 key.objectid = ino;
1073 key.type = BTRFS_INODE_REF_KEY;
1074 key.offset = 0;
1075
1076 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1077 if (ret < 0)
1078 goto out;
1079 if (ret) {
1080 ret = 1;
1081 goto out;
1082 }
1083 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1084 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001085 (found_key.type != BTRFS_INODE_REF_KEY &&
1086 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001087 ret = -ENOENT;
1088 goto out;
1089 }
1090
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001091 ret = iterate_inode_ref(root, p, &found_key, 1,
1092 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001093 if (ret < 0)
1094 goto out;
1095 ret = 0;
1096
1097out:
1098 btrfs_free_path(p);
1099 return ret;
1100}
1101
1102struct backref_ctx {
1103 struct send_ctx *sctx;
1104
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001105 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001106 /* number of total found references */
1107 u64 found;
1108
1109 /*
1110 * used for clones found in send_root. clones found behind cur_objectid
1111 * and cur_offset are not considered as allowed clones.
1112 */
1113 u64 cur_objectid;
1114 u64 cur_offset;
1115
1116 /* may be truncated in case it's the last extent in a file */
1117 u64 extent_len;
1118
1119 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001120 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001121};
1122
1123static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1124{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001125 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001126 struct clone_root *cr = (struct clone_root *)elt;
1127
1128 if (root < cr->root->objectid)
1129 return -1;
1130 if (root > cr->root->objectid)
1131 return 1;
1132 return 0;
1133}
1134
1135static int __clone_root_cmp_sort(const void *e1, const void *e2)
1136{
1137 struct clone_root *cr1 = (struct clone_root *)e1;
1138 struct clone_root *cr2 = (struct clone_root *)e2;
1139
1140 if (cr1->root->objectid < cr2->root->objectid)
1141 return -1;
1142 if (cr1->root->objectid > cr2->root->objectid)
1143 return 1;
1144 return 0;
1145}
1146
1147/*
1148 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001149 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001150 */
1151static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1152{
1153 struct backref_ctx *bctx = ctx_;
1154 struct clone_root *found;
1155 int ret;
1156 u64 i_size;
1157
1158 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001159 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001160 bctx->sctx->clone_roots_cnt,
1161 sizeof(struct clone_root),
1162 __clone_root_cmp_bsearch);
1163 if (!found)
1164 return 0;
1165
1166 if (found->root == bctx->sctx->send_root &&
1167 ino == bctx->cur_objectid &&
1168 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001169 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001170 }
1171
1172 /*
Alexander Block766702e2012-07-28 14:11:31 +02001173 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001174 * accept clones from these extents.
1175 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001176 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1177 NULL, NULL, NULL);
1178 btrfs_release_path(bctx->path);
Alexander Block31db9f72012-07-25 23:19:24 +02001179 if (ret < 0)
1180 return ret;
1181
1182 if (offset + bctx->extent_len > i_size)
1183 return 0;
1184
1185 /*
1186 * Make sure we don't consider clones from send_root that are
1187 * behind the current inode/offset.
1188 */
1189 if (found->root == bctx->sctx->send_root) {
1190 /*
1191 * TODO for the moment we don't accept clones from the inode
1192 * that is currently send. We may change this when
1193 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1194 * file.
1195 */
1196 if (ino >= bctx->cur_objectid)
1197 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001198#if 0
1199 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001200 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001201 if (offset + bctx->extent_len > bctx->cur_offset)
1202 return 0;
1203#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001204 }
1205
1206 bctx->found++;
1207 found->found_refs++;
1208 if (ino < found->ino) {
1209 found->ino = ino;
1210 found->offset = offset;
1211 } else if (found->ino == ino) {
1212 /*
1213 * same extent found more then once in the same file.
1214 */
1215 if (found->offset > offset + bctx->extent_len)
1216 found->offset = offset;
1217 }
1218
1219 return 0;
1220}
1221
1222/*
Alexander Block766702e2012-07-28 14:11:31 +02001223 * Given an inode, offset and extent item, it finds a good clone for a clone
1224 * instruction. Returns -ENOENT when none could be found. The function makes
1225 * sure that the returned clone is usable at the point where sending is at the
1226 * moment. This means, that no clones are accepted which lie behind the current
1227 * inode+offset.
1228 *
Alexander Block31db9f72012-07-25 23:19:24 +02001229 * path must point to the extent item when called.
1230 */
1231static int find_extent_clone(struct send_ctx *sctx,
1232 struct btrfs_path *path,
1233 u64 ino, u64 data_offset,
1234 u64 ino_size,
1235 struct clone_root **found)
1236{
1237 int ret;
1238 int extent_type;
1239 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001240 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001241 u64 num_bytes;
1242 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001243 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001244 struct btrfs_file_extent_item *fi;
1245 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001246 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001247 struct clone_root *cur_clone_root;
1248 struct btrfs_key found_key;
1249 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001250 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001251 u32 i;
1252
1253 tmp_path = alloc_path_for_send();
1254 if (!tmp_path)
1255 return -ENOMEM;
1256
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001257 /* We only use this path under the commit sem */
1258 tmp_path->need_commit_sem = 0;
1259
Alexander Block35075bb2012-07-28 12:44:34 +02001260 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1261 if (!backref_ctx) {
1262 ret = -ENOMEM;
1263 goto out;
1264 }
1265
Josef Bacik3f8a18c2014-03-28 17:16:01 -04001266 backref_ctx->path = tmp_path;
1267
Alexander Block31db9f72012-07-25 23:19:24 +02001268 if (data_offset >= ino_size) {
1269 /*
1270 * There may be extents that lie behind the file's size.
1271 * I at least had this in combination with snapshotting while
1272 * writing large files.
1273 */
1274 ret = 0;
1275 goto out;
1276 }
1277
1278 fi = btrfs_item_ptr(eb, path->slots[0],
1279 struct btrfs_file_extent_item);
1280 extent_type = btrfs_file_extent_type(eb, fi);
1281 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1282 ret = -ENOENT;
1283 goto out;
1284 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001285 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001286
1287 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001288 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1289 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001290 ret = -ENOENT;
1291 goto out;
1292 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001293 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001294
Josef Bacik9e351cc2014-03-13 15:42:13 -04001295 down_read(&sctx->send_root->fs_info->commit_root_sem);
Liu Bo69917e42012-09-07 20:01:28 -06001296 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1297 &found_key, &flags);
Josef Bacik9e351cc2014-03-13 15:42:13 -04001298 up_read(&sctx->send_root->fs_info->commit_root_sem);
Alexander Block31db9f72012-07-25 23:19:24 +02001299 btrfs_release_path(tmp_path);
1300
1301 if (ret < 0)
1302 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001303 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001304 ret = -EIO;
1305 goto out;
1306 }
1307
1308 /*
1309 * Setup the clone roots.
1310 */
1311 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1312 cur_clone_root = sctx->clone_roots + i;
1313 cur_clone_root->ino = (u64)-1;
1314 cur_clone_root->offset = 0;
1315 cur_clone_root->found_refs = 0;
1316 }
1317
Alexander Block35075bb2012-07-28 12:44:34 +02001318 backref_ctx->sctx = sctx;
1319 backref_ctx->found = 0;
1320 backref_ctx->cur_objectid = ino;
1321 backref_ctx->cur_offset = data_offset;
1322 backref_ctx->found_itself = 0;
1323 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001324
1325 /*
1326 * The last extent of a file may be too large due to page alignment.
1327 * We need to adjust extent_len in this case so that the checks in
1328 * __iterate_backrefs work.
1329 */
1330 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001331 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001332
1333 /*
1334 * Now collect all backrefs.
1335 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001336 if (compressed == BTRFS_COMPRESS_NONE)
1337 extent_item_pos = logical - found_key.objectid;
1338 else
1339 extent_item_pos = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001340 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1341 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001342 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001343
Alexander Block31db9f72012-07-25 23:19:24 +02001344 if (ret < 0)
1345 goto out;
1346
Alexander Block35075bb2012-07-28 12:44:34 +02001347 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001348 /* found a bug in backref code? */
1349 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001350 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001351 "send_root. inode=%llu, offset=%llu, "
Chris Mason74dd17f2012-08-07 16:25:13 -04001352 "disk_byte=%llu found extent=%llu\n",
1353 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001354 goto out;
1355 }
1356
1357verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1358 "ino=%llu, "
1359 "num_bytes=%llu, logical=%llu\n",
1360 data_offset, ino, num_bytes, logical);
1361
Alexander Block35075bb2012-07-28 12:44:34 +02001362 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001363 verbose_printk("btrfs: no clones found\n");
1364
1365 cur_clone_root = NULL;
1366 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1367 if (sctx->clone_roots[i].found_refs) {
1368 if (!cur_clone_root)
1369 cur_clone_root = sctx->clone_roots + i;
1370 else if (sctx->clone_roots[i].root == sctx->send_root)
1371 /* prefer clones from send_root over others */
1372 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001373 }
1374
1375 }
1376
1377 if (cur_clone_root) {
Filipe David Borba Manana93de4ba2014-02-15 15:53:16 +00001378 if (compressed != BTRFS_COMPRESS_NONE) {
1379 /*
1380 * Offsets given by iterate_extent_inodes() are relative
1381 * to the start of the extent, we need to add logical
1382 * offset from the file extent item.
1383 * (See why at backref.c:check_extent_in_eb())
1384 */
1385 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1386 fi);
1387 }
Alexander Block31db9f72012-07-25 23:19:24 +02001388 *found = cur_clone_root;
1389 ret = 0;
1390 } else {
1391 ret = -ENOENT;
1392 }
1393
1394out:
1395 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001396 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001397 return ret;
1398}
1399
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001400static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001401 u64 ino,
1402 struct fs_path *dest)
1403{
1404 int ret;
1405 struct btrfs_path *path;
1406 struct btrfs_key key;
1407 struct btrfs_file_extent_item *ei;
1408 u8 type;
1409 u8 compression;
1410 unsigned long off;
1411 int len;
1412
1413 path = alloc_path_for_send();
1414 if (!path)
1415 return -ENOMEM;
1416
1417 key.objectid = ino;
1418 key.type = BTRFS_EXTENT_DATA_KEY;
1419 key.offset = 0;
1420 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1421 if (ret < 0)
1422 goto out;
1423 BUG_ON(ret);
1424
1425 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1426 struct btrfs_file_extent_item);
1427 type = btrfs_file_extent_type(path->nodes[0], ei);
1428 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1429 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1430 BUG_ON(compression);
1431
1432 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001433 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001434
1435 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001436
1437out:
1438 btrfs_free_path(path);
1439 return ret;
1440}
1441
1442/*
1443 * Helper function to generate a file name that is unique in the root of
1444 * send_root and parent_root. This is used to generate names for orphan inodes.
1445 */
1446static int gen_unique_name(struct send_ctx *sctx,
1447 u64 ino, u64 gen,
1448 struct fs_path *dest)
1449{
1450 int ret = 0;
1451 struct btrfs_path *path;
1452 struct btrfs_dir_item *di;
1453 char tmp[64];
1454 int len;
1455 u64 idx = 0;
1456
1457 path = alloc_path_for_send();
1458 if (!path)
1459 return -ENOMEM;
1460
1461 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001462 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001463 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001464 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001465
1466 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1467 path, BTRFS_FIRST_FREE_OBJECTID,
1468 tmp, strlen(tmp), 0);
1469 btrfs_release_path(path);
1470 if (IS_ERR(di)) {
1471 ret = PTR_ERR(di);
1472 goto out;
1473 }
1474 if (di) {
1475 /* not unique, try again */
1476 idx++;
1477 continue;
1478 }
1479
1480 if (!sctx->parent_root) {
1481 /* unique */
1482 ret = 0;
1483 break;
1484 }
1485
1486 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1487 path, BTRFS_FIRST_FREE_OBJECTID,
1488 tmp, strlen(tmp), 0);
1489 btrfs_release_path(path);
1490 if (IS_ERR(di)) {
1491 ret = PTR_ERR(di);
1492 goto out;
1493 }
1494 if (di) {
1495 /* not unique, try again */
1496 idx++;
1497 continue;
1498 }
1499 /* unique */
1500 break;
1501 }
1502
1503 ret = fs_path_add(dest, tmp, strlen(tmp));
1504
1505out:
1506 btrfs_free_path(path);
1507 return ret;
1508}
1509
1510enum inode_state {
1511 inode_state_no_change,
1512 inode_state_will_create,
1513 inode_state_did_create,
1514 inode_state_will_delete,
1515 inode_state_did_delete,
1516};
1517
1518static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1519{
1520 int ret;
1521 int left_ret;
1522 int right_ret;
1523 u64 left_gen;
1524 u64 right_gen;
1525
1526 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001527 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001528 if (ret < 0 && ret != -ENOENT)
1529 goto out;
1530 left_ret = ret;
1531
1532 if (!sctx->parent_root) {
1533 right_ret = -ENOENT;
1534 } else {
1535 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001536 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001537 if (ret < 0 && ret != -ENOENT)
1538 goto out;
1539 right_ret = ret;
1540 }
1541
1542 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001543 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001544 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001545 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001546 if (ino < sctx->send_progress)
1547 ret = inode_state_did_create;
1548 else
1549 ret = inode_state_will_create;
1550 } else if (right_gen == gen) {
1551 if (ino < sctx->send_progress)
1552 ret = inode_state_did_delete;
1553 else
1554 ret = inode_state_will_delete;
1555 } else {
1556 ret = -ENOENT;
1557 }
1558 } else if (!left_ret) {
1559 if (left_gen == gen) {
1560 if (ino < sctx->send_progress)
1561 ret = inode_state_did_create;
1562 else
1563 ret = inode_state_will_create;
1564 } else {
1565 ret = -ENOENT;
1566 }
1567 } else if (!right_ret) {
1568 if (right_gen == gen) {
1569 if (ino < sctx->send_progress)
1570 ret = inode_state_did_delete;
1571 else
1572 ret = inode_state_will_delete;
1573 } else {
1574 ret = -ENOENT;
1575 }
1576 } else {
1577 ret = -ENOENT;
1578 }
1579
1580out:
1581 return ret;
1582}
1583
1584static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1585{
1586 int ret;
1587
1588 ret = get_cur_inode_state(sctx, ino, gen);
1589 if (ret < 0)
1590 goto out;
1591
1592 if (ret == inode_state_no_change ||
1593 ret == inode_state_did_create ||
1594 ret == inode_state_will_delete)
1595 ret = 1;
1596 else
1597 ret = 0;
1598
1599out:
1600 return ret;
1601}
1602
1603/*
1604 * Helper function to lookup a dir item in a dir.
1605 */
1606static int lookup_dir_item_inode(struct btrfs_root *root,
1607 u64 dir, const char *name, int name_len,
1608 u64 *found_inode,
1609 u8 *found_type)
1610{
1611 int ret = 0;
1612 struct btrfs_dir_item *di;
1613 struct btrfs_key key;
1614 struct btrfs_path *path;
1615
1616 path = alloc_path_for_send();
1617 if (!path)
1618 return -ENOMEM;
1619
1620 di = btrfs_lookup_dir_item(NULL, root, path,
1621 dir, name, name_len, 0);
1622 if (!di) {
1623 ret = -ENOENT;
1624 goto out;
1625 }
1626 if (IS_ERR(di)) {
1627 ret = PTR_ERR(di);
1628 goto out;
1629 }
1630 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1631 *found_inode = key.objectid;
1632 *found_type = btrfs_dir_type(path->nodes[0], di);
1633
1634out:
1635 btrfs_free_path(path);
1636 return ret;
1637}
1638
Alexander Block766702e2012-07-28 14:11:31 +02001639/*
1640 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1641 * generation of the parent dir and the name of the dir entry.
1642 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001643static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001644 u64 *dir, u64 *dir_gen, struct fs_path *name)
1645{
1646 int ret;
1647 struct btrfs_key key;
1648 struct btrfs_key found_key;
1649 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001650 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001651 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001652
1653 path = alloc_path_for_send();
1654 if (!path)
1655 return -ENOMEM;
1656
1657 key.objectid = ino;
1658 key.type = BTRFS_INODE_REF_KEY;
1659 key.offset = 0;
1660
1661 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1662 if (ret < 0)
1663 goto out;
1664 if (!ret)
1665 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1666 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001667 if (ret || found_key.objectid != ino ||
1668 (found_key.type != BTRFS_INODE_REF_KEY &&
1669 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001670 ret = -ENOENT;
1671 goto out;
1672 }
1673
Filipe Manana51a60252014-05-13 22:01:02 +01001674 if (found_key.type == BTRFS_INODE_REF_KEY) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001675 struct btrfs_inode_ref *iref;
1676 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1677 struct btrfs_inode_ref);
1678 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1679 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1680 (unsigned long)(iref + 1),
1681 len);
1682 parent_dir = found_key.offset;
1683 } else {
1684 struct btrfs_inode_extref *extref;
1685 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1686 struct btrfs_inode_extref);
1687 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1688 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1689 (unsigned long)&extref->name, len);
1690 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1691 }
Alexander Block31db9f72012-07-25 23:19:24 +02001692 if (ret < 0)
1693 goto out;
1694 btrfs_release_path(path);
1695
Filipe Mananab46ab972014-03-21 12:46:54 +00001696 if (dir_gen) {
1697 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1698 NULL, NULL, NULL);
1699 if (ret < 0)
1700 goto out;
1701 }
Alexander Block31db9f72012-07-25 23:19:24 +02001702
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001703 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001704
1705out:
1706 btrfs_free_path(path);
1707 return ret;
1708}
1709
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001710static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001711 u64 ino, u64 dir,
1712 const char *name, int name_len)
1713{
1714 int ret;
1715 struct fs_path *tmp_name;
1716 u64 tmp_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001717
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001718 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001719 if (!tmp_name)
1720 return -ENOMEM;
1721
Filipe Mananab46ab972014-03-21 12:46:54 +00001722 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001723 if (ret < 0)
1724 goto out;
1725
Alexander Blockb9291af2012-07-28 11:07:18 +02001726 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001727 ret = 0;
1728 goto out;
1729 }
1730
Alexander Blocke938c8a2012-07-28 16:33:49 +02001731 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001732
1733out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001734 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001735 return ret;
1736}
1737
Alexander Block766702e2012-07-28 14:11:31 +02001738/*
1739 * Used by process_recorded_refs to determine if a new ref would overwrite an
1740 * already existing ref. In case it detects an overwrite, it returns the
1741 * inode/gen in who_ino/who_gen.
1742 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1743 * to make sure later references to the overwritten inode are possible.
1744 * Orphanizing is however only required for the first ref of an inode.
1745 * process_recorded_refs does an additional is_first_ref check to see if
1746 * orphanizing is really required.
1747 */
Alexander Block31db9f72012-07-25 23:19:24 +02001748static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1749 const char *name, int name_len,
1750 u64 *who_ino, u64 *who_gen)
1751{
1752 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001753 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001754 u64 other_inode = 0;
1755 u8 other_type = 0;
1756
1757 if (!sctx->parent_root)
1758 goto out;
1759
1760 ret = is_inode_existent(sctx, dir, dir_gen);
1761 if (ret <= 0)
1762 goto out;
1763
Josef Bacikebdad912013-08-06 16:47:48 -04001764 /*
1765 * If we have a parent root we need to verify that the parent dir was
1766 * not delted and then re-created, if it was then we have no overwrite
1767 * and we can just unlink this entry.
1768 */
1769 if (sctx->parent_root) {
1770 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1771 NULL, NULL, NULL);
1772 if (ret < 0 && ret != -ENOENT)
1773 goto out;
1774 if (ret) {
1775 ret = 0;
1776 goto out;
1777 }
1778 if (gen != dir_gen)
1779 goto out;
1780 }
1781
Alexander Block31db9f72012-07-25 23:19:24 +02001782 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1783 &other_inode, &other_type);
1784 if (ret < 0 && ret != -ENOENT)
1785 goto out;
1786 if (ret) {
1787 ret = 0;
1788 goto out;
1789 }
1790
Alexander Block766702e2012-07-28 14:11:31 +02001791 /*
1792 * Check if the overwritten ref was already processed. If yes, the ref
1793 * was already unlinked/moved, so we can safely assume that we will not
1794 * overwrite anything at this point in time.
1795 */
Alexander Block31db9f72012-07-25 23:19:24 +02001796 if (other_inode > sctx->send_progress) {
1797 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001798 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001799 if (ret < 0)
1800 goto out;
1801
1802 ret = 1;
1803 *who_ino = other_inode;
1804 } else {
1805 ret = 0;
1806 }
1807
1808out:
1809 return ret;
1810}
1811
Alexander Block766702e2012-07-28 14:11:31 +02001812/*
1813 * Checks if the ref was overwritten by an already processed inode. This is
1814 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1815 * thus the orphan name needs be used.
1816 * process_recorded_refs also uses it to avoid unlinking of refs that were
1817 * overwritten.
1818 */
Alexander Block31db9f72012-07-25 23:19:24 +02001819static int did_overwrite_ref(struct send_ctx *sctx,
1820 u64 dir, u64 dir_gen,
1821 u64 ino, u64 ino_gen,
1822 const char *name, int name_len)
1823{
1824 int ret = 0;
1825 u64 gen;
1826 u64 ow_inode;
1827 u8 other_type;
1828
1829 if (!sctx->parent_root)
1830 goto out;
1831
1832 ret = is_inode_existent(sctx, dir, dir_gen);
1833 if (ret <= 0)
1834 goto out;
1835
1836 /* check if the ref was overwritten by another ref */
1837 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1838 &ow_inode, &other_type);
1839 if (ret < 0 && ret != -ENOENT)
1840 goto out;
1841 if (ret) {
1842 /* was never and will never be overwritten */
1843 ret = 0;
1844 goto out;
1845 }
1846
1847 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001848 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001849 if (ret < 0)
1850 goto out;
1851
1852 if (ow_inode == ino && gen == ino_gen) {
1853 ret = 0;
1854 goto out;
1855 }
1856
1857 /* we know that it is or will be overwritten. check this now */
1858 if (ow_inode < sctx->send_progress)
1859 ret = 1;
1860 else
1861 ret = 0;
1862
1863out:
1864 return ret;
1865}
1866
Alexander Block766702e2012-07-28 14:11:31 +02001867/*
1868 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1869 * that got overwritten. This is used by process_recorded_refs to determine
1870 * if it has to use the path as returned by get_cur_path or the orphan name.
1871 */
Alexander Block31db9f72012-07-25 23:19:24 +02001872static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1873{
1874 int ret = 0;
1875 struct fs_path *name = NULL;
1876 u64 dir;
1877 u64 dir_gen;
1878
1879 if (!sctx->parent_root)
1880 goto out;
1881
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001882 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001883 if (!name)
1884 return -ENOMEM;
1885
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001886 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001887 if (ret < 0)
1888 goto out;
1889
1890 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1891 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001892
1893out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001894 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001895 return ret;
1896}
1897
Alexander Block766702e2012-07-28 14:11:31 +02001898/*
1899 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1900 * so we need to do some special handling in case we have clashes. This function
1901 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001902 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001903 */
Alexander Block31db9f72012-07-25 23:19:24 +02001904static int name_cache_insert(struct send_ctx *sctx,
1905 struct name_cache_entry *nce)
1906{
1907 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001908 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001909
Alexander Block7e0926f2012-07-28 14:20:58 +02001910 nce_head = radix_tree_lookup(&sctx->name_cache,
1911 (unsigned long)nce->ino);
1912 if (!nce_head) {
1913 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001914 if (!nce_head) {
1915 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001916 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001917 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001918 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001919
Alexander Block7e0926f2012-07-28 14:20:58 +02001920 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001921 if (ret < 0) {
1922 kfree(nce_head);
1923 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001924 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001925 }
Alexander Block31db9f72012-07-25 23:19:24 +02001926 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001927 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001928 list_add_tail(&nce->list, &sctx->name_cache_list);
1929 sctx->name_cache_size++;
1930
1931 return ret;
1932}
1933
1934static void name_cache_delete(struct send_ctx *sctx,
1935 struct name_cache_entry *nce)
1936{
Alexander Block7e0926f2012-07-28 14:20:58 +02001937 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001938
Alexander Block7e0926f2012-07-28 14:20:58 +02001939 nce_head = radix_tree_lookup(&sctx->name_cache,
1940 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01001941 if (!nce_head) {
1942 btrfs_err(sctx->send_root->fs_info,
1943 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1944 nce->ino, sctx->name_cache_size);
1945 }
Alexander Block31db9f72012-07-25 23:19:24 +02001946
Alexander Block7e0926f2012-07-28 14:20:58 +02001947 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001948 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001949 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001950
David Sterba57fb8912014-02-03 19:24:40 +01001951 /*
1952 * We may not get to the final release of nce_head if the lookup fails
1953 */
1954 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02001955 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1956 kfree(nce_head);
1957 }
Alexander Block31db9f72012-07-25 23:19:24 +02001958}
1959
1960static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1961 u64 ino, u64 gen)
1962{
Alexander Block7e0926f2012-07-28 14:20:58 +02001963 struct list_head *nce_head;
1964 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001965
Alexander Block7e0926f2012-07-28 14:20:58 +02001966 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1967 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001968 return NULL;
1969
Alexander Block7e0926f2012-07-28 14:20:58 +02001970 list_for_each_entry(cur, nce_head, radix_list) {
1971 if (cur->ino == ino && cur->gen == gen)
1972 return cur;
1973 }
Alexander Block31db9f72012-07-25 23:19:24 +02001974 return NULL;
1975}
1976
Alexander Block766702e2012-07-28 14:11:31 +02001977/*
1978 * Removes the entry from the list and adds it back to the end. This marks the
1979 * entry as recently used so that name_cache_clean_unused does not remove it.
1980 */
Alexander Block31db9f72012-07-25 23:19:24 +02001981static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1982{
1983 list_del(&nce->list);
1984 list_add_tail(&nce->list, &sctx->name_cache_list);
1985}
1986
Alexander Block766702e2012-07-28 14:11:31 +02001987/*
1988 * Remove some entries from the beginning of name_cache_list.
1989 */
Alexander Block31db9f72012-07-25 23:19:24 +02001990static void name_cache_clean_unused(struct send_ctx *sctx)
1991{
1992 struct name_cache_entry *nce;
1993
1994 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1995 return;
1996
1997 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1998 nce = list_entry(sctx->name_cache_list.next,
1999 struct name_cache_entry, list);
2000 name_cache_delete(sctx, nce);
2001 kfree(nce);
2002 }
2003}
2004
2005static void name_cache_free(struct send_ctx *sctx)
2006{
2007 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02002008
Alexander Blocke938c8a2012-07-28 16:33:49 +02002009 while (!list_empty(&sctx->name_cache_list)) {
2010 nce = list_entry(sctx->name_cache_list.next,
2011 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002012 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02002013 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02002014 }
2015}
2016
Alexander Block766702e2012-07-28 14:11:31 +02002017/*
2018 * Used by get_cur_path for each ref up to the root.
2019 * Returns 0 if it succeeded.
2020 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2021 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2022 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2023 * Returns <0 in case of error.
2024 */
Alexander Block31db9f72012-07-25 23:19:24 +02002025static int __get_cur_name_and_parent(struct send_ctx *sctx,
2026 u64 ino, u64 gen,
2027 u64 *parent_ino,
2028 u64 *parent_gen,
2029 struct fs_path *dest)
2030{
2031 int ret;
2032 int nce_ret;
Alexander Block31db9f72012-07-25 23:19:24 +02002033 struct name_cache_entry *nce = NULL;
2034
Alexander Block766702e2012-07-28 14:11:31 +02002035 /*
2036 * First check if we already did a call to this function with the same
2037 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2038 * return the cached result.
2039 */
Alexander Block31db9f72012-07-25 23:19:24 +02002040 nce = name_cache_search(sctx, ino, gen);
2041 if (nce) {
2042 if (ino < sctx->send_progress && nce->need_later_update) {
2043 name_cache_delete(sctx, nce);
2044 kfree(nce);
2045 nce = NULL;
2046 } else {
2047 name_cache_used(sctx, nce);
2048 *parent_ino = nce->parent_ino;
2049 *parent_gen = nce->parent_gen;
2050 ret = fs_path_add(dest, nce->name, nce->name_len);
2051 if (ret < 0)
2052 goto out;
2053 ret = nce->ret;
2054 goto out;
2055 }
2056 }
2057
Alexander Block766702e2012-07-28 14:11:31 +02002058 /*
2059 * If the inode is not existent yet, add the orphan name and return 1.
2060 * This should only happen for the parent dir that we determine in
2061 * __record_new_ref
2062 */
Alexander Block31db9f72012-07-25 23:19:24 +02002063 ret = is_inode_existent(sctx, ino, gen);
2064 if (ret < 0)
2065 goto out;
2066
2067 if (!ret) {
2068 ret = gen_unique_name(sctx, ino, gen, dest);
2069 if (ret < 0)
2070 goto out;
2071 ret = 1;
2072 goto out_cache;
2073 }
2074
Alexander Block766702e2012-07-28 14:11:31 +02002075 /*
2076 * Depending on whether the inode was already processed or not, use
2077 * send_root or parent_root for ref lookup.
2078 */
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002079 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002080 ret = get_first_ref(sctx->send_root, ino,
2081 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002082 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002083 ret = get_first_ref(sctx->parent_root, ino,
2084 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002085 if (ret < 0)
2086 goto out;
2087
Alexander Block766702e2012-07-28 14:11:31 +02002088 /*
2089 * Check if the ref was overwritten by an inode's ref that was processed
2090 * earlier. If yes, treat as orphan and return 1.
2091 */
Alexander Block31db9f72012-07-25 23:19:24 +02002092 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2093 dest->start, dest->end - dest->start);
2094 if (ret < 0)
2095 goto out;
2096 if (ret) {
2097 fs_path_reset(dest);
2098 ret = gen_unique_name(sctx, ino, gen, dest);
2099 if (ret < 0)
2100 goto out;
2101 ret = 1;
2102 }
2103
2104out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002105 /*
2106 * Store the result of the lookup in the name cache.
2107 */
Alexander Block31db9f72012-07-25 23:19:24 +02002108 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2109 if (!nce) {
2110 ret = -ENOMEM;
2111 goto out;
2112 }
2113
2114 nce->ino = ino;
2115 nce->gen = gen;
2116 nce->parent_ino = *parent_ino;
2117 nce->parent_gen = *parent_gen;
2118 nce->name_len = fs_path_len(dest);
2119 nce->ret = ret;
2120 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002121
2122 if (ino < sctx->send_progress)
2123 nce->need_later_update = 0;
2124 else
2125 nce->need_later_update = 1;
2126
2127 nce_ret = name_cache_insert(sctx, nce);
2128 if (nce_ret < 0)
2129 ret = nce_ret;
2130 name_cache_clean_unused(sctx);
2131
2132out:
Alexander Block31db9f72012-07-25 23:19:24 +02002133 return ret;
2134}
2135
2136/*
2137 * Magic happens here. This function returns the first ref to an inode as it
2138 * would look like while receiving the stream at this point in time.
2139 * We walk the path up to the root. For every inode in between, we check if it
2140 * was already processed/sent. If yes, we continue with the parent as found
2141 * in send_root. If not, we continue with the parent as found in parent_root.
2142 * If we encounter an inode that was deleted at this point in time, we use the
2143 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2144 * that were not created yet and overwritten inodes/refs.
2145 *
2146 * When do we have have orphan inodes:
2147 * 1. When an inode is freshly created and thus no valid refs are available yet
2148 * 2. When a directory lost all it's refs (deleted) but still has dir items
2149 * inside which were not processed yet (pending for move/delete). If anyone
2150 * tried to get the path to the dir items, it would get a path inside that
2151 * orphan directory.
2152 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2153 * of an unprocessed inode. If in that case the first ref would be
2154 * overwritten, the overwritten inode gets "orphanized". Later when we
2155 * process this overwritten inode, it is restored at a new place by moving
2156 * the orphan inode.
2157 *
2158 * sctx->send_progress tells this function at which point in time receiving
2159 * would be.
2160 */
2161static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2162 struct fs_path *dest)
2163{
2164 int ret = 0;
2165 struct fs_path *name = NULL;
2166 u64 parent_inode = 0;
2167 u64 parent_gen = 0;
2168 int stop = 0;
2169
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002170 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002171 if (!name) {
2172 ret = -ENOMEM;
2173 goto out;
2174 }
2175
2176 dest->reversed = 1;
2177 fs_path_reset(dest);
2178
2179 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2180 fs_path_reset(name);
2181
Filipe Manana9dc44212014-02-19 14:31:44 +00002182 if (is_waiting_for_rm(sctx, ino)) {
2183 ret = gen_unique_name(sctx, ino, gen, name);
2184 if (ret < 0)
2185 goto out;
2186 ret = fs_path_add_path(dest, name);
2187 break;
2188 }
2189
Filipe Mananabf0d1f42014-02-21 00:01:32 +00002190 if (is_waiting_for_move(sctx, ino)) {
2191 ret = get_first_ref(sctx->parent_root, ino,
2192 &parent_inode, &parent_gen, name);
2193 } else {
2194 ret = __get_cur_name_and_parent(sctx, ino, gen,
2195 &parent_inode,
2196 &parent_gen, name);
2197 if (ret)
2198 stop = 1;
2199 }
2200
Alexander Block31db9f72012-07-25 23:19:24 +02002201 if (ret < 0)
2202 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002203
Alexander Block31db9f72012-07-25 23:19:24 +02002204 ret = fs_path_add_path(dest, name);
2205 if (ret < 0)
2206 goto out;
2207
2208 ino = parent_inode;
2209 gen = parent_gen;
2210 }
2211
2212out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002213 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002214 if (!ret)
2215 fs_path_unreverse(dest);
2216 return ret;
2217}
2218
2219/*
Alexander Block31db9f72012-07-25 23:19:24 +02002220 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2221 */
2222static int send_subvol_begin(struct send_ctx *sctx)
2223{
2224 int ret;
2225 struct btrfs_root *send_root = sctx->send_root;
2226 struct btrfs_root *parent_root = sctx->parent_root;
2227 struct btrfs_path *path;
2228 struct btrfs_key key;
2229 struct btrfs_root_ref *ref;
2230 struct extent_buffer *leaf;
2231 char *name = NULL;
2232 int namelen;
2233
Wang Shilongffcfaf82014-01-15 00:26:43 +08002234 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002235 if (!path)
2236 return -ENOMEM;
2237
2238 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2239 if (!name) {
2240 btrfs_free_path(path);
2241 return -ENOMEM;
2242 }
2243
2244 key.objectid = send_root->objectid;
2245 key.type = BTRFS_ROOT_BACKREF_KEY;
2246 key.offset = 0;
2247
2248 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2249 &key, path, 1, 0);
2250 if (ret < 0)
2251 goto out;
2252 if (ret) {
2253 ret = -ENOENT;
2254 goto out;
2255 }
2256
2257 leaf = path->nodes[0];
2258 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2259 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2260 key.objectid != send_root->objectid) {
2261 ret = -ENOENT;
2262 goto out;
2263 }
2264 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2265 namelen = btrfs_root_ref_name_len(leaf, ref);
2266 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2267 btrfs_release_path(path);
2268
Alexander Block31db9f72012-07-25 23:19:24 +02002269 if (parent_root) {
2270 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2271 if (ret < 0)
2272 goto out;
2273 } else {
2274 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2275 if (ret < 0)
2276 goto out;
2277 }
2278
2279 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2280 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2281 sctx->send_root->root_item.uuid);
2282 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002283 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002284 if (parent_root) {
2285 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2286 sctx->parent_root->root_item.uuid);
2287 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002288 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002289 }
2290
2291 ret = send_cmd(sctx);
2292
2293tlv_put_failure:
2294out:
2295 btrfs_free_path(path);
2296 kfree(name);
2297 return ret;
2298}
2299
2300static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2301{
2302 int ret = 0;
2303 struct fs_path *p;
2304
2305verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2306
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002307 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002308 if (!p)
2309 return -ENOMEM;
2310
2311 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2312 if (ret < 0)
2313 goto out;
2314
2315 ret = get_cur_path(sctx, ino, gen, p);
2316 if (ret < 0)
2317 goto out;
2318 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2319 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2320
2321 ret = send_cmd(sctx);
2322
2323tlv_put_failure:
2324out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002325 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002326 return ret;
2327}
2328
2329static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2330{
2331 int ret = 0;
2332 struct fs_path *p;
2333
2334verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2335
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002336 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002337 if (!p)
2338 return -ENOMEM;
2339
2340 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2341 if (ret < 0)
2342 goto out;
2343
2344 ret = get_cur_path(sctx, ino, gen, p);
2345 if (ret < 0)
2346 goto out;
2347 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2348 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2349
2350 ret = send_cmd(sctx);
2351
2352tlv_put_failure:
2353out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002354 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002355 return ret;
2356}
2357
2358static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2359{
2360 int ret = 0;
2361 struct fs_path *p;
2362
2363verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2364
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002365 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002366 if (!p)
2367 return -ENOMEM;
2368
2369 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2370 if (ret < 0)
2371 goto out;
2372
2373 ret = get_cur_path(sctx, ino, gen, p);
2374 if (ret < 0)
2375 goto out;
2376 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2377 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2378 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2379
2380 ret = send_cmd(sctx);
2381
2382tlv_put_failure:
2383out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002384 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002385 return ret;
2386}
2387
2388static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2389{
2390 int ret = 0;
2391 struct fs_path *p = NULL;
2392 struct btrfs_inode_item *ii;
2393 struct btrfs_path *path = NULL;
2394 struct extent_buffer *eb;
2395 struct btrfs_key key;
2396 int slot;
2397
2398verbose_printk("btrfs: send_utimes %llu\n", ino);
2399
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002400 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002401 if (!p)
2402 return -ENOMEM;
2403
2404 path = alloc_path_for_send();
2405 if (!path) {
2406 ret = -ENOMEM;
2407 goto out;
2408 }
2409
2410 key.objectid = ino;
2411 key.type = BTRFS_INODE_ITEM_KEY;
2412 key.offset = 0;
2413 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2414 if (ret < 0)
2415 goto out;
2416
2417 eb = path->nodes[0];
2418 slot = path->slots[0];
2419 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2420
2421 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2422 if (ret < 0)
2423 goto out;
2424
2425 ret = get_cur_path(sctx, ino, gen, p);
2426 if (ret < 0)
2427 goto out;
2428 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2429 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2430 btrfs_inode_atime(ii));
2431 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2432 btrfs_inode_mtime(ii));
2433 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2434 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002435 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002436
2437 ret = send_cmd(sctx);
2438
2439tlv_put_failure:
2440out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002441 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002442 btrfs_free_path(path);
2443 return ret;
2444}
2445
2446/*
2447 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2448 * a valid path yet because we did not process the refs yet. So, the inode
2449 * is created as orphan.
2450 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002451static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002452{
2453 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002454 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002455 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002456 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002457 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002458 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002459
Alexander Block1f4692d2012-07-28 10:42:24 +02002460verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002461
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002462 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002463 if (!p)
2464 return -ENOMEM;
2465
Liu Bo644d1942014-02-27 17:29:01 +08002466 if (ino != sctx->cur_ino) {
2467 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2468 NULL, NULL, &rdev);
2469 if (ret < 0)
2470 goto out;
2471 } else {
2472 gen = sctx->cur_inode_gen;
2473 mode = sctx->cur_inode_mode;
2474 rdev = sctx->cur_inode_rdev;
2475 }
Alexander Block31db9f72012-07-25 23:19:24 +02002476
Alexander Blocke938c8a2012-07-28 16:33:49 +02002477 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002478 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002479 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002480 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002481 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002482 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002483 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002484 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002485 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002486 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002487 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002488 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002489 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002490 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2491 (int)(mode & S_IFMT));
2492 ret = -ENOTSUPP;
2493 goto out;
2494 }
2495
2496 ret = begin_cmd(sctx, cmd);
2497 if (ret < 0)
2498 goto out;
2499
Alexander Block1f4692d2012-07-28 10:42:24 +02002500 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002501 if (ret < 0)
2502 goto out;
2503
2504 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002505 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002506
2507 if (S_ISLNK(mode)) {
2508 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002509 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002510 if (ret < 0)
2511 goto out;
2512 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2513 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2514 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002515 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2516 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002517 }
2518
2519 ret = send_cmd(sctx);
2520 if (ret < 0)
2521 goto out;
2522
2523
2524tlv_put_failure:
2525out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002526 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002527 return ret;
2528}
2529
Alexander Block1f4692d2012-07-28 10:42:24 +02002530/*
2531 * We need some special handling for inodes that get processed before the parent
2532 * directory got created. See process_recorded_refs for details.
2533 * This function does the check if we already created the dir out of order.
2534 */
2535static int did_create_dir(struct send_ctx *sctx, u64 dir)
2536{
2537 int ret = 0;
2538 struct btrfs_path *path = NULL;
2539 struct btrfs_key key;
2540 struct btrfs_key found_key;
2541 struct btrfs_key di_key;
2542 struct extent_buffer *eb;
2543 struct btrfs_dir_item *di;
2544 int slot;
2545
2546 path = alloc_path_for_send();
2547 if (!path) {
2548 ret = -ENOMEM;
2549 goto out;
2550 }
2551
2552 key.objectid = dir;
2553 key.type = BTRFS_DIR_INDEX_KEY;
2554 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002555 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2556 if (ret < 0)
2557 goto out;
2558
Alexander Block1f4692d2012-07-28 10:42:24 +02002559 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002560 eb = path->nodes[0];
2561 slot = path->slots[0];
2562 if (slot >= btrfs_header_nritems(eb)) {
2563 ret = btrfs_next_leaf(sctx->send_root, path);
2564 if (ret < 0) {
2565 goto out;
2566 } else if (ret > 0) {
2567 ret = 0;
2568 break;
2569 }
2570 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002571 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002572
2573 btrfs_item_key_to_cpu(eb, &found_key, slot);
2574 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002575 found_key.type != key.type) {
2576 ret = 0;
2577 goto out;
2578 }
2579
2580 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2581 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2582
Josef Bacika0525412013-08-12 10:56:14 -04002583 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2584 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002585 ret = 1;
2586 goto out;
2587 }
2588
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002589 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002590 }
2591
2592out:
2593 btrfs_free_path(path);
2594 return ret;
2595}
2596
2597/*
2598 * Only creates the inode if it is:
2599 * 1. Not a directory
2600 * 2. Or a directory which was not created already due to out of order
2601 * directories. See did_create_dir and process_recorded_refs for details.
2602 */
2603static int send_create_inode_if_needed(struct send_ctx *sctx)
2604{
2605 int ret;
2606
2607 if (S_ISDIR(sctx->cur_inode_mode)) {
2608 ret = did_create_dir(sctx, sctx->cur_ino);
2609 if (ret < 0)
2610 goto out;
2611 if (ret) {
2612 ret = 0;
2613 goto out;
2614 }
2615 }
2616
2617 ret = send_create_inode(sctx, sctx->cur_ino);
2618 if (ret < 0)
2619 goto out;
2620
2621out:
2622 return ret;
2623}
2624
Alexander Block31db9f72012-07-25 23:19:24 +02002625struct recorded_ref {
2626 struct list_head list;
2627 char *dir_path;
2628 char *name;
2629 struct fs_path *full_path;
2630 u64 dir;
2631 u64 dir_gen;
2632 int dir_path_len;
2633 int name_len;
2634};
2635
2636/*
2637 * We need to process new refs before deleted refs, but compare_tree gives us
2638 * everything mixed. So we first record all refs and later process them.
2639 * This function is a helper to record one ref.
2640 */
Liu Boa4d96d62014-03-03 21:31:03 +08002641static int __record_ref(struct list_head *head, u64 dir,
Alexander Block31db9f72012-07-25 23:19:24 +02002642 u64 dir_gen, struct fs_path *path)
2643{
2644 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002645
2646 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2647 if (!ref)
2648 return -ENOMEM;
2649
2650 ref->dir = dir;
2651 ref->dir_gen = dir_gen;
2652 ref->full_path = path;
2653
Andy Shevchenkoed848852013-08-21 10:32:13 +03002654 ref->name = (char *)kbasename(ref->full_path->start);
2655 ref->name_len = ref->full_path->end - ref->name;
2656 ref->dir_path = ref->full_path->start;
2657 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002658 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002659 else
Alexander Block31db9f72012-07-25 23:19:24 +02002660 ref->dir_path_len = ref->full_path->end -
2661 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002662
2663 list_add_tail(&ref->list, head);
2664 return 0;
2665}
2666
Josef Bacikba5e8f22013-08-16 16:52:55 -04002667static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2668{
2669 struct recorded_ref *new;
2670
2671 new = kmalloc(sizeof(*ref), GFP_NOFS);
2672 if (!new)
2673 return -ENOMEM;
2674
2675 new->dir = ref->dir;
2676 new->dir_gen = ref->dir_gen;
2677 new->full_path = NULL;
2678 INIT_LIST_HEAD(&new->list);
2679 list_add_tail(&new->list, list);
2680 return 0;
2681}
2682
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002683static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002684{
2685 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002686
Alexander Blocke938c8a2012-07-28 16:33:49 +02002687 while (!list_empty(head)) {
2688 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002689 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002690 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002691 kfree(cur);
2692 }
Alexander Block31db9f72012-07-25 23:19:24 +02002693}
2694
2695static void free_recorded_refs(struct send_ctx *sctx)
2696{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002697 __free_recorded_refs(&sctx->new_refs);
2698 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002699}
2700
2701/*
Alexander Block766702e2012-07-28 14:11:31 +02002702 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002703 * ref of an unprocessed inode gets overwritten and for all non empty
2704 * directories.
2705 */
2706static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2707 struct fs_path *path)
2708{
2709 int ret;
2710 struct fs_path *orphan;
2711
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002712 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002713 if (!orphan)
2714 return -ENOMEM;
2715
2716 ret = gen_unique_name(sctx, ino, gen, orphan);
2717 if (ret < 0)
2718 goto out;
2719
2720 ret = send_rename(sctx, path, orphan);
2721
2722out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002723 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002724 return ret;
2725}
2726
Filipe Manana9dc44212014-02-19 14:31:44 +00002727static struct orphan_dir_info *
2728add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2729{
2730 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2731 struct rb_node *parent = NULL;
2732 struct orphan_dir_info *entry, *odi;
2733
2734 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2735 if (!odi)
2736 return ERR_PTR(-ENOMEM);
2737 odi->ino = dir_ino;
2738 odi->gen = 0;
2739
2740 while (*p) {
2741 parent = *p;
2742 entry = rb_entry(parent, struct orphan_dir_info, node);
2743 if (dir_ino < entry->ino) {
2744 p = &(*p)->rb_left;
2745 } else if (dir_ino > entry->ino) {
2746 p = &(*p)->rb_right;
2747 } else {
2748 kfree(odi);
2749 return entry;
2750 }
2751 }
2752
2753 rb_link_node(&odi->node, parent, p);
2754 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2755 return odi;
2756}
2757
2758static struct orphan_dir_info *
2759get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2760{
2761 struct rb_node *n = sctx->orphan_dirs.rb_node;
2762 struct orphan_dir_info *entry;
2763
2764 while (n) {
2765 entry = rb_entry(n, struct orphan_dir_info, node);
2766 if (dir_ino < entry->ino)
2767 n = n->rb_left;
2768 else if (dir_ino > entry->ino)
2769 n = n->rb_right;
2770 else
2771 return entry;
2772 }
2773 return NULL;
2774}
2775
2776static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2777{
2778 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2779
2780 return odi != NULL;
2781}
2782
2783static void free_orphan_dir_info(struct send_ctx *sctx,
2784 struct orphan_dir_info *odi)
2785{
2786 if (!odi)
2787 return;
2788 rb_erase(&odi->node, &sctx->orphan_dirs);
2789 kfree(odi);
2790}
2791
Alexander Block31db9f72012-07-25 23:19:24 +02002792/*
2793 * Returns 1 if a directory can be removed at this point in time.
2794 * We check this by iterating all dir items and checking if the inode behind
2795 * the dir item was already processed.
2796 */
Filipe Manana9dc44212014-02-19 14:31:44 +00002797static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2798 u64 send_progress)
Alexander Block31db9f72012-07-25 23:19:24 +02002799{
2800 int ret = 0;
2801 struct btrfs_root *root = sctx->parent_root;
2802 struct btrfs_path *path;
2803 struct btrfs_key key;
2804 struct btrfs_key found_key;
2805 struct btrfs_key loc;
2806 struct btrfs_dir_item *di;
2807
Alexander Block6d85ed02012-08-01 14:48:59 +02002808 /*
2809 * Don't try to rmdir the top/root subvolume dir.
2810 */
2811 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2812 return 0;
2813
Alexander Block31db9f72012-07-25 23:19:24 +02002814 path = alloc_path_for_send();
2815 if (!path)
2816 return -ENOMEM;
2817
2818 key.objectid = dir;
2819 key.type = BTRFS_DIR_INDEX_KEY;
2820 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002821 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2822 if (ret < 0)
2823 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002824
2825 while (1) {
Filipe Manana9dc44212014-02-19 14:31:44 +00002826 struct waiting_dir_move *dm;
2827
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002828 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2829 ret = btrfs_next_leaf(root, path);
2830 if (ret < 0)
2831 goto out;
2832 else if (ret > 0)
2833 break;
2834 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002835 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002836 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2837 path->slots[0]);
2838 if (found_key.objectid != key.objectid ||
2839 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002840 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002841
2842 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2843 struct btrfs_dir_item);
2844 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2845
Filipe Manana9dc44212014-02-19 14:31:44 +00002846 dm = get_waiting_dir_move(sctx, loc.objectid);
2847 if (dm) {
2848 struct orphan_dir_info *odi;
2849
2850 odi = add_orphan_dir_info(sctx, dir);
2851 if (IS_ERR(odi)) {
2852 ret = PTR_ERR(odi);
2853 goto out;
2854 }
2855 odi->gen = dir_gen;
2856 dm->rmdir_ino = dir;
2857 ret = 0;
2858 goto out;
2859 }
2860
Alexander Block31db9f72012-07-25 23:19:24 +02002861 if (loc.objectid > send_progress) {
2862 ret = 0;
2863 goto out;
2864 }
2865
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002866 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002867 }
2868
2869 ret = 1;
2870
2871out:
2872 btrfs_free_path(path);
2873 return ret;
2874}
2875
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002876static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2877{
Filipe Manana9dc44212014-02-19 14:31:44 +00002878 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002879
Filipe Manana9dc44212014-02-19 14:31:44 +00002880 return entry != NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002881}
2882
2883static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2884{
2885 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2886 struct rb_node *parent = NULL;
2887 struct waiting_dir_move *entry, *dm;
2888
2889 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2890 if (!dm)
2891 return -ENOMEM;
2892 dm->ino = ino;
Filipe Manana9dc44212014-02-19 14:31:44 +00002893 dm->rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002894
2895 while (*p) {
2896 parent = *p;
2897 entry = rb_entry(parent, struct waiting_dir_move, node);
2898 if (ino < entry->ino) {
2899 p = &(*p)->rb_left;
2900 } else if (ino > entry->ino) {
2901 p = &(*p)->rb_right;
2902 } else {
2903 kfree(dm);
2904 return -EEXIST;
2905 }
2906 }
2907
2908 rb_link_node(&dm->node, parent, p);
2909 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2910 return 0;
2911}
2912
Filipe Manana9dc44212014-02-19 14:31:44 +00002913static struct waiting_dir_move *
2914get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002915{
2916 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2917 struct waiting_dir_move *entry;
2918
2919 while (n) {
2920 entry = rb_entry(n, struct waiting_dir_move, node);
Filipe Manana9dc44212014-02-19 14:31:44 +00002921 if (ino < entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002922 n = n->rb_left;
Filipe Manana9dc44212014-02-19 14:31:44 +00002923 else if (ino > entry->ino)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002924 n = n->rb_right;
Filipe Manana9dc44212014-02-19 14:31:44 +00002925 else
2926 return entry;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002927 }
Filipe Manana9dc44212014-02-19 14:31:44 +00002928 return NULL;
2929}
2930
2931static void free_waiting_dir_move(struct send_ctx *sctx,
2932 struct waiting_dir_move *dm)
2933{
2934 if (!dm)
2935 return;
2936 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2937 kfree(dm);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002938}
2939
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00002940static int add_pending_dir_move(struct send_ctx *sctx,
2941 u64 ino,
2942 u64 ino_gen,
Filipe Mananaf9594922014-03-27 20:14:01 +00002943 u64 parent_ino,
2944 struct list_head *new_refs,
2945 struct list_head *deleted_refs)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002946{
2947 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2948 struct rb_node *parent = NULL;
Chris Mason73b802f2014-03-21 15:30:44 -07002949 struct pending_dir_move *entry = NULL, *pm;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002950 struct recorded_ref *cur;
2951 int exists = 0;
2952 int ret;
2953
2954 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2955 if (!pm)
2956 return -ENOMEM;
2957 pm->parent_ino = parent_ino;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00002958 pm->ino = ino;
2959 pm->gen = ino_gen;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002960 INIT_LIST_HEAD(&pm->list);
2961 INIT_LIST_HEAD(&pm->update_refs);
2962 RB_CLEAR_NODE(&pm->node);
2963
2964 while (*p) {
2965 parent = *p;
2966 entry = rb_entry(parent, struct pending_dir_move, node);
2967 if (parent_ino < entry->parent_ino) {
2968 p = &(*p)->rb_left;
2969 } else if (parent_ino > entry->parent_ino) {
2970 p = &(*p)->rb_right;
2971 } else {
2972 exists = 1;
2973 break;
2974 }
2975 }
2976
Filipe Mananaf9594922014-03-27 20:14:01 +00002977 list_for_each_entry(cur, deleted_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002978 ret = dup_ref(cur, &pm->update_refs);
2979 if (ret < 0)
2980 goto out;
2981 }
Filipe Mananaf9594922014-03-27 20:14:01 +00002982 list_for_each_entry(cur, new_refs, list) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002983 ret = dup_ref(cur, &pm->update_refs);
2984 if (ret < 0)
2985 goto out;
2986 }
2987
2988 ret = add_waiting_dir_move(sctx, pm->ino);
2989 if (ret)
2990 goto out;
2991
2992 if (exists) {
2993 list_add_tail(&pm->list, &entry->list);
2994 } else {
2995 rb_link_node(&pm->node, parent, p);
2996 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
2997 }
2998 ret = 0;
2999out:
3000 if (ret) {
3001 __free_recorded_refs(&pm->update_refs);
3002 kfree(pm);
3003 }
3004 return ret;
3005}
3006
3007static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3008 u64 parent_ino)
3009{
3010 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3011 struct pending_dir_move *entry;
3012
3013 while (n) {
3014 entry = rb_entry(n, struct pending_dir_move, node);
3015 if (parent_ino < entry->parent_ino)
3016 n = n->rb_left;
3017 else if (parent_ino > entry->parent_ino)
3018 n = n->rb_right;
3019 else
3020 return entry;
3021 }
3022 return NULL;
3023}
3024
Filipe Mananaf9594922014-03-27 20:14:01 +00003025static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3026 u64 ino, u64 gen, u64 *ancestor_ino)
3027{
3028 int ret = 0;
3029 u64 parent_inode = 0;
3030 u64 parent_gen = 0;
3031 u64 start_ino = ino;
3032
3033 *ancestor_ino = 0;
3034 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3035 fs_path_reset(name);
3036
3037 if (is_waiting_for_rm(sctx, ino))
3038 break;
3039 if (is_waiting_for_move(sctx, ino)) {
3040 if (*ancestor_ino == 0)
3041 *ancestor_ino = ino;
3042 ret = get_first_ref(sctx->parent_root, ino,
3043 &parent_inode, &parent_gen, name);
3044 } else {
3045 ret = __get_cur_name_and_parent(sctx, ino, gen,
3046 &parent_inode,
3047 &parent_gen, name);
3048 if (ret > 0) {
3049 ret = 0;
3050 break;
3051 }
3052 }
3053 if (ret < 0)
3054 break;
3055 if (parent_inode == start_ino) {
3056 ret = 1;
3057 if (*ancestor_ino == 0)
3058 *ancestor_ino = ino;
3059 break;
3060 }
3061 ino = parent_inode;
3062 gen = parent_gen;
3063 }
3064 return ret;
3065}
3066
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003067static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3068{
3069 struct fs_path *from_path = NULL;
3070 struct fs_path *to_path = NULL;
Filipe Manana2b863a12014-02-16 13:43:11 +00003071 struct fs_path *name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003072 u64 orig_progress = sctx->send_progress;
3073 struct recorded_ref *cur;
Filipe Manana2b863a12014-02-16 13:43:11 +00003074 u64 parent_ino, parent_gen;
Filipe Manana9dc44212014-02-19 14:31:44 +00003075 struct waiting_dir_move *dm = NULL;
3076 u64 rmdir_ino = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003077 int ret;
Filipe Mananaf9594922014-03-27 20:14:01 +00003078 u64 ancestor = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003079
Filipe Manana2b863a12014-02-16 13:43:11 +00003080 name = fs_path_alloc();
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003081 from_path = fs_path_alloc();
Filipe Manana2b863a12014-02-16 13:43:11 +00003082 if (!name || !from_path) {
3083 ret = -ENOMEM;
3084 goto out;
3085 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003086
Filipe Manana9dc44212014-02-19 14:31:44 +00003087 dm = get_waiting_dir_move(sctx, pm->ino);
3088 ASSERT(dm);
3089 rmdir_ino = dm->rmdir_ino;
3090 free_waiting_dir_move(sctx, dm);
Filipe Manana2b863a12014-02-16 13:43:11 +00003091
3092 ret = get_first_ref(sctx->parent_root, pm->ino,
3093 &parent_ino, &parent_gen, name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003094 if (ret < 0)
3095 goto out;
3096
Filipe Mananac992ec92014-03-22 17:15:24 +00003097 ret = get_cur_path(sctx, parent_ino, parent_gen,
3098 from_path);
3099 if (ret < 0)
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003100 goto out;
Filipe Mananac992ec92014-03-22 17:15:24 +00003101 ret = fs_path_add_path(from_path, name);
3102 if (ret < 0)
3103 goto out;
3104
Filipe Mananaf9594922014-03-27 20:14:01 +00003105 sctx->send_progress = sctx->cur_ino + 1;
3106 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3107 if (ret) {
3108 LIST_HEAD(deleted_refs);
3109 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3110 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3111 &pm->update_refs, &deleted_refs);
3112 if (ret < 0)
3113 goto out;
3114 if (rmdir_ino) {
3115 dm = get_waiting_dir_move(sctx, pm->ino);
3116 ASSERT(dm);
3117 dm->rmdir_ino = rmdir_ino;
3118 }
3119 goto out;
3120 }
Filipe Mananac992ec92014-03-22 17:15:24 +00003121 fs_path_reset(name);
3122 to_path = name;
3123 name = NULL;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003124 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3125 if (ret < 0)
3126 goto out;
3127
3128 ret = send_rename(sctx, from_path, to_path);
3129 if (ret < 0)
3130 goto out;
3131
Filipe Manana9dc44212014-02-19 14:31:44 +00003132 if (rmdir_ino) {
3133 struct orphan_dir_info *odi;
3134
3135 odi = get_orphan_dir_info(sctx, rmdir_ino);
3136 if (!odi) {
3137 /* already deleted */
3138 goto finish;
3139 }
3140 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3141 if (ret < 0)
3142 goto out;
3143 if (!ret)
3144 goto finish;
3145
3146 name = fs_path_alloc();
3147 if (!name) {
3148 ret = -ENOMEM;
3149 goto out;
3150 }
3151 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3152 if (ret < 0)
3153 goto out;
3154 ret = send_rmdir(sctx, name);
3155 if (ret < 0)
3156 goto out;
3157 free_orphan_dir_info(sctx, odi);
3158 }
3159
3160finish:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003161 ret = send_utimes(sctx, pm->ino, pm->gen);
3162 if (ret < 0)
3163 goto out;
3164
3165 /*
3166 * After rename/move, need to update the utimes of both new parent(s)
3167 * and old parent(s).
3168 */
3169 list_for_each_entry(cur, &pm->update_refs, list) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003170 if (cur->dir == rmdir_ino)
3171 continue;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003172 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3173 if (ret < 0)
3174 goto out;
3175 }
3176
3177out:
Filipe Manana2b863a12014-02-16 13:43:11 +00003178 fs_path_free(name);
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003179 fs_path_free(from_path);
3180 fs_path_free(to_path);
3181 sctx->send_progress = orig_progress;
3182
3183 return ret;
3184}
3185
3186static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3187{
3188 if (!list_empty(&m->list))
3189 list_del(&m->list);
3190 if (!RB_EMPTY_NODE(&m->node))
3191 rb_erase(&m->node, &sctx->pending_dir_moves);
3192 __free_recorded_refs(&m->update_refs);
3193 kfree(m);
3194}
3195
3196static void tail_append_pending_moves(struct pending_dir_move *moves,
3197 struct list_head *stack)
3198{
3199 if (list_empty(&moves->list)) {
3200 list_add_tail(&moves->list, stack);
3201 } else {
3202 LIST_HEAD(list);
3203 list_splice_init(&moves->list, &list);
3204 list_add_tail(&moves->list, stack);
3205 list_splice_tail(&list, stack);
3206 }
3207}
3208
3209static int apply_children_dir_moves(struct send_ctx *sctx)
3210{
3211 struct pending_dir_move *pm;
3212 struct list_head stack;
3213 u64 parent_ino = sctx->cur_ino;
3214 int ret = 0;
3215
3216 pm = get_pending_dir_moves(sctx, parent_ino);
3217 if (!pm)
3218 return 0;
3219
3220 INIT_LIST_HEAD(&stack);
3221 tail_append_pending_moves(pm, &stack);
3222
3223 while (!list_empty(&stack)) {
3224 pm = list_first_entry(&stack, struct pending_dir_move, list);
3225 parent_ino = pm->ino;
3226 ret = apply_dir_move(sctx, pm);
3227 free_pending_move(sctx, pm);
3228 if (ret)
3229 goto out;
3230 pm = get_pending_dir_moves(sctx, parent_ino);
3231 if (pm)
3232 tail_append_pending_moves(pm, &stack);
3233 }
3234 return 0;
3235
3236out:
3237 while (!list_empty(&stack)) {
3238 pm = list_first_entry(&stack, struct pending_dir_move, list);
3239 free_pending_move(sctx, pm);
3240 }
3241 return ret;
3242}
3243
3244static int wait_for_parent_move(struct send_ctx *sctx,
3245 struct recorded_ref *parent_ref)
3246{
Filipe Mananaf9594922014-03-27 20:14:01 +00003247 int ret = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003248 u64 ino = parent_ref->dir;
3249 u64 parent_ino_before, parent_ino_after;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003250 struct fs_path *path_before = NULL;
3251 struct fs_path *path_after = NULL;
3252 int len1, len2;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003253
3254 path_after = fs_path_alloc();
Filipe Mananaf9594922014-03-27 20:14:01 +00003255 path_before = fs_path_alloc();
3256 if (!path_after || !path_before) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003257 ret = -ENOMEM;
3258 goto out;
3259 }
3260
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003261 /*
Filipe Mananaf9594922014-03-27 20:14:01 +00003262 * Our current directory inode may not yet be renamed/moved because some
3263 * ancestor (immediate or not) has to be renamed/moved first. So find if
3264 * such ancestor exists and make sure our own rename/move happens after
3265 * that ancestor is processed.
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003266 */
Filipe Mananaf9594922014-03-27 20:14:01 +00003267 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3268 if (is_waiting_for_move(sctx, ino)) {
3269 ret = 1;
3270 break;
3271 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003272
3273 fs_path_reset(path_before);
3274 fs_path_reset(path_after);
3275
3276 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
Filipe Mananaf9594922014-03-27 20:14:01 +00003277 NULL, path_after);
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003278 if (ret < 0)
3279 goto out;
3280 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3281 NULL, path_before);
Filipe Mananaf9594922014-03-27 20:14:01 +00003282 if (ret < 0 && ret != -ENOENT) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003283 goto out;
Filipe Mananaf9594922014-03-27 20:14:01 +00003284 } else if (ret == -ENOENT) {
3285 ret = 1;
3286 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003287 }
3288
3289 len1 = fs_path_len(path_before);
3290 len2 = fs_path_len(path_after);
Filipe Mananaf9594922014-03-27 20:14:01 +00003291 if (ino > sctx->cur_ino &&
3292 (parent_ino_before != parent_ino_after || len1 != len2 ||
3293 memcmp(path_before->start, path_after->start, len1))) {
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003294 ret = 1;
Filipe Mananaf9594922014-03-27 20:14:01 +00003295 break;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003296 }
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003297 ino = parent_ino_after;
Filipe Mananabfa7e1f2014-03-19 14:20:54 +00003298 }
3299
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003300out:
3301 fs_path_free(path_before);
3302 fs_path_free(path_after);
3303
Filipe Mananaf9594922014-03-27 20:14:01 +00003304 if (ret == 1) {
3305 ret = add_pending_dir_move(sctx,
3306 sctx->cur_ino,
3307 sctx->cur_inode_gen,
3308 ino,
3309 &sctx->new_refs,
3310 &sctx->deleted_refs);
3311 if (!ret)
3312 ret = 1;
3313 }
3314
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003315 return ret;
3316}
3317
Alexander Block31db9f72012-07-25 23:19:24 +02003318/*
3319 * This does all the move/link/unlink/rmdir magic.
3320 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003321static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003322{
3323 int ret = 0;
3324 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003325 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003326 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003327 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003328 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003329 u64 ow_gen;
3330 int did_overwrite = 0;
3331 int is_orphan = 0;
Filipe Manana29d6d302014-02-16 21:01:39 +00003332 u64 last_dir_ino_rm = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003333
3334verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3335
Alexander Block6d85ed02012-08-01 14:48:59 +02003336 /*
3337 * This should never happen as the root dir always has the same ref
3338 * which is always '..'
3339 */
3340 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003341 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003342
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003343 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003344 if (!valid_path) {
3345 ret = -ENOMEM;
3346 goto out;
3347 }
3348
Alexander Block31db9f72012-07-25 23:19:24 +02003349 /*
3350 * First, check if the first ref of the current inode was overwritten
3351 * before. If yes, we know that the current inode was already orphanized
3352 * and thus use the orphan name. If not, we can use get_cur_path to
3353 * get the path of the first ref as it would like while receiving at
3354 * this point in time.
3355 * New inodes are always orphan at the beginning, so force to use the
3356 * orphan name in this case.
3357 * The first ref is stored in valid_path and will be updated if it
3358 * gets moved around.
3359 */
3360 if (!sctx->cur_inode_new) {
3361 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3362 sctx->cur_inode_gen);
3363 if (ret < 0)
3364 goto out;
3365 if (ret)
3366 did_overwrite = 1;
3367 }
3368 if (sctx->cur_inode_new || did_overwrite) {
3369 ret = gen_unique_name(sctx, sctx->cur_ino,
3370 sctx->cur_inode_gen, valid_path);
3371 if (ret < 0)
3372 goto out;
3373 is_orphan = 1;
3374 } else {
3375 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3376 valid_path);
3377 if (ret < 0)
3378 goto out;
3379 }
3380
3381 list_for_each_entry(cur, &sctx->new_refs, list) {
3382 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003383 * We may have refs where the parent directory does not exist
3384 * yet. This happens if the parent directories inum is higher
3385 * the the current inum. To handle this case, we create the
3386 * parent directory out of order. But we need to check if this
3387 * did already happen before due to other refs in the same dir.
3388 */
3389 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3390 if (ret < 0)
3391 goto out;
3392 if (ret == inode_state_will_create) {
3393 ret = 0;
3394 /*
3395 * First check if any of the current inodes refs did
3396 * already create the dir.
3397 */
3398 list_for_each_entry(cur2, &sctx->new_refs, list) {
3399 if (cur == cur2)
3400 break;
3401 if (cur2->dir == cur->dir) {
3402 ret = 1;
3403 break;
3404 }
3405 }
3406
3407 /*
3408 * If that did not happen, check if a previous inode
3409 * did already create the dir.
3410 */
3411 if (!ret)
3412 ret = did_create_dir(sctx, cur->dir);
3413 if (ret < 0)
3414 goto out;
3415 if (!ret) {
3416 ret = send_create_inode(sctx, cur->dir);
3417 if (ret < 0)
3418 goto out;
3419 }
3420 }
3421
3422 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003423 * Check if this new ref would overwrite the first ref of
3424 * another unprocessed inode. If yes, orphanize the
3425 * overwritten inode. If we find an overwritten ref that is
3426 * not the first ref, simply unlink it.
3427 */
3428 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3429 cur->name, cur->name_len,
3430 &ow_inode, &ow_gen);
3431 if (ret < 0)
3432 goto out;
3433 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003434 ret = is_first_ref(sctx->parent_root,
3435 ow_inode, cur->dir, cur->name,
3436 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003437 if (ret < 0)
3438 goto out;
3439 if (ret) {
3440 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3441 cur->full_path);
3442 if (ret < 0)
3443 goto out;
3444 } else {
3445 ret = send_unlink(sctx, cur->full_path);
3446 if (ret < 0)
3447 goto out;
3448 }
3449 }
3450
3451 /*
3452 * link/move the ref to the new place. If we have an orphan
3453 * inode, move it and update valid_path. If not, link or move
3454 * it depending on the inode mode.
3455 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003456 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003457 ret = send_rename(sctx, valid_path, cur->full_path);
3458 if (ret < 0)
3459 goto out;
3460 is_orphan = 0;
3461 ret = fs_path_copy(valid_path, cur->full_path);
3462 if (ret < 0)
3463 goto out;
3464 } else {
3465 if (S_ISDIR(sctx->cur_inode_mode)) {
3466 /*
3467 * Dirs can't be linked, so move it. For moved
3468 * dirs, we always have one new and one deleted
3469 * ref. The deleted ref is ignored later.
3470 */
Filipe David Borba Mananad86477b2014-01-30 13:27:12 +00003471 ret = wait_for_parent_move(sctx, cur);
3472 if (ret < 0)
3473 goto out;
3474 if (ret) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003475 *pending_move = 1;
3476 } else {
3477 ret = send_rename(sctx, valid_path,
3478 cur->full_path);
3479 if (!ret)
3480 ret = fs_path_copy(valid_path,
3481 cur->full_path);
3482 }
Alexander Block31db9f72012-07-25 23:19:24 +02003483 if (ret < 0)
3484 goto out;
3485 } else {
3486 ret = send_link(sctx, cur->full_path,
3487 valid_path);
3488 if (ret < 0)
3489 goto out;
3490 }
3491 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003492 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003493 if (ret < 0)
3494 goto out;
3495 }
3496
3497 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3498 /*
3499 * Check if we can already rmdir the directory. If not,
3500 * orphanize it. For every dir item inside that gets deleted
3501 * later, we do this check again and rmdir it then if possible.
3502 * See the use of check_dirs for more details.
3503 */
Filipe Manana9dc44212014-02-19 14:31:44 +00003504 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3505 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003506 if (ret < 0)
3507 goto out;
3508 if (ret) {
3509 ret = send_rmdir(sctx, valid_path);
3510 if (ret < 0)
3511 goto out;
3512 } else if (!is_orphan) {
3513 ret = orphanize_inode(sctx, sctx->cur_ino,
3514 sctx->cur_inode_gen, valid_path);
3515 if (ret < 0)
3516 goto out;
3517 is_orphan = 1;
3518 }
3519
3520 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003521 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003522 if (ret < 0)
3523 goto out;
3524 }
Alexander Blockccf16262012-07-28 11:46:29 +02003525 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3526 !list_empty(&sctx->deleted_refs)) {
3527 /*
3528 * We have a moved dir. Add the old parent to check_dirs
3529 */
3530 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3531 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003532 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003533 if (ret < 0)
3534 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003535 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3536 /*
3537 * We have a non dir inode. Go through all deleted refs and
3538 * unlink them if they were not already overwritten by other
3539 * inodes.
3540 */
3541 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3542 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3543 sctx->cur_ino, sctx->cur_inode_gen,
3544 cur->name, cur->name_len);
3545 if (ret < 0)
3546 goto out;
3547 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003548 ret = send_unlink(sctx, cur->full_path);
3549 if (ret < 0)
3550 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003551 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003552 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003553 if (ret < 0)
3554 goto out;
3555 }
Alexander Block31db9f72012-07-25 23:19:24 +02003556 /*
3557 * If the inode is still orphan, unlink the orphan. This may
3558 * happen when a previous inode did overwrite the first ref
3559 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003560 * inode. Unlinking does not mean that the inode is deleted in
3561 * all cases. There may still be links to this inode in other
3562 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003563 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003564 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003565 ret = send_unlink(sctx, valid_path);
3566 if (ret < 0)
3567 goto out;
3568 }
3569 }
3570
3571 /*
3572 * We did collect all parent dirs where cur_inode was once located. We
3573 * now go through all these dirs and check if they are pending for
3574 * deletion and if it's finally possible to perform the rmdir now.
3575 * We also update the inode stats of the parent dirs here.
3576 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003577 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003578 /*
3579 * In case we had refs into dirs that were not processed yet,
3580 * we don't need to do the utime and rmdir logic for these dirs.
3581 * The dir will be processed later.
3582 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003583 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003584 continue;
3585
Josef Bacikba5e8f22013-08-16 16:52:55 -04003586 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003587 if (ret < 0)
3588 goto out;
3589
3590 if (ret == inode_state_did_create ||
3591 ret == inode_state_no_change) {
3592 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003593 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003594 if (ret < 0)
3595 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003596 } else if (ret == inode_state_did_delete &&
3597 cur->dir != last_dir_ino_rm) {
Filipe Manana9dc44212014-02-19 14:31:44 +00003598 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3599 sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003600 if (ret < 0)
3601 goto out;
3602 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003603 ret = get_cur_path(sctx, cur->dir,
3604 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003605 if (ret < 0)
3606 goto out;
3607 ret = send_rmdir(sctx, valid_path);
3608 if (ret < 0)
3609 goto out;
Filipe Manana29d6d302014-02-16 21:01:39 +00003610 last_dir_ino_rm = cur->dir;
Alexander Block31db9f72012-07-25 23:19:24 +02003611 }
3612 }
3613 }
3614
Alexander Block31db9f72012-07-25 23:19:24 +02003615 ret = 0;
3616
3617out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04003618 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003619 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003620 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003621 return ret;
3622}
3623
Liu Boa4d96d62014-03-03 21:31:03 +08003624static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3625 struct fs_path *name, void *ctx, struct list_head *refs)
Alexander Block31db9f72012-07-25 23:19:24 +02003626{
3627 int ret = 0;
3628 struct send_ctx *sctx = ctx;
3629 struct fs_path *p;
3630 u64 gen;
3631
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003632 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003633 if (!p)
3634 return -ENOMEM;
3635
Liu Boa4d96d62014-03-03 21:31:03 +08003636 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003637 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003638 if (ret < 0)
3639 goto out;
3640
Alexander Block31db9f72012-07-25 23:19:24 +02003641 ret = get_cur_path(sctx, dir, gen, p);
3642 if (ret < 0)
3643 goto out;
3644 ret = fs_path_add_path(p, name);
3645 if (ret < 0)
3646 goto out;
3647
Liu Boa4d96d62014-03-03 21:31:03 +08003648 ret = __record_ref(refs, dir, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003649
3650out:
3651 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003652 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003653 return ret;
3654}
3655
Liu Boa4d96d62014-03-03 21:31:03 +08003656static int __record_new_ref(int num, u64 dir, int index,
3657 struct fs_path *name,
3658 void *ctx)
3659{
3660 struct send_ctx *sctx = ctx;
3661 return record_ref(sctx->send_root, num, dir, index, name,
3662 ctx, &sctx->new_refs);
3663}
3664
3665
Alexander Block31db9f72012-07-25 23:19:24 +02003666static int __record_deleted_ref(int num, u64 dir, int index,
3667 struct fs_path *name,
3668 void *ctx)
3669{
Alexander Block31db9f72012-07-25 23:19:24 +02003670 struct send_ctx *sctx = ctx;
Liu Boa4d96d62014-03-03 21:31:03 +08003671 return record_ref(sctx->parent_root, num, dir, index, name,
3672 ctx, &sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02003673}
3674
3675static int record_new_ref(struct send_ctx *sctx)
3676{
3677 int ret;
3678
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003679 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3680 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003681 if (ret < 0)
3682 goto out;
3683 ret = 0;
3684
3685out:
3686 return ret;
3687}
3688
3689static int record_deleted_ref(struct send_ctx *sctx)
3690{
3691 int ret;
3692
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003693 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3694 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003695 if (ret < 0)
3696 goto out;
3697 ret = 0;
3698
3699out:
3700 return ret;
3701}
3702
3703struct find_ref_ctx {
3704 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003705 u64 dir_gen;
3706 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003707 struct fs_path *name;
3708 int found_idx;
3709};
3710
3711static int __find_iref(int num, u64 dir, int index,
3712 struct fs_path *name,
3713 void *ctx_)
3714{
3715 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003716 u64 dir_gen;
3717 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003718
3719 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3720 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003721 /*
3722 * To avoid doing extra lookups we'll only do this if everything
3723 * else matches.
3724 */
3725 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3726 NULL, NULL, NULL);
3727 if (ret)
3728 return ret;
3729 if (dir_gen != ctx->dir_gen)
3730 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003731 ctx->found_idx = num;
3732 return 1;
3733 }
3734 return 0;
3735}
3736
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003737static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003738 struct btrfs_path *path,
3739 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003740 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003741{
3742 int ret;
3743 struct find_ref_ctx ctx;
3744
3745 ctx.dir = dir;
3746 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003747 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003748 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003749 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003750
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003751 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003752 if (ret < 0)
3753 return ret;
3754
3755 if (ctx.found_idx == -1)
3756 return -ENOENT;
3757
3758 return ctx.found_idx;
3759}
3760
3761static int __record_changed_new_ref(int num, u64 dir, int index,
3762 struct fs_path *name,
3763 void *ctx)
3764{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003765 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003766 int ret;
3767 struct send_ctx *sctx = ctx;
3768
Josef Bacikba5e8f22013-08-16 16:52:55 -04003769 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3770 NULL, NULL, NULL);
3771 if (ret)
3772 return ret;
3773
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003774 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003775 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003776 if (ret == -ENOENT)
3777 ret = __record_new_ref(num, dir, index, name, sctx);
3778 else if (ret > 0)
3779 ret = 0;
3780
3781 return ret;
3782}
3783
3784static int __record_changed_deleted_ref(int num, u64 dir, int index,
3785 struct fs_path *name,
3786 void *ctx)
3787{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003788 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003789 int ret;
3790 struct send_ctx *sctx = ctx;
3791
Josef Bacikba5e8f22013-08-16 16:52:55 -04003792 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3793 NULL, NULL, NULL);
3794 if (ret)
3795 return ret;
3796
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003797 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003798 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003799 if (ret == -ENOENT)
3800 ret = __record_deleted_ref(num, dir, index, name, sctx);
3801 else if (ret > 0)
3802 ret = 0;
3803
3804 return ret;
3805}
3806
3807static int record_changed_ref(struct send_ctx *sctx)
3808{
3809 int ret = 0;
3810
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003811 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003812 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3813 if (ret < 0)
3814 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003815 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003816 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3817 if (ret < 0)
3818 goto out;
3819 ret = 0;
3820
3821out:
3822 return ret;
3823}
3824
3825/*
3826 * Record and process all refs at once. Needed when an inode changes the
3827 * generation number, which means that it was deleted and recreated.
3828 */
3829static int process_all_refs(struct send_ctx *sctx,
3830 enum btrfs_compare_tree_result cmd)
3831{
3832 int ret;
3833 struct btrfs_root *root;
3834 struct btrfs_path *path;
3835 struct btrfs_key key;
3836 struct btrfs_key found_key;
3837 struct extent_buffer *eb;
3838 int slot;
3839 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003840 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003841
3842 path = alloc_path_for_send();
3843 if (!path)
3844 return -ENOMEM;
3845
3846 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3847 root = sctx->send_root;
3848 cb = __record_new_ref;
3849 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3850 root = sctx->parent_root;
3851 cb = __record_deleted_ref;
3852 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01003853 btrfs_err(sctx->send_root->fs_info,
3854 "Wrong command %d in process_all_refs", cmd);
3855 ret = -EINVAL;
3856 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003857 }
3858
3859 key.objectid = sctx->cmp_key->objectid;
3860 key.type = BTRFS_INODE_REF_KEY;
3861 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003862 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3863 if (ret < 0)
3864 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003865
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003866 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02003867 eb = path->nodes[0];
3868 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003869 if (slot >= btrfs_header_nritems(eb)) {
3870 ret = btrfs_next_leaf(root, path);
3871 if (ret < 0)
3872 goto out;
3873 else if (ret > 0)
3874 break;
3875 continue;
3876 }
3877
Alexander Block31db9f72012-07-25 23:19:24 +02003878 btrfs_item_key_to_cpu(eb, &found_key, slot);
3879
3880 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003881 (found_key.type != BTRFS_INODE_REF_KEY &&
3882 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003883 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003884
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003885 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003886 if (ret < 0)
3887 goto out;
3888
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003889 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003890 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003891 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003892
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003893 ret = process_recorded_refs(sctx, &pending_move);
3894 /* Only applicable to an incremental send. */
3895 ASSERT(pending_move == 0);
Alexander Block31db9f72012-07-25 23:19:24 +02003896
3897out:
3898 btrfs_free_path(path);
3899 return ret;
3900}
3901
3902static int send_set_xattr(struct send_ctx *sctx,
3903 struct fs_path *path,
3904 const char *name, int name_len,
3905 const char *data, int data_len)
3906{
3907 int ret = 0;
3908
3909 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3910 if (ret < 0)
3911 goto out;
3912
3913 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3914 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3915 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3916
3917 ret = send_cmd(sctx);
3918
3919tlv_put_failure:
3920out:
3921 return ret;
3922}
3923
3924static int send_remove_xattr(struct send_ctx *sctx,
3925 struct fs_path *path,
3926 const char *name, int name_len)
3927{
3928 int ret = 0;
3929
3930 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3931 if (ret < 0)
3932 goto out;
3933
3934 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3935 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3936
3937 ret = send_cmd(sctx);
3938
3939tlv_put_failure:
3940out:
3941 return ret;
3942}
3943
3944static int __process_new_xattr(int num, struct btrfs_key *di_key,
3945 const char *name, int name_len,
3946 const char *data, int data_len,
3947 u8 type, void *ctx)
3948{
3949 int ret;
3950 struct send_ctx *sctx = ctx;
3951 struct fs_path *p;
3952 posix_acl_xattr_header dummy_acl;
3953
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003954 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003955 if (!p)
3956 return -ENOMEM;
3957
3958 /*
3959 * This hack is needed because empty acl's are stored as zero byte
3960 * data in xattrs. Problem with that is, that receiving these zero byte
3961 * acl's will fail later. To fix this, we send a dummy acl list that
3962 * only contains the version number and no entries.
3963 */
3964 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3965 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3966 if (data_len == 0) {
3967 dummy_acl.a_version =
3968 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3969 data = (char *)&dummy_acl;
3970 data_len = sizeof(dummy_acl);
3971 }
3972 }
3973
3974 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3975 if (ret < 0)
3976 goto out;
3977
3978 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3979
3980out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003981 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003982 return ret;
3983}
3984
3985static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3986 const char *name, int name_len,
3987 const char *data, int data_len,
3988 u8 type, void *ctx)
3989{
3990 int ret;
3991 struct send_ctx *sctx = ctx;
3992 struct fs_path *p;
3993
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003994 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003995 if (!p)
3996 return -ENOMEM;
3997
3998 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3999 if (ret < 0)
4000 goto out;
4001
4002 ret = send_remove_xattr(sctx, p, name, name_len);
4003
4004out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004005 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004006 return ret;
4007}
4008
4009static int process_new_xattr(struct send_ctx *sctx)
4010{
4011 int ret = 0;
4012
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004013 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4014 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004015
4016 return ret;
4017}
4018
4019static int process_deleted_xattr(struct send_ctx *sctx)
4020{
4021 int ret;
4022
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004023 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4024 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004025
4026 return ret;
4027}
4028
4029struct find_xattr_ctx {
4030 const char *name;
4031 int name_len;
4032 int found_idx;
4033 char *found_data;
4034 int found_data_len;
4035};
4036
4037static int __find_xattr(int num, struct btrfs_key *di_key,
4038 const char *name, int name_len,
4039 const char *data, int data_len,
4040 u8 type, void *vctx)
4041{
4042 struct find_xattr_ctx *ctx = vctx;
4043
4044 if (name_len == ctx->name_len &&
4045 strncmp(name, ctx->name, name_len) == 0) {
4046 ctx->found_idx = num;
4047 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00004048 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02004049 if (!ctx->found_data)
4050 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02004051 return 1;
4052 }
4053 return 0;
4054}
4055
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004056static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02004057 struct btrfs_path *path,
4058 struct btrfs_key *key,
4059 const char *name, int name_len,
4060 char **data, int *data_len)
4061{
4062 int ret;
4063 struct find_xattr_ctx ctx;
4064
4065 ctx.name = name;
4066 ctx.name_len = name_len;
4067 ctx.found_idx = -1;
4068 ctx.found_data = NULL;
4069 ctx.found_data_len = 0;
4070
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004071 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004072 if (ret < 0)
4073 return ret;
4074
4075 if (ctx.found_idx == -1)
4076 return -ENOENT;
4077 if (data) {
4078 *data = ctx.found_data;
4079 *data_len = ctx.found_data_len;
4080 } else {
4081 kfree(ctx.found_data);
4082 }
4083 return ctx.found_idx;
4084}
4085
4086
4087static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4088 const char *name, int name_len,
4089 const char *data, int data_len,
4090 u8 type, void *ctx)
4091{
4092 int ret;
4093 struct send_ctx *sctx = ctx;
4094 char *found_data = NULL;
4095 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004096
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004097 ret = find_xattr(sctx->parent_root, sctx->right_path,
4098 sctx->cmp_key, name, name_len, &found_data,
4099 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02004100 if (ret == -ENOENT) {
4101 ret = __process_new_xattr(num, di_key, name, name_len, data,
4102 data_len, type, ctx);
4103 } else if (ret >= 0) {
4104 if (data_len != found_data_len ||
4105 memcmp(data, found_data, data_len)) {
4106 ret = __process_new_xattr(num, di_key, name, name_len,
4107 data, data_len, type, ctx);
4108 } else {
4109 ret = 0;
4110 }
4111 }
4112
4113 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02004114 return ret;
4115}
4116
4117static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4118 const char *name, int name_len,
4119 const char *data, int data_len,
4120 u8 type, void *ctx)
4121{
4122 int ret;
4123 struct send_ctx *sctx = ctx;
4124
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004125 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4126 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004127 if (ret == -ENOENT)
4128 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4129 data_len, type, ctx);
4130 else if (ret >= 0)
4131 ret = 0;
4132
4133 return ret;
4134}
4135
4136static int process_changed_xattr(struct send_ctx *sctx)
4137{
4138 int ret = 0;
4139
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004140 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004141 sctx->cmp_key, __process_changed_new_xattr, sctx);
4142 if (ret < 0)
4143 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004144 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02004145 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4146
4147out:
4148 return ret;
4149}
4150
4151static int process_all_new_xattrs(struct send_ctx *sctx)
4152{
4153 int ret;
4154 struct btrfs_root *root;
4155 struct btrfs_path *path;
4156 struct btrfs_key key;
4157 struct btrfs_key found_key;
4158 struct extent_buffer *eb;
4159 int slot;
4160
4161 path = alloc_path_for_send();
4162 if (!path)
4163 return -ENOMEM;
4164
4165 root = sctx->send_root;
4166
4167 key.objectid = sctx->cmp_key->objectid;
4168 key.type = BTRFS_XATTR_ITEM_KEY;
4169 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004170 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4171 if (ret < 0)
4172 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004173
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004174 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004175 eb = path->nodes[0];
4176 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004177 if (slot >= btrfs_header_nritems(eb)) {
4178 ret = btrfs_next_leaf(root, path);
4179 if (ret < 0) {
4180 goto out;
4181 } else if (ret > 0) {
4182 ret = 0;
4183 break;
4184 }
4185 continue;
4186 }
Alexander Block31db9f72012-07-25 23:19:24 +02004187
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004188 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02004189 if (found_key.objectid != key.objectid ||
4190 found_key.type != key.type) {
4191 ret = 0;
4192 goto out;
4193 }
4194
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004195 ret = iterate_dir_item(root, path, &found_key,
4196 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004197 if (ret < 0)
4198 goto out;
4199
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00004200 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004201 }
4202
4203out:
4204 btrfs_free_path(path);
4205 return ret;
4206}
4207
Josef Baciked259092013-10-25 11:36:01 -04004208static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4209{
4210 struct btrfs_root *root = sctx->send_root;
4211 struct btrfs_fs_info *fs_info = root->fs_info;
4212 struct inode *inode;
4213 struct page *page;
4214 char *addr;
4215 struct btrfs_key key;
4216 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4217 pgoff_t last_index;
4218 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4219 ssize_t ret = 0;
4220
4221 key.objectid = sctx->cur_ino;
4222 key.type = BTRFS_INODE_ITEM_KEY;
4223 key.offset = 0;
4224
4225 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4226 if (IS_ERR(inode))
4227 return PTR_ERR(inode);
4228
4229 if (offset + len > i_size_read(inode)) {
4230 if (offset > i_size_read(inode))
4231 len = 0;
4232 else
4233 len = offset - i_size_read(inode);
4234 }
4235 if (len == 0)
4236 goto out;
4237
4238 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
Liu Bo2131bcd32014-03-05 10:07:35 +08004239
4240 /* initial readahead */
4241 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4242 file_ra_state_init(&sctx->ra, inode->i_mapping);
4243 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4244 last_index - index + 1);
4245
Josef Baciked259092013-10-25 11:36:01 -04004246 while (index <= last_index) {
4247 unsigned cur_len = min_t(unsigned, len,
4248 PAGE_CACHE_SIZE - pg_offset);
4249 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4250 if (!page) {
4251 ret = -ENOMEM;
4252 break;
4253 }
4254
4255 if (!PageUptodate(page)) {
4256 btrfs_readpage(NULL, page);
4257 lock_page(page);
4258 if (!PageUptodate(page)) {
4259 unlock_page(page);
4260 page_cache_release(page);
4261 ret = -EIO;
4262 break;
4263 }
4264 }
4265
4266 addr = kmap(page);
4267 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4268 kunmap(page);
4269 unlock_page(page);
4270 page_cache_release(page);
4271 index++;
4272 pg_offset = 0;
4273 len -= cur_len;
4274 ret += cur_len;
4275 }
4276out:
4277 iput(inode);
4278 return ret;
4279}
4280
Alexander Block31db9f72012-07-25 23:19:24 +02004281/*
4282 * Read some bytes from the current inode/file and send a write command to
4283 * user space.
4284 */
4285static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4286{
4287 int ret = 0;
4288 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004289 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004290
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004291 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004292 if (!p)
4293 return -ENOMEM;
4294
Alexander Block31db9f72012-07-25 23:19:24 +02004295verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4296
Josef Baciked259092013-10-25 11:36:01 -04004297 num_read = fill_read_buf(sctx, offset, len);
4298 if (num_read <= 0) {
4299 if (num_read < 0)
4300 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004301 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004302 }
Alexander Block31db9f72012-07-25 23:19:24 +02004303
4304 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4305 if (ret < 0)
4306 goto out;
4307
4308 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4309 if (ret < 0)
4310 goto out;
4311
4312 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4313 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004314 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004315
4316 ret = send_cmd(sctx);
4317
4318tlv_put_failure:
4319out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004320 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004321 if (ret < 0)
4322 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004323 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004324}
4325
4326/*
4327 * Send a clone command to user space.
4328 */
4329static int send_clone(struct send_ctx *sctx,
4330 u64 offset, u32 len,
4331 struct clone_root *clone_root)
4332{
4333 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004334 struct fs_path *p;
4335 u64 gen;
4336
4337verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4338 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4339 clone_root->root->objectid, clone_root->ino,
4340 clone_root->offset);
4341
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004342 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004343 if (!p)
4344 return -ENOMEM;
4345
4346 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
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_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4355 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4356 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4357
Alexander Blocke938c8a2012-07-28 16:33:49 +02004358 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004359 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004360 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004361 if (ret < 0)
4362 goto out;
4363 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4364 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004365 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004366 }
4367 if (ret < 0)
4368 goto out;
4369
4370 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02004371 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004372 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004373 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004374 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4375 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4376 clone_root->offset);
4377
4378 ret = send_cmd(sctx);
4379
4380tlv_put_failure:
4381out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004382 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004383 return ret;
4384}
4385
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004386/*
4387 * Send an update extent command to user space.
4388 */
4389static int send_update_extent(struct send_ctx *sctx,
4390 u64 offset, u32 len)
4391{
4392 int ret = 0;
4393 struct fs_path *p;
4394
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004395 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004396 if (!p)
4397 return -ENOMEM;
4398
4399 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4400 if (ret < 0)
4401 goto out;
4402
4403 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4404 if (ret < 0)
4405 goto out;
4406
4407 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4408 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4409 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4410
4411 ret = send_cmd(sctx);
4412
4413tlv_put_failure:
4414out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004415 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004416 return ret;
4417}
4418
Josef Bacik16e75492013-10-22 12:18:51 -04004419static int send_hole(struct send_ctx *sctx, u64 end)
4420{
4421 struct fs_path *p = NULL;
4422 u64 offset = sctx->cur_inode_last_extent;
4423 u64 len;
4424 int ret = 0;
4425
4426 p = fs_path_alloc();
4427 if (!p)
4428 return -ENOMEM;
Filipe Mananac715e152014-03-31 14:52:14 +01004429 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4430 if (ret < 0)
4431 goto tlv_put_failure;
Josef Bacik16e75492013-10-22 12:18:51 -04004432 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4433 while (offset < end) {
4434 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4435
4436 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4437 if (ret < 0)
4438 break;
Josef Bacik16e75492013-10-22 12:18:51 -04004439 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4440 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4441 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4442 ret = send_cmd(sctx);
4443 if (ret < 0)
4444 break;
4445 offset += len;
4446 }
4447tlv_put_failure:
4448 fs_path_free(p);
4449 return ret;
4450}
4451
Alexander Block31db9f72012-07-25 23:19:24 +02004452static int send_write_or_clone(struct send_ctx *sctx,
4453 struct btrfs_path *path,
4454 struct btrfs_key *key,
4455 struct clone_root *clone_root)
4456{
4457 int ret = 0;
4458 struct btrfs_file_extent_item *ei;
4459 u64 offset = key->offset;
4460 u64 pos = 0;
4461 u64 len;
4462 u32 l;
4463 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004464 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02004465
4466 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4467 struct btrfs_file_extent_item);
4468 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004469 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004470 len = btrfs_file_extent_inline_len(path->nodes[0],
4471 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004472 /*
4473 * it is possible the inline item won't cover the whole page,
4474 * but there may be items after this page. Make
4475 * sure to send the whole thing
4476 */
4477 len = PAGE_CACHE_ALIGN(len);
4478 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004479 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004480 }
Alexander Block31db9f72012-07-25 23:19:24 +02004481
4482 if (offset + len > sctx->cur_inode_size)
4483 len = sctx->cur_inode_size - offset;
4484 if (len == 0) {
4485 ret = 0;
4486 goto out;
4487 }
4488
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004489 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004490 ret = send_clone(sctx, offset, len, clone_root);
4491 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4492 ret = send_update_extent(sctx, offset, len);
4493 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004494 while (pos < len) {
4495 l = len - pos;
4496 if (l > BTRFS_SEND_READ_SIZE)
4497 l = BTRFS_SEND_READ_SIZE;
4498 ret = send_write(sctx, pos + offset, l);
4499 if (ret < 0)
4500 goto out;
4501 if (!ret)
4502 break;
4503 pos += ret;
4504 }
4505 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004506 }
Alexander Block31db9f72012-07-25 23:19:24 +02004507out:
4508 return ret;
4509}
4510
4511static int is_extent_unchanged(struct send_ctx *sctx,
4512 struct btrfs_path *left_path,
4513 struct btrfs_key *ekey)
4514{
4515 int ret = 0;
4516 struct btrfs_key key;
4517 struct btrfs_path *path = NULL;
4518 struct extent_buffer *eb;
4519 int slot;
4520 struct btrfs_key found_key;
4521 struct btrfs_file_extent_item *ei;
4522 u64 left_disknr;
4523 u64 right_disknr;
4524 u64 left_offset;
4525 u64 right_offset;
4526 u64 left_offset_fixed;
4527 u64 left_len;
4528 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04004529 u64 left_gen;
4530 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004531 u8 left_type;
4532 u8 right_type;
4533
4534 path = alloc_path_for_send();
4535 if (!path)
4536 return -ENOMEM;
4537
4538 eb = left_path->nodes[0];
4539 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02004540 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4541 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004542
4543 if (left_type != BTRFS_FILE_EXTENT_REG) {
4544 ret = 0;
4545 goto out;
4546 }
Chris Mason74dd17f2012-08-07 16:25:13 -04004547 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4548 left_len = btrfs_file_extent_num_bytes(eb, ei);
4549 left_offset = btrfs_file_extent_offset(eb, ei);
4550 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004551
4552 /*
4553 * Following comments will refer to these graphics. L is the left
4554 * extents which we are checking at the moment. 1-8 are the right
4555 * extents that we iterate.
4556 *
4557 * |-----L-----|
4558 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4559 *
4560 * |-----L-----|
4561 * |--1--|-2b-|...(same as above)
4562 *
4563 * Alternative situation. Happens on files where extents got split.
4564 * |-----L-----|
4565 * |-----------7-----------|-6-|
4566 *
4567 * Alternative situation. Happens on files which got larger.
4568 * |-----L-----|
4569 * |-8-|
4570 * Nothing follows after 8.
4571 */
4572
4573 key.objectid = ekey->objectid;
4574 key.type = BTRFS_EXTENT_DATA_KEY;
4575 key.offset = ekey->offset;
4576 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4577 if (ret < 0)
4578 goto out;
4579 if (ret) {
4580 ret = 0;
4581 goto out;
4582 }
4583
4584 /*
4585 * Handle special case where the right side has no extents at all.
4586 */
4587 eb = path->nodes[0];
4588 slot = path->slots[0];
4589 btrfs_item_key_to_cpu(eb, &found_key, slot);
4590 if (found_key.objectid != key.objectid ||
4591 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004592 /* If we're a hole then just pretend nothing changed */
4593 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004594 goto out;
4595 }
4596
4597 /*
4598 * We're now on 2a, 2b or 7.
4599 */
4600 key = found_key;
4601 while (key.offset < ekey->offset + left_len) {
4602 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4603 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004604 if (right_type != BTRFS_FILE_EXTENT_REG) {
4605 ret = 0;
4606 goto out;
4607 }
4608
Josef Bacik007d31f2013-10-31 16:49:02 -04004609 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4610 right_len = btrfs_file_extent_num_bytes(eb, ei);
4611 right_offset = btrfs_file_extent_offset(eb, ei);
4612 right_gen = btrfs_file_extent_generation(eb, ei);
4613
Alexander Block31db9f72012-07-25 23:19:24 +02004614 /*
4615 * Are we at extent 8? If yes, we know the extent is changed.
4616 * This may only happen on the first iteration.
4617 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02004618 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004619 /* If we're a hole just pretend nothing changed */
4620 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004621 goto out;
4622 }
4623
4624 left_offset_fixed = left_offset;
4625 if (key.offset < ekey->offset) {
4626 /* Fix the right offset for 2a and 7. */
4627 right_offset += ekey->offset - key.offset;
4628 } else {
4629 /* Fix the left offset for all behind 2a and 2b */
4630 left_offset_fixed += key.offset - ekey->offset;
4631 }
4632
4633 /*
4634 * Check if we have the same extent.
4635 */
Alexander Block39540962012-08-01 12:46:05 +02004636 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04004637 left_offset_fixed != right_offset ||
4638 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02004639 ret = 0;
4640 goto out;
4641 }
4642
4643 /*
4644 * Go to the next extent.
4645 */
4646 ret = btrfs_next_item(sctx->parent_root, path);
4647 if (ret < 0)
4648 goto out;
4649 if (!ret) {
4650 eb = path->nodes[0];
4651 slot = path->slots[0];
4652 btrfs_item_key_to_cpu(eb, &found_key, slot);
4653 }
4654 if (ret || found_key.objectid != key.objectid ||
4655 found_key.type != key.type) {
4656 key.offset += right_len;
4657 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00004658 }
4659 if (found_key.offset != key.offset + right_len) {
4660 ret = 0;
4661 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004662 }
4663 key = found_key;
4664 }
4665
4666 /*
4667 * We're now behind the left extent (treat as unchanged) or at the end
4668 * of the right side (treat as changed).
4669 */
4670 if (key.offset >= ekey->offset + left_len)
4671 ret = 1;
4672 else
4673 ret = 0;
4674
4675
4676out:
4677 btrfs_free_path(path);
4678 return ret;
4679}
4680
Josef Bacik16e75492013-10-22 12:18:51 -04004681static int get_last_extent(struct send_ctx *sctx, u64 offset)
4682{
4683 struct btrfs_path *path;
4684 struct btrfs_root *root = sctx->send_root;
4685 struct btrfs_file_extent_item *fi;
4686 struct btrfs_key key;
4687 u64 extent_end;
4688 u8 type;
4689 int ret;
4690
4691 path = alloc_path_for_send();
4692 if (!path)
4693 return -ENOMEM;
4694
4695 sctx->cur_inode_last_extent = 0;
4696
4697 key.objectid = sctx->cur_ino;
4698 key.type = BTRFS_EXTENT_DATA_KEY;
4699 key.offset = offset;
4700 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4701 if (ret < 0)
4702 goto out;
4703 ret = 0;
4704 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4705 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4706 goto out;
4707
4708 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4709 struct btrfs_file_extent_item);
4710 type = btrfs_file_extent_type(path->nodes[0], fi);
4711 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004712 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4713 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004714 extent_end = ALIGN(key.offset + size,
4715 sctx->send_root->sectorsize);
4716 } else {
4717 extent_end = key.offset +
4718 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4719 }
4720 sctx->cur_inode_last_extent = extent_end;
4721out:
4722 btrfs_free_path(path);
4723 return ret;
4724}
4725
4726static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4727 struct btrfs_key *key)
4728{
4729 struct btrfs_file_extent_item *fi;
4730 u64 extent_end;
4731 u8 type;
4732 int ret = 0;
4733
4734 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4735 return 0;
4736
4737 if (sctx->cur_inode_last_extent == (u64)-1) {
4738 ret = get_last_extent(sctx, key->offset - 1);
4739 if (ret)
4740 return ret;
4741 }
4742
4743 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4744 struct btrfs_file_extent_item);
4745 type = btrfs_file_extent_type(path->nodes[0], fi);
4746 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004747 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4748 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004749 extent_end = ALIGN(key->offset + size,
4750 sctx->send_root->sectorsize);
4751 } else {
4752 extent_end = key->offset +
4753 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4754 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00004755
4756 if (path->slots[0] == 0 &&
4757 sctx->cur_inode_last_extent < key->offset) {
4758 /*
4759 * We might have skipped entire leafs that contained only
4760 * file extent items for our current inode. These leafs have
4761 * a generation number smaller (older) than the one in the
4762 * current leaf and the leaf our last extent came from, and
4763 * are located between these 2 leafs.
4764 */
4765 ret = get_last_extent(sctx, key->offset - 1);
4766 if (ret)
4767 return ret;
4768 }
4769
Josef Bacik16e75492013-10-22 12:18:51 -04004770 if (sctx->cur_inode_last_extent < key->offset)
4771 ret = send_hole(sctx, key->offset);
4772 sctx->cur_inode_last_extent = extent_end;
4773 return ret;
4774}
4775
Alexander Block31db9f72012-07-25 23:19:24 +02004776static int process_extent(struct send_ctx *sctx,
4777 struct btrfs_path *path,
4778 struct btrfs_key *key)
4779{
Alexander Block31db9f72012-07-25 23:19:24 +02004780 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004781 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004782
4783 if (S_ISLNK(sctx->cur_inode_mode))
4784 return 0;
4785
4786 if (sctx->parent_root && !sctx->cur_inode_new) {
4787 ret = is_extent_unchanged(sctx, path, key);
4788 if (ret < 0)
4789 goto out;
4790 if (ret) {
4791 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004792 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004793 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004794 } else {
4795 struct btrfs_file_extent_item *ei;
4796 u8 type;
4797
4798 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4799 struct btrfs_file_extent_item);
4800 type = btrfs_file_extent_type(path->nodes[0], ei);
4801 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4802 type == BTRFS_FILE_EXTENT_REG) {
4803 /*
4804 * The send spec does not have a prealloc command yet,
4805 * so just leave a hole for prealloc'ed extents until
4806 * we have enough commands queued up to justify rev'ing
4807 * the send spec.
4808 */
4809 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4810 ret = 0;
4811 goto out;
4812 }
4813
4814 /* Have a hole, just skip it. */
4815 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4816 ret = 0;
4817 goto out;
4818 }
4819 }
Alexander Block31db9f72012-07-25 23:19:24 +02004820 }
4821
4822 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4823 sctx->cur_inode_size, &found_clone);
4824 if (ret != -ENOENT && ret < 0)
4825 goto out;
4826
4827 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004828 if (ret)
4829 goto out;
4830out_hole:
4831 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004832out:
4833 return ret;
4834}
4835
4836static int process_all_extents(struct send_ctx *sctx)
4837{
4838 int ret;
4839 struct btrfs_root *root;
4840 struct btrfs_path *path;
4841 struct btrfs_key key;
4842 struct btrfs_key found_key;
4843 struct extent_buffer *eb;
4844 int slot;
4845
4846 root = sctx->send_root;
4847 path = alloc_path_for_send();
4848 if (!path)
4849 return -ENOMEM;
4850
4851 key.objectid = sctx->cmp_key->objectid;
4852 key.type = BTRFS_EXTENT_DATA_KEY;
4853 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004854 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4855 if (ret < 0)
4856 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004857
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004858 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004859 eb = path->nodes[0];
4860 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004861
4862 if (slot >= btrfs_header_nritems(eb)) {
4863 ret = btrfs_next_leaf(root, path);
4864 if (ret < 0) {
4865 goto out;
4866 } else if (ret > 0) {
4867 ret = 0;
4868 break;
4869 }
4870 continue;
4871 }
4872
Alexander Block31db9f72012-07-25 23:19:24 +02004873 btrfs_item_key_to_cpu(eb, &found_key, slot);
4874
4875 if (found_key.objectid != key.objectid ||
4876 found_key.type != key.type) {
4877 ret = 0;
4878 goto out;
4879 }
4880
4881 ret = process_extent(sctx, path, &found_key);
4882 if (ret < 0)
4883 goto out;
4884
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004885 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004886 }
4887
4888out:
4889 btrfs_free_path(path);
4890 return ret;
4891}
4892
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004893static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4894 int *pending_move,
4895 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02004896{
4897 int ret = 0;
4898
4899 if (sctx->cur_ino == 0)
4900 goto out;
4901 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004902 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004903 goto out;
4904 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4905 goto out;
4906
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004907 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004908 if (ret < 0)
4909 goto out;
4910
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004911 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004912out:
4913 return ret;
4914}
4915
4916static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4917{
4918 int ret = 0;
4919 u64 left_mode;
4920 u64 left_uid;
4921 u64 left_gid;
4922 u64 right_mode;
4923 u64 right_uid;
4924 u64 right_gid;
4925 int need_chmod = 0;
4926 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004927 int pending_move = 0;
4928 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004929
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004930 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4931 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02004932 if (ret < 0)
4933 goto out;
4934
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004935 /*
4936 * We have processed the refs and thus need to advance send_progress.
4937 * Now, calls to get_cur_xxx will take the updated refs of the current
4938 * inode into account.
4939 *
4940 * On the other hand, if our current inode is a directory and couldn't
4941 * be moved/renamed because its parent was renamed/moved too and it has
4942 * a higher inode number, we can only move/rename our current inode
4943 * after we moved/renamed its parent. Therefore in this case operate on
4944 * the old path (pre move/rename) of our current inode, and the
4945 * move/rename will be performed later.
4946 */
4947 if (refs_processed && !pending_move)
4948 sctx->send_progress = sctx->cur_ino + 1;
4949
Alexander Block31db9f72012-07-25 23:19:24 +02004950 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4951 goto out;
4952 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4953 goto out;
4954
4955 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004956 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004957 if (ret < 0)
4958 goto out;
4959
Alex Lyakase2d044f2012-10-17 13:52:47 +00004960 if (!sctx->parent_root || sctx->cur_inode_new) {
4961 need_chown = 1;
4962 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004963 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004964 } else {
4965 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4966 NULL, NULL, &right_mode, &right_uid,
4967 &right_gid, NULL);
4968 if (ret < 0)
4969 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004970
Alex Lyakase2d044f2012-10-17 13:52:47 +00004971 if (left_uid != right_uid || left_gid != right_gid)
4972 need_chown = 1;
4973 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4974 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004975 }
4976
4977 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04004978 if (need_send_hole(sctx)) {
Filipe Manana766b5e52014-03-30 23:02:53 +01004979 if (sctx->cur_inode_last_extent == (u64)-1 ||
4980 sctx->cur_inode_last_extent <
4981 sctx->cur_inode_size) {
Josef Bacik16e75492013-10-22 12:18:51 -04004982 ret = get_last_extent(sctx, (u64)-1);
4983 if (ret)
4984 goto out;
4985 }
4986 if (sctx->cur_inode_last_extent <
4987 sctx->cur_inode_size) {
4988 ret = send_hole(sctx, sctx->cur_inode_size);
4989 if (ret)
4990 goto out;
4991 }
4992 }
Alexander Block31db9f72012-07-25 23:19:24 +02004993 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4994 sctx->cur_inode_size);
4995 if (ret < 0)
4996 goto out;
4997 }
4998
4999 if (need_chown) {
5000 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5001 left_uid, left_gid);
5002 if (ret < 0)
5003 goto out;
5004 }
5005 if (need_chmod) {
5006 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5007 left_mode);
5008 if (ret < 0)
5009 goto out;
5010 }
5011
5012 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005013 * If other directory inodes depended on our current directory
5014 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02005015 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005016 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5017 ret = apply_children_dir_moves(sctx);
5018 if (ret)
5019 goto out;
Filipe Mananafcbd2152014-03-03 12:28:40 +00005020 /*
5021 * Need to send that every time, no matter if it actually
5022 * changed between the two trees as we have done changes to
5023 * the inode before. If our inode is a directory and it's
5024 * waiting to be moved/renamed, we will send its utimes when
5025 * it's moved/renamed, therefore we don't need to do it here.
5026 */
5027 sctx->send_progress = sctx->cur_ino + 1;
5028 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5029 if (ret < 0)
5030 goto out;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005031 }
5032
Alexander Block31db9f72012-07-25 23:19:24 +02005033out:
5034 return ret;
5035}
5036
5037static int changed_inode(struct send_ctx *sctx,
5038 enum btrfs_compare_tree_result result)
5039{
5040 int ret = 0;
5041 struct btrfs_key *key = sctx->cmp_key;
5042 struct btrfs_inode_item *left_ii = NULL;
5043 struct btrfs_inode_item *right_ii = NULL;
5044 u64 left_gen = 0;
5045 u64 right_gen = 0;
5046
Alexander Block31db9f72012-07-25 23:19:24 +02005047 sctx->cur_ino = key->objectid;
5048 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005049 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005050
5051 /*
5052 * Set send_progress to current inode. This will tell all get_cur_xxx
5053 * functions that the current inode's refs are not updated yet. Later,
5054 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5055 */
Alexander Block31db9f72012-07-25 23:19:24 +02005056 sctx->send_progress = sctx->cur_ino;
5057
5058 if (result == BTRFS_COMPARE_TREE_NEW ||
5059 result == BTRFS_COMPARE_TREE_CHANGED) {
5060 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5061 sctx->left_path->slots[0],
5062 struct btrfs_inode_item);
5063 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5064 left_ii);
5065 } else {
5066 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5067 sctx->right_path->slots[0],
5068 struct btrfs_inode_item);
5069 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5070 right_ii);
5071 }
5072 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5073 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5074 sctx->right_path->slots[0],
5075 struct btrfs_inode_item);
5076
5077 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5078 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02005079
5080 /*
5081 * The cur_ino = root dir case is special here. We can't treat
5082 * the inode as deleted+reused because it would generate a
5083 * stream that tries to delete/mkdir the root dir.
5084 */
5085 if (left_gen != right_gen &&
5086 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02005087 sctx->cur_inode_new_gen = 1;
5088 }
5089
5090 if (result == BTRFS_COMPARE_TREE_NEW) {
5091 sctx->cur_inode_gen = left_gen;
5092 sctx->cur_inode_new = 1;
5093 sctx->cur_inode_deleted = 0;
5094 sctx->cur_inode_size = btrfs_inode_size(
5095 sctx->left_path->nodes[0], left_ii);
5096 sctx->cur_inode_mode = btrfs_inode_mode(
5097 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005098 sctx->cur_inode_rdev = btrfs_inode_rdev(
5099 sctx->left_path->nodes[0], left_ii);
Alexander Block31db9f72012-07-25 23:19:24 +02005100 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02005101 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005102 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5103 sctx->cur_inode_gen = right_gen;
5104 sctx->cur_inode_new = 0;
5105 sctx->cur_inode_deleted = 1;
5106 sctx->cur_inode_size = btrfs_inode_size(
5107 sctx->right_path->nodes[0], right_ii);
5108 sctx->cur_inode_mode = btrfs_inode_mode(
5109 sctx->right_path->nodes[0], right_ii);
5110 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02005111 /*
5112 * We need to do some special handling in case the inode was
5113 * reported as changed with a changed generation number. This
5114 * means that the original inode was deleted and new inode
5115 * reused the same inum. So we have to treat the old inode as
5116 * deleted and the new one as new.
5117 */
Alexander Block31db9f72012-07-25 23:19:24 +02005118 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02005119 /*
5120 * First, process the inode as if it was deleted.
5121 */
Alexander Block31db9f72012-07-25 23:19:24 +02005122 sctx->cur_inode_gen = right_gen;
5123 sctx->cur_inode_new = 0;
5124 sctx->cur_inode_deleted = 1;
5125 sctx->cur_inode_size = btrfs_inode_size(
5126 sctx->right_path->nodes[0], right_ii);
5127 sctx->cur_inode_mode = btrfs_inode_mode(
5128 sctx->right_path->nodes[0], right_ii);
5129 ret = process_all_refs(sctx,
5130 BTRFS_COMPARE_TREE_DELETED);
5131 if (ret < 0)
5132 goto out;
5133
Alexander Block766702e2012-07-28 14:11:31 +02005134 /*
5135 * Now process the inode as if it was new.
5136 */
Alexander Block31db9f72012-07-25 23:19:24 +02005137 sctx->cur_inode_gen = left_gen;
5138 sctx->cur_inode_new = 1;
5139 sctx->cur_inode_deleted = 0;
5140 sctx->cur_inode_size = btrfs_inode_size(
5141 sctx->left_path->nodes[0], left_ii);
5142 sctx->cur_inode_mode = btrfs_inode_mode(
5143 sctx->left_path->nodes[0], left_ii);
Liu Bo644d1942014-02-27 17:29:01 +08005144 sctx->cur_inode_rdev = btrfs_inode_rdev(
5145 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02005146 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02005147 if (ret < 0)
5148 goto out;
5149
5150 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5151 if (ret < 0)
5152 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02005153 /*
5154 * Advance send_progress now as we did not get into
5155 * process_recorded_refs_if_needed in the new_gen case.
5156 */
5157 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02005158
5159 /*
5160 * Now process all extents and xattrs of the inode as if
5161 * they were all new.
5162 */
Alexander Block31db9f72012-07-25 23:19:24 +02005163 ret = process_all_extents(sctx);
5164 if (ret < 0)
5165 goto out;
5166 ret = process_all_new_xattrs(sctx);
5167 if (ret < 0)
5168 goto out;
5169 } else {
5170 sctx->cur_inode_gen = left_gen;
5171 sctx->cur_inode_new = 0;
5172 sctx->cur_inode_new_gen = 0;
5173 sctx->cur_inode_deleted = 0;
5174 sctx->cur_inode_size = btrfs_inode_size(
5175 sctx->left_path->nodes[0], left_ii);
5176 sctx->cur_inode_mode = btrfs_inode_mode(
5177 sctx->left_path->nodes[0], left_ii);
5178 }
5179 }
5180
5181out:
5182 return ret;
5183}
5184
Alexander Block766702e2012-07-28 14:11:31 +02005185/*
5186 * We have to process new refs before deleted refs, but compare_trees gives us
5187 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5188 * first and later process them in process_recorded_refs.
5189 * For the cur_inode_new_gen case, we skip recording completely because
5190 * changed_inode did already initiate processing of refs. The reason for this is
5191 * that in this case, compare_tree actually compares the refs of 2 different
5192 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5193 * refs of the right tree as deleted and all refs of the left tree as new.
5194 */
Alexander Block31db9f72012-07-25 23:19:24 +02005195static int changed_ref(struct send_ctx *sctx,
5196 enum btrfs_compare_tree_result result)
5197{
5198 int ret = 0;
5199
5200 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5201
5202 if (!sctx->cur_inode_new_gen &&
5203 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5204 if (result == BTRFS_COMPARE_TREE_NEW)
5205 ret = record_new_ref(sctx);
5206 else if (result == BTRFS_COMPARE_TREE_DELETED)
5207 ret = record_deleted_ref(sctx);
5208 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5209 ret = record_changed_ref(sctx);
5210 }
5211
5212 return ret;
5213}
5214
Alexander Block766702e2012-07-28 14:11:31 +02005215/*
5216 * Process new/deleted/changed xattrs. We skip processing in the
5217 * cur_inode_new_gen case because changed_inode did already initiate processing
5218 * of xattrs. The reason is the same as in changed_ref
5219 */
Alexander Block31db9f72012-07-25 23:19:24 +02005220static int changed_xattr(struct send_ctx *sctx,
5221 enum btrfs_compare_tree_result result)
5222{
5223 int ret = 0;
5224
5225 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5226
5227 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5228 if (result == BTRFS_COMPARE_TREE_NEW)
5229 ret = process_new_xattr(sctx);
5230 else if (result == BTRFS_COMPARE_TREE_DELETED)
5231 ret = process_deleted_xattr(sctx);
5232 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5233 ret = process_changed_xattr(sctx);
5234 }
5235
5236 return ret;
5237}
5238
Alexander Block766702e2012-07-28 14:11:31 +02005239/*
5240 * Process new/deleted/changed extents. We skip processing in the
5241 * cur_inode_new_gen case because changed_inode did already initiate processing
5242 * of extents. The reason is the same as in changed_ref
5243 */
Alexander Block31db9f72012-07-25 23:19:24 +02005244static int changed_extent(struct send_ctx *sctx,
5245 enum btrfs_compare_tree_result result)
5246{
5247 int ret = 0;
5248
5249 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5250
5251 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5252 if (result != BTRFS_COMPARE_TREE_DELETED)
5253 ret = process_extent(sctx, sctx->left_path,
5254 sctx->cmp_key);
5255 }
5256
5257 return ret;
5258}
5259
Josef Bacikba5e8f22013-08-16 16:52:55 -04005260static int dir_changed(struct send_ctx *sctx, u64 dir)
5261{
5262 u64 orig_gen, new_gen;
5263 int ret;
5264
5265 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5266 NULL, NULL);
5267 if (ret)
5268 return ret;
5269
5270 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5271 NULL, NULL, NULL);
5272 if (ret)
5273 return ret;
5274
5275 return (orig_gen != new_gen) ? 1 : 0;
5276}
5277
5278static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5279 struct btrfs_key *key)
5280{
5281 struct btrfs_inode_extref *extref;
5282 struct extent_buffer *leaf;
5283 u64 dirid = 0, last_dirid = 0;
5284 unsigned long ptr;
5285 u32 item_size;
5286 u32 cur_offset = 0;
5287 int ref_name_len;
5288 int ret = 0;
5289
5290 /* Easy case, just check this one dirid */
5291 if (key->type == BTRFS_INODE_REF_KEY) {
5292 dirid = key->offset;
5293
5294 ret = dir_changed(sctx, dirid);
5295 goto out;
5296 }
5297
5298 leaf = path->nodes[0];
5299 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5300 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5301 while (cur_offset < item_size) {
5302 extref = (struct btrfs_inode_extref *)(ptr +
5303 cur_offset);
5304 dirid = btrfs_inode_extref_parent(leaf, extref);
5305 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5306 cur_offset += ref_name_len + sizeof(*extref);
5307 if (dirid == last_dirid)
5308 continue;
5309 ret = dir_changed(sctx, dirid);
5310 if (ret)
5311 break;
5312 last_dirid = dirid;
5313 }
5314out:
5315 return ret;
5316}
5317
Alexander Block766702e2012-07-28 14:11:31 +02005318/*
5319 * Updates compare related fields in sctx and simply forwards to the actual
5320 * changed_xxx functions.
5321 */
Alexander Block31db9f72012-07-25 23:19:24 +02005322static int changed_cb(struct btrfs_root *left_root,
5323 struct btrfs_root *right_root,
5324 struct btrfs_path *left_path,
5325 struct btrfs_path *right_path,
5326 struct btrfs_key *key,
5327 enum btrfs_compare_tree_result result,
5328 void *ctx)
5329{
5330 int ret = 0;
5331 struct send_ctx *sctx = ctx;
5332
Josef Bacikba5e8f22013-08-16 16:52:55 -04005333 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005334 if (key->type == BTRFS_INODE_REF_KEY ||
5335 key->type == BTRFS_INODE_EXTREF_KEY) {
5336 ret = compare_refs(sctx, left_path, key);
5337 if (!ret)
5338 return 0;
5339 if (ret < 0)
5340 return ret;
5341 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5342 return maybe_send_hole(sctx, left_path, key);
5343 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005344 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005345 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005346 result = BTRFS_COMPARE_TREE_CHANGED;
5347 ret = 0;
5348 }
5349
Alexander Block31db9f72012-07-25 23:19:24 +02005350 sctx->left_path = left_path;
5351 sctx->right_path = right_path;
5352 sctx->cmp_key = key;
5353
5354 ret = finish_inode_if_needed(sctx, 0);
5355 if (ret < 0)
5356 goto out;
5357
Alexander Block2981e222012-08-01 14:47:03 +02005358 /* Ignore non-FS objects */
5359 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5360 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5361 goto out;
5362
Alexander Block31db9f72012-07-25 23:19:24 +02005363 if (key->type == BTRFS_INODE_ITEM_KEY)
5364 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005365 else if (key->type == BTRFS_INODE_REF_KEY ||
5366 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005367 ret = changed_ref(sctx, result);
5368 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5369 ret = changed_xattr(sctx, result);
5370 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5371 ret = changed_extent(sctx, result);
5372
5373out:
5374 return ret;
5375}
5376
5377static int full_send_tree(struct send_ctx *sctx)
5378{
5379 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005380 struct btrfs_root *send_root = sctx->send_root;
5381 struct btrfs_key key;
5382 struct btrfs_key found_key;
5383 struct btrfs_path *path;
5384 struct extent_buffer *eb;
5385 int slot;
Alexander Block31db9f72012-07-25 23:19:24 +02005386
5387 path = alloc_path_for_send();
5388 if (!path)
5389 return -ENOMEM;
5390
Alexander Block31db9f72012-07-25 23:19:24 +02005391 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5392 key.type = BTRFS_INODE_ITEM_KEY;
5393 key.offset = 0;
5394
Alexander Block31db9f72012-07-25 23:19:24 +02005395 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5396 if (ret < 0)
5397 goto out;
5398 if (ret)
5399 goto out_finish;
5400
5401 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005402 eb = path->nodes[0];
5403 slot = path->slots[0];
5404 btrfs_item_key_to_cpu(eb, &found_key, slot);
5405
5406 ret = changed_cb(send_root, NULL, path, NULL,
5407 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5408 if (ret < 0)
5409 goto out;
5410
5411 key.objectid = found_key.objectid;
5412 key.type = found_key.type;
5413 key.offset = found_key.offset + 1;
5414
5415 ret = btrfs_next_item(send_root, path);
5416 if (ret < 0)
5417 goto out;
5418 if (ret) {
5419 ret = 0;
5420 break;
5421 }
5422 }
5423
5424out_finish:
5425 ret = finish_inode_if_needed(sctx, 1);
5426
5427out:
5428 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02005429 return ret;
5430}
5431
5432static int send_subvol(struct send_ctx *sctx)
5433{
5434 int ret;
5435
Stefan Behrensc2c71322013-04-10 17:10:52 +00005436 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5437 ret = send_header(sctx);
5438 if (ret < 0)
5439 goto out;
5440 }
Alexander Block31db9f72012-07-25 23:19:24 +02005441
5442 ret = send_subvol_begin(sctx);
5443 if (ret < 0)
5444 goto out;
5445
5446 if (sctx->parent_root) {
5447 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5448 changed_cb, sctx);
5449 if (ret < 0)
5450 goto out;
5451 ret = finish_inode_if_needed(sctx, 1);
5452 if (ret < 0)
5453 goto out;
5454 } else {
5455 ret = full_send_tree(sctx);
5456 if (ret < 0)
5457 goto out;
5458 }
5459
5460out:
Alexander Block31db9f72012-07-25 23:19:24 +02005461 free_recorded_refs(sctx);
5462 return ret;
5463}
5464
David Sterba66ef7d62013-12-17 15:07:20 +01005465static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5466{
5467 spin_lock(&root->root_item_lock);
5468 root->send_in_progress--;
5469 /*
5470 * Not much left to do, we don't know why it's unbalanced and
5471 * can't blindly reset it to 0.
5472 */
5473 if (root->send_in_progress < 0)
5474 btrfs_err(root->fs_info,
5475 "send_in_progres unbalanced %d root %llu\n",
5476 root->send_in_progress, root->root_key.objectid);
5477 spin_unlock(&root->root_item_lock);
5478}
5479
Alexander Block31db9f72012-07-25 23:19:24 +02005480long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5481{
5482 int ret = 0;
5483 struct btrfs_root *send_root;
5484 struct btrfs_root *clone_root;
5485 struct btrfs_fs_info *fs_info;
5486 struct btrfs_ioctl_send_args *arg = NULL;
5487 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02005488 struct send_ctx *sctx = NULL;
5489 u32 i;
5490 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01005491 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08005492 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08005493 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02005494
5495 if (!capable(CAP_SYS_ADMIN))
5496 return -EPERM;
5497
Al Viro496ad9a2013-01-23 17:07:38 -05005498 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02005499 fs_info = send_root->fs_info;
5500
Josef Bacik139f8072013-05-20 11:26:50 -04005501 /*
David Sterba2c686532013-12-16 17:34:17 +01005502 * The subvolume must remain read-only during send, protect against
David Sterba521e0542014-04-15 16:41:44 +02005503 * making it RW. This also protects against deletion.
David Sterba2c686532013-12-16 17:34:17 +01005504 */
5505 spin_lock(&send_root->root_item_lock);
5506 send_root->send_in_progress++;
5507 spin_unlock(&send_root->root_item_lock);
5508
5509 /*
Josef Bacik139f8072013-05-20 11:26:50 -04005510 * This is done when we lookup the root, it should already be complete
5511 * by the time we get here.
5512 */
5513 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5514
5515 /*
David Sterba2c686532013-12-16 17:34:17 +01005516 * Userspace tools do the checks and warn the user if it's
5517 * not RO.
5518 */
5519 if (!btrfs_root_readonly(send_root)) {
5520 ret = -EPERM;
5521 goto out;
5522 }
5523
Alexander Block31db9f72012-07-25 23:19:24 +02005524 arg = memdup_user(arg_, sizeof(*arg));
5525 if (IS_ERR(arg)) {
5526 ret = PTR_ERR(arg);
5527 arg = NULL;
5528 goto out;
5529 }
5530
5531 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05005532 sizeof(*arg->clone_sources) *
5533 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02005534 ret = -EFAULT;
5535 goto out;
5536 }
5537
Stefan Behrensc2c71322013-04-10 17:10:52 +00005538 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005539 ret = -EINVAL;
5540 goto out;
5541 }
5542
Alexander Block31db9f72012-07-25 23:19:24 +02005543 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5544 if (!sctx) {
5545 ret = -ENOMEM;
5546 goto out;
5547 }
5548
5549 INIT_LIST_HEAD(&sctx->new_refs);
5550 INIT_LIST_HEAD(&sctx->deleted_refs);
5551 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5552 INIT_LIST_HEAD(&sctx->name_cache_list);
5553
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005554 sctx->flags = arg->flags;
5555
Alexander Block31db9f72012-07-25 23:19:24 +02005556 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00005557 if (!sctx->send_filp) {
5558 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02005559 goto out;
5560 }
5561
Alexander Block31db9f72012-07-25 23:19:24 +02005562 sctx->send_root = send_root;
David Sterba521e0542014-04-15 16:41:44 +02005563 /*
5564 * Unlikely but possible, if the subvolume is marked for deletion but
5565 * is slow to remove the directory entry, send can still be started
5566 */
5567 if (btrfs_root_dead(sctx->send_root)) {
5568 ret = -EPERM;
5569 goto out;
5570 }
5571
Alexander Block31db9f72012-07-25 23:19:24 +02005572 sctx->clone_roots_cnt = arg->clone_sources_count;
5573
5574 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5575 sctx->send_buf = vmalloc(sctx->send_max_size);
5576 if (!sctx->send_buf) {
5577 ret = -ENOMEM;
5578 goto out;
5579 }
5580
5581 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5582 if (!sctx->read_buf) {
5583 ret = -ENOMEM;
5584 goto out;
5585 }
5586
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005587 sctx->pending_dir_moves = RB_ROOT;
5588 sctx->waiting_dir_moves = RB_ROOT;
Filipe Manana9dc44212014-02-19 14:31:44 +00005589 sctx->orphan_dirs = RB_ROOT;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005590
Alexander Block31db9f72012-07-25 23:19:24 +02005591 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5592 (arg->clone_sources_count + 1));
5593 if (!sctx->clone_roots) {
5594 ret = -ENOMEM;
5595 goto out;
5596 }
5597
5598 if (arg->clone_sources_count) {
5599 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5600 sizeof(*arg->clone_sources));
5601 if (!clone_sources_tmp) {
5602 ret = -ENOMEM;
5603 goto out;
5604 }
5605
5606 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5607 arg->clone_sources_count *
5608 sizeof(*arg->clone_sources));
5609 if (ret) {
5610 ret = -EFAULT;
5611 goto out;
5612 }
5613
5614 for (i = 0; i < arg->clone_sources_count; i++) {
5615 key.objectid = clone_sources_tmp[i];
5616 key.type = BTRFS_ROOT_ITEM_KEY;
5617 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005618
5619 index = srcu_read_lock(&fs_info->subvol_srcu);
5620
Alexander Block31db9f72012-07-25 23:19:24 +02005621 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02005622 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005623 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005624 ret = PTR_ERR(clone_root);
5625 goto out;
5626 }
David Sterba2c686532013-12-16 17:34:17 +01005627 clone_sources_to_rollback = i + 1;
5628 spin_lock(&clone_root->root_item_lock);
5629 clone_root->send_in_progress++;
5630 if (!btrfs_root_readonly(clone_root)) {
5631 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005632 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005633 ret = -EPERM;
5634 goto out;
5635 }
5636 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005637 srcu_read_unlock(&fs_info->subvol_srcu, index);
5638
Alexander Block31db9f72012-07-25 23:19:24 +02005639 sctx->clone_roots[i].root = clone_root;
5640 }
5641 vfree(clone_sources_tmp);
5642 clone_sources_tmp = NULL;
5643 }
5644
5645 if (arg->parent_root) {
5646 key.objectid = arg->parent_root;
5647 key.type = BTRFS_ROOT_ITEM_KEY;
5648 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005649
5650 index = srcu_read_lock(&fs_info->subvol_srcu);
5651
Alexander Block31db9f72012-07-25 23:19:24 +02005652 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005653 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005654 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005655 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02005656 goto out;
5657 }
Wang Shilong18f687d2014-01-07 17:25:19 +08005658
David Sterba2c686532013-12-16 17:34:17 +01005659 spin_lock(&sctx->parent_root->root_item_lock);
5660 sctx->parent_root->send_in_progress++;
David Sterba521e0542014-04-15 16:41:44 +02005661 if (!btrfs_root_readonly(sctx->parent_root) ||
5662 btrfs_root_dead(sctx->parent_root)) {
David Sterba2c686532013-12-16 17:34:17 +01005663 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005664 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005665 ret = -EPERM;
5666 goto out;
5667 }
5668 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005669
5670 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005671 }
5672
5673 /*
5674 * Clones from send_root are allowed, but only if the clone source
5675 * is behind the current send position. This is checked while searching
5676 * for possible clone sources.
5677 */
5678 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5679
5680 /* We do a bsearch later */
5681 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5682 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5683 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08005684 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005685
Josef Bacika26e8c92014-03-28 17:07:27 -04005686 current->journal_info = (void *)BTRFS_SEND_TRANS_STUB;
Alexander Block31db9f72012-07-25 23:19:24 +02005687 ret = send_subvol(sctx);
Josef Bacika26e8c92014-03-28 17:07:27 -04005688 current->journal_info = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02005689 if (ret < 0)
5690 goto out;
5691
Stefan Behrensc2c71322013-04-10 17:10:52 +00005692 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5693 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5694 if (ret < 0)
5695 goto out;
5696 ret = send_cmd(sctx);
5697 if (ret < 0)
5698 goto out;
5699 }
Alexander Block31db9f72012-07-25 23:19:24 +02005700
5701out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005702 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5703 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5704 struct rb_node *n;
5705 struct pending_dir_move *pm;
5706
5707 n = rb_first(&sctx->pending_dir_moves);
5708 pm = rb_entry(n, struct pending_dir_move, node);
5709 while (!list_empty(&pm->list)) {
5710 struct pending_dir_move *pm2;
5711
5712 pm2 = list_first_entry(&pm->list,
5713 struct pending_dir_move, list);
5714 free_pending_move(sctx, pm2);
5715 }
5716 free_pending_move(sctx, pm);
5717 }
5718
5719 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5720 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5721 struct rb_node *n;
5722 struct waiting_dir_move *dm;
5723
5724 n = rb_first(&sctx->waiting_dir_moves);
5725 dm = rb_entry(n, struct waiting_dir_move, node);
5726 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5727 kfree(dm);
5728 }
5729
Filipe Manana9dc44212014-02-19 14:31:44 +00005730 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5731 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5732 struct rb_node *n;
5733 struct orphan_dir_info *odi;
5734
5735 n = rb_first(&sctx->orphan_dirs);
5736 odi = rb_entry(n, struct orphan_dir_info, node);
5737 free_orphan_dir_info(sctx, odi);
5738 }
5739
Wang Shilong896c14f2014-01-07 17:25:18 +08005740 if (sort_clone_roots) {
5741 for (i = 0; i < sctx->clone_roots_cnt; i++)
5742 btrfs_root_dec_send_in_progress(
5743 sctx->clone_roots[i].root);
5744 } else {
5745 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5746 btrfs_root_dec_send_in_progress(
5747 sctx->clone_roots[i].root);
5748
5749 btrfs_root_dec_send_in_progress(send_root);
5750 }
David Sterba66ef7d62013-12-17 15:07:20 +01005751 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5752 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01005753
Alexander Block31db9f72012-07-25 23:19:24 +02005754 kfree(arg);
5755 vfree(clone_sources_tmp);
5756
5757 if (sctx) {
5758 if (sctx->send_filp)
5759 fput(sctx->send_filp);
5760
5761 vfree(sctx->clone_roots);
5762 vfree(sctx->send_buf);
5763 vfree(sctx->read_buf);
5764
5765 name_cache_free(sctx);
5766
5767 kfree(sctx);
5768 }
5769
5770 return ret;
5771}