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