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