blob: 4405aae05281fe9689110f78d5e29e56a3100f5e [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 };
60 char pad[PAGE_SIZE];
61 };
62};
63#define FS_PATH_INLINE_SIZE \
64 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
65
66
67/* reused for each extent */
68struct clone_root {
69 struct btrfs_root *root;
70 u64 ino;
71 u64 offset;
72
73 u64 found_refs;
74};
75
76#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
77#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
78
79struct send_ctx {
80 struct file *send_filp;
81 loff_t send_off;
82 char *send_buf;
83 u32 send_size;
84 u32 send_max_size;
85 u64 total_send_size;
86 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000087 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020088
Alexander Block31db9f72012-07-25 23:19:24 +020089 struct btrfs_root *send_root;
90 struct btrfs_root *parent_root;
91 struct clone_root *clone_roots;
92 int clone_roots_cnt;
93
94 /* current state of the compare_tree call */
95 struct btrfs_path *left_path;
96 struct btrfs_path *right_path;
97 struct btrfs_key *cmp_key;
98
99 /*
100 * infos of the currently processed inode. In case of deleted inodes,
101 * these are the values from the deleted inode.
102 */
103 u64 cur_ino;
104 u64 cur_inode_gen;
105 int cur_inode_new;
106 int cur_inode_new_gen;
107 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200108 u64 cur_inode_size;
109 u64 cur_inode_mode;
Josef Bacik16e75492013-10-22 12:18:51 -0400110 u64 cur_inode_last_extent;
Alexander Block31db9f72012-07-25 23:19:24 +0200111
112 u64 send_progress;
113
114 struct list_head new_refs;
115 struct list_head deleted_refs;
116
117 struct radix_tree_root name_cache;
118 struct list_head name_cache_list;
119 int name_cache_size;
120
Alexander Block31db9f72012-07-25 23:19:24 +0200121 char *read_buf;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000122
123 /*
124 * We process inodes by their increasing order, so if before an
125 * incremental send we reverse the parent/child relationship of
126 * directories such that a directory with a lower inode number was
127 * the parent of a directory with a higher inode number, and the one
128 * becoming the new parent got renamed too, we can't rename/move the
129 * directory with lower inode number when we finish processing it - we
130 * must process the directory with higher inode number first, then
131 * rename/move it and then rename/move the directory with lower inode
132 * number. Example follows.
133 *
134 * Tree state when the first send was performed:
135 *
136 * .
137 * |-- a (ino 257)
138 * |-- b (ino 258)
139 * |
140 * |
141 * |-- c (ino 259)
142 * | |-- d (ino 260)
143 * |
144 * |-- c2 (ino 261)
145 *
146 * Tree state when the second (incremental) send is performed:
147 *
148 * .
149 * |-- a (ino 257)
150 * |-- b (ino 258)
151 * |-- c2 (ino 261)
152 * |-- d2 (ino 260)
153 * |-- cc (ino 259)
154 *
155 * The sequence of steps that lead to the second state was:
156 *
157 * mv /a/b/c/d /a/b/c2/d2
158 * mv /a/b/c /a/b/c2/d2/cc
159 *
160 * "c" has lower inode number, but we can't move it (2nd mv operation)
161 * before we move "d", which has higher inode number.
162 *
163 * So we just memorize which move/rename operations must be performed
164 * later when their respective parent is processed and moved/renamed.
165 */
166
167 /* Indexed by parent directory inode number. */
168 struct rb_root pending_dir_moves;
169
170 /*
171 * Reverse index, indexed by the inode number of a directory that
172 * is waiting for the move/rename of its immediate parent before its
173 * own move/rename can be performed.
174 */
175 struct rb_root waiting_dir_moves;
176};
177
178struct pending_dir_move {
179 struct rb_node node;
180 struct list_head list;
181 u64 parent_ino;
182 u64 ino;
183 u64 gen;
184 struct list_head update_refs;
185};
186
187struct waiting_dir_move {
188 struct rb_node node;
189 u64 ino;
Alexander Block31db9f72012-07-25 23:19:24 +0200190};
191
192struct name_cache_entry {
193 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200194 /*
195 * radix_tree has only 32bit entries but we need to handle 64bit inums.
196 * We use the lower 32bit of the 64bit inum to store it in the tree. If
197 * more then one inum would fall into the same entry, we use radix_list
198 * to store the additional entries. radix_list is also used to store
199 * entries where two entries have the same inum but different
200 * generations.
201 */
202 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200203 u64 ino;
204 u64 gen;
205 u64 parent_ino;
206 u64 parent_gen;
207 int ret;
208 int need_later_update;
209 int name_len;
210 char name[];
211};
212
Filipe David Borba Manana9f037402014-01-22 10:00:53 +0000213static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
214
Josef Bacik16e75492013-10-22 12:18:51 -0400215static int need_send_hole(struct send_ctx *sctx)
216{
217 return (sctx->parent_root && !sctx->cur_inode_new &&
218 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
219 S_ISREG(sctx->cur_inode_mode));
220}
221
Alexander Block31db9f72012-07-25 23:19:24 +0200222static void fs_path_reset(struct fs_path *p)
223{
224 if (p->reversed) {
225 p->start = p->buf + p->buf_len - 1;
226 p->end = p->start;
227 *p->start = 0;
228 } else {
229 p->start = p->buf;
230 p->end = p->start;
231 *p->start = 0;
232 }
233}
234
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000235static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200236{
237 struct fs_path *p;
238
239 p = kmalloc(sizeof(*p), GFP_NOFS);
240 if (!p)
241 return NULL;
242 p->reversed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200243 p->buf = p->inline_buf;
244 p->buf_len = FS_PATH_INLINE_SIZE;
245 fs_path_reset(p);
246 return p;
247}
248
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000249static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200250{
251 struct fs_path *p;
252
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000253 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200254 if (!p)
255 return NULL;
256 p->reversed = 1;
257 fs_path_reset(p);
258 return p;
259}
260
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000261static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200262{
263 if (!p)
264 return;
265 if (p->buf != p->inline_buf) {
David Sterbae25a8122014-02-03 19:23:33 +0100266 if (is_vmalloc_addr(p->buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200267 vfree(p->buf);
268 else
269 kfree(p->buf);
270 }
271 kfree(p);
272}
273
274static int fs_path_len(struct fs_path *p)
275{
276 return p->end - p->start;
277}
278
279static int fs_path_ensure_buf(struct fs_path *p, int len)
280{
281 char *tmp_buf;
282 int path_len;
283 int old_buf_len;
284
285 len++;
286
287 if (p->buf_len >= len)
288 return 0;
289
290 path_len = p->end - p->start;
291 old_buf_len = p->buf_len;
292 len = PAGE_ALIGN(len);
293
294 if (p->buf == p->inline_buf) {
Joe Perches8be04b92013-06-19 12:15:53 -0700295 tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +0200296 if (!tmp_buf) {
297 tmp_buf = vmalloc(len);
298 if (!tmp_buf)
299 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +0200300 }
301 memcpy(tmp_buf, p->buf, p->buf_len);
302 p->buf = tmp_buf;
303 p->buf_len = len;
304 } else {
David Sterbae25a8122014-02-03 19:23:33 +0100305 if (is_vmalloc_addr(p->buf)) {
Alexander Block31db9f72012-07-25 23:19:24 +0200306 tmp_buf = vmalloc(len);
307 if (!tmp_buf)
308 return -ENOMEM;
309 memcpy(tmp_buf, p->buf, p->buf_len);
310 vfree(p->buf);
311 } else {
312 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
313 if (!tmp_buf) {
314 tmp_buf = vmalloc(len);
315 if (!tmp_buf)
316 return -ENOMEM;
317 memcpy(tmp_buf, p->buf, p->buf_len);
318 kfree(p->buf);
Alexander Block31db9f72012-07-25 23:19:24 +0200319 }
320 }
321 p->buf = tmp_buf;
322 p->buf_len = len;
323 }
324 if (p->reversed) {
325 tmp_buf = p->buf + old_buf_len - path_len - 1;
326 p->end = p->buf + p->buf_len - 1;
327 p->start = p->end - path_len;
328 memmove(p->start, tmp_buf, path_len + 1);
329 } else {
330 p->start = p->buf;
331 p->end = p->start + path_len;
332 }
333 return 0;
334}
335
David Sterbab23ab572014-02-03 19:23:19 +0100336static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
337 char **prepared)
Alexander Block31db9f72012-07-25 23:19:24 +0200338{
339 int ret;
340 int new_len;
341
342 new_len = p->end - p->start + name_len;
343 if (p->start != p->end)
344 new_len++;
345 ret = fs_path_ensure_buf(p, new_len);
346 if (ret < 0)
347 goto out;
348
349 if (p->reversed) {
350 if (p->start != p->end)
351 *--p->start = '/';
352 p->start -= name_len;
David Sterbab23ab572014-02-03 19:23:19 +0100353 *prepared = p->start;
Alexander Block31db9f72012-07-25 23:19:24 +0200354 } else {
355 if (p->start != p->end)
356 *p->end++ = '/';
David Sterbab23ab572014-02-03 19:23:19 +0100357 *prepared = p->end;
Alexander Block31db9f72012-07-25 23:19:24 +0200358 p->end += name_len;
359 *p->end = 0;
360 }
361
362out:
363 return ret;
364}
365
366static int fs_path_add(struct fs_path *p, const char *name, int name_len)
367{
368 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100369 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200370
David Sterbab23ab572014-02-03 19:23:19 +0100371 ret = fs_path_prepare_for_add(p, name_len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200372 if (ret < 0)
373 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100374 memcpy(prepared, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200375
376out:
377 return ret;
378}
379
380static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
381{
382 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100383 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200384
David Sterbab23ab572014-02-03 19:23:19 +0100385 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200386 if (ret < 0)
387 goto out;
David Sterbab23ab572014-02-03 19:23:19 +0100388 memcpy(prepared, p2->start, p2->end - p2->start);
Alexander Block31db9f72012-07-25 23:19:24 +0200389
390out:
391 return ret;
392}
393
394static int fs_path_add_from_extent_buffer(struct fs_path *p,
395 struct extent_buffer *eb,
396 unsigned long off, int len)
397{
398 int ret;
David Sterbab23ab572014-02-03 19:23:19 +0100399 char *prepared;
Alexander Block31db9f72012-07-25 23:19:24 +0200400
David Sterbab23ab572014-02-03 19:23:19 +0100401 ret = fs_path_prepare_for_add(p, len, &prepared);
Alexander Block31db9f72012-07-25 23:19:24 +0200402 if (ret < 0)
403 goto out;
404
David Sterbab23ab572014-02-03 19:23:19 +0100405 read_extent_buffer(eb, prepared, off, len);
Alexander Block31db9f72012-07-25 23:19:24 +0200406
407out:
408 return ret;
409}
410
Alexander Block31db9f72012-07-25 23:19:24 +0200411static int fs_path_copy(struct fs_path *p, struct fs_path *from)
412{
413 int ret;
414
415 p->reversed = from->reversed;
416 fs_path_reset(p);
417
418 ret = fs_path_add_path(p, from);
419
420 return ret;
421}
422
423
424static void fs_path_unreverse(struct fs_path *p)
425{
426 char *tmp;
427 int len;
428
429 if (!p->reversed)
430 return;
431
432 tmp = p->start;
433 len = p->end - p->start;
434 p->start = p->buf;
435 p->end = p->start + len;
436 memmove(p->start, tmp, len + 1);
437 p->reversed = 0;
438}
439
440static struct btrfs_path *alloc_path_for_send(void)
441{
442 struct btrfs_path *path;
443
444 path = btrfs_alloc_path();
445 if (!path)
446 return NULL;
447 path->search_commit_root = 1;
448 path->skip_locking = 1;
449 return path;
450}
451
Eric Sandeen48a3b632013-04-25 20:41:01 +0000452static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200453{
454 int ret;
455 mm_segment_t old_fs;
456 u32 pos = 0;
457
458 old_fs = get_fs();
459 set_fs(KERNEL_DS);
460
461 while (pos < len) {
Anand Jain1bcea352012-09-14 00:04:21 -0600462 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200463 /* TODO handle that correctly */
464 /*if (ret == -ERESTARTSYS) {
465 continue;
466 }*/
467 if (ret < 0)
468 goto out;
469 if (ret == 0) {
470 ret = -EIO;
471 goto out;
472 }
473 pos += ret;
474 }
475
476 ret = 0;
477
478out:
479 set_fs(old_fs);
480 return ret;
481}
482
483static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
484{
485 struct btrfs_tlv_header *hdr;
486 int total_len = sizeof(*hdr) + len;
487 int left = sctx->send_max_size - sctx->send_size;
488
489 if (unlikely(left < total_len))
490 return -EOVERFLOW;
491
492 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
493 hdr->tlv_type = cpu_to_le16(attr);
494 hdr->tlv_len = cpu_to_le16(len);
495 memcpy(hdr + 1, data, len);
496 sctx->send_size += total_len;
497
498 return 0;
499}
500
David Sterba95bc79d2013-12-16 17:34:10 +0100501#define TLV_PUT_DEFINE_INT(bits) \
502 static int tlv_put_u##bits(struct send_ctx *sctx, \
503 u##bits attr, u##bits value) \
504 { \
505 __le##bits __tmp = cpu_to_le##bits(value); \
506 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
507 }
Alexander Block31db9f72012-07-25 23:19:24 +0200508
David Sterba95bc79d2013-12-16 17:34:10 +0100509TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200510
511static int tlv_put_string(struct send_ctx *sctx, u16 attr,
512 const char *str, int len)
513{
514 if (len == -1)
515 len = strlen(str);
516 return tlv_put(sctx, attr, str, len);
517}
518
519static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
520 const u8 *uuid)
521{
522 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
523}
524
Alexander Block31db9f72012-07-25 23:19:24 +0200525static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
526 struct extent_buffer *eb,
527 struct btrfs_timespec *ts)
528{
529 struct btrfs_timespec bts;
530 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
531 return tlv_put(sctx, attr, &bts, sizeof(bts));
532}
533
534
535#define TLV_PUT(sctx, attrtype, attrlen, data) \
536 do { \
537 ret = tlv_put(sctx, attrtype, attrlen, data); \
538 if (ret < 0) \
539 goto tlv_put_failure; \
540 } while (0)
541
542#define TLV_PUT_INT(sctx, attrtype, bits, value) \
543 do { \
544 ret = tlv_put_u##bits(sctx, attrtype, value); \
545 if (ret < 0) \
546 goto tlv_put_failure; \
547 } while (0)
548
549#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
550#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
551#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
552#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
553#define TLV_PUT_STRING(sctx, attrtype, str, len) \
554 do { \
555 ret = tlv_put_string(sctx, attrtype, str, len); \
556 if (ret < 0) \
557 goto tlv_put_failure; \
558 } while (0)
559#define TLV_PUT_PATH(sctx, attrtype, p) \
560 do { \
561 ret = tlv_put_string(sctx, attrtype, p->start, \
562 p->end - p->start); \
563 if (ret < 0) \
564 goto tlv_put_failure; \
565 } while(0)
566#define TLV_PUT_UUID(sctx, attrtype, uuid) \
567 do { \
568 ret = tlv_put_uuid(sctx, attrtype, uuid); \
569 if (ret < 0) \
570 goto tlv_put_failure; \
571 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200572#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
573 do { \
574 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
575 if (ret < 0) \
576 goto tlv_put_failure; \
577 } while (0)
578
579static int send_header(struct send_ctx *sctx)
580{
581 struct btrfs_stream_header hdr;
582
583 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
584 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
585
Anand Jain1bcea352012-09-14 00:04:21 -0600586 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
587 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200588}
589
590/*
591 * For each command/item we want to send to userspace, we call this function.
592 */
593static int begin_cmd(struct send_ctx *sctx, int cmd)
594{
595 struct btrfs_cmd_header *hdr;
596
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530597 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200598 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200599
600 BUG_ON(sctx->send_size);
601
602 sctx->send_size += sizeof(*hdr);
603 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
604 hdr->cmd = cpu_to_le16(cmd);
605
606 return 0;
607}
608
609static int send_cmd(struct send_ctx *sctx)
610{
611 int ret;
612 struct btrfs_cmd_header *hdr;
613 u32 crc;
614
615 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
616 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
617 hdr->crc = 0;
618
Filipe David Borba Manana0b947af2014-01-29 21:06:04 +0000619 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
Alexander Block31db9f72012-07-25 23:19:24 +0200620 hdr->crc = cpu_to_le32(crc);
621
Anand Jain1bcea352012-09-14 00:04:21 -0600622 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
623 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200624
625 sctx->total_send_size += sctx->send_size;
626 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
627 sctx->send_size = 0;
628
629 return ret;
630}
631
632/*
633 * Sends a move instruction to user space
634 */
635static int send_rename(struct send_ctx *sctx,
636 struct fs_path *from, struct fs_path *to)
637{
638 int ret;
639
640verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
641
642 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
643 if (ret < 0)
644 goto out;
645
646 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
647 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
648
649 ret = send_cmd(sctx);
650
651tlv_put_failure:
652out:
653 return ret;
654}
655
656/*
657 * Sends a link instruction to user space
658 */
659static int send_link(struct send_ctx *sctx,
660 struct fs_path *path, struct fs_path *lnk)
661{
662 int ret;
663
664verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
665
666 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
667 if (ret < 0)
668 goto out;
669
670 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
671 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
672
673 ret = send_cmd(sctx);
674
675tlv_put_failure:
676out:
677 return ret;
678}
679
680/*
681 * Sends an unlink instruction to user space
682 */
683static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
684{
685 int ret;
686
687verbose_printk("btrfs: send_unlink %s\n", path->start);
688
689 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
690 if (ret < 0)
691 goto out;
692
693 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
694
695 ret = send_cmd(sctx);
696
697tlv_put_failure:
698out:
699 return ret;
700}
701
702/*
703 * Sends a rmdir instruction to user space
704 */
705static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
706{
707 int ret;
708
709verbose_printk("btrfs: send_rmdir %s\n", path->start);
710
711 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
712 if (ret < 0)
713 goto out;
714
715 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
716
717 ret = send_cmd(sctx);
718
719tlv_put_failure:
720out:
721 return ret;
722}
723
724/*
725 * Helper function to retrieve some fields from an inode item.
726 */
727static int get_inode_info(struct btrfs_root *root,
728 u64 ino, u64 *size, u64 *gen,
Alexander Block85a7b332012-07-26 23:39:10 +0200729 u64 *mode, u64 *uid, u64 *gid,
730 u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200731{
732 int ret;
733 struct btrfs_inode_item *ii;
734 struct btrfs_key key;
735 struct btrfs_path *path;
736
737 path = alloc_path_for_send();
738 if (!path)
739 return -ENOMEM;
740
741 key.objectid = ino;
742 key.type = BTRFS_INODE_ITEM_KEY;
743 key.offset = 0;
744 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
745 if (ret < 0)
746 goto out;
747 if (ret) {
748 ret = -ENOENT;
749 goto out;
750 }
751
752 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
753 struct btrfs_inode_item);
754 if (size)
755 *size = btrfs_inode_size(path->nodes[0], ii);
756 if (gen)
757 *gen = btrfs_inode_generation(path->nodes[0], ii);
758 if (mode)
759 *mode = btrfs_inode_mode(path->nodes[0], ii);
760 if (uid)
761 *uid = btrfs_inode_uid(path->nodes[0], ii);
762 if (gid)
763 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200764 if (rdev)
765 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200766
767out:
768 btrfs_free_path(path);
769 return ret;
770}
771
772typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
773 struct fs_path *p,
774 void *ctx);
775
776/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000777 * Helper function to iterate the entries in ONE btrfs_inode_ref or
778 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200779 * The iterate callback may return a non zero value to stop iteration. This can
780 * be a negative value for error codes or 1 to simply stop it.
781 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000782 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200783 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000784static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200785 struct btrfs_key *found_key, int resolve,
786 iterate_inode_ref_t iterate, void *ctx)
787{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000788 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200789 struct btrfs_item *item;
790 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000791 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200792 struct btrfs_path *tmp_path;
793 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000794 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200795 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000796 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200797 u32 name_len;
798 char *start;
799 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000800 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200801 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000802 u64 dir;
803 unsigned long name_off;
804 unsigned long elem_size;
805 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200806
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000807 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200808 if (!p)
809 return -ENOMEM;
810
811 tmp_path = alloc_path_for_send();
812 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000813 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200814 return -ENOMEM;
815 }
816
Alexander Block31db9f72012-07-25 23:19:24 +0200817
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000818 if (found_key->type == BTRFS_INODE_REF_KEY) {
819 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
820 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100821 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000822 total = btrfs_item_size(eb, item);
823 elem_size = sizeof(*iref);
824 } else {
825 ptr = btrfs_item_ptr_offset(eb, slot);
826 total = btrfs_item_size_nr(eb, slot);
827 elem_size = sizeof(*extref);
828 }
829
Alexander Block31db9f72012-07-25 23:19:24 +0200830 while (cur < total) {
831 fs_path_reset(p);
832
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000833 if (found_key->type == BTRFS_INODE_REF_KEY) {
834 iref = (struct btrfs_inode_ref *)(ptr + cur);
835 name_len = btrfs_inode_ref_name_len(eb, iref);
836 name_off = (unsigned long)(iref + 1);
837 index = btrfs_inode_ref_index(eb, iref);
838 dir = found_key->offset;
839 } else {
840 extref = (struct btrfs_inode_extref *)(ptr + cur);
841 name_len = btrfs_inode_extref_name_len(eb, extref);
842 name_off = (unsigned long)&extref->name;
843 index = btrfs_inode_extref_index(eb, extref);
844 dir = btrfs_inode_extref_parent(eb, extref);
845 }
846
Alexander Block31db9f72012-07-25 23:19:24 +0200847 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000848 start = btrfs_ref_to_path(root, tmp_path, name_len,
849 name_off, eb, dir,
850 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200851 if (IS_ERR(start)) {
852 ret = PTR_ERR(start);
853 goto out;
854 }
855 if (start < p->buf) {
856 /* overflow , try again with larger buffer */
857 ret = fs_path_ensure_buf(p,
858 p->buf_len + p->buf - start);
859 if (ret < 0)
860 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000861 start = btrfs_ref_to_path(root, tmp_path,
862 name_len, name_off,
863 eb, dir,
864 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200865 if (IS_ERR(start)) {
866 ret = PTR_ERR(start);
867 goto out;
868 }
869 BUG_ON(start < p->buf);
870 }
871 p->start = start;
872 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000873 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
874 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200875 if (ret < 0)
876 goto out;
877 }
878
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000879 cur += elem_size + name_len;
880 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200881 if (ret)
882 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200883 num++;
884 }
885
886out:
887 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000888 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200889 return ret;
890}
891
892typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
893 const char *name, int name_len,
894 const char *data, int data_len,
895 u8 type, void *ctx);
896
897/*
898 * Helper function to iterate the entries in ONE btrfs_dir_item.
899 * The iterate callback may return a non zero value to stop iteration. This can
900 * be a negative value for error codes or 1 to simply stop it.
901 *
902 * path must point to the dir item when called.
903 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000904static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200905 struct btrfs_key *found_key,
906 iterate_dir_item_t iterate, void *ctx)
907{
908 int ret = 0;
909 struct extent_buffer *eb;
910 struct btrfs_item *item;
911 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200912 struct btrfs_key di_key;
913 char *buf = NULL;
914 char *buf2 = NULL;
915 int buf_len;
916 int buf_virtual = 0;
917 u32 name_len;
918 u32 data_len;
919 u32 cur;
920 u32 len;
921 u32 total;
922 int slot;
923 int num;
924 u8 type;
925
926 buf_len = PAGE_SIZE;
927 buf = kmalloc(buf_len, GFP_NOFS);
928 if (!buf) {
929 ret = -ENOMEM;
930 goto out;
931 }
932
Alexander Block31db9f72012-07-25 23:19:24 +0200933 eb = path->nodes[0];
934 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +0100935 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +0200936 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
937 cur = 0;
938 len = 0;
939 total = btrfs_item_size(eb, item);
940
941 num = 0;
942 while (cur < total) {
943 name_len = btrfs_dir_name_len(eb, di);
944 data_len = btrfs_dir_data_len(eb, di);
945 type = btrfs_dir_type(eb, di);
946 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
947
948 if (name_len + data_len > buf_len) {
949 buf_len = PAGE_ALIGN(name_len + data_len);
950 if (buf_virtual) {
951 buf2 = vmalloc(buf_len);
952 if (!buf2) {
953 ret = -ENOMEM;
954 goto out;
955 }
956 vfree(buf);
957 } else {
958 buf2 = krealloc(buf, buf_len, GFP_NOFS);
959 if (!buf2) {
960 buf2 = vmalloc(buf_len);
961 if (!buf2) {
962 ret = -ENOMEM;
963 goto out;
964 }
965 kfree(buf);
966 buf_virtual = 1;
967 }
968 }
969
970 buf = buf2;
971 buf2 = NULL;
972 }
973
974 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
975 name_len + data_len);
976
977 len = sizeof(*di) + name_len + data_len;
978 di = (struct btrfs_dir_item *)((char *)di + len);
979 cur += len;
980
981 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
982 data_len, type, ctx);
983 if (ret < 0)
984 goto out;
985 if (ret) {
986 ret = 0;
987 goto out;
988 }
989
990 num++;
991 }
992
993out:
Alexander Block31db9f72012-07-25 23:19:24 +0200994 if (buf_virtual)
995 vfree(buf);
996 else
997 kfree(buf);
998 return ret;
999}
1000
1001static int __copy_first_ref(int num, u64 dir, int index,
1002 struct fs_path *p, void *ctx)
1003{
1004 int ret;
1005 struct fs_path *pt = ctx;
1006
1007 ret = fs_path_copy(pt, p);
1008 if (ret < 0)
1009 return ret;
1010
1011 /* we want the first only */
1012 return 1;
1013}
1014
1015/*
1016 * Retrieve the first path of an inode. If an inode has more then one
1017 * ref/hardlink, this is ignored.
1018 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001019static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001020 u64 ino, struct fs_path *path)
1021{
1022 int ret;
1023 struct btrfs_key key, found_key;
1024 struct btrfs_path *p;
1025
1026 p = alloc_path_for_send();
1027 if (!p)
1028 return -ENOMEM;
1029
1030 fs_path_reset(path);
1031
1032 key.objectid = ino;
1033 key.type = BTRFS_INODE_REF_KEY;
1034 key.offset = 0;
1035
1036 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1037 if (ret < 0)
1038 goto out;
1039 if (ret) {
1040 ret = 1;
1041 goto out;
1042 }
1043 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1044 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001045 (found_key.type != BTRFS_INODE_REF_KEY &&
1046 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001047 ret = -ENOENT;
1048 goto out;
1049 }
1050
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001051 ret = iterate_inode_ref(root, p, &found_key, 1,
1052 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001053 if (ret < 0)
1054 goto out;
1055 ret = 0;
1056
1057out:
1058 btrfs_free_path(p);
1059 return ret;
1060}
1061
1062struct backref_ctx {
1063 struct send_ctx *sctx;
1064
1065 /* number of total found references */
1066 u64 found;
1067
1068 /*
1069 * used for clones found in send_root. clones found behind cur_objectid
1070 * and cur_offset are not considered as allowed clones.
1071 */
1072 u64 cur_objectid;
1073 u64 cur_offset;
1074
1075 /* may be truncated in case it's the last extent in a file */
1076 u64 extent_len;
1077
1078 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001079 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001080};
1081
1082static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1083{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001084 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001085 struct clone_root *cr = (struct clone_root *)elt;
1086
1087 if (root < cr->root->objectid)
1088 return -1;
1089 if (root > cr->root->objectid)
1090 return 1;
1091 return 0;
1092}
1093
1094static int __clone_root_cmp_sort(const void *e1, const void *e2)
1095{
1096 struct clone_root *cr1 = (struct clone_root *)e1;
1097 struct clone_root *cr2 = (struct clone_root *)e2;
1098
1099 if (cr1->root->objectid < cr2->root->objectid)
1100 return -1;
1101 if (cr1->root->objectid > cr2->root->objectid)
1102 return 1;
1103 return 0;
1104}
1105
1106/*
1107 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001108 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001109 */
1110static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1111{
1112 struct backref_ctx *bctx = ctx_;
1113 struct clone_root *found;
1114 int ret;
1115 u64 i_size;
1116
1117 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001118 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001119 bctx->sctx->clone_roots_cnt,
1120 sizeof(struct clone_root),
1121 __clone_root_cmp_bsearch);
1122 if (!found)
1123 return 0;
1124
1125 if (found->root == bctx->sctx->send_root &&
1126 ino == bctx->cur_objectid &&
1127 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001128 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001129 }
1130
1131 /*
Alexander Block766702e2012-07-28 14:11:31 +02001132 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001133 * accept clones from these extents.
1134 */
Alexander Block85a7b332012-07-26 23:39:10 +02001135 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1136 NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001137 if (ret < 0)
1138 return ret;
1139
1140 if (offset + bctx->extent_len > i_size)
1141 return 0;
1142
1143 /*
1144 * Make sure we don't consider clones from send_root that are
1145 * behind the current inode/offset.
1146 */
1147 if (found->root == bctx->sctx->send_root) {
1148 /*
1149 * TODO for the moment we don't accept clones from the inode
1150 * that is currently send. We may change this when
1151 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1152 * file.
1153 */
1154 if (ino >= bctx->cur_objectid)
1155 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001156#if 0
1157 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001158 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001159 if (offset + bctx->extent_len > bctx->cur_offset)
1160 return 0;
1161#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001162 }
1163
1164 bctx->found++;
1165 found->found_refs++;
1166 if (ino < found->ino) {
1167 found->ino = ino;
1168 found->offset = offset;
1169 } else if (found->ino == ino) {
1170 /*
1171 * same extent found more then once in the same file.
1172 */
1173 if (found->offset > offset + bctx->extent_len)
1174 found->offset = offset;
1175 }
1176
1177 return 0;
1178}
1179
1180/*
Alexander Block766702e2012-07-28 14:11:31 +02001181 * Given an inode, offset and extent item, it finds a good clone for a clone
1182 * instruction. Returns -ENOENT when none could be found. The function makes
1183 * sure that the returned clone is usable at the point where sending is at the
1184 * moment. This means, that no clones are accepted which lie behind the current
1185 * inode+offset.
1186 *
Alexander Block31db9f72012-07-25 23:19:24 +02001187 * path must point to the extent item when called.
1188 */
1189static int find_extent_clone(struct send_ctx *sctx,
1190 struct btrfs_path *path,
1191 u64 ino, u64 data_offset,
1192 u64 ino_size,
1193 struct clone_root **found)
1194{
1195 int ret;
1196 int extent_type;
1197 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001198 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001199 u64 num_bytes;
1200 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001201 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001202 struct btrfs_file_extent_item *fi;
1203 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001204 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001205 struct clone_root *cur_clone_root;
1206 struct btrfs_key found_key;
1207 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001208 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001209 u32 i;
1210
1211 tmp_path = alloc_path_for_send();
1212 if (!tmp_path)
1213 return -ENOMEM;
1214
Alexander Block35075bb2012-07-28 12:44:34 +02001215 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1216 if (!backref_ctx) {
1217 ret = -ENOMEM;
1218 goto out;
1219 }
1220
Alexander Block31db9f72012-07-25 23:19:24 +02001221 if (data_offset >= ino_size) {
1222 /*
1223 * There may be extents that lie behind the file's size.
1224 * I at least had this in combination with snapshotting while
1225 * writing large files.
1226 */
1227 ret = 0;
1228 goto out;
1229 }
1230
1231 fi = btrfs_item_ptr(eb, path->slots[0],
1232 struct btrfs_file_extent_item);
1233 extent_type = btrfs_file_extent_type(eb, fi);
1234 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1235 ret = -ENOENT;
1236 goto out;
1237 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001238 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001239
1240 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001241 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1242 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001243 ret = -ENOENT;
1244 goto out;
1245 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001246 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001247
Liu Bo69917e42012-09-07 20:01:28 -06001248 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1249 &found_key, &flags);
Alexander Block31db9f72012-07-25 23:19:24 +02001250 btrfs_release_path(tmp_path);
1251
1252 if (ret < 0)
1253 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001254 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001255 ret = -EIO;
1256 goto out;
1257 }
1258
1259 /*
1260 * Setup the clone roots.
1261 */
1262 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1263 cur_clone_root = sctx->clone_roots + i;
1264 cur_clone_root->ino = (u64)-1;
1265 cur_clone_root->offset = 0;
1266 cur_clone_root->found_refs = 0;
1267 }
1268
Alexander Block35075bb2012-07-28 12:44:34 +02001269 backref_ctx->sctx = sctx;
1270 backref_ctx->found = 0;
1271 backref_ctx->cur_objectid = ino;
1272 backref_ctx->cur_offset = data_offset;
1273 backref_ctx->found_itself = 0;
1274 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001275
1276 /*
1277 * The last extent of a file may be too large due to page alignment.
1278 * We need to adjust extent_len in this case so that the checks in
1279 * __iterate_backrefs work.
1280 */
1281 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001282 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001283
1284 /*
1285 * Now collect all backrefs.
1286 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001287 if (compressed == BTRFS_COMPRESS_NONE)
1288 extent_item_pos = logical - found_key.objectid;
1289 else
1290 extent_item_pos = 0;
1291
Alexander Block31db9f72012-07-25 23:19:24 +02001292 extent_item_pos = logical - found_key.objectid;
1293 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1294 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001295 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001296
Alexander Block31db9f72012-07-25 23:19:24 +02001297 if (ret < 0)
1298 goto out;
1299
Alexander Block35075bb2012-07-28 12:44:34 +02001300 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001301 /* found a bug in backref code? */
1302 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001303 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001304 "send_root. inode=%llu, offset=%llu, "
Chris Mason74dd17f2012-08-07 16:25:13 -04001305 "disk_byte=%llu found extent=%llu\n",
1306 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001307 goto out;
1308 }
1309
1310verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1311 "ino=%llu, "
1312 "num_bytes=%llu, logical=%llu\n",
1313 data_offset, ino, num_bytes, logical);
1314
Alexander Block35075bb2012-07-28 12:44:34 +02001315 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001316 verbose_printk("btrfs: no clones found\n");
1317
1318 cur_clone_root = NULL;
1319 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1320 if (sctx->clone_roots[i].found_refs) {
1321 if (!cur_clone_root)
1322 cur_clone_root = sctx->clone_roots + i;
1323 else if (sctx->clone_roots[i].root == sctx->send_root)
1324 /* prefer clones from send_root over others */
1325 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001326 }
1327
1328 }
1329
1330 if (cur_clone_root) {
Filipe David Borba Manana93de4ba2014-02-15 15:53:16 +00001331 if (compressed != BTRFS_COMPRESS_NONE) {
1332 /*
1333 * Offsets given by iterate_extent_inodes() are relative
1334 * to the start of the extent, we need to add logical
1335 * offset from the file extent item.
1336 * (See why at backref.c:check_extent_in_eb())
1337 */
1338 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1339 fi);
1340 }
Alexander Block31db9f72012-07-25 23:19:24 +02001341 *found = cur_clone_root;
1342 ret = 0;
1343 } else {
1344 ret = -ENOENT;
1345 }
1346
1347out:
1348 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001349 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001350 return ret;
1351}
1352
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001353static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001354 u64 ino,
1355 struct fs_path *dest)
1356{
1357 int ret;
1358 struct btrfs_path *path;
1359 struct btrfs_key key;
1360 struct btrfs_file_extent_item *ei;
1361 u8 type;
1362 u8 compression;
1363 unsigned long off;
1364 int len;
1365
1366 path = alloc_path_for_send();
1367 if (!path)
1368 return -ENOMEM;
1369
1370 key.objectid = ino;
1371 key.type = BTRFS_EXTENT_DATA_KEY;
1372 key.offset = 0;
1373 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1374 if (ret < 0)
1375 goto out;
1376 BUG_ON(ret);
1377
1378 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1379 struct btrfs_file_extent_item);
1380 type = btrfs_file_extent_type(path->nodes[0], ei);
1381 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1382 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1383 BUG_ON(compression);
1384
1385 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001386 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001387
1388 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001389
1390out:
1391 btrfs_free_path(path);
1392 return ret;
1393}
1394
1395/*
1396 * Helper function to generate a file name that is unique in the root of
1397 * send_root and parent_root. This is used to generate names for orphan inodes.
1398 */
1399static int gen_unique_name(struct send_ctx *sctx,
1400 u64 ino, u64 gen,
1401 struct fs_path *dest)
1402{
1403 int ret = 0;
1404 struct btrfs_path *path;
1405 struct btrfs_dir_item *di;
1406 char tmp[64];
1407 int len;
1408 u64 idx = 0;
1409
1410 path = alloc_path_for_send();
1411 if (!path)
1412 return -ENOMEM;
1413
1414 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001415 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001416 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001417 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001418
1419 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1420 path, BTRFS_FIRST_FREE_OBJECTID,
1421 tmp, strlen(tmp), 0);
1422 btrfs_release_path(path);
1423 if (IS_ERR(di)) {
1424 ret = PTR_ERR(di);
1425 goto out;
1426 }
1427 if (di) {
1428 /* not unique, try again */
1429 idx++;
1430 continue;
1431 }
1432
1433 if (!sctx->parent_root) {
1434 /* unique */
1435 ret = 0;
1436 break;
1437 }
1438
1439 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1440 path, BTRFS_FIRST_FREE_OBJECTID,
1441 tmp, strlen(tmp), 0);
1442 btrfs_release_path(path);
1443 if (IS_ERR(di)) {
1444 ret = PTR_ERR(di);
1445 goto out;
1446 }
1447 if (di) {
1448 /* not unique, try again */
1449 idx++;
1450 continue;
1451 }
1452 /* unique */
1453 break;
1454 }
1455
1456 ret = fs_path_add(dest, tmp, strlen(tmp));
1457
1458out:
1459 btrfs_free_path(path);
1460 return ret;
1461}
1462
1463enum inode_state {
1464 inode_state_no_change,
1465 inode_state_will_create,
1466 inode_state_did_create,
1467 inode_state_will_delete,
1468 inode_state_did_delete,
1469};
1470
1471static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1472{
1473 int ret;
1474 int left_ret;
1475 int right_ret;
1476 u64 left_gen;
1477 u64 right_gen;
1478
1479 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001480 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001481 if (ret < 0 && ret != -ENOENT)
1482 goto out;
1483 left_ret = ret;
1484
1485 if (!sctx->parent_root) {
1486 right_ret = -ENOENT;
1487 } else {
1488 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001489 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001490 if (ret < 0 && ret != -ENOENT)
1491 goto out;
1492 right_ret = ret;
1493 }
1494
1495 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001496 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001497 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001498 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001499 if (ino < sctx->send_progress)
1500 ret = inode_state_did_create;
1501 else
1502 ret = inode_state_will_create;
1503 } else if (right_gen == gen) {
1504 if (ino < sctx->send_progress)
1505 ret = inode_state_did_delete;
1506 else
1507 ret = inode_state_will_delete;
1508 } else {
1509 ret = -ENOENT;
1510 }
1511 } else if (!left_ret) {
1512 if (left_gen == gen) {
1513 if (ino < sctx->send_progress)
1514 ret = inode_state_did_create;
1515 else
1516 ret = inode_state_will_create;
1517 } else {
1518 ret = -ENOENT;
1519 }
1520 } else if (!right_ret) {
1521 if (right_gen == gen) {
1522 if (ino < sctx->send_progress)
1523 ret = inode_state_did_delete;
1524 else
1525 ret = inode_state_will_delete;
1526 } else {
1527 ret = -ENOENT;
1528 }
1529 } else {
1530 ret = -ENOENT;
1531 }
1532
1533out:
1534 return ret;
1535}
1536
1537static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1538{
1539 int ret;
1540
1541 ret = get_cur_inode_state(sctx, ino, gen);
1542 if (ret < 0)
1543 goto out;
1544
1545 if (ret == inode_state_no_change ||
1546 ret == inode_state_did_create ||
1547 ret == inode_state_will_delete)
1548 ret = 1;
1549 else
1550 ret = 0;
1551
1552out:
1553 return ret;
1554}
1555
1556/*
1557 * Helper function to lookup a dir item in a dir.
1558 */
1559static int lookup_dir_item_inode(struct btrfs_root *root,
1560 u64 dir, const char *name, int name_len,
1561 u64 *found_inode,
1562 u8 *found_type)
1563{
1564 int ret = 0;
1565 struct btrfs_dir_item *di;
1566 struct btrfs_key key;
1567 struct btrfs_path *path;
1568
1569 path = alloc_path_for_send();
1570 if (!path)
1571 return -ENOMEM;
1572
1573 di = btrfs_lookup_dir_item(NULL, root, path,
1574 dir, name, name_len, 0);
1575 if (!di) {
1576 ret = -ENOENT;
1577 goto out;
1578 }
1579 if (IS_ERR(di)) {
1580 ret = PTR_ERR(di);
1581 goto out;
1582 }
1583 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1584 *found_inode = key.objectid;
1585 *found_type = btrfs_dir_type(path->nodes[0], di);
1586
1587out:
1588 btrfs_free_path(path);
1589 return ret;
1590}
1591
Alexander Block766702e2012-07-28 14:11:31 +02001592/*
1593 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1594 * generation of the parent dir and the name of the dir entry.
1595 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001596static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001597 u64 *dir, u64 *dir_gen, struct fs_path *name)
1598{
1599 int ret;
1600 struct btrfs_key key;
1601 struct btrfs_key found_key;
1602 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001603 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001604 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001605
1606 path = alloc_path_for_send();
1607 if (!path)
1608 return -ENOMEM;
1609
1610 key.objectid = ino;
1611 key.type = BTRFS_INODE_REF_KEY;
1612 key.offset = 0;
1613
1614 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1615 if (ret < 0)
1616 goto out;
1617 if (!ret)
1618 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1619 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001620 if (ret || found_key.objectid != ino ||
1621 (found_key.type != BTRFS_INODE_REF_KEY &&
1622 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001623 ret = -ENOENT;
1624 goto out;
1625 }
1626
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001627 if (key.type == BTRFS_INODE_REF_KEY) {
1628 struct btrfs_inode_ref *iref;
1629 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1630 struct btrfs_inode_ref);
1631 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1632 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1633 (unsigned long)(iref + 1),
1634 len);
1635 parent_dir = found_key.offset;
1636 } else {
1637 struct btrfs_inode_extref *extref;
1638 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1639 struct btrfs_inode_extref);
1640 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1641 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1642 (unsigned long)&extref->name, len);
1643 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1644 }
Alexander Block31db9f72012-07-25 23:19:24 +02001645 if (ret < 0)
1646 goto out;
1647 btrfs_release_path(path);
1648
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001649 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001650 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001651 if (ret < 0)
1652 goto out;
1653
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001654 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001655
1656out:
1657 btrfs_free_path(path);
1658 return ret;
1659}
1660
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001661static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001662 u64 ino, u64 dir,
1663 const char *name, int name_len)
1664{
1665 int ret;
1666 struct fs_path *tmp_name;
1667 u64 tmp_dir;
1668 u64 tmp_dir_gen;
1669
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001670 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001671 if (!tmp_name)
1672 return -ENOMEM;
1673
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001674 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001675 if (ret < 0)
1676 goto out;
1677
Alexander Blockb9291af2012-07-28 11:07:18 +02001678 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001679 ret = 0;
1680 goto out;
1681 }
1682
Alexander Blocke938c8a2012-07-28 16:33:49 +02001683 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001684
1685out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001686 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001687 return ret;
1688}
1689
Alexander Block766702e2012-07-28 14:11:31 +02001690/*
1691 * Used by process_recorded_refs to determine if a new ref would overwrite an
1692 * already existing ref. In case it detects an overwrite, it returns the
1693 * inode/gen in who_ino/who_gen.
1694 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1695 * to make sure later references to the overwritten inode are possible.
1696 * Orphanizing is however only required for the first ref of an inode.
1697 * process_recorded_refs does an additional is_first_ref check to see if
1698 * orphanizing is really required.
1699 */
Alexander Block31db9f72012-07-25 23:19:24 +02001700static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1701 const char *name, int name_len,
1702 u64 *who_ino, u64 *who_gen)
1703{
1704 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001705 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001706 u64 other_inode = 0;
1707 u8 other_type = 0;
1708
1709 if (!sctx->parent_root)
1710 goto out;
1711
1712 ret = is_inode_existent(sctx, dir, dir_gen);
1713 if (ret <= 0)
1714 goto out;
1715
Josef Bacikebdad912013-08-06 16:47:48 -04001716 /*
1717 * If we have a parent root we need to verify that the parent dir was
1718 * not delted and then re-created, if it was then we have no overwrite
1719 * and we can just unlink this entry.
1720 */
1721 if (sctx->parent_root) {
1722 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1723 NULL, NULL, NULL);
1724 if (ret < 0 && ret != -ENOENT)
1725 goto out;
1726 if (ret) {
1727 ret = 0;
1728 goto out;
1729 }
1730 if (gen != dir_gen)
1731 goto out;
1732 }
1733
Alexander Block31db9f72012-07-25 23:19:24 +02001734 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1735 &other_inode, &other_type);
1736 if (ret < 0 && ret != -ENOENT)
1737 goto out;
1738 if (ret) {
1739 ret = 0;
1740 goto out;
1741 }
1742
Alexander Block766702e2012-07-28 14:11:31 +02001743 /*
1744 * Check if the overwritten ref was already processed. If yes, the ref
1745 * was already unlinked/moved, so we can safely assume that we will not
1746 * overwrite anything at this point in time.
1747 */
Alexander Block31db9f72012-07-25 23:19:24 +02001748 if (other_inode > sctx->send_progress) {
1749 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001750 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001751 if (ret < 0)
1752 goto out;
1753
1754 ret = 1;
1755 *who_ino = other_inode;
1756 } else {
1757 ret = 0;
1758 }
1759
1760out:
1761 return ret;
1762}
1763
Alexander Block766702e2012-07-28 14:11:31 +02001764/*
1765 * Checks if the ref was overwritten by an already processed inode. This is
1766 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1767 * thus the orphan name needs be used.
1768 * process_recorded_refs also uses it to avoid unlinking of refs that were
1769 * overwritten.
1770 */
Alexander Block31db9f72012-07-25 23:19:24 +02001771static int did_overwrite_ref(struct send_ctx *sctx,
1772 u64 dir, u64 dir_gen,
1773 u64 ino, u64 ino_gen,
1774 const char *name, int name_len)
1775{
1776 int ret = 0;
1777 u64 gen;
1778 u64 ow_inode;
1779 u8 other_type;
1780
1781 if (!sctx->parent_root)
1782 goto out;
1783
1784 ret = is_inode_existent(sctx, dir, dir_gen);
1785 if (ret <= 0)
1786 goto out;
1787
1788 /* check if the ref was overwritten by another ref */
1789 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1790 &ow_inode, &other_type);
1791 if (ret < 0 && ret != -ENOENT)
1792 goto out;
1793 if (ret) {
1794 /* was never and will never be overwritten */
1795 ret = 0;
1796 goto out;
1797 }
1798
1799 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001800 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001801 if (ret < 0)
1802 goto out;
1803
1804 if (ow_inode == ino && gen == ino_gen) {
1805 ret = 0;
1806 goto out;
1807 }
1808
1809 /* we know that it is or will be overwritten. check this now */
1810 if (ow_inode < sctx->send_progress)
1811 ret = 1;
1812 else
1813 ret = 0;
1814
1815out:
1816 return ret;
1817}
1818
Alexander Block766702e2012-07-28 14:11:31 +02001819/*
1820 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1821 * that got overwritten. This is used by process_recorded_refs to determine
1822 * if it has to use the path as returned by get_cur_path or the orphan name.
1823 */
Alexander Block31db9f72012-07-25 23:19:24 +02001824static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1825{
1826 int ret = 0;
1827 struct fs_path *name = NULL;
1828 u64 dir;
1829 u64 dir_gen;
1830
1831 if (!sctx->parent_root)
1832 goto out;
1833
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001834 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001835 if (!name)
1836 return -ENOMEM;
1837
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001838 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001839 if (ret < 0)
1840 goto out;
1841
1842 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1843 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001844
1845out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001846 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001847 return ret;
1848}
1849
Alexander Block766702e2012-07-28 14:11:31 +02001850/*
1851 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1852 * so we need to do some special handling in case we have clashes. This function
1853 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001854 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001855 */
Alexander Block31db9f72012-07-25 23:19:24 +02001856static int name_cache_insert(struct send_ctx *sctx,
1857 struct name_cache_entry *nce)
1858{
1859 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001860 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001861
Alexander Block7e0926f2012-07-28 14:20:58 +02001862 nce_head = radix_tree_lookup(&sctx->name_cache,
1863 (unsigned long)nce->ino);
1864 if (!nce_head) {
1865 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001866 if (!nce_head) {
1867 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001868 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001869 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001870 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001871
Alexander Block7e0926f2012-07-28 14:20:58 +02001872 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001873 if (ret < 0) {
1874 kfree(nce_head);
1875 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001876 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001877 }
Alexander Block31db9f72012-07-25 23:19:24 +02001878 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001879 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001880 list_add_tail(&nce->list, &sctx->name_cache_list);
1881 sctx->name_cache_size++;
1882
1883 return ret;
1884}
1885
1886static void name_cache_delete(struct send_ctx *sctx,
1887 struct name_cache_entry *nce)
1888{
Alexander Block7e0926f2012-07-28 14:20:58 +02001889 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001890
Alexander Block7e0926f2012-07-28 14:20:58 +02001891 nce_head = radix_tree_lookup(&sctx->name_cache,
1892 (unsigned long)nce->ino);
1893 BUG_ON(!nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001894
Alexander Block7e0926f2012-07-28 14:20:58 +02001895 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001896 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001897 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001898
1899 if (list_empty(nce_head)) {
1900 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1901 kfree(nce_head);
1902 }
Alexander Block31db9f72012-07-25 23:19:24 +02001903}
1904
1905static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1906 u64 ino, u64 gen)
1907{
Alexander Block7e0926f2012-07-28 14:20:58 +02001908 struct list_head *nce_head;
1909 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001910
Alexander Block7e0926f2012-07-28 14:20:58 +02001911 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1912 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001913 return NULL;
1914
Alexander Block7e0926f2012-07-28 14:20:58 +02001915 list_for_each_entry(cur, nce_head, radix_list) {
1916 if (cur->ino == ino && cur->gen == gen)
1917 return cur;
1918 }
Alexander Block31db9f72012-07-25 23:19:24 +02001919 return NULL;
1920}
1921
Alexander Block766702e2012-07-28 14:11:31 +02001922/*
1923 * Removes the entry from the list and adds it back to the end. This marks the
1924 * entry as recently used so that name_cache_clean_unused does not remove it.
1925 */
Alexander Block31db9f72012-07-25 23:19:24 +02001926static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1927{
1928 list_del(&nce->list);
1929 list_add_tail(&nce->list, &sctx->name_cache_list);
1930}
1931
Alexander Block766702e2012-07-28 14:11:31 +02001932/*
1933 * Remove some entries from the beginning of name_cache_list.
1934 */
Alexander Block31db9f72012-07-25 23:19:24 +02001935static void name_cache_clean_unused(struct send_ctx *sctx)
1936{
1937 struct name_cache_entry *nce;
1938
1939 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1940 return;
1941
1942 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1943 nce = list_entry(sctx->name_cache_list.next,
1944 struct name_cache_entry, list);
1945 name_cache_delete(sctx, nce);
1946 kfree(nce);
1947 }
1948}
1949
1950static void name_cache_free(struct send_ctx *sctx)
1951{
1952 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02001953
Alexander Blocke938c8a2012-07-28 16:33:49 +02001954 while (!list_empty(&sctx->name_cache_list)) {
1955 nce = list_entry(sctx->name_cache_list.next,
1956 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02001957 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02001958 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001959 }
1960}
1961
Alexander Block766702e2012-07-28 14:11:31 +02001962/*
1963 * Used by get_cur_path for each ref up to the root.
1964 * Returns 0 if it succeeded.
1965 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1966 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1967 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1968 * Returns <0 in case of error.
1969 */
Alexander Block31db9f72012-07-25 23:19:24 +02001970static int __get_cur_name_and_parent(struct send_ctx *sctx,
1971 u64 ino, u64 gen,
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00001972 int skip_name_cache,
Alexander Block31db9f72012-07-25 23:19:24 +02001973 u64 *parent_ino,
1974 u64 *parent_gen,
1975 struct fs_path *dest)
1976{
1977 int ret;
1978 int nce_ret;
1979 struct btrfs_path *path = NULL;
1980 struct name_cache_entry *nce = NULL;
1981
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00001982 if (skip_name_cache)
1983 goto get_ref;
Alexander Block766702e2012-07-28 14:11:31 +02001984 /*
1985 * First check if we already did a call to this function with the same
1986 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1987 * return the cached result.
1988 */
Alexander Block31db9f72012-07-25 23:19:24 +02001989 nce = name_cache_search(sctx, ino, gen);
1990 if (nce) {
1991 if (ino < sctx->send_progress && nce->need_later_update) {
1992 name_cache_delete(sctx, nce);
1993 kfree(nce);
1994 nce = NULL;
1995 } else {
1996 name_cache_used(sctx, nce);
1997 *parent_ino = nce->parent_ino;
1998 *parent_gen = nce->parent_gen;
1999 ret = fs_path_add(dest, nce->name, nce->name_len);
2000 if (ret < 0)
2001 goto out;
2002 ret = nce->ret;
2003 goto out;
2004 }
2005 }
2006
2007 path = alloc_path_for_send();
2008 if (!path)
2009 return -ENOMEM;
2010
Alexander Block766702e2012-07-28 14:11:31 +02002011 /*
2012 * If the inode is not existent yet, add the orphan name and return 1.
2013 * This should only happen for the parent dir that we determine in
2014 * __record_new_ref
2015 */
Alexander Block31db9f72012-07-25 23:19:24 +02002016 ret = is_inode_existent(sctx, ino, gen);
2017 if (ret < 0)
2018 goto out;
2019
2020 if (!ret) {
2021 ret = gen_unique_name(sctx, ino, gen, dest);
2022 if (ret < 0)
2023 goto out;
2024 ret = 1;
2025 goto out_cache;
2026 }
2027
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002028get_ref:
Alexander Block766702e2012-07-28 14:11:31 +02002029 /*
2030 * Depending on whether the inode was already processed or not, use
2031 * send_root or parent_root for ref lookup.
2032 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002033 if (ino < sctx->send_progress && !skip_name_cache)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002034 ret = get_first_ref(sctx->send_root, ino,
2035 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002036 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002037 ret = get_first_ref(sctx->parent_root, ino,
2038 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002039 if (ret < 0)
2040 goto out;
2041
Alexander Block766702e2012-07-28 14:11:31 +02002042 /*
2043 * Check if the ref was overwritten by an inode's ref that was processed
2044 * earlier. If yes, treat as orphan and return 1.
2045 */
Alexander Block31db9f72012-07-25 23:19:24 +02002046 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2047 dest->start, dest->end - dest->start);
2048 if (ret < 0)
2049 goto out;
2050 if (ret) {
2051 fs_path_reset(dest);
2052 ret = gen_unique_name(sctx, ino, gen, dest);
2053 if (ret < 0)
2054 goto out;
2055 ret = 1;
2056 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002057 if (skip_name_cache)
2058 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002059
2060out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002061 /*
2062 * Store the result of the lookup in the name cache.
2063 */
Alexander Block31db9f72012-07-25 23:19:24 +02002064 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2065 if (!nce) {
2066 ret = -ENOMEM;
2067 goto out;
2068 }
2069
2070 nce->ino = ino;
2071 nce->gen = gen;
2072 nce->parent_ino = *parent_ino;
2073 nce->parent_gen = *parent_gen;
2074 nce->name_len = fs_path_len(dest);
2075 nce->ret = ret;
2076 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002077
2078 if (ino < sctx->send_progress)
2079 nce->need_later_update = 0;
2080 else
2081 nce->need_later_update = 1;
2082
2083 nce_ret = name_cache_insert(sctx, nce);
2084 if (nce_ret < 0)
2085 ret = nce_ret;
2086 name_cache_clean_unused(sctx);
2087
2088out:
2089 btrfs_free_path(path);
2090 return ret;
2091}
2092
2093/*
2094 * Magic happens here. This function returns the first ref to an inode as it
2095 * would look like while receiving the stream at this point in time.
2096 * We walk the path up to the root. For every inode in between, we check if it
2097 * was already processed/sent. If yes, we continue with the parent as found
2098 * in send_root. If not, we continue with the parent as found in parent_root.
2099 * If we encounter an inode that was deleted at this point in time, we use the
2100 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2101 * that were not created yet and overwritten inodes/refs.
2102 *
2103 * When do we have have orphan inodes:
2104 * 1. When an inode is freshly created and thus no valid refs are available yet
2105 * 2. When a directory lost all it's refs (deleted) but still has dir items
2106 * inside which were not processed yet (pending for move/delete). If anyone
2107 * tried to get the path to the dir items, it would get a path inside that
2108 * orphan directory.
2109 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2110 * of an unprocessed inode. If in that case the first ref would be
2111 * overwritten, the overwritten inode gets "orphanized". Later when we
2112 * process this overwritten inode, it is restored at a new place by moving
2113 * the orphan inode.
2114 *
2115 * sctx->send_progress tells this function at which point in time receiving
2116 * would be.
2117 */
2118static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2119 struct fs_path *dest)
2120{
2121 int ret = 0;
2122 struct fs_path *name = NULL;
2123 u64 parent_inode = 0;
2124 u64 parent_gen = 0;
2125 int stop = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002126 int skip_name_cache = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002127
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002128 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002129 if (!name) {
2130 ret = -ENOMEM;
2131 goto out;
2132 }
2133
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002134 if (is_waiting_for_move(sctx, ino))
2135 skip_name_cache = 1;
2136
Alexander Block31db9f72012-07-25 23:19:24 +02002137 dest->reversed = 1;
2138 fs_path_reset(dest);
2139
2140 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2141 fs_path_reset(name);
2142
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002143 ret = __get_cur_name_and_parent(sctx, ino, gen, skip_name_cache,
Alexander Block31db9f72012-07-25 23:19:24 +02002144 &parent_inode, &parent_gen, name);
2145 if (ret < 0)
2146 goto out;
2147 if (ret)
2148 stop = 1;
2149
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002150 if (!skip_name_cache &&
Filipe David Borba Manana03cb4fb2014-02-01 02:00:15 +00002151 is_waiting_for_move(sctx, parent_inode))
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002152 skip_name_cache = 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002153
Alexander Block31db9f72012-07-25 23:19:24 +02002154 ret = fs_path_add_path(dest, name);
2155 if (ret < 0)
2156 goto out;
2157
2158 ino = parent_inode;
2159 gen = parent_gen;
2160 }
2161
2162out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002163 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002164 if (!ret)
2165 fs_path_unreverse(dest);
2166 return ret;
2167}
2168
2169/*
Alexander Block31db9f72012-07-25 23:19:24 +02002170 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2171 */
2172static int send_subvol_begin(struct send_ctx *sctx)
2173{
2174 int ret;
2175 struct btrfs_root *send_root = sctx->send_root;
2176 struct btrfs_root *parent_root = sctx->parent_root;
2177 struct btrfs_path *path;
2178 struct btrfs_key key;
2179 struct btrfs_root_ref *ref;
2180 struct extent_buffer *leaf;
2181 char *name = NULL;
2182 int namelen;
2183
Wang Shilongffcfaf82014-01-15 00:26:43 +08002184 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002185 if (!path)
2186 return -ENOMEM;
2187
2188 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2189 if (!name) {
2190 btrfs_free_path(path);
2191 return -ENOMEM;
2192 }
2193
2194 key.objectid = send_root->objectid;
2195 key.type = BTRFS_ROOT_BACKREF_KEY;
2196 key.offset = 0;
2197
2198 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2199 &key, path, 1, 0);
2200 if (ret < 0)
2201 goto out;
2202 if (ret) {
2203 ret = -ENOENT;
2204 goto out;
2205 }
2206
2207 leaf = path->nodes[0];
2208 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2209 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2210 key.objectid != send_root->objectid) {
2211 ret = -ENOENT;
2212 goto out;
2213 }
2214 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2215 namelen = btrfs_root_ref_name_len(leaf, ref);
2216 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2217 btrfs_release_path(path);
2218
Alexander Block31db9f72012-07-25 23:19:24 +02002219 if (parent_root) {
2220 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2221 if (ret < 0)
2222 goto out;
2223 } else {
2224 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2225 if (ret < 0)
2226 goto out;
2227 }
2228
2229 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2230 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2231 sctx->send_root->root_item.uuid);
2232 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002233 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002234 if (parent_root) {
2235 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2236 sctx->parent_root->root_item.uuid);
2237 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002238 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002239 }
2240
2241 ret = send_cmd(sctx);
2242
2243tlv_put_failure:
2244out:
2245 btrfs_free_path(path);
2246 kfree(name);
2247 return ret;
2248}
2249
2250static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2251{
2252 int ret = 0;
2253 struct fs_path *p;
2254
2255verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2256
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002257 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002258 if (!p)
2259 return -ENOMEM;
2260
2261 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2262 if (ret < 0)
2263 goto out;
2264
2265 ret = get_cur_path(sctx, ino, gen, p);
2266 if (ret < 0)
2267 goto out;
2268 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2269 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2270
2271 ret = send_cmd(sctx);
2272
2273tlv_put_failure:
2274out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002275 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002276 return ret;
2277}
2278
2279static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2280{
2281 int ret = 0;
2282 struct fs_path *p;
2283
2284verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2285
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002286 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002287 if (!p)
2288 return -ENOMEM;
2289
2290 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2291 if (ret < 0)
2292 goto out;
2293
2294 ret = get_cur_path(sctx, ino, gen, p);
2295 if (ret < 0)
2296 goto out;
2297 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2298 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2299
2300 ret = send_cmd(sctx);
2301
2302tlv_put_failure:
2303out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002304 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002305 return ret;
2306}
2307
2308static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2309{
2310 int ret = 0;
2311 struct fs_path *p;
2312
2313verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2314
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002315 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002316 if (!p)
2317 return -ENOMEM;
2318
2319 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2320 if (ret < 0)
2321 goto out;
2322
2323 ret = get_cur_path(sctx, ino, gen, p);
2324 if (ret < 0)
2325 goto out;
2326 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2327 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2328 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2329
2330 ret = send_cmd(sctx);
2331
2332tlv_put_failure:
2333out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002334 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002335 return ret;
2336}
2337
2338static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2339{
2340 int ret = 0;
2341 struct fs_path *p = NULL;
2342 struct btrfs_inode_item *ii;
2343 struct btrfs_path *path = NULL;
2344 struct extent_buffer *eb;
2345 struct btrfs_key key;
2346 int slot;
2347
2348verbose_printk("btrfs: send_utimes %llu\n", ino);
2349
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002350 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002351 if (!p)
2352 return -ENOMEM;
2353
2354 path = alloc_path_for_send();
2355 if (!path) {
2356 ret = -ENOMEM;
2357 goto out;
2358 }
2359
2360 key.objectid = ino;
2361 key.type = BTRFS_INODE_ITEM_KEY;
2362 key.offset = 0;
2363 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2364 if (ret < 0)
2365 goto out;
2366
2367 eb = path->nodes[0];
2368 slot = path->slots[0];
2369 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2370
2371 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2372 if (ret < 0)
2373 goto out;
2374
2375 ret = get_cur_path(sctx, ino, gen, p);
2376 if (ret < 0)
2377 goto out;
2378 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2379 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2380 btrfs_inode_atime(ii));
2381 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2382 btrfs_inode_mtime(ii));
2383 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2384 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002385 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002386
2387 ret = send_cmd(sctx);
2388
2389tlv_put_failure:
2390out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002391 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002392 btrfs_free_path(path);
2393 return ret;
2394}
2395
2396/*
2397 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2398 * a valid path yet because we did not process the refs yet. So, the inode
2399 * is created as orphan.
2400 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002401static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002402{
2403 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002404 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002405 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002406 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002407 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002408 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002409
Alexander Block1f4692d2012-07-28 10:42:24 +02002410verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002411
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002412 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002413 if (!p)
2414 return -ENOMEM;
2415
Alexander Block1f4692d2012-07-28 10:42:24 +02002416 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2417 NULL, &rdev);
2418 if (ret < 0)
2419 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002420
Alexander Blocke938c8a2012-07-28 16:33:49 +02002421 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002422 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002423 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002424 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002425 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002426 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002427 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002428 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002429 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002430 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002431 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002432 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002433 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002434 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2435 (int)(mode & S_IFMT));
2436 ret = -ENOTSUPP;
2437 goto out;
2438 }
2439
2440 ret = begin_cmd(sctx, cmd);
2441 if (ret < 0)
2442 goto out;
2443
Alexander Block1f4692d2012-07-28 10:42:24 +02002444 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002445 if (ret < 0)
2446 goto out;
2447
2448 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002449 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002450
2451 if (S_ISLNK(mode)) {
2452 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002453 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002454 if (ret < 0)
2455 goto out;
2456 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2457 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2458 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002459 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2460 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002461 }
2462
2463 ret = send_cmd(sctx);
2464 if (ret < 0)
2465 goto out;
2466
2467
2468tlv_put_failure:
2469out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002470 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002471 return ret;
2472}
2473
Alexander Block1f4692d2012-07-28 10:42:24 +02002474/*
2475 * We need some special handling for inodes that get processed before the parent
2476 * directory got created. See process_recorded_refs for details.
2477 * This function does the check if we already created the dir out of order.
2478 */
2479static int did_create_dir(struct send_ctx *sctx, u64 dir)
2480{
2481 int ret = 0;
2482 struct btrfs_path *path = NULL;
2483 struct btrfs_key key;
2484 struct btrfs_key found_key;
2485 struct btrfs_key di_key;
2486 struct extent_buffer *eb;
2487 struct btrfs_dir_item *di;
2488 int slot;
2489
2490 path = alloc_path_for_send();
2491 if (!path) {
2492 ret = -ENOMEM;
2493 goto out;
2494 }
2495
2496 key.objectid = dir;
2497 key.type = BTRFS_DIR_INDEX_KEY;
2498 key.offset = 0;
2499 while (1) {
2500 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2501 1, 0);
2502 if (ret < 0)
2503 goto out;
2504 if (!ret) {
2505 eb = path->nodes[0];
2506 slot = path->slots[0];
2507 btrfs_item_key_to_cpu(eb, &found_key, slot);
2508 }
2509 if (ret || found_key.objectid != key.objectid ||
2510 found_key.type != key.type) {
2511 ret = 0;
2512 goto out;
2513 }
2514
2515 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2516 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2517
Josef Bacika0525412013-08-12 10:56:14 -04002518 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2519 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002520 ret = 1;
2521 goto out;
2522 }
2523
2524 key.offset = found_key.offset + 1;
2525 btrfs_release_path(path);
2526 }
2527
2528out:
2529 btrfs_free_path(path);
2530 return ret;
2531}
2532
2533/*
2534 * Only creates the inode if it is:
2535 * 1. Not a directory
2536 * 2. Or a directory which was not created already due to out of order
2537 * directories. See did_create_dir and process_recorded_refs for details.
2538 */
2539static int send_create_inode_if_needed(struct send_ctx *sctx)
2540{
2541 int ret;
2542
2543 if (S_ISDIR(sctx->cur_inode_mode)) {
2544 ret = did_create_dir(sctx, sctx->cur_ino);
2545 if (ret < 0)
2546 goto out;
2547 if (ret) {
2548 ret = 0;
2549 goto out;
2550 }
2551 }
2552
2553 ret = send_create_inode(sctx, sctx->cur_ino);
2554 if (ret < 0)
2555 goto out;
2556
2557out:
2558 return ret;
2559}
2560
Alexander Block31db9f72012-07-25 23:19:24 +02002561struct recorded_ref {
2562 struct list_head list;
2563 char *dir_path;
2564 char *name;
2565 struct fs_path *full_path;
2566 u64 dir;
2567 u64 dir_gen;
2568 int dir_path_len;
2569 int name_len;
2570};
2571
2572/*
2573 * We need to process new refs before deleted refs, but compare_tree gives us
2574 * everything mixed. So we first record all refs and later process them.
2575 * This function is a helper to record one ref.
2576 */
2577static int record_ref(struct list_head *head, u64 dir,
2578 u64 dir_gen, struct fs_path *path)
2579{
2580 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002581
2582 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2583 if (!ref)
2584 return -ENOMEM;
2585
2586 ref->dir = dir;
2587 ref->dir_gen = dir_gen;
2588 ref->full_path = path;
2589
Andy Shevchenkoed848852013-08-21 10:32:13 +03002590 ref->name = (char *)kbasename(ref->full_path->start);
2591 ref->name_len = ref->full_path->end - ref->name;
2592 ref->dir_path = ref->full_path->start;
2593 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002594 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002595 else
Alexander Block31db9f72012-07-25 23:19:24 +02002596 ref->dir_path_len = ref->full_path->end -
2597 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002598
2599 list_add_tail(&ref->list, head);
2600 return 0;
2601}
2602
Josef Bacikba5e8f22013-08-16 16:52:55 -04002603static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2604{
2605 struct recorded_ref *new;
2606
2607 new = kmalloc(sizeof(*ref), GFP_NOFS);
2608 if (!new)
2609 return -ENOMEM;
2610
2611 new->dir = ref->dir;
2612 new->dir_gen = ref->dir_gen;
2613 new->full_path = NULL;
2614 INIT_LIST_HEAD(&new->list);
2615 list_add_tail(&new->list, list);
2616 return 0;
2617}
2618
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002619static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002620{
2621 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002622
Alexander Blocke938c8a2012-07-28 16:33:49 +02002623 while (!list_empty(head)) {
2624 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002625 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002626 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002627 kfree(cur);
2628 }
Alexander Block31db9f72012-07-25 23:19:24 +02002629}
2630
2631static void free_recorded_refs(struct send_ctx *sctx)
2632{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002633 __free_recorded_refs(&sctx->new_refs);
2634 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002635}
2636
2637/*
Alexander Block766702e2012-07-28 14:11:31 +02002638 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002639 * ref of an unprocessed inode gets overwritten and for all non empty
2640 * directories.
2641 */
2642static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2643 struct fs_path *path)
2644{
2645 int ret;
2646 struct fs_path *orphan;
2647
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002648 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002649 if (!orphan)
2650 return -ENOMEM;
2651
2652 ret = gen_unique_name(sctx, ino, gen, orphan);
2653 if (ret < 0)
2654 goto out;
2655
2656 ret = send_rename(sctx, path, orphan);
2657
2658out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002659 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002660 return ret;
2661}
2662
2663/*
2664 * Returns 1 if a directory can be removed at this point in time.
2665 * We check this by iterating all dir items and checking if the inode behind
2666 * the dir item was already processed.
2667 */
2668static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2669{
2670 int ret = 0;
2671 struct btrfs_root *root = sctx->parent_root;
2672 struct btrfs_path *path;
2673 struct btrfs_key key;
2674 struct btrfs_key found_key;
2675 struct btrfs_key loc;
2676 struct btrfs_dir_item *di;
2677
Alexander Block6d85ed02012-08-01 14:48:59 +02002678 /*
2679 * Don't try to rmdir the top/root subvolume dir.
2680 */
2681 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2682 return 0;
2683
Alexander Block31db9f72012-07-25 23:19:24 +02002684 path = alloc_path_for_send();
2685 if (!path)
2686 return -ENOMEM;
2687
2688 key.objectid = dir;
2689 key.type = BTRFS_DIR_INDEX_KEY;
2690 key.offset = 0;
2691
2692 while (1) {
2693 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2694 if (ret < 0)
2695 goto out;
2696 if (!ret) {
2697 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2698 path->slots[0]);
2699 }
2700 if (ret || found_key.objectid != key.objectid ||
2701 found_key.type != key.type) {
2702 break;
2703 }
2704
2705 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2706 struct btrfs_dir_item);
2707 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2708
2709 if (loc.objectid > send_progress) {
2710 ret = 0;
2711 goto out;
2712 }
2713
2714 btrfs_release_path(path);
2715 key.offset = found_key.offset + 1;
2716 }
2717
2718 ret = 1;
2719
2720out:
2721 btrfs_free_path(path);
2722 return ret;
2723}
2724
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002725static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2726{
2727 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2728 struct waiting_dir_move *entry;
2729
2730 while (n) {
2731 entry = rb_entry(n, struct waiting_dir_move, node);
2732 if (ino < entry->ino)
2733 n = n->rb_left;
2734 else if (ino > entry->ino)
2735 n = n->rb_right;
2736 else
2737 return 1;
2738 }
2739 return 0;
2740}
2741
2742static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2743{
2744 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2745 struct rb_node *parent = NULL;
2746 struct waiting_dir_move *entry, *dm;
2747
2748 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2749 if (!dm)
2750 return -ENOMEM;
2751 dm->ino = ino;
2752
2753 while (*p) {
2754 parent = *p;
2755 entry = rb_entry(parent, struct waiting_dir_move, node);
2756 if (ino < entry->ino) {
2757 p = &(*p)->rb_left;
2758 } else if (ino > entry->ino) {
2759 p = &(*p)->rb_right;
2760 } else {
2761 kfree(dm);
2762 return -EEXIST;
2763 }
2764 }
2765
2766 rb_link_node(&dm->node, parent, p);
2767 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2768 return 0;
2769}
2770
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002771static int del_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2772{
2773 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2774 struct waiting_dir_move *entry;
2775
2776 while (n) {
2777 entry = rb_entry(n, struct waiting_dir_move, node);
2778 if (ino < entry->ino) {
2779 n = n->rb_left;
2780 } else if (ino > entry->ino) {
2781 n = n->rb_right;
2782 } else {
2783 rb_erase(&entry->node, &sctx->waiting_dir_moves);
2784 kfree(entry);
2785 return 0;
2786 }
2787 }
2788 return -ENOENT;
2789}
2790
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002791static int add_pending_dir_move(struct send_ctx *sctx, u64 parent_ino)
2792{
2793 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2794 struct rb_node *parent = NULL;
2795 struct pending_dir_move *entry, *pm;
2796 struct recorded_ref *cur;
2797 int exists = 0;
2798 int ret;
2799
2800 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2801 if (!pm)
2802 return -ENOMEM;
2803 pm->parent_ino = parent_ino;
2804 pm->ino = sctx->cur_ino;
2805 pm->gen = sctx->cur_inode_gen;
2806 INIT_LIST_HEAD(&pm->list);
2807 INIT_LIST_HEAD(&pm->update_refs);
2808 RB_CLEAR_NODE(&pm->node);
2809
2810 while (*p) {
2811 parent = *p;
2812 entry = rb_entry(parent, struct pending_dir_move, node);
2813 if (parent_ino < entry->parent_ino) {
2814 p = &(*p)->rb_left;
2815 } else if (parent_ino > entry->parent_ino) {
2816 p = &(*p)->rb_right;
2817 } else {
2818 exists = 1;
2819 break;
2820 }
2821 }
2822
2823 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2824 ret = dup_ref(cur, &pm->update_refs);
2825 if (ret < 0)
2826 goto out;
2827 }
2828 list_for_each_entry(cur, &sctx->new_refs, list) {
2829 ret = dup_ref(cur, &pm->update_refs);
2830 if (ret < 0)
2831 goto out;
2832 }
2833
2834 ret = add_waiting_dir_move(sctx, pm->ino);
2835 if (ret)
2836 goto out;
2837
2838 if (exists) {
2839 list_add_tail(&pm->list, &entry->list);
2840 } else {
2841 rb_link_node(&pm->node, parent, p);
2842 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
2843 }
2844 ret = 0;
2845out:
2846 if (ret) {
2847 __free_recorded_refs(&pm->update_refs);
2848 kfree(pm);
2849 }
2850 return ret;
2851}
2852
2853static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
2854 u64 parent_ino)
2855{
2856 struct rb_node *n = sctx->pending_dir_moves.rb_node;
2857 struct pending_dir_move *entry;
2858
2859 while (n) {
2860 entry = rb_entry(n, struct pending_dir_move, node);
2861 if (parent_ino < entry->parent_ino)
2862 n = n->rb_left;
2863 else if (parent_ino > entry->parent_ino)
2864 n = n->rb_right;
2865 else
2866 return entry;
2867 }
2868 return NULL;
2869}
2870
2871static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
2872{
2873 struct fs_path *from_path = NULL;
2874 struct fs_path *to_path = NULL;
2875 u64 orig_progress = sctx->send_progress;
2876 struct recorded_ref *cur;
2877 int ret;
2878
2879 from_path = fs_path_alloc();
2880 if (!from_path)
2881 return -ENOMEM;
2882
2883 sctx->send_progress = pm->ino;
2884 ret = get_cur_path(sctx, pm->ino, pm->gen, from_path);
2885 if (ret < 0)
2886 goto out;
2887
2888 to_path = fs_path_alloc();
2889 if (!to_path) {
2890 ret = -ENOMEM;
2891 goto out;
2892 }
2893
2894 sctx->send_progress = sctx->cur_ino + 1;
Josef Bacik6cc98d92014-02-05 16:19:21 -05002895 ret = del_waiting_dir_move(sctx, pm->ino);
2896 ASSERT(ret == 0);
2897
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002898 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
2899 if (ret < 0)
2900 goto out;
2901
2902 ret = send_rename(sctx, from_path, to_path);
2903 if (ret < 0)
2904 goto out;
2905
2906 ret = send_utimes(sctx, pm->ino, pm->gen);
2907 if (ret < 0)
2908 goto out;
2909
2910 /*
2911 * After rename/move, need to update the utimes of both new parent(s)
2912 * and old parent(s).
2913 */
2914 list_for_each_entry(cur, &pm->update_refs, list) {
2915 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
2916 if (ret < 0)
2917 goto out;
2918 }
2919
2920out:
2921 fs_path_free(from_path);
2922 fs_path_free(to_path);
2923 sctx->send_progress = orig_progress;
2924
2925 return ret;
2926}
2927
2928static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
2929{
2930 if (!list_empty(&m->list))
2931 list_del(&m->list);
2932 if (!RB_EMPTY_NODE(&m->node))
2933 rb_erase(&m->node, &sctx->pending_dir_moves);
2934 __free_recorded_refs(&m->update_refs);
2935 kfree(m);
2936}
2937
2938static void tail_append_pending_moves(struct pending_dir_move *moves,
2939 struct list_head *stack)
2940{
2941 if (list_empty(&moves->list)) {
2942 list_add_tail(&moves->list, stack);
2943 } else {
2944 LIST_HEAD(list);
2945 list_splice_init(&moves->list, &list);
2946 list_add_tail(&moves->list, stack);
2947 list_splice_tail(&list, stack);
2948 }
2949}
2950
2951static int apply_children_dir_moves(struct send_ctx *sctx)
2952{
2953 struct pending_dir_move *pm;
2954 struct list_head stack;
2955 u64 parent_ino = sctx->cur_ino;
2956 int ret = 0;
2957
2958 pm = get_pending_dir_moves(sctx, parent_ino);
2959 if (!pm)
2960 return 0;
2961
2962 INIT_LIST_HEAD(&stack);
2963 tail_append_pending_moves(pm, &stack);
2964
2965 while (!list_empty(&stack)) {
2966 pm = list_first_entry(&stack, struct pending_dir_move, list);
2967 parent_ino = pm->ino;
2968 ret = apply_dir_move(sctx, pm);
2969 free_pending_move(sctx, pm);
2970 if (ret)
2971 goto out;
2972 pm = get_pending_dir_moves(sctx, parent_ino);
2973 if (pm)
2974 tail_append_pending_moves(pm, &stack);
2975 }
2976 return 0;
2977
2978out:
2979 while (!list_empty(&stack)) {
2980 pm = list_first_entry(&stack, struct pending_dir_move, list);
2981 free_pending_move(sctx, pm);
2982 }
2983 return ret;
2984}
2985
2986static int wait_for_parent_move(struct send_ctx *sctx,
2987 struct recorded_ref *parent_ref)
2988{
2989 int ret;
2990 u64 ino = parent_ref->dir;
2991 u64 parent_ino_before, parent_ino_after;
2992 u64 new_gen, old_gen;
2993 struct fs_path *path_before = NULL;
2994 struct fs_path *path_after = NULL;
2995 int len1, len2;
2996
2997 if (parent_ref->dir <= sctx->cur_ino)
2998 return 0;
2999
3000 if (is_waiting_for_move(sctx, ino))
3001 return 1;
3002
3003 ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen,
3004 NULL, NULL, NULL, NULL);
3005 if (ret == -ENOENT)
3006 return 0;
3007 else if (ret < 0)
3008 return ret;
3009
3010 ret = get_inode_info(sctx->send_root, ino, NULL, &new_gen,
3011 NULL, NULL, NULL, NULL);
3012 if (ret < 0)
3013 return ret;
3014
3015 if (new_gen != old_gen)
3016 return 0;
3017
3018 path_before = fs_path_alloc();
3019 if (!path_before)
3020 return -ENOMEM;
3021
3022 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3023 NULL, path_before);
3024 if (ret == -ENOENT) {
3025 ret = 0;
3026 goto out;
3027 } else if (ret < 0) {
3028 goto out;
3029 }
3030
3031 path_after = fs_path_alloc();
3032 if (!path_after) {
3033 ret = -ENOMEM;
3034 goto out;
3035 }
3036
3037 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3038 NULL, path_after);
3039 if (ret == -ENOENT) {
3040 ret = 0;
3041 goto out;
3042 } else if (ret < 0) {
3043 goto out;
3044 }
3045
3046 len1 = fs_path_len(path_before);
3047 len2 = fs_path_len(path_after);
Filipe David Borba Manana5ed7f9f2014-02-01 02:02:16 +00003048 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3049 memcmp(path_before->start, path_after->start, len1)) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003050 ret = 1;
3051 goto out;
3052 }
3053 ret = 0;
3054
3055out:
3056 fs_path_free(path_before);
3057 fs_path_free(path_after);
3058
3059 return ret;
3060}
3061
Alexander Block31db9f72012-07-25 23:19:24 +02003062/*
3063 * This does all the move/link/unlink/rmdir magic.
3064 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003065static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003066{
3067 int ret = 0;
3068 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003069 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003070 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003071 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003072 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003073 u64 ow_gen;
3074 int did_overwrite = 0;
3075 int is_orphan = 0;
3076
3077verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3078
Alexander Block6d85ed02012-08-01 14:48:59 +02003079 /*
3080 * This should never happen as the root dir always has the same ref
3081 * which is always '..'
3082 */
3083 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003084 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003085
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003086 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003087 if (!valid_path) {
3088 ret = -ENOMEM;
3089 goto out;
3090 }
3091
Alexander Block31db9f72012-07-25 23:19:24 +02003092 /*
3093 * First, check if the first ref of the current inode was overwritten
3094 * before. If yes, we know that the current inode was already orphanized
3095 * and thus use the orphan name. If not, we can use get_cur_path to
3096 * get the path of the first ref as it would like while receiving at
3097 * this point in time.
3098 * New inodes are always orphan at the beginning, so force to use the
3099 * orphan name in this case.
3100 * The first ref is stored in valid_path and will be updated if it
3101 * gets moved around.
3102 */
3103 if (!sctx->cur_inode_new) {
3104 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3105 sctx->cur_inode_gen);
3106 if (ret < 0)
3107 goto out;
3108 if (ret)
3109 did_overwrite = 1;
3110 }
3111 if (sctx->cur_inode_new || did_overwrite) {
3112 ret = gen_unique_name(sctx, sctx->cur_ino,
3113 sctx->cur_inode_gen, valid_path);
3114 if (ret < 0)
3115 goto out;
3116 is_orphan = 1;
3117 } else {
3118 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3119 valid_path);
3120 if (ret < 0)
3121 goto out;
3122 }
3123
3124 list_for_each_entry(cur, &sctx->new_refs, list) {
3125 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003126 * We may have refs where the parent directory does not exist
3127 * yet. This happens if the parent directories inum is higher
3128 * the the current inum. To handle this case, we create the
3129 * parent directory out of order. But we need to check if this
3130 * did already happen before due to other refs in the same dir.
3131 */
3132 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3133 if (ret < 0)
3134 goto out;
3135 if (ret == inode_state_will_create) {
3136 ret = 0;
3137 /*
3138 * First check if any of the current inodes refs did
3139 * already create the dir.
3140 */
3141 list_for_each_entry(cur2, &sctx->new_refs, list) {
3142 if (cur == cur2)
3143 break;
3144 if (cur2->dir == cur->dir) {
3145 ret = 1;
3146 break;
3147 }
3148 }
3149
3150 /*
3151 * If that did not happen, check if a previous inode
3152 * did already create the dir.
3153 */
3154 if (!ret)
3155 ret = did_create_dir(sctx, cur->dir);
3156 if (ret < 0)
3157 goto out;
3158 if (!ret) {
3159 ret = send_create_inode(sctx, cur->dir);
3160 if (ret < 0)
3161 goto out;
3162 }
3163 }
3164
3165 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003166 * Check if this new ref would overwrite the first ref of
3167 * another unprocessed inode. If yes, orphanize the
3168 * overwritten inode. If we find an overwritten ref that is
3169 * not the first ref, simply unlink it.
3170 */
3171 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3172 cur->name, cur->name_len,
3173 &ow_inode, &ow_gen);
3174 if (ret < 0)
3175 goto out;
3176 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003177 ret = is_first_ref(sctx->parent_root,
3178 ow_inode, cur->dir, cur->name,
3179 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003180 if (ret < 0)
3181 goto out;
3182 if (ret) {
3183 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3184 cur->full_path);
3185 if (ret < 0)
3186 goto out;
3187 } else {
3188 ret = send_unlink(sctx, cur->full_path);
3189 if (ret < 0)
3190 goto out;
3191 }
3192 }
3193
3194 /*
3195 * link/move the ref to the new place. If we have an orphan
3196 * inode, move it and update valid_path. If not, link or move
3197 * it depending on the inode mode.
3198 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003199 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003200 ret = send_rename(sctx, valid_path, cur->full_path);
3201 if (ret < 0)
3202 goto out;
3203 is_orphan = 0;
3204 ret = fs_path_copy(valid_path, cur->full_path);
3205 if (ret < 0)
3206 goto out;
3207 } else {
3208 if (S_ISDIR(sctx->cur_inode_mode)) {
3209 /*
3210 * Dirs can't be linked, so move it. For moved
3211 * dirs, we always have one new and one deleted
3212 * ref. The deleted ref is ignored later.
3213 */
Filipe David Borba Mananad86477b2014-01-30 13:27:12 +00003214 ret = wait_for_parent_move(sctx, cur);
3215 if (ret < 0)
3216 goto out;
3217 if (ret) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003218 ret = add_pending_dir_move(sctx,
3219 cur->dir);
3220 *pending_move = 1;
3221 } else {
3222 ret = send_rename(sctx, valid_path,
3223 cur->full_path);
3224 if (!ret)
3225 ret = fs_path_copy(valid_path,
3226 cur->full_path);
3227 }
Alexander Block31db9f72012-07-25 23:19:24 +02003228 if (ret < 0)
3229 goto out;
3230 } else {
3231 ret = send_link(sctx, cur->full_path,
3232 valid_path);
3233 if (ret < 0)
3234 goto out;
3235 }
3236 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003237 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003238 if (ret < 0)
3239 goto out;
3240 }
3241
3242 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3243 /*
3244 * Check if we can already rmdir the directory. If not,
3245 * orphanize it. For every dir item inside that gets deleted
3246 * later, we do this check again and rmdir it then if possible.
3247 * See the use of check_dirs for more details.
3248 */
3249 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
3250 if (ret < 0)
3251 goto out;
3252 if (ret) {
3253 ret = send_rmdir(sctx, valid_path);
3254 if (ret < 0)
3255 goto out;
3256 } else if (!is_orphan) {
3257 ret = orphanize_inode(sctx, sctx->cur_ino,
3258 sctx->cur_inode_gen, valid_path);
3259 if (ret < 0)
3260 goto out;
3261 is_orphan = 1;
3262 }
3263
3264 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003265 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003266 if (ret < 0)
3267 goto out;
3268 }
Alexander Blockccf16262012-07-28 11:46:29 +02003269 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3270 !list_empty(&sctx->deleted_refs)) {
3271 /*
3272 * We have a moved dir. Add the old parent to check_dirs
3273 */
3274 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3275 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003276 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003277 if (ret < 0)
3278 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003279 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3280 /*
3281 * We have a non dir inode. Go through all deleted refs and
3282 * unlink them if they were not already overwritten by other
3283 * inodes.
3284 */
3285 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3286 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3287 sctx->cur_ino, sctx->cur_inode_gen,
3288 cur->name, cur->name_len);
3289 if (ret < 0)
3290 goto out;
3291 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003292 ret = send_unlink(sctx, cur->full_path);
3293 if (ret < 0)
3294 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003295 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003296 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003297 if (ret < 0)
3298 goto out;
3299 }
Alexander Block31db9f72012-07-25 23:19:24 +02003300 /*
3301 * If the inode is still orphan, unlink the orphan. This may
3302 * happen when a previous inode did overwrite the first ref
3303 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003304 * inode. Unlinking does not mean that the inode is deleted in
3305 * all cases. There may still be links to this inode in other
3306 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003307 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003308 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003309 ret = send_unlink(sctx, valid_path);
3310 if (ret < 0)
3311 goto out;
3312 }
3313 }
3314
3315 /*
3316 * We did collect all parent dirs where cur_inode was once located. We
3317 * now go through all these dirs and check if they are pending for
3318 * deletion and if it's finally possible to perform the rmdir now.
3319 * We also update the inode stats of the parent dirs here.
3320 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003321 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003322 /*
3323 * In case we had refs into dirs that were not processed yet,
3324 * we don't need to do the utime and rmdir logic for these dirs.
3325 * The dir will be processed later.
3326 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003327 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003328 continue;
3329
Josef Bacikba5e8f22013-08-16 16:52:55 -04003330 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003331 if (ret < 0)
3332 goto out;
3333
3334 if (ret == inode_state_did_create ||
3335 ret == inode_state_no_change) {
3336 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003337 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003338 if (ret < 0)
3339 goto out;
3340 } else if (ret == inode_state_did_delete) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003341 ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003342 if (ret < 0)
3343 goto out;
3344 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003345 ret = get_cur_path(sctx, cur->dir,
3346 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003347 if (ret < 0)
3348 goto out;
3349 ret = send_rmdir(sctx, valid_path);
3350 if (ret < 0)
3351 goto out;
3352 }
3353 }
3354 }
3355
Alexander Block31db9f72012-07-25 23:19:24 +02003356 ret = 0;
3357
3358out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04003359 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003360 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003361 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003362 return ret;
3363}
3364
3365static int __record_new_ref(int num, u64 dir, int index,
3366 struct fs_path *name,
3367 void *ctx)
3368{
3369 int ret = 0;
3370 struct send_ctx *sctx = ctx;
3371 struct fs_path *p;
3372 u64 gen;
3373
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003374 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003375 if (!p)
3376 return -ENOMEM;
3377
3378 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003379 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003380 if (ret < 0)
3381 goto out;
3382
Alexander Block31db9f72012-07-25 23:19:24 +02003383 ret = get_cur_path(sctx, dir, gen, p);
3384 if (ret < 0)
3385 goto out;
3386 ret = fs_path_add_path(p, name);
3387 if (ret < 0)
3388 goto out;
3389
3390 ret = record_ref(&sctx->new_refs, dir, gen, p);
3391
3392out:
3393 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003394 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003395 return ret;
3396}
3397
3398static int __record_deleted_ref(int num, u64 dir, int index,
3399 struct fs_path *name,
3400 void *ctx)
3401{
3402 int ret = 0;
3403 struct send_ctx *sctx = ctx;
3404 struct fs_path *p;
3405 u64 gen;
3406
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003407 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003408 if (!p)
3409 return -ENOMEM;
3410
3411 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003412 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003413 if (ret < 0)
3414 goto out;
3415
3416 ret = get_cur_path(sctx, dir, gen, p);
3417 if (ret < 0)
3418 goto out;
3419 ret = fs_path_add_path(p, name);
3420 if (ret < 0)
3421 goto out;
3422
3423 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3424
3425out:
3426 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003427 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003428 return ret;
3429}
3430
3431static int record_new_ref(struct send_ctx *sctx)
3432{
3433 int ret;
3434
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003435 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3436 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003437 if (ret < 0)
3438 goto out;
3439 ret = 0;
3440
3441out:
3442 return ret;
3443}
3444
3445static int record_deleted_ref(struct send_ctx *sctx)
3446{
3447 int ret;
3448
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003449 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3450 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003451 if (ret < 0)
3452 goto out;
3453 ret = 0;
3454
3455out:
3456 return ret;
3457}
3458
3459struct find_ref_ctx {
3460 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003461 u64 dir_gen;
3462 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003463 struct fs_path *name;
3464 int found_idx;
3465};
3466
3467static int __find_iref(int num, u64 dir, int index,
3468 struct fs_path *name,
3469 void *ctx_)
3470{
3471 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003472 u64 dir_gen;
3473 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003474
3475 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3476 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003477 /*
3478 * To avoid doing extra lookups we'll only do this if everything
3479 * else matches.
3480 */
3481 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3482 NULL, NULL, NULL);
3483 if (ret)
3484 return ret;
3485 if (dir_gen != ctx->dir_gen)
3486 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003487 ctx->found_idx = num;
3488 return 1;
3489 }
3490 return 0;
3491}
3492
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003493static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003494 struct btrfs_path *path,
3495 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003496 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003497{
3498 int ret;
3499 struct find_ref_ctx ctx;
3500
3501 ctx.dir = dir;
3502 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003503 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003504 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003505 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003506
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003507 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003508 if (ret < 0)
3509 return ret;
3510
3511 if (ctx.found_idx == -1)
3512 return -ENOENT;
3513
3514 return ctx.found_idx;
3515}
3516
3517static int __record_changed_new_ref(int num, u64 dir, int index,
3518 struct fs_path *name,
3519 void *ctx)
3520{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003521 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003522 int ret;
3523 struct send_ctx *sctx = ctx;
3524
Josef Bacikba5e8f22013-08-16 16:52:55 -04003525 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3526 NULL, NULL, NULL);
3527 if (ret)
3528 return ret;
3529
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003530 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003531 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003532 if (ret == -ENOENT)
3533 ret = __record_new_ref(num, dir, index, name, sctx);
3534 else if (ret > 0)
3535 ret = 0;
3536
3537 return ret;
3538}
3539
3540static int __record_changed_deleted_ref(int num, u64 dir, int index,
3541 struct fs_path *name,
3542 void *ctx)
3543{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003544 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003545 int ret;
3546 struct send_ctx *sctx = ctx;
3547
Josef Bacikba5e8f22013-08-16 16:52:55 -04003548 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3549 NULL, NULL, NULL);
3550 if (ret)
3551 return ret;
3552
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003553 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003554 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003555 if (ret == -ENOENT)
3556 ret = __record_deleted_ref(num, dir, index, name, sctx);
3557 else if (ret > 0)
3558 ret = 0;
3559
3560 return ret;
3561}
3562
3563static int record_changed_ref(struct send_ctx *sctx)
3564{
3565 int ret = 0;
3566
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003567 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003568 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3569 if (ret < 0)
3570 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003571 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003572 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3573 if (ret < 0)
3574 goto out;
3575 ret = 0;
3576
3577out:
3578 return ret;
3579}
3580
3581/*
3582 * Record and process all refs at once. Needed when an inode changes the
3583 * generation number, which means that it was deleted and recreated.
3584 */
3585static int process_all_refs(struct send_ctx *sctx,
3586 enum btrfs_compare_tree_result cmd)
3587{
3588 int ret;
3589 struct btrfs_root *root;
3590 struct btrfs_path *path;
3591 struct btrfs_key key;
3592 struct btrfs_key found_key;
3593 struct extent_buffer *eb;
3594 int slot;
3595 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003596 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003597
3598 path = alloc_path_for_send();
3599 if (!path)
3600 return -ENOMEM;
3601
3602 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3603 root = sctx->send_root;
3604 cb = __record_new_ref;
3605 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3606 root = sctx->parent_root;
3607 cb = __record_deleted_ref;
3608 } else {
3609 BUG();
3610 }
3611
3612 key.objectid = sctx->cmp_key->objectid;
3613 key.type = BTRFS_INODE_REF_KEY;
3614 key.offset = 0;
3615 while (1) {
3616 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
Alexander Blocke938c8a2012-07-28 16:33:49 +02003617 if (ret < 0)
Alexander Block31db9f72012-07-25 23:19:24 +02003618 goto out;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003619 if (ret)
Alexander Block31db9f72012-07-25 23:19:24 +02003620 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003621
3622 eb = path->nodes[0];
3623 slot = path->slots[0];
3624 btrfs_item_key_to_cpu(eb, &found_key, slot);
3625
3626 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003627 (found_key.type != BTRFS_INODE_REF_KEY &&
3628 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003629 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003630
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003631 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003632 btrfs_release_path(path);
3633 if (ret < 0)
3634 goto out;
3635
3636 key.offset = found_key.offset + 1;
3637 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003638 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003639
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003640 ret = process_recorded_refs(sctx, &pending_move);
3641 /* Only applicable to an incremental send. */
3642 ASSERT(pending_move == 0);
Alexander Block31db9f72012-07-25 23:19:24 +02003643
3644out:
3645 btrfs_free_path(path);
3646 return ret;
3647}
3648
3649static int send_set_xattr(struct send_ctx *sctx,
3650 struct fs_path *path,
3651 const char *name, int name_len,
3652 const char *data, int data_len)
3653{
3654 int ret = 0;
3655
3656 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3657 if (ret < 0)
3658 goto out;
3659
3660 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3661 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3662 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3663
3664 ret = send_cmd(sctx);
3665
3666tlv_put_failure:
3667out:
3668 return ret;
3669}
3670
3671static int send_remove_xattr(struct send_ctx *sctx,
3672 struct fs_path *path,
3673 const char *name, int name_len)
3674{
3675 int ret = 0;
3676
3677 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3678 if (ret < 0)
3679 goto out;
3680
3681 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3682 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3683
3684 ret = send_cmd(sctx);
3685
3686tlv_put_failure:
3687out:
3688 return ret;
3689}
3690
3691static int __process_new_xattr(int num, struct btrfs_key *di_key,
3692 const char *name, int name_len,
3693 const char *data, int data_len,
3694 u8 type, void *ctx)
3695{
3696 int ret;
3697 struct send_ctx *sctx = ctx;
3698 struct fs_path *p;
3699 posix_acl_xattr_header dummy_acl;
3700
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003701 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003702 if (!p)
3703 return -ENOMEM;
3704
3705 /*
3706 * This hack is needed because empty acl's are stored as zero byte
3707 * data in xattrs. Problem with that is, that receiving these zero byte
3708 * acl's will fail later. To fix this, we send a dummy acl list that
3709 * only contains the version number and no entries.
3710 */
3711 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3712 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3713 if (data_len == 0) {
3714 dummy_acl.a_version =
3715 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3716 data = (char *)&dummy_acl;
3717 data_len = sizeof(dummy_acl);
3718 }
3719 }
3720
3721 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3722 if (ret < 0)
3723 goto out;
3724
3725 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3726
3727out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003728 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003729 return ret;
3730}
3731
3732static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3733 const char *name, int name_len,
3734 const char *data, int data_len,
3735 u8 type, void *ctx)
3736{
3737 int ret;
3738 struct send_ctx *sctx = ctx;
3739 struct fs_path *p;
3740
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003741 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003742 if (!p)
3743 return -ENOMEM;
3744
3745 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3746 if (ret < 0)
3747 goto out;
3748
3749 ret = send_remove_xattr(sctx, p, name, name_len);
3750
3751out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003752 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003753 return ret;
3754}
3755
3756static int process_new_xattr(struct send_ctx *sctx)
3757{
3758 int ret = 0;
3759
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003760 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3761 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003762
3763 return ret;
3764}
3765
3766static int process_deleted_xattr(struct send_ctx *sctx)
3767{
3768 int ret;
3769
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003770 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3771 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003772
3773 return ret;
3774}
3775
3776struct find_xattr_ctx {
3777 const char *name;
3778 int name_len;
3779 int found_idx;
3780 char *found_data;
3781 int found_data_len;
3782};
3783
3784static int __find_xattr(int num, struct btrfs_key *di_key,
3785 const char *name, int name_len,
3786 const char *data, int data_len,
3787 u8 type, void *vctx)
3788{
3789 struct find_xattr_ctx *ctx = vctx;
3790
3791 if (name_len == ctx->name_len &&
3792 strncmp(name, ctx->name, name_len) == 0) {
3793 ctx->found_idx = num;
3794 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00003795 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02003796 if (!ctx->found_data)
3797 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02003798 return 1;
3799 }
3800 return 0;
3801}
3802
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003803static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003804 struct btrfs_path *path,
3805 struct btrfs_key *key,
3806 const char *name, int name_len,
3807 char **data, int *data_len)
3808{
3809 int ret;
3810 struct find_xattr_ctx ctx;
3811
3812 ctx.name = name;
3813 ctx.name_len = name_len;
3814 ctx.found_idx = -1;
3815 ctx.found_data = NULL;
3816 ctx.found_data_len = 0;
3817
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003818 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003819 if (ret < 0)
3820 return ret;
3821
3822 if (ctx.found_idx == -1)
3823 return -ENOENT;
3824 if (data) {
3825 *data = ctx.found_data;
3826 *data_len = ctx.found_data_len;
3827 } else {
3828 kfree(ctx.found_data);
3829 }
3830 return ctx.found_idx;
3831}
3832
3833
3834static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3835 const char *name, int name_len,
3836 const char *data, int data_len,
3837 u8 type, void *ctx)
3838{
3839 int ret;
3840 struct send_ctx *sctx = ctx;
3841 char *found_data = NULL;
3842 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003843
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003844 ret = find_xattr(sctx->parent_root, sctx->right_path,
3845 sctx->cmp_key, name, name_len, &found_data,
3846 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003847 if (ret == -ENOENT) {
3848 ret = __process_new_xattr(num, di_key, name, name_len, data,
3849 data_len, type, ctx);
3850 } else if (ret >= 0) {
3851 if (data_len != found_data_len ||
3852 memcmp(data, found_data, data_len)) {
3853 ret = __process_new_xattr(num, di_key, name, name_len,
3854 data, data_len, type, ctx);
3855 } else {
3856 ret = 0;
3857 }
3858 }
3859
3860 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02003861 return ret;
3862}
3863
3864static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3865 const char *name, int name_len,
3866 const char *data, int data_len,
3867 u8 type, void *ctx)
3868{
3869 int ret;
3870 struct send_ctx *sctx = ctx;
3871
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003872 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
3873 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003874 if (ret == -ENOENT)
3875 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3876 data_len, type, ctx);
3877 else if (ret >= 0)
3878 ret = 0;
3879
3880 return ret;
3881}
3882
3883static int process_changed_xattr(struct send_ctx *sctx)
3884{
3885 int ret = 0;
3886
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003887 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003888 sctx->cmp_key, __process_changed_new_xattr, sctx);
3889 if (ret < 0)
3890 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003891 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003892 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3893
3894out:
3895 return ret;
3896}
3897
3898static int process_all_new_xattrs(struct send_ctx *sctx)
3899{
3900 int ret;
3901 struct btrfs_root *root;
3902 struct btrfs_path *path;
3903 struct btrfs_key key;
3904 struct btrfs_key found_key;
3905 struct extent_buffer *eb;
3906 int slot;
3907
3908 path = alloc_path_for_send();
3909 if (!path)
3910 return -ENOMEM;
3911
3912 root = sctx->send_root;
3913
3914 key.objectid = sctx->cmp_key->objectid;
3915 key.type = BTRFS_XATTR_ITEM_KEY;
3916 key.offset = 0;
3917 while (1) {
3918 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3919 if (ret < 0)
3920 goto out;
3921 if (ret) {
3922 ret = 0;
3923 goto out;
3924 }
3925
3926 eb = path->nodes[0];
3927 slot = path->slots[0];
3928 btrfs_item_key_to_cpu(eb, &found_key, slot);
3929
3930 if (found_key.objectid != key.objectid ||
3931 found_key.type != key.type) {
3932 ret = 0;
3933 goto out;
3934 }
3935
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003936 ret = iterate_dir_item(root, path, &found_key,
3937 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003938 if (ret < 0)
3939 goto out;
3940
3941 btrfs_release_path(path);
3942 key.offset = found_key.offset + 1;
3943 }
3944
3945out:
3946 btrfs_free_path(path);
3947 return ret;
3948}
3949
Josef Baciked259092013-10-25 11:36:01 -04003950static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
3951{
3952 struct btrfs_root *root = sctx->send_root;
3953 struct btrfs_fs_info *fs_info = root->fs_info;
3954 struct inode *inode;
3955 struct page *page;
3956 char *addr;
3957 struct btrfs_key key;
3958 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
3959 pgoff_t last_index;
3960 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
3961 ssize_t ret = 0;
3962
3963 key.objectid = sctx->cur_ino;
3964 key.type = BTRFS_INODE_ITEM_KEY;
3965 key.offset = 0;
3966
3967 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3968 if (IS_ERR(inode))
3969 return PTR_ERR(inode);
3970
3971 if (offset + len > i_size_read(inode)) {
3972 if (offset > i_size_read(inode))
3973 len = 0;
3974 else
3975 len = offset - i_size_read(inode);
3976 }
3977 if (len == 0)
3978 goto out;
3979
3980 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
3981 while (index <= last_index) {
3982 unsigned cur_len = min_t(unsigned, len,
3983 PAGE_CACHE_SIZE - pg_offset);
3984 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3985 if (!page) {
3986 ret = -ENOMEM;
3987 break;
3988 }
3989
3990 if (!PageUptodate(page)) {
3991 btrfs_readpage(NULL, page);
3992 lock_page(page);
3993 if (!PageUptodate(page)) {
3994 unlock_page(page);
3995 page_cache_release(page);
3996 ret = -EIO;
3997 break;
3998 }
3999 }
4000
4001 addr = kmap(page);
4002 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4003 kunmap(page);
4004 unlock_page(page);
4005 page_cache_release(page);
4006 index++;
4007 pg_offset = 0;
4008 len -= cur_len;
4009 ret += cur_len;
4010 }
4011out:
4012 iput(inode);
4013 return ret;
4014}
4015
Alexander Block31db9f72012-07-25 23:19:24 +02004016/*
4017 * Read some bytes from the current inode/file and send a write command to
4018 * user space.
4019 */
4020static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4021{
4022 int ret = 0;
4023 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004024 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004025
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004026 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004027 if (!p)
4028 return -ENOMEM;
4029
Alexander Block31db9f72012-07-25 23:19:24 +02004030verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4031
Josef Baciked259092013-10-25 11:36:01 -04004032 num_read = fill_read_buf(sctx, offset, len);
4033 if (num_read <= 0) {
4034 if (num_read < 0)
4035 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004036 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004037 }
Alexander Block31db9f72012-07-25 23:19:24 +02004038
4039 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4040 if (ret < 0)
4041 goto out;
4042
4043 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4044 if (ret < 0)
4045 goto out;
4046
4047 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4048 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004049 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004050
4051 ret = send_cmd(sctx);
4052
4053tlv_put_failure:
4054out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004055 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004056 if (ret < 0)
4057 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004058 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004059}
4060
4061/*
4062 * Send a clone command to user space.
4063 */
4064static int send_clone(struct send_ctx *sctx,
4065 u64 offset, u32 len,
4066 struct clone_root *clone_root)
4067{
4068 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004069 struct fs_path *p;
4070 u64 gen;
4071
4072verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4073 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4074 clone_root->root->objectid, clone_root->ino,
4075 clone_root->offset);
4076
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004077 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004078 if (!p)
4079 return -ENOMEM;
4080
4081 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4082 if (ret < 0)
4083 goto out;
4084
4085 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4086 if (ret < 0)
4087 goto out;
4088
4089 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4090 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4091 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4092
Alexander Blocke938c8a2012-07-28 16:33:49 +02004093 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004094 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004095 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004096 if (ret < 0)
4097 goto out;
4098 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4099 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004100 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004101 }
4102 if (ret < 0)
4103 goto out;
4104
4105 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02004106 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004107 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004108 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004109 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4110 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4111 clone_root->offset);
4112
4113 ret = send_cmd(sctx);
4114
4115tlv_put_failure:
4116out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004117 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004118 return ret;
4119}
4120
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004121/*
4122 * Send an update extent command to user space.
4123 */
4124static int send_update_extent(struct send_ctx *sctx,
4125 u64 offset, u32 len)
4126{
4127 int ret = 0;
4128 struct fs_path *p;
4129
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004130 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004131 if (!p)
4132 return -ENOMEM;
4133
4134 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4135 if (ret < 0)
4136 goto out;
4137
4138 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4139 if (ret < 0)
4140 goto out;
4141
4142 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4143 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4144 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4145
4146 ret = send_cmd(sctx);
4147
4148tlv_put_failure:
4149out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004150 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004151 return ret;
4152}
4153
Josef Bacik16e75492013-10-22 12:18:51 -04004154static int send_hole(struct send_ctx *sctx, u64 end)
4155{
4156 struct fs_path *p = NULL;
4157 u64 offset = sctx->cur_inode_last_extent;
4158 u64 len;
4159 int ret = 0;
4160
4161 p = fs_path_alloc();
4162 if (!p)
4163 return -ENOMEM;
4164 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4165 while (offset < end) {
4166 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4167
4168 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4169 if (ret < 0)
4170 break;
4171 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4172 if (ret < 0)
4173 break;
4174 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4175 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4176 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4177 ret = send_cmd(sctx);
4178 if (ret < 0)
4179 break;
4180 offset += len;
4181 }
4182tlv_put_failure:
4183 fs_path_free(p);
4184 return ret;
4185}
4186
Alexander Block31db9f72012-07-25 23:19:24 +02004187static int send_write_or_clone(struct send_ctx *sctx,
4188 struct btrfs_path *path,
4189 struct btrfs_key *key,
4190 struct clone_root *clone_root)
4191{
4192 int ret = 0;
4193 struct btrfs_file_extent_item *ei;
4194 u64 offset = key->offset;
4195 u64 pos = 0;
4196 u64 len;
4197 u32 l;
4198 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004199 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02004200
4201 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4202 struct btrfs_file_extent_item);
4203 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004204 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004205 len = btrfs_file_extent_inline_len(path->nodes[0],
4206 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004207 /*
4208 * it is possible the inline item won't cover the whole page,
4209 * but there may be items after this page. Make
4210 * sure to send the whole thing
4211 */
4212 len = PAGE_CACHE_ALIGN(len);
4213 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004214 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004215 }
Alexander Block31db9f72012-07-25 23:19:24 +02004216
4217 if (offset + len > sctx->cur_inode_size)
4218 len = sctx->cur_inode_size - offset;
4219 if (len == 0) {
4220 ret = 0;
4221 goto out;
4222 }
4223
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004224 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004225 ret = send_clone(sctx, offset, len, clone_root);
4226 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4227 ret = send_update_extent(sctx, offset, len);
4228 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004229 while (pos < len) {
4230 l = len - pos;
4231 if (l > BTRFS_SEND_READ_SIZE)
4232 l = BTRFS_SEND_READ_SIZE;
4233 ret = send_write(sctx, pos + offset, l);
4234 if (ret < 0)
4235 goto out;
4236 if (!ret)
4237 break;
4238 pos += ret;
4239 }
4240 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004241 }
Alexander Block31db9f72012-07-25 23:19:24 +02004242out:
4243 return ret;
4244}
4245
4246static int is_extent_unchanged(struct send_ctx *sctx,
4247 struct btrfs_path *left_path,
4248 struct btrfs_key *ekey)
4249{
4250 int ret = 0;
4251 struct btrfs_key key;
4252 struct btrfs_path *path = NULL;
4253 struct extent_buffer *eb;
4254 int slot;
4255 struct btrfs_key found_key;
4256 struct btrfs_file_extent_item *ei;
4257 u64 left_disknr;
4258 u64 right_disknr;
4259 u64 left_offset;
4260 u64 right_offset;
4261 u64 left_offset_fixed;
4262 u64 left_len;
4263 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04004264 u64 left_gen;
4265 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004266 u8 left_type;
4267 u8 right_type;
4268
4269 path = alloc_path_for_send();
4270 if (!path)
4271 return -ENOMEM;
4272
4273 eb = left_path->nodes[0];
4274 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02004275 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4276 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004277
4278 if (left_type != BTRFS_FILE_EXTENT_REG) {
4279 ret = 0;
4280 goto out;
4281 }
Chris Mason74dd17f2012-08-07 16:25:13 -04004282 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4283 left_len = btrfs_file_extent_num_bytes(eb, ei);
4284 left_offset = btrfs_file_extent_offset(eb, ei);
4285 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004286
4287 /*
4288 * Following comments will refer to these graphics. L is the left
4289 * extents which we are checking at the moment. 1-8 are the right
4290 * extents that we iterate.
4291 *
4292 * |-----L-----|
4293 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4294 *
4295 * |-----L-----|
4296 * |--1--|-2b-|...(same as above)
4297 *
4298 * Alternative situation. Happens on files where extents got split.
4299 * |-----L-----|
4300 * |-----------7-----------|-6-|
4301 *
4302 * Alternative situation. Happens on files which got larger.
4303 * |-----L-----|
4304 * |-8-|
4305 * Nothing follows after 8.
4306 */
4307
4308 key.objectid = ekey->objectid;
4309 key.type = BTRFS_EXTENT_DATA_KEY;
4310 key.offset = ekey->offset;
4311 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4312 if (ret < 0)
4313 goto out;
4314 if (ret) {
4315 ret = 0;
4316 goto out;
4317 }
4318
4319 /*
4320 * Handle special case where the right side has no extents at all.
4321 */
4322 eb = path->nodes[0];
4323 slot = path->slots[0];
4324 btrfs_item_key_to_cpu(eb, &found_key, slot);
4325 if (found_key.objectid != key.objectid ||
4326 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004327 /* If we're a hole then just pretend nothing changed */
4328 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004329 goto out;
4330 }
4331
4332 /*
4333 * We're now on 2a, 2b or 7.
4334 */
4335 key = found_key;
4336 while (key.offset < ekey->offset + left_len) {
4337 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4338 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004339 if (right_type != BTRFS_FILE_EXTENT_REG) {
4340 ret = 0;
4341 goto out;
4342 }
4343
Josef Bacik007d31f2013-10-31 16:49:02 -04004344 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4345 right_len = btrfs_file_extent_num_bytes(eb, ei);
4346 right_offset = btrfs_file_extent_offset(eb, ei);
4347 right_gen = btrfs_file_extent_generation(eb, ei);
4348
Alexander Block31db9f72012-07-25 23:19:24 +02004349 /*
4350 * Are we at extent 8? If yes, we know the extent is changed.
4351 * This may only happen on the first iteration.
4352 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02004353 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004354 /* If we're a hole just pretend nothing changed */
4355 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004356 goto out;
4357 }
4358
4359 left_offset_fixed = left_offset;
4360 if (key.offset < ekey->offset) {
4361 /* Fix the right offset for 2a and 7. */
4362 right_offset += ekey->offset - key.offset;
4363 } else {
4364 /* Fix the left offset for all behind 2a and 2b */
4365 left_offset_fixed += key.offset - ekey->offset;
4366 }
4367
4368 /*
4369 * Check if we have the same extent.
4370 */
Alexander Block39540962012-08-01 12:46:05 +02004371 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04004372 left_offset_fixed != right_offset ||
4373 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02004374 ret = 0;
4375 goto out;
4376 }
4377
4378 /*
4379 * Go to the next extent.
4380 */
4381 ret = btrfs_next_item(sctx->parent_root, path);
4382 if (ret < 0)
4383 goto out;
4384 if (!ret) {
4385 eb = path->nodes[0];
4386 slot = path->slots[0];
4387 btrfs_item_key_to_cpu(eb, &found_key, slot);
4388 }
4389 if (ret || found_key.objectid != key.objectid ||
4390 found_key.type != key.type) {
4391 key.offset += right_len;
4392 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00004393 }
4394 if (found_key.offset != key.offset + right_len) {
4395 ret = 0;
4396 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004397 }
4398 key = found_key;
4399 }
4400
4401 /*
4402 * We're now behind the left extent (treat as unchanged) or at the end
4403 * of the right side (treat as changed).
4404 */
4405 if (key.offset >= ekey->offset + left_len)
4406 ret = 1;
4407 else
4408 ret = 0;
4409
4410
4411out:
4412 btrfs_free_path(path);
4413 return ret;
4414}
4415
Josef Bacik16e75492013-10-22 12:18:51 -04004416static int get_last_extent(struct send_ctx *sctx, u64 offset)
4417{
4418 struct btrfs_path *path;
4419 struct btrfs_root *root = sctx->send_root;
4420 struct btrfs_file_extent_item *fi;
4421 struct btrfs_key key;
4422 u64 extent_end;
4423 u8 type;
4424 int ret;
4425
4426 path = alloc_path_for_send();
4427 if (!path)
4428 return -ENOMEM;
4429
4430 sctx->cur_inode_last_extent = 0;
4431
4432 key.objectid = sctx->cur_ino;
4433 key.type = BTRFS_EXTENT_DATA_KEY;
4434 key.offset = offset;
4435 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4436 if (ret < 0)
4437 goto out;
4438 ret = 0;
4439 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4440 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4441 goto out;
4442
4443 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4444 struct btrfs_file_extent_item);
4445 type = btrfs_file_extent_type(path->nodes[0], fi);
4446 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004447 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4448 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004449 extent_end = ALIGN(key.offset + size,
4450 sctx->send_root->sectorsize);
4451 } else {
4452 extent_end = key.offset +
4453 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4454 }
4455 sctx->cur_inode_last_extent = extent_end;
4456out:
4457 btrfs_free_path(path);
4458 return ret;
4459}
4460
4461static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4462 struct btrfs_key *key)
4463{
4464 struct btrfs_file_extent_item *fi;
4465 u64 extent_end;
4466 u8 type;
4467 int ret = 0;
4468
4469 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4470 return 0;
4471
4472 if (sctx->cur_inode_last_extent == (u64)-1) {
4473 ret = get_last_extent(sctx, key->offset - 1);
4474 if (ret)
4475 return ret;
4476 }
4477
4478 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4479 struct btrfs_file_extent_item);
4480 type = btrfs_file_extent_type(path->nodes[0], fi);
4481 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004482 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4483 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004484 extent_end = ALIGN(key->offset + size,
4485 sctx->send_root->sectorsize);
4486 } else {
4487 extent_end = key->offset +
4488 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4489 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00004490
4491 if (path->slots[0] == 0 &&
4492 sctx->cur_inode_last_extent < key->offset) {
4493 /*
4494 * We might have skipped entire leafs that contained only
4495 * file extent items for our current inode. These leafs have
4496 * a generation number smaller (older) than the one in the
4497 * current leaf and the leaf our last extent came from, and
4498 * are located between these 2 leafs.
4499 */
4500 ret = get_last_extent(sctx, key->offset - 1);
4501 if (ret)
4502 return ret;
4503 }
4504
Josef Bacik16e75492013-10-22 12:18:51 -04004505 if (sctx->cur_inode_last_extent < key->offset)
4506 ret = send_hole(sctx, key->offset);
4507 sctx->cur_inode_last_extent = extent_end;
4508 return ret;
4509}
4510
Alexander Block31db9f72012-07-25 23:19:24 +02004511static int process_extent(struct send_ctx *sctx,
4512 struct btrfs_path *path,
4513 struct btrfs_key *key)
4514{
Alexander Block31db9f72012-07-25 23:19:24 +02004515 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004516 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004517
4518 if (S_ISLNK(sctx->cur_inode_mode))
4519 return 0;
4520
4521 if (sctx->parent_root && !sctx->cur_inode_new) {
4522 ret = is_extent_unchanged(sctx, path, key);
4523 if (ret < 0)
4524 goto out;
4525 if (ret) {
4526 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004527 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004528 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004529 } else {
4530 struct btrfs_file_extent_item *ei;
4531 u8 type;
4532
4533 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4534 struct btrfs_file_extent_item);
4535 type = btrfs_file_extent_type(path->nodes[0], ei);
4536 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4537 type == BTRFS_FILE_EXTENT_REG) {
4538 /*
4539 * The send spec does not have a prealloc command yet,
4540 * so just leave a hole for prealloc'ed extents until
4541 * we have enough commands queued up to justify rev'ing
4542 * the send spec.
4543 */
4544 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4545 ret = 0;
4546 goto out;
4547 }
4548
4549 /* Have a hole, just skip it. */
4550 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4551 ret = 0;
4552 goto out;
4553 }
4554 }
Alexander Block31db9f72012-07-25 23:19:24 +02004555 }
4556
4557 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4558 sctx->cur_inode_size, &found_clone);
4559 if (ret != -ENOENT && ret < 0)
4560 goto out;
4561
4562 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004563 if (ret)
4564 goto out;
4565out_hole:
4566 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004567out:
4568 return ret;
4569}
4570
4571static int process_all_extents(struct send_ctx *sctx)
4572{
4573 int ret;
4574 struct btrfs_root *root;
4575 struct btrfs_path *path;
4576 struct btrfs_key key;
4577 struct btrfs_key found_key;
4578 struct extent_buffer *eb;
4579 int slot;
4580
4581 root = sctx->send_root;
4582 path = alloc_path_for_send();
4583 if (!path)
4584 return -ENOMEM;
4585
4586 key.objectid = sctx->cmp_key->objectid;
4587 key.type = BTRFS_EXTENT_DATA_KEY;
4588 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4590 if (ret < 0)
4591 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004592
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004593 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004594 eb = path->nodes[0];
4595 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004596
4597 if (slot >= btrfs_header_nritems(eb)) {
4598 ret = btrfs_next_leaf(root, path);
4599 if (ret < 0) {
4600 goto out;
4601 } else if (ret > 0) {
4602 ret = 0;
4603 break;
4604 }
4605 continue;
4606 }
4607
Alexander Block31db9f72012-07-25 23:19:24 +02004608 btrfs_item_key_to_cpu(eb, &found_key, slot);
4609
4610 if (found_key.objectid != key.objectid ||
4611 found_key.type != key.type) {
4612 ret = 0;
4613 goto out;
4614 }
4615
4616 ret = process_extent(sctx, path, &found_key);
4617 if (ret < 0)
4618 goto out;
4619
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004620 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004621 }
4622
4623out:
4624 btrfs_free_path(path);
4625 return ret;
4626}
4627
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004628static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4629 int *pending_move,
4630 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02004631{
4632 int ret = 0;
4633
4634 if (sctx->cur_ino == 0)
4635 goto out;
4636 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004637 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004638 goto out;
4639 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4640 goto out;
4641
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004642 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004643 if (ret < 0)
4644 goto out;
4645
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004646 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004647out:
4648 return ret;
4649}
4650
4651static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4652{
4653 int ret = 0;
4654 u64 left_mode;
4655 u64 left_uid;
4656 u64 left_gid;
4657 u64 right_mode;
4658 u64 right_uid;
4659 u64 right_gid;
4660 int need_chmod = 0;
4661 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004662 int pending_move = 0;
4663 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004664
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004665 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4666 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02004667 if (ret < 0)
4668 goto out;
4669
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004670 /*
4671 * We have processed the refs and thus need to advance send_progress.
4672 * Now, calls to get_cur_xxx will take the updated refs of the current
4673 * inode into account.
4674 *
4675 * On the other hand, if our current inode is a directory and couldn't
4676 * be moved/renamed because its parent was renamed/moved too and it has
4677 * a higher inode number, we can only move/rename our current inode
4678 * after we moved/renamed its parent. Therefore in this case operate on
4679 * the old path (pre move/rename) of our current inode, and the
4680 * move/rename will be performed later.
4681 */
4682 if (refs_processed && !pending_move)
4683 sctx->send_progress = sctx->cur_ino + 1;
4684
Alexander Block31db9f72012-07-25 23:19:24 +02004685 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4686 goto out;
4687 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4688 goto out;
4689
4690 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004691 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004692 if (ret < 0)
4693 goto out;
4694
Alex Lyakase2d044f2012-10-17 13:52:47 +00004695 if (!sctx->parent_root || sctx->cur_inode_new) {
4696 need_chown = 1;
4697 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004698 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004699 } else {
4700 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4701 NULL, NULL, &right_mode, &right_uid,
4702 &right_gid, NULL);
4703 if (ret < 0)
4704 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004705
Alex Lyakase2d044f2012-10-17 13:52:47 +00004706 if (left_uid != right_uid || left_gid != right_gid)
4707 need_chown = 1;
4708 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4709 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004710 }
4711
4712 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04004713 if (need_send_hole(sctx)) {
4714 if (sctx->cur_inode_last_extent == (u64)-1) {
4715 ret = get_last_extent(sctx, (u64)-1);
4716 if (ret)
4717 goto out;
4718 }
4719 if (sctx->cur_inode_last_extent <
4720 sctx->cur_inode_size) {
4721 ret = send_hole(sctx, sctx->cur_inode_size);
4722 if (ret)
4723 goto out;
4724 }
4725 }
Alexander Block31db9f72012-07-25 23:19:24 +02004726 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4727 sctx->cur_inode_size);
4728 if (ret < 0)
4729 goto out;
4730 }
4731
4732 if (need_chown) {
4733 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4734 left_uid, left_gid);
4735 if (ret < 0)
4736 goto out;
4737 }
4738 if (need_chmod) {
4739 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4740 left_mode);
4741 if (ret < 0)
4742 goto out;
4743 }
4744
4745 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004746 * If other directory inodes depended on our current directory
4747 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02004748 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004749 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
4750 ret = apply_children_dir_moves(sctx);
4751 if (ret)
4752 goto out;
4753 }
4754
4755 /*
4756 * Need to send that every time, no matter if it actually
4757 * changed between the two trees as we have done changes to
4758 * the inode before.
4759 */
4760 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004761 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4762 if (ret < 0)
4763 goto out;
4764
4765out:
4766 return ret;
4767}
4768
4769static int changed_inode(struct send_ctx *sctx,
4770 enum btrfs_compare_tree_result result)
4771{
4772 int ret = 0;
4773 struct btrfs_key *key = sctx->cmp_key;
4774 struct btrfs_inode_item *left_ii = NULL;
4775 struct btrfs_inode_item *right_ii = NULL;
4776 u64 left_gen = 0;
4777 u64 right_gen = 0;
4778
Alexander Block31db9f72012-07-25 23:19:24 +02004779 sctx->cur_ino = key->objectid;
4780 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004781 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004782
4783 /*
4784 * Set send_progress to current inode. This will tell all get_cur_xxx
4785 * functions that the current inode's refs are not updated yet. Later,
4786 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4787 */
Alexander Block31db9f72012-07-25 23:19:24 +02004788 sctx->send_progress = sctx->cur_ino;
4789
4790 if (result == BTRFS_COMPARE_TREE_NEW ||
4791 result == BTRFS_COMPARE_TREE_CHANGED) {
4792 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4793 sctx->left_path->slots[0],
4794 struct btrfs_inode_item);
4795 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4796 left_ii);
4797 } else {
4798 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4799 sctx->right_path->slots[0],
4800 struct btrfs_inode_item);
4801 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4802 right_ii);
4803 }
4804 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4805 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4806 sctx->right_path->slots[0],
4807 struct btrfs_inode_item);
4808
4809 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4810 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02004811
4812 /*
4813 * The cur_ino = root dir case is special here. We can't treat
4814 * the inode as deleted+reused because it would generate a
4815 * stream that tries to delete/mkdir the root dir.
4816 */
4817 if (left_gen != right_gen &&
4818 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02004819 sctx->cur_inode_new_gen = 1;
4820 }
4821
4822 if (result == BTRFS_COMPARE_TREE_NEW) {
4823 sctx->cur_inode_gen = left_gen;
4824 sctx->cur_inode_new = 1;
4825 sctx->cur_inode_deleted = 0;
4826 sctx->cur_inode_size = btrfs_inode_size(
4827 sctx->left_path->nodes[0], left_ii);
4828 sctx->cur_inode_mode = btrfs_inode_mode(
4829 sctx->left_path->nodes[0], left_ii);
4830 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02004831 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004832 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4833 sctx->cur_inode_gen = right_gen;
4834 sctx->cur_inode_new = 0;
4835 sctx->cur_inode_deleted = 1;
4836 sctx->cur_inode_size = btrfs_inode_size(
4837 sctx->right_path->nodes[0], right_ii);
4838 sctx->cur_inode_mode = btrfs_inode_mode(
4839 sctx->right_path->nodes[0], right_ii);
4840 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02004841 /*
4842 * We need to do some special handling in case the inode was
4843 * reported as changed with a changed generation number. This
4844 * means that the original inode was deleted and new inode
4845 * reused the same inum. So we have to treat the old inode as
4846 * deleted and the new one as new.
4847 */
Alexander Block31db9f72012-07-25 23:19:24 +02004848 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02004849 /*
4850 * First, process the inode as if it was deleted.
4851 */
Alexander Block31db9f72012-07-25 23:19:24 +02004852 sctx->cur_inode_gen = right_gen;
4853 sctx->cur_inode_new = 0;
4854 sctx->cur_inode_deleted = 1;
4855 sctx->cur_inode_size = btrfs_inode_size(
4856 sctx->right_path->nodes[0], right_ii);
4857 sctx->cur_inode_mode = btrfs_inode_mode(
4858 sctx->right_path->nodes[0], right_ii);
4859 ret = process_all_refs(sctx,
4860 BTRFS_COMPARE_TREE_DELETED);
4861 if (ret < 0)
4862 goto out;
4863
Alexander Block766702e2012-07-28 14:11:31 +02004864 /*
4865 * Now process the inode as if it was new.
4866 */
Alexander Block31db9f72012-07-25 23:19:24 +02004867 sctx->cur_inode_gen = left_gen;
4868 sctx->cur_inode_new = 1;
4869 sctx->cur_inode_deleted = 0;
4870 sctx->cur_inode_size = btrfs_inode_size(
4871 sctx->left_path->nodes[0], left_ii);
4872 sctx->cur_inode_mode = btrfs_inode_mode(
4873 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02004874 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004875 if (ret < 0)
4876 goto out;
4877
4878 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4879 if (ret < 0)
4880 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004881 /*
4882 * Advance send_progress now as we did not get into
4883 * process_recorded_refs_if_needed in the new_gen case.
4884 */
4885 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02004886
4887 /*
4888 * Now process all extents and xattrs of the inode as if
4889 * they were all new.
4890 */
Alexander Block31db9f72012-07-25 23:19:24 +02004891 ret = process_all_extents(sctx);
4892 if (ret < 0)
4893 goto out;
4894 ret = process_all_new_xattrs(sctx);
4895 if (ret < 0)
4896 goto out;
4897 } else {
4898 sctx->cur_inode_gen = left_gen;
4899 sctx->cur_inode_new = 0;
4900 sctx->cur_inode_new_gen = 0;
4901 sctx->cur_inode_deleted = 0;
4902 sctx->cur_inode_size = btrfs_inode_size(
4903 sctx->left_path->nodes[0], left_ii);
4904 sctx->cur_inode_mode = btrfs_inode_mode(
4905 sctx->left_path->nodes[0], left_ii);
4906 }
4907 }
4908
4909out:
4910 return ret;
4911}
4912
Alexander Block766702e2012-07-28 14:11:31 +02004913/*
4914 * We have to process new refs before deleted refs, but compare_trees gives us
4915 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4916 * first and later process them in process_recorded_refs.
4917 * For the cur_inode_new_gen case, we skip recording completely because
4918 * changed_inode did already initiate processing of refs. The reason for this is
4919 * that in this case, compare_tree actually compares the refs of 2 different
4920 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4921 * refs of the right tree as deleted and all refs of the left tree as new.
4922 */
Alexander Block31db9f72012-07-25 23:19:24 +02004923static int changed_ref(struct send_ctx *sctx,
4924 enum btrfs_compare_tree_result result)
4925{
4926 int ret = 0;
4927
4928 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4929
4930 if (!sctx->cur_inode_new_gen &&
4931 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4932 if (result == BTRFS_COMPARE_TREE_NEW)
4933 ret = record_new_ref(sctx);
4934 else if (result == BTRFS_COMPARE_TREE_DELETED)
4935 ret = record_deleted_ref(sctx);
4936 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4937 ret = record_changed_ref(sctx);
4938 }
4939
4940 return ret;
4941}
4942
Alexander Block766702e2012-07-28 14:11:31 +02004943/*
4944 * Process new/deleted/changed xattrs. We skip processing in the
4945 * cur_inode_new_gen case because changed_inode did already initiate processing
4946 * of xattrs. The reason is the same as in changed_ref
4947 */
Alexander Block31db9f72012-07-25 23:19:24 +02004948static int changed_xattr(struct send_ctx *sctx,
4949 enum btrfs_compare_tree_result result)
4950{
4951 int ret = 0;
4952
4953 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4954
4955 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4956 if (result == BTRFS_COMPARE_TREE_NEW)
4957 ret = process_new_xattr(sctx);
4958 else if (result == BTRFS_COMPARE_TREE_DELETED)
4959 ret = process_deleted_xattr(sctx);
4960 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4961 ret = process_changed_xattr(sctx);
4962 }
4963
4964 return ret;
4965}
4966
Alexander Block766702e2012-07-28 14:11:31 +02004967/*
4968 * Process new/deleted/changed extents. We skip processing in the
4969 * cur_inode_new_gen case because changed_inode did already initiate processing
4970 * of extents. The reason is the same as in changed_ref
4971 */
Alexander Block31db9f72012-07-25 23:19:24 +02004972static int changed_extent(struct send_ctx *sctx,
4973 enum btrfs_compare_tree_result result)
4974{
4975 int ret = 0;
4976
4977 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4978
4979 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4980 if (result != BTRFS_COMPARE_TREE_DELETED)
4981 ret = process_extent(sctx, sctx->left_path,
4982 sctx->cmp_key);
4983 }
4984
4985 return ret;
4986}
4987
Josef Bacikba5e8f22013-08-16 16:52:55 -04004988static int dir_changed(struct send_ctx *sctx, u64 dir)
4989{
4990 u64 orig_gen, new_gen;
4991 int ret;
4992
4993 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
4994 NULL, NULL);
4995 if (ret)
4996 return ret;
4997
4998 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
4999 NULL, NULL, NULL);
5000 if (ret)
5001 return ret;
5002
5003 return (orig_gen != new_gen) ? 1 : 0;
5004}
5005
5006static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5007 struct btrfs_key *key)
5008{
5009 struct btrfs_inode_extref *extref;
5010 struct extent_buffer *leaf;
5011 u64 dirid = 0, last_dirid = 0;
5012 unsigned long ptr;
5013 u32 item_size;
5014 u32 cur_offset = 0;
5015 int ref_name_len;
5016 int ret = 0;
5017
5018 /* Easy case, just check this one dirid */
5019 if (key->type == BTRFS_INODE_REF_KEY) {
5020 dirid = key->offset;
5021
5022 ret = dir_changed(sctx, dirid);
5023 goto out;
5024 }
5025
5026 leaf = path->nodes[0];
5027 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5028 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5029 while (cur_offset < item_size) {
5030 extref = (struct btrfs_inode_extref *)(ptr +
5031 cur_offset);
5032 dirid = btrfs_inode_extref_parent(leaf, extref);
5033 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5034 cur_offset += ref_name_len + sizeof(*extref);
5035 if (dirid == last_dirid)
5036 continue;
5037 ret = dir_changed(sctx, dirid);
5038 if (ret)
5039 break;
5040 last_dirid = dirid;
5041 }
5042out:
5043 return ret;
5044}
5045
Alexander Block766702e2012-07-28 14:11:31 +02005046/*
5047 * Updates compare related fields in sctx and simply forwards to the actual
5048 * changed_xxx functions.
5049 */
Alexander Block31db9f72012-07-25 23:19:24 +02005050static int changed_cb(struct btrfs_root *left_root,
5051 struct btrfs_root *right_root,
5052 struct btrfs_path *left_path,
5053 struct btrfs_path *right_path,
5054 struct btrfs_key *key,
5055 enum btrfs_compare_tree_result result,
5056 void *ctx)
5057{
5058 int ret = 0;
5059 struct send_ctx *sctx = ctx;
5060
Josef Bacikba5e8f22013-08-16 16:52:55 -04005061 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005062 if (key->type == BTRFS_INODE_REF_KEY ||
5063 key->type == BTRFS_INODE_EXTREF_KEY) {
5064 ret = compare_refs(sctx, left_path, key);
5065 if (!ret)
5066 return 0;
5067 if (ret < 0)
5068 return ret;
5069 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5070 return maybe_send_hole(sctx, left_path, key);
5071 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005072 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005073 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005074 result = BTRFS_COMPARE_TREE_CHANGED;
5075 ret = 0;
5076 }
5077
Alexander Block31db9f72012-07-25 23:19:24 +02005078 sctx->left_path = left_path;
5079 sctx->right_path = right_path;
5080 sctx->cmp_key = key;
5081
5082 ret = finish_inode_if_needed(sctx, 0);
5083 if (ret < 0)
5084 goto out;
5085
Alexander Block2981e222012-08-01 14:47:03 +02005086 /* Ignore non-FS objects */
5087 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5088 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5089 goto out;
5090
Alexander Block31db9f72012-07-25 23:19:24 +02005091 if (key->type == BTRFS_INODE_ITEM_KEY)
5092 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005093 else if (key->type == BTRFS_INODE_REF_KEY ||
5094 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005095 ret = changed_ref(sctx, result);
5096 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5097 ret = changed_xattr(sctx, result);
5098 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5099 ret = changed_extent(sctx, result);
5100
5101out:
5102 return ret;
5103}
5104
5105static int full_send_tree(struct send_ctx *sctx)
5106{
5107 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005108 struct btrfs_root *send_root = sctx->send_root;
5109 struct btrfs_key key;
5110 struct btrfs_key found_key;
5111 struct btrfs_path *path;
5112 struct extent_buffer *eb;
5113 int slot;
5114 u64 start_ctransid;
5115 u64 ctransid;
5116
5117 path = alloc_path_for_send();
5118 if (!path)
5119 return -ENOMEM;
5120
Anand Jain5f3ab902012-12-07 09:28:54 +00005121 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005122 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005123 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005124
5125 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5126 key.type = BTRFS_INODE_ITEM_KEY;
5127 key.offset = 0;
5128
Alexander Block31db9f72012-07-25 23:19:24 +02005129 /*
Alexander Block766702e2012-07-28 14:11:31 +02005130 * Make sure the tree has not changed after re-joining. We detect this
5131 * by comparing start_ctransid and ctransid. They should always match.
Alexander Block31db9f72012-07-25 23:19:24 +02005132 */
Anand Jain5f3ab902012-12-07 09:28:54 +00005133 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005134 ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005135 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005136
5137 if (ctransid != start_ctransid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005138 WARN(1, KERN_WARNING "BTRFS: the root that you're trying to "
Alexander Block31db9f72012-07-25 23:19:24 +02005139 "send was modified in between. This is "
5140 "probably a bug.\n");
5141 ret = -EIO;
5142 goto out;
5143 }
5144
5145 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5146 if (ret < 0)
5147 goto out;
5148 if (ret)
5149 goto out_finish;
5150
5151 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005152 eb = path->nodes[0];
5153 slot = path->slots[0];
5154 btrfs_item_key_to_cpu(eb, &found_key, slot);
5155
5156 ret = changed_cb(send_root, NULL, path, NULL,
5157 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5158 if (ret < 0)
5159 goto out;
5160
5161 key.objectid = found_key.objectid;
5162 key.type = found_key.type;
5163 key.offset = found_key.offset + 1;
5164
5165 ret = btrfs_next_item(send_root, path);
5166 if (ret < 0)
5167 goto out;
5168 if (ret) {
5169 ret = 0;
5170 break;
5171 }
5172 }
5173
5174out_finish:
5175 ret = finish_inode_if_needed(sctx, 1);
5176
5177out:
5178 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02005179 return ret;
5180}
5181
5182static int send_subvol(struct send_ctx *sctx)
5183{
5184 int ret;
5185
Stefan Behrensc2c71322013-04-10 17:10:52 +00005186 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5187 ret = send_header(sctx);
5188 if (ret < 0)
5189 goto out;
5190 }
Alexander Block31db9f72012-07-25 23:19:24 +02005191
5192 ret = send_subvol_begin(sctx);
5193 if (ret < 0)
5194 goto out;
5195
5196 if (sctx->parent_root) {
5197 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5198 changed_cb, sctx);
5199 if (ret < 0)
5200 goto out;
5201 ret = finish_inode_if_needed(sctx, 1);
5202 if (ret < 0)
5203 goto out;
5204 } else {
5205 ret = full_send_tree(sctx);
5206 if (ret < 0)
5207 goto out;
5208 }
5209
5210out:
Alexander Block31db9f72012-07-25 23:19:24 +02005211 free_recorded_refs(sctx);
5212 return ret;
5213}
5214
David Sterba66ef7d62013-12-17 15:07:20 +01005215static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5216{
5217 spin_lock(&root->root_item_lock);
5218 root->send_in_progress--;
5219 /*
5220 * Not much left to do, we don't know why it's unbalanced and
5221 * can't blindly reset it to 0.
5222 */
5223 if (root->send_in_progress < 0)
5224 btrfs_err(root->fs_info,
5225 "send_in_progres unbalanced %d root %llu\n",
5226 root->send_in_progress, root->root_key.objectid);
5227 spin_unlock(&root->root_item_lock);
5228}
5229
Alexander Block31db9f72012-07-25 23:19:24 +02005230long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5231{
5232 int ret = 0;
5233 struct btrfs_root *send_root;
5234 struct btrfs_root *clone_root;
5235 struct btrfs_fs_info *fs_info;
5236 struct btrfs_ioctl_send_args *arg = NULL;
5237 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02005238 struct send_ctx *sctx = NULL;
5239 u32 i;
5240 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01005241 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08005242 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08005243 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02005244
5245 if (!capable(CAP_SYS_ADMIN))
5246 return -EPERM;
5247
Al Viro496ad9a2013-01-23 17:07:38 -05005248 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02005249 fs_info = send_root->fs_info;
5250
Josef Bacik139f8072013-05-20 11:26:50 -04005251 /*
David Sterba2c686532013-12-16 17:34:17 +01005252 * The subvolume must remain read-only during send, protect against
5253 * making it RW.
5254 */
5255 spin_lock(&send_root->root_item_lock);
5256 send_root->send_in_progress++;
5257 spin_unlock(&send_root->root_item_lock);
5258
5259 /*
Josef Bacik139f8072013-05-20 11:26:50 -04005260 * This is done when we lookup the root, it should already be complete
5261 * by the time we get here.
5262 */
5263 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5264
5265 /*
David Sterba2c686532013-12-16 17:34:17 +01005266 * Userspace tools do the checks and warn the user if it's
5267 * not RO.
5268 */
5269 if (!btrfs_root_readonly(send_root)) {
5270 ret = -EPERM;
5271 goto out;
5272 }
5273
Alexander Block31db9f72012-07-25 23:19:24 +02005274 arg = memdup_user(arg_, sizeof(*arg));
5275 if (IS_ERR(arg)) {
5276 ret = PTR_ERR(arg);
5277 arg = NULL;
5278 goto out;
5279 }
5280
5281 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05005282 sizeof(*arg->clone_sources) *
5283 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02005284 ret = -EFAULT;
5285 goto out;
5286 }
5287
Stefan Behrensc2c71322013-04-10 17:10:52 +00005288 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005289 ret = -EINVAL;
5290 goto out;
5291 }
5292
Alexander Block31db9f72012-07-25 23:19:24 +02005293 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5294 if (!sctx) {
5295 ret = -ENOMEM;
5296 goto out;
5297 }
5298
5299 INIT_LIST_HEAD(&sctx->new_refs);
5300 INIT_LIST_HEAD(&sctx->deleted_refs);
5301 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5302 INIT_LIST_HEAD(&sctx->name_cache_list);
5303
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005304 sctx->flags = arg->flags;
5305
Alexander Block31db9f72012-07-25 23:19:24 +02005306 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00005307 if (!sctx->send_filp) {
5308 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02005309 goto out;
5310 }
5311
Alexander Block31db9f72012-07-25 23:19:24 +02005312 sctx->send_root = send_root;
5313 sctx->clone_roots_cnt = arg->clone_sources_count;
5314
5315 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5316 sctx->send_buf = vmalloc(sctx->send_max_size);
5317 if (!sctx->send_buf) {
5318 ret = -ENOMEM;
5319 goto out;
5320 }
5321
5322 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5323 if (!sctx->read_buf) {
5324 ret = -ENOMEM;
5325 goto out;
5326 }
5327
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005328 sctx->pending_dir_moves = RB_ROOT;
5329 sctx->waiting_dir_moves = RB_ROOT;
5330
Alexander Block31db9f72012-07-25 23:19:24 +02005331 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5332 (arg->clone_sources_count + 1));
5333 if (!sctx->clone_roots) {
5334 ret = -ENOMEM;
5335 goto out;
5336 }
5337
5338 if (arg->clone_sources_count) {
5339 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5340 sizeof(*arg->clone_sources));
5341 if (!clone_sources_tmp) {
5342 ret = -ENOMEM;
5343 goto out;
5344 }
5345
5346 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5347 arg->clone_sources_count *
5348 sizeof(*arg->clone_sources));
5349 if (ret) {
5350 ret = -EFAULT;
5351 goto out;
5352 }
5353
5354 for (i = 0; i < arg->clone_sources_count; i++) {
5355 key.objectid = clone_sources_tmp[i];
5356 key.type = BTRFS_ROOT_ITEM_KEY;
5357 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005358
5359 index = srcu_read_lock(&fs_info->subvol_srcu);
5360
Alexander Block31db9f72012-07-25 23:19:24 +02005361 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02005362 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005363 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005364 ret = PTR_ERR(clone_root);
5365 goto out;
5366 }
David Sterba2c686532013-12-16 17:34:17 +01005367 clone_sources_to_rollback = i + 1;
5368 spin_lock(&clone_root->root_item_lock);
5369 clone_root->send_in_progress++;
5370 if (!btrfs_root_readonly(clone_root)) {
5371 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005372 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005373 ret = -EPERM;
5374 goto out;
5375 }
5376 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005377 srcu_read_unlock(&fs_info->subvol_srcu, index);
5378
Alexander Block31db9f72012-07-25 23:19:24 +02005379 sctx->clone_roots[i].root = clone_root;
5380 }
5381 vfree(clone_sources_tmp);
5382 clone_sources_tmp = NULL;
5383 }
5384
5385 if (arg->parent_root) {
5386 key.objectid = arg->parent_root;
5387 key.type = BTRFS_ROOT_ITEM_KEY;
5388 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005389
5390 index = srcu_read_lock(&fs_info->subvol_srcu);
5391
Alexander Block31db9f72012-07-25 23:19:24 +02005392 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005393 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005394 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005395 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02005396 goto out;
5397 }
Wang Shilong18f687d2014-01-07 17:25:19 +08005398
David Sterba2c686532013-12-16 17:34:17 +01005399 spin_lock(&sctx->parent_root->root_item_lock);
5400 sctx->parent_root->send_in_progress++;
5401 if (!btrfs_root_readonly(sctx->parent_root)) {
5402 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005403 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005404 ret = -EPERM;
5405 goto out;
5406 }
5407 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005408
5409 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005410 }
5411
5412 /*
5413 * Clones from send_root are allowed, but only if the clone source
5414 * is behind the current send position. This is checked while searching
5415 * for possible clone sources.
5416 */
5417 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5418
5419 /* We do a bsearch later */
5420 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5421 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5422 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08005423 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005424
5425 ret = send_subvol(sctx);
5426 if (ret < 0)
5427 goto out;
5428
Stefan Behrensc2c71322013-04-10 17:10:52 +00005429 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5430 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5431 if (ret < 0)
5432 goto out;
5433 ret = send_cmd(sctx);
5434 if (ret < 0)
5435 goto out;
5436 }
Alexander Block31db9f72012-07-25 23:19:24 +02005437
5438out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005439 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5440 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5441 struct rb_node *n;
5442 struct pending_dir_move *pm;
5443
5444 n = rb_first(&sctx->pending_dir_moves);
5445 pm = rb_entry(n, struct pending_dir_move, node);
5446 while (!list_empty(&pm->list)) {
5447 struct pending_dir_move *pm2;
5448
5449 pm2 = list_first_entry(&pm->list,
5450 struct pending_dir_move, list);
5451 free_pending_move(sctx, pm2);
5452 }
5453 free_pending_move(sctx, pm);
5454 }
5455
5456 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5457 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5458 struct rb_node *n;
5459 struct waiting_dir_move *dm;
5460
5461 n = rb_first(&sctx->waiting_dir_moves);
5462 dm = rb_entry(n, struct waiting_dir_move, node);
5463 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5464 kfree(dm);
5465 }
5466
Wang Shilong896c14f2014-01-07 17:25:18 +08005467 if (sort_clone_roots) {
5468 for (i = 0; i < sctx->clone_roots_cnt; i++)
5469 btrfs_root_dec_send_in_progress(
5470 sctx->clone_roots[i].root);
5471 } else {
5472 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5473 btrfs_root_dec_send_in_progress(
5474 sctx->clone_roots[i].root);
5475
5476 btrfs_root_dec_send_in_progress(send_root);
5477 }
David Sterba66ef7d62013-12-17 15:07:20 +01005478 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5479 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01005480
Alexander Block31db9f72012-07-25 23:19:24 +02005481 kfree(arg);
5482 vfree(clone_sources_tmp);
5483
5484 if (sctx) {
5485 if (sctx->send_filp)
5486 fput(sctx->send_filp);
5487
5488 vfree(sctx->clone_roots);
5489 vfree(sctx->send_buf);
5490 vfree(sctx->read_buf);
5491
5492 name_cache_free(sctx);
5493
5494 kfree(sctx);
5495 }
5496
5497 return ret;
5498}