blob: a2621e7aaa5c531e2b6d5c01ad384712c979551a [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;
Alexander Block31db9f72012-07-25 23:19:24 +02001291 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1292 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001293 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001294
Alexander Block31db9f72012-07-25 23:19:24 +02001295 if (ret < 0)
1296 goto out;
1297
Alexander Block35075bb2012-07-28 12:44:34 +02001298 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001299 /* found a bug in backref code? */
1300 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001301 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001302 "send_root. inode=%llu, offset=%llu, "
Chris Mason74dd17f2012-08-07 16:25:13 -04001303 "disk_byte=%llu found extent=%llu\n",
1304 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001305 goto out;
1306 }
1307
1308verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1309 "ino=%llu, "
1310 "num_bytes=%llu, logical=%llu\n",
1311 data_offset, ino, num_bytes, logical);
1312
Alexander Block35075bb2012-07-28 12:44:34 +02001313 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001314 verbose_printk("btrfs: no clones found\n");
1315
1316 cur_clone_root = NULL;
1317 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1318 if (sctx->clone_roots[i].found_refs) {
1319 if (!cur_clone_root)
1320 cur_clone_root = sctx->clone_roots + i;
1321 else if (sctx->clone_roots[i].root == sctx->send_root)
1322 /* prefer clones from send_root over others */
1323 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001324 }
1325
1326 }
1327
1328 if (cur_clone_root) {
Filipe David Borba Manana93de4ba2014-02-15 15:53:16 +00001329 if (compressed != BTRFS_COMPRESS_NONE) {
1330 /*
1331 * Offsets given by iterate_extent_inodes() are relative
1332 * to the start of the extent, we need to add logical
1333 * offset from the file extent item.
1334 * (See why at backref.c:check_extent_in_eb())
1335 */
1336 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1337 fi);
1338 }
Alexander Block31db9f72012-07-25 23:19:24 +02001339 *found = cur_clone_root;
1340 ret = 0;
1341 } else {
1342 ret = -ENOENT;
1343 }
1344
1345out:
1346 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001347 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001348 return ret;
1349}
1350
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001351static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001352 u64 ino,
1353 struct fs_path *dest)
1354{
1355 int ret;
1356 struct btrfs_path *path;
1357 struct btrfs_key key;
1358 struct btrfs_file_extent_item *ei;
1359 u8 type;
1360 u8 compression;
1361 unsigned long off;
1362 int len;
1363
1364 path = alloc_path_for_send();
1365 if (!path)
1366 return -ENOMEM;
1367
1368 key.objectid = ino;
1369 key.type = BTRFS_EXTENT_DATA_KEY;
1370 key.offset = 0;
1371 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1372 if (ret < 0)
1373 goto out;
1374 BUG_ON(ret);
1375
1376 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1377 struct btrfs_file_extent_item);
1378 type = btrfs_file_extent_type(path->nodes[0], ei);
1379 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1380 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1381 BUG_ON(compression);
1382
1383 off = btrfs_file_extent_inline_start(ei);
Chris Mason514ac8a2014-01-03 21:07:00 -08001384 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
Alexander Block31db9f72012-07-25 23:19:24 +02001385
1386 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001387
1388out:
1389 btrfs_free_path(path);
1390 return ret;
1391}
1392
1393/*
1394 * Helper function to generate a file name that is unique in the root of
1395 * send_root and parent_root. This is used to generate names for orphan inodes.
1396 */
1397static int gen_unique_name(struct send_ctx *sctx,
1398 u64 ino, u64 gen,
1399 struct fs_path *dest)
1400{
1401 int ret = 0;
1402 struct btrfs_path *path;
1403 struct btrfs_dir_item *di;
1404 char tmp[64];
1405 int len;
1406 u64 idx = 0;
1407
1408 path = alloc_path_for_send();
1409 if (!path)
1410 return -ENOMEM;
1411
1412 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001413 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001414 ino, gen, idx);
David Sterba64792f22014-02-03 18:24:09 +01001415 ASSERT(len < sizeof(tmp));
Alexander Block31db9f72012-07-25 23:19:24 +02001416
1417 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1418 path, BTRFS_FIRST_FREE_OBJECTID,
1419 tmp, strlen(tmp), 0);
1420 btrfs_release_path(path);
1421 if (IS_ERR(di)) {
1422 ret = PTR_ERR(di);
1423 goto out;
1424 }
1425 if (di) {
1426 /* not unique, try again */
1427 idx++;
1428 continue;
1429 }
1430
1431 if (!sctx->parent_root) {
1432 /* unique */
1433 ret = 0;
1434 break;
1435 }
1436
1437 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1438 path, BTRFS_FIRST_FREE_OBJECTID,
1439 tmp, strlen(tmp), 0);
1440 btrfs_release_path(path);
1441 if (IS_ERR(di)) {
1442 ret = PTR_ERR(di);
1443 goto out;
1444 }
1445 if (di) {
1446 /* not unique, try again */
1447 idx++;
1448 continue;
1449 }
1450 /* unique */
1451 break;
1452 }
1453
1454 ret = fs_path_add(dest, tmp, strlen(tmp));
1455
1456out:
1457 btrfs_free_path(path);
1458 return ret;
1459}
1460
1461enum inode_state {
1462 inode_state_no_change,
1463 inode_state_will_create,
1464 inode_state_did_create,
1465 inode_state_will_delete,
1466 inode_state_did_delete,
1467};
1468
1469static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1470{
1471 int ret;
1472 int left_ret;
1473 int right_ret;
1474 u64 left_gen;
1475 u64 right_gen;
1476
1477 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001478 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001479 if (ret < 0 && ret != -ENOENT)
1480 goto out;
1481 left_ret = ret;
1482
1483 if (!sctx->parent_root) {
1484 right_ret = -ENOENT;
1485 } else {
1486 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001487 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001488 if (ret < 0 && ret != -ENOENT)
1489 goto out;
1490 right_ret = ret;
1491 }
1492
1493 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001494 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001495 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001496 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001497 if (ino < sctx->send_progress)
1498 ret = inode_state_did_create;
1499 else
1500 ret = inode_state_will_create;
1501 } else if (right_gen == gen) {
1502 if (ino < sctx->send_progress)
1503 ret = inode_state_did_delete;
1504 else
1505 ret = inode_state_will_delete;
1506 } else {
1507 ret = -ENOENT;
1508 }
1509 } else if (!left_ret) {
1510 if (left_gen == gen) {
1511 if (ino < sctx->send_progress)
1512 ret = inode_state_did_create;
1513 else
1514 ret = inode_state_will_create;
1515 } else {
1516 ret = -ENOENT;
1517 }
1518 } else if (!right_ret) {
1519 if (right_gen == gen) {
1520 if (ino < sctx->send_progress)
1521 ret = inode_state_did_delete;
1522 else
1523 ret = inode_state_will_delete;
1524 } else {
1525 ret = -ENOENT;
1526 }
1527 } else {
1528 ret = -ENOENT;
1529 }
1530
1531out:
1532 return ret;
1533}
1534
1535static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1536{
1537 int ret;
1538
1539 ret = get_cur_inode_state(sctx, ino, gen);
1540 if (ret < 0)
1541 goto out;
1542
1543 if (ret == inode_state_no_change ||
1544 ret == inode_state_did_create ||
1545 ret == inode_state_will_delete)
1546 ret = 1;
1547 else
1548 ret = 0;
1549
1550out:
1551 return ret;
1552}
1553
1554/*
1555 * Helper function to lookup a dir item in a dir.
1556 */
1557static int lookup_dir_item_inode(struct btrfs_root *root,
1558 u64 dir, const char *name, int name_len,
1559 u64 *found_inode,
1560 u8 *found_type)
1561{
1562 int ret = 0;
1563 struct btrfs_dir_item *di;
1564 struct btrfs_key key;
1565 struct btrfs_path *path;
1566
1567 path = alloc_path_for_send();
1568 if (!path)
1569 return -ENOMEM;
1570
1571 di = btrfs_lookup_dir_item(NULL, root, path,
1572 dir, name, name_len, 0);
1573 if (!di) {
1574 ret = -ENOENT;
1575 goto out;
1576 }
1577 if (IS_ERR(di)) {
1578 ret = PTR_ERR(di);
1579 goto out;
1580 }
1581 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1582 *found_inode = key.objectid;
1583 *found_type = btrfs_dir_type(path->nodes[0], di);
1584
1585out:
1586 btrfs_free_path(path);
1587 return ret;
1588}
1589
Alexander Block766702e2012-07-28 14:11:31 +02001590/*
1591 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1592 * generation of the parent dir and the name of the dir entry.
1593 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001594static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001595 u64 *dir, u64 *dir_gen, struct fs_path *name)
1596{
1597 int ret;
1598 struct btrfs_key key;
1599 struct btrfs_key found_key;
1600 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001601 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001602 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001603
1604 path = alloc_path_for_send();
1605 if (!path)
1606 return -ENOMEM;
1607
1608 key.objectid = ino;
1609 key.type = BTRFS_INODE_REF_KEY;
1610 key.offset = 0;
1611
1612 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1613 if (ret < 0)
1614 goto out;
1615 if (!ret)
1616 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1617 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001618 if (ret || found_key.objectid != ino ||
1619 (found_key.type != BTRFS_INODE_REF_KEY &&
1620 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001621 ret = -ENOENT;
1622 goto out;
1623 }
1624
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001625 if (key.type == BTRFS_INODE_REF_KEY) {
1626 struct btrfs_inode_ref *iref;
1627 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1628 struct btrfs_inode_ref);
1629 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1630 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1631 (unsigned long)(iref + 1),
1632 len);
1633 parent_dir = found_key.offset;
1634 } else {
1635 struct btrfs_inode_extref *extref;
1636 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1637 struct btrfs_inode_extref);
1638 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1639 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1640 (unsigned long)&extref->name, len);
1641 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1642 }
Alexander Block31db9f72012-07-25 23:19:24 +02001643 if (ret < 0)
1644 goto out;
1645 btrfs_release_path(path);
1646
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001647 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001648 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001649 if (ret < 0)
1650 goto out;
1651
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001652 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001653
1654out:
1655 btrfs_free_path(path);
1656 return ret;
1657}
1658
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001659static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001660 u64 ino, u64 dir,
1661 const char *name, int name_len)
1662{
1663 int ret;
1664 struct fs_path *tmp_name;
1665 u64 tmp_dir;
1666 u64 tmp_dir_gen;
1667
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001668 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001669 if (!tmp_name)
1670 return -ENOMEM;
1671
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001672 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001673 if (ret < 0)
1674 goto out;
1675
Alexander Blockb9291af2012-07-28 11:07:18 +02001676 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001677 ret = 0;
1678 goto out;
1679 }
1680
Alexander Blocke938c8a2012-07-28 16:33:49 +02001681 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001682
1683out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001684 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001685 return ret;
1686}
1687
Alexander Block766702e2012-07-28 14:11:31 +02001688/*
1689 * Used by process_recorded_refs to determine if a new ref would overwrite an
1690 * already existing ref. In case it detects an overwrite, it returns the
1691 * inode/gen in who_ino/who_gen.
1692 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1693 * to make sure later references to the overwritten inode are possible.
1694 * Orphanizing is however only required for the first ref of an inode.
1695 * process_recorded_refs does an additional is_first_ref check to see if
1696 * orphanizing is really required.
1697 */
Alexander Block31db9f72012-07-25 23:19:24 +02001698static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1699 const char *name, int name_len,
1700 u64 *who_ino, u64 *who_gen)
1701{
1702 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001703 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001704 u64 other_inode = 0;
1705 u8 other_type = 0;
1706
1707 if (!sctx->parent_root)
1708 goto out;
1709
1710 ret = is_inode_existent(sctx, dir, dir_gen);
1711 if (ret <= 0)
1712 goto out;
1713
Josef Bacikebdad912013-08-06 16:47:48 -04001714 /*
1715 * If we have a parent root we need to verify that the parent dir was
1716 * not delted and then re-created, if it was then we have no overwrite
1717 * and we can just unlink this entry.
1718 */
1719 if (sctx->parent_root) {
1720 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1721 NULL, NULL, NULL);
1722 if (ret < 0 && ret != -ENOENT)
1723 goto out;
1724 if (ret) {
1725 ret = 0;
1726 goto out;
1727 }
1728 if (gen != dir_gen)
1729 goto out;
1730 }
1731
Alexander Block31db9f72012-07-25 23:19:24 +02001732 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1733 &other_inode, &other_type);
1734 if (ret < 0 && ret != -ENOENT)
1735 goto out;
1736 if (ret) {
1737 ret = 0;
1738 goto out;
1739 }
1740
Alexander Block766702e2012-07-28 14:11:31 +02001741 /*
1742 * Check if the overwritten ref was already processed. If yes, the ref
1743 * was already unlinked/moved, so we can safely assume that we will not
1744 * overwrite anything at this point in time.
1745 */
Alexander Block31db9f72012-07-25 23:19:24 +02001746 if (other_inode > sctx->send_progress) {
1747 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001748 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001749 if (ret < 0)
1750 goto out;
1751
1752 ret = 1;
1753 *who_ino = other_inode;
1754 } else {
1755 ret = 0;
1756 }
1757
1758out:
1759 return ret;
1760}
1761
Alexander Block766702e2012-07-28 14:11:31 +02001762/*
1763 * Checks if the ref was overwritten by an already processed inode. This is
1764 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1765 * thus the orphan name needs be used.
1766 * process_recorded_refs also uses it to avoid unlinking of refs that were
1767 * overwritten.
1768 */
Alexander Block31db9f72012-07-25 23:19:24 +02001769static int did_overwrite_ref(struct send_ctx *sctx,
1770 u64 dir, u64 dir_gen,
1771 u64 ino, u64 ino_gen,
1772 const char *name, int name_len)
1773{
1774 int ret = 0;
1775 u64 gen;
1776 u64 ow_inode;
1777 u8 other_type;
1778
1779 if (!sctx->parent_root)
1780 goto out;
1781
1782 ret = is_inode_existent(sctx, dir, dir_gen);
1783 if (ret <= 0)
1784 goto out;
1785
1786 /* check if the ref was overwritten by another ref */
1787 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1788 &ow_inode, &other_type);
1789 if (ret < 0 && ret != -ENOENT)
1790 goto out;
1791 if (ret) {
1792 /* was never and will never be overwritten */
1793 ret = 0;
1794 goto out;
1795 }
1796
1797 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001798 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001799 if (ret < 0)
1800 goto out;
1801
1802 if (ow_inode == ino && gen == ino_gen) {
1803 ret = 0;
1804 goto out;
1805 }
1806
1807 /* we know that it is or will be overwritten. check this now */
1808 if (ow_inode < sctx->send_progress)
1809 ret = 1;
1810 else
1811 ret = 0;
1812
1813out:
1814 return ret;
1815}
1816
Alexander Block766702e2012-07-28 14:11:31 +02001817/*
1818 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1819 * that got overwritten. This is used by process_recorded_refs to determine
1820 * if it has to use the path as returned by get_cur_path or the orphan name.
1821 */
Alexander Block31db9f72012-07-25 23:19:24 +02001822static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1823{
1824 int ret = 0;
1825 struct fs_path *name = NULL;
1826 u64 dir;
1827 u64 dir_gen;
1828
1829 if (!sctx->parent_root)
1830 goto out;
1831
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001832 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001833 if (!name)
1834 return -ENOMEM;
1835
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001836 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001837 if (ret < 0)
1838 goto out;
1839
1840 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1841 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001842
1843out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001844 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001845 return ret;
1846}
1847
Alexander Block766702e2012-07-28 14:11:31 +02001848/*
1849 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1850 * so we need to do some special handling in case we have clashes. This function
1851 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001852 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001853 */
Alexander Block31db9f72012-07-25 23:19:24 +02001854static int name_cache_insert(struct send_ctx *sctx,
1855 struct name_cache_entry *nce)
1856{
1857 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001858 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001859
Alexander Block7e0926f2012-07-28 14:20:58 +02001860 nce_head = radix_tree_lookup(&sctx->name_cache,
1861 (unsigned long)nce->ino);
1862 if (!nce_head) {
1863 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001864 if (!nce_head) {
1865 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001866 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001867 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001868 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001869
Alexander Block7e0926f2012-07-28 14:20:58 +02001870 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001871 if (ret < 0) {
1872 kfree(nce_head);
1873 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001874 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001875 }
Alexander Block31db9f72012-07-25 23:19:24 +02001876 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001877 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001878 list_add_tail(&nce->list, &sctx->name_cache_list);
1879 sctx->name_cache_size++;
1880
1881 return ret;
1882}
1883
1884static void name_cache_delete(struct send_ctx *sctx,
1885 struct name_cache_entry *nce)
1886{
Alexander Block7e0926f2012-07-28 14:20:58 +02001887 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001888
Alexander Block7e0926f2012-07-28 14:20:58 +02001889 nce_head = radix_tree_lookup(&sctx->name_cache,
1890 (unsigned long)nce->ino);
David Sterba57fb8912014-02-03 19:24:40 +01001891 if (!nce_head) {
1892 btrfs_err(sctx->send_root->fs_info,
1893 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1894 nce->ino, sctx->name_cache_size);
1895 }
Alexander Block31db9f72012-07-25 23:19:24 +02001896
Alexander Block7e0926f2012-07-28 14:20:58 +02001897 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001898 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001899 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001900
David Sterba57fb8912014-02-03 19:24:40 +01001901 /*
1902 * We may not get to the final release of nce_head if the lookup fails
1903 */
1904 if (nce_head && list_empty(nce_head)) {
Alexander Block7e0926f2012-07-28 14:20:58 +02001905 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1906 kfree(nce_head);
1907 }
Alexander Block31db9f72012-07-25 23:19:24 +02001908}
1909
1910static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1911 u64 ino, u64 gen)
1912{
Alexander Block7e0926f2012-07-28 14:20:58 +02001913 struct list_head *nce_head;
1914 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001915
Alexander Block7e0926f2012-07-28 14:20:58 +02001916 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1917 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001918 return NULL;
1919
Alexander Block7e0926f2012-07-28 14:20:58 +02001920 list_for_each_entry(cur, nce_head, radix_list) {
1921 if (cur->ino == ino && cur->gen == gen)
1922 return cur;
1923 }
Alexander Block31db9f72012-07-25 23:19:24 +02001924 return NULL;
1925}
1926
Alexander Block766702e2012-07-28 14:11:31 +02001927/*
1928 * Removes the entry from the list and adds it back to the end. This marks the
1929 * entry as recently used so that name_cache_clean_unused does not remove it.
1930 */
Alexander Block31db9f72012-07-25 23:19:24 +02001931static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1932{
1933 list_del(&nce->list);
1934 list_add_tail(&nce->list, &sctx->name_cache_list);
1935}
1936
Alexander Block766702e2012-07-28 14:11:31 +02001937/*
1938 * Remove some entries from the beginning of name_cache_list.
1939 */
Alexander Block31db9f72012-07-25 23:19:24 +02001940static void name_cache_clean_unused(struct send_ctx *sctx)
1941{
1942 struct name_cache_entry *nce;
1943
1944 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1945 return;
1946
1947 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1948 nce = list_entry(sctx->name_cache_list.next,
1949 struct name_cache_entry, list);
1950 name_cache_delete(sctx, nce);
1951 kfree(nce);
1952 }
1953}
1954
1955static void name_cache_free(struct send_ctx *sctx)
1956{
1957 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02001958
Alexander Blocke938c8a2012-07-28 16:33:49 +02001959 while (!list_empty(&sctx->name_cache_list)) {
1960 nce = list_entry(sctx->name_cache_list.next,
1961 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02001962 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02001963 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001964 }
1965}
1966
Alexander Block766702e2012-07-28 14:11:31 +02001967/*
1968 * Used by get_cur_path for each ref up to the root.
1969 * Returns 0 if it succeeded.
1970 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1971 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1972 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1973 * Returns <0 in case of error.
1974 */
Alexander Block31db9f72012-07-25 23:19:24 +02001975static int __get_cur_name_and_parent(struct send_ctx *sctx,
1976 u64 ino, u64 gen,
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00001977 int skip_name_cache,
Alexander Block31db9f72012-07-25 23:19:24 +02001978 u64 *parent_ino,
1979 u64 *parent_gen,
1980 struct fs_path *dest)
1981{
1982 int ret;
1983 int nce_ret;
1984 struct btrfs_path *path = NULL;
1985 struct name_cache_entry *nce = NULL;
1986
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00001987 if (skip_name_cache)
1988 goto get_ref;
Alexander Block766702e2012-07-28 14:11:31 +02001989 /*
1990 * First check if we already did a call to this function with the same
1991 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1992 * return the cached result.
1993 */
Alexander Block31db9f72012-07-25 23:19:24 +02001994 nce = name_cache_search(sctx, ino, gen);
1995 if (nce) {
1996 if (ino < sctx->send_progress && nce->need_later_update) {
1997 name_cache_delete(sctx, nce);
1998 kfree(nce);
1999 nce = NULL;
2000 } else {
2001 name_cache_used(sctx, nce);
2002 *parent_ino = nce->parent_ino;
2003 *parent_gen = nce->parent_gen;
2004 ret = fs_path_add(dest, nce->name, nce->name_len);
2005 if (ret < 0)
2006 goto out;
2007 ret = nce->ret;
2008 goto out;
2009 }
2010 }
2011
2012 path = alloc_path_for_send();
2013 if (!path)
2014 return -ENOMEM;
2015
Alexander Block766702e2012-07-28 14:11:31 +02002016 /*
2017 * If the inode is not existent yet, add the orphan name and return 1.
2018 * This should only happen for the parent dir that we determine in
2019 * __record_new_ref
2020 */
Alexander Block31db9f72012-07-25 23:19:24 +02002021 ret = is_inode_existent(sctx, ino, gen);
2022 if (ret < 0)
2023 goto out;
2024
2025 if (!ret) {
2026 ret = gen_unique_name(sctx, ino, gen, dest);
2027 if (ret < 0)
2028 goto out;
2029 ret = 1;
2030 goto out_cache;
2031 }
2032
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002033get_ref:
Alexander Block766702e2012-07-28 14:11:31 +02002034 /*
2035 * Depending on whether the inode was already processed or not, use
2036 * send_root or parent_root for ref lookup.
2037 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002038 if (ino < sctx->send_progress && !skip_name_cache)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002039 ret = get_first_ref(sctx->send_root, ino,
2040 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002041 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002042 ret = get_first_ref(sctx->parent_root, ino,
2043 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02002044 if (ret < 0)
2045 goto out;
2046
Alexander Block766702e2012-07-28 14:11:31 +02002047 /*
2048 * Check if the ref was overwritten by an inode's ref that was processed
2049 * earlier. If yes, treat as orphan and return 1.
2050 */
Alexander Block31db9f72012-07-25 23:19:24 +02002051 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2052 dest->start, dest->end - dest->start);
2053 if (ret < 0)
2054 goto out;
2055 if (ret) {
2056 fs_path_reset(dest);
2057 ret = gen_unique_name(sctx, ino, gen, dest);
2058 if (ret < 0)
2059 goto out;
2060 ret = 1;
2061 }
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002062 if (skip_name_cache)
2063 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002064
2065out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002066 /*
2067 * Store the result of the lookup in the name cache.
2068 */
Alexander Block31db9f72012-07-25 23:19:24 +02002069 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2070 if (!nce) {
2071 ret = -ENOMEM;
2072 goto out;
2073 }
2074
2075 nce->ino = ino;
2076 nce->gen = gen;
2077 nce->parent_ino = *parent_ino;
2078 nce->parent_gen = *parent_gen;
2079 nce->name_len = fs_path_len(dest);
2080 nce->ret = ret;
2081 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002082
2083 if (ino < sctx->send_progress)
2084 nce->need_later_update = 0;
2085 else
2086 nce->need_later_update = 1;
2087
2088 nce_ret = name_cache_insert(sctx, nce);
2089 if (nce_ret < 0)
2090 ret = nce_ret;
2091 name_cache_clean_unused(sctx);
2092
2093out:
2094 btrfs_free_path(path);
2095 return ret;
2096}
2097
2098/*
2099 * Magic happens here. This function returns the first ref to an inode as it
2100 * would look like while receiving the stream at this point in time.
2101 * We walk the path up to the root. For every inode in between, we check if it
2102 * was already processed/sent. If yes, we continue with the parent as found
2103 * in send_root. If not, we continue with the parent as found in parent_root.
2104 * If we encounter an inode that was deleted at this point in time, we use the
2105 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2106 * that were not created yet and overwritten inodes/refs.
2107 *
2108 * When do we have have orphan inodes:
2109 * 1. When an inode is freshly created and thus no valid refs are available yet
2110 * 2. When a directory lost all it's refs (deleted) but still has dir items
2111 * inside which were not processed yet (pending for move/delete). If anyone
2112 * tried to get the path to the dir items, it would get a path inside that
2113 * orphan directory.
2114 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2115 * of an unprocessed inode. If in that case the first ref would be
2116 * overwritten, the overwritten inode gets "orphanized". Later when we
2117 * process this overwritten inode, it is restored at a new place by moving
2118 * the orphan inode.
2119 *
2120 * sctx->send_progress tells this function at which point in time receiving
2121 * would be.
2122 */
2123static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2124 struct fs_path *dest)
2125{
2126 int ret = 0;
2127 struct fs_path *name = NULL;
2128 u64 parent_inode = 0;
2129 u64 parent_gen = 0;
2130 int stop = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002131 int skip_name_cache = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002132
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002133 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002134 if (!name) {
2135 ret = -ENOMEM;
2136 goto out;
2137 }
2138
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002139 if (is_waiting_for_move(sctx, ino))
2140 skip_name_cache = 1;
2141
Alexander Block31db9f72012-07-25 23:19:24 +02002142 dest->reversed = 1;
2143 fs_path_reset(dest);
2144
2145 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2146 fs_path_reset(name);
2147
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002148 ret = __get_cur_name_and_parent(sctx, ino, gen, skip_name_cache,
Alexander Block31db9f72012-07-25 23:19:24 +02002149 &parent_inode, &parent_gen, name);
2150 if (ret < 0)
2151 goto out;
2152 if (ret)
2153 stop = 1;
2154
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002155 if (!skip_name_cache &&
Filipe David Borba Manana03cb4fb2014-02-01 02:00:15 +00002156 is_waiting_for_move(sctx, parent_inode))
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002157 skip_name_cache = 1;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002158
Alexander Block31db9f72012-07-25 23:19:24 +02002159 ret = fs_path_add_path(dest, name);
2160 if (ret < 0)
2161 goto out;
2162
2163 ino = parent_inode;
2164 gen = parent_gen;
2165 }
2166
2167out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002168 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002169 if (!ret)
2170 fs_path_unreverse(dest);
2171 return ret;
2172}
2173
2174/*
Alexander Block31db9f72012-07-25 23:19:24 +02002175 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2176 */
2177static int send_subvol_begin(struct send_ctx *sctx)
2178{
2179 int ret;
2180 struct btrfs_root *send_root = sctx->send_root;
2181 struct btrfs_root *parent_root = sctx->parent_root;
2182 struct btrfs_path *path;
2183 struct btrfs_key key;
2184 struct btrfs_root_ref *ref;
2185 struct extent_buffer *leaf;
2186 char *name = NULL;
2187 int namelen;
2188
Wang Shilongffcfaf82014-01-15 00:26:43 +08002189 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002190 if (!path)
2191 return -ENOMEM;
2192
2193 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2194 if (!name) {
2195 btrfs_free_path(path);
2196 return -ENOMEM;
2197 }
2198
2199 key.objectid = send_root->objectid;
2200 key.type = BTRFS_ROOT_BACKREF_KEY;
2201 key.offset = 0;
2202
2203 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2204 &key, path, 1, 0);
2205 if (ret < 0)
2206 goto out;
2207 if (ret) {
2208 ret = -ENOENT;
2209 goto out;
2210 }
2211
2212 leaf = path->nodes[0];
2213 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2214 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2215 key.objectid != send_root->objectid) {
2216 ret = -ENOENT;
2217 goto out;
2218 }
2219 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2220 namelen = btrfs_root_ref_name_len(leaf, ref);
2221 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2222 btrfs_release_path(path);
2223
Alexander Block31db9f72012-07-25 23:19:24 +02002224 if (parent_root) {
2225 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2226 if (ret < 0)
2227 goto out;
2228 } else {
2229 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2230 if (ret < 0)
2231 goto out;
2232 }
2233
2234 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2235 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2236 sctx->send_root->root_item.uuid);
2237 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002238 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002239 if (parent_root) {
2240 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2241 sctx->parent_root->root_item.uuid);
2242 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002243 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002244 }
2245
2246 ret = send_cmd(sctx);
2247
2248tlv_put_failure:
2249out:
2250 btrfs_free_path(path);
2251 kfree(name);
2252 return ret;
2253}
2254
2255static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2256{
2257 int ret = 0;
2258 struct fs_path *p;
2259
2260verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2261
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002262 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002263 if (!p)
2264 return -ENOMEM;
2265
2266 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2267 if (ret < 0)
2268 goto out;
2269
2270 ret = get_cur_path(sctx, ino, gen, p);
2271 if (ret < 0)
2272 goto out;
2273 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2274 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2275
2276 ret = send_cmd(sctx);
2277
2278tlv_put_failure:
2279out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002280 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002281 return ret;
2282}
2283
2284static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2285{
2286 int ret = 0;
2287 struct fs_path *p;
2288
2289verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2290
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002291 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002292 if (!p)
2293 return -ENOMEM;
2294
2295 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2296 if (ret < 0)
2297 goto out;
2298
2299 ret = get_cur_path(sctx, ino, gen, p);
2300 if (ret < 0)
2301 goto out;
2302 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2303 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2304
2305 ret = send_cmd(sctx);
2306
2307tlv_put_failure:
2308out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002309 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002310 return ret;
2311}
2312
2313static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2314{
2315 int ret = 0;
2316 struct fs_path *p;
2317
2318verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2319
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002320 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002321 if (!p)
2322 return -ENOMEM;
2323
2324 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2325 if (ret < 0)
2326 goto out;
2327
2328 ret = get_cur_path(sctx, ino, gen, p);
2329 if (ret < 0)
2330 goto out;
2331 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2332 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2333 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2334
2335 ret = send_cmd(sctx);
2336
2337tlv_put_failure:
2338out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002339 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002340 return ret;
2341}
2342
2343static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2344{
2345 int ret = 0;
2346 struct fs_path *p = NULL;
2347 struct btrfs_inode_item *ii;
2348 struct btrfs_path *path = NULL;
2349 struct extent_buffer *eb;
2350 struct btrfs_key key;
2351 int slot;
2352
2353verbose_printk("btrfs: send_utimes %llu\n", ino);
2354
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002355 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002356 if (!p)
2357 return -ENOMEM;
2358
2359 path = alloc_path_for_send();
2360 if (!path) {
2361 ret = -ENOMEM;
2362 goto out;
2363 }
2364
2365 key.objectid = ino;
2366 key.type = BTRFS_INODE_ITEM_KEY;
2367 key.offset = 0;
2368 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2369 if (ret < 0)
2370 goto out;
2371
2372 eb = path->nodes[0];
2373 slot = path->slots[0];
2374 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2375
2376 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2377 if (ret < 0)
2378 goto out;
2379
2380 ret = get_cur_path(sctx, ino, gen, p);
2381 if (ret < 0)
2382 goto out;
2383 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2384 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2385 btrfs_inode_atime(ii));
2386 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2387 btrfs_inode_mtime(ii));
2388 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2389 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002390 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002391
2392 ret = send_cmd(sctx);
2393
2394tlv_put_failure:
2395out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002396 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002397 btrfs_free_path(path);
2398 return ret;
2399}
2400
2401/*
2402 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2403 * a valid path yet because we did not process the refs yet. So, the inode
2404 * is created as orphan.
2405 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002406static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002407{
2408 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002409 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002410 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002411 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002412 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002413 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002414
Alexander Block1f4692d2012-07-28 10:42:24 +02002415verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002416
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002417 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002418 if (!p)
2419 return -ENOMEM;
2420
Alexander Block1f4692d2012-07-28 10:42:24 +02002421 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2422 NULL, &rdev);
2423 if (ret < 0)
2424 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002425
Alexander Blocke938c8a2012-07-28 16:33:49 +02002426 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002427 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002428 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002429 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002430 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002431 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002432 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002433 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002434 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002435 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002436 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002437 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002438 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002439 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2440 (int)(mode & S_IFMT));
2441 ret = -ENOTSUPP;
2442 goto out;
2443 }
2444
2445 ret = begin_cmd(sctx, cmd);
2446 if (ret < 0)
2447 goto out;
2448
Alexander Block1f4692d2012-07-28 10:42:24 +02002449 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002450 if (ret < 0)
2451 goto out;
2452
2453 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002454 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002455
2456 if (S_ISLNK(mode)) {
2457 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002458 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002459 if (ret < 0)
2460 goto out;
2461 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2462 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2463 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002464 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2465 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002466 }
2467
2468 ret = send_cmd(sctx);
2469 if (ret < 0)
2470 goto out;
2471
2472
2473tlv_put_failure:
2474out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002475 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002476 return ret;
2477}
2478
Alexander Block1f4692d2012-07-28 10:42:24 +02002479/*
2480 * We need some special handling for inodes that get processed before the parent
2481 * directory got created. See process_recorded_refs for details.
2482 * This function does the check if we already created the dir out of order.
2483 */
2484static int did_create_dir(struct send_ctx *sctx, u64 dir)
2485{
2486 int ret = 0;
2487 struct btrfs_path *path = NULL;
2488 struct btrfs_key key;
2489 struct btrfs_key found_key;
2490 struct btrfs_key di_key;
2491 struct extent_buffer *eb;
2492 struct btrfs_dir_item *di;
2493 int slot;
2494
2495 path = alloc_path_for_send();
2496 if (!path) {
2497 ret = -ENOMEM;
2498 goto out;
2499 }
2500
2501 key.objectid = dir;
2502 key.type = BTRFS_DIR_INDEX_KEY;
2503 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002504 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2505 if (ret < 0)
2506 goto out;
2507
Alexander Block1f4692d2012-07-28 10:42:24 +02002508 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002509 eb = path->nodes[0];
2510 slot = path->slots[0];
2511 if (slot >= btrfs_header_nritems(eb)) {
2512 ret = btrfs_next_leaf(sctx->send_root, path);
2513 if (ret < 0) {
2514 goto out;
2515 } else if (ret > 0) {
2516 ret = 0;
2517 break;
2518 }
2519 continue;
Alexander Block1f4692d2012-07-28 10:42:24 +02002520 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002521
2522 btrfs_item_key_to_cpu(eb, &found_key, slot);
2523 if (found_key.objectid != key.objectid ||
Alexander Block1f4692d2012-07-28 10:42:24 +02002524 found_key.type != key.type) {
2525 ret = 0;
2526 goto out;
2527 }
2528
2529 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2530 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2531
Josef Bacika0525412013-08-12 10:56:14 -04002532 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2533 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002534 ret = 1;
2535 goto out;
2536 }
2537
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002538 path->slots[0]++;
Alexander Block1f4692d2012-07-28 10:42:24 +02002539 }
2540
2541out:
2542 btrfs_free_path(path);
2543 return ret;
2544}
2545
2546/*
2547 * Only creates the inode if it is:
2548 * 1. Not a directory
2549 * 2. Or a directory which was not created already due to out of order
2550 * directories. See did_create_dir and process_recorded_refs for details.
2551 */
2552static int send_create_inode_if_needed(struct send_ctx *sctx)
2553{
2554 int ret;
2555
2556 if (S_ISDIR(sctx->cur_inode_mode)) {
2557 ret = did_create_dir(sctx, sctx->cur_ino);
2558 if (ret < 0)
2559 goto out;
2560 if (ret) {
2561 ret = 0;
2562 goto out;
2563 }
2564 }
2565
2566 ret = send_create_inode(sctx, sctx->cur_ino);
2567 if (ret < 0)
2568 goto out;
2569
2570out:
2571 return ret;
2572}
2573
Alexander Block31db9f72012-07-25 23:19:24 +02002574struct recorded_ref {
2575 struct list_head list;
2576 char *dir_path;
2577 char *name;
2578 struct fs_path *full_path;
2579 u64 dir;
2580 u64 dir_gen;
2581 int dir_path_len;
2582 int name_len;
2583};
2584
2585/*
2586 * We need to process new refs before deleted refs, but compare_tree gives us
2587 * everything mixed. So we first record all refs and later process them.
2588 * This function is a helper to record one ref.
2589 */
2590static int record_ref(struct list_head *head, u64 dir,
2591 u64 dir_gen, struct fs_path *path)
2592{
2593 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002594
2595 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2596 if (!ref)
2597 return -ENOMEM;
2598
2599 ref->dir = dir;
2600 ref->dir_gen = dir_gen;
2601 ref->full_path = path;
2602
Andy Shevchenkoed848852013-08-21 10:32:13 +03002603 ref->name = (char *)kbasename(ref->full_path->start);
2604 ref->name_len = ref->full_path->end - ref->name;
2605 ref->dir_path = ref->full_path->start;
2606 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002607 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002608 else
Alexander Block31db9f72012-07-25 23:19:24 +02002609 ref->dir_path_len = ref->full_path->end -
2610 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002611
2612 list_add_tail(&ref->list, head);
2613 return 0;
2614}
2615
Josef Bacikba5e8f22013-08-16 16:52:55 -04002616static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2617{
2618 struct recorded_ref *new;
2619
2620 new = kmalloc(sizeof(*ref), GFP_NOFS);
2621 if (!new)
2622 return -ENOMEM;
2623
2624 new->dir = ref->dir;
2625 new->dir_gen = ref->dir_gen;
2626 new->full_path = NULL;
2627 INIT_LIST_HEAD(&new->list);
2628 list_add_tail(&new->list, list);
2629 return 0;
2630}
2631
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002632static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002633{
2634 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002635
Alexander Blocke938c8a2012-07-28 16:33:49 +02002636 while (!list_empty(head)) {
2637 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002638 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002639 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002640 kfree(cur);
2641 }
Alexander Block31db9f72012-07-25 23:19:24 +02002642}
2643
2644static void free_recorded_refs(struct send_ctx *sctx)
2645{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002646 __free_recorded_refs(&sctx->new_refs);
2647 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002648}
2649
2650/*
Alexander Block766702e2012-07-28 14:11:31 +02002651 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002652 * ref of an unprocessed inode gets overwritten and for all non empty
2653 * directories.
2654 */
2655static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2656 struct fs_path *path)
2657{
2658 int ret;
2659 struct fs_path *orphan;
2660
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002661 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002662 if (!orphan)
2663 return -ENOMEM;
2664
2665 ret = gen_unique_name(sctx, ino, gen, orphan);
2666 if (ret < 0)
2667 goto out;
2668
2669 ret = send_rename(sctx, path, orphan);
2670
2671out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002672 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002673 return ret;
2674}
2675
2676/*
2677 * Returns 1 if a directory can be removed at this point in time.
2678 * We check this by iterating all dir items and checking if the inode behind
2679 * the dir item was already processed.
2680 */
2681static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2682{
2683 int ret = 0;
2684 struct btrfs_root *root = sctx->parent_root;
2685 struct btrfs_path *path;
2686 struct btrfs_key key;
2687 struct btrfs_key found_key;
2688 struct btrfs_key loc;
2689 struct btrfs_dir_item *di;
2690
Alexander Block6d85ed02012-08-01 14:48:59 +02002691 /*
2692 * Don't try to rmdir the top/root subvolume dir.
2693 */
2694 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2695 return 0;
2696
Alexander Block31db9f72012-07-25 23:19:24 +02002697 path = alloc_path_for_send();
2698 if (!path)
2699 return -ENOMEM;
2700
2701 key.objectid = dir;
2702 key.type = BTRFS_DIR_INDEX_KEY;
2703 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002704 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2705 if (ret < 0)
2706 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002707
2708 while (1) {
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002709 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2710 ret = btrfs_next_leaf(root, path);
2711 if (ret < 0)
2712 goto out;
2713 else if (ret > 0)
2714 break;
2715 continue;
Alexander Block31db9f72012-07-25 23:19:24 +02002716 }
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002717 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2718 path->slots[0]);
2719 if (found_key.objectid != key.objectid ||
2720 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02002721 break;
Alexander Block31db9f72012-07-25 23:19:24 +02002722
2723 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2724 struct btrfs_dir_item);
2725 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2726
2727 if (loc.objectid > send_progress) {
2728 ret = 0;
2729 goto out;
2730 }
2731
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00002732 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02002733 }
2734
2735 ret = 1;
2736
2737out:
2738 btrfs_free_path(path);
2739 return ret;
2740}
2741
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002742static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2743{
2744 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2745 struct waiting_dir_move *entry;
2746
2747 while (n) {
2748 entry = rb_entry(n, struct waiting_dir_move, node);
2749 if (ino < entry->ino)
2750 n = n->rb_left;
2751 else if (ino > entry->ino)
2752 n = n->rb_right;
2753 else
2754 return 1;
2755 }
2756 return 0;
2757}
2758
2759static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2760{
2761 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2762 struct rb_node *parent = NULL;
2763 struct waiting_dir_move *entry, *dm;
2764
2765 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2766 if (!dm)
2767 return -ENOMEM;
2768 dm->ino = ino;
2769
2770 while (*p) {
2771 parent = *p;
2772 entry = rb_entry(parent, struct waiting_dir_move, node);
2773 if (ino < entry->ino) {
2774 p = &(*p)->rb_left;
2775 } else if (ino > entry->ino) {
2776 p = &(*p)->rb_right;
2777 } else {
2778 kfree(dm);
2779 return -EEXIST;
2780 }
2781 }
2782
2783 rb_link_node(&dm->node, parent, p);
2784 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2785 return 0;
2786}
2787
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002788static int del_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2789{
2790 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2791 struct waiting_dir_move *entry;
2792
2793 while (n) {
2794 entry = rb_entry(n, struct waiting_dir_move, node);
2795 if (ino < entry->ino) {
2796 n = n->rb_left;
2797 } else if (ino > entry->ino) {
2798 n = n->rb_right;
2799 } else {
2800 rb_erase(&entry->node, &sctx->waiting_dir_moves);
2801 kfree(entry);
2802 return 0;
2803 }
2804 }
2805 return -ENOENT;
2806}
2807
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002808static int add_pending_dir_move(struct send_ctx *sctx, u64 parent_ino)
2809{
2810 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2811 struct rb_node *parent = NULL;
2812 struct pending_dir_move *entry, *pm;
2813 struct recorded_ref *cur;
2814 int exists = 0;
2815 int ret;
2816
2817 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2818 if (!pm)
2819 return -ENOMEM;
2820 pm->parent_ino = parent_ino;
2821 pm->ino = sctx->cur_ino;
2822 pm->gen = sctx->cur_inode_gen;
2823 INIT_LIST_HEAD(&pm->list);
2824 INIT_LIST_HEAD(&pm->update_refs);
2825 RB_CLEAR_NODE(&pm->node);
2826
2827 while (*p) {
2828 parent = *p;
2829 entry = rb_entry(parent, struct pending_dir_move, node);
2830 if (parent_ino < entry->parent_ino) {
2831 p = &(*p)->rb_left;
2832 } else if (parent_ino > entry->parent_ino) {
2833 p = &(*p)->rb_right;
2834 } else {
2835 exists = 1;
2836 break;
2837 }
2838 }
2839
2840 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2841 ret = dup_ref(cur, &pm->update_refs);
2842 if (ret < 0)
2843 goto out;
2844 }
2845 list_for_each_entry(cur, &sctx->new_refs, list) {
2846 ret = dup_ref(cur, &pm->update_refs);
2847 if (ret < 0)
2848 goto out;
2849 }
2850
2851 ret = add_waiting_dir_move(sctx, pm->ino);
2852 if (ret)
2853 goto out;
2854
2855 if (exists) {
2856 list_add_tail(&pm->list, &entry->list);
2857 } else {
2858 rb_link_node(&pm->node, parent, p);
2859 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
2860 }
2861 ret = 0;
2862out:
2863 if (ret) {
2864 __free_recorded_refs(&pm->update_refs);
2865 kfree(pm);
2866 }
2867 return ret;
2868}
2869
2870static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
2871 u64 parent_ino)
2872{
2873 struct rb_node *n = sctx->pending_dir_moves.rb_node;
2874 struct pending_dir_move *entry;
2875
2876 while (n) {
2877 entry = rb_entry(n, struct pending_dir_move, node);
2878 if (parent_ino < entry->parent_ino)
2879 n = n->rb_left;
2880 else if (parent_ino > entry->parent_ino)
2881 n = n->rb_right;
2882 else
2883 return entry;
2884 }
2885 return NULL;
2886}
2887
2888static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
2889{
2890 struct fs_path *from_path = NULL;
2891 struct fs_path *to_path = NULL;
2892 u64 orig_progress = sctx->send_progress;
2893 struct recorded_ref *cur;
2894 int ret;
2895
2896 from_path = fs_path_alloc();
2897 if (!from_path)
2898 return -ENOMEM;
2899
2900 sctx->send_progress = pm->ino;
2901 ret = get_cur_path(sctx, pm->ino, pm->gen, from_path);
2902 if (ret < 0)
2903 goto out;
2904
2905 to_path = fs_path_alloc();
2906 if (!to_path) {
2907 ret = -ENOMEM;
2908 goto out;
2909 }
2910
2911 sctx->send_progress = sctx->cur_ino + 1;
Josef Bacik6cc98d92014-02-05 16:19:21 -05002912 ret = del_waiting_dir_move(sctx, pm->ino);
2913 ASSERT(ret == 0);
2914
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00002915 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
2916 if (ret < 0)
2917 goto out;
2918
2919 ret = send_rename(sctx, from_path, to_path);
2920 if (ret < 0)
2921 goto out;
2922
2923 ret = send_utimes(sctx, pm->ino, pm->gen);
2924 if (ret < 0)
2925 goto out;
2926
2927 /*
2928 * After rename/move, need to update the utimes of both new parent(s)
2929 * and old parent(s).
2930 */
2931 list_for_each_entry(cur, &pm->update_refs, list) {
2932 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
2933 if (ret < 0)
2934 goto out;
2935 }
2936
2937out:
2938 fs_path_free(from_path);
2939 fs_path_free(to_path);
2940 sctx->send_progress = orig_progress;
2941
2942 return ret;
2943}
2944
2945static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
2946{
2947 if (!list_empty(&m->list))
2948 list_del(&m->list);
2949 if (!RB_EMPTY_NODE(&m->node))
2950 rb_erase(&m->node, &sctx->pending_dir_moves);
2951 __free_recorded_refs(&m->update_refs);
2952 kfree(m);
2953}
2954
2955static void tail_append_pending_moves(struct pending_dir_move *moves,
2956 struct list_head *stack)
2957{
2958 if (list_empty(&moves->list)) {
2959 list_add_tail(&moves->list, stack);
2960 } else {
2961 LIST_HEAD(list);
2962 list_splice_init(&moves->list, &list);
2963 list_add_tail(&moves->list, stack);
2964 list_splice_tail(&list, stack);
2965 }
2966}
2967
2968static int apply_children_dir_moves(struct send_ctx *sctx)
2969{
2970 struct pending_dir_move *pm;
2971 struct list_head stack;
2972 u64 parent_ino = sctx->cur_ino;
2973 int ret = 0;
2974
2975 pm = get_pending_dir_moves(sctx, parent_ino);
2976 if (!pm)
2977 return 0;
2978
2979 INIT_LIST_HEAD(&stack);
2980 tail_append_pending_moves(pm, &stack);
2981
2982 while (!list_empty(&stack)) {
2983 pm = list_first_entry(&stack, struct pending_dir_move, list);
2984 parent_ino = pm->ino;
2985 ret = apply_dir_move(sctx, pm);
2986 free_pending_move(sctx, pm);
2987 if (ret)
2988 goto out;
2989 pm = get_pending_dir_moves(sctx, parent_ino);
2990 if (pm)
2991 tail_append_pending_moves(pm, &stack);
2992 }
2993 return 0;
2994
2995out:
2996 while (!list_empty(&stack)) {
2997 pm = list_first_entry(&stack, struct pending_dir_move, list);
2998 free_pending_move(sctx, pm);
2999 }
3000 return ret;
3001}
3002
3003static int wait_for_parent_move(struct send_ctx *sctx,
3004 struct recorded_ref *parent_ref)
3005{
3006 int ret;
3007 u64 ino = parent_ref->dir;
3008 u64 parent_ino_before, parent_ino_after;
3009 u64 new_gen, old_gen;
3010 struct fs_path *path_before = NULL;
3011 struct fs_path *path_after = NULL;
3012 int len1, len2;
3013
3014 if (parent_ref->dir <= sctx->cur_ino)
3015 return 0;
3016
3017 if (is_waiting_for_move(sctx, ino))
3018 return 1;
3019
3020 ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen,
3021 NULL, NULL, NULL, NULL);
3022 if (ret == -ENOENT)
3023 return 0;
3024 else if (ret < 0)
3025 return ret;
3026
3027 ret = get_inode_info(sctx->send_root, ino, NULL, &new_gen,
3028 NULL, NULL, NULL, NULL);
3029 if (ret < 0)
3030 return ret;
3031
3032 if (new_gen != old_gen)
3033 return 0;
3034
3035 path_before = fs_path_alloc();
3036 if (!path_before)
3037 return -ENOMEM;
3038
3039 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3040 NULL, path_before);
3041 if (ret == -ENOENT) {
3042 ret = 0;
3043 goto out;
3044 } else if (ret < 0) {
3045 goto out;
3046 }
3047
3048 path_after = fs_path_alloc();
3049 if (!path_after) {
3050 ret = -ENOMEM;
3051 goto out;
3052 }
3053
3054 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3055 NULL, path_after);
3056 if (ret == -ENOENT) {
3057 ret = 0;
3058 goto out;
3059 } else if (ret < 0) {
3060 goto out;
3061 }
3062
3063 len1 = fs_path_len(path_before);
3064 len2 = fs_path_len(path_after);
Filipe David Borba Manana5ed7f9f2014-02-01 02:02:16 +00003065 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3066 memcmp(path_before->start, path_after->start, len1)) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003067 ret = 1;
3068 goto out;
3069 }
3070 ret = 0;
3071
3072out:
3073 fs_path_free(path_before);
3074 fs_path_free(path_after);
3075
3076 return ret;
3077}
3078
Alexander Block31db9f72012-07-25 23:19:24 +02003079/*
3080 * This does all the move/link/unlink/rmdir magic.
3081 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003082static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
Alexander Block31db9f72012-07-25 23:19:24 +02003083{
3084 int ret = 0;
3085 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02003086 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003087 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02003088 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04003089 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003090 u64 ow_gen;
3091 int did_overwrite = 0;
3092 int is_orphan = 0;
3093
3094verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3095
Alexander Block6d85ed02012-08-01 14:48:59 +02003096 /*
3097 * This should never happen as the root dir always has the same ref
3098 * which is always '..'
3099 */
3100 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003101 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02003102
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003103 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003104 if (!valid_path) {
3105 ret = -ENOMEM;
3106 goto out;
3107 }
3108
Alexander Block31db9f72012-07-25 23:19:24 +02003109 /*
3110 * First, check if the first ref of the current inode was overwritten
3111 * before. If yes, we know that the current inode was already orphanized
3112 * and thus use the orphan name. If not, we can use get_cur_path to
3113 * get the path of the first ref as it would like while receiving at
3114 * this point in time.
3115 * New inodes are always orphan at the beginning, so force to use the
3116 * orphan name in this case.
3117 * The first ref is stored in valid_path and will be updated if it
3118 * gets moved around.
3119 */
3120 if (!sctx->cur_inode_new) {
3121 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3122 sctx->cur_inode_gen);
3123 if (ret < 0)
3124 goto out;
3125 if (ret)
3126 did_overwrite = 1;
3127 }
3128 if (sctx->cur_inode_new || did_overwrite) {
3129 ret = gen_unique_name(sctx, sctx->cur_ino,
3130 sctx->cur_inode_gen, valid_path);
3131 if (ret < 0)
3132 goto out;
3133 is_orphan = 1;
3134 } else {
3135 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3136 valid_path);
3137 if (ret < 0)
3138 goto out;
3139 }
3140
3141 list_for_each_entry(cur, &sctx->new_refs, list) {
3142 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02003143 * We may have refs where the parent directory does not exist
3144 * yet. This happens if the parent directories inum is higher
3145 * the the current inum. To handle this case, we create the
3146 * parent directory out of order. But we need to check if this
3147 * did already happen before due to other refs in the same dir.
3148 */
3149 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3150 if (ret < 0)
3151 goto out;
3152 if (ret == inode_state_will_create) {
3153 ret = 0;
3154 /*
3155 * First check if any of the current inodes refs did
3156 * already create the dir.
3157 */
3158 list_for_each_entry(cur2, &sctx->new_refs, list) {
3159 if (cur == cur2)
3160 break;
3161 if (cur2->dir == cur->dir) {
3162 ret = 1;
3163 break;
3164 }
3165 }
3166
3167 /*
3168 * If that did not happen, check if a previous inode
3169 * did already create the dir.
3170 */
3171 if (!ret)
3172 ret = did_create_dir(sctx, cur->dir);
3173 if (ret < 0)
3174 goto out;
3175 if (!ret) {
3176 ret = send_create_inode(sctx, cur->dir);
3177 if (ret < 0)
3178 goto out;
3179 }
3180 }
3181
3182 /*
Alexander Block31db9f72012-07-25 23:19:24 +02003183 * Check if this new ref would overwrite the first ref of
3184 * another unprocessed inode. If yes, orphanize the
3185 * overwritten inode. If we find an overwritten ref that is
3186 * not the first ref, simply unlink it.
3187 */
3188 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3189 cur->name, cur->name_len,
3190 &ow_inode, &ow_gen);
3191 if (ret < 0)
3192 goto out;
3193 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003194 ret = is_first_ref(sctx->parent_root,
3195 ow_inode, cur->dir, cur->name,
3196 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003197 if (ret < 0)
3198 goto out;
3199 if (ret) {
3200 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3201 cur->full_path);
3202 if (ret < 0)
3203 goto out;
3204 } else {
3205 ret = send_unlink(sctx, cur->full_path);
3206 if (ret < 0)
3207 goto out;
3208 }
3209 }
3210
3211 /*
3212 * link/move the ref to the new place. If we have an orphan
3213 * inode, move it and update valid_path. If not, link or move
3214 * it depending on the inode mode.
3215 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003216 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003217 ret = send_rename(sctx, valid_path, cur->full_path);
3218 if (ret < 0)
3219 goto out;
3220 is_orphan = 0;
3221 ret = fs_path_copy(valid_path, cur->full_path);
3222 if (ret < 0)
3223 goto out;
3224 } else {
3225 if (S_ISDIR(sctx->cur_inode_mode)) {
3226 /*
3227 * Dirs can't be linked, so move it. For moved
3228 * dirs, we always have one new and one deleted
3229 * ref. The deleted ref is ignored later.
3230 */
Filipe David Borba Mananad86477b2014-01-30 13:27:12 +00003231 ret = wait_for_parent_move(sctx, cur);
3232 if (ret < 0)
3233 goto out;
3234 if (ret) {
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003235 ret = add_pending_dir_move(sctx,
3236 cur->dir);
3237 *pending_move = 1;
3238 } else {
3239 ret = send_rename(sctx, valid_path,
3240 cur->full_path);
3241 if (!ret)
3242 ret = fs_path_copy(valid_path,
3243 cur->full_path);
3244 }
Alexander Block31db9f72012-07-25 23:19:24 +02003245 if (ret < 0)
3246 goto out;
3247 } else {
3248 ret = send_link(sctx, cur->full_path,
3249 valid_path);
3250 if (ret < 0)
3251 goto out;
3252 }
3253 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003254 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003255 if (ret < 0)
3256 goto out;
3257 }
3258
3259 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3260 /*
3261 * Check if we can already rmdir the directory. If not,
3262 * orphanize it. For every dir item inside that gets deleted
3263 * later, we do this check again and rmdir it then if possible.
3264 * See the use of check_dirs for more details.
3265 */
3266 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
3267 if (ret < 0)
3268 goto out;
3269 if (ret) {
3270 ret = send_rmdir(sctx, valid_path);
3271 if (ret < 0)
3272 goto out;
3273 } else if (!is_orphan) {
3274 ret = orphanize_inode(sctx, sctx->cur_ino,
3275 sctx->cur_inode_gen, valid_path);
3276 if (ret < 0)
3277 goto out;
3278 is_orphan = 1;
3279 }
3280
3281 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003282 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003283 if (ret < 0)
3284 goto out;
3285 }
Alexander Blockccf16262012-07-28 11:46:29 +02003286 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3287 !list_empty(&sctx->deleted_refs)) {
3288 /*
3289 * We have a moved dir. Add the old parent to check_dirs
3290 */
3291 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3292 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04003293 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02003294 if (ret < 0)
3295 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003296 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3297 /*
3298 * We have a non dir inode. Go through all deleted refs and
3299 * unlink them if they were not already overwritten by other
3300 * inodes.
3301 */
3302 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3303 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3304 sctx->cur_ino, sctx->cur_inode_gen,
3305 cur->name, cur->name_len);
3306 if (ret < 0)
3307 goto out;
3308 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02003309 ret = send_unlink(sctx, cur->full_path);
3310 if (ret < 0)
3311 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003312 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04003313 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003314 if (ret < 0)
3315 goto out;
3316 }
Alexander Block31db9f72012-07-25 23:19:24 +02003317 /*
3318 * If the inode is still orphan, unlink the orphan. This may
3319 * happen when a previous inode did overwrite the first ref
3320 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02003321 * inode. Unlinking does not mean that the inode is deleted in
3322 * all cases. There may still be links to this inode in other
3323 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02003324 */
Alexander Block1f4692d2012-07-28 10:42:24 +02003325 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02003326 ret = send_unlink(sctx, valid_path);
3327 if (ret < 0)
3328 goto out;
3329 }
3330 }
3331
3332 /*
3333 * We did collect all parent dirs where cur_inode was once located. We
3334 * now go through all these dirs and check if they are pending for
3335 * deletion and if it's finally possible to perform the rmdir now.
3336 * We also update the inode stats of the parent dirs here.
3337 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003338 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02003339 /*
3340 * In case we had refs into dirs that were not processed yet,
3341 * we don't need to do the utime and rmdir logic for these dirs.
3342 * The dir will be processed later.
3343 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003344 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02003345 continue;
3346
Josef Bacikba5e8f22013-08-16 16:52:55 -04003347 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003348 if (ret < 0)
3349 goto out;
3350
3351 if (ret == inode_state_did_create ||
3352 ret == inode_state_no_change) {
3353 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04003354 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02003355 if (ret < 0)
3356 goto out;
3357 } else if (ret == inode_state_did_delete) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003358 ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02003359 if (ret < 0)
3360 goto out;
3361 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003362 ret = get_cur_path(sctx, cur->dir,
3363 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003364 if (ret < 0)
3365 goto out;
3366 ret = send_rmdir(sctx, valid_path);
3367 if (ret < 0)
3368 goto out;
3369 }
3370 }
3371 }
3372
Alexander Block31db9f72012-07-25 23:19:24 +02003373 ret = 0;
3374
3375out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04003376 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02003377 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003378 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003379 return ret;
3380}
3381
3382static int __record_new_ref(int num, u64 dir, int index,
3383 struct fs_path *name,
3384 void *ctx)
3385{
3386 int ret = 0;
3387 struct send_ctx *sctx = ctx;
3388 struct fs_path *p;
3389 u64 gen;
3390
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003391 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003392 if (!p)
3393 return -ENOMEM;
3394
3395 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003396 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003397 if (ret < 0)
3398 goto out;
3399
Alexander Block31db9f72012-07-25 23:19:24 +02003400 ret = get_cur_path(sctx, dir, gen, p);
3401 if (ret < 0)
3402 goto out;
3403 ret = fs_path_add_path(p, name);
3404 if (ret < 0)
3405 goto out;
3406
3407 ret = record_ref(&sctx->new_refs, dir, gen, p);
3408
3409out:
3410 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003411 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003412 return ret;
3413}
3414
3415static int __record_deleted_ref(int num, u64 dir, int index,
3416 struct fs_path *name,
3417 void *ctx)
3418{
3419 int ret = 0;
3420 struct send_ctx *sctx = ctx;
3421 struct fs_path *p;
3422 u64 gen;
3423
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003424 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003425 if (!p)
3426 return -ENOMEM;
3427
3428 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003429 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003430 if (ret < 0)
3431 goto out;
3432
3433 ret = get_cur_path(sctx, dir, gen, p);
3434 if (ret < 0)
3435 goto out;
3436 ret = fs_path_add_path(p, name);
3437 if (ret < 0)
3438 goto out;
3439
3440 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3441
3442out:
3443 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003444 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003445 return ret;
3446}
3447
3448static int record_new_ref(struct send_ctx *sctx)
3449{
3450 int ret;
3451
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003452 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3453 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003454 if (ret < 0)
3455 goto out;
3456 ret = 0;
3457
3458out:
3459 return ret;
3460}
3461
3462static int record_deleted_ref(struct send_ctx *sctx)
3463{
3464 int ret;
3465
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003466 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3467 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003468 if (ret < 0)
3469 goto out;
3470 ret = 0;
3471
3472out:
3473 return ret;
3474}
3475
3476struct find_ref_ctx {
3477 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003478 u64 dir_gen;
3479 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003480 struct fs_path *name;
3481 int found_idx;
3482};
3483
3484static int __find_iref(int num, u64 dir, int index,
3485 struct fs_path *name,
3486 void *ctx_)
3487{
3488 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003489 u64 dir_gen;
3490 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003491
3492 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3493 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003494 /*
3495 * To avoid doing extra lookups we'll only do this if everything
3496 * else matches.
3497 */
3498 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3499 NULL, NULL, NULL);
3500 if (ret)
3501 return ret;
3502 if (dir_gen != ctx->dir_gen)
3503 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003504 ctx->found_idx = num;
3505 return 1;
3506 }
3507 return 0;
3508}
3509
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003510static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003511 struct btrfs_path *path,
3512 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003513 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003514{
3515 int ret;
3516 struct find_ref_ctx ctx;
3517
3518 ctx.dir = dir;
3519 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003520 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003521 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003522 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003523
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003524 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003525 if (ret < 0)
3526 return ret;
3527
3528 if (ctx.found_idx == -1)
3529 return -ENOENT;
3530
3531 return ctx.found_idx;
3532}
3533
3534static int __record_changed_new_ref(int num, u64 dir, int index,
3535 struct fs_path *name,
3536 void *ctx)
3537{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003538 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003539 int ret;
3540 struct send_ctx *sctx = ctx;
3541
Josef Bacikba5e8f22013-08-16 16:52:55 -04003542 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3543 NULL, NULL, NULL);
3544 if (ret)
3545 return ret;
3546
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003547 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003548 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003549 if (ret == -ENOENT)
3550 ret = __record_new_ref(num, dir, index, name, sctx);
3551 else if (ret > 0)
3552 ret = 0;
3553
3554 return ret;
3555}
3556
3557static int __record_changed_deleted_ref(int num, u64 dir, int index,
3558 struct fs_path *name,
3559 void *ctx)
3560{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003561 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003562 int ret;
3563 struct send_ctx *sctx = ctx;
3564
Josef Bacikba5e8f22013-08-16 16:52:55 -04003565 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3566 NULL, NULL, NULL);
3567 if (ret)
3568 return ret;
3569
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003570 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003571 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003572 if (ret == -ENOENT)
3573 ret = __record_deleted_ref(num, dir, index, name, sctx);
3574 else if (ret > 0)
3575 ret = 0;
3576
3577 return ret;
3578}
3579
3580static int record_changed_ref(struct send_ctx *sctx)
3581{
3582 int ret = 0;
3583
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003584 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003585 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3586 if (ret < 0)
3587 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003588 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003589 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3590 if (ret < 0)
3591 goto out;
3592 ret = 0;
3593
3594out:
3595 return ret;
3596}
3597
3598/*
3599 * Record and process all refs at once. Needed when an inode changes the
3600 * generation number, which means that it was deleted and recreated.
3601 */
3602static int process_all_refs(struct send_ctx *sctx,
3603 enum btrfs_compare_tree_result cmd)
3604{
3605 int ret;
3606 struct btrfs_root *root;
3607 struct btrfs_path *path;
3608 struct btrfs_key key;
3609 struct btrfs_key found_key;
3610 struct extent_buffer *eb;
3611 int slot;
3612 iterate_inode_ref_t cb;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003613 int pending_move = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003614
3615 path = alloc_path_for_send();
3616 if (!path)
3617 return -ENOMEM;
3618
3619 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3620 root = sctx->send_root;
3621 cb = __record_new_ref;
3622 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3623 root = sctx->parent_root;
3624 cb = __record_deleted_ref;
3625 } else {
David Sterba4d1a63b2014-02-03 19:24:19 +01003626 btrfs_err(sctx->send_root->fs_info,
3627 "Wrong command %d in process_all_refs", cmd);
3628 ret = -EINVAL;
3629 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003630 }
3631
3632 key.objectid = sctx->cmp_key->objectid;
3633 key.type = BTRFS_INODE_REF_KEY;
3634 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003635 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3636 if (ret < 0)
3637 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003638
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003639 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02003640 eb = path->nodes[0];
3641 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003642 if (slot >= btrfs_header_nritems(eb)) {
3643 ret = btrfs_next_leaf(root, path);
3644 if (ret < 0)
3645 goto out;
3646 else if (ret > 0)
3647 break;
3648 continue;
3649 }
3650
Alexander Block31db9f72012-07-25 23:19:24 +02003651 btrfs_item_key_to_cpu(eb, &found_key, slot);
3652
3653 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003654 (found_key.type != BTRFS_INODE_REF_KEY &&
3655 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003656 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003657
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003658 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003659 if (ret < 0)
3660 goto out;
3661
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003662 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003663 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003664 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003665
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00003666 ret = process_recorded_refs(sctx, &pending_move);
3667 /* Only applicable to an incremental send. */
3668 ASSERT(pending_move == 0);
Alexander Block31db9f72012-07-25 23:19:24 +02003669
3670out:
3671 btrfs_free_path(path);
3672 return ret;
3673}
3674
3675static int send_set_xattr(struct send_ctx *sctx,
3676 struct fs_path *path,
3677 const char *name, int name_len,
3678 const char *data, int data_len)
3679{
3680 int ret = 0;
3681
3682 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3683 if (ret < 0)
3684 goto out;
3685
3686 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3687 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3688 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3689
3690 ret = send_cmd(sctx);
3691
3692tlv_put_failure:
3693out:
3694 return ret;
3695}
3696
3697static int send_remove_xattr(struct send_ctx *sctx,
3698 struct fs_path *path,
3699 const char *name, int name_len)
3700{
3701 int ret = 0;
3702
3703 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3704 if (ret < 0)
3705 goto out;
3706
3707 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3708 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3709
3710 ret = send_cmd(sctx);
3711
3712tlv_put_failure:
3713out:
3714 return ret;
3715}
3716
3717static int __process_new_xattr(int num, struct btrfs_key *di_key,
3718 const char *name, int name_len,
3719 const char *data, int data_len,
3720 u8 type, void *ctx)
3721{
3722 int ret;
3723 struct send_ctx *sctx = ctx;
3724 struct fs_path *p;
3725 posix_acl_xattr_header dummy_acl;
3726
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003727 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003728 if (!p)
3729 return -ENOMEM;
3730
3731 /*
3732 * This hack is needed because empty acl's are stored as zero byte
3733 * data in xattrs. Problem with that is, that receiving these zero byte
3734 * acl's will fail later. To fix this, we send a dummy acl list that
3735 * only contains the version number and no entries.
3736 */
3737 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3738 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3739 if (data_len == 0) {
3740 dummy_acl.a_version =
3741 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3742 data = (char *)&dummy_acl;
3743 data_len = sizeof(dummy_acl);
3744 }
3745 }
3746
3747 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3748 if (ret < 0)
3749 goto out;
3750
3751 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3752
3753out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003754 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003755 return ret;
3756}
3757
3758static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3759 const char *name, int name_len,
3760 const char *data, int data_len,
3761 u8 type, void *ctx)
3762{
3763 int ret;
3764 struct send_ctx *sctx = ctx;
3765 struct fs_path *p;
3766
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003767 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003768 if (!p)
3769 return -ENOMEM;
3770
3771 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3772 if (ret < 0)
3773 goto out;
3774
3775 ret = send_remove_xattr(sctx, p, name, name_len);
3776
3777out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003778 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003779 return ret;
3780}
3781
3782static int process_new_xattr(struct send_ctx *sctx)
3783{
3784 int ret = 0;
3785
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003786 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3787 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003788
3789 return ret;
3790}
3791
3792static int process_deleted_xattr(struct send_ctx *sctx)
3793{
3794 int ret;
3795
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003796 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3797 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003798
3799 return ret;
3800}
3801
3802struct find_xattr_ctx {
3803 const char *name;
3804 int name_len;
3805 int found_idx;
3806 char *found_data;
3807 int found_data_len;
3808};
3809
3810static int __find_xattr(int num, struct btrfs_key *di_key,
3811 const char *name, int name_len,
3812 const char *data, int data_len,
3813 u8 type, void *vctx)
3814{
3815 struct find_xattr_ctx *ctx = vctx;
3816
3817 if (name_len == ctx->name_len &&
3818 strncmp(name, ctx->name, name_len) == 0) {
3819 ctx->found_idx = num;
3820 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00003821 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02003822 if (!ctx->found_data)
3823 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02003824 return 1;
3825 }
3826 return 0;
3827}
3828
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003829static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003830 struct btrfs_path *path,
3831 struct btrfs_key *key,
3832 const char *name, int name_len,
3833 char **data, int *data_len)
3834{
3835 int ret;
3836 struct find_xattr_ctx ctx;
3837
3838 ctx.name = name;
3839 ctx.name_len = name_len;
3840 ctx.found_idx = -1;
3841 ctx.found_data = NULL;
3842 ctx.found_data_len = 0;
3843
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003844 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003845 if (ret < 0)
3846 return ret;
3847
3848 if (ctx.found_idx == -1)
3849 return -ENOENT;
3850 if (data) {
3851 *data = ctx.found_data;
3852 *data_len = ctx.found_data_len;
3853 } else {
3854 kfree(ctx.found_data);
3855 }
3856 return ctx.found_idx;
3857}
3858
3859
3860static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3861 const char *name, int name_len,
3862 const char *data, int data_len,
3863 u8 type, void *ctx)
3864{
3865 int ret;
3866 struct send_ctx *sctx = ctx;
3867 char *found_data = NULL;
3868 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003869
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003870 ret = find_xattr(sctx->parent_root, sctx->right_path,
3871 sctx->cmp_key, name, name_len, &found_data,
3872 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003873 if (ret == -ENOENT) {
3874 ret = __process_new_xattr(num, di_key, name, name_len, data,
3875 data_len, type, ctx);
3876 } else if (ret >= 0) {
3877 if (data_len != found_data_len ||
3878 memcmp(data, found_data, data_len)) {
3879 ret = __process_new_xattr(num, di_key, name, name_len,
3880 data, data_len, type, ctx);
3881 } else {
3882 ret = 0;
3883 }
3884 }
3885
3886 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02003887 return ret;
3888}
3889
3890static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3891 const char *name, int name_len,
3892 const char *data, int data_len,
3893 u8 type, void *ctx)
3894{
3895 int ret;
3896 struct send_ctx *sctx = ctx;
3897
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003898 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
3899 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003900 if (ret == -ENOENT)
3901 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3902 data_len, type, ctx);
3903 else if (ret >= 0)
3904 ret = 0;
3905
3906 return ret;
3907}
3908
3909static int process_changed_xattr(struct send_ctx *sctx)
3910{
3911 int ret = 0;
3912
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003913 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003914 sctx->cmp_key, __process_changed_new_xattr, sctx);
3915 if (ret < 0)
3916 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003917 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003918 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3919
3920out:
3921 return ret;
3922}
3923
3924static int process_all_new_xattrs(struct send_ctx *sctx)
3925{
3926 int ret;
3927 struct btrfs_root *root;
3928 struct btrfs_path *path;
3929 struct btrfs_key key;
3930 struct btrfs_key found_key;
3931 struct extent_buffer *eb;
3932 int slot;
3933
3934 path = alloc_path_for_send();
3935 if (!path)
3936 return -ENOMEM;
3937
3938 root = sctx->send_root;
3939
3940 key.objectid = sctx->cmp_key->objectid;
3941 key.type = BTRFS_XATTR_ITEM_KEY;
3942 key.offset = 0;
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003943 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3944 if (ret < 0)
3945 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003946
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003947 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02003948 eb = path->nodes[0];
3949 slot = path->slots[0];
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003950 if (slot >= btrfs_header_nritems(eb)) {
3951 ret = btrfs_next_leaf(root, path);
3952 if (ret < 0) {
3953 goto out;
3954 } else if (ret > 0) {
3955 ret = 0;
3956 break;
3957 }
3958 continue;
3959 }
Alexander Block31db9f72012-07-25 23:19:24 +02003960
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003961 btrfs_item_key_to_cpu(eb, &found_key, slot);
Alexander Block31db9f72012-07-25 23:19:24 +02003962 if (found_key.objectid != key.objectid ||
3963 found_key.type != key.type) {
3964 ret = 0;
3965 goto out;
3966 }
3967
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003968 ret = iterate_dir_item(root, path, &found_key,
3969 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003970 if (ret < 0)
3971 goto out;
3972
Filipe David Borba Mananadff6d0a2014-02-05 16:48:56 +00003973 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02003974 }
3975
3976out:
3977 btrfs_free_path(path);
3978 return ret;
3979}
3980
Josef Baciked259092013-10-25 11:36:01 -04003981static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
3982{
3983 struct btrfs_root *root = sctx->send_root;
3984 struct btrfs_fs_info *fs_info = root->fs_info;
3985 struct inode *inode;
3986 struct page *page;
3987 char *addr;
3988 struct btrfs_key key;
3989 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
3990 pgoff_t last_index;
3991 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
3992 ssize_t ret = 0;
3993
3994 key.objectid = sctx->cur_ino;
3995 key.type = BTRFS_INODE_ITEM_KEY;
3996 key.offset = 0;
3997
3998 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3999 if (IS_ERR(inode))
4000 return PTR_ERR(inode);
4001
4002 if (offset + len > i_size_read(inode)) {
4003 if (offset > i_size_read(inode))
4004 len = 0;
4005 else
4006 len = offset - i_size_read(inode);
4007 }
4008 if (len == 0)
4009 goto out;
4010
4011 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
4012 while (index <= last_index) {
4013 unsigned cur_len = min_t(unsigned, len,
4014 PAGE_CACHE_SIZE - pg_offset);
4015 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4016 if (!page) {
4017 ret = -ENOMEM;
4018 break;
4019 }
4020
4021 if (!PageUptodate(page)) {
4022 btrfs_readpage(NULL, page);
4023 lock_page(page);
4024 if (!PageUptodate(page)) {
4025 unlock_page(page);
4026 page_cache_release(page);
4027 ret = -EIO;
4028 break;
4029 }
4030 }
4031
4032 addr = kmap(page);
4033 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4034 kunmap(page);
4035 unlock_page(page);
4036 page_cache_release(page);
4037 index++;
4038 pg_offset = 0;
4039 len -= cur_len;
4040 ret += cur_len;
4041 }
4042out:
4043 iput(inode);
4044 return ret;
4045}
4046
Alexander Block31db9f72012-07-25 23:19:24 +02004047/*
4048 * Read some bytes from the current inode/file and send a write command to
4049 * user space.
4050 */
4051static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4052{
4053 int ret = 0;
4054 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04004055 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004056
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004057 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004058 if (!p)
4059 return -ENOMEM;
4060
Alexander Block31db9f72012-07-25 23:19:24 +02004061verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4062
Josef Baciked259092013-10-25 11:36:01 -04004063 num_read = fill_read_buf(sctx, offset, len);
4064 if (num_read <= 0) {
4065 if (num_read < 0)
4066 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004067 goto out;
Josef Baciked259092013-10-25 11:36:01 -04004068 }
Alexander Block31db9f72012-07-25 23:19:24 +02004069
4070 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4071 if (ret < 0)
4072 goto out;
4073
4074 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4075 if (ret < 0)
4076 goto out;
4077
4078 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4079 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02004080 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02004081
4082 ret = send_cmd(sctx);
4083
4084tlv_put_failure:
4085out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004086 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004087 if (ret < 0)
4088 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02004089 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02004090}
4091
4092/*
4093 * Send a clone command to user space.
4094 */
4095static int send_clone(struct send_ctx *sctx,
4096 u64 offset, u32 len,
4097 struct clone_root *clone_root)
4098{
4099 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004100 struct fs_path *p;
4101 u64 gen;
4102
4103verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4104 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4105 clone_root->root->objectid, clone_root->ino,
4106 clone_root->offset);
4107
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004108 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02004109 if (!p)
4110 return -ENOMEM;
4111
4112 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4113 if (ret < 0)
4114 goto out;
4115
4116 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4117 if (ret < 0)
4118 goto out;
4119
4120 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4121 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4122 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4123
Alexander Blocke938c8a2012-07-28 16:33:49 +02004124 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02004125 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004126 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004127 if (ret < 0)
4128 goto out;
4129 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4130 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004131 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02004132 }
4133 if (ret < 0)
4134 goto out;
4135
4136 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02004137 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02004138 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00004139 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02004140 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4141 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4142 clone_root->offset);
4143
4144 ret = send_cmd(sctx);
4145
4146tlv_put_failure:
4147out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004148 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02004149 return ret;
4150}
4151
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004152/*
4153 * Send an update extent command to user space.
4154 */
4155static int send_update_extent(struct send_ctx *sctx,
4156 u64 offset, u32 len)
4157{
4158 int ret = 0;
4159 struct fs_path *p;
4160
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004161 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004162 if (!p)
4163 return -ENOMEM;
4164
4165 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4166 if (ret < 0)
4167 goto out;
4168
4169 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4170 if (ret < 0)
4171 goto out;
4172
4173 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4174 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4175 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4176
4177 ret = send_cmd(sctx);
4178
4179tlv_put_failure:
4180out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00004181 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004182 return ret;
4183}
4184
Josef Bacik16e75492013-10-22 12:18:51 -04004185static int send_hole(struct send_ctx *sctx, u64 end)
4186{
4187 struct fs_path *p = NULL;
4188 u64 offset = sctx->cur_inode_last_extent;
4189 u64 len;
4190 int ret = 0;
4191
4192 p = fs_path_alloc();
4193 if (!p)
4194 return -ENOMEM;
4195 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4196 while (offset < end) {
4197 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4198
4199 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4200 if (ret < 0)
4201 break;
4202 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4203 if (ret < 0)
4204 break;
4205 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4206 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4207 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4208 ret = send_cmd(sctx);
4209 if (ret < 0)
4210 break;
4211 offset += len;
4212 }
4213tlv_put_failure:
4214 fs_path_free(p);
4215 return ret;
4216}
4217
Alexander Block31db9f72012-07-25 23:19:24 +02004218static int send_write_or_clone(struct send_ctx *sctx,
4219 struct btrfs_path *path,
4220 struct btrfs_key *key,
4221 struct clone_root *clone_root)
4222{
4223 int ret = 0;
4224 struct btrfs_file_extent_item *ei;
4225 u64 offset = key->offset;
4226 u64 pos = 0;
4227 u64 len;
4228 u32 l;
4229 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004230 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02004231
4232 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4233 struct btrfs_file_extent_item);
4234 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004235 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004236 len = btrfs_file_extent_inline_len(path->nodes[0],
4237 path->slots[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004238 /*
4239 * it is possible the inline item won't cover the whole page,
4240 * but there may be items after this page. Make
4241 * sure to send the whole thing
4242 */
4243 len = PAGE_CACHE_ALIGN(len);
4244 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004245 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04004246 }
Alexander Block31db9f72012-07-25 23:19:24 +02004247
4248 if (offset + len > sctx->cur_inode_size)
4249 len = sctx->cur_inode_size - offset;
4250 if (len == 0) {
4251 ret = 0;
4252 goto out;
4253 }
4254
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00004255 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004256 ret = send_clone(sctx, offset, len, clone_root);
4257 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4258 ret = send_update_extent(sctx, offset, len);
4259 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02004260 while (pos < len) {
4261 l = len - pos;
4262 if (l > BTRFS_SEND_READ_SIZE)
4263 l = BTRFS_SEND_READ_SIZE;
4264 ret = send_write(sctx, pos + offset, l);
4265 if (ret < 0)
4266 goto out;
4267 if (!ret)
4268 break;
4269 pos += ret;
4270 }
4271 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004272 }
Alexander Block31db9f72012-07-25 23:19:24 +02004273out:
4274 return ret;
4275}
4276
4277static int is_extent_unchanged(struct send_ctx *sctx,
4278 struct btrfs_path *left_path,
4279 struct btrfs_key *ekey)
4280{
4281 int ret = 0;
4282 struct btrfs_key key;
4283 struct btrfs_path *path = NULL;
4284 struct extent_buffer *eb;
4285 int slot;
4286 struct btrfs_key found_key;
4287 struct btrfs_file_extent_item *ei;
4288 u64 left_disknr;
4289 u64 right_disknr;
4290 u64 left_offset;
4291 u64 right_offset;
4292 u64 left_offset_fixed;
4293 u64 left_len;
4294 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04004295 u64 left_gen;
4296 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02004297 u8 left_type;
4298 u8 right_type;
4299
4300 path = alloc_path_for_send();
4301 if (!path)
4302 return -ENOMEM;
4303
4304 eb = left_path->nodes[0];
4305 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02004306 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4307 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004308
4309 if (left_type != BTRFS_FILE_EXTENT_REG) {
4310 ret = 0;
4311 goto out;
4312 }
Chris Mason74dd17f2012-08-07 16:25:13 -04004313 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4314 left_len = btrfs_file_extent_num_bytes(eb, ei);
4315 left_offset = btrfs_file_extent_offset(eb, ei);
4316 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004317
4318 /*
4319 * Following comments will refer to these graphics. L is the left
4320 * extents which we are checking at the moment. 1-8 are the right
4321 * extents that we iterate.
4322 *
4323 * |-----L-----|
4324 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4325 *
4326 * |-----L-----|
4327 * |--1--|-2b-|...(same as above)
4328 *
4329 * Alternative situation. Happens on files where extents got split.
4330 * |-----L-----|
4331 * |-----------7-----------|-6-|
4332 *
4333 * Alternative situation. Happens on files which got larger.
4334 * |-----L-----|
4335 * |-8-|
4336 * Nothing follows after 8.
4337 */
4338
4339 key.objectid = ekey->objectid;
4340 key.type = BTRFS_EXTENT_DATA_KEY;
4341 key.offset = ekey->offset;
4342 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4343 if (ret < 0)
4344 goto out;
4345 if (ret) {
4346 ret = 0;
4347 goto out;
4348 }
4349
4350 /*
4351 * Handle special case where the right side has no extents at all.
4352 */
4353 eb = path->nodes[0];
4354 slot = path->slots[0];
4355 btrfs_item_key_to_cpu(eb, &found_key, slot);
4356 if (found_key.objectid != key.objectid ||
4357 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004358 /* If we're a hole then just pretend nothing changed */
4359 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004360 goto out;
4361 }
4362
4363 /*
4364 * We're now on 2a, 2b or 7.
4365 */
4366 key = found_key;
4367 while (key.offset < ekey->offset + left_len) {
4368 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4369 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02004370 if (right_type != BTRFS_FILE_EXTENT_REG) {
4371 ret = 0;
4372 goto out;
4373 }
4374
Josef Bacik007d31f2013-10-31 16:49:02 -04004375 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4376 right_len = btrfs_file_extent_num_bytes(eb, ei);
4377 right_offset = btrfs_file_extent_offset(eb, ei);
4378 right_gen = btrfs_file_extent_generation(eb, ei);
4379
Alexander Block31db9f72012-07-25 23:19:24 +02004380 /*
4381 * Are we at extent 8? If yes, we know the extent is changed.
4382 * This may only happen on the first iteration.
4383 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02004384 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04004385 /* If we're a hole just pretend nothing changed */
4386 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004387 goto out;
4388 }
4389
4390 left_offset_fixed = left_offset;
4391 if (key.offset < ekey->offset) {
4392 /* Fix the right offset for 2a and 7. */
4393 right_offset += ekey->offset - key.offset;
4394 } else {
4395 /* Fix the left offset for all behind 2a and 2b */
4396 left_offset_fixed += key.offset - ekey->offset;
4397 }
4398
4399 /*
4400 * Check if we have the same extent.
4401 */
Alexander Block39540962012-08-01 12:46:05 +02004402 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04004403 left_offset_fixed != right_offset ||
4404 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02004405 ret = 0;
4406 goto out;
4407 }
4408
4409 /*
4410 * Go to the next extent.
4411 */
4412 ret = btrfs_next_item(sctx->parent_root, path);
4413 if (ret < 0)
4414 goto out;
4415 if (!ret) {
4416 eb = path->nodes[0];
4417 slot = path->slots[0];
4418 btrfs_item_key_to_cpu(eb, &found_key, slot);
4419 }
4420 if (ret || found_key.objectid != key.objectid ||
4421 found_key.type != key.type) {
4422 key.offset += right_len;
4423 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00004424 }
4425 if (found_key.offset != key.offset + right_len) {
4426 ret = 0;
4427 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004428 }
4429 key = found_key;
4430 }
4431
4432 /*
4433 * We're now behind the left extent (treat as unchanged) or at the end
4434 * of the right side (treat as changed).
4435 */
4436 if (key.offset >= ekey->offset + left_len)
4437 ret = 1;
4438 else
4439 ret = 0;
4440
4441
4442out:
4443 btrfs_free_path(path);
4444 return ret;
4445}
4446
Josef Bacik16e75492013-10-22 12:18:51 -04004447static int get_last_extent(struct send_ctx *sctx, u64 offset)
4448{
4449 struct btrfs_path *path;
4450 struct btrfs_root *root = sctx->send_root;
4451 struct btrfs_file_extent_item *fi;
4452 struct btrfs_key key;
4453 u64 extent_end;
4454 u8 type;
4455 int ret;
4456
4457 path = alloc_path_for_send();
4458 if (!path)
4459 return -ENOMEM;
4460
4461 sctx->cur_inode_last_extent = 0;
4462
4463 key.objectid = sctx->cur_ino;
4464 key.type = BTRFS_EXTENT_DATA_KEY;
4465 key.offset = offset;
4466 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4467 if (ret < 0)
4468 goto out;
4469 ret = 0;
4470 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4471 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4472 goto out;
4473
4474 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4475 struct btrfs_file_extent_item);
4476 type = btrfs_file_extent_type(path->nodes[0], fi);
4477 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004478 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4479 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004480 extent_end = ALIGN(key.offset + size,
4481 sctx->send_root->sectorsize);
4482 } else {
4483 extent_end = key.offset +
4484 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4485 }
4486 sctx->cur_inode_last_extent = extent_end;
4487out:
4488 btrfs_free_path(path);
4489 return ret;
4490}
4491
4492static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4493 struct btrfs_key *key)
4494{
4495 struct btrfs_file_extent_item *fi;
4496 u64 extent_end;
4497 u8 type;
4498 int ret = 0;
4499
4500 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4501 return 0;
4502
4503 if (sctx->cur_inode_last_extent == (u64)-1) {
4504 ret = get_last_extent(sctx, key->offset - 1);
4505 if (ret)
4506 return ret;
4507 }
4508
4509 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4510 struct btrfs_file_extent_item);
4511 type = btrfs_file_extent_type(path->nodes[0], fi);
4512 if (type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004513 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4514 path->slots[0], fi);
Josef Bacik16e75492013-10-22 12:18:51 -04004515 extent_end = ALIGN(key->offset + size,
4516 sctx->send_root->sectorsize);
4517 } else {
4518 extent_end = key->offset +
4519 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4520 }
Filipe David Borba Mananabf54f412014-01-28 01:38:06 +00004521
4522 if (path->slots[0] == 0 &&
4523 sctx->cur_inode_last_extent < key->offset) {
4524 /*
4525 * We might have skipped entire leafs that contained only
4526 * file extent items for our current inode. These leafs have
4527 * a generation number smaller (older) than the one in the
4528 * current leaf and the leaf our last extent came from, and
4529 * are located between these 2 leafs.
4530 */
4531 ret = get_last_extent(sctx, key->offset - 1);
4532 if (ret)
4533 return ret;
4534 }
4535
Josef Bacik16e75492013-10-22 12:18:51 -04004536 if (sctx->cur_inode_last_extent < key->offset)
4537 ret = send_hole(sctx, key->offset);
4538 sctx->cur_inode_last_extent = extent_end;
4539 return ret;
4540}
4541
Alexander Block31db9f72012-07-25 23:19:24 +02004542static int process_extent(struct send_ctx *sctx,
4543 struct btrfs_path *path,
4544 struct btrfs_key *key)
4545{
Alexander Block31db9f72012-07-25 23:19:24 +02004546 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004547 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004548
4549 if (S_ISLNK(sctx->cur_inode_mode))
4550 return 0;
4551
4552 if (sctx->parent_root && !sctx->cur_inode_new) {
4553 ret = is_extent_unchanged(sctx, path, key);
4554 if (ret < 0)
4555 goto out;
4556 if (ret) {
4557 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004558 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004559 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004560 } else {
4561 struct btrfs_file_extent_item *ei;
4562 u8 type;
4563
4564 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4565 struct btrfs_file_extent_item);
4566 type = btrfs_file_extent_type(path->nodes[0], ei);
4567 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4568 type == BTRFS_FILE_EXTENT_REG) {
4569 /*
4570 * The send spec does not have a prealloc command yet,
4571 * so just leave a hole for prealloc'ed extents until
4572 * we have enough commands queued up to justify rev'ing
4573 * the send spec.
4574 */
4575 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4576 ret = 0;
4577 goto out;
4578 }
4579
4580 /* Have a hole, just skip it. */
4581 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4582 ret = 0;
4583 goto out;
4584 }
4585 }
Alexander Block31db9f72012-07-25 23:19:24 +02004586 }
4587
4588 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4589 sctx->cur_inode_size, &found_clone);
4590 if (ret != -ENOENT && ret < 0)
4591 goto out;
4592
4593 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004594 if (ret)
4595 goto out;
4596out_hole:
4597 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004598out:
4599 return ret;
4600}
4601
4602static int process_all_extents(struct send_ctx *sctx)
4603{
4604 int ret;
4605 struct btrfs_root *root;
4606 struct btrfs_path *path;
4607 struct btrfs_key key;
4608 struct btrfs_key found_key;
4609 struct extent_buffer *eb;
4610 int slot;
4611
4612 root = sctx->send_root;
4613 path = alloc_path_for_send();
4614 if (!path)
4615 return -ENOMEM;
4616
4617 key.objectid = sctx->cmp_key->objectid;
4618 key.type = BTRFS_EXTENT_DATA_KEY;
4619 key.offset = 0;
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004620 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4621 if (ret < 0)
4622 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004623
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004624 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004625 eb = path->nodes[0];
4626 slot = path->slots[0];
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004627
4628 if (slot >= btrfs_header_nritems(eb)) {
4629 ret = btrfs_next_leaf(root, path);
4630 if (ret < 0) {
4631 goto out;
4632 } else if (ret > 0) {
4633 ret = 0;
4634 break;
4635 }
4636 continue;
4637 }
4638
Alexander Block31db9f72012-07-25 23:19:24 +02004639 btrfs_item_key_to_cpu(eb, &found_key, slot);
4640
4641 if (found_key.objectid != key.objectid ||
4642 found_key.type != key.type) {
4643 ret = 0;
4644 goto out;
4645 }
4646
4647 ret = process_extent(sctx, path, &found_key);
4648 if (ret < 0)
4649 goto out;
4650
Filipe David Borba Manana7fdd29d2014-01-24 17:42:09 +00004651 path->slots[0]++;
Alexander Block31db9f72012-07-25 23:19:24 +02004652 }
4653
4654out:
4655 btrfs_free_path(path);
4656 return ret;
4657}
4658
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004659static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4660 int *pending_move,
4661 int *refs_processed)
Alexander Block31db9f72012-07-25 23:19:24 +02004662{
4663 int ret = 0;
4664
4665 if (sctx->cur_ino == 0)
4666 goto out;
4667 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004668 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004669 goto out;
4670 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4671 goto out;
4672
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004673 ret = process_recorded_refs(sctx, pending_move);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004674 if (ret < 0)
4675 goto out;
4676
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004677 *refs_processed = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004678out:
4679 return ret;
4680}
4681
4682static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4683{
4684 int ret = 0;
4685 u64 left_mode;
4686 u64 left_uid;
4687 u64 left_gid;
4688 u64 right_mode;
4689 u64 right_uid;
4690 u64 right_gid;
4691 int need_chmod = 0;
4692 int need_chown = 0;
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004693 int pending_move = 0;
4694 int refs_processed = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004695
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004696 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4697 &refs_processed);
Alexander Block31db9f72012-07-25 23:19:24 +02004698 if (ret < 0)
4699 goto out;
4700
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004701 /*
4702 * We have processed the refs and thus need to advance send_progress.
4703 * Now, calls to get_cur_xxx will take the updated refs of the current
4704 * inode into account.
4705 *
4706 * On the other hand, if our current inode is a directory and couldn't
4707 * be moved/renamed because its parent was renamed/moved too and it has
4708 * a higher inode number, we can only move/rename our current inode
4709 * after we moved/renamed its parent. Therefore in this case operate on
4710 * the old path (pre move/rename) of our current inode, and the
4711 * move/rename will be performed later.
4712 */
4713 if (refs_processed && !pending_move)
4714 sctx->send_progress = sctx->cur_ino + 1;
4715
Alexander Block31db9f72012-07-25 23:19:24 +02004716 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4717 goto out;
4718 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4719 goto out;
4720
4721 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004722 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004723 if (ret < 0)
4724 goto out;
4725
Alex Lyakase2d044f2012-10-17 13:52:47 +00004726 if (!sctx->parent_root || sctx->cur_inode_new) {
4727 need_chown = 1;
4728 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004729 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004730 } else {
4731 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4732 NULL, NULL, &right_mode, &right_uid,
4733 &right_gid, NULL);
4734 if (ret < 0)
4735 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004736
Alex Lyakase2d044f2012-10-17 13:52:47 +00004737 if (left_uid != right_uid || left_gid != right_gid)
4738 need_chown = 1;
4739 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4740 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004741 }
4742
4743 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04004744 if (need_send_hole(sctx)) {
4745 if (sctx->cur_inode_last_extent == (u64)-1) {
4746 ret = get_last_extent(sctx, (u64)-1);
4747 if (ret)
4748 goto out;
4749 }
4750 if (sctx->cur_inode_last_extent <
4751 sctx->cur_inode_size) {
4752 ret = send_hole(sctx, sctx->cur_inode_size);
4753 if (ret)
4754 goto out;
4755 }
4756 }
Alexander Block31db9f72012-07-25 23:19:24 +02004757 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4758 sctx->cur_inode_size);
4759 if (ret < 0)
4760 goto out;
4761 }
4762
4763 if (need_chown) {
4764 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4765 left_uid, left_gid);
4766 if (ret < 0)
4767 goto out;
4768 }
4769 if (need_chmod) {
4770 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4771 left_mode);
4772 if (ret < 0)
4773 goto out;
4774 }
4775
4776 /*
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004777 * If other directory inodes depended on our current directory
4778 * inode's move/rename, now do their move/rename operations.
Alexander Block31db9f72012-07-25 23:19:24 +02004779 */
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00004780 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
4781 ret = apply_children_dir_moves(sctx);
4782 if (ret)
4783 goto out;
4784 }
4785
4786 /*
4787 * Need to send that every time, no matter if it actually
4788 * changed between the two trees as we have done changes to
4789 * the inode before.
4790 */
4791 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004792 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4793 if (ret < 0)
4794 goto out;
4795
4796out:
4797 return ret;
4798}
4799
4800static int changed_inode(struct send_ctx *sctx,
4801 enum btrfs_compare_tree_result result)
4802{
4803 int ret = 0;
4804 struct btrfs_key *key = sctx->cmp_key;
4805 struct btrfs_inode_item *left_ii = NULL;
4806 struct btrfs_inode_item *right_ii = NULL;
4807 u64 left_gen = 0;
4808 u64 right_gen = 0;
4809
Alexander Block31db9f72012-07-25 23:19:24 +02004810 sctx->cur_ino = key->objectid;
4811 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004812 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004813
4814 /*
4815 * Set send_progress to current inode. This will tell all get_cur_xxx
4816 * functions that the current inode's refs are not updated yet. Later,
4817 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4818 */
Alexander Block31db9f72012-07-25 23:19:24 +02004819 sctx->send_progress = sctx->cur_ino;
4820
4821 if (result == BTRFS_COMPARE_TREE_NEW ||
4822 result == BTRFS_COMPARE_TREE_CHANGED) {
4823 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4824 sctx->left_path->slots[0],
4825 struct btrfs_inode_item);
4826 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4827 left_ii);
4828 } else {
4829 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4830 sctx->right_path->slots[0],
4831 struct btrfs_inode_item);
4832 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4833 right_ii);
4834 }
4835 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4836 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4837 sctx->right_path->slots[0],
4838 struct btrfs_inode_item);
4839
4840 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4841 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02004842
4843 /*
4844 * The cur_ino = root dir case is special here. We can't treat
4845 * the inode as deleted+reused because it would generate a
4846 * stream that tries to delete/mkdir the root dir.
4847 */
4848 if (left_gen != right_gen &&
4849 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02004850 sctx->cur_inode_new_gen = 1;
4851 }
4852
4853 if (result == BTRFS_COMPARE_TREE_NEW) {
4854 sctx->cur_inode_gen = left_gen;
4855 sctx->cur_inode_new = 1;
4856 sctx->cur_inode_deleted = 0;
4857 sctx->cur_inode_size = btrfs_inode_size(
4858 sctx->left_path->nodes[0], left_ii);
4859 sctx->cur_inode_mode = btrfs_inode_mode(
4860 sctx->left_path->nodes[0], left_ii);
4861 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02004862 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004863 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4864 sctx->cur_inode_gen = right_gen;
4865 sctx->cur_inode_new = 0;
4866 sctx->cur_inode_deleted = 1;
4867 sctx->cur_inode_size = btrfs_inode_size(
4868 sctx->right_path->nodes[0], right_ii);
4869 sctx->cur_inode_mode = btrfs_inode_mode(
4870 sctx->right_path->nodes[0], right_ii);
4871 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02004872 /*
4873 * We need to do some special handling in case the inode was
4874 * reported as changed with a changed generation number. This
4875 * means that the original inode was deleted and new inode
4876 * reused the same inum. So we have to treat the old inode as
4877 * deleted and the new one as new.
4878 */
Alexander Block31db9f72012-07-25 23:19:24 +02004879 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02004880 /*
4881 * First, process the inode as if it was deleted.
4882 */
Alexander Block31db9f72012-07-25 23:19:24 +02004883 sctx->cur_inode_gen = right_gen;
4884 sctx->cur_inode_new = 0;
4885 sctx->cur_inode_deleted = 1;
4886 sctx->cur_inode_size = btrfs_inode_size(
4887 sctx->right_path->nodes[0], right_ii);
4888 sctx->cur_inode_mode = btrfs_inode_mode(
4889 sctx->right_path->nodes[0], right_ii);
4890 ret = process_all_refs(sctx,
4891 BTRFS_COMPARE_TREE_DELETED);
4892 if (ret < 0)
4893 goto out;
4894
Alexander Block766702e2012-07-28 14:11:31 +02004895 /*
4896 * Now process the inode as if it was new.
4897 */
Alexander Block31db9f72012-07-25 23:19:24 +02004898 sctx->cur_inode_gen = left_gen;
4899 sctx->cur_inode_new = 1;
4900 sctx->cur_inode_deleted = 0;
4901 sctx->cur_inode_size = btrfs_inode_size(
4902 sctx->left_path->nodes[0], left_ii);
4903 sctx->cur_inode_mode = btrfs_inode_mode(
4904 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02004905 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004906 if (ret < 0)
4907 goto out;
4908
4909 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4910 if (ret < 0)
4911 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004912 /*
4913 * Advance send_progress now as we did not get into
4914 * process_recorded_refs_if_needed in the new_gen case.
4915 */
4916 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02004917
4918 /*
4919 * Now process all extents and xattrs of the inode as if
4920 * they were all new.
4921 */
Alexander Block31db9f72012-07-25 23:19:24 +02004922 ret = process_all_extents(sctx);
4923 if (ret < 0)
4924 goto out;
4925 ret = process_all_new_xattrs(sctx);
4926 if (ret < 0)
4927 goto out;
4928 } else {
4929 sctx->cur_inode_gen = left_gen;
4930 sctx->cur_inode_new = 0;
4931 sctx->cur_inode_new_gen = 0;
4932 sctx->cur_inode_deleted = 0;
4933 sctx->cur_inode_size = btrfs_inode_size(
4934 sctx->left_path->nodes[0], left_ii);
4935 sctx->cur_inode_mode = btrfs_inode_mode(
4936 sctx->left_path->nodes[0], left_ii);
4937 }
4938 }
4939
4940out:
4941 return ret;
4942}
4943
Alexander Block766702e2012-07-28 14:11:31 +02004944/*
4945 * We have to process new refs before deleted refs, but compare_trees gives us
4946 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4947 * first and later process them in process_recorded_refs.
4948 * For the cur_inode_new_gen case, we skip recording completely because
4949 * changed_inode did already initiate processing of refs. The reason for this is
4950 * that in this case, compare_tree actually compares the refs of 2 different
4951 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4952 * refs of the right tree as deleted and all refs of the left tree as new.
4953 */
Alexander Block31db9f72012-07-25 23:19:24 +02004954static int changed_ref(struct send_ctx *sctx,
4955 enum btrfs_compare_tree_result result)
4956{
4957 int ret = 0;
4958
4959 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4960
4961 if (!sctx->cur_inode_new_gen &&
4962 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4963 if (result == BTRFS_COMPARE_TREE_NEW)
4964 ret = record_new_ref(sctx);
4965 else if (result == BTRFS_COMPARE_TREE_DELETED)
4966 ret = record_deleted_ref(sctx);
4967 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4968 ret = record_changed_ref(sctx);
4969 }
4970
4971 return ret;
4972}
4973
Alexander Block766702e2012-07-28 14:11:31 +02004974/*
4975 * Process new/deleted/changed xattrs. We skip processing in the
4976 * cur_inode_new_gen case because changed_inode did already initiate processing
4977 * of xattrs. The reason is the same as in changed_ref
4978 */
Alexander Block31db9f72012-07-25 23:19:24 +02004979static int changed_xattr(struct send_ctx *sctx,
4980 enum btrfs_compare_tree_result result)
4981{
4982 int ret = 0;
4983
4984 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4985
4986 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4987 if (result == BTRFS_COMPARE_TREE_NEW)
4988 ret = process_new_xattr(sctx);
4989 else if (result == BTRFS_COMPARE_TREE_DELETED)
4990 ret = process_deleted_xattr(sctx);
4991 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4992 ret = process_changed_xattr(sctx);
4993 }
4994
4995 return ret;
4996}
4997
Alexander Block766702e2012-07-28 14:11:31 +02004998/*
4999 * Process new/deleted/changed extents. We skip processing in the
5000 * cur_inode_new_gen case because changed_inode did already initiate processing
5001 * of extents. The reason is the same as in changed_ref
5002 */
Alexander Block31db9f72012-07-25 23:19:24 +02005003static int changed_extent(struct send_ctx *sctx,
5004 enum btrfs_compare_tree_result result)
5005{
5006 int ret = 0;
5007
5008 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5009
5010 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5011 if (result != BTRFS_COMPARE_TREE_DELETED)
5012 ret = process_extent(sctx, sctx->left_path,
5013 sctx->cmp_key);
5014 }
5015
5016 return ret;
5017}
5018
Josef Bacikba5e8f22013-08-16 16:52:55 -04005019static int dir_changed(struct send_ctx *sctx, u64 dir)
5020{
5021 u64 orig_gen, new_gen;
5022 int ret;
5023
5024 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5025 NULL, NULL);
5026 if (ret)
5027 return ret;
5028
5029 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5030 NULL, NULL, NULL);
5031 if (ret)
5032 return ret;
5033
5034 return (orig_gen != new_gen) ? 1 : 0;
5035}
5036
5037static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5038 struct btrfs_key *key)
5039{
5040 struct btrfs_inode_extref *extref;
5041 struct extent_buffer *leaf;
5042 u64 dirid = 0, last_dirid = 0;
5043 unsigned long ptr;
5044 u32 item_size;
5045 u32 cur_offset = 0;
5046 int ref_name_len;
5047 int ret = 0;
5048
5049 /* Easy case, just check this one dirid */
5050 if (key->type == BTRFS_INODE_REF_KEY) {
5051 dirid = key->offset;
5052
5053 ret = dir_changed(sctx, dirid);
5054 goto out;
5055 }
5056
5057 leaf = path->nodes[0];
5058 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5059 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5060 while (cur_offset < item_size) {
5061 extref = (struct btrfs_inode_extref *)(ptr +
5062 cur_offset);
5063 dirid = btrfs_inode_extref_parent(leaf, extref);
5064 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5065 cur_offset += ref_name_len + sizeof(*extref);
5066 if (dirid == last_dirid)
5067 continue;
5068 ret = dir_changed(sctx, dirid);
5069 if (ret)
5070 break;
5071 last_dirid = dirid;
5072 }
5073out:
5074 return ret;
5075}
5076
Alexander Block766702e2012-07-28 14:11:31 +02005077/*
5078 * Updates compare related fields in sctx and simply forwards to the actual
5079 * changed_xxx functions.
5080 */
Alexander Block31db9f72012-07-25 23:19:24 +02005081static int changed_cb(struct btrfs_root *left_root,
5082 struct btrfs_root *right_root,
5083 struct btrfs_path *left_path,
5084 struct btrfs_path *right_path,
5085 struct btrfs_key *key,
5086 enum btrfs_compare_tree_result result,
5087 void *ctx)
5088{
5089 int ret = 0;
5090 struct send_ctx *sctx = ctx;
5091
Josef Bacikba5e8f22013-08-16 16:52:55 -04005092 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04005093 if (key->type == BTRFS_INODE_REF_KEY ||
5094 key->type == BTRFS_INODE_EXTREF_KEY) {
5095 ret = compare_refs(sctx, left_path, key);
5096 if (!ret)
5097 return 0;
5098 if (ret < 0)
5099 return ret;
5100 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5101 return maybe_send_hole(sctx, left_path, key);
5102 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005103 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005104 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04005105 result = BTRFS_COMPARE_TREE_CHANGED;
5106 ret = 0;
5107 }
5108
Alexander Block31db9f72012-07-25 23:19:24 +02005109 sctx->left_path = left_path;
5110 sctx->right_path = right_path;
5111 sctx->cmp_key = key;
5112
5113 ret = finish_inode_if_needed(sctx, 0);
5114 if (ret < 0)
5115 goto out;
5116
Alexander Block2981e222012-08-01 14:47:03 +02005117 /* Ignore non-FS objects */
5118 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5119 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5120 goto out;
5121
Alexander Block31db9f72012-07-25 23:19:24 +02005122 if (key->type == BTRFS_INODE_ITEM_KEY)
5123 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00005124 else if (key->type == BTRFS_INODE_REF_KEY ||
5125 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02005126 ret = changed_ref(sctx, result);
5127 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5128 ret = changed_xattr(sctx, result);
5129 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5130 ret = changed_extent(sctx, result);
5131
5132out:
5133 return ret;
5134}
5135
5136static int full_send_tree(struct send_ctx *sctx)
5137{
5138 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02005139 struct btrfs_root *send_root = sctx->send_root;
5140 struct btrfs_key key;
5141 struct btrfs_key found_key;
5142 struct btrfs_path *path;
5143 struct extent_buffer *eb;
5144 int slot;
5145 u64 start_ctransid;
5146 u64 ctransid;
5147
5148 path = alloc_path_for_send();
5149 if (!path)
5150 return -ENOMEM;
5151
Anand Jain5f3ab902012-12-07 09:28:54 +00005152 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005153 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005154 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005155
5156 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5157 key.type = BTRFS_INODE_ITEM_KEY;
5158 key.offset = 0;
5159
Alexander Block31db9f72012-07-25 23:19:24 +02005160 /*
Alexander Block766702e2012-07-28 14:11:31 +02005161 * Make sure the tree has not changed after re-joining. We detect this
5162 * by comparing start_ctransid and ctransid. They should always match.
Alexander Block31db9f72012-07-25 23:19:24 +02005163 */
Anand Jain5f3ab902012-12-07 09:28:54 +00005164 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005165 ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005166 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02005167
5168 if (ctransid != start_ctransid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005169 WARN(1, KERN_WARNING "BTRFS: the root that you're trying to "
Alexander Block31db9f72012-07-25 23:19:24 +02005170 "send was modified in between. This is "
5171 "probably a bug.\n");
5172 ret = -EIO;
5173 goto out;
5174 }
5175
5176 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5177 if (ret < 0)
5178 goto out;
5179 if (ret)
5180 goto out_finish;
5181
5182 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02005183 eb = path->nodes[0];
5184 slot = path->slots[0];
5185 btrfs_item_key_to_cpu(eb, &found_key, slot);
5186
5187 ret = changed_cb(send_root, NULL, path, NULL,
5188 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5189 if (ret < 0)
5190 goto out;
5191
5192 key.objectid = found_key.objectid;
5193 key.type = found_key.type;
5194 key.offset = found_key.offset + 1;
5195
5196 ret = btrfs_next_item(send_root, path);
5197 if (ret < 0)
5198 goto out;
5199 if (ret) {
5200 ret = 0;
5201 break;
5202 }
5203 }
5204
5205out_finish:
5206 ret = finish_inode_if_needed(sctx, 1);
5207
5208out:
5209 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02005210 return ret;
5211}
5212
5213static int send_subvol(struct send_ctx *sctx)
5214{
5215 int ret;
5216
Stefan Behrensc2c71322013-04-10 17:10:52 +00005217 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5218 ret = send_header(sctx);
5219 if (ret < 0)
5220 goto out;
5221 }
Alexander Block31db9f72012-07-25 23:19:24 +02005222
5223 ret = send_subvol_begin(sctx);
5224 if (ret < 0)
5225 goto out;
5226
5227 if (sctx->parent_root) {
5228 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5229 changed_cb, sctx);
5230 if (ret < 0)
5231 goto out;
5232 ret = finish_inode_if_needed(sctx, 1);
5233 if (ret < 0)
5234 goto out;
5235 } else {
5236 ret = full_send_tree(sctx);
5237 if (ret < 0)
5238 goto out;
5239 }
5240
5241out:
Alexander Block31db9f72012-07-25 23:19:24 +02005242 free_recorded_refs(sctx);
5243 return ret;
5244}
5245
David Sterba66ef7d62013-12-17 15:07:20 +01005246static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5247{
5248 spin_lock(&root->root_item_lock);
5249 root->send_in_progress--;
5250 /*
5251 * Not much left to do, we don't know why it's unbalanced and
5252 * can't blindly reset it to 0.
5253 */
5254 if (root->send_in_progress < 0)
5255 btrfs_err(root->fs_info,
5256 "send_in_progres unbalanced %d root %llu\n",
5257 root->send_in_progress, root->root_key.objectid);
5258 spin_unlock(&root->root_item_lock);
5259}
5260
Alexander Block31db9f72012-07-25 23:19:24 +02005261long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5262{
5263 int ret = 0;
5264 struct btrfs_root *send_root;
5265 struct btrfs_root *clone_root;
5266 struct btrfs_fs_info *fs_info;
5267 struct btrfs_ioctl_send_args *arg = NULL;
5268 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02005269 struct send_ctx *sctx = NULL;
5270 u32 i;
5271 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01005272 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08005273 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08005274 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02005275
5276 if (!capable(CAP_SYS_ADMIN))
5277 return -EPERM;
5278
Al Viro496ad9a2013-01-23 17:07:38 -05005279 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02005280 fs_info = send_root->fs_info;
5281
Josef Bacik139f8072013-05-20 11:26:50 -04005282 /*
David Sterba2c686532013-12-16 17:34:17 +01005283 * The subvolume must remain read-only during send, protect against
5284 * making it RW.
5285 */
5286 spin_lock(&send_root->root_item_lock);
5287 send_root->send_in_progress++;
5288 spin_unlock(&send_root->root_item_lock);
5289
5290 /*
Josef Bacik139f8072013-05-20 11:26:50 -04005291 * This is done when we lookup the root, it should already be complete
5292 * by the time we get here.
5293 */
5294 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5295
5296 /*
David Sterba2c686532013-12-16 17:34:17 +01005297 * Userspace tools do the checks and warn the user if it's
5298 * not RO.
5299 */
5300 if (!btrfs_root_readonly(send_root)) {
5301 ret = -EPERM;
5302 goto out;
5303 }
5304
Alexander Block31db9f72012-07-25 23:19:24 +02005305 arg = memdup_user(arg_, sizeof(*arg));
5306 if (IS_ERR(arg)) {
5307 ret = PTR_ERR(arg);
5308 arg = NULL;
5309 goto out;
5310 }
5311
5312 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05005313 sizeof(*arg->clone_sources) *
5314 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02005315 ret = -EFAULT;
5316 goto out;
5317 }
5318
Stefan Behrensc2c71322013-04-10 17:10:52 +00005319 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005320 ret = -EINVAL;
5321 goto out;
5322 }
5323
Alexander Block31db9f72012-07-25 23:19:24 +02005324 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5325 if (!sctx) {
5326 ret = -ENOMEM;
5327 goto out;
5328 }
5329
5330 INIT_LIST_HEAD(&sctx->new_refs);
5331 INIT_LIST_HEAD(&sctx->deleted_refs);
5332 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5333 INIT_LIST_HEAD(&sctx->name_cache_list);
5334
Mark Fashehcb95e7b2013-02-04 20:54:57 +00005335 sctx->flags = arg->flags;
5336
Alexander Block31db9f72012-07-25 23:19:24 +02005337 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00005338 if (!sctx->send_filp) {
5339 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02005340 goto out;
5341 }
5342
Alexander Block31db9f72012-07-25 23:19:24 +02005343 sctx->send_root = send_root;
5344 sctx->clone_roots_cnt = arg->clone_sources_count;
5345
5346 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5347 sctx->send_buf = vmalloc(sctx->send_max_size);
5348 if (!sctx->send_buf) {
5349 ret = -ENOMEM;
5350 goto out;
5351 }
5352
5353 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5354 if (!sctx->read_buf) {
5355 ret = -ENOMEM;
5356 goto out;
5357 }
5358
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005359 sctx->pending_dir_moves = RB_ROOT;
5360 sctx->waiting_dir_moves = RB_ROOT;
5361
Alexander Block31db9f72012-07-25 23:19:24 +02005362 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5363 (arg->clone_sources_count + 1));
5364 if (!sctx->clone_roots) {
5365 ret = -ENOMEM;
5366 goto out;
5367 }
5368
5369 if (arg->clone_sources_count) {
5370 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5371 sizeof(*arg->clone_sources));
5372 if (!clone_sources_tmp) {
5373 ret = -ENOMEM;
5374 goto out;
5375 }
5376
5377 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5378 arg->clone_sources_count *
5379 sizeof(*arg->clone_sources));
5380 if (ret) {
5381 ret = -EFAULT;
5382 goto out;
5383 }
5384
5385 for (i = 0; i < arg->clone_sources_count; i++) {
5386 key.objectid = clone_sources_tmp[i];
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 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02005393 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005394 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005395 ret = PTR_ERR(clone_root);
5396 goto out;
5397 }
David Sterba2c686532013-12-16 17:34:17 +01005398 clone_sources_to_rollback = i + 1;
5399 spin_lock(&clone_root->root_item_lock);
5400 clone_root->send_in_progress++;
5401 if (!btrfs_root_readonly(clone_root)) {
5402 spin_unlock(&clone_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(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005408 srcu_read_unlock(&fs_info->subvol_srcu, index);
5409
Alexander Block31db9f72012-07-25 23:19:24 +02005410 sctx->clone_roots[i].root = clone_root;
5411 }
5412 vfree(clone_sources_tmp);
5413 clone_sources_tmp = NULL;
5414 }
5415
5416 if (arg->parent_root) {
5417 key.objectid = arg->parent_root;
5418 key.type = BTRFS_ROOT_ITEM_KEY;
5419 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08005420
5421 index = srcu_read_lock(&fs_info->subvol_srcu);
5422
Alexander Block31db9f72012-07-25 23:19:24 +02005423 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005424 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08005425 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00005426 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02005427 goto out;
5428 }
Wang Shilong18f687d2014-01-07 17:25:19 +08005429
David Sterba2c686532013-12-16 17:34:17 +01005430 spin_lock(&sctx->parent_root->root_item_lock);
5431 sctx->parent_root->send_in_progress++;
5432 if (!btrfs_root_readonly(sctx->parent_root)) {
5433 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005434 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01005435 ret = -EPERM;
5436 goto out;
5437 }
5438 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08005439
5440 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02005441 }
5442
5443 /*
5444 * Clones from send_root are allowed, but only if the clone source
5445 * is behind the current send position. This is checked while searching
5446 * for possible clone sources.
5447 */
5448 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5449
5450 /* We do a bsearch later */
5451 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5452 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5453 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08005454 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02005455
5456 ret = send_subvol(sctx);
5457 if (ret < 0)
5458 goto out;
5459
Stefan Behrensc2c71322013-04-10 17:10:52 +00005460 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5461 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5462 if (ret < 0)
5463 goto out;
5464 ret = send_cmd(sctx);
5465 if (ret < 0)
5466 goto out;
5467 }
Alexander Block31db9f72012-07-25 23:19:24 +02005468
5469out:
Filipe David Borba Manana9f037402014-01-22 10:00:53 +00005470 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5471 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5472 struct rb_node *n;
5473 struct pending_dir_move *pm;
5474
5475 n = rb_first(&sctx->pending_dir_moves);
5476 pm = rb_entry(n, struct pending_dir_move, node);
5477 while (!list_empty(&pm->list)) {
5478 struct pending_dir_move *pm2;
5479
5480 pm2 = list_first_entry(&pm->list,
5481 struct pending_dir_move, list);
5482 free_pending_move(sctx, pm2);
5483 }
5484 free_pending_move(sctx, pm);
5485 }
5486
5487 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5488 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5489 struct rb_node *n;
5490 struct waiting_dir_move *dm;
5491
5492 n = rb_first(&sctx->waiting_dir_moves);
5493 dm = rb_entry(n, struct waiting_dir_move, node);
5494 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5495 kfree(dm);
5496 }
5497
Wang Shilong896c14f2014-01-07 17:25:18 +08005498 if (sort_clone_roots) {
5499 for (i = 0; i < sctx->clone_roots_cnt; i++)
5500 btrfs_root_dec_send_in_progress(
5501 sctx->clone_roots[i].root);
5502 } else {
5503 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5504 btrfs_root_dec_send_in_progress(
5505 sctx->clone_roots[i].root);
5506
5507 btrfs_root_dec_send_in_progress(send_root);
5508 }
David Sterba66ef7d62013-12-17 15:07:20 +01005509 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5510 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01005511
Alexander Block31db9f72012-07-25 23:19:24 +02005512 kfree(arg);
5513 vfree(clone_sources_tmp);
5514
5515 if (sctx) {
5516 if (sctx->send_filp)
5517 fput(sctx->send_filp);
5518
5519 vfree(sctx->clone_roots);
5520 vfree(sctx->send_buf);
5521 vfree(sctx->read_buf);
5522
5523 name_cache_free(sctx);
5524
5525 kfree(sctx);
5526 }
5527
5528 return ret;
5529}