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