blob: fc1f0abb8fe463ae3d68f7fbe84f5aab47cf1200 [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>
27#include <linux/crc32c.h>
Stephen Rothwella1857eb2012-07-27 10:11:13 +100028#include <linux/vmalloc.h>
Andy Shevchenkoed848852013-08-21 10:32:13 +030029#include <linux/string.h>
Alexander Block31db9f72012-07-25 23:19:24 +020030
31#include "send.h"
32#include "backref.h"
33#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;
54 char *prepared;
55
56 char *buf;
57 int buf_len;
Stefan Behrens35a36212013-08-14 18:12:25 +020058 unsigned int reversed:1;
59 unsigned int virtual_mem:1;
Alexander Block31db9f72012-07-25 23:19:24 +020060 char inline_buf[];
61 };
62 char pad[PAGE_SIZE];
63 };
64};
65#define FS_PATH_INLINE_SIZE \
66 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
67
68
69/* reused for each extent */
70struct clone_root {
71 struct btrfs_root *root;
72 u64 ino;
73 u64 offset;
74
75 u64 found_refs;
76};
77
78#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
79#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
80
81struct send_ctx {
82 struct file *send_filp;
83 loff_t send_off;
84 char *send_buf;
85 u32 send_size;
86 u32 send_max_size;
87 u64 total_send_size;
88 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000089 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020090
Alexander Block31db9f72012-07-25 23:19:24 +020091 struct btrfs_root *send_root;
92 struct btrfs_root *parent_root;
93 struct clone_root *clone_roots;
94 int clone_roots_cnt;
95
96 /* current state of the compare_tree call */
97 struct btrfs_path *left_path;
98 struct btrfs_path *right_path;
99 struct btrfs_key *cmp_key;
100
101 /*
102 * infos of the currently processed inode. In case of deleted inodes,
103 * these are the values from the deleted inode.
104 */
105 u64 cur_ino;
106 u64 cur_inode_gen;
107 int cur_inode_new;
108 int cur_inode_new_gen;
109 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200110 u64 cur_inode_size;
111 u64 cur_inode_mode;
Josef Bacik16e75492013-10-22 12:18:51 -0400112 u64 cur_inode_last_extent;
Alexander Block31db9f72012-07-25 23:19:24 +0200113
114 u64 send_progress;
115
116 struct list_head new_refs;
117 struct list_head deleted_refs;
118
119 struct radix_tree_root name_cache;
120 struct list_head name_cache_list;
121 int name_cache_size;
122
Alexander Block31db9f72012-07-25 23:19:24 +0200123 char *read_buf;
124};
125
126struct name_cache_entry {
127 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200128 /*
129 * radix_tree has only 32bit entries but we need to handle 64bit inums.
130 * We use the lower 32bit of the 64bit inum to store it in the tree. If
131 * more then one inum would fall into the same entry, we use radix_list
132 * to store the additional entries. radix_list is also used to store
133 * entries where two entries have the same inum but different
134 * generations.
135 */
136 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200137 u64 ino;
138 u64 gen;
139 u64 parent_ino;
140 u64 parent_gen;
141 int ret;
142 int need_later_update;
143 int name_len;
144 char name[];
145};
146
Josef Bacik16e75492013-10-22 12:18:51 -0400147static int need_send_hole(struct send_ctx *sctx)
148{
149 return (sctx->parent_root && !sctx->cur_inode_new &&
150 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
151 S_ISREG(sctx->cur_inode_mode));
152}
153
Alexander Block31db9f72012-07-25 23:19:24 +0200154static void fs_path_reset(struct fs_path *p)
155{
156 if (p->reversed) {
157 p->start = p->buf + p->buf_len - 1;
158 p->end = p->start;
159 *p->start = 0;
160 } else {
161 p->start = p->buf;
162 p->end = p->start;
163 *p->start = 0;
164 }
165}
166
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000167static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200168{
169 struct fs_path *p;
170
171 p = kmalloc(sizeof(*p), GFP_NOFS);
172 if (!p)
173 return NULL;
174 p->reversed = 0;
175 p->virtual_mem = 0;
176 p->buf = p->inline_buf;
177 p->buf_len = FS_PATH_INLINE_SIZE;
178 fs_path_reset(p);
179 return p;
180}
181
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000182static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200183{
184 struct fs_path *p;
185
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000186 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200187 if (!p)
188 return NULL;
189 p->reversed = 1;
190 fs_path_reset(p);
191 return p;
192}
193
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000194static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200195{
196 if (!p)
197 return;
198 if (p->buf != p->inline_buf) {
199 if (p->virtual_mem)
200 vfree(p->buf);
201 else
202 kfree(p->buf);
203 }
204 kfree(p);
205}
206
207static int fs_path_len(struct fs_path *p)
208{
209 return p->end - p->start;
210}
211
212static int fs_path_ensure_buf(struct fs_path *p, int len)
213{
214 char *tmp_buf;
215 int path_len;
216 int old_buf_len;
217
218 len++;
219
220 if (p->buf_len >= len)
221 return 0;
222
223 path_len = p->end - p->start;
224 old_buf_len = p->buf_len;
225 len = PAGE_ALIGN(len);
226
227 if (p->buf == p->inline_buf) {
Joe Perches8be04b92013-06-19 12:15:53 -0700228 tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +0200229 if (!tmp_buf) {
230 tmp_buf = vmalloc(len);
231 if (!tmp_buf)
232 return -ENOMEM;
233 p->virtual_mem = 1;
234 }
235 memcpy(tmp_buf, p->buf, p->buf_len);
236 p->buf = tmp_buf;
237 p->buf_len = len;
238 } else {
239 if (p->virtual_mem) {
240 tmp_buf = vmalloc(len);
241 if (!tmp_buf)
242 return -ENOMEM;
243 memcpy(tmp_buf, p->buf, p->buf_len);
244 vfree(p->buf);
245 } else {
246 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
247 if (!tmp_buf) {
248 tmp_buf = vmalloc(len);
249 if (!tmp_buf)
250 return -ENOMEM;
251 memcpy(tmp_buf, p->buf, p->buf_len);
252 kfree(p->buf);
253 p->virtual_mem = 1;
254 }
255 }
256 p->buf = tmp_buf;
257 p->buf_len = len;
258 }
259 if (p->reversed) {
260 tmp_buf = p->buf + old_buf_len - path_len - 1;
261 p->end = p->buf + p->buf_len - 1;
262 p->start = p->end - path_len;
263 memmove(p->start, tmp_buf, path_len + 1);
264 } else {
265 p->start = p->buf;
266 p->end = p->start + path_len;
267 }
268 return 0;
269}
270
271static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
272{
273 int ret;
274 int new_len;
275
276 new_len = p->end - p->start + name_len;
277 if (p->start != p->end)
278 new_len++;
279 ret = fs_path_ensure_buf(p, new_len);
280 if (ret < 0)
281 goto out;
282
283 if (p->reversed) {
284 if (p->start != p->end)
285 *--p->start = '/';
286 p->start -= name_len;
287 p->prepared = p->start;
288 } else {
289 if (p->start != p->end)
290 *p->end++ = '/';
291 p->prepared = p->end;
292 p->end += name_len;
293 *p->end = 0;
294 }
295
296out:
297 return ret;
298}
299
300static int fs_path_add(struct fs_path *p, const char *name, int name_len)
301{
302 int ret;
303
304 ret = fs_path_prepare_for_add(p, name_len);
305 if (ret < 0)
306 goto out;
307 memcpy(p->prepared, name, name_len);
308 p->prepared = NULL;
309
310out:
311 return ret;
312}
313
314static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
315{
316 int ret;
317
318 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
319 if (ret < 0)
320 goto out;
321 memcpy(p->prepared, p2->start, p2->end - p2->start);
322 p->prepared = NULL;
323
324out:
325 return ret;
326}
327
328static int fs_path_add_from_extent_buffer(struct fs_path *p,
329 struct extent_buffer *eb,
330 unsigned long off, int len)
331{
332 int ret;
333
334 ret = fs_path_prepare_for_add(p, len);
335 if (ret < 0)
336 goto out;
337
338 read_extent_buffer(eb, p->prepared, off, len);
339 p->prepared = NULL;
340
341out:
342 return ret;
343}
344
Alexander Block31db9f72012-07-25 23:19:24 +0200345static int fs_path_copy(struct fs_path *p, struct fs_path *from)
346{
347 int ret;
348
349 p->reversed = from->reversed;
350 fs_path_reset(p);
351
352 ret = fs_path_add_path(p, from);
353
354 return ret;
355}
356
357
358static void fs_path_unreverse(struct fs_path *p)
359{
360 char *tmp;
361 int len;
362
363 if (!p->reversed)
364 return;
365
366 tmp = p->start;
367 len = p->end - p->start;
368 p->start = p->buf;
369 p->end = p->start + len;
370 memmove(p->start, tmp, len + 1);
371 p->reversed = 0;
372}
373
374static struct btrfs_path *alloc_path_for_send(void)
375{
376 struct btrfs_path *path;
377
378 path = btrfs_alloc_path();
379 if (!path)
380 return NULL;
381 path->search_commit_root = 1;
382 path->skip_locking = 1;
383 return path;
384}
385
Eric Sandeen48a3b632013-04-25 20:41:01 +0000386static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200387{
388 int ret;
389 mm_segment_t old_fs;
390 u32 pos = 0;
391
392 old_fs = get_fs();
393 set_fs(KERNEL_DS);
394
395 while (pos < len) {
Anand Jain1bcea352012-09-14 00:04:21 -0600396 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200397 /* TODO handle that correctly */
398 /*if (ret == -ERESTARTSYS) {
399 continue;
400 }*/
401 if (ret < 0)
402 goto out;
403 if (ret == 0) {
404 ret = -EIO;
405 goto out;
406 }
407 pos += ret;
408 }
409
410 ret = 0;
411
412out:
413 set_fs(old_fs);
414 return ret;
415}
416
417static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
418{
419 struct btrfs_tlv_header *hdr;
420 int total_len = sizeof(*hdr) + len;
421 int left = sctx->send_max_size - sctx->send_size;
422
423 if (unlikely(left < total_len))
424 return -EOVERFLOW;
425
426 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
427 hdr->tlv_type = cpu_to_le16(attr);
428 hdr->tlv_len = cpu_to_le16(len);
429 memcpy(hdr + 1, data, len);
430 sctx->send_size += total_len;
431
432 return 0;
433}
434
David Sterba95bc79d2013-12-16 17:34:10 +0100435#define TLV_PUT_DEFINE_INT(bits) \
436 static int tlv_put_u##bits(struct send_ctx *sctx, \
437 u##bits attr, u##bits value) \
438 { \
439 __le##bits __tmp = cpu_to_le##bits(value); \
440 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
441 }
Alexander Block31db9f72012-07-25 23:19:24 +0200442
David Sterba95bc79d2013-12-16 17:34:10 +0100443TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200444
445static int tlv_put_string(struct send_ctx *sctx, u16 attr,
446 const char *str, int len)
447{
448 if (len == -1)
449 len = strlen(str);
450 return tlv_put(sctx, attr, str, len);
451}
452
453static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
454 const u8 *uuid)
455{
456 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
457}
458
Alexander Block31db9f72012-07-25 23:19:24 +0200459static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
460 struct extent_buffer *eb,
461 struct btrfs_timespec *ts)
462{
463 struct btrfs_timespec bts;
464 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
465 return tlv_put(sctx, attr, &bts, sizeof(bts));
466}
467
468
469#define TLV_PUT(sctx, attrtype, attrlen, data) \
470 do { \
471 ret = tlv_put(sctx, attrtype, attrlen, data); \
472 if (ret < 0) \
473 goto tlv_put_failure; \
474 } while (0)
475
476#define TLV_PUT_INT(sctx, attrtype, bits, value) \
477 do { \
478 ret = tlv_put_u##bits(sctx, attrtype, value); \
479 if (ret < 0) \
480 goto tlv_put_failure; \
481 } while (0)
482
483#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
484#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
485#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
486#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
487#define TLV_PUT_STRING(sctx, attrtype, str, len) \
488 do { \
489 ret = tlv_put_string(sctx, attrtype, str, len); \
490 if (ret < 0) \
491 goto tlv_put_failure; \
492 } while (0)
493#define TLV_PUT_PATH(sctx, attrtype, p) \
494 do { \
495 ret = tlv_put_string(sctx, attrtype, p->start, \
496 p->end - p->start); \
497 if (ret < 0) \
498 goto tlv_put_failure; \
499 } while(0)
500#define TLV_PUT_UUID(sctx, attrtype, uuid) \
501 do { \
502 ret = tlv_put_uuid(sctx, attrtype, uuid); \
503 if (ret < 0) \
504 goto tlv_put_failure; \
505 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200506#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
507 do { \
508 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
509 if (ret < 0) \
510 goto tlv_put_failure; \
511 } while (0)
512
513static int send_header(struct send_ctx *sctx)
514{
515 struct btrfs_stream_header hdr;
516
517 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
518 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
519
Anand Jain1bcea352012-09-14 00:04:21 -0600520 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
521 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200522}
523
524/*
525 * For each command/item we want to send to userspace, we call this function.
526 */
527static int begin_cmd(struct send_ctx *sctx, int cmd)
528{
529 struct btrfs_cmd_header *hdr;
530
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530531 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200532 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200533
534 BUG_ON(sctx->send_size);
535
536 sctx->send_size += sizeof(*hdr);
537 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
538 hdr->cmd = cpu_to_le16(cmd);
539
540 return 0;
541}
542
543static int send_cmd(struct send_ctx *sctx)
544{
545 int ret;
546 struct btrfs_cmd_header *hdr;
547 u32 crc;
548
549 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
550 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
551 hdr->crc = 0;
552
553 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
554 hdr->crc = cpu_to_le32(crc);
555
Anand Jain1bcea352012-09-14 00:04:21 -0600556 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
557 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200558
559 sctx->total_send_size += sctx->send_size;
560 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
561 sctx->send_size = 0;
562
563 return ret;
564}
565
566/*
567 * Sends a move instruction to user space
568 */
569static int send_rename(struct send_ctx *sctx,
570 struct fs_path *from, struct fs_path *to)
571{
572 int ret;
573
574verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
575
576 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
577 if (ret < 0)
578 goto out;
579
580 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
581 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
582
583 ret = send_cmd(sctx);
584
585tlv_put_failure:
586out:
587 return ret;
588}
589
590/*
591 * Sends a link instruction to user space
592 */
593static int send_link(struct send_ctx *sctx,
594 struct fs_path *path, struct fs_path *lnk)
595{
596 int ret;
597
598verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
599
600 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
601 if (ret < 0)
602 goto out;
603
604 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
605 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
606
607 ret = send_cmd(sctx);
608
609tlv_put_failure:
610out:
611 return ret;
612}
613
614/*
615 * Sends an unlink instruction to user space
616 */
617static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
618{
619 int ret;
620
621verbose_printk("btrfs: send_unlink %s\n", path->start);
622
623 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
624 if (ret < 0)
625 goto out;
626
627 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
628
629 ret = send_cmd(sctx);
630
631tlv_put_failure:
632out:
633 return ret;
634}
635
636/*
637 * Sends a rmdir instruction to user space
638 */
639static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
640{
641 int ret;
642
643verbose_printk("btrfs: send_rmdir %s\n", path->start);
644
645 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
646 if (ret < 0)
647 goto out;
648
649 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
650
651 ret = send_cmd(sctx);
652
653tlv_put_failure:
654out:
655 return ret;
656}
657
658/*
659 * Helper function to retrieve some fields from an inode item.
660 */
661static int get_inode_info(struct btrfs_root *root,
662 u64 ino, u64 *size, u64 *gen,
Alexander Block85a7b332012-07-26 23:39:10 +0200663 u64 *mode, u64 *uid, u64 *gid,
664 u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200665{
666 int ret;
667 struct btrfs_inode_item *ii;
668 struct btrfs_key key;
669 struct btrfs_path *path;
670
671 path = alloc_path_for_send();
672 if (!path)
673 return -ENOMEM;
674
675 key.objectid = ino;
676 key.type = BTRFS_INODE_ITEM_KEY;
677 key.offset = 0;
678 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
679 if (ret < 0)
680 goto out;
681 if (ret) {
682 ret = -ENOENT;
683 goto out;
684 }
685
686 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
687 struct btrfs_inode_item);
688 if (size)
689 *size = btrfs_inode_size(path->nodes[0], ii);
690 if (gen)
691 *gen = btrfs_inode_generation(path->nodes[0], ii);
692 if (mode)
693 *mode = btrfs_inode_mode(path->nodes[0], ii);
694 if (uid)
695 *uid = btrfs_inode_uid(path->nodes[0], ii);
696 if (gid)
697 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200698 if (rdev)
699 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200700
701out:
702 btrfs_free_path(path);
703 return ret;
704}
705
706typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
707 struct fs_path *p,
708 void *ctx);
709
710/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000711 * Helper function to iterate the entries in ONE btrfs_inode_ref or
712 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200713 * The iterate callback may return a non zero value to stop iteration. This can
714 * be a negative value for error codes or 1 to simply stop it.
715 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000716 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200717 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000718static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200719 struct btrfs_key *found_key, int resolve,
720 iterate_inode_ref_t iterate, void *ctx)
721{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000722 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200723 struct btrfs_item *item;
724 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000725 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200726 struct btrfs_path *tmp_path;
727 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000728 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200729 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000730 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200731 u32 name_len;
732 char *start;
733 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000734 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200735 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000736 u64 dir;
737 unsigned long name_off;
738 unsigned long elem_size;
739 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200740
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000741 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200742 if (!p)
743 return -ENOMEM;
744
745 tmp_path = alloc_path_for_send();
746 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000747 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200748 return -ENOMEM;
749 }
750
Alexander Block31db9f72012-07-25 23:19:24 +0200751
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000752 if (found_key->type == BTRFS_INODE_REF_KEY) {
753 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
754 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100755 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000756 total = btrfs_item_size(eb, item);
757 elem_size = sizeof(*iref);
758 } else {
759 ptr = btrfs_item_ptr_offset(eb, slot);
760 total = btrfs_item_size_nr(eb, slot);
761 elem_size = sizeof(*extref);
762 }
763
Alexander Block31db9f72012-07-25 23:19:24 +0200764 while (cur < total) {
765 fs_path_reset(p);
766
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000767 if (found_key->type == BTRFS_INODE_REF_KEY) {
768 iref = (struct btrfs_inode_ref *)(ptr + cur);
769 name_len = btrfs_inode_ref_name_len(eb, iref);
770 name_off = (unsigned long)(iref + 1);
771 index = btrfs_inode_ref_index(eb, iref);
772 dir = found_key->offset;
773 } else {
774 extref = (struct btrfs_inode_extref *)(ptr + cur);
775 name_len = btrfs_inode_extref_name_len(eb, extref);
776 name_off = (unsigned long)&extref->name;
777 index = btrfs_inode_extref_index(eb, extref);
778 dir = btrfs_inode_extref_parent(eb, extref);
779 }
780
Alexander Block31db9f72012-07-25 23:19:24 +0200781 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000782 start = btrfs_ref_to_path(root, tmp_path, name_len,
783 name_off, eb, dir,
784 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200785 if (IS_ERR(start)) {
786 ret = PTR_ERR(start);
787 goto out;
788 }
789 if (start < p->buf) {
790 /* overflow , try again with larger buffer */
791 ret = fs_path_ensure_buf(p,
792 p->buf_len + p->buf - start);
793 if (ret < 0)
794 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000795 start = btrfs_ref_to_path(root, tmp_path,
796 name_len, name_off,
797 eb, dir,
798 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200799 if (IS_ERR(start)) {
800 ret = PTR_ERR(start);
801 goto out;
802 }
803 BUG_ON(start < p->buf);
804 }
805 p->start = start;
806 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000807 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
808 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200809 if (ret < 0)
810 goto out;
811 }
812
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000813 cur += elem_size + name_len;
814 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200815 if (ret)
816 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200817 num++;
818 }
819
820out:
821 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000822 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200823 return ret;
824}
825
826typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
827 const char *name, int name_len,
828 const char *data, int data_len,
829 u8 type, void *ctx);
830
831/*
832 * Helper function to iterate the entries in ONE btrfs_dir_item.
833 * The iterate callback may return a non zero value to stop iteration. This can
834 * be a negative value for error codes or 1 to simply stop it.
835 *
836 * path must point to the dir item when called.
837 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000838static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200839 struct btrfs_key *found_key,
840 iterate_dir_item_t iterate, void *ctx)
841{
842 int ret = 0;
843 struct extent_buffer *eb;
844 struct btrfs_item *item;
845 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200846 struct btrfs_key di_key;
847 char *buf = NULL;
848 char *buf2 = NULL;
849 int buf_len;
850 int buf_virtual = 0;
851 u32 name_len;
852 u32 data_len;
853 u32 cur;
854 u32 len;
855 u32 total;
856 int slot;
857 int num;
858 u8 type;
859
860 buf_len = PAGE_SIZE;
861 buf = kmalloc(buf_len, GFP_NOFS);
862 if (!buf) {
863 ret = -ENOMEM;
864 goto out;
865 }
866
Alexander Block31db9f72012-07-25 23:19:24 +0200867 eb = path->nodes[0];
868 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +0100869 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +0200870 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
871 cur = 0;
872 len = 0;
873 total = btrfs_item_size(eb, item);
874
875 num = 0;
876 while (cur < total) {
877 name_len = btrfs_dir_name_len(eb, di);
878 data_len = btrfs_dir_data_len(eb, di);
879 type = btrfs_dir_type(eb, di);
880 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
881
882 if (name_len + data_len > buf_len) {
883 buf_len = PAGE_ALIGN(name_len + data_len);
884 if (buf_virtual) {
885 buf2 = vmalloc(buf_len);
886 if (!buf2) {
887 ret = -ENOMEM;
888 goto out;
889 }
890 vfree(buf);
891 } else {
892 buf2 = krealloc(buf, buf_len, GFP_NOFS);
893 if (!buf2) {
894 buf2 = vmalloc(buf_len);
895 if (!buf2) {
896 ret = -ENOMEM;
897 goto out;
898 }
899 kfree(buf);
900 buf_virtual = 1;
901 }
902 }
903
904 buf = buf2;
905 buf2 = NULL;
906 }
907
908 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
909 name_len + data_len);
910
911 len = sizeof(*di) + name_len + data_len;
912 di = (struct btrfs_dir_item *)((char *)di + len);
913 cur += len;
914
915 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
916 data_len, type, ctx);
917 if (ret < 0)
918 goto out;
919 if (ret) {
920 ret = 0;
921 goto out;
922 }
923
924 num++;
925 }
926
927out:
Alexander Block31db9f72012-07-25 23:19:24 +0200928 if (buf_virtual)
929 vfree(buf);
930 else
931 kfree(buf);
932 return ret;
933}
934
935static int __copy_first_ref(int num, u64 dir, int index,
936 struct fs_path *p, void *ctx)
937{
938 int ret;
939 struct fs_path *pt = ctx;
940
941 ret = fs_path_copy(pt, p);
942 if (ret < 0)
943 return ret;
944
945 /* we want the first only */
946 return 1;
947}
948
949/*
950 * Retrieve the first path of an inode. If an inode has more then one
951 * ref/hardlink, this is ignored.
952 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000953static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +0200954 u64 ino, struct fs_path *path)
955{
956 int ret;
957 struct btrfs_key key, found_key;
958 struct btrfs_path *p;
959
960 p = alloc_path_for_send();
961 if (!p)
962 return -ENOMEM;
963
964 fs_path_reset(path);
965
966 key.objectid = ino;
967 key.type = BTRFS_INODE_REF_KEY;
968 key.offset = 0;
969
970 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
971 if (ret < 0)
972 goto out;
973 if (ret) {
974 ret = 1;
975 goto out;
976 }
977 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
978 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000979 (found_key.type != BTRFS_INODE_REF_KEY &&
980 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +0200981 ret = -ENOENT;
982 goto out;
983 }
984
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000985 ret = iterate_inode_ref(root, p, &found_key, 1,
986 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +0200987 if (ret < 0)
988 goto out;
989 ret = 0;
990
991out:
992 btrfs_free_path(p);
993 return ret;
994}
995
996struct backref_ctx {
997 struct send_ctx *sctx;
998
999 /* number of total found references */
1000 u64 found;
1001
1002 /*
1003 * used for clones found in send_root. clones found behind cur_objectid
1004 * and cur_offset are not considered as allowed clones.
1005 */
1006 u64 cur_objectid;
1007 u64 cur_offset;
1008
1009 /* may be truncated in case it's the last extent in a file */
1010 u64 extent_len;
1011
1012 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001013 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001014};
1015
1016static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1017{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001018 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001019 struct clone_root *cr = (struct clone_root *)elt;
1020
1021 if (root < cr->root->objectid)
1022 return -1;
1023 if (root > cr->root->objectid)
1024 return 1;
1025 return 0;
1026}
1027
1028static int __clone_root_cmp_sort(const void *e1, const void *e2)
1029{
1030 struct clone_root *cr1 = (struct clone_root *)e1;
1031 struct clone_root *cr2 = (struct clone_root *)e2;
1032
1033 if (cr1->root->objectid < cr2->root->objectid)
1034 return -1;
1035 if (cr1->root->objectid > cr2->root->objectid)
1036 return 1;
1037 return 0;
1038}
1039
1040/*
1041 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001042 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001043 */
1044static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1045{
1046 struct backref_ctx *bctx = ctx_;
1047 struct clone_root *found;
1048 int ret;
1049 u64 i_size;
1050
1051 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001052 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001053 bctx->sctx->clone_roots_cnt,
1054 sizeof(struct clone_root),
1055 __clone_root_cmp_bsearch);
1056 if (!found)
1057 return 0;
1058
1059 if (found->root == bctx->sctx->send_root &&
1060 ino == bctx->cur_objectid &&
1061 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001062 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001063 }
1064
1065 /*
Alexander Block766702e2012-07-28 14:11:31 +02001066 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001067 * accept clones from these extents.
1068 */
Alexander Block85a7b332012-07-26 23:39:10 +02001069 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1070 NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001071 if (ret < 0)
1072 return ret;
1073
1074 if (offset + bctx->extent_len > i_size)
1075 return 0;
1076
1077 /*
1078 * Make sure we don't consider clones from send_root that are
1079 * behind the current inode/offset.
1080 */
1081 if (found->root == bctx->sctx->send_root) {
1082 /*
1083 * TODO for the moment we don't accept clones from the inode
1084 * that is currently send. We may change this when
1085 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1086 * file.
1087 */
1088 if (ino >= bctx->cur_objectid)
1089 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001090#if 0
1091 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001092 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001093 if (offset + bctx->extent_len > bctx->cur_offset)
1094 return 0;
1095#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001096 }
1097
1098 bctx->found++;
1099 found->found_refs++;
1100 if (ino < found->ino) {
1101 found->ino = ino;
1102 found->offset = offset;
1103 } else if (found->ino == ino) {
1104 /*
1105 * same extent found more then once in the same file.
1106 */
1107 if (found->offset > offset + bctx->extent_len)
1108 found->offset = offset;
1109 }
1110
1111 return 0;
1112}
1113
1114/*
Alexander Block766702e2012-07-28 14:11:31 +02001115 * Given an inode, offset and extent item, it finds a good clone for a clone
1116 * instruction. Returns -ENOENT when none could be found. The function makes
1117 * sure that the returned clone is usable at the point where sending is at the
1118 * moment. This means, that no clones are accepted which lie behind the current
1119 * inode+offset.
1120 *
Alexander Block31db9f72012-07-25 23:19:24 +02001121 * path must point to the extent item when called.
1122 */
1123static int find_extent_clone(struct send_ctx *sctx,
1124 struct btrfs_path *path,
1125 u64 ino, u64 data_offset,
1126 u64 ino_size,
1127 struct clone_root **found)
1128{
1129 int ret;
1130 int extent_type;
1131 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001132 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001133 u64 num_bytes;
1134 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001135 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001136 struct btrfs_file_extent_item *fi;
1137 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001138 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001139 struct clone_root *cur_clone_root;
1140 struct btrfs_key found_key;
1141 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001142 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001143 u32 i;
1144
1145 tmp_path = alloc_path_for_send();
1146 if (!tmp_path)
1147 return -ENOMEM;
1148
Alexander Block35075bb2012-07-28 12:44:34 +02001149 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1150 if (!backref_ctx) {
1151 ret = -ENOMEM;
1152 goto out;
1153 }
1154
Alexander Block31db9f72012-07-25 23:19:24 +02001155 if (data_offset >= ino_size) {
1156 /*
1157 * There may be extents that lie behind the file's size.
1158 * I at least had this in combination with snapshotting while
1159 * writing large files.
1160 */
1161 ret = 0;
1162 goto out;
1163 }
1164
1165 fi = btrfs_item_ptr(eb, path->slots[0],
1166 struct btrfs_file_extent_item);
1167 extent_type = btrfs_file_extent_type(eb, fi);
1168 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1169 ret = -ENOENT;
1170 goto out;
1171 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001172 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001173
1174 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001175 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1176 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001177 ret = -ENOENT;
1178 goto out;
1179 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001180 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001181
Liu Bo69917e42012-09-07 20:01:28 -06001182 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1183 &found_key, &flags);
Alexander Block31db9f72012-07-25 23:19:24 +02001184 btrfs_release_path(tmp_path);
1185
1186 if (ret < 0)
1187 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001188 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001189 ret = -EIO;
1190 goto out;
1191 }
1192
1193 /*
1194 * Setup the clone roots.
1195 */
1196 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1197 cur_clone_root = sctx->clone_roots + i;
1198 cur_clone_root->ino = (u64)-1;
1199 cur_clone_root->offset = 0;
1200 cur_clone_root->found_refs = 0;
1201 }
1202
Alexander Block35075bb2012-07-28 12:44:34 +02001203 backref_ctx->sctx = sctx;
1204 backref_ctx->found = 0;
1205 backref_ctx->cur_objectid = ino;
1206 backref_ctx->cur_offset = data_offset;
1207 backref_ctx->found_itself = 0;
1208 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001209
1210 /*
1211 * The last extent of a file may be too large due to page alignment.
1212 * We need to adjust extent_len in this case so that the checks in
1213 * __iterate_backrefs work.
1214 */
1215 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001216 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001217
1218 /*
1219 * Now collect all backrefs.
1220 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001221 if (compressed == BTRFS_COMPRESS_NONE)
1222 extent_item_pos = logical - found_key.objectid;
1223 else
1224 extent_item_pos = 0;
1225
Alexander Block31db9f72012-07-25 23:19:24 +02001226 extent_item_pos = logical - found_key.objectid;
1227 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1228 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001229 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001230
Alexander Block31db9f72012-07-25 23:19:24 +02001231 if (ret < 0)
1232 goto out;
1233
Alexander Block35075bb2012-07-28 12:44:34 +02001234 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001235 /* found a bug in backref code? */
1236 ret = -EIO;
Frank Holtonefe120a2013-12-20 11:37:06 -05001237 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
Alexander Block31db9f72012-07-25 23:19:24 +02001238 "send_root. inode=%llu, offset=%llu, "
Chris Mason74dd17f2012-08-07 16:25:13 -04001239 "disk_byte=%llu found extent=%llu\n",
1240 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001241 goto out;
1242 }
1243
1244verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1245 "ino=%llu, "
1246 "num_bytes=%llu, logical=%llu\n",
1247 data_offset, ino, num_bytes, logical);
1248
Alexander Block35075bb2012-07-28 12:44:34 +02001249 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001250 verbose_printk("btrfs: no clones found\n");
1251
1252 cur_clone_root = NULL;
1253 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1254 if (sctx->clone_roots[i].found_refs) {
1255 if (!cur_clone_root)
1256 cur_clone_root = sctx->clone_roots + i;
1257 else if (sctx->clone_roots[i].root == sctx->send_root)
1258 /* prefer clones from send_root over others */
1259 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001260 }
1261
1262 }
1263
1264 if (cur_clone_root) {
1265 *found = cur_clone_root;
1266 ret = 0;
1267 } else {
1268 ret = -ENOENT;
1269 }
1270
1271out:
1272 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001273 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001274 return ret;
1275}
1276
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001277static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001278 u64 ino,
1279 struct fs_path *dest)
1280{
1281 int ret;
1282 struct btrfs_path *path;
1283 struct btrfs_key key;
1284 struct btrfs_file_extent_item *ei;
1285 u8 type;
1286 u8 compression;
1287 unsigned long off;
1288 int len;
1289
1290 path = alloc_path_for_send();
1291 if (!path)
1292 return -ENOMEM;
1293
1294 key.objectid = ino;
1295 key.type = BTRFS_EXTENT_DATA_KEY;
1296 key.offset = 0;
1297 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1298 if (ret < 0)
1299 goto out;
1300 BUG_ON(ret);
1301
1302 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1303 struct btrfs_file_extent_item);
1304 type = btrfs_file_extent_type(path->nodes[0], ei);
1305 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1306 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1307 BUG_ON(compression);
1308
1309 off = btrfs_file_extent_inline_start(ei);
1310 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1311
1312 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001313
1314out:
1315 btrfs_free_path(path);
1316 return ret;
1317}
1318
1319/*
1320 * Helper function to generate a file name that is unique in the root of
1321 * send_root and parent_root. This is used to generate names for orphan inodes.
1322 */
1323static int gen_unique_name(struct send_ctx *sctx,
1324 u64 ino, u64 gen,
1325 struct fs_path *dest)
1326{
1327 int ret = 0;
1328 struct btrfs_path *path;
1329 struct btrfs_dir_item *di;
1330 char tmp[64];
1331 int len;
1332 u64 idx = 0;
1333
1334 path = alloc_path_for_send();
1335 if (!path)
1336 return -ENOMEM;
1337
1338 while (1) {
Filipe David Borba Mananaf74b86d2014-01-21 23:36:38 +00001339 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
Alexander Block31db9f72012-07-25 23:19:24 +02001340 ino, gen, idx);
1341 if (len >= sizeof(tmp)) {
1342 /* should really not happen */
1343 ret = -EOVERFLOW;
1344 goto out;
1345 }
1346
1347 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1348 path, BTRFS_FIRST_FREE_OBJECTID,
1349 tmp, strlen(tmp), 0);
1350 btrfs_release_path(path);
1351 if (IS_ERR(di)) {
1352 ret = PTR_ERR(di);
1353 goto out;
1354 }
1355 if (di) {
1356 /* not unique, try again */
1357 idx++;
1358 continue;
1359 }
1360
1361 if (!sctx->parent_root) {
1362 /* unique */
1363 ret = 0;
1364 break;
1365 }
1366
1367 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1368 path, BTRFS_FIRST_FREE_OBJECTID,
1369 tmp, strlen(tmp), 0);
1370 btrfs_release_path(path);
1371 if (IS_ERR(di)) {
1372 ret = PTR_ERR(di);
1373 goto out;
1374 }
1375 if (di) {
1376 /* not unique, try again */
1377 idx++;
1378 continue;
1379 }
1380 /* unique */
1381 break;
1382 }
1383
1384 ret = fs_path_add(dest, tmp, strlen(tmp));
1385
1386out:
1387 btrfs_free_path(path);
1388 return ret;
1389}
1390
1391enum inode_state {
1392 inode_state_no_change,
1393 inode_state_will_create,
1394 inode_state_did_create,
1395 inode_state_will_delete,
1396 inode_state_did_delete,
1397};
1398
1399static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1400{
1401 int ret;
1402 int left_ret;
1403 int right_ret;
1404 u64 left_gen;
1405 u64 right_gen;
1406
1407 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001408 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001409 if (ret < 0 && ret != -ENOENT)
1410 goto out;
1411 left_ret = ret;
1412
1413 if (!sctx->parent_root) {
1414 right_ret = -ENOENT;
1415 } else {
1416 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001417 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001418 if (ret < 0 && ret != -ENOENT)
1419 goto out;
1420 right_ret = ret;
1421 }
1422
1423 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001424 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001425 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001426 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001427 if (ino < sctx->send_progress)
1428 ret = inode_state_did_create;
1429 else
1430 ret = inode_state_will_create;
1431 } else if (right_gen == gen) {
1432 if (ino < sctx->send_progress)
1433 ret = inode_state_did_delete;
1434 else
1435 ret = inode_state_will_delete;
1436 } else {
1437 ret = -ENOENT;
1438 }
1439 } else if (!left_ret) {
1440 if (left_gen == gen) {
1441 if (ino < sctx->send_progress)
1442 ret = inode_state_did_create;
1443 else
1444 ret = inode_state_will_create;
1445 } else {
1446 ret = -ENOENT;
1447 }
1448 } else if (!right_ret) {
1449 if (right_gen == gen) {
1450 if (ino < sctx->send_progress)
1451 ret = inode_state_did_delete;
1452 else
1453 ret = inode_state_will_delete;
1454 } else {
1455 ret = -ENOENT;
1456 }
1457 } else {
1458 ret = -ENOENT;
1459 }
1460
1461out:
1462 return ret;
1463}
1464
1465static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1466{
1467 int ret;
1468
1469 ret = get_cur_inode_state(sctx, ino, gen);
1470 if (ret < 0)
1471 goto out;
1472
1473 if (ret == inode_state_no_change ||
1474 ret == inode_state_did_create ||
1475 ret == inode_state_will_delete)
1476 ret = 1;
1477 else
1478 ret = 0;
1479
1480out:
1481 return ret;
1482}
1483
1484/*
1485 * Helper function to lookup a dir item in a dir.
1486 */
1487static int lookup_dir_item_inode(struct btrfs_root *root,
1488 u64 dir, const char *name, int name_len,
1489 u64 *found_inode,
1490 u8 *found_type)
1491{
1492 int ret = 0;
1493 struct btrfs_dir_item *di;
1494 struct btrfs_key key;
1495 struct btrfs_path *path;
1496
1497 path = alloc_path_for_send();
1498 if (!path)
1499 return -ENOMEM;
1500
1501 di = btrfs_lookup_dir_item(NULL, root, path,
1502 dir, name, name_len, 0);
1503 if (!di) {
1504 ret = -ENOENT;
1505 goto out;
1506 }
1507 if (IS_ERR(di)) {
1508 ret = PTR_ERR(di);
1509 goto out;
1510 }
1511 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1512 *found_inode = key.objectid;
1513 *found_type = btrfs_dir_type(path->nodes[0], di);
1514
1515out:
1516 btrfs_free_path(path);
1517 return ret;
1518}
1519
Alexander Block766702e2012-07-28 14:11:31 +02001520/*
1521 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1522 * generation of the parent dir and the name of the dir entry.
1523 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001524static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001525 u64 *dir, u64 *dir_gen, struct fs_path *name)
1526{
1527 int ret;
1528 struct btrfs_key key;
1529 struct btrfs_key found_key;
1530 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001531 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001532 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001533
1534 path = alloc_path_for_send();
1535 if (!path)
1536 return -ENOMEM;
1537
1538 key.objectid = ino;
1539 key.type = BTRFS_INODE_REF_KEY;
1540 key.offset = 0;
1541
1542 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1543 if (ret < 0)
1544 goto out;
1545 if (!ret)
1546 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1547 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001548 if (ret || found_key.objectid != ino ||
1549 (found_key.type != BTRFS_INODE_REF_KEY &&
1550 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001551 ret = -ENOENT;
1552 goto out;
1553 }
1554
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001555 if (key.type == BTRFS_INODE_REF_KEY) {
1556 struct btrfs_inode_ref *iref;
1557 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1558 struct btrfs_inode_ref);
1559 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1560 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1561 (unsigned long)(iref + 1),
1562 len);
1563 parent_dir = found_key.offset;
1564 } else {
1565 struct btrfs_inode_extref *extref;
1566 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1567 struct btrfs_inode_extref);
1568 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1569 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1570 (unsigned long)&extref->name, len);
1571 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1572 }
Alexander Block31db9f72012-07-25 23:19:24 +02001573 if (ret < 0)
1574 goto out;
1575 btrfs_release_path(path);
1576
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001577 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001578 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001579 if (ret < 0)
1580 goto out;
1581
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001582 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001583
1584out:
1585 btrfs_free_path(path);
1586 return ret;
1587}
1588
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001589static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001590 u64 ino, u64 dir,
1591 const char *name, int name_len)
1592{
1593 int ret;
1594 struct fs_path *tmp_name;
1595 u64 tmp_dir;
1596 u64 tmp_dir_gen;
1597
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001598 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001599 if (!tmp_name)
1600 return -ENOMEM;
1601
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001602 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001603 if (ret < 0)
1604 goto out;
1605
Alexander Blockb9291af2012-07-28 11:07:18 +02001606 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001607 ret = 0;
1608 goto out;
1609 }
1610
Alexander Blocke938c8a2012-07-28 16:33:49 +02001611 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001612
1613out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001614 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001615 return ret;
1616}
1617
Alexander Block766702e2012-07-28 14:11:31 +02001618/*
1619 * Used by process_recorded_refs to determine if a new ref would overwrite an
1620 * already existing ref. In case it detects an overwrite, it returns the
1621 * inode/gen in who_ino/who_gen.
1622 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1623 * to make sure later references to the overwritten inode are possible.
1624 * Orphanizing is however only required for the first ref of an inode.
1625 * process_recorded_refs does an additional is_first_ref check to see if
1626 * orphanizing is really required.
1627 */
Alexander Block31db9f72012-07-25 23:19:24 +02001628static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1629 const char *name, int name_len,
1630 u64 *who_ino, u64 *who_gen)
1631{
1632 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001633 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001634 u64 other_inode = 0;
1635 u8 other_type = 0;
1636
1637 if (!sctx->parent_root)
1638 goto out;
1639
1640 ret = is_inode_existent(sctx, dir, dir_gen);
1641 if (ret <= 0)
1642 goto out;
1643
Josef Bacikebdad912013-08-06 16:47:48 -04001644 /*
1645 * If we have a parent root we need to verify that the parent dir was
1646 * not delted and then re-created, if it was then we have no overwrite
1647 * and we can just unlink this entry.
1648 */
1649 if (sctx->parent_root) {
1650 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1651 NULL, NULL, NULL);
1652 if (ret < 0 && ret != -ENOENT)
1653 goto out;
1654 if (ret) {
1655 ret = 0;
1656 goto out;
1657 }
1658 if (gen != dir_gen)
1659 goto out;
1660 }
1661
Alexander Block31db9f72012-07-25 23:19:24 +02001662 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1663 &other_inode, &other_type);
1664 if (ret < 0 && ret != -ENOENT)
1665 goto out;
1666 if (ret) {
1667 ret = 0;
1668 goto out;
1669 }
1670
Alexander Block766702e2012-07-28 14:11:31 +02001671 /*
1672 * Check if the overwritten ref was already processed. If yes, the ref
1673 * was already unlinked/moved, so we can safely assume that we will not
1674 * overwrite anything at this point in time.
1675 */
Alexander Block31db9f72012-07-25 23:19:24 +02001676 if (other_inode > sctx->send_progress) {
1677 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001678 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001679 if (ret < 0)
1680 goto out;
1681
1682 ret = 1;
1683 *who_ino = other_inode;
1684 } else {
1685 ret = 0;
1686 }
1687
1688out:
1689 return ret;
1690}
1691
Alexander Block766702e2012-07-28 14:11:31 +02001692/*
1693 * Checks if the ref was overwritten by an already processed inode. This is
1694 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1695 * thus the orphan name needs be used.
1696 * process_recorded_refs also uses it to avoid unlinking of refs that were
1697 * overwritten.
1698 */
Alexander Block31db9f72012-07-25 23:19:24 +02001699static int did_overwrite_ref(struct send_ctx *sctx,
1700 u64 dir, u64 dir_gen,
1701 u64 ino, u64 ino_gen,
1702 const char *name, int name_len)
1703{
1704 int ret = 0;
1705 u64 gen;
1706 u64 ow_inode;
1707 u8 other_type;
1708
1709 if (!sctx->parent_root)
1710 goto out;
1711
1712 ret = is_inode_existent(sctx, dir, dir_gen);
1713 if (ret <= 0)
1714 goto out;
1715
1716 /* check if the ref was overwritten by another ref */
1717 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1718 &ow_inode, &other_type);
1719 if (ret < 0 && ret != -ENOENT)
1720 goto out;
1721 if (ret) {
1722 /* was never and will never be overwritten */
1723 ret = 0;
1724 goto out;
1725 }
1726
1727 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001728 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001729 if (ret < 0)
1730 goto out;
1731
1732 if (ow_inode == ino && gen == ino_gen) {
1733 ret = 0;
1734 goto out;
1735 }
1736
1737 /* we know that it is or will be overwritten. check this now */
1738 if (ow_inode < sctx->send_progress)
1739 ret = 1;
1740 else
1741 ret = 0;
1742
1743out:
1744 return ret;
1745}
1746
Alexander Block766702e2012-07-28 14:11:31 +02001747/*
1748 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1749 * that got overwritten. This is used by process_recorded_refs to determine
1750 * if it has to use the path as returned by get_cur_path or the orphan name.
1751 */
Alexander Block31db9f72012-07-25 23:19:24 +02001752static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1753{
1754 int ret = 0;
1755 struct fs_path *name = NULL;
1756 u64 dir;
1757 u64 dir_gen;
1758
1759 if (!sctx->parent_root)
1760 goto out;
1761
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001762 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001763 if (!name)
1764 return -ENOMEM;
1765
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001766 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001767 if (ret < 0)
1768 goto out;
1769
1770 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1771 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001772
1773out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001774 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001775 return ret;
1776}
1777
Alexander Block766702e2012-07-28 14:11:31 +02001778/*
1779 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1780 * so we need to do some special handling in case we have clashes. This function
1781 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001782 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001783 */
Alexander Block31db9f72012-07-25 23:19:24 +02001784static int name_cache_insert(struct send_ctx *sctx,
1785 struct name_cache_entry *nce)
1786{
1787 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001788 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001789
Alexander Block7e0926f2012-07-28 14:20:58 +02001790 nce_head = radix_tree_lookup(&sctx->name_cache,
1791 (unsigned long)nce->ino);
1792 if (!nce_head) {
1793 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001794 if (!nce_head) {
1795 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001796 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001797 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001798 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001799
Alexander Block7e0926f2012-07-28 14:20:58 +02001800 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001801 if (ret < 0) {
1802 kfree(nce_head);
1803 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001804 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001805 }
Alexander Block31db9f72012-07-25 23:19:24 +02001806 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001807 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001808 list_add_tail(&nce->list, &sctx->name_cache_list);
1809 sctx->name_cache_size++;
1810
1811 return ret;
1812}
1813
1814static void name_cache_delete(struct send_ctx *sctx,
1815 struct name_cache_entry *nce)
1816{
Alexander Block7e0926f2012-07-28 14:20:58 +02001817 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001818
Alexander Block7e0926f2012-07-28 14:20:58 +02001819 nce_head = radix_tree_lookup(&sctx->name_cache,
1820 (unsigned long)nce->ino);
1821 BUG_ON(!nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001822
Alexander Block7e0926f2012-07-28 14:20:58 +02001823 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001824 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001825 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001826
1827 if (list_empty(nce_head)) {
1828 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1829 kfree(nce_head);
1830 }
Alexander Block31db9f72012-07-25 23:19:24 +02001831}
1832
1833static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1834 u64 ino, u64 gen)
1835{
Alexander Block7e0926f2012-07-28 14:20:58 +02001836 struct list_head *nce_head;
1837 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001838
Alexander Block7e0926f2012-07-28 14:20:58 +02001839 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1840 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001841 return NULL;
1842
Alexander Block7e0926f2012-07-28 14:20:58 +02001843 list_for_each_entry(cur, nce_head, radix_list) {
1844 if (cur->ino == ino && cur->gen == gen)
1845 return cur;
1846 }
Alexander Block31db9f72012-07-25 23:19:24 +02001847 return NULL;
1848}
1849
Alexander Block766702e2012-07-28 14:11:31 +02001850/*
1851 * Removes the entry from the list and adds it back to the end. This marks the
1852 * entry as recently used so that name_cache_clean_unused does not remove it.
1853 */
Alexander Block31db9f72012-07-25 23:19:24 +02001854static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1855{
1856 list_del(&nce->list);
1857 list_add_tail(&nce->list, &sctx->name_cache_list);
1858}
1859
Alexander Block766702e2012-07-28 14:11:31 +02001860/*
1861 * Remove some entries from the beginning of name_cache_list.
1862 */
Alexander Block31db9f72012-07-25 23:19:24 +02001863static void name_cache_clean_unused(struct send_ctx *sctx)
1864{
1865 struct name_cache_entry *nce;
1866
1867 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1868 return;
1869
1870 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1871 nce = list_entry(sctx->name_cache_list.next,
1872 struct name_cache_entry, list);
1873 name_cache_delete(sctx, nce);
1874 kfree(nce);
1875 }
1876}
1877
1878static void name_cache_free(struct send_ctx *sctx)
1879{
1880 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02001881
Alexander Blocke938c8a2012-07-28 16:33:49 +02001882 while (!list_empty(&sctx->name_cache_list)) {
1883 nce = list_entry(sctx->name_cache_list.next,
1884 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02001885 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02001886 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001887 }
1888}
1889
Alexander Block766702e2012-07-28 14:11:31 +02001890/*
1891 * Used by get_cur_path for each ref up to the root.
1892 * Returns 0 if it succeeded.
1893 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1894 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1895 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1896 * Returns <0 in case of error.
1897 */
Alexander Block31db9f72012-07-25 23:19:24 +02001898static int __get_cur_name_and_parent(struct send_ctx *sctx,
1899 u64 ino, u64 gen,
1900 u64 *parent_ino,
1901 u64 *parent_gen,
1902 struct fs_path *dest)
1903{
1904 int ret;
1905 int nce_ret;
1906 struct btrfs_path *path = NULL;
1907 struct name_cache_entry *nce = NULL;
1908
Alexander Block766702e2012-07-28 14:11:31 +02001909 /*
1910 * First check if we already did a call to this function with the same
1911 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1912 * return the cached result.
1913 */
Alexander Block31db9f72012-07-25 23:19:24 +02001914 nce = name_cache_search(sctx, ino, gen);
1915 if (nce) {
1916 if (ino < sctx->send_progress && nce->need_later_update) {
1917 name_cache_delete(sctx, nce);
1918 kfree(nce);
1919 nce = NULL;
1920 } else {
1921 name_cache_used(sctx, nce);
1922 *parent_ino = nce->parent_ino;
1923 *parent_gen = nce->parent_gen;
1924 ret = fs_path_add(dest, nce->name, nce->name_len);
1925 if (ret < 0)
1926 goto out;
1927 ret = nce->ret;
1928 goto out;
1929 }
1930 }
1931
1932 path = alloc_path_for_send();
1933 if (!path)
1934 return -ENOMEM;
1935
Alexander Block766702e2012-07-28 14:11:31 +02001936 /*
1937 * If the inode is not existent yet, add the orphan name and return 1.
1938 * This should only happen for the parent dir that we determine in
1939 * __record_new_ref
1940 */
Alexander Block31db9f72012-07-25 23:19:24 +02001941 ret = is_inode_existent(sctx, ino, gen);
1942 if (ret < 0)
1943 goto out;
1944
1945 if (!ret) {
1946 ret = gen_unique_name(sctx, ino, gen, dest);
1947 if (ret < 0)
1948 goto out;
1949 ret = 1;
1950 goto out_cache;
1951 }
1952
Alexander Block766702e2012-07-28 14:11:31 +02001953 /*
1954 * Depending on whether the inode was already processed or not, use
1955 * send_root or parent_root for ref lookup.
1956 */
Alexander Block31db9f72012-07-25 23:19:24 +02001957 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001958 ret = get_first_ref(sctx->send_root, ino,
1959 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02001960 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001961 ret = get_first_ref(sctx->parent_root, ino,
1962 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02001963 if (ret < 0)
1964 goto out;
1965
Alexander Block766702e2012-07-28 14:11:31 +02001966 /*
1967 * Check if the ref was overwritten by an inode's ref that was processed
1968 * earlier. If yes, treat as orphan and return 1.
1969 */
Alexander Block31db9f72012-07-25 23:19:24 +02001970 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1971 dest->start, dest->end - dest->start);
1972 if (ret < 0)
1973 goto out;
1974 if (ret) {
1975 fs_path_reset(dest);
1976 ret = gen_unique_name(sctx, ino, gen, dest);
1977 if (ret < 0)
1978 goto out;
1979 ret = 1;
1980 }
1981
1982out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02001983 /*
1984 * Store the result of the lookup in the name cache.
1985 */
Alexander Block31db9f72012-07-25 23:19:24 +02001986 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
1987 if (!nce) {
1988 ret = -ENOMEM;
1989 goto out;
1990 }
1991
1992 nce->ino = ino;
1993 nce->gen = gen;
1994 nce->parent_ino = *parent_ino;
1995 nce->parent_gen = *parent_gen;
1996 nce->name_len = fs_path_len(dest);
1997 nce->ret = ret;
1998 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02001999
2000 if (ino < sctx->send_progress)
2001 nce->need_later_update = 0;
2002 else
2003 nce->need_later_update = 1;
2004
2005 nce_ret = name_cache_insert(sctx, nce);
2006 if (nce_ret < 0)
2007 ret = nce_ret;
2008 name_cache_clean_unused(sctx);
2009
2010out:
2011 btrfs_free_path(path);
2012 return ret;
2013}
2014
2015/*
2016 * Magic happens here. This function returns the first ref to an inode as it
2017 * would look like while receiving the stream at this point in time.
2018 * We walk the path up to the root. For every inode in between, we check if it
2019 * was already processed/sent. If yes, we continue with the parent as found
2020 * in send_root. If not, we continue with the parent as found in parent_root.
2021 * If we encounter an inode that was deleted at this point in time, we use the
2022 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2023 * that were not created yet and overwritten inodes/refs.
2024 *
2025 * When do we have have orphan inodes:
2026 * 1. When an inode is freshly created and thus no valid refs are available yet
2027 * 2. When a directory lost all it's refs (deleted) but still has dir items
2028 * inside which were not processed yet (pending for move/delete). If anyone
2029 * tried to get the path to the dir items, it would get a path inside that
2030 * orphan directory.
2031 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2032 * of an unprocessed inode. If in that case the first ref would be
2033 * overwritten, the overwritten inode gets "orphanized". Later when we
2034 * process this overwritten inode, it is restored at a new place by moving
2035 * the orphan inode.
2036 *
2037 * sctx->send_progress tells this function at which point in time receiving
2038 * would be.
2039 */
2040static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2041 struct fs_path *dest)
2042{
2043 int ret = 0;
2044 struct fs_path *name = NULL;
2045 u64 parent_inode = 0;
2046 u64 parent_gen = 0;
2047 int stop = 0;
2048
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002049 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002050 if (!name) {
2051 ret = -ENOMEM;
2052 goto out;
2053 }
2054
2055 dest->reversed = 1;
2056 fs_path_reset(dest);
2057
2058 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2059 fs_path_reset(name);
2060
2061 ret = __get_cur_name_and_parent(sctx, ino, gen,
2062 &parent_inode, &parent_gen, name);
2063 if (ret < 0)
2064 goto out;
2065 if (ret)
2066 stop = 1;
2067
2068 ret = fs_path_add_path(dest, name);
2069 if (ret < 0)
2070 goto out;
2071
2072 ino = parent_inode;
2073 gen = parent_gen;
2074 }
2075
2076out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002077 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002078 if (!ret)
2079 fs_path_unreverse(dest);
2080 return ret;
2081}
2082
2083/*
Alexander Block31db9f72012-07-25 23:19:24 +02002084 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2085 */
2086static int send_subvol_begin(struct send_ctx *sctx)
2087{
2088 int ret;
2089 struct btrfs_root *send_root = sctx->send_root;
2090 struct btrfs_root *parent_root = sctx->parent_root;
2091 struct btrfs_path *path;
2092 struct btrfs_key key;
2093 struct btrfs_root_ref *ref;
2094 struct extent_buffer *leaf;
2095 char *name = NULL;
2096 int namelen;
2097
Wang Shilongffcfaf82014-01-15 00:26:43 +08002098 path = btrfs_alloc_path();
Alexander Block31db9f72012-07-25 23:19:24 +02002099 if (!path)
2100 return -ENOMEM;
2101
2102 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2103 if (!name) {
2104 btrfs_free_path(path);
2105 return -ENOMEM;
2106 }
2107
2108 key.objectid = send_root->objectid;
2109 key.type = BTRFS_ROOT_BACKREF_KEY;
2110 key.offset = 0;
2111
2112 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2113 &key, path, 1, 0);
2114 if (ret < 0)
2115 goto out;
2116 if (ret) {
2117 ret = -ENOENT;
2118 goto out;
2119 }
2120
2121 leaf = path->nodes[0];
2122 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2123 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2124 key.objectid != send_root->objectid) {
2125 ret = -ENOENT;
2126 goto out;
2127 }
2128 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2129 namelen = btrfs_root_ref_name_len(leaf, ref);
2130 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2131 btrfs_release_path(path);
2132
Alexander Block31db9f72012-07-25 23:19:24 +02002133 if (parent_root) {
2134 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2135 if (ret < 0)
2136 goto out;
2137 } else {
2138 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2139 if (ret < 0)
2140 goto out;
2141 }
2142
2143 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2144 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2145 sctx->send_root->root_item.uuid);
2146 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002147 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002148 if (parent_root) {
2149 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2150 sctx->parent_root->root_item.uuid);
2151 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002152 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002153 }
2154
2155 ret = send_cmd(sctx);
2156
2157tlv_put_failure:
2158out:
2159 btrfs_free_path(path);
2160 kfree(name);
2161 return ret;
2162}
2163
2164static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2165{
2166 int ret = 0;
2167 struct fs_path *p;
2168
2169verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2170
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002171 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002172 if (!p)
2173 return -ENOMEM;
2174
2175 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2176 if (ret < 0)
2177 goto out;
2178
2179 ret = get_cur_path(sctx, ino, gen, p);
2180 if (ret < 0)
2181 goto out;
2182 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2183 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2184
2185 ret = send_cmd(sctx);
2186
2187tlv_put_failure:
2188out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002189 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002190 return ret;
2191}
2192
2193static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2194{
2195 int ret = 0;
2196 struct fs_path *p;
2197
2198verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2199
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002200 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002201 if (!p)
2202 return -ENOMEM;
2203
2204 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2205 if (ret < 0)
2206 goto out;
2207
2208 ret = get_cur_path(sctx, ino, gen, p);
2209 if (ret < 0)
2210 goto out;
2211 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2212 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2213
2214 ret = send_cmd(sctx);
2215
2216tlv_put_failure:
2217out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002218 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002219 return ret;
2220}
2221
2222static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2223{
2224 int ret = 0;
2225 struct fs_path *p;
2226
2227verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2228
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002229 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002230 if (!p)
2231 return -ENOMEM;
2232
2233 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2234 if (ret < 0)
2235 goto out;
2236
2237 ret = get_cur_path(sctx, ino, gen, p);
2238 if (ret < 0)
2239 goto out;
2240 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2241 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2242 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2243
2244 ret = send_cmd(sctx);
2245
2246tlv_put_failure:
2247out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002248 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002249 return ret;
2250}
2251
2252static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2253{
2254 int ret = 0;
2255 struct fs_path *p = NULL;
2256 struct btrfs_inode_item *ii;
2257 struct btrfs_path *path = NULL;
2258 struct extent_buffer *eb;
2259 struct btrfs_key key;
2260 int slot;
2261
2262verbose_printk("btrfs: send_utimes %llu\n", ino);
2263
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002264 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002265 if (!p)
2266 return -ENOMEM;
2267
2268 path = alloc_path_for_send();
2269 if (!path) {
2270 ret = -ENOMEM;
2271 goto out;
2272 }
2273
2274 key.objectid = ino;
2275 key.type = BTRFS_INODE_ITEM_KEY;
2276 key.offset = 0;
2277 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2278 if (ret < 0)
2279 goto out;
2280
2281 eb = path->nodes[0];
2282 slot = path->slots[0];
2283 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2284
2285 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2286 if (ret < 0)
2287 goto out;
2288
2289 ret = get_cur_path(sctx, ino, gen, p);
2290 if (ret < 0)
2291 goto out;
2292 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2293 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2294 btrfs_inode_atime(ii));
2295 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2296 btrfs_inode_mtime(ii));
2297 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2298 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002299 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002300
2301 ret = send_cmd(sctx);
2302
2303tlv_put_failure:
2304out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002305 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002306 btrfs_free_path(path);
2307 return ret;
2308}
2309
2310/*
2311 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2312 * a valid path yet because we did not process the refs yet. So, the inode
2313 * is created as orphan.
2314 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002315static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002316{
2317 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002318 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002319 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002320 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002321 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002322 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002323
Alexander Block1f4692d2012-07-28 10:42:24 +02002324verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002325
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002326 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002327 if (!p)
2328 return -ENOMEM;
2329
Alexander Block1f4692d2012-07-28 10:42:24 +02002330 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2331 NULL, &rdev);
2332 if (ret < 0)
2333 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002334
Alexander Blocke938c8a2012-07-28 16:33:49 +02002335 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002336 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002337 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002338 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002339 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002340 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002341 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002342 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002343 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002344 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002345 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002346 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002347 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002348 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2349 (int)(mode & S_IFMT));
2350 ret = -ENOTSUPP;
2351 goto out;
2352 }
2353
2354 ret = begin_cmd(sctx, cmd);
2355 if (ret < 0)
2356 goto out;
2357
Alexander Block1f4692d2012-07-28 10:42:24 +02002358 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002359 if (ret < 0)
2360 goto out;
2361
2362 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002363 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002364
2365 if (S_ISLNK(mode)) {
2366 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002367 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002368 if (ret < 0)
2369 goto out;
2370 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2371 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2372 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002373 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2374 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002375 }
2376
2377 ret = send_cmd(sctx);
2378 if (ret < 0)
2379 goto out;
2380
2381
2382tlv_put_failure:
2383out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002384 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002385 return ret;
2386}
2387
Alexander Block1f4692d2012-07-28 10:42:24 +02002388/*
2389 * We need some special handling for inodes that get processed before the parent
2390 * directory got created. See process_recorded_refs for details.
2391 * This function does the check if we already created the dir out of order.
2392 */
2393static int did_create_dir(struct send_ctx *sctx, u64 dir)
2394{
2395 int ret = 0;
2396 struct btrfs_path *path = NULL;
2397 struct btrfs_key key;
2398 struct btrfs_key found_key;
2399 struct btrfs_key di_key;
2400 struct extent_buffer *eb;
2401 struct btrfs_dir_item *di;
2402 int slot;
2403
2404 path = alloc_path_for_send();
2405 if (!path) {
2406 ret = -ENOMEM;
2407 goto out;
2408 }
2409
2410 key.objectid = dir;
2411 key.type = BTRFS_DIR_INDEX_KEY;
2412 key.offset = 0;
2413 while (1) {
2414 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2415 1, 0);
2416 if (ret < 0)
2417 goto out;
2418 if (!ret) {
2419 eb = path->nodes[0];
2420 slot = path->slots[0];
2421 btrfs_item_key_to_cpu(eb, &found_key, slot);
2422 }
2423 if (ret || found_key.objectid != key.objectid ||
2424 found_key.type != key.type) {
2425 ret = 0;
2426 goto out;
2427 }
2428
2429 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2430 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2431
Josef Bacika0525412013-08-12 10:56:14 -04002432 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2433 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002434 ret = 1;
2435 goto out;
2436 }
2437
2438 key.offset = found_key.offset + 1;
2439 btrfs_release_path(path);
2440 }
2441
2442out:
2443 btrfs_free_path(path);
2444 return ret;
2445}
2446
2447/*
2448 * Only creates the inode if it is:
2449 * 1. Not a directory
2450 * 2. Or a directory which was not created already due to out of order
2451 * directories. See did_create_dir and process_recorded_refs for details.
2452 */
2453static int send_create_inode_if_needed(struct send_ctx *sctx)
2454{
2455 int ret;
2456
2457 if (S_ISDIR(sctx->cur_inode_mode)) {
2458 ret = did_create_dir(sctx, sctx->cur_ino);
2459 if (ret < 0)
2460 goto out;
2461 if (ret) {
2462 ret = 0;
2463 goto out;
2464 }
2465 }
2466
2467 ret = send_create_inode(sctx, sctx->cur_ino);
2468 if (ret < 0)
2469 goto out;
2470
2471out:
2472 return ret;
2473}
2474
Alexander Block31db9f72012-07-25 23:19:24 +02002475struct recorded_ref {
2476 struct list_head list;
2477 char *dir_path;
2478 char *name;
2479 struct fs_path *full_path;
2480 u64 dir;
2481 u64 dir_gen;
2482 int dir_path_len;
2483 int name_len;
2484};
2485
2486/*
2487 * We need to process new refs before deleted refs, but compare_tree gives us
2488 * everything mixed. So we first record all refs and later process them.
2489 * This function is a helper to record one ref.
2490 */
2491static int record_ref(struct list_head *head, u64 dir,
2492 u64 dir_gen, struct fs_path *path)
2493{
2494 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002495
2496 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2497 if (!ref)
2498 return -ENOMEM;
2499
2500 ref->dir = dir;
2501 ref->dir_gen = dir_gen;
2502 ref->full_path = path;
2503
Andy Shevchenkoed848852013-08-21 10:32:13 +03002504 ref->name = (char *)kbasename(ref->full_path->start);
2505 ref->name_len = ref->full_path->end - ref->name;
2506 ref->dir_path = ref->full_path->start;
2507 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002508 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002509 else
Alexander Block31db9f72012-07-25 23:19:24 +02002510 ref->dir_path_len = ref->full_path->end -
2511 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002512
2513 list_add_tail(&ref->list, head);
2514 return 0;
2515}
2516
Josef Bacikba5e8f22013-08-16 16:52:55 -04002517static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2518{
2519 struct recorded_ref *new;
2520
2521 new = kmalloc(sizeof(*ref), GFP_NOFS);
2522 if (!new)
2523 return -ENOMEM;
2524
2525 new->dir = ref->dir;
2526 new->dir_gen = ref->dir_gen;
2527 new->full_path = NULL;
2528 INIT_LIST_HEAD(&new->list);
2529 list_add_tail(&new->list, list);
2530 return 0;
2531}
2532
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002533static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002534{
2535 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002536
Alexander Blocke938c8a2012-07-28 16:33:49 +02002537 while (!list_empty(head)) {
2538 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002539 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002540 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002541 kfree(cur);
2542 }
Alexander Block31db9f72012-07-25 23:19:24 +02002543}
2544
2545static void free_recorded_refs(struct send_ctx *sctx)
2546{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002547 __free_recorded_refs(&sctx->new_refs);
2548 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002549}
2550
2551/*
Alexander Block766702e2012-07-28 14:11:31 +02002552 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002553 * ref of an unprocessed inode gets overwritten and for all non empty
2554 * directories.
2555 */
2556static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2557 struct fs_path *path)
2558{
2559 int ret;
2560 struct fs_path *orphan;
2561
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002562 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002563 if (!orphan)
2564 return -ENOMEM;
2565
2566 ret = gen_unique_name(sctx, ino, gen, orphan);
2567 if (ret < 0)
2568 goto out;
2569
2570 ret = send_rename(sctx, path, orphan);
2571
2572out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002573 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002574 return ret;
2575}
2576
2577/*
2578 * Returns 1 if a directory can be removed at this point in time.
2579 * We check this by iterating all dir items and checking if the inode behind
2580 * the dir item was already processed.
2581 */
2582static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2583{
2584 int ret = 0;
2585 struct btrfs_root *root = sctx->parent_root;
2586 struct btrfs_path *path;
2587 struct btrfs_key key;
2588 struct btrfs_key found_key;
2589 struct btrfs_key loc;
2590 struct btrfs_dir_item *di;
2591
Alexander Block6d85ed02012-08-01 14:48:59 +02002592 /*
2593 * Don't try to rmdir the top/root subvolume dir.
2594 */
2595 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2596 return 0;
2597
Alexander Block31db9f72012-07-25 23:19:24 +02002598 path = alloc_path_for_send();
2599 if (!path)
2600 return -ENOMEM;
2601
2602 key.objectid = dir;
2603 key.type = BTRFS_DIR_INDEX_KEY;
2604 key.offset = 0;
2605
2606 while (1) {
2607 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2608 if (ret < 0)
2609 goto out;
2610 if (!ret) {
2611 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2612 path->slots[0]);
2613 }
2614 if (ret || found_key.objectid != key.objectid ||
2615 found_key.type != key.type) {
2616 break;
2617 }
2618
2619 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2620 struct btrfs_dir_item);
2621 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2622
2623 if (loc.objectid > send_progress) {
2624 ret = 0;
2625 goto out;
2626 }
2627
2628 btrfs_release_path(path);
2629 key.offset = found_key.offset + 1;
2630 }
2631
2632 ret = 1;
2633
2634out:
2635 btrfs_free_path(path);
2636 return ret;
2637}
2638
Alexander Block31db9f72012-07-25 23:19:24 +02002639/*
2640 * This does all the move/link/unlink/rmdir magic.
2641 */
2642static int process_recorded_refs(struct send_ctx *sctx)
2643{
2644 int ret = 0;
2645 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02002646 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04002647 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02002648 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04002649 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002650 u64 ow_gen;
2651 int did_overwrite = 0;
2652 int is_orphan = 0;
2653
2654verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2655
Alexander Block6d85ed02012-08-01 14:48:59 +02002656 /*
2657 * This should never happen as the root dir always has the same ref
2658 * which is always '..'
2659 */
2660 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002661 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02002662
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002663 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002664 if (!valid_path) {
2665 ret = -ENOMEM;
2666 goto out;
2667 }
2668
Alexander Block31db9f72012-07-25 23:19:24 +02002669 /*
2670 * First, check if the first ref of the current inode was overwritten
2671 * before. If yes, we know that the current inode was already orphanized
2672 * and thus use the orphan name. If not, we can use get_cur_path to
2673 * get the path of the first ref as it would like while receiving at
2674 * this point in time.
2675 * New inodes are always orphan at the beginning, so force to use the
2676 * orphan name in this case.
2677 * The first ref is stored in valid_path and will be updated if it
2678 * gets moved around.
2679 */
2680 if (!sctx->cur_inode_new) {
2681 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2682 sctx->cur_inode_gen);
2683 if (ret < 0)
2684 goto out;
2685 if (ret)
2686 did_overwrite = 1;
2687 }
2688 if (sctx->cur_inode_new || did_overwrite) {
2689 ret = gen_unique_name(sctx, sctx->cur_ino,
2690 sctx->cur_inode_gen, valid_path);
2691 if (ret < 0)
2692 goto out;
2693 is_orphan = 1;
2694 } else {
2695 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2696 valid_path);
2697 if (ret < 0)
2698 goto out;
2699 }
2700
2701 list_for_each_entry(cur, &sctx->new_refs, list) {
2702 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02002703 * We may have refs where the parent directory does not exist
2704 * yet. This happens if the parent directories inum is higher
2705 * the the current inum. To handle this case, we create the
2706 * parent directory out of order. But we need to check if this
2707 * did already happen before due to other refs in the same dir.
2708 */
2709 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2710 if (ret < 0)
2711 goto out;
2712 if (ret == inode_state_will_create) {
2713 ret = 0;
2714 /*
2715 * First check if any of the current inodes refs did
2716 * already create the dir.
2717 */
2718 list_for_each_entry(cur2, &sctx->new_refs, list) {
2719 if (cur == cur2)
2720 break;
2721 if (cur2->dir == cur->dir) {
2722 ret = 1;
2723 break;
2724 }
2725 }
2726
2727 /*
2728 * If that did not happen, check if a previous inode
2729 * did already create the dir.
2730 */
2731 if (!ret)
2732 ret = did_create_dir(sctx, cur->dir);
2733 if (ret < 0)
2734 goto out;
2735 if (!ret) {
2736 ret = send_create_inode(sctx, cur->dir);
2737 if (ret < 0)
2738 goto out;
2739 }
2740 }
2741
2742 /*
Alexander Block31db9f72012-07-25 23:19:24 +02002743 * Check if this new ref would overwrite the first ref of
2744 * another unprocessed inode. If yes, orphanize the
2745 * overwritten inode. If we find an overwritten ref that is
2746 * not the first ref, simply unlink it.
2747 */
2748 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2749 cur->name, cur->name_len,
2750 &ow_inode, &ow_gen);
2751 if (ret < 0)
2752 goto out;
2753 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002754 ret = is_first_ref(sctx->parent_root,
2755 ow_inode, cur->dir, cur->name,
2756 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02002757 if (ret < 0)
2758 goto out;
2759 if (ret) {
2760 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2761 cur->full_path);
2762 if (ret < 0)
2763 goto out;
2764 } else {
2765 ret = send_unlink(sctx, cur->full_path);
2766 if (ret < 0)
2767 goto out;
2768 }
2769 }
2770
2771 /*
2772 * link/move the ref to the new place. If we have an orphan
2773 * inode, move it and update valid_path. If not, link or move
2774 * it depending on the inode mode.
2775 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002776 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02002777 ret = send_rename(sctx, valid_path, cur->full_path);
2778 if (ret < 0)
2779 goto out;
2780 is_orphan = 0;
2781 ret = fs_path_copy(valid_path, cur->full_path);
2782 if (ret < 0)
2783 goto out;
2784 } else {
2785 if (S_ISDIR(sctx->cur_inode_mode)) {
2786 /*
2787 * Dirs can't be linked, so move it. For moved
2788 * dirs, we always have one new and one deleted
2789 * ref. The deleted ref is ignored later.
2790 */
2791 ret = send_rename(sctx, valid_path,
2792 cur->full_path);
2793 if (ret < 0)
2794 goto out;
2795 ret = fs_path_copy(valid_path, cur->full_path);
2796 if (ret < 0)
2797 goto out;
2798 } else {
2799 ret = send_link(sctx, cur->full_path,
2800 valid_path);
2801 if (ret < 0)
2802 goto out;
2803 }
2804 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04002805 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02002806 if (ret < 0)
2807 goto out;
2808 }
2809
2810 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2811 /*
2812 * Check if we can already rmdir the directory. If not,
2813 * orphanize it. For every dir item inside that gets deleted
2814 * later, we do this check again and rmdir it then if possible.
2815 * See the use of check_dirs for more details.
2816 */
2817 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2818 if (ret < 0)
2819 goto out;
2820 if (ret) {
2821 ret = send_rmdir(sctx, valid_path);
2822 if (ret < 0)
2823 goto out;
2824 } else if (!is_orphan) {
2825 ret = orphanize_inode(sctx, sctx->cur_ino,
2826 sctx->cur_inode_gen, valid_path);
2827 if (ret < 0)
2828 goto out;
2829 is_orphan = 1;
2830 }
2831
2832 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04002833 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02002834 if (ret < 0)
2835 goto out;
2836 }
Alexander Blockccf16262012-07-28 11:46:29 +02002837 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2838 !list_empty(&sctx->deleted_refs)) {
2839 /*
2840 * We have a moved dir. Add the old parent to check_dirs
2841 */
2842 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2843 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002844 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02002845 if (ret < 0)
2846 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002847 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2848 /*
2849 * We have a non dir inode. Go through all deleted refs and
2850 * unlink them if they were not already overwritten by other
2851 * inodes.
2852 */
2853 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2854 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2855 sctx->cur_ino, sctx->cur_inode_gen,
2856 cur->name, cur->name_len);
2857 if (ret < 0)
2858 goto out;
2859 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002860 ret = send_unlink(sctx, cur->full_path);
2861 if (ret < 0)
2862 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002863 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04002864 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02002865 if (ret < 0)
2866 goto out;
2867 }
Alexander Block31db9f72012-07-25 23:19:24 +02002868 /*
2869 * If the inode is still orphan, unlink the orphan. This may
2870 * happen when a previous inode did overwrite the first ref
2871 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02002872 * inode. Unlinking does not mean that the inode is deleted in
2873 * all cases. There may still be links to this inode in other
2874 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02002875 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002876 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02002877 ret = send_unlink(sctx, valid_path);
2878 if (ret < 0)
2879 goto out;
2880 }
2881 }
2882
2883 /*
2884 * We did collect all parent dirs where cur_inode was once located. We
2885 * now go through all these dirs and check if they are pending for
2886 * deletion and if it's finally possible to perform the rmdir now.
2887 * We also update the inode stats of the parent dirs here.
2888 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04002889 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02002890 /*
2891 * In case we had refs into dirs that were not processed yet,
2892 * we don't need to do the utime and rmdir logic for these dirs.
2893 * The dir will be processed later.
2894 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04002895 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002896 continue;
2897
Josef Bacikba5e8f22013-08-16 16:52:55 -04002898 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02002899 if (ret < 0)
2900 goto out;
2901
2902 if (ret == inode_state_did_create ||
2903 ret == inode_state_no_change) {
2904 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04002905 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02002906 if (ret < 0)
2907 goto out;
2908 } else if (ret == inode_state_did_delete) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04002909 ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002910 if (ret < 0)
2911 goto out;
2912 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04002913 ret = get_cur_path(sctx, cur->dir,
2914 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02002915 if (ret < 0)
2916 goto out;
2917 ret = send_rmdir(sctx, valid_path);
2918 if (ret < 0)
2919 goto out;
2920 }
2921 }
2922 }
2923
Alexander Block31db9f72012-07-25 23:19:24 +02002924 ret = 0;
2925
2926out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04002927 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02002928 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002929 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02002930 return ret;
2931}
2932
2933static int __record_new_ref(int num, u64 dir, int index,
2934 struct fs_path *name,
2935 void *ctx)
2936{
2937 int ret = 0;
2938 struct send_ctx *sctx = ctx;
2939 struct fs_path *p;
2940 u64 gen;
2941
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002942 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002943 if (!p)
2944 return -ENOMEM;
2945
2946 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02002947 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02002948 if (ret < 0)
2949 goto out;
2950
Alexander Block31db9f72012-07-25 23:19:24 +02002951 ret = get_cur_path(sctx, dir, gen, p);
2952 if (ret < 0)
2953 goto out;
2954 ret = fs_path_add_path(p, name);
2955 if (ret < 0)
2956 goto out;
2957
2958 ret = record_ref(&sctx->new_refs, dir, gen, p);
2959
2960out:
2961 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002962 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002963 return ret;
2964}
2965
2966static int __record_deleted_ref(int num, u64 dir, int index,
2967 struct fs_path *name,
2968 void *ctx)
2969{
2970 int ret = 0;
2971 struct send_ctx *sctx = ctx;
2972 struct fs_path *p;
2973 u64 gen;
2974
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002975 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002976 if (!p)
2977 return -ENOMEM;
2978
2979 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02002980 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02002981 if (ret < 0)
2982 goto out;
2983
2984 ret = get_cur_path(sctx, dir, gen, p);
2985 if (ret < 0)
2986 goto out;
2987 ret = fs_path_add_path(p, name);
2988 if (ret < 0)
2989 goto out;
2990
2991 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
2992
2993out:
2994 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002995 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002996 return ret;
2997}
2998
2999static int record_new_ref(struct send_ctx *sctx)
3000{
3001 int ret;
3002
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003003 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3004 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003005 if (ret < 0)
3006 goto out;
3007 ret = 0;
3008
3009out:
3010 return ret;
3011}
3012
3013static int record_deleted_ref(struct send_ctx *sctx)
3014{
3015 int ret;
3016
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003017 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3018 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003019 if (ret < 0)
3020 goto out;
3021 ret = 0;
3022
3023out:
3024 return ret;
3025}
3026
3027struct find_ref_ctx {
3028 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003029 u64 dir_gen;
3030 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003031 struct fs_path *name;
3032 int found_idx;
3033};
3034
3035static int __find_iref(int num, u64 dir, int index,
3036 struct fs_path *name,
3037 void *ctx_)
3038{
3039 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003040 u64 dir_gen;
3041 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003042
3043 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3044 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003045 /*
3046 * To avoid doing extra lookups we'll only do this if everything
3047 * else matches.
3048 */
3049 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3050 NULL, NULL, NULL);
3051 if (ret)
3052 return ret;
3053 if (dir_gen != ctx->dir_gen)
3054 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003055 ctx->found_idx = num;
3056 return 1;
3057 }
3058 return 0;
3059}
3060
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003061static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003062 struct btrfs_path *path,
3063 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003064 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003065{
3066 int ret;
3067 struct find_ref_ctx ctx;
3068
3069 ctx.dir = dir;
3070 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003071 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003072 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003073 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003074
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003075 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003076 if (ret < 0)
3077 return ret;
3078
3079 if (ctx.found_idx == -1)
3080 return -ENOENT;
3081
3082 return ctx.found_idx;
3083}
3084
3085static int __record_changed_new_ref(int num, u64 dir, int index,
3086 struct fs_path *name,
3087 void *ctx)
3088{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003089 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003090 int ret;
3091 struct send_ctx *sctx = ctx;
3092
Josef Bacikba5e8f22013-08-16 16:52:55 -04003093 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3094 NULL, NULL, NULL);
3095 if (ret)
3096 return ret;
3097
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003098 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003099 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003100 if (ret == -ENOENT)
3101 ret = __record_new_ref(num, dir, index, name, sctx);
3102 else if (ret > 0)
3103 ret = 0;
3104
3105 return ret;
3106}
3107
3108static int __record_changed_deleted_ref(int num, u64 dir, int index,
3109 struct fs_path *name,
3110 void *ctx)
3111{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003112 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003113 int ret;
3114 struct send_ctx *sctx = ctx;
3115
Josef Bacikba5e8f22013-08-16 16:52:55 -04003116 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3117 NULL, NULL, NULL);
3118 if (ret)
3119 return ret;
3120
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003121 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003122 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003123 if (ret == -ENOENT)
3124 ret = __record_deleted_ref(num, dir, index, name, sctx);
3125 else if (ret > 0)
3126 ret = 0;
3127
3128 return ret;
3129}
3130
3131static int record_changed_ref(struct send_ctx *sctx)
3132{
3133 int ret = 0;
3134
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003135 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003136 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3137 if (ret < 0)
3138 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003139 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003140 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3141 if (ret < 0)
3142 goto out;
3143 ret = 0;
3144
3145out:
3146 return ret;
3147}
3148
3149/*
3150 * Record and process all refs at once. Needed when an inode changes the
3151 * generation number, which means that it was deleted and recreated.
3152 */
3153static int process_all_refs(struct send_ctx *sctx,
3154 enum btrfs_compare_tree_result cmd)
3155{
3156 int ret;
3157 struct btrfs_root *root;
3158 struct btrfs_path *path;
3159 struct btrfs_key key;
3160 struct btrfs_key found_key;
3161 struct extent_buffer *eb;
3162 int slot;
3163 iterate_inode_ref_t cb;
3164
3165 path = alloc_path_for_send();
3166 if (!path)
3167 return -ENOMEM;
3168
3169 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3170 root = sctx->send_root;
3171 cb = __record_new_ref;
3172 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3173 root = sctx->parent_root;
3174 cb = __record_deleted_ref;
3175 } else {
3176 BUG();
3177 }
3178
3179 key.objectid = sctx->cmp_key->objectid;
3180 key.type = BTRFS_INODE_REF_KEY;
3181 key.offset = 0;
3182 while (1) {
3183 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
Alexander Blocke938c8a2012-07-28 16:33:49 +02003184 if (ret < 0)
Alexander Block31db9f72012-07-25 23:19:24 +02003185 goto out;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003186 if (ret)
Alexander Block31db9f72012-07-25 23:19:24 +02003187 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003188
3189 eb = path->nodes[0];
3190 slot = path->slots[0];
3191 btrfs_item_key_to_cpu(eb, &found_key, slot);
3192
3193 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003194 (found_key.type != BTRFS_INODE_REF_KEY &&
3195 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003196 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003197
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003198 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003199 btrfs_release_path(path);
3200 if (ret < 0)
3201 goto out;
3202
3203 key.offset = found_key.offset + 1;
3204 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003205 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003206
3207 ret = process_recorded_refs(sctx);
3208
3209out:
3210 btrfs_free_path(path);
3211 return ret;
3212}
3213
3214static int send_set_xattr(struct send_ctx *sctx,
3215 struct fs_path *path,
3216 const char *name, int name_len,
3217 const char *data, int data_len)
3218{
3219 int ret = 0;
3220
3221 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3222 if (ret < 0)
3223 goto out;
3224
3225 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3226 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3227 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3228
3229 ret = send_cmd(sctx);
3230
3231tlv_put_failure:
3232out:
3233 return ret;
3234}
3235
3236static int send_remove_xattr(struct send_ctx *sctx,
3237 struct fs_path *path,
3238 const char *name, int name_len)
3239{
3240 int ret = 0;
3241
3242 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3243 if (ret < 0)
3244 goto out;
3245
3246 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3247 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3248
3249 ret = send_cmd(sctx);
3250
3251tlv_put_failure:
3252out:
3253 return ret;
3254}
3255
3256static int __process_new_xattr(int num, struct btrfs_key *di_key,
3257 const char *name, int name_len,
3258 const char *data, int data_len,
3259 u8 type, void *ctx)
3260{
3261 int ret;
3262 struct send_ctx *sctx = ctx;
3263 struct fs_path *p;
3264 posix_acl_xattr_header dummy_acl;
3265
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003266 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003267 if (!p)
3268 return -ENOMEM;
3269
3270 /*
3271 * This hack is needed because empty acl's are stored as zero byte
3272 * data in xattrs. Problem with that is, that receiving these zero byte
3273 * acl's will fail later. To fix this, we send a dummy acl list that
3274 * only contains the version number and no entries.
3275 */
3276 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3277 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3278 if (data_len == 0) {
3279 dummy_acl.a_version =
3280 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3281 data = (char *)&dummy_acl;
3282 data_len = sizeof(dummy_acl);
3283 }
3284 }
3285
3286 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3287 if (ret < 0)
3288 goto out;
3289
3290 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3291
3292out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003293 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003294 return ret;
3295}
3296
3297static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3298 const char *name, int name_len,
3299 const char *data, int data_len,
3300 u8 type, void *ctx)
3301{
3302 int ret;
3303 struct send_ctx *sctx = ctx;
3304 struct fs_path *p;
3305
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003306 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003307 if (!p)
3308 return -ENOMEM;
3309
3310 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3311 if (ret < 0)
3312 goto out;
3313
3314 ret = send_remove_xattr(sctx, p, name, name_len);
3315
3316out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003317 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003318 return ret;
3319}
3320
3321static int process_new_xattr(struct send_ctx *sctx)
3322{
3323 int ret = 0;
3324
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003325 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3326 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003327
3328 return ret;
3329}
3330
3331static int process_deleted_xattr(struct send_ctx *sctx)
3332{
3333 int ret;
3334
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003335 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3336 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003337
3338 return ret;
3339}
3340
3341struct find_xattr_ctx {
3342 const char *name;
3343 int name_len;
3344 int found_idx;
3345 char *found_data;
3346 int found_data_len;
3347};
3348
3349static int __find_xattr(int num, struct btrfs_key *di_key,
3350 const char *name, int name_len,
3351 const char *data, int data_len,
3352 u8 type, void *vctx)
3353{
3354 struct find_xattr_ctx *ctx = vctx;
3355
3356 if (name_len == ctx->name_len &&
3357 strncmp(name, ctx->name, name_len) == 0) {
3358 ctx->found_idx = num;
3359 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00003360 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02003361 if (!ctx->found_data)
3362 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02003363 return 1;
3364 }
3365 return 0;
3366}
3367
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003368static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003369 struct btrfs_path *path,
3370 struct btrfs_key *key,
3371 const char *name, int name_len,
3372 char **data, int *data_len)
3373{
3374 int ret;
3375 struct find_xattr_ctx ctx;
3376
3377 ctx.name = name;
3378 ctx.name_len = name_len;
3379 ctx.found_idx = -1;
3380 ctx.found_data = NULL;
3381 ctx.found_data_len = 0;
3382
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003383 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003384 if (ret < 0)
3385 return ret;
3386
3387 if (ctx.found_idx == -1)
3388 return -ENOENT;
3389 if (data) {
3390 *data = ctx.found_data;
3391 *data_len = ctx.found_data_len;
3392 } else {
3393 kfree(ctx.found_data);
3394 }
3395 return ctx.found_idx;
3396}
3397
3398
3399static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3400 const char *name, int name_len,
3401 const char *data, int data_len,
3402 u8 type, void *ctx)
3403{
3404 int ret;
3405 struct send_ctx *sctx = ctx;
3406 char *found_data = NULL;
3407 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003408
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003409 ret = find_xattr(sctx->parent_root, sctx->right_path,
3410 sctx->cmp_key, name, name_len, &found_data,
3411 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003412 if (ret == -ENOENT) {
3413 ret = __process_new_xattr(num, di_key, name, name_len, data,
3414 data_len, type, ctx);
3415 } else if (ret >= 0) {
3416 if (data_len != found_data_len ||
3417 memcmp(data, found_data, data_len)) {
3418 ret = __process_new_xattr(num, di_key, name, name_len,
3419 data, data_len, type, ctx);
3420 } else {
3421 ret = 0;
3422 }
3423 }
3424
3425 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02003426 return ret;
3427}
3428
3429static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3430 const char *name, int name_len,
3431 const char *data, int data_len,
3432 u8 type, void *ctx)
3433{
3434 int ret;
3435 struct send_ctx *sctx = ctx;
3436
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003437 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
3438 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003439 if (ret == -ENOENT)
3440 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3441 data_len, type, ctx);
3442 else if (ret >= 0)
3443 ret = 0;
3444
3445 return ret;
3446}
3447
3448static int process_changed_xattr(struct send_ctx *sctx)
3449{
3450 int ret = 0;
3451
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003452 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003453 sctx->cmp_key, __process_changed_new_xattr, sctx);
3454 if (ret < 0)
3455 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003456 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003457 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3458
3459out:
3460 return ret;
3461}
3462
3463static int process_all_new_xattrs(struct send_ctx *sctx)
3464{
3465 int ret;
3466 struct btrfs_root *root;
3467 struct btrfs_path *path;
3468 struct btrfs_key key;
3469 struct btrfs_key found_key;
3470 struct extent_buffer *eb;
3471 int slot;
3472
3473 path = alloc_path_for_send();
3474 if (!path)
3475 return -ENOMEM;
3476
3477 root = sctx->send_root;
3478
3479 key.objectid = sctx->cmp_key->objectid;
3480 key.type = BTRFS_XATTR_ITEM_KEY;
3481 key.offset = 0;
3482 while (1) {
3483 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3484 if (ret < 0)
3485 goto out;
3486 if (ret) {
3487 ret = 0;
3488 goto out;
3489 }
3490
3491 eb = path->nodes[0];
3492 slot = path->slots[0];
3493 btrfs_item_key_to_cpu(eb, &found_key, slot);
3494
3495 if (found_key.objectid != key.objectid ||
3496 found_key.type != key.type) {
3497 ret = 0;
3498 goto out;
3499 }
3500
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003501 ret = iterate_dir_item(root, path, &found_key,
3502 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003503 if (ret < 0)
3504 goto out;
3505
3506 btrfs_release_path(path);
3507 key.offset = found_key.offset + 1;
3508 }
3509
3510out:
3511 btrfs_free_path(path);
3512 return ret;
3513}
3514
Josef Baciked259092013-10-25 11:36:01 -04003515static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
3516{
3517 struct btrfs_root *root = sctx->send_root;
3518 struct btrfs_fs_info *fs_info = root->fs_info;
3519 struct inode *inode;
3520 struct page *page;
3521 char *addr;
3522 struct btrfs_key key;
3523 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
3524 pgoff_t last_index;
3525 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
3526 ssize_t ret = 0;
3527
3528 key.objectid = sctx->cur_ino;
3529 key.type = BTRFS_INODE_ITEM_KEY;
3530 key.offset = 0;
3531
3532 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3533 if (IS_ERR(inode))
3534 return PTR_ERR(inode);
3535
3536 if (offset + len > i_size_read(inode)) {
3537 if (offset > i_size_read(inode))
3538 len = 0;
3539 else
3540 len = offset - i_size_read(inode);
3541 }
3542 if (len == 0)
3543 goto out;
3544
3545 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
3546 while (index <= last_index) {
3547 unsigned cur_len = min_t(unsigned, len,
3548 PAGE_CACHE_SIZE - pg_offset);
3549 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3550 if (!page) {
3551 ret = -ENOMEM;
3552 break;
3553 }
3554
3555 if (!PageUptodate(page)) {
3556 btrfs_readpage(NULL, page);
3557 lock_page(page);
3558 if (!PageUptodate(page)) {
3559 unlock_page(page);
3560 page_cache_release(page);
3561 ret = -EIO;
3562 break;
3563 }
3564 }
3565
3566 addr = kmap(page);
3567 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
3568 kunmap(page);
3569 unlock_page(page);
3570 page_cache_release(page);
3571 index++;
3572 pg_offset = 0;
3573 len -= cur_len;
3574 ret += cur_len;
3575 }
3576out:
3577 iput(inode);
3578 return ret;
3579}
3580
Alexander Block31db9f72012-07-25 23:19:24 +02003581/*
3582 * Read some bytes from the current inode/file and send a write command to
3583 * user space.
3584 */
3585static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3586{
3587 int ret = 0;
3588 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04003589 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003590
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003591 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003592 if (!p)
3593 return -ENOMEM;
3594
Alexander Block31db9f72012-07-25 23:19:24 +02003595verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3596
Josef Baciked259092013-10-25 11:36:01 -04003597 num_read = fill_read_buf(sctx, offset, len);
3598 if (num_read <= 0) {
3599 if (num_read < 0)
3600 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02003601 goto out;
Josef Baciked259092013-10-25 11:36:01 -04003602 }
Alexander Block31db9f72012-07-25 23:19:24 +02003603
3604 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3605 if (ret < 0)
3606 goto out;
3607
3608 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3609 if (ret < 0)
3610 goto out;
3611
3612 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3613 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02003614 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02003615
3616 ret = send_cmd(sctx);
3617
3618tlv_put_failure:
3619out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003620 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003621 if (ret < 0)
3622 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003623 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02003624}
3625
3626/*
3627 * Send a clone command to user space.
3628 */
3629static int send_clone(struct send_ctx *sctx,
3630 u64 offset, u32 len,
3631 struct clone_root *clone_root)
3632{
3633 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003634 struct fs_path *p;
3635 u64 gen;
3636
3637verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3638 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3639 clone_root->root->objectid, clone_root->ino,
3640 clone_root->offset);
3641
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003642 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003643 if (!p)
3644 return -ENOMEM;
3645
3646 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3647 if (ret < 0)
3648 goto out;
3649
3650 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3651 if (ret < 0)
3652 goto out;
3653
3654 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3655 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3656 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3657
Alexander Blocke938c8a2012-07-28 16:33:49 +02003658 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02003659 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003660 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003661 if (ret < 0)
3662 goto out;
3663 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3664 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003665 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003666 }
3667 if (ret < 0)
3668 goto out;
3669
3670 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02003671 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02003672 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00003673 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02003674 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3675 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3676 clone_root->offset);
3677
3678 ret = send_cmd(sctx);
3679
3680tlv_put_failure:
3681out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003682 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003683 return ret;
3684}
3685
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003686/*
3687 * Send an update extent command to user space.
3688 */
3689static int send_update_extent(struct send_ctx *sctx,
3690 u64 offset, u32 len)
3691{
3692 int ret = 0;
3693 struct fs_path *p;
3694
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003695 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003696 if (!p)
3697 return -ENOMEM;
3698
3699 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
3700 if (ret < 0)
3701 goto out;
3702
3703 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3704 if (ret < 0)
3705 goto out;
3706
3707 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3708 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3709 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
3710
3711 ret = send_cmd(sctx);
3712
3713tlv_put_failure:
3714out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003715 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003716 return ret;
3717}
3718
Josef Bacik16e75492013-10-22 12:18:51 -04003719static int send_hole(struct send_ctx *sctx, u64 end)
3720{
3721 struct fs_path *p = NULL;
3722 u64 offset = sctx->cur_inode_last_extent;
3723 u64 len;
3724 int ret = 0;
3725
3726 p = fs_path_alloc();
3727 if (!p)
3728 return -ENOMEM;
3729 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
3730 while (offset < end) {
3731 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
3732
3733 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3734 if (ret < 0)
3735 break;
3736 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3737 if (ret < 0)
3738 break;
3739 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3740 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3741 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
3742 ret = send_cmd(sctx);
3743 if (ret < 0)
3744 break;
3745 offset += len;
3746 }
3747tlv_put_failure:
3748 fs_path_free(p);
3749 return ret;
3750}
3751
Alexander Block31db9f72012-07-25 23:19:24 +02003752static int send_write_or_clone(struct send_ctx *sctx,
3753 struct btrfs_path *path,
3754 struct btrfs_key *key,
3755 struct clone_root *clone_root)
3756{
3757 int ret = 0;
3758 struct btrfs_file_extent_item *ei;
3759 u64 offset = key->offset;
3760 u64 pos = 0;
3761 u64 len;
3762 u32 l;
3763 u8 type;
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00003764 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
Alexander Block31db9f72012-07-25 23:19:24 +02003765
3766 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3767 struct btrfs_file_extent_item);
3768 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003769 if (type == BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02003770 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003771 /*
3772 * it is possible the inline item won't cover the whole page,
3773 * but there may be items after this page. Make
3774 * sure to send the whole thing
3775 */
3776 len = PAGE_CACHE_ALIGN(len);
3777 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02003778 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003779 }
Alexander Block31db9f72012-07-25 23:19:24 +02003780
3781 if (offset + len > sctx->cur_inode_size)
3782 len = sctx->cur_inode_size - offset;
3783 if (len == 0) {
3784 ret = 0;
3785 goto out;
3786 }
3787
Filipe David Borba Manana28e5dd82014-01-12 02:26:28 +00003788 if (clone_root && IS_ALIGNED(offset + len, bs)) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003789 ret = send_clone(sctx, offset, len, clone_root);
3790 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
3791 ret = send_update_extent(sctx, offset, len);
3792 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02003793 while (pos < len) {
3794 l = len - pos;
3795 if (l > BTRFS_SEND_READ_SIZE)
3796 l = BTRFS_SEND_READ_SIZE;
3797 ret = send_write(sctx, pos + offset, l);
3798 if (ret < 0)
3799 goto out;
3800 if (!ret)
3801 break;
3802 pos += ret;
3803 }
3804 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003805 }
Alexander Block31db9f72012-07-25 23:19:24 +02003806out:
3807 return ret;
3808}
3809
3810static int is_extent_unchanged(struct send_ctx *sctx,
3811 struct btrfs_path *left_path,
3812 struct btrfs_key *ekey)
3813{
3814 int ret = 0;
3815 struct btrfs_key key;
3816 struct btrfs_path *path = NULL;
3817 struct extent_buffer *eb;
3818 int slot;
3819 struct btrfs_key found_key;
3820 struct btrfs_file_extent_item *ei;
3821 u64 left_disknr;
3822 u64 right_disknr;
3823 u64 left_offset;
3824 u64 right_offset;
3825 u64 left_offset_fixed;
3826 u64 left_len;
3827 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04003828 u64 left_gen;
3829 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003830 u8 left_type;
3831 u8 right_type;
3832
3833 path = alloc_path_for_send();
3834 if (!path)
3835 return -ENOMEM;
3836
3837 eb = left_path->nodes[0];
3838 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02003839 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3840 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02003841
3842 if (left_type != BTRFS_FILE_EXTENT_REG) {
3843 ret = 0;
3844 goto out;
3845 }
Chris Mason74dd17f2012-08-07 16:25:13 -04003846 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3847 left_len = btrfs_file_extent_num_bytes(eb, ei);
3848 left_offset = btrfs_file_extent_offset(eb, ei);
3849 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02003850
3851 /*
3852 * Following comments will refer to these graphics. L is the left
3853 * extents which we are checking at the moment. 1-8 are the right
3854 * extents that we iterate.
3855 *
3856 * |-----L-----|
3857 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3858 *
3859 * |-----L-----|
3860 * |--1--|-2b-|...(same as above)
3861 *
3862 * Alternative situation. Happens on files where extents got split.
3863 * |-----L-----|
3864 * |-----------7-----------|-6-|
3865 *
3866 * Alternative situation. Happens on files which got larger.
3867 * |-----L-----|
3868 * |-8-|
3869 * Nothing follows after 8.
3870 */
3871
3872 key.objectid = ekey->objectid;
3873 key.type = BTRFS_EXTENT_DATA_KEY;
3874 key.offset = ekey->offset;
3875 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3876 if (ret < 0)
3877 goto out;
3878 if (ret) {
3879 ret = 0;
3880 goto out;
3881 }
3882
3883 /*
3884 * Handle special case where the right side has no extents at all.
3885 */
3886 eb = path->nodes[0];
3887 slot = path->slots[0];
3888 btrfs_item_key_to_cpu(eb, &found_key, slot);
3889 if (found_key.objectid != key.objectid ||
3890 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04003891 /* If we're a hole then just pretend nothing changed */
3892 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02003893 goto out;
3894 }
3895
3896 /*
3897 * We're now on 2a, 2b or 7.
3898 */
3899 key = found_key;
3900 while (key.offset < ekey->offset + left_len) {
3901 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3902 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02003903 if (right_type != BTRFS_FILE_EXTENT_REG) {
3904 ret = 0;
3905 goto out;
3906 }
3907
Josef Bacik007d31f2013-10-31 16:49:02 -04003908 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3909 right_len = btrfs_file_extent_num_bytes(eb, ei);
3910 right_offset = btrfs_file_extent_offset(eb, ei);
3911 right_gen = btrfs_file_extent_generation(eb, ei);
3912
Alexander Block31db9f72012-07-25 23:19:24 +02003913 /*
3914 * Are we at extent 8? If yes, we know the extent is changed.
3915 * This may only happen on the first iteration.
3916 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02003917 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04003918 /* If we're a hole just pretend nothing changed */
3919 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02003920 goto out;
3921 }
3922
3923 left_offset_fixed = left_offset;
3924 if (key.offset < ekey->offset) {
3925 /* Fix the right offset for 2a and 7. */
3926 right_offset += ekey->offset - key.offset;
3927 } else {
3928 /* Fix the left offset for all behind 2a and 2b */
3929 left_offset_fixed += key.offset - ekey->offset;
3930 }
3931
3932 /*
3933 * Check if we have the same extent.
3934 */
Alexander Block39540962012-08-01 12:46:05 +02003935 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04003936 left_offset_fixed != right_offset ||
3937 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02003938 ret = 0;
3939 goto out;
3940 }
3941
3942 /*
3943 * Go to the next extent.
3944 */
3945 ret = btrfs_next_item(sctx->parent_root, path);
3946 if (ret < 0)
3947 goto out;
3948 if (!ret) {
3949 eb = path->nodes[0];
3950 slot = path->slots[0];
3951 btrfs_item_key_to_cpu(eb, &found_key, slot);
3952 }
3953 if (ret || found_key.objectid != key.objectid ||
3954 found_key.type != key.type) {
3955 key.offset += right_len;
3956 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00003957 }
3958 if (found_key.offset != key.offset + right_len) {
3959 ret = 0;
3960 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003961 }
3962 key = found_key;
3963 }
3964
3965 /*
3966 * We're now behind the left extent (treat as unchanged) or at the end
3967 * of the right side (treat as changed).
3968 */
3969 if (key.offset >= ekey->offset + left_len)
3970 ret = 1;
3971 else
3972 ret = 0;
3973
3974
3975out:
3976 btrfs_free_path(path);
3977 return ret;
3978}
3979
Josef Bacik16e75492013-10-22 12:18:51 -04003980static int get_last_extent(struct send_ctx *sctx, u64 offset)
3981{
3982 struct btrfs_path *path;
3983 struct btrfs_root *root = sctx->send_root;
3984 struct btrfs_file_extent_item *fi;
3985 struct btrfs_key key;
3986 u64 extent_end;
3987 u8 type;
3988 int ret;
3989
3990 path = alloc_path_for_send();
3991 if (!path)
3992 return -ENOMEM;
3993
3994 sctx->cur_inode_last_extent = 0;
3995
3996 key.objectid = sctx->cur_ino;
3997 key.type = BTRFS_EXTENT_DATA_KEY;
3998 key.offset = offset;
3999 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4000 if (ret < 0)
4001 goto out;
4002 ret = 0;
4003 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4004 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4005 goto out;
4006
4007 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4008 struct btrfs_file_extent_item);
4009 type = btrfs_file_extent_type(path->nodes[0], fi);
4010 if (type == BTRFS_FILE_EXTENT_INLINE) {
4011 u64 size = btrfs_file_extent_inline_len(path->nodes[0], fi);
4012 extent_end = ALIGN(key.offset + size,
4013 sctx->send_root->sectorsize);
4014 } else {
4015 extent_end = key.offset +
4016 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4017 }
4018 sctx->cur_inode_last_extent = extent_end;
4019out:
4020 btrfs_free_path(path);
4021 return ret;
4022}
4023
4024static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4025 struct btrfs_key *key)
4026{
4027 struct btrfs_file_extent_item *fi;
4028 u64 extent_end;
4029 u8 type;
4030 int ret = 0;
4031
4032 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4033 return 0;
4034
4035 if (sctx->cur_inode_last_extent == (u64)-1) {
4036 ret = get_last_extent(sctx, key->offset - 1);
4037 if (ret)
4038 return ret;
4039 }
4040
4041 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4042 struct btrfs_file_extent_item);
4043 type = btrfs_file_extent_type(path->nodes[0], fi);
4044 if (type == BTRFS_FILE_EXTENT_INLINE) {
4045 u64 size = btrfs_file_extent_inline_len(path->nodes[0], fi);
4046 extent_end = ALIGN(key->offset + size,
4047 sctx->send_root->sectorsize);
4048 } else {
4049 extent_end = key->offset +
4050 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4051 }
4052 if (sctx->cur_inode_last_extent < key->offset)
4053 ret = send_hole(sctx, key->offset);
4054 sctx->cur_inode_last_extent = extent_end;
4055 return ret;
4056}
4057
Alexander Block31db9f72012-07-25 23:19:24 +02004058static int process_extent(struct send_ctx *sctx,
4059 struct btrfs_path *path,
4060 struct btrfs_key *key)
4061{
Alexander Block31db9f72012-07-25 23:19:24 +02004062 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004063 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004064
4065 if (S_ISLNK(sctx->cur_inode_mode))
4066 return 0;
4067
4068 if (sctx->parent_root && !sctx->cur_inode_new) {
4069 ret = is_extent_unchanged(sctx, path, key);
4070 if (ret < 0)
4071 goto out;
4072 if (ret) {
4073 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004074 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004075 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004076 } else {
4077 struct btrfs_file_extent_item *ei;
4078 u8 type;
4079
4080 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4081 struct btrfs_file_extent_item);
4082 type = btrfs_file_extent_type(path->nodes[0], ei);
4083 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4084 type == BTRFS_FILE_EXTENT_REG) {
4085 /*
4086 * The send spec does not have a prealloc command yet,
4087 * so just leave a hole for prealloc'ed extents until
4088 * we have enough commands queued up to justify rev'ing
4089 * the send spec.
4090 */
4091 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4092 ret = 0;
4093 goto out;
4094 }
4095
4096 /* Have a hole, just skip it. */
4097 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4098 ret = 0;
4099 goto out;
4100 }
4101 }
Alexander Block31db9f72012-07-25 23:19:24 +02004102 }
4103
4104 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4105 sctx->cur_inode_size, &found_clone);
4106 if (ret != -ENOENT && ret < 0)
4107 goto out;
4108
4109 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004110 if (ret)
4111 goto out;
4112out_hole:
4113 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004114out:
4115 return ret;
4116}
4117
4118static int process_all_extents(struct send_ctx *sctx)
4119{
4120 int ret;
4121 struct btrfs_root *root;
4122 struct btrfs_path *path;
4123 struct btrfs_key key;
4124 struct btrfs_key found_key;
4125 struct extent_buffer *eb;
4126 int slot;
4127
4128 root = sctx->send_root;
4129 path = alloc_path_for_send();
4130 if (!path)
4131 return -ENOMEM;
4132
4133 key.objectid = sctx->cmp_key->objectid;
4134 key.type = BTRFS_EXTENT_DATA_KEY;
4135 key.offset = 0;
4136 while (1) {
4137 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
4138 if (ret < 0)
4139 goto out;
4140 if (ret) {
4141 ret = 0;
4142 goto out;
4143 }
4144
4145 eb = path->nodes[0];
4146 slot = path->slots[0];
4147 btrfs_item_key_to_cpu(eb, &found_key, slot);
4148
4149 if (found_key.objectid != key.objectid ||
4150 found_key.type != key.type) {
4151 ret = 0;
4152 goto out;
4153 }
4154
4155 ret = process_extent(sctx, path, &found_key);
4156 if (ret < 0)
4157 goto out;
4158
4159 btrfs_release_path(path);
4160 key.offset = found_key.offset + 1;
4161 }
4162
4163out:
4164 btrfs_free_path(path);
4165 return ret;
4166}
4167
4168static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
4169{
4170 int ret = 0;
4171
4172 if (sctx->cur_ino == 0)
4173 goto out;
4174 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004175 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004176 goto out;
4177 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4178 goto out;
4179
4180 ret = process_recorded_refs(sctx);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004181 if (ret < 0)
4182 goto out;
4183
4184 /*
4185 * We have processed the refs and thus need to advance send_progress.
4186 * Now, calls to get_cur_xxx will take the updated refs of the current
4187 * inode into account.
4188 */
4189 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004190
4191out:
4192 return ret;
4193}
4194
4195static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4196{
4197 int ret = 0;
4198 u64 left_mode;
4199 u64 left_uid;
4200 u64 left_gid;
4201 u64 right_mode;
4202 u64 right_uid;
4203 u64 right_gid;
4204 int need_chmod = 0;
4205 int need_chown = 0;
4206
4207 ret = process_recorded_refs_if_needed(sctx, at_end);
4208 if (ret < 0)
4209 goto out;
4210
4211 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4212 goto out;
4213 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4214 goto out;
4215
4216 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004217 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004218 if (ret < 0)
4219 goto out;
4220
Alex Lyakase2d044f2012-10-17 13:52:47 +00004221 if (!sctx->parent_root || sctx->cur_inode_new) {
4222 need_chown = 1;
4223 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004224 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004225 } else {
4226 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4227 NULL, NULL, &right_mode, &right_uid,
4228 &right_gid, NULL);
4229 if (ret < 0)
4230 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004231
Alex Lyakase2d044f2012-10-17 13:52:47 +00004232 if (left_uid != right_uid || left_gid != right_gid)
4233 need_chown = 1;
4234 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4235 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004236 }
4237
4238 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04004239 if (need_send_hole(sctx)) {
4240 if (sctx->cur_inode_last_extent == (u64)-1) {
4241 ret = get_last_extent(sctx, (u64)-1);
4242 if (ret)
4243 goto out;
4244 }
4245 if (sctx->cur_inode_last_extent <
4246 sctx->cur_inode_size) {
4247 ret = send_hole(sctx, sctx->cur_inode_size);
4248 if (ret)
4249 goto out;
4250 }
4251 }
Alexander Block31db9f72012-07-25 23:19:24 +02004252 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4253 sctx->cur_inode_size);
4254 if (ret < 0)
4255 goto out;
4256 }
4257
4258 if (need_chown) {
4259 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4260 left_uid, left_gid);
4261 if (ret < 0)
4262 goto out;
4263 }
4264 if (need_chmod) {
4265 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4266 left_mode);
4267 if (ret < 0)
4268 goto out;
4269 }
4270
4271 /*
4272 * Need to send that every time, no matter if it actually changed
4273 * between the two trees as we have done changes to the inode before.
4274 */
4275 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4276 if (ret < 0)
4277 goto out;
4278
4279out:
4280 return ret;
4281}
4282
4283static int changed_inode(struct send_ctx *sctx,
4284 enum btrfs_compare_tree_result result)
4285{
4286 int ret = 0;
4287 struct btrfs_key *key = sctx->cmp_key;
4288 struct btrfs_inode_item *left_ii = NULL;
4289 struct btrfs_inode_item *right_ii = NULL;
4290 u64 left_gen = 0;
4291 u64 right_gen = 0;
4292
Alexander Block31db9f72012-07-25 23:19:24 +02004293 sctx->cur_ino = key->objectid;
4294 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004295 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004296
4297 /*
4298 * Set send_progress to current inode. This will tell all get_cur_xxx
4299 * functions that the current inode's refs are not updated yet. Later,
4300 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4301 */
Alexander Block31db9f72012-07-25 23:19:24 +02004302 sctx->send_progress = sctx->cur_ino;
4303
4304 if (result == BTRFS_COMPARE_TREE_NEW ||
4305 result == BTRFS_COMPARE_TREE_CHANGED) {
4306 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4307 sctx->left_path->slots[0],
4308 struct btrfs_inode_item);
4309 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4310 left_ii);
4311 } else {
4312 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4313 sctx->right_path->slots[0],
4314 struct btrfs_inode_item);
4315 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4316 right_ii);
4317 }
4318 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4319 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4320 sctx->right_path->slots[0],
4321 struct btrfs_inode_item);
4322
4323 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4324 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02004325
4326 /*
4327 * The cur_ino = root dir case is special here. We can't treat
4328 * the inode as deleted+reused because it would generate a
4329 * stream that tries to delete/mkdir the root dir.
4330 */
4331 if (left_gen != right_gen &&
4332 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02004333 sctx->cur_inode_new_gen = 1;
4334 }
4335
4336 if (result == BTRFS_COMPARE_TREE_NEW) {
4337 sctx->cur_inode_gen = left_gen;
4338 sctx->cur_inode_new = 1;
4339 sctx->cur_inode_deleted = 0;
4340 sctx->cur_inode_size = btrfs_inode_size(
4341 sctx->left_path->nodes[0], left_ii);
4342 sctx->cur_inode_mode = btrfs_inode_mode(
4343 sctx->left_path->nodes[0], left_ii);
4344 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02004345 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004346 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4347 sctx->cur_inode_gen = right_gen;
4348 sctx->cur_inode_new = 0;
4349 sctx->cur_inode_deleted = 1;
4350 sctx->cur_inode_size = btrfs_inode_size(
4351 sctx->right_path->nodes[0], right_ii);
4352 sctx->cur_inode_mode = btrfs_inode_mode(
4353 sctx->right_path->nodes[0], right_ii);
4354 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02004355 /*
4356 * We need to do some special handling in case the inode was
4357 * reported as changed with a changed generation number. This
4358 * means that the original inode was deleted and new inode
4359 * reused the same inum. So we have to treat the old inode as
4360 * deleted and the new one as new.
4361 */
Alexander Block31db9f72012-07-25 23:19:24 +02004362 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02004363 /*
4364 * First, process the inode as if it was deleted.
4365 */
Alexander Block31db9f72012-07-25 23:19:24 +02004366 sctx->cur_inode_gen = right_gen;
4367 sctx->cur_inode_new = 0;
4368 sctx->cur_inode_deleted = 1;
4369 sctx->cur_inode_size = btrfs_inode_size(
4370 sctx->right_path->nodes[0], right_ii);
4371 sctx->cur_inode_mode = btrfs_inode_mode(
4372 sctx->right_path->nodes[0], right_ii);
4373 ret = process_all_refs(sctx,
4374 BTRFS_COMPARE_TREE_DELETED);
4375 if (ret < 0)
4376 goto out;
4377
Alexander Block766702e2012-07-28 14:11:31 +02004378 /*
4379 * Now process the inode as if it was new.
4380 */
Alexander Block31db9f72012-07-25 23:19:24 +02004381 sctx->cur_inode_gen = left_gen;
4382 sctx->cur_inode_new = 1;
4383 sctx->cur_inode_deleted = 0;
4384 sctx->cur_inode_size = btrfs_inode_size(
4385 sctx->left_path->nodes[0], left_ii);
4386 sctx->cur_inode_mode = btrfs_inode_mode(
4387 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02004388 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004389 if (ret < 0)
4390 goto out;
4391
4392 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4393 if (ret < 0)
4394 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004395 /*
4396 * Advance send_progress now as we did not get into
4397 * process_recorded_refs_if_needed in the new_gen case.
4398 */
4399 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02004400
4401 /*
4402 * Now process all extents and xattrs of the inode as if
4403 * they were all new.
4404 */
Alexander Block31db9f72012-07-25 23:19:24 +02004405 ret = process_all_extents(sctx);
4406 if (ret < 0)
4407 goto out;
4408 ret = process_all_new_xattrs(sctx);
4409 if (ret < 0)
4410 goto out;
4411 } else {
4412 sctx->cur_inode_gen = left_gen;
4413 sctx->cur_inode_new = 0;
4414 sctx->cur_inode_new_gen = 0;
4415 sctx->cur_inode_deleted = 0;
4416 sctx->cur_inode_size = btrfs_inode_size(
4417 sctx->left_path->nodes[0], left_ii);
4418 sctx->cur_inode_mode = btrfs_inode_mode(
4419 sctx->left_path->nodes[0], left_ii);
4420 }
4421 }
4422
4423out:
4424 return ret;
4425}
4426
Alexander Block766702e2012-07-28 14:11:31 +02004427/*
4428 * We have to process new refs before deleted refs, but compare_trees gives us
4429 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4430 * first and later process them in process_recorded_refs.
4431 * For the cur_inode_new_gen case, we skip recording completely because
4432 * changed_inode did already initiate processing of refs. The reason for this is
4433 * that in this case, compare_tree actually compares the refs of 2 different
4434 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4435 * refs of the right tree as deleted and all refs of the left tree as new.
4436 */
Alexander Block31db9f72012-07-25 23:19:24 +02004437static int changed_ref(struct send_ctx *sctx,
4438 enum btrfs_compare_tree_result result)
4439{
4440 int ret = 0;
4441
4442 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4443
4444 if (!sctx->cur_inode_new_gen &&
4445 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4446 if (result == BTRFS_COMPARE_TREE_NEW)
4447 ret = record_new_ref(sctx);
4448 else if (result == BTRFS_COMPARE_TREE_DELETED)
4449 ret = record_deleted_ref(sctx);
4450 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4451 ret = record_changed_ref(sctx);
4452 }
4453
4454 return ret;
4455}
4456
Alexander Block766702e2012-07-28 14:11:31 +02004457/*
4458 * Process new/deleted/changed xattrs. We skip processing in the
4459 * cur_inode_new_gen case because changed_inode did already initiate processing
4460 * of xattrs. The reason is the same as in changed_ref
4461 */
Alexander Block31db9f72012-07-25 23:19:24 +02004462static int changed_xattr(struct send_ctx *sctx,
4463 enum btrfs_compare_tree_result result)
4464{
4465 int ret = 0;
4466
4467 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4468
4469 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4470 if (result == BTRFS_COMPARE_TREE_NEW)
4471 ret = process_new_xattr(sctx);
4472 else if (result == BTRFS_COMPARE_TREE_DELETED)
4473 ret = process_deleted_xattr(sctx);
4474 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4475 ret = process_changed_xattr(sctx);
4476 }
4477
4478 return ret;
4479}
4480
Alexander Block766702e2012-07-28 14:11:31 +02004481/*
4482 * Process new/deleted/changed extents. We skip processing in the
4483 * cur_inode_new_gen case because changed_inode did already initiate processing
4484 * of extents. The reason is the same as in changed_ref
4485 */
Alexander Block31db9f72012-07-25 23:19:24 +02004486static int changed_extent(struct send_ctx *sctx,
4487 enum btrfs_compare_tree_result result)
4488{
4489 int ret = 0;
4490
4491 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4492
4493 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4494 if (result != BTRFS_COMPARE_TREE_DELETED)
4495 ret = process_extent(sctx, sctx->left_path,
4496 sctx->cmp_key);
4497 }
4498
4499 return ret;
4500}
4501
Josef Bacikba5e8f22013-08-16 16:52:55 -04004502static int dir_changed(struct send_ctx *sctx, u64 dir)
4503{
4504 u64 orig_gen, new_gen;
4505 int ret;
4506
4507 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
4508 NULL, NULL);
4509 if (ret)
4510 return ret;
4511
4512 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
4513 NULL, NULL, NULL);
4514 if (ret)
4515 return ret;
4516
4517 return (orig_gen != new_gen) ? 1 : 0;
4518}
4519
4520static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
4521 struct btrfs_key *key)
4522{
4523 struct btrfs_inode_extref *extref;
4524 struct extent_buffer *leaf;
4525 u64 dirid = 0, last_dirid = 0;
4526 unsigned long ptr;
4527 u32 item_size;
4528 u32 cur_offset = 0;
4529 int ref_name_len;
4530 int ret = 0;
4531
4532 /* Easy case, just check this one dirid */
4533 if (key->type == BTRFS_INODE_REF_KEY) {
4534 dirid = key->offset;
4535
4536 ret = dir_changed(sctx, dirid);
4537 goto out;
4538 }
4539
4540 leaf = path->nodes[0];
4541 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4542 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4543 while (cur_offset < item_size) {
4544 extref = (struct btrfs_inode_extref *)(ptr +
4545 cur_offset);
4546 dirid = btrfs_inode_extref_parent(leaf, extref);
4547 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
4548 cur_offset += ref_name_len + sizeof(*extref);
4549 if (dirid == last_dirid)
4550 continue;
4551 ret = dir_changed(sctx, dirid);
4552 if (ret)
4553 break;
4554 last_dirid = dirid;
4555 }
4556out:
4557 return ret;
4558}
4559
Alexander Block766702e2012-07-28 14:11:31 +02004560/*
4561 * Updates compare related fields in sctx and simply forwards to the actual
4562 * changed_xxx functions.
4563 */
Alexander Block31db9f72012-07-25 23:19:24 +02004564static int changed_cb(struct btrfs_root *left_root,
4565 struct btrfs_root *right_root,
4566 struct btrfs_path *left_path,
4567 struct btrfs_path *right_path,
4568 struct btrfs_key *key,
4569 enum btrfs_compare_tree_result result,
4570 void *ctx)
4571{
4572 int ret = 0;
4573 struct send_ctx *sctx = ctx;
4574
Josef Bacikba5e8f22013-08-16 16:52:55 -04004575 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04004576 if (key->type == BTRFS_INODE_REF_KEY ||
4577 key->type == BTRFS_INODE_EXTREF_KEY) {
4578 ret = compare_refs(sctx, left_path, key);
4579 if (!ret)
4580 return 0;
4581 if (ret < 0)
4582 return ret;
4583 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
4584 return maybe_send_hole(sctx, left_path, key);
4585 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004586 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004587 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004588 result = BTRFS_COMPARE_TREE_CHANGED;
4589 ret = 0;
4590 }
4591
Alexander Block31db9f72012-07-25 23:19:24 +02004592 sctx->left_path = left_path;
4593 sctx->right_path = right_path;
4594 sctx->cmp_key = key;
4595
4596 ret = finish_inode_if_needed(sctx, 0);
4597 if (ret < 0)
4598 goto out;
4599
Alexander Block2981e222012-08-01 14:47:03 +02004600 /* Ignore non-FS objects */
4601 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4602 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4603 goto out;
4604
Alexander Block31db9f72012-07-25 23:19:24 +02004605 if (key->type == BTRFS_INODE_ITEM_KEY)
4606 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004607 else if (key->type == BTRFS_INODE_REF_KEY ||
4608 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004609 ret = changed_ref(sctx, result);
4610 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4611 ret = changed_xattr(sctx, result);
4612 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4613 ret = changed_extent(sctx, result);
4614
4615out:
4616 return ret;
4617}
4618
4619static int full_send_tree(struct send_ctx *sctx)
4620{
4621 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02004622 struct btrfs_root *send_root = sctx->send_root;
4623 struct btrfs_key key;
4624 struct btrfs_key found_key;
4625 struct btrfs_path *path;
4626 struct extent_buffer *eb;
4627 int slot;
4628 u64 start_ctransid;
4629 u64 ctransid;
4630
4631 path = alloc_path_for_send();
4632 if (!path)
4633 return -ENOMEM;
4634
Anand Jain5f3ab902012-12-07 09:28:54 +00004635 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004636 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00004637 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004638
4639 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4640 key.type = BTRFS_INODE_ITEM_KEY;
4641 key.offset = 0;
4642
Alexander Block31db9f72012-07-25 23:19:24 +02004643 /*
Alexander Block766702e2012-07-28 14:11:31 +02004644 * Make sure the tree has not changed after re-joining. We detect this
4645 * by comparing start_ctransid and ctransid. They should always match.
Alexander Block31db9f72012-07-25 23:19:24 +02004646 */
Anand Jain5f3ab902012-12-07 09:28:54 +00004647 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004648 ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00004649 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004650
4651 if (ctransid != start_ctransid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004652 WARN(1, KERN_WARNING "BTRFS: the root that you're trying to "
Alexander Block31db9f72012-07-25 23:19:24 +02004653 "send was modified in between. This is "
4654 "probably a bug.\n");
4655 ret = -EIO;
4656 goto out;
4657 }
4658
4659 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4660 if (ret < 0)
4661 goto out;
4662 if (ret)
4663 goto out_finish;
4664
4665 while (1) {
Alexander Block31db9f72012-07-25 23:19:24 +02004666 eb = path->nodes[0];
4667 slot = path->slots[0];
4668 btrfs_item_key_to_cpu(eb, &found_key, slot);
4669
4670 ret = changed_cb(send_root, NULL, path, NULL,
4671 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4672 if (ret < 0)
4673 goto out;
4674
4675 key.objectid = found_key.objectid;
4676 key.type = found_key.type;
4677 key.offset = found_key.offset + 1;
4678
4679 ret = btrfs_next_item(send_root, path);
4680 if (ret < 0)
4681 goto out;
4682 if (ret) {
4683 ret = 0;
4684 break;
4685 }
4686 }
4687
4688out_finish:
4689 ret = finish_inode_if_needed(sctx, 1);
4690
4691out:
4692 btrfs_free_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02004693 return ret;
4694}
4695
4696static int send_subvol(struct send_ctx *sctx)
4697{
4698 int ret;
4699
Stefan Behrensc2c71322013-04-10 17:10:52 +00004700 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
4701 ret = send_header(sctx);
4702 if (ret < 0)
4703 goto out;
4704 }
Alexander Block31db9f72012-07-25 23:19:24 +02004705
4706 ret = send_subvol_begin(sctx);
4707 if (ret < 0)
4708 goto out;
4709
4710 if (sctx->parent_root) {
4711 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4712 changed_cb, sctx);
4713 if (ret < 0)
4714 goto out;
4715 ret = finish_inode_if_needed(sctx, 1);
4716 if (ret < 0)
4717 goto out;
4718 } else {
4719 ret = full_send_tree(sctx);
4720 if (ret < 0)
4721 goto out;
4722 }
4723
4724out:
Alexander Block31db9f72012-07-25 23:19:24 +02004725 free_recorded_refs(sctx);
4726 return ret;
4727}
4728
David Sterba66ef7d62013-12-17 15:07:20 +01004729static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
4730{
4731 spin_lock(&root->root_item_lock);
4732 root->send_in_progress--;
4733 /*
4734 * Not much left to do, we don't know why it's unbalanced and
4735 * can't blindly reset it to 0.
4736 */
4737 if (root->send_in_progress < 0)
4738 btrfs_err(root->fs_info,
4739 "send_in_progres unbalanced %d root %llu\n",
4740 root->send_in_progress, root->root_key.objectid);
4741 spin_unlock(&root->root_item_lock);
4742}
4743
Alexander Block31db9f72012-07-25 23:19:24 +02004744long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4745{
4746 int ret = 0;
4747 struct btrfs_root *send_root;
4748 struct btrfs_root *clone_root;
4749 struct btrfs_fs_info *fs_info;
4750 struct btrfs_ioctl_send_args *arg = NULL;
4751 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02004752 struct send_ctx *sctx = NULL;
4753 u32 i;
4754 u64 *clone_sources_tmp = NULL;
David Sterba2c686532013-12-16 17:34:17 +01004755 int clone_sources_to_rollback = 0;
Wang Shilong896c14f2014-01-07 17:25:18 +08004756 int sort_clone_roots = 0;
Wang Shilong18f687d2014-01-07 17:25:19 +08004757 int index;
Alexander Block31db9f72012-07-25 23:19:24 +02004758
4759 if (!capable(CAP_SYS_ADMIN))
4760 return -EPERM;
4761
Al Viro496ad9a2013-01-23 17:07:38 -05004762 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02004763 fs_info = send_root->fs_info;
4764
Josef Bacik139f8072013-05-20 11:26:50 -04004765 /*
David Sterba2c686532013-12-16 17:34:17 +01004766 * The subvolume must remain read-only during send, protect against
4767 * making it RW.
4768 */
4769 spin_lock(&send_root->root_item_lock);
4770 send_root->send_in_progress++;
4771 spin_unlock(&send_root->root_item_lock);
4772
4773 /*
Josef Bacik139f8072013-05-20 11:26:50 -04004774 * This is done when we lookup the root, it should already be complete
4775 * by the time we get here.
4776 */
4777 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
4778
4779 /*
David Sterba2c686532013-12-16 17:34:17 +01004780 * Userspace tools do the checks and warn the user if it's
4781 * not RO.
4782 */
4783 if (!btrfs_root_readonly(send_root)) {
4784 ret = -EPERM;
4785 goto out;
4786 }
4787
Alexander Block31db9f72012-07-25 23:19:24 +02004788 arg = memdup_user(arg_, sizeof(*arg));
4789 if (IS_ERR(arg)) {
4790 ret = PTR_ERR(arg);
4791 arg = NULL;
4792 goto out;
4793 }
4794
4795 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05004796 sizeof(*arg->clone_sources) *
4797 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02004798 ret = -EFAULT;
4799 goto out;
4800 }
4801
Stefan Behrensc2c71322013-04-10 17:10:52 +00004802 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004803 ret = -EINVAL;
4804 goto out;
4805 }
4806
Alexander Block31db9f72012-07-25 23:19:24 +02004807 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4808 if (!sctx) {
4809 ret = -ENOMEM;
4810 goto out;
4811 }
4812
4813 INIT_LIST_HEAD(&sctx->new_refs);
4814 INIT_LIST_HEAD(&sctx->deleted_refs);
4815 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4816 INIT_LIST_HEAD(&sctx->name_cache_list);
4817
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004818 sctx->flags = arg->flags;
4819
Alexander Block31db9f72012-07-25 23:19:24 +02004820 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00004821 if (!sctx->send_filp) {
4822 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02004823 goto out;
4824 }
4825
Alexander Block31db9f72012-07-25 23:19:24 +02004826 sctx->send_root = send_root;
4827 sctx->clone_roots_cnt = arg->clone_sources_count;
4828
4829 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4830 sctx->send_buf = vmalloc(sctx->send_max_size);
4831 if (!sctx->send_buf) {
4832 ret = -ENOMEM;
4833 goto out;
4834 }
4835
4836 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4837 if (!sctx->read_buf) {
4838 ret = -ENOMEM;
4839 goto out;
4840 }
4841
4842 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4843 (arg->clone_sources_count + 1));
4844 if (!sctx->clone_roots) {
4845 ret = -ENOMEM;
4846 goto out;
4847 }
4848
4849 if (arg->clone_sources_count) {
4850 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4851 sizeof(*arg->clone_sources));
4852 if (!clone_sources_tmp) {
4853 ret = -ENOMEM;
4854 goto out;
4855 }
4856
4857 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4858 arg->clone_sources_count *
4859 sizeof(*arg->clone_sources));
4860 if (ret) {
4861 ret = -EFAULT;
4862 goto out;
4863 }
4864
4865 for (i = 0; i < arg->clone_sources_count; i++) {
4866 key.objectid = clone_sources_tmp[i];
4867 key.type = BTRFS_ROOT_ITEM_KEY;
4868 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08004869
4870 index = srcu_read_lock(&fs_info->subvol_srcu);
4871
Alexander Block31db9f72012-07-25 23:19:24 +02004872 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02004873 if (IS_ERR(clone_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08004874 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02004875 ret = PTR_ERR(clone_root);
4876 goto out;
4877 }
David Sterba2c686532013-12-16 17:34:17 +01004878 clone_sources_to_rollback = i + 1;
4879 spin_lock(&clone_root->root_item_lock);
4880 clone_root->send_in_progress++;
4881 if (!btrfs_root_readonly(clone_root)) {
4882 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08004883 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01004884 ret = -EPERM;
4885 goto out;
4886 }
4887 spin_unlock(&clone_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08004888 srcu_read_unlock(&fs_info->subvol_srcu, index);
4889
Alexander Block31db9f72012-07-25 23:19:24 +02004890 sctx->clone_roots[i].root = clone_root;
4891 }
4892 vfree(clone_sources_tmp);
4893 clone_sources_tmp = NULL;
4894 }
4895
4896 if (arg->parent_root) {
4897 key.objectid = arg->parent_root;
4898 key.type = BTRFS_ROOT_ITEM_KEY;
4899 key.offset = (u64)-1;
Wang Shilong18f687d2014-01-07 17:25:19 +08004900
4901 index = srcu_read_lock(&fs_info->subvol_srcu);
4902
Alexander Block31db9f72012-07-25 23:19:24 +02004903 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00004904 if (IS_ERR(sctx->parent_root)) {
Wang Shilong18f687d2014-01-07 17:25:19 +08004905 srcu_read_unlock(&fs_info->subvol_srcu, index);
Stefan Behrensb1b19592013-05-13 14:42:57 +00004906 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02004907 goto out;
4908 }
Wang Shilong18f687d2014-01-07 17:25:19 +08004909
David Sterba2c686532013-12-16 17:34:17 +01004910 spin_lock(&sctx->parent_root->root_item_lock);
4911 sctx->parent_root->send_in_progress++;
4912 if (!btrfs_root_readonly(sctx->parent_root)) {
4913 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08004914 srcu_read_unlock(&fs_info->subvol_srcu, index);
David Sterba2c686532013-12-16 17:34:17 +01004915 ret = -EPERM;
4916 goto out;
4917 }
4918 spin_unlock(&sctx->parent_root->root_item_lock);
Wang Shilong18f687d2014-01-07 17:25:19 +08004919
4920 srcu_read_unlock(&fs_info->subvol_srcu, index);
Alexander Block31db9f72012-07-25 23:19:24 +02004921 }
4922
4923 /*
4924 * Clones from send_root are allowed, but only if the clone source
4925 * is behind the current send position. This is checked while searching
4926 * for possible clone sources.
4927 */
4928 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4929
4930 /* We do a bsearch later */
4931 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4932 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4933 NULL);
Wang Shilong896c14f2014-01-07 17:25:18 +08004934 sort_clone_roots = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004935
4936 ret = send_subvol(sctx);
4937 if (ret < 0)
4938 goto out;
4939
Stefan Behrensc2c71322013-04-10 17:10:52 +00004940 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
4941 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4942 if (ret < 0)
4943 goto out;
4944 ret = send_cmd(sctx);
4945 if (ret < 0)
4946 goto out;
4947 }
Alexander Block31db9f72012-07-25 23:19:24 +02004948
4949out:
Wang Shilong896c14f2014-01-07 17:25:18 +08004950 if (sort_clone_roots) {
4951 for (i = 0; i < sctx->clone_roots_cnt; i++)
4952 btrfs_root_dec_send_in_progress(
4953 sctx->clone_roots[i].root);
4954 } else {
4955 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
4956 btrfs_root_dec_send_in_progress(
4957 sctx->clone_roots[i].root);
4958
4959 btrfs_root_dec_send_in_progress(send_root);
4960 }
David Sterba66ef7d62013-12-17 15:07:20 +01004961 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
4962 btrfs_root_dec_send_in_progress(sctx->parent_root);
David Sterba2c686532013-12-16 17:34:17 +01004963
Alexander Block31db9f72012-07-25 23:19:24 +02004964 kfree(arg);
4965 vfree(clone_sources_tmp);
4966
4967 if (sctx) {
4968 if (sctx->send_filp)
4969 fput(sctx->send_filp);
4970
4971 vfree(sctx->clone_roots);
4972 vfree(sctx->send_buf);
4973 vfree(sctx->read_buf);
4974
4975 name_cache_free(sctx);
4976
4977 kfree(sctx);
4978 }
4979
4980 return ret;
4981}