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