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