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