blob: d17d75ebc482cf1f0a6d0b2ffda500ad0dfedce8 [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;
Alexander Block31db9f72012-07-25 23:19:24 +0200867 struct btrfs_key di_key;
868 char *buf = NULL;
869 char *buf2 = NULL;
870 int buf_len;
871 int buf_virtual = 0;
872 u32 name_len;
873 u32 data_len;
874 u32 cur;
875 u32 len;
876 u32 total;
877 int slot;
878 int num;
879 u8 type;
880
881 buf_len = PAGE_SIZE;
882 buf = kmalloc(buf_len, GFP_NOFS);
883 if (!buf) {
884 ret = -ENOMEM;
885 goto out;
886 }
887
Alexander Block31db9f72012-07-25 23:19:24 +0200888 eb = path->nodes[0];
889 slot = path->slots[0];
890 item = btrfs_item_nr(eb, slot);
891 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
892 cur = 0;
893 len = 0;
894 total = btrfs_item_size(eb, item);
895
896 num = 0;
897 while (cur < total) {
898 name_len = btrfs_dir_name_len(eb, di);
899 data_len = btrfs_dir_data_len(eb, di);
900 type = btrfs_dir_type(eb, di);
901 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
902
903 if (name_len + data_len > buf_len) {
904 buf_len = PAGE_ALIGN(name_len + data_len);
905 if (buf_virtual) {
906 buf2 = vmalloc(buf_len);
907 if (!buf2) {
908 ret = -ENOMEM;
909 goto out;
910 }
911 vfree(buf);
912 } else {
913 buf2 = krealloc(buf, buf_len, GFP_NOFS);
914 if (!buf2) {
915 buf2 = vmalloc(buf_len);
916 if (!buf2) {
917 ret = -ENOMEM;
918 goto out;
919 }
920 kfree(buf);
921 buf_virtual = 1;
922 }
923 }
924
925 buf = buf2;
926 buf2 = NULL;
927 }
928
929 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
930 name_len + data_len);
931
932 len = sizeof(*di) + name_len + data_len;
933 di = (struct btrfs_dir_item *)((char *)di + len);
934 cur += len;
935
936 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
937 data_len, type, ctx);
938 if (ret < 0)
939 goto out;
940 if (ret) {
941 ret = 0;
942 goto out;
943 }
944
945 num++;
946 }
947
948out:
Alexander Block31db9f72012-07-25 23:19:24 +0200949 if (buf_virtual)
950 vfree(buf);
951 else
952 kfree(buf);
953 return ret;
954}
955
956static int __copy_first_ref(int num, u64 dir, int index,
957 struct fs_path *p, void *ctx)
958{
959 int ret;
960 struct fs_path *pt = ctx;
961
962 ret = fs_path_copy(pt, p);
963 if (ret < 0)
964 return ret;
965
966 /* we want the first only */
967 return 1;
968}
969
970/*
971 * Retrieve the first path of an inode. If an inode has more then one
972 * ref/hardlink, this is ignored.
973 */
974static int get_inode_path(struct send_ctx *sctx, struct btrfs_root *root,
975 u64 ino, struct fs_path *path)
976{
977 int ret;
978 struct btrfs_key key, found_key;
979 struct btrfs_path *p;
980
981 p = alloc_path_for_send();
982 if (!p)
983 return -ENOMEM;
984
985 fs_path_reset(path);
986
987 key.objectid = ino;
988 key.type = BTRFS_INODE_REF_KEY;
989 key.offset = 0;
990
991 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
992 if (ret < 0)
993 goto out;
994 if (ret) {
995 ret = 1;
996 goto out;
997 }
998 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
999 if (found_key.objectid != ino ||
1000 found_key.type != BTRFS_INODE_REF_KEY) {
1001 ret = -ENOENT;
1002 goto out;
1003 }
1004
1005 ret = iterate_inode_ref(sctx, root, p, &found_key, 1,
1006 __copy_first_ref, path);
1007 if (ret < 0)
1008 goto out;
1009 ret = 0;
1010
1011out:
1012 btrfs_free_path(p);
1013 return ret;
1014}
1015
1016struct backref_ctx {
1017 struct send_ctx *sctx;
1018
1019 /* number of total found references */
1020 u64 found;
1021
1022 /*
1023 * used for clones found in send_root. clones found behind cur_objectid
1024 * and cur_offset are not considered as allowed clones.
1025 */
1026 u64 cur_objectid;
1027 u64 cur_offset;
1028
1029 /* may be truncated in case it's the last extent in a file */
1030 u64 extent_len;
1031
1032 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001033 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001034};
1035
1036static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1037{
1038 u64 root = (u64)key;
1039 struct clone_root *cr = (struct clone_root *)elt;
1040
1041 if (root < cr->root->objectid)
1042 return -1;
1043 if (root > cr->root->objectid)
1044 return 1;
1045 return 0;
1046}
1047
1048static int __clone_root_cmp_sort(const void *e1, const void *e2)
1049{
1050 struct clone_root *cr1 = (struct clone_root *)e1;
1051 struct clone_root *cr2 = (struct clone_root *)e2;
1052
1053 if (cr1->root->objectid < cr2->root->objectid)
1054 return -1;
1055 if (cr1->root->objectid > cr2->root->objectid)
1056 return 1;
1057 return 0;
1058}
1059
1060/*
1061 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001062 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001063 */
1064static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1065{
1066 struct backref_ctx *bctx = ctx_;
1067 struct clone_root *found;
1068 int ret;
1069 u64 i_size;
1070
1071 /* First check if the root is in the list of accepted clone sources */
1072 found = bsearch((void *)root, bctx->sctx->clone_roots,
1073 bctx->sctx->clone_roots_cnt,
1074 sizeof(struct clone_root),
1075 __clone_root_cmp_bsearch);
1076 if (!found)
1077 return 0;
1078
1079 if (found->root == bctx->sctx->send_root &&
1080 ino == bctx->cur_objectid &&
1081 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001082 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001083 }
1084
1085 /*
Alexander Block766702e2012-07-28 14:11:31 +02001086 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001087 * accept clones from these extents.
1088 */
Alexander Block85a7b332012-07-26 23:39:10 +02001089 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1090 NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001091 if (ret < 0)
1092 return ret;
1093
1094 if (offset + bctx->extent_len > i_size)
1095 return 0;
1096
1097 /*
1098 * Make sure we don't consider clones from send_root that are
1099 * behind the current inode/offset.
1100 */
1101 if (found->root == bctx->sctx->send_root) {
1102 /*
1103 * TODO for the moment we don't accept clones from the inode
1104 * that is currently send. We may change this when
1105 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1106 * file.
1107 */
1108 if (ino >= bctx->cur_objectid)
1109 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001110#if 0
1111 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001112 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001113 if (offset + bctx->extent_len > bctx->cur_offset)
1114 return 0;
1115#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001116 }
1117
1118 bctx->found++;
1119 found->found_refs++;
1120 if (ino < found->ino) {
1121 found->ino = ino;
1122 found->offset = offset;
1123 } else if (found->ino == ino) {
1124 /*
1125 * same extent found more then once in the same file.
1126 */
1127 if (found->offset > offset + bctx->extent_len)
1128 found->offset = offset;
1129 }
1130
1131 return 0;
1132}
1133
1134/*
Alexander Block766702e2012-07-28 14:11:31 +02001135 * Given an inode, offset and extent item, it finds a good clone for a clone
1136 * instruction. Returns -ENOENT when none could be found. The function makes
1137 * sure that the returned clone is usable at the point where sending is at the
1138 * moment. This means, that no clones are accepted which lie behind the current
1139 * inode+offset.
1140 *
Alexander Block31db9f72012-07-25 23:19:24 +02001141 * path must point to the extent item when called.
1142 */
1143static int find_extent_clone(struct send_ctx *sctx,
1144 struct btrfs_path *path,
1145 u64 ino, u64 data_offset,
1146 u64 ino_size,
1147 struct clone_root **found)
1148{
1149 int ret;
1150 int extent_type;
1151 u64 logical;
1152 u64 num_bytes;
1153 u64 extent_item_pos;
1154 struct btrfs_file_extent_item *fi;
1155 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001156 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001157 struct clone_root *cur_clone_root;
1158 struct btrfs_key found_key;
1159 struct btrfs_path *tmp_path;
1160 u32 i;
1161
1162 tmp_path = alloc_path_for_send();
1163 if (!tmp_path)
1164 return -ENOMEM;
1165
Alexander Block35075bb2012-07-28 12:44:34 +02001166 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1167 if (!backref_ctx) {
1168 ret = -ENOMEM;
1169 goto out;
1170 }
1171
Alexander Block31db9f72012-07-25 23:19:24 +02001172 if (data_offset >= ino_size) {
1173 /*
1174 * There may be extents that lie behind the file's size.
1175 * I at least had this in combination with snapshotting while
1176 * writing large files.
1177 */
1178 ret = 0;
1179 goto out;
1180 }
1181
1182 fi = btrfs_item_ptr(eb, path->slots[0],
1183 struct btrfs_file_extent_item);
1184 extent_type = btrfs_file_extent_type(eb, fi);
1185 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1186 ret = -ENOENT;
1187 goto out;
1188 }
1189
1190 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1191 logical = btrfs_file_extent_disk_bytenr(eb, fi);
1192 if (logical == 0) {
1193 ret = -ENOENT;
1194 goto out;
1195 }
1196 logical += btrfs_file_extent_offset(eb, fi);
1197
1198 ret = extent_from_logical(sctx->send_root->fs_info,
1199 logical, tmp_path, &found_key);
1200 btrfs_release_path(tmp_path);
1201
1202 if (ret < 0)
1203 goto out;
1204 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1205 ret = -EIO;
1206 goto out;
1207 }
1208
1209 /*
1210 * Setup the clone roots.
1211 */
1212 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1213 cur_clone_root = sctx->clone_roots + i;
1214 cur_clone_root->ino = (u64)-1;
1215 cur_clone_root->offset = 0;
1216 cur_clone_root->found_refs = 0;
1217 }
1218
Alexander Block35075bb2012-07-28 12:44:34 +02001219 backref_ctx->sctx = sctx;
1220 backref_ctx->found = 0;
1221 backref_ctx->cur_objectid = ino;
1222 backref_ctx->cur_offset = data_offset;
1223 backref_ctx->found_itself = 0;
1224 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001225
1226 /*
1227 * The last extent of a file may be too large due to page alignment.
1228 * We need to adjust extent_len in this case so that the checks in
1229 * __iterate_backrefs work.
1230 */
1231 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001232 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001233
1234 /*
1235 * Now collect all backrefs.
1236 */
1237 extent_item_pos = logical - found_key.objectid;
1238 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1239 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001240 __iterate_backrefs, backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001241 if (ret < 0)
1242 goto out;
1243
Alexander Block35075bb2012-07-28 12:44:34 +02001244 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001245 /* found a bug in backref code? */
1246 ret = -EIO;
1247 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1248 "send_root. inode=%llu, offset=%llu, "
1249 "logical=%llu\n",
1250 ino, data_offset, logical);
1251 goto out;
1252 }
1253
1254verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1255 "ino=%llu, "
1256 "num_bytes=%llu, logical=%llu\n",
1257 data_offset, ino, num_bytes, logical);
1258
Alexander Block35075bb2012-07-28 12:44:34 +02001259 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001260 verbose_printk("btrfs: no clones found\n");
1261
1262 cur_clone_root = NULL;
1263 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1264 if (sctx->clone_roots[i].found_refs) {
1265 if (!cur_clone_root)
1266 cur_clone_root = sctx->clone_roots + i;
1267 else if (sctx->clone_roots[i].root == sctx->send_root)
1268 /* prefer clones from send_root over others */
1269 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001270 }
1271
1272 }
1273
1274 if (cur_clone_root) {
1275 *found = cur_clone_root;
1276 ret = 0;
1277 } else {
1278 ret = -ENOENT;
1279 }
1280
1281out:
1282 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001283 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001284 return ret;
1285}
1286
1287static int read_symlink(struct send_ctx *sctx,
1288 struct btrfs_root *root,
1289 u64 ino,
1290 struct fs_path *dest)
1291{
1292 int ret;
1293 struct btrfs_path *path;
1294 struct btrfs_key key;
1295 struct btrfs_file_extent_item *ei;
1296 u8 type;
1297 u8 compression;
1298 unsigned long off;
1299 int len;
1300
1301 path = alloc_path_for_send();
1302 if (!path)
1303 return -ENOMEM;
1304
1305 key.objectid = ino;
1306 key.type = BTRFS_EXTENT_DATA_KEY;
1307 key.offset = 0;
1308 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1309 if (ret < 0)
1310 goto out;
1311 BUG_ON(ret);
1312
1313 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1314 struct btrfs_file_extent_item);
1315 type = btrfs_file_extent_type(path->nodes[0], ei);
1316 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1317 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1318 BUG_ON(compression);
1319
1320 off = btrfs_file_extent_inline_start(ei);
1321 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1322
1323 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001324
1325out:
1326 btrfs_free_path(path);
1327 return ret;
1328}
1329
1330/*
1331 * Helper function to generate a file name that is unique in the root of
1332 * send_root and parent_root. This is used to generate names for orphan inodes.
1333 */
1334static int gen_unique_name(struct send_ctx *sctx,
1335 u64 ino, u64 gen,
1336 struct fs_path *dest)
1337{
1338 int ret = 0;
1339 struct btrfs_path *path;
1340 struct btrfs_dir_item *di;
1341 char tmp[64];
1342 int len;
1343 u64 idx = 0;
1344
1345 path = alloc_path_for_send();
1346 if (!path)
1347 return -ENOMEM;
1348
1349 while (1) {
1350 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1351 ino, gen, idx);
1352 if (len >= sizeof(tmp)) {
1353 /* should really not happen */
1354 ret = -EOVERFLOW;
1355 goto out;
1356 }
1357
1358 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1359 path, BTRFS_FIRST_FREE_OBJECTID,
1360 tmp, strlen(tmp), 0);
1361 btrfs_release_path(path);
1362 if (IS_ERR(di)) {
1363 ret = PTR_ERR(di);
1364 goto out;
1365 }
1366 if (di) {
1367 /* not unique, try again */
1368 idx++;
1369 continue;
1370 }
1371
1372 if (!sctx->parent_root) {
1373 /* unique */
1374 ret = 0;
1375 break;
1376 }
1377
1378 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1379 path, BTRFS_FIRST_FREE_OBJECTID,
1380 tmp, strlen(tmp), 0);
1381 btrfs_release_path(path);
1382 if (IS_ERR(di)) {
1383 ret = PTR_ERR(di);
1384 goto out;
1385 }
1386 if (di) {
1387 /* not unique, try again */
1388 idx++;
1389 continue;
1390 }
1391 /* unique */
1392 break;
1393 }
1394
1395 ret = fs_path_add(dest, tmp, strlen(tmp));
1396
1397out:
1398 btrfs_free_path(path);
1399 return ret;
1400}
1401
1402enum inode_state {
1403 inode_state_no_change,
1404 inode_state_will_create,
1405 inode_state_did_create,
1406 inode_state_will_delete,
1407 inode_state_did_delete,
1408};
1409
1410static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1411{
1412 int ret;
1413 int left_ret;
1414 int right_ret;
1415 u64 left_gen;
1416 u64 right_gen;
1417
1418 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001419 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001420 if (ret < 0 && ret != -ENOENT)
1421 goto out;
1422 left_ret = ret;
1423
1424 if (!sctx->parent_root) {
1425 right_ret = -ENOENT;
1426 } else {
1427 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001428 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001429 if (ret < 0 && ret != -ENOENT)
1430 goto out;
1431 right_ret = ret;
1432 }
1433
1434 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001435 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001436 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001437 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001438 if (ino < sctx->send_progress)
1439 ret = inode_state_did_create;
1440 else
1441 ret = inode_state_will_create;
1442 } else if (right_gen == gen) {
1443 if (ino < sctx->send_progress)
1444 ret = inode_state_did_delete;
1445 else
1446 ret = inode_state_will_delete;
1447 } else {
1448 ret = -ENOENT;
1449 }
1450 } else if (!left_ret) {
1451 if (left_gen == gen) {
1452 if (ino < sctx->send_progress)
1453 ret = inode_state_did_create;
1454 else
1455 ret = inode_state_will_create;
1456 } else {
1457 ret = -ENOENT;
1458 }
1459 } else if (!right_ret) {
1460 if (right_gen == gen) {
1461 if (ino < sctx->send_progress)
1462 ret = inode_state_did_delete;
1463 else
1464 ret = inode_state_will_delete;
1465 } else {
1466 ret = -ENOENT;
1467 }
1468 } else {
1469 ret = -ENOENT;
1470 }
1471
1472out:
1473 return ret;
1474}
1475
1476static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1477{
1478 int ret;
1479
1480 ret = get_cur_inode_state(sctx, ino, gen);
1481 if (ret < 0)
1482 goto out;
1483
1484 if (ret == inode_state_no_change ||
1485 ret == inode_state_did_create ||
1486 ret == inode_state_will_delete)
1487 ret = 1;
1488 else
1489 ret = 0;
1490
1491out:
1492 return ret;
1493}
1494
1495/*
1496 * Helper function to lookup a dir item in a dir.
1497 */
1498static int lookup_dir_item_inode(struct btrfs_root *root,
1499 u64 dir, const char *name, int name_len,
1500 u64 *found_inode,
1501 u8 *found_type)
1502{
1503 int ret = 0;
1504 struct btrfs_dir_item *di;
1505 struct btrfs_key key;
1506 struct btrfs_path *path;
1507
1508 path = alloc_path_for_send();
1509 if (!path)
1510 return -ENOMEM;
1511
1512 di = btrfs_lookup_dir_item(NULL, root, path,
1513 dir, name, name_len, 0);
1514 if (!di) {
1515 ret = -ENOENT;
1516 goto out;
1517 }
1518 if (IS_ERR(di)) {
1519 ret = PTR_ERR(di);
1520 goto out;
1521 }
1522 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1523 *found_inode = key.objectid;
1524 *found_type = btrfs_dir_type(path->nodes[0], di);
1525
1526out:
1527 btrfs_free_path(path);
1528 return ret;
1529}
1530
Alexander Block766702e2012-07-28 14:11:31 +02001531/*
1532 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1533 * generation of the parent dir and the name of the dir entry.
1534 */
Alexander Block31db9f72012-07-25 23:19:24 +02001535static int get_first_ref(struct send_ctx *sctx,
1536 struct btrfs_root *root, u64 ino,
1537 u64 *dir, u64 *dir_gen, struct fs_path *name)
1538{
1539 int ret;
1540 struct btrfs_key key;
1541 struct btrfs_key found_key;
1542 struct btrfs_path *path;
1543 struct btrfs_inode_ref *iref;
1544 int len;
1545
1546 path = alloc_path_for_send();
1547 if (!path)
1548 return -ENOMEM;
1549
1550 key.objectid = ino;
1551 key.type = BTRFS_INODE_REF_KEY;
1552 key.offset = 0;
1553
1554 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1555 if (ret < 0)
1556 goto out;
1557 if (!ret)
1558 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1559 path->slots[0]);
1560 if (ret || found_key.objectid != key.objectid ||
1561 found_key.type != key.type) {
1562 ret = -ENOENT;
1563 goto out;
1564 }
1565
1566 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1567 struct btrfs_inode_ref);
1568 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1569 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1570 (unsigned long)(iref + 1), len);
1571 if (ret < 0)
1572 goto out;
1573 btrfs_release_path(path);
1574
1575 ret = get_inode_info(root, found_key.offset, NULL, dir_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001576 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001577 if (ret < 0)
1578 goto out;
1579
1580 *dir = found_key.offset;
1581
1582out:
1583 btrfs_free_path(path);
1584 return ret;
1585}
1586
1587static int is_first_ref(struct send_ctx *sctx,
1588 struct btrfs_root *root,
1589 u64 ino, u64 dir,
1590 const char *name, int name_len)
1591{
1592 int ret;
1593 struct fs_path *tmp_name;
1594 u64 tmp_dir;
1595 u64 tmp_dir_gen;
1596
1597 tmp_name = fs_path_alloc(sctx);
1598 if (!tmp_name)
1599 return -ENOMEM;
1600
1601 ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1602 if (ret < 0)
1603 goto out;
1604
Alexander Blockb9291af2012-07-28 11:07:18 +02001605 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001606 ret = 0;
1607 goto out;
1608 }
1609
Alexander Blocke938c8a2012-07-28 16:33:49 +02001610 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001611
1612out:
1613 fs_path_free(sctx, tmp_name);
1614 return ret;
1615}
1616
Alexander Block766702e2012-07-28 14:11:31 +02001617/*
1618 * Used by process_recorded_refs to determine if a new ref would overwrite an
1619 * already existing ref. In case it detects an overwrite, it returns the
1620 * inode/gen in who_ino/who_gen.
1621 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1622 * to make sure later references to the overwritten inode are possible.
1623 * Orphanizing is however only required for the first ref of an inode.
1624 * process_recorded_refs does an additional is_first_ref check to see if
1625 * orphanizing is really required.
1626 */
Alexander Block31db9f72012-07-25 23:19:24 +02001627static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1628 const char *name, int name_len,
1629 u64 *who_ino, u64 *who_gen)
1630{
1631 int ret = 0;
1632 u64 other_inode = 0;
1633 u8 other_type = 0;
1634
1635 if (!sctx->parent_root)
1636 goto out;
1637
1638 ret = is_inode_existent(sctx, dir, dir_gen);
1639 if (ret <= 0)
1640 goto out;
1641
1642 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1643 &other_inode, &other_type);
1644 if (ret < 0 && ret != -ENOENT)
1645 goto out;
1646 if (ret) {
1647 ret = 0;
1648 goto out;
1649 }
1650
Alexander Block766702e2012-07-28 14:11:31 +02001651 /*
1652 * Check if the overwritten ref was already processed. If yes, the ref
1653 * was already unlinked/moved, so we can safely assume that we will not
1654 * overwrite anything at this point in time.
1655 */
Alexander Block31db9f72012-07-25 23:19:24 +02001656 if (other_inode > sctx->send_progress) {
1657 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001658 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001659 if (ret < 0)
1660 goto out;
1661
1662 ret = 1;
1663 *who_ino = other_inode;
1664 } else {
1665 ret = 0;
1666 }
1667
1668out:
1669 return ret;
1670}
1671
Alexander Block766702e2012-07-28 14:11:31 +02001672/*
1673 * Checks if the ref was overwritten by an already processed inode. This is
1674 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1675 * thus the orphan name needs be used.
1676 * process_recorded_refs also uses it to avoid unlinking of refs that were
1677 * overwritten.
1678 */
Alexander Block31db9f72012-07-25 23:19:24 +02001679static int did_overwrite_ref(struct send_ctx *sctx,
1680 u64 dir, u64 dir_gen,
1681 u64 ino, u64 ino_gen,
1682 const char *name, int name_len)
1683{
1684 int ret = 0;
1685 u64 gen;
1686 u64 ow_inode;
1687 u8 other_type;
1688
1689 if (!sctx->parent_root)
1690 goto out;
1691
1692 ret = is_inode_existent(sctx, dir, dir_gen);
1693 if (ret <= 0)
1694 goto out;
1695
1696 /* check if the ref was overwritten by another ref */
1697 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1698 &ow_inode, &other_type);
1699 if (ret < 0 && ret != -ENOENT)
1700 goto out;
1701 if (ret) {
1702 /* was never and will never be overwritten */
1703 ret = 0;
1704 goto out;
1705 }
1706
1707 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001708 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001709 if (ret < 0)
1710 goto out;
1711
1712 if (ow_inode == ino && gen == ino_gen) {
1713 ret = 0;
1714 goto out;
1715 }
1716
1717 /* we know that it is or will be overwritten. check this now */
1718 if (ow_inode < sctx->send_progress)
1719 ret = 1;
1720 else
1721 ret = 0;
1722
1723out:
1724 return ret;
1725}
1726
Alexander Block766702e2012-07-28 14:11:31 +02001727/*
1728 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1729 * that got overwritten. This is used by process_recorded_refs to determine
1730 * if it has to use the path as returned by get_cur_path or the orphan name.
1731 */
Alexander Block31db9f72012-07-25 23:19:24 +02001732static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1733{
1734 int ret = 0;
1735 struct fs_path *name = NULL;
1736 u64 dir;
1737 u64 dir_gen;
1738
1739 if (!sctx->parent_root)
1740 goto out;
1741
1742 name = fs_path_alloc(sctx);
1743 if (!name)
1744 return -ENOMEM;
1745
1746 ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name);
1747 if (ret < 0)
1748 goto out;
1749
1750 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1751 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001752
1753out:
1754 fs_path_free(sctx, name);
1755 return ret;
1756}
1757
Alexander Block766702e2012-07-28 14:11:31 +02001758/*
1759 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1760 * so we need to do some special handling in case we have clashes. This function
1761 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001762 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001763 */
Alexander Block31db9f72012-07-25 23:19:24 +02001764static int name_cache_insert(struct send_ctx *sctx,
1765 struct name_cache_entry *nce)
1766{
1767 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001768 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001769
Alexander Block7e0926f2012-07-28 14:20:58 +02001770 nce_head = radix_tree_lookup(&sctx->name_cache,
1771 (unsigned long)nce->ino);
1772 if (!nce_head) {
1773 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1774 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001775 return -ENOMEM;
Alexander Block7e0926f2012-07-28 14:20:58 +02001776 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001777
Alexander Block7e0926f2012-07-28 14:20:58 +02001778 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001779 if (ret < 0) {
1780 kfree(nce_head);
1781 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001782 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001783 }
Alexander Block31db9f72012-07-25 23:19:24 +02001784 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001785 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001786 list_add_tail(&nce->list, &sctx->name_cache_list);
1787 sctx->name_cache_size++;
1788
1789 return ret;
1790}
1791
1792static void name_cache_delete(struct send_ctx *sctx,
1793 struct name_cache_entry *nce)
1794{
Alexander Block7e0926f2012-07-28 14:20:58 +02001795 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001796
Alexander Block7e0926f2012-07-28 14:20:58 +02001797 nce_head = radix_tree_lookup(&sctx->name_cache,
1798 (unsigned long)nce->ino);
1799 BUG_ON(!nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001800
Alexander Block7e0926f2012-07-28 14:20:58 +02001801 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001802 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001803 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001804
1805 if (list_empty(nce_head)) {
1806 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1807 kfree(nce_head);
1808 }
Alexander Block31db9f72012-07-25 23:19:24 +02001809}
1810
1811static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1812 u64 ino, u64 gen)
1813{
Alexander Block7e0926f2012-07-28 14:20:58 +02001814 struct list_head *nce_head;
1815 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001816
Alexander Block7e0926f2012-07-28 14:20:58 +02001817 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1818 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001819 return NULL;
1820
Alexander Block7e0926f2012-07-28 14:20:58 +02001821 list_for_each_entry(cur, nce_head, radix_list) {
1822 if (cur->ino == ino && cur->gen == gen)
1823 return cur;
1824 }
Alexander Block31db9f72012-07-25 23:19:24 +02001825 return NULL;
1826}
1827
Alexander Block766702e2012-07-28 14:11:31 +02001828/*
1829 * Removes the entry from the list and adds it back to the end. This marks the
1830 * entry as recently used so that name_cache_clean_unused does not remove it.
1831 */
Alexander Block31db9f72012-07-25 23:19:24 +02001832static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1833{
1834 list_del(&nce->list);
1835 list_add_tail(&nce->list, &sctx->name_cache_list);
1836}
1837
Alexander Block766702e2012-07-28 14:11:31 +02001838/*
1839 * Remove some entries from the beginning of name_cache_list.
1840 */
Alexander Block31db9f72012-07-25 23:19:24 +02001841static void name_cache_clean_unused(struct send_ctx *sctx)
1842{
1843 struct name_cache_entry *nce;
1844
1845 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1846 return;
1847
1848 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1849 nce = list_entry(sctx->name_cache_list.next,
1850 struct name_cache_entry, list);
1851 name_cache_delete(sctx, nce);
1852 kfree(nce);
1853 }
1854}
1855
1856static void name_cache_free(struct send_ctx *sctx)
1857{
1858 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02001859
Alexander Blocke938c8a2012-07-28 16:33:49 +02001860 while (!list_empty(&sctx->name_cache_list)) {
1861 nce = list_entry(sctx->name_cache_list.next,
1862 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02001863 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02001864 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001865 }
1866}
1867
Alexander Block766702e2012-07-28 14:11:31 +02001868/*
1869 * Used by get_cur_path for each ref up to the root.
1870 * Returns 0 if it succeeded.
1871 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1872 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1873 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1874 * Returns <0 in case of error.
1875 */
Alexander Block31db9f72012-07-25 23:19:24 +02001876static int __get_cur_name_and_parent(struct send_ctx *sctx,
1877 u64 ino, u64 gen,
1878 u64 *parent_ino,
1879 u64 *parent_gen,
1880 struct fs_path *dest)
1881{
1882 int ret;
1883 int nce_ret;
1884 struct btrfs_path *path = NULL;
1885 struct name_cache_entry *nce = NULL;
1886
Alexander Block766702e2012-07-28 14:11:31 +02001887 /*
1888 * First check if we already did a call to this function with the same
1889 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1890 * return the cached result.
1891 */
Alexander Block31db9f72012-07-25 23:19:24 +02001892 nce = name_cache_search(sctx, ino, gen);
1893 if (nce) {
1894 if (ino < sctx->send_progress && nce->need_later_update) {
1895 name_cache_delete(sctx, nce);
1896 kfree(nce);
1897 nce = NULL;
1898 } else {
1899 name_cache_used(sctx, nce);
1900 *parent_ino = nce->parent_ino;
1901 *parent_gen = nce->parent_gen;
1902 ret = fs_path_add(dest, nce->name, nce->name_len);
1903 if (ret < 0)
1904 goto out;
1905 ret = nce->ret;
1906 goto out;
1907 }
1908 }
1909
1910 path = alloc_path_for_send();
1911 if (!path)
1912 return -ENOMEM;
1913
Alexander Block766702e2012-07-28 14:11:31 +02001914 /*
1915 * If the inode is not existent yet, add the orphan name and return 1.
1916 * This should only happen for the parent dir that we determine in
1917 * __record_new_ref
1918 */
Alexander Block31db9f72012-07-25 23:19:24 +02001919 ret = is_inode_existent(sctx, ino, gen);
1920 if (ret < 0)
1921 goto out;
1922
1923 if (!ret) {
1924 ret = gen_unique_name(sctx, ino, gen, dest);
1925 if (ret < 0)
1926 goto out;
1927 ret = 1;
1928 goto out_cache;
1929 }
1930
Alexander Block766702e2012-07-28 14:11:31 +02001931 /*
1932 * Depending on whether the inode was already processed or not, use
1933 * send_root or parent_root for ref lookup.
1934 */
Alexander Block31db9f72012-07-25 23:19:24 +02001935 if (ino < sctx->send_progress)
1936 ret = get_first_ref(sctx, sctx->send_root, ino,
1937 parent_ino, parent_gen, dest);
1938 else
1939 ret = get_first_ref(sctx, sctx->parent_root, ino,
1940 parent_ino, parent_gen, dest);
1941 if (ret < 0)
1942 goto out;
1943
Alexander Block766702e2012-07-28 14:11:31 +02001944 /*
1945 * Check if the ref was overwritten by an inode's ref that was processed
1946 * earlier. If yes, treat as orphan and return 1.
1947 */
Alexander Block31db9f72012-07-25 23:19:24 +02001948 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1949 dest->start, dest->end - dest->start);
1950 if (ret < 0)
1951 goto out;
1952 if (ret) {
1953 fs_path_reset(dest);
1954 ret = gen_unique_name(sctx, ino, gen, dest);
1955 if (ret < 0)
1956 goto out;
1957 ret = 1;
1958 }
1959
1960out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02001961 /*
1962 * Store the result of the lookup in the name cache.
1963 */
Alexander Block31db9f72012-07-25 23:19:24 +02001964 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
1965 if (!nce) {
1966 ret = -ENOMEM;
1967 goto out;
1968 }
1969
1970 nce->ino = ino;
1971 nce->gen = gen;
1972 nce->parent_ino = *parent_ino;
1973 nce->parent_gen = *parent_gen;
1974 nce->name_len = fs_path_len(dest);
1975 nce->ret = ret;
1976 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02001977
1978 if (ino < sctx->send_progress)
1979 nce->need_later_update = 0;
1980 else
1981 nce->need_later_update = 1;
1982
1983 nce_ret = name_cache_insert(sctx, nce);
1984 if (nce_ret < 0)
1985 ret = nce_ret;
1986 name_cache_clean_unused(sctx);
1987
1988out:
1989 btrfs_free_path(path);
1990 return ret;
1991}
1992
1993/*
1994 * Magic happens here. This function returns the first ref to an inode as it
1995 * would look like while receiving the stream at this point in time.
1996 * We walk the path up to the root. For every inode in between, we check if it
1997 * was already processed/sent. If yes, we continue with the parent as found
1998 * in send_root. If not, we continue with the parent as found in parent_root.
1999 * If we encounter an inode that was deleted at this point in time, we use the
2000 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2001 * that were not created yet and overwritten inodes/refs.
2002 *
2003 * When do we have have orphan inodes:
2004 * 1. When an inode is freshly created and thus no valid refs are available yet
2005 * 2. When a directory lost all it's refs (deleted) but still has dir items
2006 * inside which were not processed yet (pending for move/delete). If anyone
2007 * tried to get the path to the dir items, it would get a path inside that
2008 * orphan directory.
2009 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2010 * of an unprocessed inode. If in that case the first ref would be
2011 * overwritten, the overwritten inode gets "orphanized". Later when we
2012 * process this overwritten inode, it is restored at a new place by moving
2013 * the orphan inode.
2014 *
2015 * sctx->send_progress tells this function at which point in time receiving
2016 * would be.
2017 */
2018static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2019 struct fs_path *dest)
2020{
2021 int ret = 0;
2022 struct fs_path *name = NULL;
2023 u64 parent_inode = 0;
2024 u64 parent_gen = 0;
2025 int stop = 0;
2026
2027 name = fs_path_alloc(sctx);
2028 if (!name) {
2029 ret = -ENOMEM;
2030 goto out;
2031 }
2032
2033 dest->reversed = 1;
2034 fs_path_reset(dest);
2035
2036 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2037 fs_path_reset(name);
2038
2039 ret = __get_cur_name_and_parent(sctx, ino, gen,
2040 &parent_inode, &parent_gen, name);
2041 if (ret < 0)
2042 goto out;
2043 if (ret)
2044 stop = 1;
2045
2046 ret = fs_path_add_path(dest, name);
2047 if (ret < 0)
2048 goto out;
2049
2050 ino = parent_inode;
2051 gen = parent_gen;
2052 }
2053
2054out:
2055 fs_path_free(sctx, name);
2056 if (!ret)
2057 fs_path_unreverse(dest);
2058 return ret;
2059}
2060
2061/*
2062 * Called for regular files when sending extents data. Opens a struct file
2063 * to read from the file.
2064 */
2065static int open_cur_inode_file(struct send_ctx *sctx)
2066{
2067 int ret = 0;
2068 struct btrfs_key key;
Linus Torvaldse2aed8d2012-07-26 14:48:55 -07002069 struct path path;
Alexander Block31db9f72012-07-25 23:19:24 +02002070 struct inode *inode;
2071 struct dentry *dentry;
2072 struct file *filp;
2073 int new = 0;
2074
2075 if (sctx->cur_inode_filp)
2076 goto out;
2077
2078 key.objectid = sctx->cur_ino;
2079 key.type = BTRFS_INODE_ITEM_KEY;
2080 key.offset = 0;
2081
2082 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2083 &new);
2084 if (IS_ERR(inode)) {
2085 ret = PTR_ERR(inode);
2086 goto out;
2087 }
2088
2089 dentry = d_obtain_alias(inode);
2090 inode = NULL;
2091 if (IS_ERR(dentry)) {
2092 ret = PTR_ERR(dentry);
2093 goto out;
2094 }
2095
Linus Torvaldse2aed8d2012-07-26 14:48:55 -07002096 path.mnt = sctx->mnt;
2097 path.dentry = dentry;
2098 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2099 dput(dentry);
Alexander Block31db9f72012-07-25 23:19:24 +02002100 dentry = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02002101 if (IS_ERR(filp)) {
2102 ret = PTR_ERR(filp);
2103 goto out;
2104 }
2105 sctx->cur_inode_filp = filp;
2106
2107out:
2108 /*
2109 * no xxxput required here as every vfs op
2110 * does it by itself on failure
2111 */
2112 return ret;
2113}
2114
2115/*
2116 * Closes the struct file that was created in open_cur_inode_file
2117 */
2118static int close_cur_inode_file(struct send_ctx *sctx)
2119{
2120 int ret = 0;
2121
2122 if (!sctx->cur_inode_filp)
2123 goto out;
2124
2125 ret = filp_close(sctx->cur_inode_filp, NULL);
2126 sctx->cur_inode_filp = NULL;
2127
2128out:
2129 return ret;
2130}
2131
2132/*
2133 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2134 */
2135static int send_subvol_begin(struct send_ctx *sctx)
2136{
2137 int ret;
2138 struct btrfs_root *send_root = sctx->send_root;
2139 struct btrfs_root *parent_root = sctx->parent_root;
2140 struct btrfs_path *path;
2141 struct btrfs_key key;
2142 struct btrfs_root_ref *ref;
2143 struct extent_buffer *leaf;
2144 char *name = NULL;
2145 int namelen;
2146
2147 path = alloc_path_for_send();
2148 if (!path)
2149 return -ENOMEM;
2150
2151 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2152 if (!name) {
2153 btrfs_free_path(path);
2154 return -ENOMEM;
2155 }
2156
2157 key.objectid = send_root->objectid;
2158 key.type = BTRFS_ROOT_BACKREF_KEY;
2159 key.offset = 0;
2160
2161 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2162 &key, path, 1, 0);
2163 if (ret < 0)
2164 goto out;
2165 if (ret) {
2166 ret = -ENOENT;
2167 goto out;
2168 }
2169
2170 leaf = path->nodes[0];
2171 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2172 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2173 key.objectid != send_root->objectid) {
2174 ret = -ENOENT;
2175 goto out;
2176 }
2177 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2178 namelen = btrfs_root_ref_name_len(leaf, ref);
2179 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2180 btrfs_release_path(path);
2181
Alexander Block31db9f72012-07-25 23:19:24 +02002182 if (parent_root) {
2183 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2184 if (ret < 0)
2185 goto out;
2186 } else {
2187 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2188 if (ret < 0)
2189 goto out;
2190 }
2191
2192 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2193 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2194 sctx->send_root->root_item.uuid);
2195 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2196 sctx->send_root->root_item.ctransid);
2197 if (parent_root) {
2198 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2199 sctx->parent_root->root_item.uuid);
2200 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2201 sctx->parent_root->root_item.ctransid);
2202 }
2203
2204 ret = send_cmd(sctx);
2205
2206tlv_put_failure:
2207out:
2208 btrfs_free_path(path);
2209 kfree(name);
2210 return ret;
2211}
2212
2213static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2214{
2215 int ret = 0;
2216 struct fs_path *p;
2217
2218verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2219
2220 p = fs_path_alloc(sctx);
2221 if (!p)
2222 return -ENOMEM;
2223
2224 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2225 if (ret < 0)
2226 goto out;
2227
2228 ret = get_cur_path(sctx, ino, gen, p);
2229 if (ret < 0)
2230 goto out;
2231 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2232 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2233
2234 ret = send_cmd(sctx);
2235
2236tlv_put_failure:
2237out:
2238 fs_path_free(sctx, p);
2239 return ret;
2240}
2241
2242static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2243{
2244 int ret = 0;
2245 struct fs_path *p;
2246
2247verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2248
2249 p = fs_path_alloc(sctx);
2250 if (!p)
2251 return -ENOMEM;
2252
2253 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2254 if (ret < 0)
2255 goto out;
2256
2257 ret = get_cur_path(sctx, ino, gen, p);
2258 if (ret < 0)
2259 goto out;
2260 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2261 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2262
2263 ret = send_cmd(sctx);
2264
2265tlv_put_failure:
2266out:
2267 fs_path_free(sctx, p);
2268 return ret;
2269}
2270
2271static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2272{
2273 int ret = 0;
2274 struct fs_path *p;
2275
2276verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2277
2278 p = fs_path_alloc(sctx);
2279 if (!p)
2280 return -ENOMEM;
2281
2282 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2283 if (ret < 0)
2284 goto out;
2285
2286 ret = get_cur_path(sctx, ino, gen, p);
2287 if (ret < 0)
2288 goto out;
2289 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2290 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2291 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2292
2293 ret = send_cmd(sctx);
2294
2295tlv_put_failure:
2296out:
2297 fs_path_free(sctx, p);
2298 return ret;
2299}
2300
2301static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2302{
2303 int ret = 0;
2304 struct fs_path *p = NULL;
2305 struct btrfs_inode_item *ii;
2306 struct btrfs_path *path = NULL;
2307 struct extent_buffer *eb;
2308 struct btrfs_key key;
2309 int slot;
2310
2311verbose_printk("btrfs: send_utimes %llu\n", ino);
2312
2313 p = fs_path_alloc(sctx);
2314 if (!p)
2315 return -ENOMEM;
2316
2317 path = alloc_path_for_send();
2318 if (!path) {
2319 ret = -ENOMEM;
2320 goto out;
2321 }
2322
2323 key.objectid = ino;
2324 key.type = BTRFS_INODE_ITEM_KEY;
2325 key.offset = 0;
2326 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2327 if (ret < 0)
2328 goto out;
2329
2330 eb = path->nodes[0];
2331 slot = path->slots[0];
2332 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2333
2334 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2335 if (ret < 0)
2336 goto out;
2337
2338 ret = get_cur_path(sctx, ino, gen, p);
2339 if (ret < 0)
2340 goto out;
2341 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2342 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2343 btrfs_inode_atime(ii));
2344 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2345 btrfs_inode_mtime(ii));
2346 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2347 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002348 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002349
2350 ret = send_cmd(sctx);
2351
2352tlv_put_failure:
2353out:
2354 fs_path_free(sctx, p);
2355 btrfs_free_path(path);
2356 return ret;
2357}
2358
2359/*
2360 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2361 * a valid path yet because we did not process the refs yet. So, the inode
2362 * is created as orphan.
2363 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002364static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002365{
2366 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002367 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002368 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002369 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002370 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002371 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002372
Alexander Block1f4692d2012-07-28 10:42:24 +02002373verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002374
2375 p = fs_path_alloc(sctx);
2376 if (!p)
2377 return -ENOMEM;
2378
Alexander Block1f4692d2012-07-28 10:42:24 +02002379 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2380 NULL, &rdev);
2381 if (ret < 0)
2382 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002383
Alexander Blocke938c8a2012-07-28 16:33:49 +02002384 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002385 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002386 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002387 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002388 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002389 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002390 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002391 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002392 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002393 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002394 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002395 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002396 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002397 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2398 (int)(mode & S_IFMT));
2399 ret = -ENOTSUPP;
2400 goto out;
2401 }
2402
2403 ret = begin_cmd(sctx, cmd);
2404 if (ret < 0)
2405 goto out;
2406
Alexander Block1f4692d2012-07-28 10:42:24 +02002407 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002408 if (ret < 0)
2409 goto out;
2410
2411 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002412 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002413
2414 if (S_ISLNK(mode)) {
2415 fs_path_reset(p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002416 ret = read_symlink(sctx, sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002417 if (ret < 0)
2418 goto out;
2419 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2420 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2421 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002422 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, rdev);
Alexander Block31db9f72012-07-25 23:19:24 +02002423 }
2424
2425 ret = send_cmd(sctx);
2426 if (ret < 0)
2427 goto out;
2428
2429
2430tlv_put_failure:
2431out:
2432 fs_path_free(sctx, p);
2433 return ret;
2434}
2435
Alexander Block1f4692d2012-07-28 10:42:24 +02002436/*
2437 * We need some special handling for inodes that get processed before the parent
2438 * directory got created. See process_recorded_refs for details.
2439 * This function does the check if we already created the dir out of order.
2440 */
2441static int did_create_dir(struct send_ctx *sctx, u64 dir)
2442{
2443 int ret = 0;
2444 struct btrfs_path *path = NULL;
2445 struct btrfs_key key;
2446 struct btrfs_key found_key;
2447 struct btrfs_key di_key;
2448 struct extent_buffer *eb;
2449 struct btrfs_dir_item *di;
2450 int slot;
2451
2452 path = alloc_path_for_send();
2453 if (!path) {
2454 ret = -ENOMEM;
2455 goto out;
2456 }
2457
2458 key.objectid = dir;
2459 key.type = BTRFS_DIR_INDEX_KEY;
2460 key.offset = 0;
2461 while (1) {
2462 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2463 1, 0);
2464 if (ret < 0)
2465 goto out;
2466 if (!ret) {
2467 eb = path->nodes[0];
2468 slot = path->slots[0];
2469 btrfs_item_key_to_cpu(eb, &found_key, slot);
2470 }
2471 if (ret || found_key.objectid != key.objectid ||
2472 found_key.type != key.type) {
2473 ret = 0;
2474 goto out;
2475 }
2476
2477 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2478 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2479
2480 if (di_key.objectid < sctx->send_progress) {
2481 ret = 1;
2482 goto out;
2483 }
2484
2485 key.offset = found_key.offset + 1;
2486 btrfs_release_path(path);
2487 }
2488
2489out:
2490 btrfs_free_path(path);
2491 return ret;
2492}
2493
2494/*
2495 * Only creates the inode if it is:
2496 * 1. Not a directory
2497 * 2. Or a directory which was not created already due to out of order
2498 * directories. See did_create_dir and process_recorded_refs for details.
2499 */
2500static int send_create_inode_if_needed(struct send_ctx *sctx)
2501{
2502 int ret;
2503
2504 if (S_ISDIR(sctx->cur_inode_mode)) {
2505 ret = did_create_dir(sctx, sctx->cur_ino);
2506 if (ret < 0)
2507 goto out;
2508 if (ret) {
2509 ret = 0;
2510 goto out;
2511 }
2512 }
2513
2514 ret = send_create_inode(sctx, sctx->cur_ino);
2515 if (ret < 0)
2516 goto out;
2517
2518out:
2519 return ret;
2520}
2521
Alexander Block31db9f72012-07-25 23:19:24 +02002522struct recorded_ref {
2523 struct list_head list;
2524 char *dir_path;
2525 char *name;
2526 struct fs_path *full_path;
2527 u64 dir;
2528 u64 dir_gen;
2529 int dir_path_len;
2530 int name_len;
2531};
2532
2533/*
2534 * We need to process new refs before deleted refs, but compare_tree gives us
2535 * everything mixed. So we first record all refs and later process them.
2536 * This function is a helper to record one ref.
2537 */
2538static int record_ref(struct list_head *head, u64 dir,
2539 u64 dir_gen, struct fs_path *path)
2540{
2541 struct recorded_ref *ref;
2542 char *tmp;
2543
2544 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2545 if (!ref)
2546 return -ENOMEM;
2547
2548 ref->dir = dir;
2549 ref->dir_gen = dir_gen;
2550 ref->full_path = path;
2551
2552 tmp = strrchr(ref->full_path->start, '/');
2553 if (!tmp) {
2554 ref->name_len = ref->full_path->end - ref->full_path->start;
2555 ref->name = ref->full_path->start;
2556 ref->dir_path_len = 0;
2557 ref->dir_path = ref->full_path->start;
2558 } else {
2559 tmp++;
2560 ref->name_len = ref->full_path->end - tmp;
2561 ref->name = tmp;
2562 ref->dir_path = ref->full_path->start;
2563 ref->dir_path_len = ref->full_path->end -
2564 ref->full_path->start - 1 - ref->name_len;
2565 }
2566
2567 list_add_tail(&ref->list, head);
2568 return 0;
2569}
2570
2571static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head)
2572{
2573 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002574
Alexander Blocke938c8a2012-07-28 16:33:49 +02002575 while (!list_empty(head)) {
2576 cur = list_entry(head->next, struct recorded_ref, list);
Alexander Block31db9f72012-07-25 23:19:24 +02002577 fs_path_free(sctx, cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002578 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002579 kfree(cur);
2580 }
Alexander Block31db9f72012-07-25 23:19:24 +02002581}
2582
2583static void free_recorded_refs(struct send_ctx *sctx)
2584{
2585 __free_recorded_refs(sctx, &sctx->new_refs);
2586 __free_recorded_refs(sctx, &sctx->deleted_refs);
2587}
2588
2589/*
Alexander Block766702e2012-07-28 14:11:31 +02002590 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002591 * ref of an unprocessed inode gets overwritten and for all non empty
2592 * directories.
2593 */
2594static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2595 struct fs_path *path)
2596{
2597 int ret;
2598 struct fs_path *orphan;
2599
2600 orphan = fs_path_alloc(sctx);
2601 if (!orphan)
2602 return -ENOMEM;
2603
2604 ret = gen_unique_name(sctx, ino, gen, orphan);
2605 if (ret < 0)
2606 goto out;
2607
2608 ret = send_rename(sctx, path, orphan);
2609
2610out:
2611 fs_path_free(sctx, orphan);
2612 return ret;
2613}
2614
2615/*
2616 * Returns 1 if a directory can be removed at this point in time.
2617 * We check this by iterating all dir items and checking if the inode behind
2618 * the dir item was already processed.
2619 */
2620static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2621{
2622 int ret = 0;
2623 struct btrfs_root *root = sctx->parent_root;
2624 struct btrfs_path *path;
2625 struct btrfs_key key;
2626 struct btrfs_key found_key;
2627 struct btrfs_key loc;
2628 struct btrfs_dir_item *di;
2629
Alexander Block6d85ed02012-08-01 14:48:59 +02002630 /*
2631 * Don't try to rmdir the top/root subvolume dir.
2632 */
2633 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2634 return 0;
2635
Alexander Block31db9f72012-07-25 23:19:24 +02002636 path = alloc_path_for_send();
2637 if (!path)
2638 return -ENOMEM;
2639
2640 key.objectid = dir;
2641 key.type = BTRFS_DIR_INDEX_KEY;
2642 key.offset = 0;
2643
2644 while (1) {
2645 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2646 if (ret < 0)
2647 goto out;
2648 if (!ret) {
2649 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2650 path->slots[0]);
2651 }
2652 if (ret || found_key.objectid != key.objectid ||
2653 found_key.type != key.type) {
2654 break;
2655 }
2656
2657 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2658 struct btrfs_dir_item);
2659 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2660
2661 if (loc.objectid > send_progress) {
2662 ret = 0;
2663 goto out;
2664 }
2665
2666 btrfs_release_path(path);
2667 key.offset = found_key.offset + 1;
2668 }
2669
2670 ret = 1;
2671
2672out:
2673 btrfs_free_path(path);
2674 return ret;
2675}
2676
Alexander Block31db9f72012-07-25 23:19:24 +02002677/*
2678 * This does all the move/link/unlink/rmdir magic.
2679 */
2680static int process_recorded_refs(struct send_ctx *sctx)
2681{
2682 int ret = 0;
2683 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02002684 struct recorded_ref *cur2;
Alexander Block31db9f72012-07-25 23:19:24 +02002685 struct ulist *check_dirs = NULL;
2686 struct ulist_iterator uit;
2687 struct ulist_node *un;
2688 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04002689 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002690 u64 ow_gen;
2691 int did_overwrite = 0;
2692 int is_orphan = 0;
2693
2694verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2695
Alexander Block6d85ed02012-08-01 14:48:59 +02002696 /*
2697 * This should never happen as the root dir always has the same ref
2698 * which is always '..'
2699 */
2700 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
2701
Alexander Block31db9f72012-07-25 23:19:24 +02002702 valid_path = fs_path_alloc(sctx);
2703 if (!valid_path) {
2704 ret = -ENOMEM;
2705 goto out;
2706 }
2707
2708 check_dirs = ulist_alloc(GFP_NOFS);
2709 if (!check_dirs) {
2710 ret = -ENOMEM;
2711 goto out;
2712 }
2713
2714 /*
2715 * First, check if the first ref of the current inode was overwritten
2716 * before. If yes, we know that the current inode was already orphanized
2717 * and thus use the orphan name. If not, we can use get_cur_path to
2718 * get the path of the first ref as it would like while receiving at
2719 * this point in time.
2720 * New inodes are always orphan at the beginning, so force to use the
2721 * orphan name in this case.
2722 * The first ref is stored in valid_path and will be updated if it
2723 * gets moved around.
2724 */
2725 if (!sctx->cur_inode_new) {
2726 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2727 sctx->cur_inode_gen);
2728 if (ret < 0)
2729 goto out;
2730 if (ret)
2731 did_overwrite = 1;
2732 }
2733 if (sctx->cur_inode_new || did_overwrite) {
2734 ret = gen_unique_name(sctx, sctx->cur_ino,
2735 sctx->cur_inode_gen, valid_path);
2736 if (ret < 0)
2737 goto out;
2738 is_orphan = 1;
2739 } else {
2740 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2741 valid_path);
2742 if (ret < 0)
2743 goto out;
2744 }
2745
2746 list_for_each_entry(cur, &sctx->new_refs, list) {
2747 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02002748 * We may have refs where the parent directory does not exist
2749 * yet. This happens if the parent directories inum is higher
2750 * the the current inum. To handle this case, we create the
2751 * parent directory out of order. But we need to check if this
2752 * did already happen before due to other refs in the same dir.
2753 */
2754 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2755 if (ret < 0)
2756 goto out;
2757 if (ret == inode_state_will_create) {
2758 ret = 0;
2759 /*
2760 * First check if any of the current inodes refs did
2761 * already create the dir.
2762 */
2763 list_for_each_entry(cur2, &sctx->new_refs, list) {
2764 if (cur == cur2)
2765 break;
2766 if (cur2->dir == cur->dir) {
2767 ret = 1;
2768 break;
2769 }
2770 }
2771
2772 /*
2773 * If that did not happen, check if a previous inode
2774 * did already create the dir.
2775 */
2776 if (!ret)
2777 ret = did_create_dir(sctx, cur->dir);
2778 if (ret < 0)
2779 goto out;
2780 if (!ret) {
2781 ret = send_create_inode(sctx, cur->dir);
2782 if (ret < 0)
2783 goto out;
2784 }
2785 }
2786
2787 /*
Alexander Block31db9f72012-07-25 23:19:24 +02002788 * Check if this new ref would overwrite the first ref of
2789 * another unprocessed inode. If yes, orphanize the
2790 * overwritten inode. If we find an overwritten ref that is
2791 * not the first ref, simply unlink it.
2792 */
2793 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2794 cur->name, cur->name_len,
2795 &ow_inode, &ow_gen);
2796 if (ret < 0)
2797 goto out;
2798 if (ret) {
2799 ret = is_first_ref(sctx, sctx->parent_root,
2800 ow_inode, cur->dir, cur->name,
2801 cur->name_len);
2802 if (ret < 0)
2803 goto out;
2804 if (ret) {
2805 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2806 cur->full_path);
2807 if (ret < 0)
2808 goto out;
2809 } else {
2810 ret = send_unlink(sctx, cur->full_path);
2811 if (ret < 0)
2812 goto out;
2813 }
2814 }
2815
2816 /*
2817 * link/move the ref to the new place. If we have an orphan
2818 * inode, move it and update valid_path. If not, link or move
2819 * it depending on the inode mode.
2820 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002821 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02002822 ret = send_rename(sctx, valid_path, cur->full_path);
2823 if (ret < 0)
2824 goto out;
2825 is_orphan = 0;
2826 ret = fs_path_copy(valid_path, cur->full_path);
2827 if (ret < 0)
2828 goto out;
2829 } else {
2830 if (S_ISDIR(sctx->cur_inode_mode)) {
2831 /*
2832 * Dirs can't be linked, so move it. For moved
2833 * dirs, we always have one new and one deleted
2834 * ref. The deleted ref is ignored later.
2835 */
2836 ret = send_rename(sctx, valid_path,
2837 cur->full_path);
2838 if (ret < 0)
2839 goto out;
2840 ret = fs_path_copy(valid_path, cur->full_path);
2841 if (ret < 0)
2842 goto out;
2843 } else {
2844 ret = send_link(sctx, cur->full_path,
2845 valid_path);
2846 if (ret < 0)
2847 goto out;
2848 }
2849 }
2850 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2851 GFP_NOFS);
2852 if (ret < 0)
2853 goto out;
2854 }
2855
2856 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2857 /*
2858 * Check if we can already rmdir the directory. If not,
2859 * orphanize it. For every dir item inside that gets deleted
2860 * later, we do this check again and rmdir it then if possible.
2861 * See the use of check_dirs for more details.
2862 */
2863 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2864 if (ret < 0)
2865 goto out;
2866 if (ret) {
2867 ret = send_rmdir(sctx, valid_path);
2868 if (ret < 0)
2869 goto out;
2870 } else if (!is_orphan) {
2871 ret = orphanize_inode(sctx, sctx->cur_ino,
2872 sctx->cur_inode_gen, valid_path);
2873 if (ret < 0)
2874 goto out;
2875 is_orphan = 1;
2876 }
2877
2878 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2879 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2880 GFP_NOFS);
2881 if (ret < 0)
2882 goto out;
2883 }
Alexander Blockccf16262012-07-28 11:46:29 +02002884 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2885 !list_empty(&sctx->deleted_refs)) {
2886 /*
2887 * We have a moved dir. Add the old parent to check_dirs
2888 */
2889 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2890 list);
2891 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2892 GFP_NOFS);
2893 if (ret < 0)
2894 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002895 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2896 /*
2897 * We have a non dir inode. Go through all deleted refs and
2898 * unlink them if they were not already overwritten by other
2899 * inodes.
2900 */
2901 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2902 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2903 sctx->cur_ino, sctx->cur_inode_gen,
2904 cur->name, cur->name_len);
2905 if (ret < 0)
2906 goto out;
2907 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002908 ret = send_unlink(sctx, cur->full_path);
2909 if (ret < 0)
2910 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002911 }
2912 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2913 GFP_NOFS);
2914 if (ret < 0)
2915 goto out;
2916 }
2917
2918 /*
2919 * If the inode is still orphan, unlink the orphan. This may
2920 * happen when a previous inode did overwrite the first ref
2921 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02002922 * inode. Unlinking does not mean that the inode is deleted in
2923 * all cases. There may still be links to this inode in other
2924 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02002925 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002926 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02002927 ret = send_unlink(sctx, valid_path);
2928 if (ret < 0)
2929 goto out;
2930 }
2931 }
2932
2933 /*
2934 * We did collect all parent dirs where cur_inode was once located. We
2935 * now go through all these dirs and check if they are pending for
2936 * deletion and if it's finally possible to perform the rmdir now.
2937 * We also update the inode stats of the parent dirs here.
2938 */
2939 ULIST_ITER_INIT(&uit);
2940 while ((un = ulist_next(check_dirs, &uit))) {
Alexander Block766702e2012-07-28 14:11:31 +02002941 /*
2942 * In case we had refs into dirs that were not processed yet,
2943 * we don't need to do the utime and rmdir logic for these dirs.
2944 * The dir will be processed later.
2945 */
Alexander Block31db9f72012-07-25 23:19:24 +02002946 if (un->val > sctx->cur_ino)
2947 continue;
2948
2949 ret = get_cur_inode_state(sctx, un->val, un->aux);
2950 if (ret < 0)
2951 goto out;
2952
2953 if (ret == inode_state_did_create ||
2954 ret == inode_state_no_change) {
2955 /* TODO delayed utimes */
2956 ret = send_utimes(sctx, un->val, un->aux);
2957 if (ret < 0)
2958 goto out;
2959 } else if (ret == inode_state_did_delete) {
2960 ret = can_rmdir(sctx, un->val, sctx->cur_ino);
2961 if (ret < 0)
2962 goto out;
2963 if (ret) {
2964 ret = get_cur_path(sctx, un->val, un->aux,
2965 valid_path);
2966 if (ret < 0)
2967 goto out;
2968 ret = send_rmdir(sctx, valid_path);
2969 if (ret < 0)
2970 goto out;
2971 }
2972 }
2973 }
2974
Alexander Block31db9f72012-07-25 23:19:24 +02002975 ret = 0;
2976
2977out:
2978 free_recorded_refs(sctx);
2979 ulist_free(check_dirs);
2980 fs_path_free(sctx, valid_path);
2981 return ret;
2982}
2983
2984static int __record_new_ref(int num, u64 dir, int index,
2985 struct fs_path *name,
2986 void *ctx)
2987{
2988 int ret = 0;
2989 struct send_ctx *sctx = ctx;
2990 struct fs_path *p;
2991 u64 gen;
2992
2993 p = fs_path_alloc(sctx);
2994 if (!p)
2995 return -ENOMEM;
2996
2997 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02002998 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02002999 if (ret < 0)
3000 goto out;
3001
Alexander Block31db9f72012-07-25 23:19:24 +02003002 ret = get_cur_path(sctx, dir, gen, p);
3003 if (ret < 0)
3004 goto out;
3005 ret = fs_path_add_path(p, name);
3006 if (ret < 0)
3007 goto out;
3008
3009 ret = record_ref(&sctx->new_refs, dir, gen, p);
3010
3011out:
3012 if (ret)
3013 fs_path_free(sctx, p);
3014 return ret;
3015}
3016
3017static int __record_deleted_ref(int num, u64 dir, int index,
3018 struct fs_path *name,
3019 void *ctx)
3020{
3021 int ret = 0;
3022 struct send_ctx *sctx = ctx;
3023 struct fs_path *p;
3024 u64 gen;
3025
3026 p = fs_path_alloc(sctx);
3027 if (!p)
3028 return -ENOMEM;
3029
3030 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003031 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003032 if (ret < 0)
3033 goto out;
3034
3035 ret = get_cur_path(sctx, dir, gen, p);
3036 if (ret < 0)
3037 goto out;
3038 ret = fs_path_add_path(p, name);
3039 if (ret < 0)
3040 goto out;
3041
3042 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3043
3044out:
3045 if (ret)
3046 fs_path_free(sctx, p);
3047 return ret;
3048}
3049
3050static int record_new_ref(struct send_ctx *sctx)
3051{
3052 int ret;
3053
3054 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3055 sctx->cmp_key, 0, __record_new_ref, sctx);
3056 if (ret < 0)
3057 goto out;
3058 ret = 0;
3059
3060out:
3061 return ret;
3062}
3063
3064static int record_deleted_ref(struct send_ctx *sctx)
3065{
3066 int ret;
3067
3068 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3069 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3070 if (ret < 0)
3071 goto out;
3072 ret = 0;
3073
3074out:
3075 return ret;
3076}
3077
3078struct find_ref_ctx {
3079 u64 dir;
3080 struct fs_path *name;
3081 int found_idx;
3082};
3083
3084static int __find_iref(int num, u64 dir, int index,
3085 struct fs_path *name,
3086 void *ctx_)
3087{
3088 struct find_ref_ctx *ctx = ctx_;
3089
3090 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3091 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3092 ctx->found_idx = num;
3093 return 1;
3094 }
3095 return 0;
3096}
3097
3098static int find_iref(struct send_ctx *sctx,
3099 struct btrfs_root *root,
3100 struct btrfs_path *path,
3101 struct btrfs_key *key,
3102 u64 dir, struct fs_path *name)
3103{
3104 int ret;
3105 struct find_ref_ctx ctx;
3106
3107 ctx.dir = dir;
3108 ctx.name = name;
3109 ctx.found_idx = -1;
3110
3111 ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
3112 if (ret < 0)
3113 return ret;
3114
3115 if (ctx.found_idx == -1)
3116 return -ENOENT;
3117
3118 return ctx.found_idx;
3119}
3120
3121static int __record_changed_new_ref(int num, u64 dir, int index,
3122 struct fs_path *name,
3123 void *ctx)
3124{
3125 int ret;
3126 struct send_ctx *sctx = ctx;
3127
3128 ret = find_iref(sctx, sctx->parent_root, sctx->right_path,
3129 sctx->cmp_key, dir, name);
3130 if (ret == -ENOENT)
3131 ret = __record_new_ref(num, dir, index, name, sctx);
3132 else if (ret > 0)
3133 ret = 0;
3134
3135 return ret;
3136}
3137
3138static int __record_changed_deleted_ref(int num, u64 dir, int index,
3139 struct fs_path *name,
3140 void *ctx)
3141{
3142 int ret;
3143 struct send_ctx *sctx = ctx;
3144
3145 ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3146 dir, name);
3147 if (ret == -ENOENT)
3148 ret = __record_deleted_ref(num, dir, index, name, sctx);
3149 else if (ret > 0)
3150 ret = 0;
3151
3152 return ret;
3153}
3154
3155static int record_changed_ref(struct send_ctx *sctx)
3156{
3157 int ret = 0;
3158
3159 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3160 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3161 if (ret < 0)
3162 goto out;
3163 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3164 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3165 if (ret < 0)
3166 goto out;
3167 ret = 0;
3168
3169out:
3170 return ret;
3171}
3172
3173/*
3174 * Record and process all refs at once. Needed when an inode changes the
3175 * generation number, which means that it was deleted and recreated.
3176 */
3177static int process_all_refs(struct send_ctx *sctx,
3178 enum btrfs_compare_tree_result cmd)
3179{
3180 int ret;
3181 struct btrfs_root *root;
3182 struct btrfs_path *path;
3183 struct btrfs_key key;
3184 struct btrfs_key found_key;
3185 struct extent_buffer *eb;
3186 int slot;
3187 iterate_inode_ref_t cb;
3188
3189 path = alloc_path_for_send();
3190 if (!path)
3191 return -ENOMEM;
3192
3193 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3194 root = sctx->send_root;
3195 cb = __record_new_ref;
3196 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3197 root = sctx->parent_root;
3198 cb = __record_deleted_ref;
3199 } else {
3200 BUG();
3201 }
3202
3203 key.objectid = sctx->cmp_key->objectid;
3204 key.type = BTRFS_INODE_REF_KEY;
3205 key.offset = 0;
3206 while (1) {
3207 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
Alexander Blocke938c8a2012-07-28 16:33:49 +02003208 if (ret < 0)
Alexander Block31db9f72012-07-25 23:19:24 +02003209 goto out;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003210 if (ret)
Alexander Block31db9f72012-07-25 23:19:24 +02003211 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003212
3213 eb = path->nodes[0];
3214 slot = path->slots[0];
3215 btrfs_item_key_to_cpu(eb, &found_key, slot);
3216
3217 if (found_key.objectid != key.objectid ||
Alexander Blocke938c8a2012-07-28 16:33:49 +02003218 found_key.type != key.type)
Alexander Block31db9f72012-07-25 23:19:24 +02003219 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003220
Alexander Block2f28f472012-08-01 14:42:14 +02003221 ret = iterate_inode_ref(sctx, root, path, &found_key, 0, cb,
3222 sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003223 btrfs_release_path(path);
3224 if (ret < 0)
3225 goto out;
3226
3227 key.offset = found_key.offset + 1;
3228 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003229 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003230
3231 ret = process_recorded_refs(sctx);
3232
3233out:
3234 btrfs_free_path(path);
3235 return ret;
3236}
3237
3238static int send_set_xattr(struct send_ctx *sctx,
3239 struct fs_path *path,
3240 const char *name, int name_len,
3241 const char *data, int data_len)
3242{
3243 int ret = 0;
3244
3245 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3246 if (ret < 0)
3247 goto out;
3248
3249 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3250 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3251 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3252
3253 ret = send_cmd(sctx);
3254
3255tlv_put_failure:
3256out:
3257 return ret;
3258}
3259
3260static int send_remove_xattr(struct send_ctx *sctx,
3261 struct fs_path *path,
3262 const char *name, int name_len)
3263{
3264 int ret = 0;
3265
3266 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3267 if (ret < 0)
3268 goto out;
3269
3270 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3271 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3272
3273 ret = send_cmd(sctx);
3274
3275tlv_put_failure:
3276out:
3277 return ret;
3278}
3279
3280static int __process_new_xattr(int num, struct btrfs_key *di_key,
3281 const char *name, int name_len,
3282 const char *data, int data_len,
3283 u8 type, void *ctx)
3284{
3285 int ret;
3286 struct send_ctx *sctx = ctx;
3287 struct fs_path *p;
3288 posix_acl_xattr_header dummy_acl;
3289
3290 p = fs_path_alloc(sctx);
3291 if (!p)
3292 return -ENOMEM;
3293
3294 /*
3295 * This hack is needed because empty acl's are stored as zero byte
3296 * data in xattrs. Problem with that is, that receiving these zero byte
3297 * acl's will fail later. To fix this, we send a dummy acl list that
3298 * only contains the version number and no entries.
3299 */
3300 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3301 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3302 if (data_len == 0) {
3303 dummy_acl.a_version =
3304 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3305 data = (char *)&dummy_acl;
3306 data_len = sizeof(dummy_acl);
3307 }
3308 }
3309
3310 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3311 if (ret < 0)
3312 goto out;
3313
3314 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3315
3316out:
3317 fs_path_free(sctx, p);
3318 return ret;
3319}
3320
3321static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3322 const char *name, int name_len,
3323 const char *data, int data_len,
3324 u8 type, void *ctx)
3325{
3326 int ret;
3327 struct send_ctx *sctx = ctx;
3328 struct fs_path *p;
3329
3330 p = fs_path_alloc(sctx);
3331 if (!p)
3332 return -ENOMEM;
3333
3334 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3335 if (ret < 0)
3336 goto out;
3337
3338 ret = send_remove_xattr(sctx, p, name, name_len);
3339
3340out:
3341 fs_path_free(sctx, p);
3342 return ret;
3343}
3344
3345static int process_new_xattr(struct send_ctx *sctx)
3346{
3347 int ret = 0;
3348
3349 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3350 sctx->cmp_key, __process_new_xattr, sctx);
3351
3352 return ret;
3353}
3354
3355static int process_deleted_xattr(struct send_ctx *sctx)
3356{
3357 int ret;
3358
3359 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3360 sctx->cmp_key, __process_deleted_xattr, sctx);
3361
3362 return ret;
3363}
3364
3365struct find_xattr_ctx {
3366 const char *name;
3367 int name_len;
3368 int found_idx;
3369 char *found_data;
3370 int found_data_len;
3371};
3372
3373static int __find_xattr(int num, struct btrfs_key *di_key,
3374 const char *name, int name_len,
3375 const char *data, int data_len,
3376 u8 type, void *vctx)
3377{
3378 struct find_xattr_ctx *ctx = vctx;
3379
3380 if (name_len == ctx->name_len &&
3381 strncmp(name, ctx->name, name_len) == 0) {
3382 ctx->found_idx = num;
3383 ctx->found_data_len = data_len;
3384 ctx->found_data = kmalloc(data_len, GFP_NOFS);
3385 if (!ctx->found_data)
3386 return -ENOMEM;
3387 memcpy(ctx->found_data, data, data_len);
3388 return 1;
3389 }
3390 return 0;
3391}
3392
3393static int find_xattr(struct send_ctx *sctx,
3394 struct btrfs_root *root,
3395 struct btrfs_path *path,
3396 struct btrfs_key *key,
3397 const char *name, int name_len,
3398 char **data, int *data_len)
3399{
3400 int ret;
3401 struct find_xattr_ctx ctx;
3402
3403 ctx.name = name;
3404 ctx.name_len = name_len;
3405 ctx.found_idx = -1;
3406 ctx.found_data = NULL;
3407 ctx.found_data_len = 0;
3408
3409 ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
3410 if (ret < 0)
3411 return ret;
3412
3413 if (ctx.found_idx == -1)
3414 return -ENOENT;
3415 if (data) {
3416 *data = ctx.found_data;
3417 *data_len = ctx.found_data_len;
3418 } else {
3419 kfree(ctx.found_data);
3420 }
3421 return ctx.found_idx;
3422}
3423
3424
3425static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3426 const char *name, int name_len,
3427 const char *data, int data_len,
3428 u8 type, void *ctx)
3429{
3430 int ret;
3431 struct send_ctx *sctx = ctx;
3432 char *found_data = NULL;
3433 int found_data_len = 0;
3434 struct fs_path *p = NULL;
3435
3436 ret = find_xattr(sctx, sctx->parent_root, sctx->right_path,
3437 sctx->cmp_key, name, name_len, &found_data,
3438 &found_data_len);
3439 if (ret == -ENOENT) {
3440 ret = __process_new_xattr(num, di_key, name, name_len, data,
3441 data_len, type, ctx);
3442 } else if (ret >= 0) {
3443 if (data_len != found_data_len ||
3444 memcmp(data, found_data, data_len)) {
3445 ret = __process_new_xattr(num, di_key, name, name_len,
3446 data, data_len, type, ctx);
3447 } else {
3448 ret = 0;
3449 }
3450 }
3451
3452 kfree(found_data);
3453 fs_path_free(sctx, p);
3454 return ret;
3455}
3456
3457static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3458 const char *name, int name_len,
3459 const char *data, int data_len,
3460 u8 type, void *ctx)
3461{
3462 int ret;
3463 struct send_ctx *sctx = ctx;
3464
3465 ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3466 name, name_len, NULL, NULL);
3467 if (ret == -ENOENT)
3468 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3469 data_len, type, ctx);
3470 else if (ret >= 0)
3471 ret = 0;
3472
3473 return ret;
3474}
3475
3476static int process_changed_xattr(struct send_ctx *sctx)
3477{
3478 int ret = 0;
3479
3480 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3481 sctx->cmp_key, __process_changed_new_xattr, sctx);
3482 if (ret < 0)
3483 goto out;
3484 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3485 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3486
3487out:
3488 return ret;
3489}
3490
3491static int process_all_new_xattrs(struct send_ctx *sctx)
3492{
3493 int ret;
3494 struct btrfs_root *root;
3495 struct btrfs_path *path;
3496 struct btrfs_key key;
3497 struct btrfs_key found_key;
3498 struct extent_buffer *eb;
3499 int slot;
3500
3501 path = alloc_path_for_send();
3502 if (!path)
3503 return -ENOMEM;
3504
3505 root = sctx->send_root;
3506
3507 key.objectid = sctx->cmp_key->objectid;
3508 key.type = BTRFS_XATTR_ITEM_KEY;
3509 key.offset = 0;
3510 while (1) {
3511 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3512 if (ret < 0)
3513 goto out;
3514 if (ret) {
3515 ret = 0;
3516 goto out;
3517 }
3518
3519 eb = path->nodes[0];
3520 slot = path->slots[0];
3521 btrfs_item_key_to_cpu(eb, &found_key, slot);
3522
3523 if (found_key.objectid != key.objectid ||
3524 found_key.type != key.type) {
3525 ret = 0;
3526 goto out;
3527 }
3528
3529 ret = iterate_dir_item(sctx, root, path, &found_key,
3530 __process_new_xattr, sctx);
3531 if (ret < 0)
3532 goto out;
3533
3534 btrfs_release_path(path);
3535 key.offset = found_key.offset + 1;
3536 }
3537
3538out:
3539 btrfs_free_path(path);
3540 return ret;
3541}
3542
3543/*
3544 * Read some bytes from the current inode/file and send a write command to
3545 * user space.
3546 */
3547static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3548{
3549 int ret = 0;
3550 struct fs_path *p;
3551 loff_t pos = offset;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003552 int num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003553 mm_segment_t old_fs;
3554
3555 p = fs_path_alloc(sctx);
3556 if (!p)
3557 return -ENOMEM;
3558
3559 /*
3560 * vfs normally only accepts user space buffers for security reasons.
3561 * we only read from the file and also only provide the read_buf buffer
3562 * to vfs. As this buffer does not come from a user space call, it's
3563 * ok to temporary allow kernel space buffers.
3564 */
3565 old_fs = get_fs();
3566 set_fs(KERNEL_DS);
3567
3568verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3569
3570 ret = open_cur_inode_file(sctx);
3571 if (ret < 0)
3572 goto out;
3573
3574 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3575 if (ret < 0)
3576 goto out;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003577 num_read = ret;
3578 if (!num_read)
Alexander Block31db9f72012-07-25 23:19:24 +02003579 goto out;
3580
3581 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3582 if (ret < 0)
3583 goto out;
3584
3585 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3586 if (ret < 0)
3587 goto out;
3588
3589 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3590 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02003591 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02003592
3593 ret = send_cmd(sctx);
3594
3595tlv_put_failure:
3596out:
3597 fs_path_free(sctx, p);
3598 set_fs(old_fs);
3599 if (ret < 0)
3600 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003601 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02003602}
3603
3604/*
3605 * Send a clone command to user space.
3606 */
3607static int send_clone(struct send_ctx *sctx,
3608 u64 offset, u32 len,
3609 struct clone_root *clone_root)
3610{
3611 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003612 struct fs_path *p;
3613 u64 gen;
3614
3615verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3616 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3617 clone_root->root->objectid, clone_root->ino,
3618 clone_root->offset);
3619
3620 p = fs_path_alloc(sctx);
3621 if (!p)
3622 return -ENOMEM;
3623
3624 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3625 if (ret < 0)
3626 goto out;
3627
3628 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3629 if (ret < 0)
3630 goto out;
3631
3632 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3633 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3634 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3635
Alexander Blocke938c8a2012-07-28 16:33:49 +02003636 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02003637 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003638 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003639 if (ret < 0)
3640 goto out;
3641 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3642 } else {
Alexander Blocke938c8a2012-07-28 16:33:49 +02003643 ret = get_inode_path(sctx, clone_root->root,
3644 clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003645 }
3646 if (ret < 0)
3647 goto out;
3648
3649 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02003650 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02003651 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02003652 clone_root->root->root_item.ctransid);
Alexander Block31db9f72012-07-25 23:19:24 +02003653 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3654 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3655 clone_root->offset);
3656
3657 ret = send_cmd(sctx);
3658
3659tlv_put_failure:
3660out:
3661 fs_path_free(sctx, p);
3662 return ret;
3663}
3664
3665static int send_write_or_clone(struct send_ctx *sctx,
3666 struct btrfs_path *path,
3667 struct btrfs_key *key,
3668 struct clone_root *clone_root)
3669{
3670 int ret = 0;
3671 struct btrfs_file_extent_item *ei;
3672 u64 offset = key->offset;
3673 u64 pos = 0;
3674 u64 len;
3675 u32 l;
3676 u8 type;
3677
3678 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3679 struct btrfs_file_extent_item);
3680 type = btrfs_file_extent_type(path->nodes[0], ei);
3681 if (type == BTRFS_FILE_EXTENT_INLINE)
3682 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3683 else
3684 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3685
3686 if (offset + len > sctx->cur_inode_size)
3687 len = sctx->cur_inode_size - offset;
3688 if (len == 0) {
3689 ret = 0;
3690 goto out;
3691 }
3692
3693 if (!clone_root) {
3694 while (pos < len) {
3695 l = len - pos;
3696 if (l > BTRFS_SEND_READ_SIZE)
3697 l = BTRFS_SEND_READ_SIZE;
3698 ret = send_write(sctx, pos + offset, l);
3699 if (ret < 0)
3700 goto out;
3701 if (!ret)
3702 break;
3703 pos += ret;
3704 }
3705 ret = 0;
3706 } else {
3707 ret = send_clone(sctx, offset, len, clone_root);
3708 }
3709
3710out:
3711 return ret;
3712}
3713
3714static int is_extent_unchanged(struct send_ctx *sctx,
3715 struct btrfs_path *left_path,
3716 struct btrfs_key *ekey)
3717{
3718 int ret = 0;
3719 struct btrfs_key key;
3720 struct btrfs_path *path = NULL;
3721 struct extent_buffer *eb;
3722 int slot;
3723 struct btrfs_key found_key;
3724 struct btrfs_file_extent_item *ei;
3725 u64 left_disknr;
3726 u64 right_disknr;
3727 u64 left_offset;
3728 u64 right_offset;
3729 u64 left_offset_fixed;
3730 u64 left_len;
3731 u64 right_len;
3732 u8 left_type;
3733 u8 right_type;
3734
3735 path = alloc_path_for_send();
3736 if (!path)
3737 return -ENOMEM;
3738
3739 eb = left_path->nodes[0];
3740 slot = left_path->slots[0];
3741
3742 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3743 left_type = btrfs_file_extent_type(eb, ei);
3744 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3745 left_len = btrfs_file_extent_num_bytes(eb, ei);
3746 left_offset = btrfs_file_extent_offset(eb, ei);
3747
3748 if (left_type != BTRFS_FILE_EXTENT_REG) {
3749 ret = 0;
3750 goto out;
3751 }
3752
3753 /*
3754 * Following comments will refer to these graphics. L is the left
3755 * extents which we are checking at the moment. 1-8 are the right
3756 * extents that we iterate.
3757 *
3758 * |-----L-----|
3759 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3760 *
3761 * |-----L-----|
3762 * |--1--|-2b-|...(same as above)
3763 *
3764 * Alternative situation. Happens on files where extents got split.
3765 * |-----L-----|
3766 * |-----------7-----------|-6-|
3767 *
3768 * Alternative situation. Happens on files which got larger.
3769 * |-----L-----|
3770 * |-8-|
3771 * Nothing follows after 8.
3772 */
3773
3774 key.objectid = ekey->objectid;
3775 key.type = BTRFS_EXTENT_DATA_KEY;
3776 key.offset = ekey->offset;
3777 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3778 if (ret < 0)
3779 goto out;
3780 if (ret) {
3781 ret = 0;
3782 goto out;
3783 }
3784
3785 /*
3786 * Handle special case where the right side has no extents at all.
3787 */
3788 eb = path->nodes[0];
3789 slot = path->slots[0];
3790 btrfs_item_key_to_cpu(eb, &found_key, slot);
3791 if (found_key.objectid != key.objectid ||
3792 found_key.type != key.type) {
3793 ret = 0;
3794 goto out;
3795 }
3796
3797 /*
3798 * We're now on 2a, 2b or 7.
3799 */
3800 key = found_key;
3801 while (key.offset < ekey->offset + left_len) {
3802 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3803 right_type = btrfs_file_extent_type(eb, ei);
3804 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3805 right_len = btrfs_file_extent_num_bytes(eb, ei);
3806 right_offset = btrfs_file_extent_offset(eb, ei);
3807
3808 if (right_type != BTRFS_FILE_EXTENT_REG) {
3809 ret = 0;
3810 goto out;
3811 }
3812
3813 /*
3814 * Are we at extent 8? If yes, we know the extent is changed.
3815 * This may only happen on the first iteration.
3816 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02003817 if (found_key.offset + right_len <= ekey->offset) {
Alexander Block31db9f72012-07-25 23:19:24 +02003818 ret = 0;
3819 goto out;
3820 }
3821
3822 left_offset_fixed = left_offset;
3823 if (key.offset < ekey->offset) {
3824 /* Fix the right offset for 2a and 7. */
3825 right_offset += ekey->offset - key.offset;
3826 } else {
3827 /* Fix the left offset for all behind 2a and 2b */
3828 left_offset_fixed += key.offset - ekey->offset;
3829 }
3830
3831 /*
3832 * Check if we have the same extent.
3833 */
Alexander Block39540962012-08-01 12:46:05 +02003834 if (left_disknr != right_disknr ||
3835 left_offset_fixed != right_offset) {
Alexander Block31db9f72012-07-25 23:19:24 +02003836 ret = 0;
3837 goto out;
3838 }
3839
3840 /*
3841 * Go to the next extent.
3842 */
3843 ret = btrfs_next_item(sctx->parent_root, path);
3844 if (ret < 0)
3845 goto out;
3846 if (!ret) {
3847 eb = path->nodes[0];
3848 slot = path->slots[0];
3849 btrfs_item_key_to_cpu(eb, &found_key, slot);
3850 }
3851 if (ret || found_key.objectid != key.objectid ||
3852 found_key.type != key.type) {
3853 key.offset += right_len;
3854 break;
3855 } else {
3856 if (found_key.offset != key.offset + right_len) {
3857 /* Should really not happen */
3858 ret = -EIO;
3859 goto out;
3860 }
3861 }
3862 key = found_key;
3863 }
3864
3865 /*
3866 * We're now behind the left extent (treat as unchanged) or at the end
3867 * of the right side (treat as changed).
3868 */
3869 if (key.offset >= ekey->offset + left_len)
3870 ret = 1;
3871 else
3872 ret = 0;
3873
3874
3875out:
3876 btrfs_free_path(path);
3877 return ret;
3878}
3879
3880static int process_extent(struct send_ctx *sctx,
3881 struct btrfs_path *path,
3882 struct btrfs_key *key)
3883{
3884 int ret = 0;
3885 struct clone_root *found_clone = NULL;
3886
3887 if (S_ISLNK(sctx->cur_inode_mode))
3888 return 0;
3889
3890 if (sctx->parent_root && !sctx->cur_inode_new) {
3891 ret = is_extent_unchanged(sctx, path, key);
3892 if (ret < 0)
3893 goto out;
3894 if (ret) {
3895 ret = 0;
3896 goto out;
3897 }
3898 }
3899
3900 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
3901 sctx->cur_inode_size, &found_clone);
3902 if (ret != -ENOENT && ret < 0)
3903 goto out;
3904
3905 ret = send_write_or_clone(sctx, path, key, found_clone);
3906
3907out:
3908 return ret;
3909}
3910
3911static int process_all_extents(struct send_ctx *sctx)
3912{
3913 int ret;
3914 struct btrfs_root *root;
3915 struct btrfs_path *path;
3916 struct btrfs_key key;
3917 struct btrfs_key found_key;
3918 struct extent_buffer *eb;
3919 int slot;
3920
3921 root = sctx->send_root;
3922 path = alloc_path_for_send();
3923 if (!path)
3924 return -ENOMEM;
3925
3926 key.objectid = sctx->cmp_key->objectid;
3927 key.type = BTRFS_EXTENT_DATA_KEY;
3928 key.offset = 0;
3929 while (1) {
3930 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3931 if (ret < 0)
3932 goto out;
3933 if (ret) {
3934 ret = 0;
3935 goto out;
3936 }
3937
3938 eb = path->nodes[0];
3939 slot = path->slots[0];
3940 btrfs_item_key_to_cpu(eb, &found_key, slot);
3941
3942 if (found_key.objectid != key.objectid ||
3943 found_key.type != key.type) {
3944 ret = 0;
3945 goto out;
3946 }
3947
3948 ret = process_extent(sctx, path, &found_key);
3949 if (ret < 0)
3950 goto out;
3951
3952 btrfs_release_path(path);
3953 key.offset = found_key.offset + 1;
3954 }
3955
3956out:
3957 btrfs_free_path(path);
3958 return ret;
3959}
3960
3961static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
3962{
3963 int ret = 0;
3964
3965 if (sctx->cur_ino == 0)
3966 goto out;
3967 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
3968 sctx->cmp_key->type <= BTRFS_INODE_REF_KEY)
3969 goto out;
3970 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
3971 goto out;
3972
3973 ret = process_recorded_refs(sctx);
Alexander Blocke479d9b2012-07-28 16:09:35 +02003974 if (ret < 0)
3975 goto out;
3976
3977 /*
3978 * We have processed the refs and thus need to advance send_progress.
3979 * Now, calls to get_cur_xxx will take the updated refs of the current
3980 * inode into account.
3981 */
3982 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02003983
3984out:
3985 return ret;
3986}
3987
3988static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
3989{
3990 int ret = 0;
3991 u64 left_mode;
3992 u64 left_uid;
3993 u64 left_gid;
3994 u64 right_mode;
3995 u64 right_uid;
3996 u64 right_gid;
3997 int need_chmod = 0;
3998 int need_chown = 0;
3999
4000 ret = process_recorded_refs_if_needed(sctx, at_end);
4001 if (ret < 0)
4002 goto out;
4003
4004 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4005 goto out;
4006 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4007 goto out;
4008
4009 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004010 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004011 if (ret < 0)
4012 goto out;
4013
4014 if (!S_ISLNK(sctx->cur_inode_mode)) {
4015 if (!sctx->parent_root || sctx->cur_inode_new) {
4016 need_chmod = 1;
4017 need_chown = 1;
4018 } else {
4019 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4020 NULL, NULL, &right_mode, &right_uid,
Alexander Block85a7b332012-07-26 23:39:10 +02004021 &right_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004022 if (ret < 0)
4023 goto out;
4024
4025 if (left_uid != right_uid || left_gid != right_gid)
4026 need_chown = 1;
4027 if (left_mode != right_mode)
4028 need_chmod = 1;
4029 }
4030 }
4031
4032 if (S_ISREG(sctx->cur_inode_mode)) {
4033 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4034 sctx->cur_inode_size);
4035 if (ret < 0)
4036 goto out;
4037 }
4038
4039 if (need_chown) {
4040 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4041 left_uid, left_gid);
4042 if (ret < 0)
4043 goto out;
4044 }
4045 if (need_chmod) {
4046 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4047 left_mode);
4048 if (ret < 0)
4049 goto out;
4050 }
4051
4052 /*
4053 * Need to send that every time, no matter if it actually changed
4054 * between the two trees as we have done changes to the inode before.
4055 */
4056 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4057 if (ret < 0)
4058 goto out;
4059
4060out:
4061 return ret;
4062}
4063
4064static int changed_inode(struct send_ctx *sctx,
4065 enum btrfs_compare_tree_result result)
4066{
4067 int ret = 0;
4068 struct btrfs_key *key = sctx->cmp_key;
4069 struct btrfs_inode_item *left_ii = NULL;
4070 struct btrfs_inode_item *right_ii = NULL;
4071 u64 left_gen = 0;
4072 u64 right_gen = 0;
4073
4074 ret = close_cur_inode_file(sctx);
4075 if (ret < 0)
4076 goto out;
4077
4078 sctx->cur_ino = key->objectid;
4079 sctx->cur_inode_new_gen = 0;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004080
4081 /*
4082 * Set send_progress to current inode. This will tell all get_cur_xxx
4083 * functions that the current inode's refs are not updated yet. Later,
4084 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4085 */
Alexander Block31db9f72012-07-25 23:19:24 +02004086 sctx->send_progress = sctx->cur_ino;
4087
4088 if (result == BTRFS_COMPARE_TREE_NEW ||
4089 result == BTRFS_COMPARE_TREE_CHANGED) {
4090 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4091 sctx->left_path->slots[0],
4092 struct btrfs_inode_item);
4093 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4094 left_ii);
4095 } else {
4096 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4097 sctx->right_path->slots[0],
4098 struct btrfs_inode_item);
4099 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4100 right_ii);
4101 }
4102 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4103 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4104 sctx->right_path->slots[0],
4105 struct btrfs_inode_item);
4106
4107 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4108 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02004109
4110 /*
4111 * The cur_ino = root dir case is special here. We can't treat
4112 * the inode as deleted+reused because it would generate a
4113 * stream that tries to delete/mkdir the root dir.
4114 */
4115 if (left_gen != right_gen &&
4116 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02004117 sctx->cur_inode_new_gen = 1;
4118 }
4119
4120 if (result == BTRFS_COMPARE_TREE_NEW) {
4121 sctx->cur_inode_gen = left_gen;
4122 sctx->cur_inode_new = 1;
4123 sctx->cur_inode_deleted = 0;
4124 sctx->cur_inode_size = btrfs_inode_size(
4125 sctx->left_path->nodes[0], left_ii);
4126 sctx->cur_inode_mode = btrfs_inode_mode(
4127 sctx->left_path->nodes[0], left_ii);
4128 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02004129 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004130 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4131 sctx->cur_inode_gen = right_gen;
4132 sctx->cur_inode_new = 0;
4133 sctx->cur_inode_deleted = 1;
4134 sctx->cur_inode_size = btrfs_inode_size(
4135 sctx->right_path->nodes[0], right_ii);
4136 sctx->cur_inode_mode = btrfs_inode_mode(
4137 sctx->right_path->nodes[0], right_ii);
4138 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02004139 /*
4140 * We need to do some special handling in case the inode was
4141 * reported as changed with a changed generation number. This
4142 * means that the original inode was deleted and new inode
4143 * reused the same inum. So we have to treat the old inode as
4144 * deleted and the new one as new.
4145 */
Alexander Block31db9f72012-07-25 23:19:24 +02004146 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02004147 /*
4148 * First, process the inode as if it was deleted.
4149 */
Alexander Block31db9f72012-07-25 23:19:24 +02004150 sctx->cur_inode_gen = right_gen;
4151 sctx->cur_inode_new = 0;
4152 sctx->cur_inode_deleted = 1;
4153 sctx->cur_inode_size = btrfs_inode_size(
4154 sctx->right_path->nodes[0], right_ii);
4155 sctx->cur_inode_mode = btrfs_inode_mode(
4156 sctx->right_path->nodes[0], right_ii);
4157 ret = process_all_refs(sctx,
4158 BTRFS_COMPARE_TREE_DELETED);
4159 if (ret < 0)
4160 goto out;
4161
Alexander Block766702e2012-07-28 14:11:31 +02004162 /*
4163 * Now process the inode as if it was new.
4164 */
Alexander Block31db9f72012-07-25 23:19:24 +02004165 sctx->cur_inode_gen = left_gen;
4166 sctx->cur_inode_new = 1;
4167 sctx->cur_inode_deleted = 0;
4168 sctx->cur_inode_size = btrfs_inode_size(
4169 sctx->left_path->nodes[0], left_ii);
4170 sctx->cur_inode_mode = btrfs_inode_mode(
4171 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02004172 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004173 if (ret < 0)
4174 goto out;
4175
4176 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4177 if (ret < 0)
4178 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004179 /*
4180 * Advance send_progress now as we did not get into
4181 * process_recorded_refs_if_needed in the new_gen case.
4182 */
4183 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02004184
4185 /*
4186 * Now process all extents and xattrs of the inode as if
4187 * they were all new.
4188 */
Alexander Block31db9f72012-07-25 23:19:24 +02004189 ret = process_all_extents(sctx);
4190 if (ret < 0)
4191 goto out;
4192 ret = process_all_new_xattrs(sctx);
4193 if (ret < 0)
4194 goto out;
4195 } else {
4196 sctx->cur_inode_gen = left_gen;
4197 sctx->cur_inode_new = 0;
4198 sctx->cur_inode_new_gen = 0;
4199 sctx->cur_inode_deleted = 0;
4200 sctx->cur_inode_size = btrfs_inode_size(
4201 sctx->left_path->nodes[0], left_ii);
4202 sctx->cur_inode_mode = btrfs_inode_mode(
4203 sctx->left_path->nodes[0], left_ii);
4204 }
4205 }
4206
4207out:
4208 return ret;
4209}
4210
Alexander Block766702e2012-07-28 14:11:31 +02004211/*
4212 * We have to process new refs before deleted refs, but compare_trees gives us
4213 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4214 * first and later process them in process_recorded_refs.
4215 * For the cur_inode_new_gen case, we skip recording completely because
4216 * changed_inode did already initiate processing of refs. The reason for this is
4217 * that in this case, compare_tree actually compares the refs of 2 different
4218 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4219 * refs of the right tree as deleted and all refs of the left tree as new.
4220 */
Alexander Block31db9f72012-07-25 23:19:24 +02004221static int changed_ref(struct send_ctx *sctx,
4222 enum btrfs_compare_tree_result result)
4223{
4224 int ret = 0;
4225
4226 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4227
4228 if (!sctx->cur_inode_new_gen &&
4229 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4230 if (result == BTRFS_COMPARE_TREE_NEW)
4231 ret = record_new_ref(sctx);
4232 else if (result == BTRFS_COMPARE_TREE_DELETED)
4233 ret = record_deleted_ref(sctx);
4234 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4235 ret = record_changed_ref(sctx);
4236 }
4237
4238 return ret;
4239}
4240
Alexander Block766702e2012-07-28 14:11:31 +02004241/*
4242 * Process new/deleted/changed xattrs. We skip processing in the
4243 * cur_inode_new_gen case because changed_inode did already initiate processing
4244 * of xattrs. The reason is the same as in changed_ref
4245 */
Alexander Block31db9f72012-07-25 23:19:24 +02004246static int changed_xattr(struct send_ctx *sctx,
4247 enum btrfs_compare_tree_result result)
4248{
4249 int ret = 0;
4250
4251 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4252
4253 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4254 if (result == BTRFS_COMPARE_TREE_NEW)
4255 ret = process_new_xattr(sctx);
4256 else if (result == BTRFS_COMPARE_TREE_DELETED)
4257 ret = process_deleted_xattr(sctx);
4258 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4259 ret = process_changed_xattr(sctx);
4260 }
4261
4262 return ret;
4263}
4264
Alexander Block766702e2012-07-28 14:11:31 +02004265/*
4266 * Process new/deleted/changed extents. We skip processing in the
4267 * cur_inode_new_gen case because changed_inode did already initiate processing
4268 * of extents. The reason is the same as in changed_ref
4269 */
Alexander Block31db9f72012-07-25 23:19:24 +02004270static int changed_extent(struct send_ctx *sctx,
4271 enum btrfs_compare_tree_result result)
4272{
4273 int ret = 0;
4274
4275 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4276
4277 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4278 if (result != BTRFS_COMPARE_TREE_DELETED)
4279 ret = process_extent(sctx, sctx->left_path,
4280 sctx->cmp_key);
4281 }
4282
4283 return ret;
4284}
4285
Alexander Block766702e2012-07-28 14:11:31 +02004286/*
4287 * Updates compare related fields in sctx and simply forwards to the actual
4288 * changed_xxx functions.
4289 */
Alexander Block31db9f72012-07-25 23:19:24 +02004290static int changed_cb(struct btrfs_root *left_root,
4291 struct btrfs_root *right_root,
4292 struct btrfs_path *left_path,
4293 struct btrfs_path *right_path,
4294 struct btrfs_key *key,
4295 enum btrfs_compare_tree_result result,
4296 void *ctx)
4297{
4298 int ret = 0;
4299 struct send_ctx *sctx = ctx;
4300
4301 sctx->left_path = left_path;
4302 sctx->right_path = right_path;
4303 sctx->cmp_key = key;
4304
4305 ret = finish_inode_if_needed(sctx, 0);
4306 if (ret < 0)
4307 goto out;
4308
Alexander Block2981e222012-08-01 14:47:03 +02004309 /* Ignore non-FS objects */
4310 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4311 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4312 goto out;
4313
Alexander Block31db9f72012-07-25 23:19:24 +02004314 if (key->type == BTRFS_INODE_ITEM_KEY)
4315 ret = changed_inode(sctx, result);
4316 else if (key->type == BTRFS_INODE_REF_KEY)
4317 ret = changed_ref(sctx, result);
4318 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4319 ret = changed_xattr(sctx, result);
4320 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4321 ret = changed_extent(sctx, result);
4322
4323out:
4324 return ret;
4325}
4326
4327static int full_send_tree(struct send_ctx *sctx)
4328{
4329 int ret;
4330 struct btrfs_trans_handle *trans = NULL;
4331 struct btrfs_root *send_root = sctx->send_root;
4332 struct btrfs_key key;
4333 struct btrfs_key found_key;
4334 struct btrfs_path *path;
4335 struct extent_buffer *eb;
4336 int slot;
4337 u64 start_ctransid;
4338 u64 ctransid;
4339
4340 path = alloc_path_for_send();
4341 if (!path)
4342 return -ENOMEM;
4343
4344 spin_lock(&send_root->root_times_lock);
4345 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4346 spin_unlock(&send_root->root_times_lock);
4347
4348 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4349 key.type = BTRFS_INODE_ITEM_KEY;
4350 key.offset = 0;
4351
4352join_trans:
4353 /*
4354 * We need to make sure the transaction does not get committed
4355 * while we do anything on commit roots. Join a transaction to prevent
4356 * this.
4357 */
4358 trans = btrfs_join_transaction(send_root);
4359 if (IS_ERR(trans)) {
4360 ret = PTR_ERR(trans);
4361 trans = NULL;
4362 goto out;
4363 }
4364
4365 /*
Alexander Block766702e2012-07-28 14:11:31 +02004366 * Make sure the tree has not changed after re-joining. We detect this
4367 * by comparing start_ctransid and ctransid. They should always match.
Alexander Block31db9f72012-07-25 23:19:24 +02004368 */
4369 spin_lock(&send_root->root_times_lock);
4370 ctransid = btrfs_root_ctransid(&send_root->root_item);
4371 spin_unlock(&send_root->root_times_lock);
4372
4373 if (ctransid != start_ctransid) {
4374 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4375 "send was modified in between. This is "
4376 "probably a bug.\n");
4377 ret = -EIO;
4378 goto out;
4379 }
4380
4381 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4382 if (ret < 0)
4383 goto out;
4384 if (ret)
4385 goto out_finish;
4386
4387 while (1) {
4388 /*
4389 * When someone want to commit while we iterate, end the
4390 * joined transaction and rejoin.
4391 */
4392 if (btrfs_should_end_transaction(trans, send_root)) {
4393 ret = btrfs_end_transaction(trans, send_root);
4394 trans = NULL;
4395 if (ret < 0)
4396 goto out;
4397 btrfs_release_path(path);
4398 goto join_trans;
4399 }
4400
4401 eb = path->nodes[0];
4402 slot = path->slots[0];
4403 btrfs_item_key_to_cpu(eb, &found_key, slot);
4404
4405 ret = changed_cb(send_root, NULL, path, NULL,
4406 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4407 if (ret < 0)
4408 goto out;
4409
4410 key.objectid = found_key.objectid;
4411 key.type = found_key.type;
4412 key.offset = found_key.offset + 1;
4413
4414 ret = btrfs_next_item(send_root, path);
4415 if (ret < 0)
4416 goto out;
4417 if (ret) {
4418 ret = 0;
4419 break;
4420 }
4421 }
4422
4423out_finish:
4424 ret = finish_inode_if_needed(sctx, 1);
4425
4426out:
4427 btrfs_free_path(path);
4428 if (trans) {
4429 if (!ret)
4430 ret = btrfs_end_transaction(trans, send_root);
4431 else
4432 btrfs_end_transaction(trans, send_root);
4433 }
4434 return ret;
4435}
4436
4437static int send_subvol(struct send_ctx *sctx)
4438{
4439 int ret;
4440
4441 ret = send_header(sctx);
4442 if (ret < 0)
4443 goto out;
4444
4445 ret = send_subvol_begin(sctx);
4446 if (ret < 0)
4447 goto out;
4448
4449 if (sctx->parent_root) {
4450 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4451 changed_cb, sctx);
4452 if (ret < 0)
4453 goto out;
4454 ret = finish_inode_if_needed(sctx, 1);
4455 if (ret < 0)
4456 goto out;
4457 } else {
4458 ret = full_send_tree(sctx);
4459 if (ret < 0)
4460 goto out;
4461 }
4462
4463out:
4464 if (!ret)
4465 ret = close_cur_inode_file(sctx);
4466 else
4467 close_cur_inode_file(sctx);
4468
4469 free_recorded_refs(sctx);
4470 return ret;
4471}
4472
4473long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4474{
4475 int ret = 0;
4476 struct btrfs_root *send_root;
4477 struct btrfs_root *clone_root;
4478 struct btrfs_fs_info *fs_info;
4479 struct btrfs_ioctl_send_args *arg = NULL;
4480 struct btrfs_key key;
4481 struct file *filp = NULL;
4482 struct send_ctx *sctx = NULL;
4483 u32 i;
4484 u64 *clone_sources_tmp = NULL;
4485
4486 if (!capable(CAP_SYS_ADMIN))
4487 return -EPERM;
4488
4489 send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root;
4490 fs_info = send_root->fs_info;
4491
4492 arg = memdup_user(arg_, sizeof(*arg));
4493 if (IS_ERR(arg)) {
4494 ret = PTR_ERR(arg);
4495 arg = NULL;
4496 goto out;
4497 }
4498
4499 if (!access_ok(VERIFY_READ, arg->clone_sources,
4500 sizeof(*arg->clone_sources *
4501 arg->clone_sources_count))) {
4502 ret = -EFAULT;
4503 goto out;
4504 }
4505
4506 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4507 if (!sctx) {
4508 ret = -ENOMEM;
4509 goto out;
4510 }
4511
4512 INIT_LIST_HEAD(&sctx->new_refs);
4513 INIT_LIST_HEAD(&sctx->deleted_refs);
4514 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4515 INIT_LIST_HEAD(&sctx->name_cache_list);
4516
4517 sctx->send_filp = fget(arg->send_fd);
4518 if (IS_ERR(sctx->send_filp)) {
4519 ret = PTR_ERR(sctx->send_filp);
4520 goto out;
4521 }
4522
4523 sctx->mnt = mnt_file->f_path.mnt;
4524
4525 sctx->send_root = send_root;
4526 sctx->clone_roots_cnt = arg->clone_sources_count;
4527
4528 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4529 sctx->send_buf = vmalloc(sctx->send_max_size);
4530 if (!sctx->send_buf) {
4531 ret = -ENOMEM;
4532 goto out;
4533 }
4534
4535 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4536 if (!sctx->read_buf) {
4537 ret = -ENOMEM;
4538 goto out;
4539 }
4540
4541 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4542 (arg->clone_sources_count + 1));
4543 if (!sctx->clone_roots) {
4544 ret = -ENOMEM;
4545 goto out;
4546 }
4547
4548 if (arg->clone_sources_count) {
4549 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4550 sizeof(*arg->clone_sources));
4551 if (!clone_sources_tmp) {
4552 ret = -ENOMEM;
4553 goto out;
4554 }
4555
4556 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4557 arg->clone_sources_count *
4558 sizeof(*arg->clone_sources));
4559 if (ret) {
4560 ret = -EFAULT;
4561 goto out;
4562 }
4563
4564 for (i = 0; i < arg->clone_sources_count; i++) {
4565 key.objectid = clone_sources_tmp[i];
4566 key.type = BTRFS_ROOT_ITEM_KEY;
4567 key.offset = (u64)-1;
4568 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4569 if (!clone_root) {
4570 ret = -EINVAL;
4571 goto out;
4572 }
4573 if (IS_ERR(clone_root)) {
4574 ret = PTR_ERR(clone_root);
4575 goto out;
4576 }
4577 sctx->clone_roots[i].root = clone_root;
4578 }
4579 vfree(clone_sources_tmp);
4580 clone_sources_tmp = NULL;
4581 }
4582
4583 if (arg->parent_root) {
4584 key.objectid = arg->parent_root;
4585 key.type = BTRFS_ROOT_ITEM_KEY;
4586 key.offset = (u64)-1;
4587 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4588 if (!sctx->parent_root) {
4589 ret = -EINVAL;
4590 goto out;
4591 }
4592 }
4593
4594 /*
4595 * Clones from send_root are allowed, but only if the clone source
4596 * is behind the current send position. This is checked while searching
4597 * for possible clone sources.
4598 */
4599 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4600
4601 /* We do a bsearch later */
4602 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4603 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4604 NULL);
4605
4606 ret = send_subvol(sctx);
4607 if (ret < 0)
4608 goto out;
4609
4610 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4611 if (ret < 0)
4612 goto out;
4613 ret = send_cmd(sctx);
4614 if (ret < 0)
4615 goto out;
4616
4617out:
4618 if (filp)
4619 fput(filp);
4620 kfree(arg);
4621 vfree(clone_sources_tmp);
4622
4623 if (sctx) {
4624 if (sctx->send_filp)
4625 fput(sctx->send_filp);
4626
4627 vfree(sctx->clone_roots);
4628 vfree(sctx->send_buf);
4629 vfree(sctx->read_buf);
4630
4631 name_cache_free(sctx);
4632
4633 kfree(sctx);
4634 }
4635
4636 return ret;
4637}