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