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