]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/btrfs/send.c
Merge tag 'remoteproc-for-3.6' of git://git.kernel.org/pub/scm/linux/kernel/git/ohad...
[mirror_ubuntu-hirsute-kernel.git] / fs / btrfs / send.c
CommitLineData
31db9f7c
AB
1/*
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>
28
29#include "send.h"
30#include "backref.h"
31#include "locking.h"
32#include "disk-io.h"
33#include "btrfs_inode.h"
34#include "transaction.h"
35
36static int g_verbose = 0;
37
38#define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
39
40/*
41 * A fs_path is a helper to dynamically build path names with unknown size.
42 * It reallocates the internal buffer on demand.
43 * It allows fast adding of path elements on the right side (normal path) and
44 * fast adding to the left side (reversed path). A reversed path can also be
45 * unreversed if needed.
46 */
47struct fs_path {
48 union {
49 struct {
50 char *start;
51 char *end;
52 char *prepared;
53
54 char *buf;
55 int buf_len;
56 int reversed:1;
57 int virtual_mem:1;
58 char inline_buf[];
59 };
60 char pad[PAGE_SIZE];
61 };
62};
63#define FS_PATH_INLINE_SIZE \
64 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
65
66
67/* reused for each extent */
68struct clone_root {
69 struct btrfs_root *root;
70 u64 ino;
71 u64 offset;
72
73 u64 found_refs;
74};
75
76#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
77#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
78
79struct send_ctx {
80 struct file *send_filp;
81 loff_t send_off;
82 char *send_buf;
83 u32 send_size;
84 u32 send_max_size;
85 u64 total_send_size;
86 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
87
88 struct vfsmount *mnt;
89
90 struct btrfs_root *send_root;
91 struct btrfs_root *parent_root;
92 struct clone_root *clone_roots;
93 int clone_roots_cnt;
94
95 /* current state of the compare_tree call */
96 struct btrfs_path *left_path;
97 struct btrfs_path *right_path;
98 struct btrfs_key *cmp_key;
99
100 /*
101 * infos of the currently processed inode. In case of deleted inodes,
102 * these are the values from the deleted inode.
103 */
104 u64 cur_ino;
105 u64 cur_inode_gen;
106 int cur_inode_new;
107 int cur_inode_new_gen;
108 int cur_inode_deleted;
109 int cur_inode_first_ref_orphan;
110 u64 cur_inode_size;
111 u64 cur_inode_mode;
112
113 u64 send_progress;
114
115 struct list_head new_refs;
116 struct list_head deleted_refs;
117
118 struct radix_tree_root name_cache;
119 struct list_head name_cache_list;
120 int name_cache_size;
121
122 struct file *cur_inode_filp;
123 char *read_buf;
124};
125
126struct name_cache_entry {
127 struct list_head list;
128 struct list_head use_list;
129 u64 ino;
130 u64 gen;
131 u64 parent_ino;
132 u64 parent_gen;
133 int ret;
134 int need_later_update;
135 int name_len;
136 char name[];
137};
138
139static void fs_path_reset(struct fs_path *p)
140{
141 if (p->reversed) {
142 p->start = p->buf + p->buf_len - 1;
143 p->end = p->start;
144 *p->start = 0;
145 } else {
146 p->start = p->buf;
147 p->end = p->start;
148 *p->start = 0;
149 }
150}
151
152static struct fs_path *fs_path_alloc(struct send_ctx *sctx)
153{
154 struct fs_path *p;
155
156 p = kmalloc(sizeof(*p), GFP_NOFS);
157 if (!p)
158 return NULL;
159 p->reversed = 0;
160 p->virtual_mem = 0;
161 p->buf = p->inline_buf;
162 p->buf_len = FS_PATH_INLINE_SIZE;
163 fs_path_reset(p);
164 return p;
165}
166
167static struct fs_path *fs_path_alloc_reversed(struct send_ctx *sctx)
168{
169 struct fs_path *p;
170
171 p = fs_path_alloc(sctx);
172 if (!p)
173 return NULL;
174 p->reversed = 1;
175 fs_path_reset(p);
176 return p;
177}
178
179static void fs_path_free(struct send_ctx *sctx, struct fs_path *p)
180{
181 if (!p)
182 return;
183 if (p->buf != p->inline_buf) {
184 if (p->virtual_mem)
185 vfree(p->buf);
186 else
187 kfree(p->buf);
188 }
189 kfree(p);
190}
191
192static int fs_path_len(struct fs_path *p)
193{
194 return p->end - p->start;
195}
196
197static int fs_path_ensure_buf(struct fs_path *p, int len)
198{
199 char *tmp_buf;
200 int path_len;
201 int old_buf_len;
202
203 len++;
204
205 if (p->buf_len >= len)
206 return 0;
207
208 path_len = p->end - p->start;
209 old_buf_len = p->buf_len;
210 len = PAGE_ALIGN(len);
211
212 if (p->buf == p->inline_buf) {
213 tmp_buf = kmalloc(len, GFP_NOFS);
214 if (!tmp_buf) {
215 tmp_buf = vmalloc(len);
216 if (!tmp_buf)
217 return -ENOMEM;
218 p->virtual_mem = 1;
219 }
220 memcpy(tmp_buf, p->buf, p->buf_len);
221 p->buf = tmp_buf;
222 p->buf_len = len;
223 } else {
224 if (p->virtual_mem) {
225 tmp_buf = vmalloc(len);
226 if (!tmp_buf)
227 return -ENOMEM;
228 memcpy(tmp_buf, p->buf, p->buf_len);
229 vfree(p->buf);
230 } else {
231 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
232 if (!tmp_buf) {
233 tmp_buf = vmalloc(len);
234 if (!tmp_buf)
235 return -ENOMEM;
236 memcpy(tmp_buf, p->buf, p->buf_len);
237 kfree(p->buf);
238 p->virtual_mem = 1;
239 }
240 }
241 p->buf = tmp_buf;
242 p->buf_len = len;
243 }
244 if (p->reversed) {
245 tmp_buf = p->buf + old_buf_len - path_len - 1;
246 p->end = p->buf + p->buf_len - 1;
247 p->start = p->end - path_len;
248 memmove(p->start, tmp_buf, path_len + 1);
249 } else {
250 p->start = p->buf;
251 p->end = p->start + path_len;
252 }
253 return 0;
254}
255
256static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
257{
258 int ret;
259 int new_len;
260
261 new_len = p->end - p->start + name_len;
262 if (p->start != p->end)
263 new_len++;
264 ret = fs_path_ensure_buf(p, new_len);
265 if (ret < 0)
266 goto out;
267
268 if (p->reversed) {
269 if (p->start != p->end)
270 *--p->start = '/';
271 p->start -= name_len;
272 p->prepared = p->start;
273 } else {
274 if (p->start != p->end)
275 *p->end++ = '/';
276 p->prepared = p->end;
277 p->end += name_len;
278 *p->end = 0;
279 }
280
281out:
282 return ret;
283}
284
285static int fs_path_add(struct fs_path *p, const char *name, int name_len)
286{
287 int ret;
288
289 ret = fs_path_prepare_for_add(p, name_len);
290 if (ret < 0)
291 goto out;
292 memcpy(p->prepared, name, name_len);
293 p->prepared = NULL;
294
295out:
296 return ret;
297}
298
299static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
300{
301 int ret;
302
303 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
304 if (ret < 0)
305 goto out;
306 memcpy(p->prepared, p2->start, p2->end - p2->start);
307 p->prepared = NULL;
308
309out:
310 return ret;
311}
312
313static int fs_path_add_from_extent_buffer(struct fs_path *p,
314 struct extent_buffer *eb,
315 unsigned long off, int len)
316{
317 int ret;
318
319 ret = fs_path_prepare_for_add(p, len);
320 if (ret < 0)
321 goto out;
322
323 read_extent_buffer(eb, p->prepared, off, len);
324 p->prepared = NULL;
325
326out:
327 return ret;
328}
329
330static void fs_path_remove(struct fs_path *p)
331{
332 BUG_ON(p->reversed);
333 while (p->start != p->end && *p->end != '/')
334 p->end--;
335 *p->end = 0;
336}
337
338static int fs_path_copy(struct fs_path *p, struct fs_path *from)
339{
340 int ret;
341
342 p->reversed = from->reversed;
343 fs_path_reset(p);
344
345 ret = fs_path_add_path(p, from);
346
347 return ret;
348}
349
350
351static void fs_path_unreverse(struct fs_path *p)
352{
353 char *tmp;
354 int len;
355
356 if (!p->reversed)
357 return;
358
359 tmp = p->start;
360 len = p->end - p->start;
361 p->start = p->buf;
362 p->end = p->start + len;
363 memmove(p->start, tmp, len + 1);
364 p->reversed = 0;
365}
366
367static struct btrfs_path *alloc_path_for_send(void)
368{
369 struct btrfs_path *path;
370
371 path = btrfs_alloc_path();
372 if (!path)
373 return NULL;
374 path->search_commit_root = 1;
375 path->skip_locking = 1;
376 return path;
377}
378
379static int write_buf(struct send_ctx *sctx, const void *buf, u32 len)
380{
381 int ret;
382 mm_segment_t old_fs;
383 u32 pos = 0;
384
385 old_fs = get_fs();
386 set_fs(KERNEL_DS);
387
388 while (pos < len) {
389 ret = vfs_write(sctx->send_filp, (char *)buf + pos, len - pos,
390 &sctx->send_off);
391 /* TODO handle that correctly */
392 /*if (ret == -ERESTARTSYS) {
393 continue;
394 }*/
395 if (ret < 0)
396 goto out;
397 if (ret == 0) {
398 ret = -EIO;
399 goto out;
400 }
401 pos += ret;
402 }
403
404 ret = 0;
405
406out:
407 set_fs(old_fs);
408 return ret;
409}
410
411static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
412{
413 struct btrfs_tlv_header *hdr;
414 int total_len = sizeof(*hdr) + len;
415 int left = sctx->send_max_size - sctx->send_size;
416
417 if (unlikely(left < total_len))
418 return -EOVERFLOW;
419
420 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
421 hdr->tlv_type = cpu_to_le16(attr);
422 hdr->tlv_len = cpu_to_le16(len);
423 memcpy(hdr + 1, data, len);
424 sctx->send_size += total_len;
425
426 return 0;
427}
428
429#if 0
430static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
431{
432 return tlv_put(sctx, attr, &value, sizeof(value));
433}
434
435static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
436{
437 __le16 tmp = cpu_to_le16(value);
438 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
439}
440
441static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
442{
443 __le32 tmp = cpu_to_le32(value);
444 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
445}
446#endif
447
448static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
449{
450 __le64 tmp = cpu_to_le64(value);
451 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
452}
453
454static int tlv_put_string(struct send_ctx *sctx, u16 attr,
455 const char *str, int len)
456{
457 if (len == -1)
458 len = strlen(str);
459 return tlv_put(sctx, attr, str, len);
460}
461
462static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
463 const u8 *uuid)
464{
465 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
466}
467
468#if 0
469static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
470 struct timespec *ts)
471{
472 struct btrfs_timespec bts;
473 bts.sec = cpu_to_le64(ts->tv_sec);
474 bts.nsec = cpu_to_le32(ts->tv_nsec);
475 return tlv_put(sctx, attr, &bts, sizeof(bts));
476}
477#endif
478
479static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
480 struct extent_buffer *eb,
481 struct btrfs_timespec *ts)
482{
483 struct btrfs_timespec bts;
484 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
485 return tlv_put(sctx, attr, &bts, sizeof(bts));
486}
487
488
489#define TLV_PUT(sctx, attrtype, attrlen, data) \
490 do { \
491 ret = tlv_put(sctx, attrtype, attrlen, data); \
492 if (ret < 0) \
493 goto tlv_put_failure; \
494 } while (0)
495
496#define TLV_PUT_INT(sctx, attrtype, bits, value) \
497 do { \
498 ret = tlv_put_u##bits(sctx, attrtype, value); \
499 if (ret < 0) \
500 goto tlv_put_failure; \
501 } while (0)
502
503#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
504#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
505#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
506#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
507#define TLV_PUT_STRING(sctx, attrtype, str, len) \
508 do { \
509 ret = tlv_put_string(sctx, attrtype, str, len); \
510 if (ret < 0) \
511 goto tlv_put_failure; \
512 } while (0)
513#define TLV_PUT_PATH(sctx, attrtype, p) \
514 do { \
515 ret = tlv_put_string(sctx, attrtype, p->start, \
516 p->end - p->start); \
517 if (ret < 0) \
518 goto tlv_put_failure; \
519 } while(0)
520#define TLV_PUT_UUID(sctx, attrtype, uuid) \
521 do { \
522 ret = tlv_put_uuid(sctx, attrtype, uuid); \
523 if (ret < 0) \
524 goto tlv_put_failure; \
525 } while (0)
526#define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
527 do { \
528 ret = tlv_put_timespec(sctx, attrtype, ts); \
529 if (ret < 0) \
530 goto tlv_put_failure; \
531 } while (0)
532#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
533 do { \
534 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
535 if (ret < 0) \
536 goto tlv_put_failure; \
537 } while (0)
538
539static int send_header(struct send_ctx *sctx)
540{
541 struct btrfs_stream_header hdr;
542
543 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
544 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
545
546 return write_buf(sctx, &hdr, sizeof(hdr));
547}
548
549/*
550 * For each command/item we want to send to userspace, we call this function.
551 */
552static int begin_cmd(struct send_ctx *sctx, int cmd)
553{
554 struct btrfs_cmd_header *hdr;
555
556 if (!sctx->send_buf) {
557 WARN_ON(1);
558 return -EINVAL;
559 }
560
561 BUG_ON(sctx->send_size);
562
563 sctx->send_size += sizeof(*hdr);
564 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
565 hdr->cmd = cpu_to_le16(cmd);
566
567 return 0;
568}
569
570static int send_cmd(struct send_ctx *sctx)
571{
572 int ret;
573 struct btrfs_cmd_header *hdr;
574 u32 crc;
575
576 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
577 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
578 hdr->crc = 0;
579
580 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
581 hdr->crc = cpu_to_le32(crc);
582
583 ret = write_buf(sctx, sctx->send_buf, sctx->send_size);
584
585 sctx->total_send_size += sctx->send_size;
586 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
587 sctx->send_size = 0;
588
589 return ret;
590}
591
592/*
593 * Sends a move instruction to user space
594 */
595static int send_rename(struct send_ctx *sctx,
596 struct fs_path *from, struct fs_path *to)
597{
598 int ret;
599
600verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
601
602 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
603 if (ret < 0)
604 goto out;
605
606 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
607 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
608
609 ret = send_cmd(sctx);
610
611tlv_put_failure:
612out:
613 return ret;
614}
615
616/*
617 * Sends a link instruction to user space
618 */
619static int send_link(struct send_ctx *sctx,
620 struct fs_path *path, struct fs_path *lnk)
621{
622 int ret;
623
624verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
625
626 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
627 if (ret < 0)
628 goto out;
629
630 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
631 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
632
633 ret = send_cmd(sctx);
634
635tlv_put_failure:
636out:
637 return ret;
638}
639
640/*
641 * Sends an unlink instruction to user space
642 */
643static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
644{
645 int ret;
646
647verbose_printk("btrfs: send_unlink %s\n", path->start);
648
649 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
650 if (ret < 0)
651 goto out;
652
653 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
654
655 ret = send_cmd(sctx);
656
657tlv_put_failure:
658out:
659 return ret;
660}
661
662/*
663 * Sends a rmdir instruction to user space
664 */
665static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
666{
667 int ret;
668
669verbose_printk("btrfs: send_rmdir %s\n", path->start);
670
671 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
672 if (ret < 0)
673 goto out;
674
675 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
676
677 ret = send_cmd(sctx);
678
679tlv_put_failure:
680out:
681 return ret;
682}
683
684/*
685 * Helper function to retrieve some fields from an inode item.
686 */
687static int get_inode_info(struct btrfs_root *root,
688 u64 ino, u64 *size, u64 *gen,
689 u64 *mode, u64 *uid, u64 *gid)
690{
691 int ret;
692 struct btrfs_inode_item *ii;
693 struct btrfs_key key;
694 struct btrfs_path *path;
695
696 path = alloc_path_for_send();
697 if (!path)
698 return -ENOMEM;
699
700 key.objectid = ino;
701 key.type = BTRFS_INODE_ITEM_KEY;
702 key.offset = 0;
703 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
704 if (ret < 0)
705 goto out;
706 if (ret) {
707 ret = -ENOENT;
708 goto out;
709 }
710
711 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
712 struct btrfs_inode_item);
713 if (size)
714 *size = btrfs_inode_size(path->nodes[0], ii);
715 if (gen)
716 *gen = btrfs_inode_generation(path->nodes[0], ii);
717 if (mode)
718 *mode = btrfs_inode_mode(path->nodes[0], ii);
719 if (uid)
720 *uid = btrfs_inode_uid(path->nodes[0], ii);
721 if (gid)
722 *gid = btrfs_inode_gid(path->nodes[0], ii);
723
724out:
725 btrfs_free_path(path);
726 return ret;
727}
728
729typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
730 struct fs_path *p,
731 void *ctx);
732
733/*
734 * Helper function to iterate the entries in ONE btrfs_inode_ref.
735 * The iterate callback may return a non zero value to stop iteration. This can
736 * be a negative value for error codes or 1 to simply stop it.
737 *
738 * path must point to the INODE_REF when called.
739 */
740static int iterate_inode_ref(struct send_ctx *sctx,
741 struct btrfs_root *root, struct btrfs_path *path,
742 struct btrfs_key *found_key, int resolve,
743 iterate_inode_ref_t iterate, void *ctx)
744{
745 struct extent_buffer *eb;
746 struct btrfs_item *item;
747 struct btrfs_inode_ref *iref;
748 struct btrfs_path *tmp_path;
749 struct fs_path *p;
750 u32 cur;
751 u32 len;
752 u32 total;
753 int slot;
754 u32 name_len;
755 char *start;
756 int ret = 0;
757 int num;
758 int index;
759
760 p = fs_path_alloc_reversed(sctx);
761 if (!p)
762 return -ENOMEM;
763
764 tmp_path = alloc_path_for_send();
765 if (!tmp_path) {
766 fs_path_free(sctx, p);
767 return -ENOMEM;
768 }
769
770 eb = path->nodes[0];
771 slot = path->slots[0];
772 item = btrfs_item_nr(eb, slot);
773 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
774 cur = 0;
775 len = 0;
776 total = btrfs_item_size(eb, item);
777
778 num = 0;
779 while (cur < total) {
780 fs_path_reset(p);
781
782 name_len = btrfs_inode_ref_name_len(eb, iref);
783 index = btrfs_inode_ref_index(eb, iref);
784 if (resolve) {
785 start = btrfs_iref_to_path(root, tmp_path, iref, eb,
786 found_key->offset, p->buf,
787 p->buf_len);
788 if (IS_ERR(start)) {
789 ret = PTR_ERR(start);
790 goto out;
791 }
792 if (start < p->buf) {
793 /* overflow , try again with larger buffer */
794 ret = fs_path_ensure_buf(p,
795 p->buf_len + p->buf - start);
796 if (ret < 0)
797 goto out;
798 start = btrfs_iref_to_path(root, tmp_path, iref,
799 eb, found_key->offset, p->buf,
800 p->buf_len);
801 if (IS_ERR(start)) {
802 ret = PTR_ERR(start);
803 goto out;
804 }
805 BUG_ON(start < p->buf);
806 }
807 p->start = start;
808 } else {
809 ret = fs_path_add_from_extent_buffer(p, eb,
810 (unsigned long)(iref + 1), name_len);
811 if (ret < 0)
812 goto out;
813 }
814
815
816 len = sizeof(*iref) + name_len;
817 iref = (struct btrfs_inode_ref *)((char *)iref + len);
818 cur += len;
819
820 ret = iterate(num, found_key->offset, index, p, ctx);
821 if (ret)
822 goto out;
823
824 num++;
825 }
826
827out:
828 btrfs_free_path(tmp_path);
829 fs_path_free(sctx, p);
830 return ret;
831}
832
833typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
834 const char *name, int name_len,
835 const char *data, int data_len,
836 u8 type, void *ctx);
837
838/*
839 * Helper function to iterate the entries in ONE btrfs_dir_item.
840 * The iterate callback may return a non zero value to stop iteration. This can
841 * be a negative value for error codes or 1 to simply stop it.
842 *
843 * path must point to the dir item when called.
844 */
845static int iterate_dir_item(struct send_ctx *sctx,
846 struct btrfs_root *root, struct btrfs_path *path,
847 struct btrfs_key *found_key,
848 iterate_dir_item_t iterate, void *ctx)
849{
850 int ret = 0;
851 struct extent_buffer *eb;
852 struct btrfs_item *item;
853 struct btrfs_dir_item *di;
854 struct btrfs_path *tmp_path = NULL;
855 struct btrfs_key di_key;
856 char *buf = NULL;
857 char *buf2 = NULL;
858 int buf_len;
859 int buf_virtual = 0;
860 u32 name_len;
861 u32 data_len;
862 u32 cur;
863 u32 len;
864 u32 total;
865 int slot;
866 int num;
867 u8 type;
868
869 buf_len = PAGE_SIZE;
870 buf = kmalloc(buf_len, GFP_NOFS);
871 if (!buf) {
872 ret = -ENOMEM;
873 goto out;
874 }
875
876 tmp_path = alloc_path_for_send();
877 if (!tmp_path) {
878 ret = -ENOMEM;
879 goto out;
880 }
881
882 eb = path->nodes[0];
883 slot = path->slots[0];
884 item = btrfs_item_nr(eb, slot);
885 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
886 cur = 0;
887 len = 0;
888 total = btrfs_item_size(eb, item);
889
890 num = 0;
891 while (cur < total) {
892 name_len = btrfs_dir_name_len(eb, di);
893 data_len = btrfs_dir_data_len(eb, di);
894 type = btrfs_dir_type(eb, di);
895 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
896
897 if (name_len + data_len > buf_len) {
898 buf_len = PAGE_ALIGN(name_len + data_len);
899 if (buf_virtual) {
900 buf2 = vmalloc(buf_len);
901 if (!buf2) {
902 ret = -ENOMEM;
903 goto out;
904 }
905 vfree(buf);
906 } else {
907 buf2 = krealloc(buf, buf_len, GFP_NOFS);
908 if (!buf2) {
909 buf2 = vmalloc(buf_len);
910 if (!buf2) {
911 ret = -ENOMEM;
912 goto out;
913 }
914 kfree(buf);
915 buf_virtual = 1;
916 }
917 }
918
919 buf = buf2;
920 buf2 = NULL;
921 }
922
923 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
924 name_len + data_len);
925
926 len = sizeof(*di) + name_len + data_len;
927 di = (struct btrfs_dir_item *)((char *)di + len);
928 cur += len;
929
930 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
931 data_len, type, ctx);
932 if (ret < 0)
933 goto out;
934 if (ret) {
935 ret = 0;
936 goto out;
937 }
938
939 num++;
940 }
941
942out:
943 btrfs_free_path(tmp_path);
944 if (buf_virtual)
945 vfree(buf);
946 else
947 kfree(buf);
948 return ret;
949}
950
951static int __copy_first_ref(int num, u64 dir, int index,
952 struct fs_path *p, void *ctx)
953{
954 int ret;
955 struct fs_path *pt = ctx;
956
957 ret = fs_path_copy(pt, p);
958 if (ret < 0)
959 return ret;
960
961 /* we want the first only */
962 return 1;
963}
964
965/*
966 * Retrieve the first path of an inode. If an inode has more then one
967 * ref/hardlink, this is ignored.
968 */
969static int get_inode_path(struct send_ctx *sctx, struct btrfs_root *root,
970 u64 ino, struct fs_path *path)
971{
972 int ret;
973 struct btrfs_key key, found_key;
974 struct btrfs_path *p;
975
976 p = alloc_path_for_send();
977 if (!p)
978 return -ENOMEM;
979
980 fs_path_reset(path);
981
982 key.objectid = ino;
983 key.type = BTRFS_INODE_REF_KEY;
984 key.offset = 0;
985
986 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
987 if (ret < 0)
988 goto out;
989 if (ret) {
990 ret = 1;
991 goto out;
992 }
993 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
994 if (found_key.objectid != ino ||
995 found_key.type != BTRFS_INODE_REF_KEY) {
996 ret = -ENOENT;
997 goto out;
998 }
999
1000 ret = iterate_inode_ref(sctx, root, p, &found_key, 1,
1001 __copy_first_ref, path);
1002 if (ret < 0)
1003 goto out;
1004 ret = 0;
1005
1006out:
1007 btrfs_free_path(p);
1008 return ret;
1009}
1010
1011struct backref_ctx {
1012 struct send_ctx *sctx;
1013
1014 /* number of total found references */
1015 u64 found;
1016
1017 /*
1018 * used for clones found in send_root. clones found behind cur_objectid
1019 * and cur_offset are not considered as allowed clones.
1020 */
1021 u64 cur_objectid;
1022 u64 cur_offset;
1023
1024 /* may be truncated in case it's the last extent in a file */
1025 u64 extent_len;
1026
1027 /* Just to check for bugs in backref resolving */
1028 int found_in_send_root;
1029};
1030
1031static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1032{
1033 u64 root = (u64)key;
1034 struct clone_root *cr = (struct clone_root *)elt;
1035
1036 if (root < cr->root->objectid)
1037 return -1;
1038 if (root > cr->root->objectid)
1039 return 1;
1040 return 0;
1041}
1042
1043static int __clone_root_cmp_sort(const void *e1, const void *e2)
1044{
1045 struct clone_root *cr1 = (struct clone_root *)e1;
1046 struct clone_root *cr2 = (struct clone_root *)e2;
1047
1048 if (cr1->root->objectid < cr2->root->objectid)
1049 return -1;
1050 if (cr1->root->objectid > cr2->root->objectid)
1051 return 1;
1052 return 0;
1053}
1054
1055/*
1056 * Called for every backref that is found for the current extent.
1057 */
1058static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1059{
1060 struct backref_ctx *bctx = ctx_;
1061 struct clone_root *found;
1062 int ret;
1063 u64 i_size;
1064
1065 /* First check if the root is in the list of accepted clone sources */
1066 found = bsearch((void *)root, bctx->sctx->clone_roots,
1067 bctx->sctx->clone_roots_cnt,
1068 sizeof(struct clone_root),
1069 __clone_root_cmp_bsearch);
1070 if (!found)
1071 return 0;
1072
1073 if (found->root == bctx->sctx->send_root &&
1074 ino == bctx->cur_objectid &&
1075 offset == bctx->cur_offset) {
1076 bctx->found_in_send_root = 1;
1077 }
1078
1079 /*
1080 * There are inodes that have extents that lie behind it's i_size. Don't
1081 * accept clones from these extents.
1082 */
1083 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL);
1084 if (ret < 0)
1085 return ret;
1086
1087 if (offset + bctx->extent_len > i_size)
1088 return 0;
1089
1090 /*
1091 * Make sure we don't consider clones from send_root that are
1092 * behind the current inode/offset.
1093 */
1094 if (found->root == bctx->sctx->send_root) {
1095 /*
1096 * TODO for the moment we don't accept clones from the inode
1097 * that is currently send. We may change this when
1098 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1099 * file.
1100 */
1101 if (ino >= bctx->cur_objectid)
1102 return 0;
1103 /*if (ino > ctx->cur_objectid)
1104 return 0;
1105 if (offset + ctx->extent_len > ctx->cur_offset)
1106 return 0;*/
1107
1108 bctx->found++;
1109 found->found_refs++;
1110 found->ino = ino;
1111 found->offset = offset;
1112 return 0;
1113 }
1114
1115 bctx->found++;
1116 found->found_refs++;
1117 if (ino < found->ino) {
1118 found->ino = ino;
1119 found->offset = offset;
1120 } else if (found->ino == ino) {
1121 /*
1122 * same extent found more then once in the same file.
1123 */
1124 if (found->offset > offset + bctx->extent_len)
1125 found->offset = offset;
1126 }
1127
1128 return 0;
1129}
1130
1131/*
1132 * path must point to the extent item when called.
1133 */
1134static int find_extent_clone(struct send_ctx *sctx,
1135 struct btrfs_path *path,
1136 u64 ino, u64 data_offset,
1137 u64 ino_size,
1138 struct clone_root **found)
1139{
1140 int ret;
1141 int extent_type;
1142 u64 logical;
1143 u64 num_bytes;
1144 u64 extent_item_pos;
1145 struct btrfs_file_extent_item *fi;
1146 struct extent_buffer *eb = path->nodes[0];
1147 struct backref_ctx backref_ctx;
1148 struct clone_root *cur_clone_root;
1149 struct btrfs_key found_key;
1150 struct btrfs_path *tmp_path;
1151 u32 i;
1152
1153 tmp_path = alloc_path_for_send();
1154 if (!tmp_path)
1155 return -ENOMEM;
1156
1157 if (data_offset >= ino_size) {
1158 /*
1159 * There may be extents that lie behind the file's size.
1160 * I at least had this in combination with snapshotting while
1161 * writing large files.
1162 */
1163 ret = 0;
1164 goto out;
1165 }
1166
1167 fi = btrfs_item_ptr(eb, path->slots[0],
1168 struct btrfs_file_extent_item);
1169 extent_type = btrfs_file_extent_type(eb, fi);
1170 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1171 ret = -ENOENT;
1172 goto out;
1173 }
1174
1175 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1176 logical = btrfs_file_extent_disk_bytenr(eb, fi);
1177 if (logical == 0) {
1178 ret = -ENOENT;
1179 goto out;
1180 }
1181 logical += btrfs_file_extent_offset(eb, fi);
1182
1183 ret = extent_from_logical(sctx->send_root->fs_info,
1184 logical, tmp_path, &found_key);
1185 btrfs_release_path(tmp_path);
1186
1187 if (ret < 0)
1188 goto out;
1189 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1190 ret = -EIO;
1191 goto out;
1192 }
1193
1194 /*
1195 * Setup the clone roots.
1196 */
1197 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1198 cur_clone_root = sctx->clone_roots + i;
1199 cur_clone_root->ino = (u64)-1;
1200 cur_clone_root->offset = 0;
1201 cur_clone_root->found_refs = 0;
1202 }
1203
1204 backref_ctx.sctx = sctx;
1205 backref_ctx.found = 0;
1206 backref_ctx.cur_objectid = ino;
1207 backref_ctx.cur_offset = data_offset;
1208 backref_ctx.found_in_send_root = 0;
1209 backref_ctx.extent_len = num_bytes;
1210
1211 /*
1212 * The last extent of a file may be too large due to page alignment.
1213 * We need to adjust extent_len in this case so that the checks in
1214 * __iterate_backrefs work.
1215 */
1216 if (data_offset + num_bytes >= ino_size)
1217 backref_ctx.extent_len = ino_size - data_offset;
1218
1219 /*
1220 * Now collect all backrefs.
1221 */
1222 extent_item_pos = logical - found_key.objectid;
1223 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1224 found_key.objectid, extent_item_pos, 1,
1225 __iterate_backrefs, &backref_ctx);
1226 if (ret < 0)
1227 goto out;
1228
1229 if (!backref_ctx.found_in_send_root) {
1230 /* found a bug in backref code? */
1231 ret = -EIO;
1232 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1233 "send_root. inode=%llu, offset=%llu, "
1234 "logical=%llu\n",
1235 ino, data_offset, logical);
1236 goto out;
1237 }
1238
1239verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1240 "ino=%llu, "
1241 "num_bytes=%llu, logical=%llu\n",
1242 data_offset, ino, num_bytes, logical);
1243
1244 if (!backref_ctx.found)
1245 verbose_printk("btrfs: no clones found\n");
1246
1247 cur_clone_root = NULL;
1248 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1249 if (sctx->clone_roots[i].found_refs) {
1250 if (!cur_clone_root)
1251 cur_clone_root = sctx->clone_roots + i;
1252 else if (sctx->clone_roots[i].root == sctx->send_root)
1253 /* prefer clones from send_root over others */
1254 cur_clone_root = sctx->clone_roots + i;
1255 break;
1256 }
1257
1258 }
1259
1260 if (cur_clone_root) {
1261 *found = cur_clone_root;
1262 ret = 0;
1263 } else {
1264 ret = -ENOENT;
1265 }
1266
1267out:
1268 btrfs_free_path(tmp_path);
1269 return ret;
1270}
1271
1272static int read_symlink(struct send_ctx *sctx,
1273 struct btrfs_root *root,
1274 u64 ino,
1275 struct fs_path *dest)
1276{
1277 int ret;
1278 struct btrfs_path *path;
1279 struct btrfs_key key;
1280 struct btrfs_file_extent_item *ei;
1281 u8 type;
1282 u8 compression;
1283 unsigned long off;
1284 int len;
1285
1286 path = alloc_path_for_send();
1287 if (!path)
1288 return -ENOMEM;
1289
1290 key.objectid = ino;
1291 key.type = BTRFS_EXTENT_DATA_KEY;
1292 key.offset = 0;
1293 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1294 if (ret < 0)
1295 goto out;
1296 BUG_ON(ret);
1297
1298 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1299 struct btrfs_file_extent_item);
1300 type = btrfs_file_extent_type(path->nodes[0], ei);
1301 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1302 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1303 BUG_ON(compression);
1304
1305 off = btrfs_file_extent_inline_start(ei);
1306 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1307
1308 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1309 if (ret < 0)
1310 goto out;
1311
1312out:
1313 btrfs_free_path(path);
1314 return ret;
1315}
1316
1317/*
1318 * Helper function to generate a file name that is unique in the root of
1319 * send_root and parent_root. This is used to generate names for orphan inodes.
1320 */
1321static int gen_unique_name(struct send_ctx *sctx,
1322 u64 ino, u64 gen,
1323 struct fs_path *dest)
1324{
1325 int ret = 0;
1326 struct btrfs_path *path;
1327 struct btrfs_dir_item *di;
1328 char tmp[64];
1329 int len;
1330 u64 idx = 0;
1331
1332 path = alloc_path_for_send();
1333 if (!path)
1334 return -ENOMEM;
1335
1336 while (1) {
1337 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1338 ino, gen, idx);
1339 if (len >= sizeof(tmp)) {
1340 /* should really not happen */
1341 ret = -EOVERFLOW;
1342 goto out;
1343 }
1344
1345 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1346 path, BTRFS_FIRST_FREE_OBJECTID,
1347 tmp, strlen(tmp), 0);
1348 btrfs_release_path(path);
1349 if (IS_ERR(di)) {
1350 ret = PTR_ERR(di);
1351 goto out;
1352 }
1353 if (di) {
1354 /* not unique, try again */
1355 idx++;
1356 continue;
1357 }
1358
1359 if (!sctx->parent_root) {
1360 /* unique */
1361 ret = 0;
1362 break;
1363 }
1364
1365 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1366 path, BTRFS_FIRST_FREE_OBJECTID,
1367 tmp, strlen(tmp), 0);
1368 btrfs_release_path(path);
1369 if (IS_ERR(di)) {
1370 ret = PTR_ERR(di);
1371 goto out;
1372 }
1373 if (di) {
1374 /* not unique, try again */
1375 idx++;
1376 continue;
1377 }
1378 /* unique */
1379 break;
1380 }
1381
1382 ret = fs_path_add(dest, tmp, strlen(tmp));
1383
1384out:
1385 btrfs_free_path(path);
1386 return ret;
1387}
1388
1389enum inode_state {
1390 inode_state_no_change,
1391 inode_state_will_create,
1392 inode_state_did_create,
1393 inode_state_will_delete,
1394 inode_state_did_delete,
1395};
1396
1397static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1398{
1399 int ret;
1400 int left_ret;
1401 int right_ret;
1402 u64 left_gen;
1403 u64 right_gen;
1404
1405 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1406 NULL);
1407 if (ret < 0 && ret != -ENOENT)
1408 goto out;
1409 left_ret = ret;
1410
1411 if (!sctx->parent_root) {
1412 right_ret = -ENOENT;
1413 } else {
1414 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1415 NULL, NULL, NULL);
1416 if (ret < 0 && ret != -ENOENT)
1417 goto out;
1418 right_ret = ret;
1419 }
1420
1421 if (!left_ret && !right_ret) {
1422 if (left_gen == gen && right_gen == gen)
1423 ret = inode_state_no_change;
1424 else if (left_gen == gen) {
1425 if (ino < sctx->send_progress)
1426 ret = inode_state_did_create;
1427 else
1428 ret = inode_state_will_create;
1429 } else if (right_gen == gen) {
1430 if (ino < sctx->send_progress)
1431 ret = inode_state_did_delete;
1432 else
1433 ret = inode_state_will_delete;
1434 } else {
1435 ret = -ENOENT;
1436 }
1437 } else if (!left_ret) {
1438 if (left_gen == gen) {
1439 if (ino < sctx->send_progress)
1440 ret = inode_state_did_create;
1441 else
1442 ret = inode_state_will_create;
1443 } else {
1444 ret = -ENOENT;
1445 }
1446 } else if (!right_ret) {
1447 if (right_gen == gen) {
1448 if (ino < sctx->send_progress)
1449 ret = inode_state_did_delete;
1450 else
1451 ret = inode_state_will_delete;
1452 } else {
1453 ret = -ENOENT;
1454 }
1455 } else {
1456 ret = -ENOENT;
1457 }
1458
1459out:
1460 return ret;
1461}
1462
1463static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1464{
1465 int ret;
1466
1467 ret = get_cur_inode_state(sctx, ino, gen);
1468 if (ret < 0)
1469 goto out;
1470
1471 if (ret == inode_state_no_change ||
1472 ret == inode_state_did_create ||
1473 ret == inode_state_will_delete)
1474 ret = 1;
1475 else
1476 ret = 0;
1477
1478out:
1479 return ret;
1480}
1481
1482/*
1483 * Helper function to lookup a dir item in a dir.
1484 */
1485static int lookup_dir_item_inode(struct btrfs_root *root,
1486 u64 dir, const char *name, int name_len,
1487 u64 *found_inode,
1488 u8 *found_type)
1489{
1490 int ret = 0;
1491 struct btrfs_dir_item *di;
1492 struct btrfs_key key;
1493 struct btrfs_path *path;
1494
1495 path = alloc_path_for_send();
1496 if (!path)
1497 return -ENOMEM;
1498
1499 di = btrfs_lookup_dir_item(NULL, root, path,
1500 dir, name, name_len, 0);
1501 if (!di) {
1502 ret = -ENOENT;
1503 goto out;
1504 }
1505 if (IS_ERR(di)) {
1506 ret = PTR_ERR(di);
1507 goto out;
1508 }
1509 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1510 *found_inode = key.objectid;
1511 *found_type = btrfs_dir_type(path->nodes[0], di);
1512
1513out:
1514 btrfs_free_path(path);
1515 return ret;
1516}
1517
1518static int get_first_ref(struct send_ctx *sctx,
1519 struct btrfs_root *root, u64 ino,
1520 u64 *dir, u64 *dir_gen, struct fs_path *name)
1521{
1522 int ret;
1523 struct btrfs_key key;
1524 struct btrfs_key found_key;
1525 struct btrfs_path *path;
1526 struct btrfs_inode_ref *iref;
1527 int len;
1528
1529 path = alloc_path_for_send();
1530 if (!path)
1531 return -ENOMEM;
1532
1533 key.objectid = ino;
1534 key.type = BTRFS_INODE_REF_KEY;
1535 key.offset = 0;
1536
1537 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1538 if (ret < 0)
1539 goto out;
1540 if (!ret)
1541 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1542 path->slots[0]);
1543 if (ret || found_key.objectid != key.objectid ||
1544 found_key.type != key.type) {
1545 ret = -ENOENT;
1546 goto out;
1547 }
1548
1549 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1550 struct btrfs_inode_ref);
1551 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1552 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1553 (unsigned long)(iref + 1), len);
1554 if (ret < 0)
1555 goto out;
1556 btrfs_release_path(path);
1557
1558 ret = get_inode_info(root, found_key.offset, NULL, dir_gen, NULL, NULL,
1559 NULL);
1560 if (ret < 0)
1561 goto out;
1562
1563 *dir = found_key.offset;
1564
1565out:
1566 btrfs_free_path(path);
1567 return ret;
1568}
1569
1570static int is_first_ref(struct send_ctx *sctx,
1571 struct btrfs_root *root,
1572 u64 ino, u64 dir,
1573 const char *name, int name_len)
1574{
1575 int ret;
1576 struct fs_path *tmp_name;
1577 u64 tmp_dir;
1578 u64 tmp_dir_gen;
1579
1580 tmp_name = fs_path_alloc(sctx);
1581 if (!tmp_name)
1582 return -ENOMEM;
1583
1584 ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1585 if (ret < 0)
1586 goto out;
1587
1588 if (name_len != fs_path_len(tmp_name)) {
1589 ret = 0;
1590 goto out;
1591 }
1592
1593 ret = memcmp(tmp_name->start, name, name_len);
1594 if (ret)
1595 ret = 0;
1596 else
1597 ret = 1;
1598
1599out:
1600 fs_path_free(sctx, tmp_name);
1601 return ret;
1602}
1603
1604static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1605 const char *name, int name_len,
1606 u64 *who_ino, u64 *who_gen)
1607{
1608 int ret = 0;
1609 u64 other_inode = 0;
1610 u8 other_type = 0;
1611
1612 if (!sctx->parent_root)
1613 goto out;
1614
1615 ret = is_inode_existent(sctx, dir, dir_gen);
1616 if (ret <= 0)
1617 goto out;
1618
1619 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1620 &other_inode, &other_type);
1621 if (ret < 0 && ret != -ENOENT)
1622 goto out;
1623 if (ret) {
1624 ret = 0;
1625 goto out;
1626 }
1627
1628 if (other_inode > sctx->send_progress) {
1629 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1630 who_gen, NULL, NULL, NULL);
1631 if (ret < 0)
1632 goto out;
1633
1634 ret = 1;
1635 *who_ino = other_inode;
1636 } else {
1637 ret = 0;
1638 }
1639
1640out:
1641 return ret;
1642}
1643
1644static int did_overwrite_ref(struct send_ctx *sctx,
1645 u64 dir, u64 dir_gen,
1646 u64 ino, u64 ino_gen,
1647 const char *name, int name_len)
1648{
1649 int ret = 0;
1650 u64 gen;
1651 u64 ow_inode;
1652 u8 other_type;
1653
1654 if (!sctx->parent_root)
1655 goto out;
1656
1657 ret = is_inode_existent(sctx, dir, dir_gen);
1658 if (ret <= 0)
1659 goto out;
1660
1661 /* check if the ref was overwritten by another ref */
1662 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1663 &ow_inode, &other_type);
1664 if (ret < 0 && ret != -ENOENT)
1665 goto out;
1666 if (ret) {
1667 /* was never and will never be overwritten */
1668 ret = 0;
1669 goto out;
1670 }
1671
1672 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1673 NULL);
1674 if (ret < 0)
1675 goto out;
1676
1677 if (ow_inode == ino && gen == ino_gen) {
1678 ret = 0;
1679 goto out;
1680 }
1681
1682 /* we know that it is or will be overwritten. check this now */
1683 if (ow_inode < sctx->send_progress)
1684 ret = 1;
1685 else
1686 ret = 0;
1687
1688out:
1689 return ret;
1690}
1691
1692static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1693{
1694 int ret = 0;
1695 struct fs_path *name = NULL;
1696 u64 dir;
1697 u64 dir_gen;
1698
1699 if (!sctx->parent_root)
1700 goto out;
1701
1702 name = fs_path_alloc(sctx);
1703 if (!name)
1704 return -ENOMEM;
1705
1706 ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name);
1707 if (ret < 0)
1708 goto out;
1709
1710 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1711 name->start, fs_path_len(name));
1712 if (ret < 0)
1713 goto out;
1714
1715out:
1716 fs_path_free(sctx, name);
1717 return ret;
1718}
1719
1720static int name_cache_insert(struct send_ctx *sctx,
1721 struct name_cache_entry *nce)
1722{
1723 int ret = 0;
1724 struct name_cache_entry **ncea;
1725
1726 ncea = radix_tree_lookup(&sctx->name_cache, nce->ino);
1727 if (ncea) {
1728 if (!ncea[0])
1729 ncea[0] = nce;
1730 else if (!ncea[1])
1731 ncea[1] = nce;
1732 else
1733 BUG();
1734 } else {
1735 ncea = kmalloc(sizeof(void *) * 2, GFP_NOFS);
1736 if (!ncea)
1737 return -ENOMEM;
1738
1739 ncea[0] = nce;
1740 ncea[1] = NULL;
1741 ret = radix_tree_insert(&sctx->name_cache, nce->ino, ncea);
1742 if (ret < 0)
1743 return ret;
1744 }
1745 list_add_tail(&nce->list, &sctx->name_cache_list);
1746 sctx->name_cache_size++;
1747
1748 return ret;
1749}
1750
1751static void name_cache_delete(struct send_ctx *sctx,
1752 struct name_cache_entry *nce)
1753{
1754 struct name_cache_entry **ncea;
1755
1756 ncea = radix_tree_lookup(&sctx->name_cache, nce->ino);
1757 BUG_ON(!ncea);
1758
1759 if (ncea[0] == nce)
1760 ncea[0] = NULL;
1761 else if (ncea[1] == nce)
1762 ncea[1] = NULL;
1763 else
1764 BUG();
1765
1766 if (!ncea[0] && !ncea[1]) {
1767 radix_tree_delete(&sctx->name_cache, nce->ino);
1768 kfree(ncea);
1769 }
1770
1771 list_del(&nce->list);
1772
1773 sctx->name_cache_size--;
1774}
1775
1776static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1777 u64 ino, u64 gen)
1778{
1779 struct name_cache_entry **ncea;
1780
1781 ncea = radix_tree_lookup(&sctx->name_cache, ino);
1782 if (!ncea)
1783 return NULL;
1784
1785 if (ncea[0] && ncea[0]->gen == gen)
1786 return ncea[0];
1787 else if (ncea[1] && ncea[1]->gen == gen)
1788 return ncea[1];
1789 return NULL;
1790}
1791
1792static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1793{
1794 list_del(&nce->list);
1795 list_add_tail(&nce->list, &sctx->name_cache_list);
1796}
1797
1798static void name_cache_clean_unused(struct send_ctx *sctx)
1799{
1800 struct name_cache_entry *nce;
1801
1802 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1803 return;
1804
1805 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1806 nce = list_entry(sctx->name_cache_list.next,
1807 struct name_cache_entry, list);
1808 name_cache_delete(sctx, nce);
1809 kfree(nce);
1810 }
1811}
1812
1813static void name_cache_free(struct send_ctx *sctx)
1814{
1815 struct name_cache_entry *nce;
1816 struct name_cache_entry *tmp;
1817
1818 list_for_each_entry_safe(nce, tmp, &sctx->name_cache_list, list) {
1819 name_cache_delete(sctx, nce);
1820 }
1821}
1822
1823static int __get_cur_name_and_parent(struct send_ctx *sctx,
1824 u64 ino, u64 gen,
1825 u64 *parent_ino,
1826 u64 *parent_gen,
1827 struct fs_path *dest)
1828{
1829 int ret;
1830 int nce_ret;
1831 struct btrfs_path *path = NULL;
1832 struct name_cache_entry *nce = NULL;
1833
1834 nce = name_cache_search(sctx, ino, gen);
1835 if (nce) {
1836 if (ino < sctx->send_progress && nce->need_later_update) {
1837 name_cache_delete(sctx, nce);
1838 kfree(nce);
1839 nce = NULL;
1840 } else {
1841 name_cache_used(sctx, nce);
1842 *parent_ino = nce->parent_ino;
1843 *parent_gen = nce->parent_gen;
1844 ret = fs_path_add(dest, nce->name, nce->name_len);
1845 if (ret < 0)
1846 goto out;
1847 ret = nce->ret;
1848 goto out;
1849 }
1850 }
1851
1852 path = alloc_path_for_send();
1853 if (!path)
1854 return -ENOMEM;
1855
1856 ret = is_inode_existent(sctx, ino, gen);
1857 if (ret < 0)
1858 goto out;
1859
1860 if (!ret) {
1861 ret = gen_unique_name(sctx, ino, gen, dest);
1862 if (ret < 0)
1863 goto out;
1864 ret = 1;
1865 goto out_cache;
1866 }
1867
1868 if (ino < sctx->send_progress)
1869 ret = get_first_ref(sctx, sctx->send_root, ino,
1870 parent_ino, parent_gen, dest);
1871 else
1872 ret = get_first_ref(sctx, sctx->parent_root, ino,
1873 parent_ino, parent_gen, dest);
1874 if (ret < 0)
1875 goto out;
1876
1877 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1878 dest->start, dest->end - dest->start);
1879 if (ret < 0)
1880 goto out;
1881 if (ret) {
1882 fs_path_reset(dest);
1883 ret = gen_unique_name(sctx, ino, gen, dest);
1884 if (ret < 0)
1885 goto out;
1886 ret = 1;
1887 }
1888
1889out_cache:
1890 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
1891 if (!nce) {
1892 ret = -ENOMEM;
1893 goto out;
1894 }
1895
1896 nce->ino = ino;
1897 nce->gen = gen;
1898 nce->parent_ino = *parent_ino;
1899 nce->parent_gen = *parent_gen;
1900 nce->name_len = fs_path_len(dest);
1901 nce->ret = ret;
1902 strcpy(nce->name, dest->start);
1903 memset(&nce->use_list, 0, sizeof(nce->use_list));
1904
1905 if (ino < sctx->send_progress)
1906 nce->need_later_update = 0;
1907 else
1908 nce->need_later_update = 1;
1909
1910 nce_ret = name_cache_insert(sctx, nce);
1911 if (nce_ret < 0)
1912 ret = nce_ret;
1913 name_cache_clean_unused(sctx);
1914
1915out:
1916 btrfs_free_path(path);
1917 return ret;
1918}
1919
1920/*
1921 * Magic happens here. This function returns the first ref to an inode as it
1922 * would look like while receiving the stream at this point in time.
1923 * We walk the path up to the root. For every inode in between, we check if it
1924 * was already processed/sent. If yes, we continue with the parent as found
1925 * in send_root. If not, we continue with the parent as found in parent_root.
1926 * If we encounter an inode that was deleted at this point in time, we use the
1927 * inodes "orphan" name instead of the real name and stop. Same with new inodes
1928 * that were not created yet and overwritten inodes/refs.
1929 *
1930 * When do we have have orphan inodes:
1931 * 1. When an inode is freshly created and thus no valid refs are available yet
1932 * 2. When a directory lost all it's refs (deleted) but still has dir items
1933 * inside which were not processed yet (pending for move/delete). If anyone
1934 * tried to get the path to the dir items, it would get a path inside that
1935 * orphan directory.
1936 * 3. When an inode is moved around or gets new links, it may overwrite the ref
1937 * of an unprocessed inode. If in that case the first ref would be
1938 * overwritten, the overwritten inode gets "orphanized". Later when we
1939 * process this overwritten inode, it is restored at a new place by moving
1940 * the orphan inode.
1941 *
1942 * sctx->send_progress tells this function at which point in time receiving
1943 * would be.
1944 */
1945static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
1946 struct fs_path *dest)
1947{
1948 int ret = 0;
1949 struct fs_path *name = NULL;
1950 u64 parent_inode = 0;
1951 u64 parent_gen = 0;
1952 int stop = 0;
1953
1954 name = fs_path_alloc(sctx);
1955 if (!name) {
1956 ret = -ENOMEM;
1957 goto out;
1958 }
1959
1960 dest->reversed = 1;
1961 fs_path_reset(dest);
1962
1963 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
1964 fs_path_reset(name);
1965
1966 ret = __get_cur_name_and_parent(sctx, ino, gen,
1967 &parent_inode, &parent_gen, name);
1968 if (ret < 0)
1969 goto out;
1970 if (ret)
1971 stop = 1;
1972
1973 ret = fs_path_add_path(dest, name);
1974 if (ret < 0)
1975 goto out;
1976
1977 ino = parent_inode;
1978 gen = parent_gen;
1979 }
1980
1981out:
1982 fs_path_free(sctx, name);
1983 if (!ret)
1984 fs_path_unreverse(dest);
1985 return ret;
1986}
1987
1988/*
1989 * Called for regular files when sending extents data. Opens a struct file
1990 * to read from the file.
1991 */
1992static int open_cur_inode_file(struct send_ctx *sctx)
1993{
1994 int ret = 0;
1995 struct btrfs_key key;
e2aed8df 1996 struct path path;
31db9f7c
AB
1997 struct inode *inode;
1998 struct dentry *dentry;
1999 struct file *filp;
2000 int new = 0;
2001
2002 if (sctx->cur_inode_filp)
2003 goto out;
2004
2005 key.objectid = sctx->cur_ino;
2006 key.type = BTRFS_INODE_ITEM_KEY;
2007 key.offset = 0;
2008
2009 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2010 &new);
2011 if (IS_ERR(inode)) {
2012 ret = PTR_ERR(inode);
2013 goto out;
2014 }
2015
2016 dentry = d_obtain_alias(inode);
2017 inode = NULL;
2018 if (IS_ERR(dentry)) {
2019 ret = PTR_ERR(dentry);
2020 goto out;
2021 }
2022
e2aed8df
LT
2023 path.mnt = sctx->mnt;
2024 path.dentry = dentry;
2025 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2026 dput(dentry);
31db9f7c 2027 dentry = NULL;
31db9f7c
AB
2028 if (IS_ERR(filp)) {
2029 ret = PTR_ERR(filp);
2030 goto out;
2031 }
2032 sctx->cur_inode_filp = filp;
2033
2034out:
2035 /*
2036 * no xxxput required here as every vfs op
2037 * does it by itself on failure
2038 */
2039 return ret;
2040}
2041
2042/*
2043 * Closes the struct file that was created in open_cur_inode_file
2044 */
2045static int close_cur_inode_file(struct send_ctx *sctx)
2046{
2047 int ret = 0;
2048
2049 if (!sctx->cur_inode_filp)
2050 goto out;
2051
2052 ret = filp_close(sctx->cur_inode_filp, NULL);
2053 sctx->cur_inode_filp = NULL;
2054
2055out:
2056 return ret;
2057}
2058
2059/*
2060 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2061 */
2062static int send_subvol_begin(struct send_ctx *sctx)
2063{
2064 int ret;
2065 struct btrfs_root *send_root = sctx->send_root;
2066 struct btrfs_root *parent_root = sctx->parent_root;
2067 struct btrfs_path *path;
2068 struct btrfs_key key;
2069 struct btrfs_root_ref *ref;
2070 struct extent_buffer *leaf;
2071 char *name = NULL;
2072 int namelen;
2073
2074 path = alloc_path_for_send();
2075 if (!path)
2076 return -ENOMEM;
2077
2078 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2079 if (!name) {
2080 btrfs_free_path(path);
2081 return -ENOMEM;
2082 }
2083
2084 key.objectid = send_root->objectid;
2085 key.type = BTRFS_ROOT_BACKREF_KEY;
2086 key.offset = 0;
2087
2088 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2089 &key, path, 1, 0);
2090 if (ret < 0)
2091 goto out;
2092 if (ret) {
2093 ret = -ENOENT;
2094 goto out;
2095 }
2096
2097 leaf = path->nodes[0];
2098 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2099 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2100 key.objectid != send_root->objectid) {
2101 ret = -ENOENT;
2102 goto out;
2103 }
2104 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2105 namelen = btrfs_root_ref_name_len(leaf, ref);
2106 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2107 btrfs_release_path(path);
2108
2109 if (ret < 0)
2110 goto out;
2111
2112 if (parent_root) {
2113 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2114 if (ret < 0)
2115 goto out;
2116 } else {
2117 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2118 if (ret < 0)
2119 goto out;
2120 }
2121
2122 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2123 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2124 sctx->send_root->root_item.uuid);
2125 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2126 sctx->send_root->root_item.ctransid);
2127 if (parent_root) {
2128 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2129 sctx->parent_root->root_item.uuid);
2130 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2131 sctx->parent_root->root_item.ctransid);
2132 }
2133
2134 ret = send_cmd(sctx);
2135
2136tlv_put_failure:
2137out:
2138 btrfs_free_path(path);
2139 kfree(name);
2140 return ret;
2141}
2142
2143static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2144{
2145 int ret = 0;
2146 struct fs_path *p;
2147
2148verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2149
2150 p = fs_path_alloc(sctx);
2151 if (!p)
2152 return -ENOMEM;
2153
2154 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2155 if (ret < 0)
2156 goto out;
2157
2158 ret = get_cur_path(sctx, ino, gen, p);
2159 if (ret < 0)
2160 goto out;
2161 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2162 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2163
2164 ret = send_cmd(sctx);
2165
2166tlv_put_failure:
2167out:
2168 fs_path_free(sctx, p);
2169 return ret;
2170}
2171
2172static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2173{
2174 int ret = 0;
2175 struct fs_path *p;
2176
2177verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2178
2179 p = fs_path_alloc(sctx);
2180 if (!p)
2181 return -ENOMEM;
2182
2183 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2184 if (ret < 0)
2185 goto out;
2186
2187 ret = get_cur_path(sctx, ino, gen, p);
2188 if (ret < 0)
2189 goto out;
2190 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2191 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2192
2193 ret = send_cmd(sctx);
2194
2195tlv_put_failure:
2196out:
2197 fs_path_free(sctx, p);
2198 return ret;
2199}
2200
2201static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2202{
2203 int ret = 0;
2204 struct fs_path *p;
2205
2206verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2207
2208 p = fs_path_alloc(sctx);
2209 if (!p)
2210 return -ENOMEM;
2211
2212 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2213 if (ret < 0)
2214 goto out;
2215
2216 ret = get_cur_path(sctx, ino, gen, p);
2217 if (ret < 0)
2218 goto out;
2219 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2220 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2221 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2222
2223 ret = send_cmd(sctx);
2224
2225tlv_put_failure:
2226out:
2227 fs_path_free(sctx, p);
2228 return ret;
2229}
2230
2231static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2232{
2233 int ret = 0;
2234 struct fs_path *p = NULL;
2235 struct btrfs_inode_item *ii;
2236 struct btrfs_path *path = NULL;
2237 struct extent_buffer *eb;
2238 struct btrfs_key key;
2239 int slot;
2240
2241verbose_printk("btrfs: send_utimes %llu\n", ino);
2242
2243 p = fs_path_alloc(sctx);
2244 if (!p)
2245 return -ENOMEM;
2246
2247 path = alloc_path_for_send();
2248 if (!path) {
2249 ret = -ENOMEM;
2250 goto out;
2251 }
2252
2253 key.objectid = ino;
2254 key.type = BTRFS_INODE_ITEM_KEY;
2255 key.offset = 0;
2256 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2257 if (ret < 0)
2258 goto out;
2259
2260 eb = path->nodes[0];
2261 slot = path->slots[0];
2262 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2263
2264 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2265 if (ret < 0)
2266 goto out;
2267
2268 ret = get_cur_path(sctx, ino, gen, p);
2269 if (ret < 0)
2270 goto out;
2271 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2272 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2273 btrfs_inode_atime(ii));
2274 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2275 btrfs_inode_mtime(ii));
2276 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2277 btrfs_inode_ctime(ii));
2278 /* TODO otime? */
2279
2280 ret = send_cmd(sctx);
2281
2282tlv_put_failure:
2283out:
2284 fs_path_free(sctx, p);
2285 btrfs_free_path(path);
2286 return ret;
2287}
2288
2289/*
2290 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2291 * a valid path yet because we did not process the refs yet. So, the inode
2292 * is created as orphan.
2293 */
2294static int send_create_inode(struct send_ctx *sctx, struct btrfs_path *path,
2295 struct btrfs_key *key)
2296{
2297 int ret = 0;
2298 struct extent_buffer *eb = path->nodes[0];
2299 struct btrfs_inode_item *ii;
2300 struct fs_path *p;
2301 int slot = path->slots[0];
2302 int cmd;
2303 u64 mode;
2304
2305verbose_printk("btrfs: send_create_inode %llu\n", sctx->cur_ino);
2306
2307 p = fs_path_alloc(sctx);
2308 if (!p)
2309 return -ENOMEM;
2310
2311 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2312 mode = btrfs_inode_mode(eb, ii);
2313
2314 if (S_ISREG(mode))
2315 cmd = BTRFS_SEND_C_MKFILE;
2316 else if (S_ISDIR(mode))
2317 cmd = BTRFS_SEND_C_MKDIR;
2318 else if (S_ISLNK(mode))
2319 cmd = BTRFS_SEND_C_SYMLINK;
2320 else if (S_ISCHR(mode) || S_ISBLK(mode))
2321 cmd = BTRFS_SEND_C_MKNOD;
2322 else if (S_ISFIFO(mode))
2323 cmd = BTRFS_SEND_C_MKFIFO;
2324 else if (S_ISSOCK(mode))
2325 cmd = BTRFS_SEND_C_MKSOCK;
2326 else {
2327 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2328 (int)(mode & S_IFMT));
2329 ret = -ENOTSUPP;
2330 goto out;
2331 }
2332
2333 ret = begin_cmd(sctx, cmd);
2334 if (ret < 0)
2335 goto out;
2336
2337 ret = gen_unique_name(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
2338 if (ret < 0)
2339 goto out;
2340
2341 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2342 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, sctx->cur_ino);
2343
2344 if (S_ISLNK(mode)) {
2345 fs_path_reset(p);
2346 ret = read_symlink(sctx, sctx->send_root, sctx->cur_ino, p);
2347 if (ret < 0)
2348 goto out;
2349 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2350 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2351 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2352 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, btrfs_inode_rdev(eb, ii));
2353 }
2354
2355 ret = send_cmd(sctx);
2356 if (ret < 0)
2357 goto out;
2358
2359
2360tlv_put_failure:
2361out:
2362 fs_path_free(sctx, p);
2363 return ret;
2364}
2365
2366struct recorded_ref {
2367 struct list_head list;
2368 char *dir_path;
2369 char *name;
2370 struct fs_path *full_path;
2371 u64 dir;
2372 u64 dir_gen;
2373 int dir_path_len;
2374 int name_len;
2375};
2376
2377/*
2378 * We need to process new refs before deleted refs, but compare_tree gives us
2379 * everything mixed. So we first record all refs and later process them.
2380 * This function is a helper to record one ref.
2381 */
2382static int record_ref(struct list_head *head, u64 dir,
2383 u64 dir_gen, struct fs_path *path)
2384{
2385 struct recorded_ref *ref;
2386 char *tmp;
2387
2388 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2389 if (!ref)
2390 return -ENOMEM;
2391
2392 ref->dir = dir;
2393 ref->dir_gen = dir_gen;
2394 ref->full_path = path;
2395
2396 tmp = strrchr(ref->full_path->start, '/');
2397 if (!tmp) {
2398 ref->name_len = ref->full_path->end - ref->full_path->start;
2399 ref->name = ref->full_path->start;
2400 ref->dir_path_len = 0;
2401 ref->dir_path = ref->full_path->start;
2402 } else {
2403 tmp++;
2404 ref->name_len = ref->full_path->end - tmp;
2405 ref->name = tmp;
2406 ref->dir_path = ref->full_path->start;
2407 ref->dir_path_len = ref->full_path->end -
2408 ref->full_path->start - 1 - ref->name_len;
2409 }
2410
2411 list_add_tail(&ref->list, head);
2412 return 0;
2413}
2414
2415static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head)
2416{
2417 struct recorded_ref *cur;
2418 struct recorded_ref *tmp;
2419
2420 list_for_each_entry_safe(cur, tmp, head, list) {
2421 fs_path_free(sctx, cur->full_path);
2422 kfree(cur);
2423 }
2424 INIT_LIST_HEAD(head);
2425}
2426
2427static void free_recorded_refs(struct send_ctx *sctx)
2428{
2429 __free_recorded_refs(sctx, &sctx->new_refs);
2430 __free_recorded_refs(sctx, &sctx->deleted_refs);
2431}
2432
2433/*
2434 * Renames/moves a file/dir to it's orphan name. Used when the first
2435 * ref of an unprocessed inode gets overwritten and for all non empty
2436 * directories.
2437 */
2438static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2439 struct fs_path *path)
2440{
2441 int ret;
2442 struct fs_path *orphan;
2443
2444 orphan = fs_path_alloc(sctx);
2445 if (!orphan)
2446 return -ENOMEM;
2447
2448 ret = gen_unique_name(sctx, ino, gen, orphan);
2449 if (ret < 0)
2450 goto out;
2451
2452 ret = send_rename(sctx, path, orphan);
2453
2454out:
2455 fs_path_free(sctx, orphan);
2456 return ret;
2457}
2458
2459/*
2460 * Returns 1 if a directory can be removed at this point in time.
2461 * We check this by iterating all dir items and checking if the inode behind
2462 * the dir item was already processed.
2463 */
2464static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2465{
2466 int ret = 0;
2467 struct btrfs_root *root = sctx->parent_root;
2468 struct btrfs_path *path;
2469 struct btrfs_key key;
2470 struct btrfs_key found_key;
2471 struct btrfs_key loc;
2472 struct btrfs_dir_item *di;
2473
2474 path = alloc_path_for_send();
2475 if (!path)
2476 return -ENOMEM;
2477
2478 key.objectid = dir;
2479 key.type = BTRFS_DIR_INDEX_KEY;
2480 key.offset = 0;
2481
2482 while (1) {
2483 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2484 if (ret < 0)
2485 goto out;
2486 if (!ret) {
2487 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2488 path->slots[0]);
2489 }
2490 if (ret || found_key.objectid != key.objectid ||
2491 found_key.type != key.type) {
2492 break;
2493 }
2494
2495 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2496 struct btrfs_dir_item);
2497 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2498
2499 if (loc.objectid > send_progress) {
2500 ret = 0;
2501 goto out;
2502 }
2503
2504 btrfs_release_path(path);
2505 key.offset = found_key.offset + 1;
2506 }
2507
2508 ret = 1;
2509
2510out:
2511 btrfs_free_path(path);
2512 return ret;
2513}
2514
2515struct finish_unordered_dir_ctx {
2516 struct send_ctx *sctx;
2517 struct fs_path *cur_path;
2518 struct fs_path *dir_path;
2519 u64 dir_ino;
2520 int need_delete;
2521 int delete_pass;
2522};
2523
2524int __finish_unordered_dir(int num, struct btrfs_key *di_key,
2525 const char *name, int name_len,
2526 const char *data, int data_len,
2527 u8 type, void *ctx)
2528{
2529 int ret = 0;
2530 struct finish_unordered_dir_ctx *fctx = ctx;
2531 struct send_ctx *sctx = fctx->sctx;
2532 u64 di_gen;
2533 u64 di_mode;
2534 int is_orphan = 0;
2535
2536 if (di_key->objectid >= fctx->dir_ino)
2537 goto out;
2538
2539 fs_path_reset(fctx->cur_path);
2540
2541 ret = get_inode_info(sctx->send_root, di_key->objectid,
2542 NULL, &di_gen, &di_mode, NULL, NULL);
2543 if (ret < 0)
2544 goto out;
2545
2546 ret = is_first_ref(sctx, sctx->send_root, di_key->objectid,
2547 fctx->dir_ino, name, name_len);
2548 if (ret < 0)
2549 goto out;
2550 if (ret) {
2551 is_orphan = 1;
2552 ret = gen_unique_name(sctx, di_key->objectid, di_gen,
2553 fctx->cur_path);
2554 } else {
2555 ret = get_cur_path(sctx, di_key->objectid, di_gen,
2556 fctx->cur_path);
2557 }
2558 if (ret < 0)
2559 goto out;
2560
2561 ret = fs_path_add(fctx->dir_path, name, name_len);
2562 if (ret < 0)
2563 goto out;
2564
2565 if (!fctx->delete_pass) {
2566 if (S_ISDIR(di_mode)) {
2567 ret = send_rename(sctx, fctx->cur_path,
2568 fctx->dir_path);
2569 } else {
2570 ret = send_link(sctx, fctx->dir_path,
2571 fctx->cur_path);
2572 if (is_orphan)
2573 fctx->need_delete = 1;
2574 }
2575 } else if (!S_ISDIR(di_mode)) {
2576 ret = send_unlink(sctx, fctx->cur_path);
2577 } else {
2578 ret = 0;
2579 }
2580
2581 fs_path_remove(fctx->dir_path);
2582
2583out:
2584 return ret;
2585}
2586
2587/*
2588 * Go through all dir items and see if we find refs which could not be created
2589 * in the past because the dir did not exist at that time.
2590 */
2591static int finish_outoforder_dir(struct send_ctx *sctx, u64 dir, u64 dir_gen)
2592{
2593 int ret = 0;
2594 struct btrfs_path *path = NULL;
2595 struct btrfs_key key;
2596 struct btrfs_key found_key;
2597 struct extent_buffer *eb;
2598 struct finish_unordered_dir_ctx fctx;
2599 int slot;
2600
2601 path = alloc_path_for_send();
2602 if (!path) {
2603 ret = -ENOMEM;
2604 goto out;
2605 }
2606
2607 memset(&fctx, 0, sizeof(fctx));
2608 fctx.sctx = sctx;
2609 fctx.cur_path = fs_path_alloc(sctx);
2610 fctx.dir_path = fs_path_alloc(sctx);
2611 if (!fctx.cur_path || !fctx.dir_path) {
2612 ret = -ENOMEM;
2613 goto out;
2614 }
2615 fctx.dir_ino = dir;
2616
2617 ret = get_cur_path(sctx, dir, dir_gen, fctx.dir_path);
2618 if (ret < 0)
2619 goto out;
2620
2621 /*
2622 * We do two passes. The first links in the new refs and the second
2623 * deletes orphans if required. Deletion of orphans is not required for
2624 * directory inodes, as we always have only one ref and use rename
2625 * instead of link for those.
2626 */
2627
2628again:
2629 key.objectid = dir;
2630 key.type = BTRFS_DIR_ITEM_KEY;
2631 key.offset = 0;
2632 while (1) {
2633 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2634 1, 0);
2635 if (ret < 0)
2636 goto out;
2637 eb = path->nodes[0];
2638 slot = path->slots[0];
2639 btrfs_item_key_to_cpu(eb, &found_key, slot);
2640
2641 if (found_key.objectid != key.objectid ||
2642 found_key.type != key.type) {
2643 btrfs_release_path(path);
2644 break;
2645 }
2646
2647 ret = iterate_dir_item(sctx, sctx->send_root, path,
2648 &found_key, __finish_unordered_dir,
2649 &fctx);
2650 if (ret < 0)
2651 goto out;
2652
2653 key.offset = found_key.offset + 1;
2654 btrfs_release_path(path);
2655 }
2656
2657 if (!fctx.delete_pass && fctx.need_delete) {
2658 fctx.delete_pass = 1;
2659 goto again;
2660 }
2661
2662out:
2663 btrfs_free_path(path);
2664 fs_path_free(sctx, fctx.cur_path);
2665 fs_path_free(sctx, fctx.dir_path);
2666 return ret;
2667}
2668
2669/*
2670 * This does all the move/link/unlink/rmdir magic.
2671 */
2672static int process_recorded_refs(struct send_ctx *sctx)
2673{
2674 int ret = 0;
2675 struct recorded_ref *cur;
2676 struct ulist *check_dirs = NULL;
2677 struct ulist_iterator uit;
2678 struct ulist_node *un;
2679 struct fs_path *valid_path = NULL;
b24baf69 2680 u64 ow_inode = 0;
31db9f7c
AB
2681 u64 ow_gen;
2682 int did_overwrite = 0;
2683 int is_orphan = 0;
2684
2685verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2686
2687 valid_path = fs_path_alloc(sctx);
2688 if (!valid_path) {
2689 ret = -ENOMEM;
2690 goto out;
2691 }
2692
2693 check_dirs = ulist_alloc(GFP_NOFS);
2694 if (!check_dirs) {
2695 ret = -ENOMEM;
2696 goto out;
2697 }
2698
2699 /*
2700 * First, check if the first ref of the current inode was overwritten
2701 * before. If yes, we know that the current inode was already orphanized
2702 * and thus use the orphan name. If not, we can use get_cur_path to
2703 * get the path of the first ref as it would like while receiving at
2704 * this point in time.
2705 * New inodes are always orphan at the beginning, so force to use the
2706 * orphan name in this case.
2707 * The first ref is stored in valid_path and will be updated if it
2708 * gets moved around.
2709 */
2710 if (!sctx->cur_inode_new) {
2711 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2712 sctx->cur_inode_gen);
2713 if (ret < 0)
2714 goto out;
2715 if (ret)
2716 did_overwrite = 1;
2717 }
2718 if (sctx->cur_inode_new || did_overwrite) {
2719 ret = gen_unique_name(sctx, sctx->cur_ino,
2720 sctx->cur_inode_gen, valid_path);
2721 if (ret < 0)
2722 goto out;
2723 is_orphan = 1;
2724 } else {
2725 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2726 valid_path);
2727 if (ret < 0)
2728 goto out;
2729 }
2730
2731 list_for_each_entry(cur, &sctx->new_refs, list) {
2732 /*
2733 * Check if this new ref would overwrite the first ref of
2734 * another unprocessed inode. If yes, orphanize the
2735 * overwritten inode. If we find an overwritten ref that is
2736 * not the first ref, simply unlink it.
2737 */
2738 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2739 cur->name, cur->name_len,
2740 &ow_inode, &ow_gen);
2741 if (ret < 0)
2742 goto out;
2743 if (ret) {
2744 ret = is_first_ref(sctx, sctx->parent_root,
2745 ow_inode, cur->dir, cur->name,
2746 cur->name_len);
2747 if (ret < 0)
2748 goto out;
2749 if (ret) {
2750 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2751 cur->full_path);
2752 if (ret < 0)
2753 goto out;
2754 } else {
2755 ret = send_unlink(sctx, cur->full_path);
2756 if (ret < 0)
2757 goto out;
2758 }
2759 }
2760
2761 /*
2762 * link/move the ref to the new place. If we have an orphan
2763 * inode, move it and update valid_path. If not, link or move
2764 * it depending on the inode mode.
2765 */
2766 if (is_orphan && !sctx->cur_inode_first_ref_orphan) {
2767 ret = send_rename(sctx, valid_path, cur->full_path);
2768 if (ret < 0)
2769 goto out;
2770 is_orphan = 0;
2771 ret = fs_path_copy(valid_path, cur->full_path);
2772 if (ret < 0)
2773 goto out;
2774 } else {
2775 if (S_ISDIR(sctx->cur_inode_mode)) {
2776 /*
2777 * Dirs can't be linked, so move it. For moved
2778 * dirs, we always have one new and one deleted
2779 * ref. The deleted ref is ignored later.
2780 */
2781 ret = send_rename(sctx, valid_path,
2782 cur->full_path);
2783 if (ret < 0)
2784 goto out;
2785 ret = fs_path_copy(valid_path, cur->full_path);
2786 if (ret < 0)
2787 goto out;
2788 } else {
2789 ret = send_link(sctx, cur->full_path,
2790 valid_path);
2791 if (ret < 0)
2792 goto out;
2793 }
2794 }
2795 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2796 GFP_NOFS);
2797 if (ret < 0)
2798 goto out;
2799 }
2800
2801 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2802 /*
2803 * Check if we can already rmdir the directory. If not,
2804 * orphanize it. For every dir item inside that gets deleted
2805 * later, we do this check again and rmdir it then if possible.
2806 * See the use of check_dirs for more details.
2807 */
2808 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2809 if (ret < 0)
2810 goto out;
2811 if (ret) {
2812 ret = send_rmdir(sctx, valid_path);
2813 if (ret < 0)
2814 goto out;
2815 } else if (!is_orphan) {
2816 ret = orphanize_inode(sctx, sctx->cur_ino,
2817 sctx->cur_inode_gen, valid_path);
2818 if (ret < 0)
2819 goto out;
2820 is_orphan = 1;
2821 }
2822
2823 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2824 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2825 GFP_NOFS);
2826 if (ret < 0)
2827 goto out;
2828 }
2829 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2830 /*
2831 * We have a non dir inode. Go through all deleted refs and
2832 * unlink them if they were not already overwritten by other
2833 * inodes.
2834 */
2835 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2836 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2837 sctx->cur_ino, sctx->cur_inode_gen,
2838 cur->name, cur->name_len);
2839 if (ret < 0)
2840 goto out;
2841 if (!ret) {
2842 /*
2843 * In case the inode was moved to a directory
2844 * that was not created yet (see
2845 * __record_new_ref), we can not unlink the ref
2846 * as it will be needed later when the parent
2847 * directory is created, so that we can move in
2848 * the inode to the new dir.
2849 */
2850 if (!is_orphan &&
2851 sctx->cur_inode_first_ref_orphan) {
2852 ret = orphanize_inode(sctx,
2853 sctx->cur_ino,
2854 sctx->cur_inode_gen,
2855 cur->full_path);
2856 if (ret < 0)
2857 goto out;
2858 ret = gen_unique_name(sctx,
2859 sctx->cur_ino,
2860 sctx->cur_inode_gen,
2861 valid_path);
2862 if (ret < 0)
2863 goto out;
2864 is_orphan = 1;
2865
2866 } else {
2867 ret = send_unlink(sctx, cur->full_path);
2868 if (ret < 0)
2869 goto out;
2870 }
2871 }
2872 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2873 GFP_NOFS);
2874 if (ret < 0)
2875 goto out;
2876 }
2877
2878 /*
2879 * If the inode is still orphan, unlink the orphan. This may
2880 * happen when a previous inode did overwrite the first ref
2881 * of this inode and no new refs were added for the current
2882 * inode.
2883 * We can however not delete the orphan in case the inode relies
2884 * in a directory that was not created yet (see
2885 * __record_new_ref)
2886 */
2887 if (is_orphan && !sctx->cur_inode_first_ref_orphan) {
2888 ret = send_unlink(sctx, valid_path);
2889 if (ret < 0)
2890 goto out;
2891 }
2892 }
2893
2894 /*
2895 * We did collect all parent dirs where cur_inode was once located. We
2896 * now go through all these dirs and check if they are pending for
2897 * deletion and if it's finally possible to perform the rmdir now.
2898 * We also update the inode stats of the parent dirs here.
2899 */
2900 ULIST_ITER_INIT(&uit);
2901 while ((un = ulist_next(check_dirs, &uit))) {
2902 if (un->val > sctx->cur_ino)
2903 continue;
2904
2905 ret = get_cur_inode_state(sctx, un->val, un->aux);
2906 if (ret < 0)
2907 goto out;
2908
2909 if (ret == inode_state_did_create ||
2910 ret == inode_state_no_change) {
2911 /* TODO delayed utimes */
2912 ret = send_utimes(sctx, un->val, un->aux);
2913 if (ret < 0)
2914 goto out;
2915 } else if (ret == inode_state_did_delete) {
2916 ret = can_rmdir(sctx, un->val, sctx->cur_ino);
2917 if (ret < 0)
2918 goto out;
2919 if (ret) {
2920 ret = get_cur_path(sctx, un->val, un->aux,
2921 valid_path);
2922 if (ret < 0)
2923 goto out;
2924 ret = send_rmdir(sctx, valid_path);
2925 if (ret < 0)
2926 goto out;
2927 }
2928 }
2929 }
2930
2931 /*
2932 * Current inode is now at it's new position, so we must increase
2933 * send_progress
2934 */
2935 sctx->send_progress = sctx->cur_ino + 1;
2936
2937 /*
2938 * We may have a directory here that has pending refs which could not
2939 * be created before (because the dir did not exist before, see
2940 * __record_new_ref). finish_outoforder_dir will link/move the pending
2941 * refs.
2942 */
2943 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_new) {
2944 ret = finish_outoforder_dir(sctx, sctx->cur_ino,
2945 sctx->cur_inode_gen);
2946 if (ret < 0)
2947 goto out;
2948 }
2949
2950 ret = 0;
2951
2952out:
2953 free_recorded_refs(sctx);
2954 ulist_free(check_dirs);
2955 fs_path_free(sctx, valid_path);
2956 return ret;
2957}
2958
2959static int __record_new_ref(int num, u64 dir, int index,
2960 struct fs_path *name,
2961 void *ctx)
2962{
2963 int ret = 0;
2964 struct send_ctx *sctx = ctx;
2965 struct fs_path *p;
2966 u64 gen;
2967
2968 p = fs_path_alloc(sctx);
2969 if (!p)
2970 return -ENOMEM;
2971
2972 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
2973 NULL);
2974 if (ret < 0)
2975 goto out;
2976
2977 /*
2978 * The parent may be non-existent at this point in time. This happens
2979 * if the ino of the parent dir is higher then the current ino. In this
2980 * case, we can not process this ref until the parent dir is finally
2981 * created. If we reach the parent dir later, process_recorded_refs
2982 * will go through all dir items and process the refs that could not be
2983 * processed before. In case this is the first ref, we set
2984 * cur_inode_first_ref_orphan to 1 to inform process_recorded_refs to
2985 * keep an orphan of the inode so that it later can be used for
2986 * link/move
2987 */
2988 ret = is_inode_existent(sctx, dir, gen);
2989 if (ret < 0)
2990 goto out;
2991 if (!ret) {
2992 ret = is_first_ref(sctx, sctx->send_root, sctx->cur_ino, dir,
2993 name->start, fs_path_len(name));
2994 if (ret < 0)
2995 goto out;
2996 if (ret)
2997 sctx->cur_inode_first_ref_orphan = 1;
2998 ret = 0;
2999 goto out;
3000 }
3001
3002 ret = get_cur_path(sctx, dir, gen, p);
3003 if (ret < 0)
3004 goto out;
3005 ret = fs_path_add_path(p, name);
3006 if (ret < 0)
3007 goto out;
3008
3009 ret = record_ref(&sctx->new_refs, dir, gen, p);
3010
3011out:
3012 if (ret)
3013 fs_path_free(sctx, p);
3014 return ret;
3015}
3016
3017static int __record_deleted_ref(int num, u64 dir, int index,
3018 struct fs_path *name,
3019 void *ctx)
3020{
3021 int ret = 0;
3022 struct send_ctx *sctx = ctx;
3023 struct fs_path *p;
3024 u64 gen;
3025
3026 p = fs_path_alloc(sctx);
3027 if (!p)
3028 return -ENOMEM;
3029
3030 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3031 NULL);
3032 if (ret < 0)
3033 goto out;
3034
3035 ret = get_cur_path(sctx, dir, gen, p);
3036 if (ret < 0)
3037 goto out;
3038 ret = fs_path_add_path(p, name);
3039 if (ret < 0)
3040 goto out;
3041
3042 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3043
3044out:
3045 if (ret)
3046 fs_path_free(sctx, p);
3047 return ret;
3048}
3049
3050static int record_new_ref(struct send_ctx *sctx)
3051{
3052 int ret;
3053
3054 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3055 sctx->cmp_key, 0, __record_new_ref, sctx);
3056 if (ret < 0)
3057 goto out;
3058 ret = 0;
3059
3060out:
3061 return ret;
3062}
3063
3064static int record_deleted_ref(struct send_ctx *sctx)
3065{
3066 int ret;
3067
3068 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3069 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3070 if (ret < 0)
3071 goto out;
3072 ret = 0;
3073
3074out:
3075 return ret;
3076}
3077
3078struct find_ref_ctx {
3079 u64 dir;
3080 struct fs_path *name;
3081 int found_idx;
3082};
3083
3084static int __find_iref(int num, u64 dir, int index,
3085 struct fs_path *name,
3086 void *ctx_)
3087{
3088 struct find_ref_ctx *ctx = ctx_;
3089
3090 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3091 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3092 ctx->found_idx = num;
3093 return 1;
3094 }
3095 return 0;
3096}
3097
3098static int find_iref(struct send_ctx *sctx,
3099 struct btrfs_root *root,
3100 struct btrfs_path *path,
3101 struct btrfs_key *key,
3102 u64 dir, struct fs_path *name)
3103{
3104 int ret;
3105 struct find_ref_ctx ctx;
3106
3107 ctx.dir = dir;
3108 ctx.name = name;
3109 ctx.found_idx = -1;
3110
3111 ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
3112 if (ret < 0)
3113 return ret;
3114
3115 if (ctx.found_idx == -1)
3116 return -ENOENT;
3117
3118 return ctx.found_idx;
3119}
3120
3121static int __record_changed_new_ref(int num, u64 dir, int index,
3122 struct fs_path *name,
3123 void *ctx)
3124{
3125 int ret;
3126 struct send_ctx *sctx = ctx;
3127
3128 ret = find_iref(sctx, sctx->parent_root, sctx->right_path,
3129 sctx->cmp_key, dir, name);
3130 if (ret == -ENOENT)
3131 ret = __record_new_ref(num, dir, index, name, sctx);
3132 else if (ret > 0)
3133 ret = 0;
3134
3135 return ret;
3136}
3137
3138static int __record_changed_deleted_ref(int num, u64 dir, int index,
3139 struct fs_path *name,
3140 void *ctx)
3141{
3142 int ret;
3143 struct send_ctx *sctx = ctx;
3144
3145 ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3146 dir, name);
3147 if (ret == -ENOENT)
3148 ret = __record_deleted_ref(num, dir, index, name, sctx);
3149 else if (ret > 0)
3150 ret = 0;
3151
3152 return ret;
3153}
3154
3155static int record_changed_ref(struct send_ctx *sctx)
3156{
3157 int ret = 0;
3158
3159 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3160 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3161 if (ret < 0)
3162 goto out;
3163 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3164 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3165 if (ret < 0)
3166 goto out;
3167 ret = 0;
3168
3169out:
3170 return ret;
3171}
3172
3173/*
3174 * Record and process all refs at once. Needed when an inode changes the
3175 * generation number, which means that it was deleted and recreated.
3176 */
3177static int process_all_refs(struct send_ctx *sctx,
3178 enum btrfs_compare_tree_result cmd)
3179{
3180 int ret;
3181 struct btrfs_root *root;
3182 struct btrfs_path *path;
3183 struct btrfs_key key;
3184 struct btrfs_key found_key;
3185 struct extent_buffer *eb;
3186 int slot;
3187 iterate_inode_ref_t cb;
3188
3189 path = alloc_path_for_send();
3190 if (!path)
3191 return -ENOMEM;
3192
3193 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3194 root = sctx->send_root;
3195 cb = __record_new_ref;
3196 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3197 root = sctx->parent_root;
3198 cb = __record_deleted_ref;
3199 } else {
3200 BUG();
3201 }
3202
3203 key.objectid = sctx->cmp_key->objectid;
3204 key.type = BTRFS_INODE_REF_KEY;
3205 key.offset = 0;
3206 while (1) {
3207 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3208 if (ret < 0) {
3209 btrfs_release_path(path);
3210 goto out;
3211 }
3212 if (ret) {
3213 btrfs_release_path(path);
3214 break;
3215 }
3216
3217 eb = path->nodes[0];
3218 slot = path->slots[0];
3219 btrfs_item_key_to_cpu(eb, &found_key, slot);
3220
3221 if (found_key.objectid != key.objectid ||
3222 found_key.type != key.type) {
3223 btrfs_release_path(path);
3224 break;
3225 }
3226
3227 ret = iterate_inode_ref(sctx, sctx->parent_root, path,
3228 &found_key, 0, cb, sctx);
3229 btrfs_release_path(path);
3230 if (ret < 0)
3231 goto out;
3232
3233 key.offset = found_key.offset + 1;
3234 }
3235
3236 ret = process_recorded_refs(sctx);
3237
3238out:
3239 btrfs_free_path(path);
3240 return ret;
3241}
3242
3243static int send_set_xattr(struct send_ctx *sctx,
3244 struct fs_path *path,
3245 const char *name, int name_len,
3246 const char *data, int data_len)
3247{
3248 int ret = 0;
3249
3250 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3251 if (ret < 0)
3252 goto out;
3253
3254 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3255 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3256 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3257
3258 ret = send_cmd(sctx);
3259
3260tlv_put_failure:
3261out:
3262 return ret;
3263}
3264
3265static int send_remove_xattr(struct send_ctx *sctx,
3266 struct fs_path *path,
3267 const char *name, int name_len)
3268{
3269 int ret = 0;
3270
3271 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3272 if (ret < 0)
3273 goto out;
3274
3275 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3276 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3277
3278 ret = send_cmd(sctx);
3279
3280tlv_put_failure:
3281out:
3282 return ret;
3283}
3284
3285static int __process_new_xattr(int num, struct btrfs_key *di_key,
3286 const char *name, int name_len,
3287 const char *data, int data_len,
3288 u8 type, void *ctx)
3289{
3290 int ret;
3291 struct send_ctx *sctx = ctx;
3292 struct fs_path *p;
3293 posix_acl_xattr_header dummy_acl;
3294
3295 p = fs_path_alloc(sctx);
3296 if (!p)
3297 return -ENOMEM;
3298
3299 /*
3300 * This hack is needed because empty acl's are stored as zero byte
3301 * data in xattrs. Problem with that is, that receiving these zero byte
3302 * acl's will fail later. To fix this, we send a dummy acl list that
3303 * only contains the version number and no entries.
3304 */
3305 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3306 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3307 if (data_len == 0) {
3308 dummy_acl.a_version =
3309 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3310 data = (char *)&dummy_acl;
3311 data_len = sizeof(dummy_acl);
3312 }
3313 }
3314
3315 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3316 if (ret < 0)
3317 goto out;
3318
3319 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3320
3321out:
3322 fs_path_free(sctx, p);
3323 return ret;
3324}
3325
3326static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3327 const char *name, int name_len,
3328 const char *data, int data_len,
3329 u8 type, void *ctx)
3330{
3331 int ret;
3332 struct send_ctx *sctx = ctx;
3333 struct fs_path *p;
3334
3335 p = fs_path_alloc(sctx);
3336 if (!p)
3337 return -ENOMEM;
3338
3339 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3340 if (ret < 0)
3341 goto out;
3342
3343 ret = send_remove_xattr(sctx, p, name, name_len);
3344
3345out:
3346 fs_path_free(sctx, p);
3347 return ret;
3348}
3349
3350static int process_new_xattr(struct send_ctx *sctx)
3351{
3352 int ret = 0;
3353
3354 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3355 sctx->cmp_key, __process_new_xattr, sctx);
3356
3357 return ret;
3358}
3359
3360static int process_deleted_xattr(struct send_ctx *sctx)
3361{
3362 int ret;
3363
3364 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3365 sctx->cmp_key, __process_deleted_xattr, sctx);
3366
3367 return ret;
3368}
3369
3370struct find_xattr_ctx {
3371 const char *name;
3372 int name_len;
3373 int found_idx;
3374 char *found_data;
3375 int found_data_len;
3376};
3377
3378static int __find_xattr(int num, struct btrfs_key *di_key,
3379 const char *name, int name_len,
3380 const char *data, int data_len,
3381 u8 type, void *vctx)
3382{
3383 struct find_xattr_ctx *ctx = vctx;
3384
3385 if (name_len == ctx->name_len &&
3386 strncmp(name, ctx->name, name_len) == 0) {
3387 ctx->found_idx = num;
3388 ctx->found_data_len = data_len;
3389 ctx->found_data = kmalloc(data_len, GFP_NOFS);
3390 if (!ctx->found_data)
3391 return -ENOMEM;
3392 memcpy(ctx->found_data, data, data_len);
3393 return 1;
3394 }
3395 return 0;
3396}
3397
3398static int find_xattr(struct send_ctx *sctx,
3399 struct btrfs_root *root,
3400 struct btrfs_path *path,
3401 struct btrfs_key *key,
3402 const char *name, int name_len,
3403 char **data, int *data_len)
3404{
3405 int ret;
3406 struct find_xattr_ctx ctx;
3407
3408 ctx.name = name;
3409 ctx.name_len = name_len;
3410 ctx.found_idx = -1;
3411 ctx.found_data = NULL;
3412 ctx.found_data_len = 0;
3413
3414 ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
3415 if (ret < 0)
3416 return ret;
3417
3418 if (ctx.found_idx == -1)
3419 return -ENOENT;
3420 if (data) {
3421 *data = ctx.found_data;
3422 *data_len = ctx.found_data_len;
3423 } else {
3424 kfree(ctx.found_data);
3425 }
3426 return ctx.found_idx;
3427}
3428
3429
3430static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3431 const char *name, int name_len,
3432 const char *data, int data_len,
3433 u8 type, void *ctx)
3434{
3435 int ret;
3436 struct send_ctx *sctx = ctx;
3437 char *found_data = NULL;
3438 int found_data_len = 0;
3439 struct fs_path *p = NULL;
3440
3441 ret = find_xattr(sctx, sctx->parent_root, sctx->right_path,
3442 sctx->cmp_key, name, name_len, &found_data,
3443 &found_data_len);
3444 if (ret == -ENOENT) {
3445 ret = __process_new_xattr(num, di_key, name, name_len, data,
3446 data_len, type, ctx);
3447 } else if (ret >= 0) {
3448 if (data_len != found_data_len ||
3449 memcmp(data, found_data, data_len)) {
3450 ret = __process_new_xattr(num, di_key, name, name_len,
3451 data, data_len, type, ctx);
3452 } else {
3453 ret = 0;
3454 }
3455 }
3456
3457 kfree(found_data);
3458 fs_path_free(sctx, p);
3459 return ret;
3460}
3461
3462static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3463 const char *name, int name_len,
3464 const char *data, int data_len,
3465 u8 type, void *ctx)
3466{
3467 int ret;
3468 struct send_ctx *sctx = ctx;
3469
3470 ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3471 name, name_len, NULL, NULL);
3472 if (ret == -ENOENT)
3473 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3474 data_len, type, ctx);
3475 else if (ret >= 0)
3476 ret = 0;
3477
3478 return ret;
3479}
3480
3481static int process_changed_xattr(struct send_ctx *sctx)
3482{
3483 int ret = 0;
3484
3485 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3486 sctx->cmp_key, __process_changed_new_xattr, sctx);
3487 if (ret < 0)
3488 goto out;
3489 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3490 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3491
3492out:
3493 return ret;
3494}
3495
3496static int process_all_new_xattrs(struct send_ctx *sctx)
3497{
3498 int ret;
3499 struct btrfs_root *root;
3500 struct btrfs_path *path;
3501 struct btrfs_key key;
3502 struct btrfs_key found_key;
3503 struct extent_buffer *eb;
3504 int slot;
3505
3506 path = alloc_path_for_send();
3507 if (!path)
3508 return -ENOMEM;
3509
3510 root = sctx->send_root;
3511
3512 key.objectid = sctx->cmp_key->objectid;
3513 key.type = BTRFS_XATTR_ITEM_KEY;
3514 key.offset = 0;
3515 while (1) {
3516 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3517 if (ret < 0)
3518 goto out;
3519 if (ret) {
3520 ret = 0;
3521 goto out;
3522 }
3523
3524 eb = path->nodes[0];
3525 slot = path->slots[0];
3526 btrfs_item_key_to_cpu(eb, &found_key, slot);
3527
3528 if (found_key.objectid != key.objectid ||
3529 found_key.type != key.type) {
3530 ret = 0;
3531 goto out;
3532 }
3533
3534 ret = iterate_dir_item(sctx, root, path, &found_key,
3535 __process_new_xattr, sctx);
3536 if (ret < 0)
3537 goto out;
3538
3539 btrfs_release_path(path);
3540 key.offset = found_key.offset + 1;
3541 }
3542
3543out:
3544 btrfs_free_path(path);
3545 return ret;
3546}
3547
3548/*
3549 * Read some bytes from the current inode/file and send a write command to
3550 * user space.
3551 */
3552static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3553{
3554 int ret = 0;
3555 struct fs_path *p;
3556 loff_t pos = offset;
b24baf69 3557 int readed = 0;
31db9f7c
AB
3558 mm_segment_t old_fs;
3559
3560 p = fs_path_alloc(sctx);
3561 if (!p)
3562 return -ENOMEM;
3563
3564 /*
3565 * vfs normally only accepts user space buffers for security reasons.
3566 * we only read from the file and also only provide the read_buf buffer
3567 * to vfs. As this buffer does not come from a user space call, it's
3568 * ok to temporary allow kernel space buffers.
3569 */
3570 old_fs = get_fs();
3571 set_fs(KERNEL_DS);
3572
3573verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3574
3575 ret = open_cur_inode_file(sctx);
3576 if (ret < 0)
3577 goto out;
3578
3579 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3580 if (ret < 0)
3581 goto out;
3582 readed = ret;
3583 if (!readed)
3584 goto out;
3585
3586 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3587 if (ret < 0)
3588 goto out;
3589
3590 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3591 if (ret < 0)
3592 goto out;
3593
3594 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3595 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3596 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, readed);
3597
3598 ret = send_cmd(sctx);
3599
3600tlv_put_failure:
3601out:
3602 fs_path_free(sctx, p);
3603 set_fs(old_fs);
3604 if (ret < 0)
3605 return ret;
3606 return readed;
3607}
3608
3609/*
3610 * Send a clone command to user space.
3611 */
3612static int send_clone(struct send_ctx *sctx,
3613 u64 offset, u32 len,
3614 struct clone_root *clone_root)
3615{
3616 int ret = 0;
3617 struct btrfs_root *clone_root2 = clone_root->root;
3618 struct fs_path *p;
3619 u64 gen;
3620
3621verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3622 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3623 clone_root->root->objectid, clone_root->ino,
3624 clone_root->offset);
3625
3626 p = fs_path_alloc(sctx);
3627 if (!p)
3628 return -ENOMEM;
3629
3630 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3631 if (ret < 0)
3632 goto out;
3633
3634 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3635 if (ret < 0)
3636 goto out;
3637
3638 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3639 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3640 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3641
3642 if (clone_root2 == sctx->send_root) {
3643 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3644 &gen, NULL, NULL, NULL);
3645 if (ret < 0)
3646 goto out;
3647 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3648 } else {
3649 ret = get_inode_path(sctx, clone_root2, clone_root->ino, p);
3650 }
3651 if (ret < 0)
3652 goto out;
3653
3654 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3655 clone_root2->root_item.uuid);
3656 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3657 clone_root2->root_item.ctransid);
3658 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3659 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3660 clone_root->offset);
3661
3662 ret = send_cmd(sctx);
3663
3664tlv_put_failure:
3665out:
3666 fs_path_free(sctx, p);
3667 return ret;
3668}
3669
3670static int send_write_or_clone(struct send_ctx *sctx,
3671 struct btrfs_path *path,
3672 struct btrfs_key *key,
3673 struct clone_root *clone_root)
3674{
3675 int ret = 0;
3676 struct btrfs_file_extent_item *ei;
3677 u64 offset = key->offset;
3678 u64 pos = 0;
3679 u64 len;
3680 u32 l;
3681 u8 type;
3682
3683 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3684 struct btrfs_file_extent_item);
3685 type = btrfs_file_extent_type(path->nodes[0], ei);
3686 if (type == BTRFS_FILE_EXTENT_INLINE)
3687 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3688 else
3689 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3690
3691 if (offset + len > sctx->cur_inode_size)
3692 len = sctx->cur_inode_size - offset;
3693 if (len == 0) {
3694 ret = 0;
3695 goto out;
3696 }
3697
3698 if (!clone_root) {
3699 while (pos < len) {
3700 l = len - pos;
3701 if (l > BTRFS_SEND_READ_SIZE)
3702 l = BTRFS_SEND_READ_SIZE;
3703 ret = send_write(sctx, pos + offset, l);
3704 if (ret < 0)
3705 goto out;
3706 if (!ret)
3707 break;
3708 pos += ret;
3709 }
3710 ret = 0;
3711 } else {
3712 ret = send_clone(sctx, offset, len, clone_root);
3713 }
3714
3715out:
3716 return ret;
3717}
3718
3719static int is_extent_unchanged(struct send_ctx *sctx,
3720 struct btrfs_path *left_path,
3721 struct btrfs_key *ekey)
3722{
3723 int ret = 0;
3724 struct btrfs_key key;
3725 struct btrfs_path *path = NULL;
3726 struct extent_buffer *eb;
3727 int slot;
3728 struct btrfs_key found_key;
3729 struct btrfs_file_extent_item *ei;
3730 u64 left_disknr;
3731 u64 right_disknr;
3732 u64 left_offset;
3733 u64 right_offset;
3734 u64 left_offset_fixed;
3735 u64 left_len;
3736 u64 right_len;
3737 u8 left_type;
3738 u8 right_type;
3739
3740 path = alloc_path_for_send();
3741 if (!path)
3742 return -ENOMEM;
3743
3744 eb = left_path->nodes[0];
3745 slot = left_path->slots[0];
3746
3747 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3748 left_type = btrfs_file_extent_type(eb, ei);
3749 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3750 left_len = btrfs_file_extent_num_bytes(eb, ei);
3751 left_offset = btrfs_file_extent_offset(eb, ei);
3752
3753 if (left_type != BTRFS_FILE_EXTENT_REG) {
3754 ret = 0;
3755 goto out;
3756 }
3757
3758 /*
3759 * Following comments will refer to these graphics. L is the left
3760 * extents which we are checking at the moment. 1-8 are the right
3761 * extents that we iterate.
3762 *
3763 * |-----L-----|
3764 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3765 *
3766 * |-----L-----|
3767 * |--1--|-2b-|...(same as above)
3768 *
3769 * Alternative situation. Happens on files where extents got split.
3770 * |-----L-----|
3771 * |-----------7-----------|-6-|
3772 *
3773 * Alternative situation. Happens on files which got larger.
3774 * |-----L-----|
3775 * |-8-|
3776 * Nothing follows after 8.
3777 */
3778
3779 key.objectid = ekey->objectid;
3780 key.type = BTRFS_EXTENT_DATA_KEY;
3781 key.offset = ekey->offset;
3782 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3783 if (ret < 0)
3784 goto out;
3785 if (ret) {
3786 ret = 0;
3787 goto out;
3788 }
3789
3790 /*
3791 * Handle special case where the right side has no extents at all.
3792 */
3793 eb = path->nodes[0];
3794 slot = path->slots[0];
3795 btrfs_item_key_to_cpu(eb, &found_key, slot);
3796 if (found_key.objectid != key.objectid ||
3797 found_key.type != key.type) {
3798 ret = 0;
3799 goto out;
3800 }
3801
3802 /*
3803 * We're now on 2a, 2b or 7.
3804 */
3805 key = found_key;
3806 while (key.offset < ekey->offset + left_len) {
3807 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3808 right_type = btrfs_file_extent_type(eb, ei);
3809 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3810 right_len = btrfs_file_extent_num_bytes(eb, ei);
3811 right_offset = btrfs_file_extent_offset(eb, ei);
3812
3813 if (right_type != BTRFS_FILE_EXTENT_REG) {
3814 ret = 0;
3815 goto out;
3816 }
3817
3818 /*
3819 * Are we at extent 8? If yes, we know the extent is changed.
3820 * This may only happen on the first iteration.
3821 */
3822 if (found_key.offset + right_len < ekey->offset) {
3823 ret = 0;
3824 goto out;
3825 }
3826
3827 left_offset_fixed = left_offset;
3828 if (key.offset < ekey->offset) {
3829 /* Fix the right offset for 2a and 7. */
3830 right_offset += ekey->offset - key.offset;
3831 } else {
3832 /* Fix the left offset for all behind 2a and 2b */
3833 left_offset_fixed += key.offset - ekey->offset;
3834 }
3835
3836 /*
3837 * Check if we have the same extent.
3838 */
3839 if (left_disknr + left_offset_fixed !=
3840 right_disknr + right_offset) {
3841 ret = 0;
3842 goto out;
3843 }
3844
3845 /*
3846 * Go to the next extent.
3847 */
3848 ret = btrfs_next_item(sctx->parent_root, path);
3849 if (ret < 0)
3850 goto out;
3851 if (!ret) {
3852 eb = path->nodes[0];
3853 slot = path->slots[0];
3854 btrfs_item_key_to_cpu(eb, &found_key, slot);
3855 }
3856 if (ret || found_key.objectid != key.objectid ||
3857 found_key.type != key.type) {
3858 key.offset += right_len;
3859 break;
3860 } else {
3861 if (found_key.offset != key.offset + right_len) {
3862 /* Should really not happen */
3863 ret = -EIO;
3864 goto out;
3865 }
3866 }
3867 key = found_key;
3868 }
3869
3870 /*
3871 * We're now behind the left extent (treat as unchanged) or at the end
3872 * of the right side (treat as changed).
3873 */
3874 if (key.offset >= ekey->offset + left_len)
3875 ret = 1;
3876 else
3877 ret = 0;
3878
3879
3880out:
3881 btrfs_free_path(path);
3882 return ret;
3883}
3884
3885static int process_extent(struct send_ctx *sctx,
3886 struct btrfs_path *path,
3887 struct btrfs_key *key)
3888{
3889 int ret = 0;
3890 struct clone_root *found_clone = NULL;
3891
3892 if (S_ISLNK(sctx->cur_inode_mode))
3893 return 0;
3894
3895 if (sctx->parent_root && !sctx->cur_inode_new) {
3896 ret = is_extent_unchanged(sctx, path, key);
3897 if (ret < 0)
3898 goto out;
3899 if (ret) {
3900 ret = 0;
3901 goto out;
3902 }
3903 }
3904
3905 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
3906 sctx->cur_inode_size, &found_clone);
3907 if (ret != -ENOENT && ret < 0)
3908 goto out;
3909
3910 ret = send_write_or_clone(sctx, path, key, found_clone);
3911
3912out:
3913 return ret;
3914}
3915
3916static int process_all_extents(struct send_ctx *sctx)
3917{
3918 int ret;
3919 struct btrfs_root *root;
3920 struct btrfs_path *path;
3921 struct btrfs_key key;
3922 struct btrfs_key found_key;
3923 struct extent_buffer *eb;
3924 int slot;
3925
3926 root = sctx->send_root;
3927 path = alloc_path_for_send();
3928 if (!path)
3929 return -ENOMEM;
3930
3931 key.objectid = sctx->cmp_key->objectid;
3932 key.type = BTRFS_EXTENT_DATA_KEY;
3933 key.offset = 0;
3934 while (1) {
3935 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3936 if (ret < 0)
3937 goto out;
3938 if (ret) {
3939 ret = 0;
3940 goto out;
3941 }
3942
3943 eb = path->nodes[0];
3944 slot = path->slots[0];
3945 btrfs_item_key_to_cpu(eb, &found_key, slot);
3946
3947 if (found_key.objectid != key.objectid ||
3948 found_key.type != key.type) {
3949 ret = 0;
3950 goto out;
3951 }
3952
3953 ret = process_extent(sctx, path, &found_key);
3954 if (ret < 0)
3955 goto out;
3956
3957 btrfs_release_path(path);
3958 key.offset = found_key.offset + 1;
3959 }
3960
3961out:
3962 btrfs_free_path(path);
3963 return ret;
3964}
3965
3966static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
3967{
3968 int ret = 0;
3969
3970 if (sctx->cur_ino == 0)
3971 goto out;
3972 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
3973 sctx->cmp_key->type <= BTRFS_INODE_REF_KEY)
3974 goto out;
3975 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
3976 goto out;
3977
3978 ret = process_recorded_refs(sctx);
3979
3980out:
3981 return ret;
3982}
3983
3984static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
3985{
3986 int ret = 0;
3987 u64 left_mode;
3988 u64 left_uid;
3989 u64 left_gid;
3990 u64 right_mode;
3991 u64 right_uid;
3992 u64 right_gid;
3993 int need_chmod = 0;
3994 int need_chown = 0;
3995
3996 ret = process_recorded_refs_if_needed(sctx, at_end);
3997 if (ret < 0)
3998 goto out;
3999
4000 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4001 goto out;
4002 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4003 goto out;
4004
4005 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4006 &left_mode, &left_uid, &left_gid);
4007 if (ret < 0)
4008 goto out;
4009
4010 if (!S_ISLNK(sctx->cur_inode_mode)) {
4011 if (!sctx->parent_root || sctx->cur_inode_new) {
4012 need_chmod = 1;
4013 need_chown = 1;
4014 } else {
4015 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4016 NULL, NULL, &right_mode, &right_uid,
4017 &right_gid);
4018 if (ret < 0)
4019 goto out;
4020
4021 if (left_uid != right_uid || left_gid != right_gid)
4022 need_chown = 1;
4023 if (left_mode != right_mode)
4024 need_chmod = 1;
4025 }
4026 }
4027
4028 if (S_ISREG(sctx->cur_inode_mode)) {
4029 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4030 sctx->cur_inode_size);
4031 if (ret < 0)
4032 goto out;
4033 }
4034
4035 if (need_chown) {
4036 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4037 left_uid, left_gid);
4038 if (ret < 0)
4039 goto out;
4040 }
4041 if (need_chmod) {
4042 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4043 left_mode);
4044 if (ret < 0)
4045 goto out;
4046 }
4047
4048 /*
4049 * Need to send that every time, no matter if it actually changed
4050 * between the two trees as we have done changes to the inode before.
4051 */
4052 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4053 if (ret < 0)
4054 goto out;
4055
4056out:
4057 return ret;
4058}
4059
4060static int changed_inode(struct send_ctx *sctx,
4061 enum btrfs_compare_tree_result result)
4062{
4063 int ret = 0;
4064 struct btrfs_key *key = sctx->cmp_key;
4065 struct btrfs_inode_item *left_ii = NULL;
4066 struct btrfs_inode_item *right_ii = NULL;
4067 u64 left_gen = 0;
4068 u64 right_gen = 0;
4069
4070 ret = close_cur_inode_file(sctx);
4071 if (ret < 0)
4072 goto out;
4073
4074 sctx->cur_ino = key->objectid;
4075 sctx->cur_inode_new_gen = 0;
4076 sctx->cur_inode_first_ref_orphan = 0;
4077 sctx->send_progress = sctx->cur_ino;
4078
4079 if (result == BTRFS_COMPARE_TREE_NEW ||
4080 result == BTRFS_COMPARE_TREE_CHANGED) {
4081 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4082 sctx->left_path->slots[0],
4083 struct btrfs_inode_item);
4084 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4085 left_ii);
4086 } else {
4087 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4088 sctx->right_path->slots[0],
4089 struct btrfs_inode_item);
4090 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4091 right_ii);
4092 }
4093 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4094 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4095 sctx->right_path->slots[0],
4096 struct btrfs_inode_item);
4097
4098 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4099 right_ii);
4100 if (left_gen != right_gen)
4101 sctx->cur_inode_new_gen = 1;
4102 }
4103
4104 if (result == BTRFS_COMPARE_TREE_NEW) {
4105 sctx->cur_inode_gen = left_gen;
4106 sctx->cur_inode_new = 1;
4107 sctx->cur_inode_deleted = 0;
4108 sctx->cur_inode_size = btrfs_inode_size(
4109 sctx->left_path->nodes[0], left_ii);
4110 sctx->cur_inode_mode = btrfs_inode_mode(
4111 sctx->left_path->nodes[0], left_ii);
4112 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4113 ret = send_create_inode(sctx, sctx->left_path,
4114 sctx->cmp_key);
4115 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4116 sctx->cur_inode_gen = right_gen;
4117 sctx->cur_inode_new = 0;
4118 sctx->cur_inode_deleted = 1;
4119 sctx->cur_inode_size = btrfs_inode_size(
4120 sctx->right_path->nodes[0], right_ii);
4121 sctx->cur_inode_mode = btrfs_inode_mode(
4122 sctx->right_path->nodes[0], right_ii);
4123 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4124 if (sctx->cur_inode_new_gen) {
4125 sctx->cur_inode_gen = right_gen;
4126 sctx->cur_inode_new = 0;
4127 sctx->cur_inode_deleted = 1;
4128 sctx->cur_inode_size = btrfs_inode_size(
4129 sctx->right_path->nodes[0], right_ii);
4130 sctx->cur_inode_mode = btrfs_inode_mode(
4131 sctx->right_path->nodes[0], right_ii);
4132 ret = process_all_refs(sctx,
4133 BTRFS_COMPARE_TREE_DELETED);
4134 if (ret < 0)
4135 goto out;
4136
4137 sctx->cur_inode_gen = left_gen;
4138 sctx->cur_inode_new = 1;
4139 sctx->cur_inode_deleted = 0;
4140 sctx->cur_inode_size = btrfs_inode_size(
4141 sctx->left_path->nodes[0], left_ii);
4142 sctx->cur_inode_mode = btrfs_inode_mode(
4143 sctx->left_path->nodes[0], left_ii);
4144 ret = send_create_inode(sctx, sctx->left_path,
4145 sctx->cmp_key);
4146 if (ret < 0)
4147 goto out;
4148
4149 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4150 if (ret < 0)
4151 goto out;
4152 ret = process_all_extents(sctx);
4153 if (ret < 0)
4154 goto out;
4155 ret = process_all_new_xattrs(sctx);
4156 if (ret < 0)
4157 goto out;
4158 } else {
4159 sctx->cur_inode_gen = left_gen;
4160 sctx->cur_inode_new = 0;
4161 sctx->cur_inode_new_gen = 0;
4162 sctx->cur_inode_deleted = 0;
4163 sctx->cur_inode_size = btrfs_inode_size(
4164 sctx->left_path->nodes[0], left_ii);
4165 sctx->cur_inode_mode = btrfs_inode_mode(
4166 sctx->left_path->nodes[0], left_ii);
4167 }
4168 }
4169
4170out:
4171 return ret;
4172}
4173
4174static int changed_ref(struct send_ctx *sctx,
4175 enum btrfs_compare_tree_result result)
4176{
4177 int ret = 0;
4178
4179 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4180
4181 if (!sctx->cur_inode_new_gen &&
4182 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4183 if (result == BTRFS_COMPARE_TREE_NEW)
4184 ret = record_new_ref(sctx);
4185 else if (result == BTRFS_COMPARE_TREE_DELETED)
4186 ret = record_deleted_ref(sctx);
4187 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4188 ret = record_changed_ref(sctx);
4189 }
4190
4191 return ret;
4192}
4193
4194static int changed_xattr(struct send_ctx *sctx,
4195 enum btrfs_compare_tree_result result)
4196{
4197 int ret = 0;
4198
4199 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4200
4201 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4202 if (result == BTRFS_COMPARE_TREE_NEW)
4203 ret = process_new_xattr(sctx);
4204 else if (result == BTRFS_COMPARE_TREE_DELETED)
4205 ret = process_deleted_xattr(sctx);
4206 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4207 ret = process_changed_xattr(sctx);
4208 }
4209
4210 return ret;
4211}
4212
4213static int changed_extent(struct send_ctx *sctx,
4214 enum btrfs_compare_tree_result result)
4215{
4216 int ret = 0;
4217
4218 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4219
4220 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4221 if (result != BTRFS_COMPARE_TREE_DELETED)
4222 ret = process_extent(sctx, sctx->left_path,
4223 sctx->cmp_key);
4224 }
4225
4226 return ret;
4227}
4228
4229
4230static int changed_cb(struct btrfs_root *left_root,
4231 struct btrfs_root *right_root,
4232 struct btrfs_path *left_path,
4233 struct btrfs_path *right_path,
4234 struct btrfs_key *key,
4235 enum btrfs_compare_tree_result result,
4236 void *ctx)
4237{
4238 int ret = 0;
4239 struct send_ctx *sctx = ctx;
4240
4241 sctx->left_path = left_path;
4242 sctx->right_path = right_path;
4243 sctx->cmp_key = key;
4244
4245 ret = finish_inode_if_needed(sctx, 0);
4246 if (ret < 0)
4247 goto out;
4248
4249 if (key->type == BTRFS_INODE_ITEM_KEY)
4250 ret = changed_inode(sctx, result);
4251 else if (key->type == BTRFS_INODE_REF_KEY)
4252 ret = changed_ref(sctx, result);
4253 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4254 ret = changed_xattr(sctx, result);
4255 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4256 ret = changed_extent(sctx, result);
4257
4258out:
4259 return ret;
4260}
4261
4262static int full_send_tree(struct send_ctx *sctx)
4263{
4264 int ret;
4265 struct btrfs_trans_handle *trans = NULL;
4266 struct btrfs_root *send_root = sctx->send_root;
4267 struct btrfs_key key;
4268 struct btrfs_key found_key;
4269 struct btrfs_path *path;
4270 struct extent_buffer *eb;
4271 int slot;
4272 u64 start_ctransid;
4273 u64 ctransid;
4274
4275 path = alloc_path_for_send();
4276 if (!path)
4277 return -ENOMEM;
4278
4279 spin_lock(&send_root->root_times_lock);
4280 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4281 spin_unlock(&send_root->root_times_lock);
4282
4283 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4284 key.type = BTRFS_INODE_ITEM_KEY;
4285 key.offset = 0;
4286
4287join_trans:
4288 /*
4289 * We need to make sure the transaction does not get committed
4290 * while we do anything on commit roots. Join a transaction to prevent
4291 * this.
4292 */
4293 trans = btrfs_join_transaction(send_root);
4294 if (IS_ERR(trans)) {
4295 ret = PTR_ERR(trans);
4296 trans = NULL;
4297 goto out;
4298 }
4299
4300 /*
4301 * Make sure the tree has not changed
4302 */
4303 spin_lock(&send_root->root_times_lock);
4304 ctransid = btrfs_root_ctransid(&send_root->root_item);
4305 spin_unlock(&send_root->root_times_lock);
4306
4307 if (ctransid != start_ctransid) {
4308 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4309 "send was modified in between. This is "
4310 "probably a bug.\n");
4311 ret = -EIO;
4312 goto out;
4313 }
4314
4315 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4316 if (ret < 0)
4317 goto out;
4318 if (ret)
4319 goto out_finish;
4320
4321 while (1) {
4322 /*
4323 * When someone want to commit while we iterate, end the
4324 * joined transaction and rejoin.
4325 */
4326 if (btrfs_should_end_transaction(trans, send_root)) {
4327 ret = btrfs_end_transaction(trans, send_root);
4328 trans = NULL;
4329 if (ret < 0)
4330 goto out;
4331 btrfs_release_path(path);
4332 goto join_trans;
4333 }
4334
4335 eb = path->nodes[0];
4336 slot = path->slots[0];
4337 btrfs_item_key_to_cpu(eb, &found_key, slot);
4338
4339 ret = changed_cb(send_root, NULL, path, NULL,
4340 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4341 if (ret < 0)
4342 goto out;
4343
4344 key.objectid = found_key.objectid;
4345 key.type = found_key.type;
4346 key.offset = found_key.offset + 1;
4347
4348 ret = btrfs_next_item(send_root, path);
4349 if (ret < 0)
4350 goto out;
4351 if (ret) {
4352 ret = 0;
4353 break;
4354 }
4355 }
4356
4357out_finish:
4358 ret = finish_inode_if_needed(sctx, 1);
4359
4360out:
4361 btrfs_free_path(path);
4362 if (trans) {
4363 if (!ret)
4364 ret = btrfs_end_transaction(trans, send_root);
4365 else
4366 btrfs_end_transaction(trans, send_root);
4367 }
4368 return ret;
4369}
4370
4371static int send_subvol(struct send_ctx *sctx)
4372{
4373 int ret;
4374
4375 ret = send_header(sctx);
4376 if (ret < 0)
4377 goto out;
4378
4379 ret = send_subvol_begin(sctx);
4380 if (ret < 0)
4381 goto out;
4382
4383 if (sctx->parent_root) {
4384 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4385 changed_cb, sctx);
4386 if (ret < 0)
4387 goto out;
4388 ret = finish_inode_if_needed(sctx, 1);
4389 if (ret < 0)
4390 goto out;
4391 } else {
4392 ret = full_send_tree(sctx);
4393 if (ret < 0)
4394 goto out;
4395 }
4396
4397out:
4398 if (!ret)
4399 ret = close_cur_inode_file(sctx);
4400 else
4401 close_cur_inode_file(sctx);
4402
4403 free_recorded_refs(sctx);
4404 return ret;
4405}
4406
4407long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4408{
4409 int ret = 0;
4410 struct btrfs_root *send_root;
4411 struct btrfs_root *clone_root;
4412 struct btrfs_fs_info *fs_info;
4413 struct btrfs_ioctl_send_args *arg = NULL;
4414 struct btrfs_key key;
4415 struct file *filp = NULL;
4416 struct send_ctx *sctx = NULL;
4417 u32 i;
4418 u64 *clone_sources_tmp = NULL;
4419
4420 if (!capable(CAP_SYS_ADMIN))
4421 return -EPERM;
4422
4423 send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root;
4424 fs_info = send_root->fs_info;
4425
4426 arg = memdup_user(arg_, sizeof(*arg));
4427 if (IS_ERR(arg)) {
4428 ret = PTR_ERR(arg);
4429 arg = NULL;
4430 goto out;
4431 }
4432
4433 if (!access_ok(VERIFY_READ, arg->clone_sources,
4434 sizeof(*arg->clone_sources *
4435 arg->clone_sources_count))) {
4436 ret = -EFAULT;
4437 goto out;
4438 }
4439
4440 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4441 if (!sctx) {
4442 ret = -ENOMEM;
4443 goto out;
4444 }
4445
4446 INIT_LIST_HEAD(&sctx->new_refs);
4447 INIT_LIST_HEAD(&sctx->deleted_refs);
4448 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4449 INIT_LIST_HEAD(&sctx->name_cache_list);
4450
4451 sctx->send_filp = fget(arg->send_fd);
4452 if (IS_ERR(sctx->send_filp)) {
4453 ret = PTR_ERR(sctx->send_filp);
4454 goto out;
4455 }
4456
4457 sctx->mnt = mnt_file->f_path.mnt;
4458
4459 sctx->send_root = send_root;
4460 sctx->clone_roots_cnt = arg->clone_sources_count;
4461
4462 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4463 sctx->send_buf = vmalloc(sctx->send_max_size);
4464 if (!sctx->send_buf) {
4465 ret = -ENOMEM;
4466 goto out;
4467 }
4468
4469 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4470 if (!sctx->read_buf) {
4471 ret = -ENOMEM;
4472 goto out;
4473 }
4474
4475 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4476 (arg->clone_sources_count + 1));
4477 if (!sctx->clone_roots) {
4478 ret = -ENOMEM;
4479 goto out;
4480 }
4481
4482 if (arg->clone_sources_count) {
4483 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4484 sizeof(*arg->clone_sources));
4485 if (!clone_sources_tmp) {
4486 ret = -ENOMEM;
4487 goto out;
4488 }
4489
4490 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4491 arg->clone_sources_count *
4492 sizeof(*arg->clone_sources));
4493 if (ret) {
4494 ret = -EFAULT;
4495 goto out;
4496 }
4497
4498 for (i = 0; i < arg->clone_sources_count; i++) {
4499 key.objectid = clone_sources_tmp[i];
4500 key.type = BTRFS_ROOT_ITEM_KEY;
4501 key.offset = (u64)-1;
4502 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4503 if (!clone_root) {
4504 ret = -EINVAL;
4505 goto out;
4506 }
4507 if (IS_ERR(clone_root)) {
4508 ret = PTR_ERR(clone_root);
4509 goto out;
4510 }
4511 sctx->clone_roots[i].root = clone_root;
4512 }
4513 vfree(clone_sources_tmp);
4514 clone_sources_tmp = NULL;
4515 }
4516
4517 if (arg->parent_root) {
4518 key.objectid = arg->parent_root;
4519 key.type = BTRFS_ROOT_ITEM_KEY;
4520 key.offset = (u64)-1;
4521 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4522 if (!sctx->parent_root) {
4523 ret = -EINVAL;
4524 goto out;
4525 }
4526 }
4527
4528 /*
4529 * Clones from send_root are allowed, but only if the clone source
4530 * is behind the current send position. This is checked while searching
4531 * for possible clone sources.
4532 */
4533 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4534
4535 /* We do a bsearch later */
4536 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4537 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4538 NULL);
4539
4540 ret = send_subvol(sctx);
4541 if (ret < 0)
4542 goto out;
4543
4544 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4545 if (ret < 0)
4546 goto out;
4547 ret = send_cmd(sctx);
4548 if (ret < 0)
4549 goto out;
4550
4551out:
4552 if (filp)
4553 fput(filp);
4554 kfree(arg);
4555 vfree(clone_sources_tmp);
4556
4557 if (sctx) {
4558 if (sctx->send_filp)
4559 fput(sctx->send_filp);
4560
4561 vfree(sctx->clone_roots);
4562 vfree(sctx->send_buf);
4563 vfree(sctx->read_buf);
4564
4565 name_cache_free(sctx);
4566
4567 kfree(sctx);
4568 }
4569
4570 return ret;
4571}