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