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