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