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