x86/xen: resume timer irqs early
[linux/fpc-iii.git] / fs / btrfs / send.c
blob76736b57de5edfa6af7eed02911d3bacfe5dc976
1 /*
2 * Copyright (C) 2012 Alexander Block. All rights reserved.
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.
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.
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.
19 #include <linux/bsearch.h>
20 #include <linux/fs.h>
21 #include <linux/file.h>
22 #include <linux/sort.h>
23 #include <linux/mount.h>
24 #include <linux/xattr.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/radix-tree.h>
27 #include <linux/crc32c.h>
28 #include <linux/vmalloc.h>
29 #include <linux/string.h>
31 #include "send.h"
32 #include "backref.h"
33 #include "locking.h"
34 #include "disk-io.h"
35 #include "btrfs_inode.h"
36 #include "transaction.h"
38 static int g_verbose = 0;
40 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
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.
49 struct fs_path {
50 union {
51 struct {
52 char *start;
53 char *end;
54 char *prepared;
56 char *buf;
57 int buf_len;
58 unsigned int reversed:1;
59 unsigned int virtual_mem:1;
60 char inline_buf[];
62 char pad[PAGE_SIZE];
65 #define FS_PATH_INLINE_SIZE \
66 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
69 /* reused for each extent */
70 struct clone_root {
71 struct btrfs_root *root;
72 u64 ino;
73 u64 offset;
75 u64 found_refs;
78 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
79 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
81 struct send_ctx {
82 struct file *send_filp;
83 loff_t send_off;
84 char *send_buf;
85 u32 send_size;
86 u32 send_max_size;
87 u64 total_send_size;
88 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
89 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
91 struct vfsmount *mnt;
93 struct btrfs_root *send_root;
94 struct btrfs_root *parent_root;
95 struct clone_root *clone_roots;
96 int clone_roots_cnt;
98 /* current state of the compare_tree call */
99 struct btrfs_path *left_path;
100 struct btrfs_path *right_path;
101 struct btrfs_key *cmp_key;
104 * infos of the currently processed inode. In case of deleted inodes,
105 * these are the values from the deleted inode.
107 u64 cur_ino;
108 u64 cur_inode_gen;
109 int cur_inode_new;
110 int cur_inode_new_gen;
111 int cur_inode_deleted;
112 u64 cur_inode_size;
113 u64 cur_inode_mode;
115 u64 send_progress;
117 struct list_head new_refs;
118 struct list_head deleted_refs;
120 struct radix_tree_root name_cache;
121 struct list_head name_cache_list;
122 int name_cache_size;
124 char *read_buf;
127 struct name_cache_entry {
128 struct list_head list;
130 * radix_tree has only 32bit entries but we need to handle 64bit inums.
131 * We use the lower 32bit of the 64bit inum to store it in the tree. If
132 * more then one inum would fall into the same entry, we use radix_list
133 * to store the additional entries. radix_list is also used to store
134 * entries where two entries have the same inum but different
135 * generations.
137 struct list_head radix_list;
138 u64 ino;
139 u64 gen;
140 u64 parent_ino;
141 u64 parent_gen;
142 int ret;
143 int need_later_update;
144 int name_len;
145 char name[];
148 static void fs_path_reset(struct fs_path *p)
150 if (p->reversed) {
151 p->start = p->buf + p->buf_len - 1;
152 p->end = p->start;
153 *p->start = 0;
154 } else {
155 p->start = p->buf;
156 p->end = p->start;
157 *p->start = 0;
161 static struct fs_path *fs_path_alloc(void)
163 struct fs_path *p;
165 p = kmalloc(sizeof(*p), GFP_NOFS);
166 if (!p)
167 return NULL;
168 p->reversed = 0;
169 p->virtual_mem = 0;
170 p->buf = p->inline_buf;
171 p->buf_len = FS_PATH_INLINE_SIZE;
172 fs_path_reset(p);
173 return p;
176 static struct fs_path *fs_path_alloc_reversed(void)
178 struct fs_path *p;
180 p = fs_path_alloc();
181 if (!p)
182 return NULL;
183 p->reversed = 1;
184 fs_path_reset(p);
185 return p;
188 static void fs_path_free(struct fs_path *p)
190 if (!p)
191 return;
192 if (p->buf != p->inline_buf) {
193 if (p->virtual_mem)
194 vfree(p->buf);
195 else
196 kfree(p->buf);
198 kfree(p);
201 static int fs_path_len(struct fs_path *p)
203 return p->end - p->start;
206 static int fs_path_ensure_buf(struct fs_path *p, int len)
208 char *tmp_buf;
209 int path_len;
210 int old_buf_len;
212 len++;
214 if (p->buf_len >= len)
215 return 0;
217 path_len = p->end - p->start;
218 old_buf_len = p->buf_len;
219 len = PAGE_ALIGN(len);
221 if (p->buf == p->inline_buf) {
222 tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN);
223 if (!tmp_buf) {
224 tmp_buf = vmalloc(len);
225 if (!tmp_buf)
226 return -ENOMEM;
227 p->virtual_mem = 1;
229 memcpy(tmp_buf, p->buf, p->buf_len);
230 p->buf = tmp_buf;
231 p->buf_len = len;
232 } else {
233 if (p->virtual_mem) {
234 tmp_buf = vmalloc(len);
235 if (!tmp_buf)
236 return -ENOMEM;
237 memcpy(tmp_buf, p->buf, p->buf_len);
238 vfree(p->buf);
239 } else {
240 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
241 if (!tmp_buf) {
242 tmp_buf = vmalloc(len);
243 if (!tmp_buf)
244 return -ENOMEM;
245 memcpy(tmp_buf, p->buf, p->buf_len);
246 kfree(p->buf);
247 p->virtual_mem = 1;
250 p->buf = tmp_buf;
251 p->buf_len = len;
253 if (p->reversed) {
254 tmp_buf = p->buf + old_buf_len - path_len - 1;
255 p->end = p->buf + p->buf_len - 1;
256 p->start = p->end - path_len;
257 memmove(p->start, tmp_buf, path_len + 1);
258 } else {
259 p->start = p->buf;
260 p->end = p->start + path_len;
262 return 0;
265 static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
267 int ret;
268 int new_len;
270 new_len = p->end - p->start + name_len;
271 if (p->start != p->end)
272 new_len++;
273 ret = fs_path_ensure_buf(p, new_len);
274 if (ret < 0)
275 goto out;
277 if (p->reversed) {
278 if (p->start != p->end)
279 *--p->start = '/';
280 p->start -= name_len;
281 p->prepared = p->start;
282 } else {
283 if (p->start != p->end)
284 *p->end++ = '/';
285 p->prepared = p->end;
286 p->end += name_len;
287 *p->end = 0;
290 out:
291 return ret;
294 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
296 int ret;
298 ret = fs_path_prepare_for_add(p, name_len);
299 if (ret < 0)
300 goto out;
301 memcpy(p->prepared, name, name_len);
302 p->prepared = NULL;
304 out:
305 return ret;
308 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
310 int ret;
312 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
313 if (ret < 0)
314 goto out;
315 memcpy(p->prepared, p2->start, p2->end - p2->start);
316 p->prepared = NULL;
318 out:
319 return ret;
322 static int fs_path_add_from_extent_buffer(struct fs_path *p,
323 struct extent_buffer *eb,
324 unsigned long off, int len)
326 int ret;
328 ret = fs_path_prepare_for_add(p, len);
329 if (ret < 0)
330 goto out;
332 read_extent_buffer(eb, p->prepared, off, len);
333 p->prepared = NULL;
335 out:
336 return ret;
339 #if 0
340 static void fs_path_remove(struct fs_path *p)
342 BUG_ON(p->reversed);
343 while (p->start != p->end && *p->end != '/')
344 p->end--;
345 *p->end = 0;
347 #endif
349 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
351 int ret;
353 p->reversed = from->reversed;
354 fs_path_reset(p);
356 ret = fs_path_add_path(p, from);
358 return ret;
362 static void fs_path_unreverse(struct fs_path *p)
364 char *tmp;
365 int len;
367 if (!p->reversed)
368 return;
370 tmp = p->start;
371 len = p->end - p->start;
372 p->start = p->buf;
373 p->end = p->start + len;
374 memmove(p->start, tmp, len + 1);
375 p->reversed = 0;
378 static struct btrfs_path *alloc_path_for_send(void)
380 struct btrfs_path *path;
382 path = btrfs_alloc_path();
383 if (!path)
384 return NULL;
385 path->search_commit_root = 1;
386 path->skip_locking = 1;
387 return path;
390 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
392 int ret;
393 mm_segment_t old_fs;
394 u32 pos = 0;
396 old_fs = get_fs();
397 set_fs(KERNEL_DS);
399 while (pos < len) {
400 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
401 /* TODO handle that correctly */
402 /*if (ret == -ERESTARTSYS) {
403 continue;
405 if (ret < 0)
406 goto out;
407 if (ret == 0) {
408 ret = -EIO;
409 goto out;
411 pos += ret;
414 ret = 0;
416 out:
417 set_fs(old_fs);
418 return ret;
421 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
423 struct btrfs_tlv_header *hdr;
424 int total_len = sizeof(*hdr) + len;
425 int left = sctx->send_max_size - sctx->send_size;
427 if (unlikely(left < total_len))
428 return -EOVERFLOW;
430 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
431 hdr->tlv_type = cpu_to_le16(attr);
432 hdr->tlv_len = cpu_to_le16(len);
433 memcpy(hdr + 1, data, len);
434 sctx->send_size += total_len;
436 return 0;
439 #if 0
440 static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
442 return tlv_put(sctx, attr, &value, sizeof(value));
445 static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
447 __le16 tmp = cpu_to_le16(value);
448 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
451 static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
453 __le32 tmp = cpu_to_le32(value);
454 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
456 #endif
458 static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
460 __le64 tmp = cpu_to_le64(value);
461 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
464 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
465 const char *str, int len)
467 if (len == -1)
468 len = strlen(str);
469 return tlv_put(sctx, attr, str, len);
472 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
473 const u8 *uuid)
475 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
478 #if 0
479 static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
480 struct timespec *ts)
482 struct btrfs_timespec bts;
483 bts.sec = cpu_to_le64(ts->tv_sec);
484 bts.nsec = cpu_to_le32(ts->tv_nsec);
485 return tlv_put(sctx, attr, &bts, sizeof(bts));
487 #endif
489 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
490 struct extent_buffer *eb,
491 struct btrfs_timespec *ts)
493 struct btrfs_timespec bts;
494 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
495 return tlv_put(sctx, attr, &bts, sizeof(bts));
499 #define TLV_PUT(sctx, attrtype, attrlen, data) \
500 do { \
501 ret = tlv_put(sctx, attrtype, attrlen, data); \
502 if (ret < 0) \
503 goto tlv_put_failure; \
504 } while (0)
506 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
507 do { \
508 ret = tlv_put_u##bits(sctx, attrtype, value); \
509 if (ret < 0) \
510 goto tlv_put_failure; \
511 } while (0)
513 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
514 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
515 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
516 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
517 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
518 do { \
519 ret = tlv_put_string(sctx, attrtype, str, len); \
520 if (ret < 0) \
521 goto tlv_put_failure; \
522 } while (0)
523 #define TLV_PUT_PATH(sctx, attrtype, p) \
524 do { \
525 ret = tlv_put_string(sctx, attrtype, p->start, \
526 p->end - p->start); \
527 if (ret < 0) \
528 goto tlv_put_failure; \
529 } while(0)
530 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
531 do { \
532 ret = tlv_put_uuid(sctx, attrtype, uuid); \
533 if (ret < 0) \
534 goto tlv_put_failure; \
535 } while (0)
536 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
537 do { \
538 ret = tlv_put_timespec(sctx, attrtype, ts); \
539 if (ret < 0) \
540 goto tlv_put_failure; \
541 } while (0)
542 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
543 do { \
544 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
545 if (ret < 0) \
546 goto tlv_put_failure; \
547 } while (0)
549 static int send_header(struct send_ctx *sctx)
551 struct btrfs_stream_header hdr;
553 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
554 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
556 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
557 &sctx->send_off);
561 * For each command/item we want to send to userspace, we call this function.
563 static int begin_cmd(struct send_ctx *sctx, int cmd)
565 struct btrfs_cmd_header *hdr;
567 if (!sctx->send_buf) {
568 WARN_ON(1);
569 return -EINVAL;
572 BUG_ON(sctx->send_size);
574 sctx->send_size += sizeof(*hdr);
575 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
576 hdr->cmd = cpu_to_le16(cmd);
578 return 0;
581 static int send_cmd(struct send_ctx *sctx)
583 int ret;
584 struct btrfs_cmd_header *hdr;
585 u32 crc;
587 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
588 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
589 hdr->crc = 0;
591 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
592 hdr->crc = cpu_to_le32(crc);
594 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
595 &sctx->send_off);
597 sctx->total_send_size += sctx->send_size;
598 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
599 sctx->send_size = 0;
601 return ret;
605 * Sends a move instruction to user space
607 static int send_rename(struct send_ctx *sctx,
608 struct fs_path *from, struct fs_path *to)
610 int ret;
612 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
614 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
615 if (ret < 0)
616 goto out;
618 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
619 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
621 ret = send_cmd(sctx);
623 tlv_put_failure:
624 out:
625 return ret;
629 * Sends a link instruction to user space
631 static int send_link(struct send_ctx *sctx,
632 struct fs_path *path, struct fs_path *lnk)
634 int ret;
636 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
638 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
639 if (ret < 0)
640 goto out;
642 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
643 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
645 ret = send_cmd(sctx);
647 tlv_put_failure:
648 out:
649 return ret;
653 * Sends an unlink instruction to user space
655 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
657 int ret;
659 verbose_printk("btrfs: send_unlink %s\n", path->start);
661 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
662 if (ret < 0)
663 goto out;
665 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
667 ret = send_cmd(sctx);
669 tlv_put_failure:
670 out:
671 return ret;
675 * Sends a rmdir instruction to user space
677 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
679 int ret;
681 verbose_printk("btrfs: send_rmdir %s\n", path->start);
683 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
684 if (ret < 0)
685 goto out;
687 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
689 ret = send_cmd(sctx);
691 tlv_put_failure:
692 out:
693 return ret;
697 * Helper function to retrieve some fields from an inode item.
699 static int get_inode_info(struct btrfs_root *root,
700 u64 ino, u64 *size, u64 *gen,
701 u64 *mode, u64 *uid, u64 *gid,
702 u64 *rdev)
704 int ret;
705 struct btrfs_inode_item *ii;
706 struct btrfs_key key;
707 struct btrfs_path *path;
709 path = alloc_path_for_send();
710 if (!path)
711 return -ENOMEM;
713 key.objectid = ino;
714 key.type = BTRFS_INODE_ITEM_KEY;
715 key.offset = 0;
716 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
717 if (ret < 0)
718 goto out;
719 if (ret) {
720 ret = -ENOENT;
721 goto out;
724 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
725 struct btrfs_inode_item);
726 if (size)
727 *size = btrfs_inode_size(path->nodes[0], ii);
728 if (gen)
729 *gen = btrfs_inode_generation(path->nodes[0], ii);
730 if (mode)
731 *mode = btrfs_inode_mode(path->nodes[0], ii);
732 if (uid)
733 *uid = btrfs_inode_uid(path->nodes[0], ii);
734 if (gid)
735 *gid = btrfs_inode_gid(path->nodes[0], ii);
736 if (rdev)
737 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
739 out:
740 btrfs_free_path(path);
741 return ret;
744 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
745 struct fs_path *p,
746 void *ctx);
749 * Helper function to iterate the entries in ONE btrfs_inode_ref or
750 * btrfs_inode_extref.
751 * The iterate callback may return a non zero value to stop iteration. This can
752 * be a negative value for error codes or 1 to simply stop it.
754 * path must point to the INODE_REF or INODE_EXTREF when called.
756 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
757 struct btrfs_key *found_key, int resolve,
758 iterate_inode_ref_t iterate, void *ctx)
760 struct extent_buffer *eb = path->nodes[0];
761 struct btrfs_item *item;
762 struct btrfs_inode_ref *iref;
763 struct btrfs_inode_extref *extref;
764 struct btrfs_path *tmp_path;
765 struct fs_path *p;
766 u32 cur = 0;
767 u32 total;
768 int slot = path->slots[0];
769 u32 name_len;
770 char *start;
771 int ret = 0;
772 int num = 0;
773 int index;
774 u64 dir;
775 unsigned long name_off;
776 unsigned long elem_size;
777 unsigned long ptr;
779 p = fs_path_alloc_reversed();
780 if (!p)
781 return -ENOMEM;
783 tmp_path = alloc_path_for_send();
784 if (!tmp_path) {
785 fs_path_free(p);
786 return -ENOMEM;
790 if (found_key->type == BTRFS_INODE_REF_KEY) {
791 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
792 struct btrfs_inode_ref);
793 item = btrfs_item_nr(eb, slot);
794 total = btrfs_item_size(eb, item);
795 elem_size = sizeof(*iref);
796 } else {
797 ptr = btrfs_item_ptr_offset(eb, slot);
798 total = btrfs_item_size_nr(eb, slot);
799 elem_size = sizeof(*extref);
802 while (cur < total) {
803 fs_path_reset(p);
805 if (found_key->type == BTRFS_INODE_REF_KEY) {
806 iref = (struct btrfs_inode_ref *)(ptr + cur);
807 name_len = btrfs_inode_ref_name_len(eb, iref);
808 name_off = (unsigned long)(iref + 1);
809 index = btrfs_inode_ref_index(eb, iref);
810 dir = found_key->offset;
811 } else {
812 extref = (struct btrfs_inode_extref *)(ptr + cur);
813 name_len = btrfs_inode_extref_name_len(eb, extref);
814 name_off = (unsigned long)&extref->name;
815 index = btrfs_inode_extref_index(eb, extref);
816 dir = btrfs_inode_extref_parent(eb, extref);
819 if (resolve) {
820 start = btrfs_ref_to_path(root, tmp_path, name_len,
821 name_off, eb, dir,
822 p->buf, p->buf_len);
823 if (IS_ERR(start)) {
824 ret = PTR_ERR(start);
825 goto out;
827 if (start < p->buf) {
828 /* overflow , try again with larger buffer */
829 ret = fs_path_ensure_buf(p,
830 p->buf_len + p->buf - start);
831 if (ret < 0)
832 goto out;
833 start = btrfs_ref_to_path(root, tmp_path,
834 name_len, name_off,
835 eb, dir,
836 p->buf, p->buf_len);
837 if (IS_ERR(start)) {
838 ret = PTR_ERR(start);
839 goto out;
841 BUG_ON(start < p->buf);
843 p->start = start;
844 } else {
845 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
846 name_len);
847 if (ret < 0)
848 goto out;
851 cur += elem_size + name_len;
852 ret = iterate(num, dir, index, p, ctx);
853 if (ret)
854 goto out;
855 num++;
858 out:
859 btrfs_free_path(tmp_path);
860 fs_path_free(p);
861 return ret;
864 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
865 const char *name, int name_len,
866 const char *data, int data_len,
867 u8 type, void *ctx);
870 * Helper function to iterate the entries in ONE btrfs_dir_item.
871 * The iterate callback may return a non zero value to stop iteration. This can
872 * be a negative value for error codes or 1 to simply stop it.
874 * path must point to the dir item when called.
876 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
877 struct btrfs_key *found_key,
878 iterate_dir_item_t iterate, void *ctx)
880 int ret = 0;
881 struct extent_buffer *eb;
882 struct btrfs_item *item;
883 struct btrfs_dir_item *di;
884 struct btrfs_key di_key;
885 char *buf = NULL;
886 char *buf2 = NULL;
887 int buf_len;
888 int buf_virtual = 0;
889 u32 name_len;
890 u32 data_len;
891 u32 cur;
892 u32 len;
893 u32 total;
894 int slot;
895 int num;
896 u8 type;
898 buf_len = PAGE_SIZE;
899 buf = kmalloc(buf_len, GFP_NOFS);
900 if (!buf) {
901 ret = -ENOMEM;
902 goto out;
905 eb = path->nodes[0];
906 slot = path->slots[0];
907 item = btrfs_item_nr(eb, slot);
908 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
909 cur = 0;
910 len = 0;
911 total = btrfs_item_size(eb, item);
913 num = 0;
914 while (cur < total) {
915 name_len = btrfs_dir_name_len(eb, di);
916 data_len = btrfs_dir_data_len(eb, di);
917 type = btrfs_dir_type(eb, di);
918 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
920 if (name_len + data_len > buf_len) {
921 buf_len = PAGE_ALIGN(name_len + data_len);
922 if (buf_virtual) {
923 buf2 = vmalloc(buf_len);
924 if (!buf2) {
925 ret = -ENOMEM;
926 goto out;
928 vfree(buf);
929 } else {
930 buf2 = krealloc(buf, buf_len, GFP_NOFS);
931 if (!buf2) {
932 buf2 = vmalloc(buf_len);
933 if (!buf2) {
934 ret = -ENOMEM;
935 goto out;
937 kfree(buf);
938 buf_virtual = 1;
942 buf = buf2;
943 buf2 = NULL;
946 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
947 name_len + data_len);
949 len = sizeof(*di) + name_len + data_len;
950 di = (struct btrfs_dir_item *)((char *)di + len);
951 cur += len;
953 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
954 data_len, type, ctx);
955 if (ret < 0)
956 goto out;
957 if (ret) {
958 ret = 0;
959 goto out;
962 num++;
965 out:
966 if (buf_virtual)
967 vfree(buf);
968 else
969 kfree(buf);
970 return ret;
973 static int __copy_first_ref(int num, u64 dir, int index,
974 struct fs_path *p, void *ctx)
976 int ret;
977 struct fs_path *pt = ctx;
979 ret = fs_path_copy(pt, p);
980 if (ret < 0)
981 return ret;
983 /* we want the first only */
984 return 1;
988 * Retrieve the first path of an inode. If an inode has more then one
989 * ref/hardlink, this is ignored.
991 static int get_inode_path(struct btrfs_root *root,
992 u64 ino, struct fs_path *path)
994 int ret;
995 struct btrfs_key key, found_key;
996 struct btrfs_path *p;
998 p = alloc_path_for_send();
999 if (!p)
1000 return -ENOMEM;
1002 fs_path_reset(path);
1004 key.objectid = ino;
1005 key.type = BTRFS_INODE_REF_KEY;
1006 key.offset = 0;
1008 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1009 if (ret < 0)
1010 goto out;
1011 if (ret) {
1012 ret = 1;
1013 goto out;
1015 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1016 if (found_key.objectid != ino ||
1017 (found_key.type != BTRFS_INODE_REF_KEY &&
1018 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1019 ret = -ENOENT;
1020 goto out;
1023 ret = iterate_inode_ref(root, p, &found_key, 1,
1024 __copy_first_ref, path);
1025 if (ret < 0)
1026 goto out;
1027 ret = 0;
1029 out:
1030 btrfs_free_path(p);
1031 return ret;
1034 struct backref_ctx {
1035 struct send_ctx *sctx;
1037 /* number of total found references */
1038 u64 found;
1041 * used for clones found in send_root. clones found behind cur_objectid
1042 * and cur_offset are not considered as allowed clones.
1044 u64 cur_objectid;
1045 u64 cur_offset;
1047 /* may be truncated in case it's the last extent in a file */
1048 u64 extent_len;
1050 /* Just to check for bugs in backref resolving */
1051 int found_itself;
1054 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1056 u64 root = (u64)(uintptr_t)key;
1057 struct clone_root *cr = (struct clone_root *)elt;
1059 if (root < cr->root->objectid)
1060 return -1;
1061 if (root > cr->root->objectid)
1062 return 1;
1063 return 0;
1066 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1068 struct clone_root *cr1 = (struct clone_root *)e1;
1069 struct clone_root *cr2 = (struct clone_root *)e2;
1071 if (cr1->root->objectid < cr2->root->objectid)
1072 return -1;
1073 if (cr1->root->objectid > cr2->root->objectid)
1074 return 1;
1075 return 0;
1079 * Called for every backref that is found for the current extent.
1080 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1082 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1084 struct backref_ctx *bctx = ctx_;
1085 struct clone_root *found;
1086 int ret;
1087 u64 i_size;
1089 /* First check if the root is in the list of accepted clone sources */
1090 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1091 bctx->sctx->clone_roots_cnt,
1092 sizeof(struct clone_root),
1093 __clone_root_cmp_bsearch);
1094 if (!found)
1095 return 0;
1097 if (found->root == bctx->sctx->send_root &&
1098 ino == bctx->cur_objectid &&
1099 offset == bctx->cur_offset) {
1100 bctx->found_itself = 1;
1104 * There are inodes that have extents that lie behind its i_size. Don't
1105 * accept clones from these extents.
1107 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1108 NULL);
1109 if (ret < 0)
1110 return ret;
1112 if (offset + bctx->extent_len > i_size)
1113 return 0;
1116 * Make sure we don't consider clones from send_root that are
1117 * behind the current inode/offset.
1119 if (found->root == bctx->sctx->send_root) {
1121 * TODO for the moment we don't accept clones from the inode
1122 * that is currently send. We may change this when
1123 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1124 * file.
1126 if (ino >= bctx->cur_objectid)
1127 return 0;
1128 #if 0
1129 if (ino > bctx->cur_objectid)
1130 return 0;
1131 if (offset + bctx->extent_len > bctx->cur_offset)
1132 return 0;
1133 #endif
1136 bctx->found++;
1137 found->found_refs++;
1138 if (ino < found->ino) {
1139 found->ino = ino;
1140 found->offset = offset;
1141 } else if (found->ino == ino) {
1143 * same extent found more then once in the same file.
1145 if (found->offset > offset + bctx->extent_len)
1146 found->offset = offset;
1149 return 0;
1153 * Given an inode, offset and extent item, it finds a good clone for a clone
1154 * instruction. Returns -ENOENT when none could be found. The function makes
1155 * sure that the returned clone is usable at the point where sending is at the
1156 * moment. This means, that no clones are accepted which lie behind the current
1157 * inode+offset.
1159 * path must point to the extent item when called.
1161 static int find_extent_clone(struct send_ctx *sctx,
1162 struct btrfs_path *path,
1163 u64 ino, u64 data_offset,
1164 u64 ino_size,
1165 struct clone_root **found)
1167 int ret;
1168 int extent_type;
1169 u64 logical;
1170 u64 disk_byte;
1171 u64 num_bytes;
1172 u64 extent_item_pos;
1173 u64 flags = 0;
1174 struct btrfs_file_extent_item *fi;
1175 struct extent_buffer *eb = path->nodes[0];
1176 struct backref_ctx *backref_ctx = NULL;
1177 struct clone_root *cur_clone_root;
1178 struct btrfs_key found_key;
1179 struct btrfs_path *tmp_path;
1180 int compressed;
1181 u32 i;
1183 tmp_path = alloc_path_for_send();
1184 if (!tmp_path)
1185 return -ENOMEM;
1187 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1188 if (!backref_ctx) {
1189 ret = -ENOMEM;
1190 goto out;
1193 if (data_offset >= ino_size) {
1195 * There may be extents that lie behind the file's size.
1196 * I at least had this in combination with snapshotting while
1197 * writing large files.
1199 ret = 0;
1200 goto out;
1203 fi = btrfs_item_ptr(eb, path->slots[0],
1204 struct btrfs_file_extent_item);
1205 extent_type = btrfs_file_extent_type(eb, fi);
1206 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1207 ret = -ENOENT;
1208 goto out;
1210 compressed = btrfs_file_extent_compression(eb, fi);
1212 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1213 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1214 if (disk_byte == 0) {
1215 ret = -ENOENT;
1216 goto out;
1218 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1220 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1221 &found_key, &flags);
1222 btrfs_release_path(tmp_path);
1224 if (ret < 0)
1225 goto out;
1226 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1227 ret = -EIO;
1228 goto out;
1232 * Setup the clone roots.
1234 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1235 cur_clone_root = sctx->clone_roots + i;
1236 cur_clone_root->ino = (u64)-1;
1237 cur_clone_root->offset = 0;
1238 cur_clone_root->found_refs = 0;
1241 backref_ctx->sctx = sctx;
1242 backref_ctx->found = 0;
1243 backref_ctx->cur_objectid = ino;
1244 backref_ctx->cur_offset = data_offset;
1245 backref_ctx->found_itself = 0;
1246 backref_ctx->extent_len = num_bytes;
1249 * The last extent of a file may be too large due to page alignment.
1250 * We need to adjust extent_len in this case so that the checks in
1251 * __iterate_backrefs work.
1253 if (data_offset + num_bytes >= ino_size)
1254 backref_ctx->extent_len = ino_size - data_offset;
1257 * Now collect all backrefs.
1259 if (compressed == BTRFS_COMPRESS_NONE)
1260 extent_item_pos = logical - found_key.objectid;
1261 else
1262 extent_item_pos = 0;
1264 extent_item_pos = logical - found_key.objectid;
1265 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1266 found_key.objectid, extent_item_pos, 1,
1267 __iterate_backrefs, backref_ctx);
1269 if (ret < 0)
1270 goto out;
1272 if (!backref_ctx->found_itself) {
1273 /* found a bug in backref code? */
1274 ret = -EIO;
1275 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1276 "send_root. inode=%llu, offset=%llu, "
1277 "disk_byte=%llu found extent=%llu\n",
1278 ino, data_offset, disk_byte, found_key.objectid);
1279 goto out;
1282 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1283 "ino=%llu, "
1284 "num_bytes=%llu, logical=%llu\n",
1285 data_offset, ino, num_bytes, logical);
1287 if (!backref_ctx->found)
1288 verbose_printk("btrfs: no clones found\n");
1290 cur_clone_root = NULL;
1291 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1292 if (sctx->clone_roots[i].found_refs) {
1293 if (!cur_clone_root)
1294 cur_clone_root = sctx->clone_roots + i;
1295 else if (sctx->clone_roots[i].root == sctx->send_root)
1296 /* prefer clones from send_root over others */
1297 cur_clone_root = sctx->clone_roots + i;
1302 if (cur_clone_root) {
1303 *found = cur_clone_root;
1304 ret = 0;
1305 } else {
1306 ret = -ENOENT;
1309 out:
1310 btrfs_free_path(tmp_path);
1311 kfree(backref_ctx);
1312 return ret;
1315 static int read_symlink(struct btrfs_root *root,
1316 u64 ino,
1317 struct fs_path *dest)
1319 int ret;
1320 struct btrfs_path *path;
1321 struct btrfs_key key;
1322 struct btrfs_file_extent_item *ei;
1323 u8 type;
1324 u8 compression;
1325 unsigned long off;
1326 int len;
1328 path = alloc_path_for_send();
1329 if (!path)
1330 return -ENOMEM;
1332 key.objectid = ino;
1333 key.type = BTRFS_EXTENT_DATA_KEY;
1334 key.offset = 0;
1335 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1336 if (ret < 0)
1337 goto out;
1338 BUG_ON(ret);
1340 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1341 struct btrfs_file_extent_item);
1342 type = btrfs_file_extent_type(path->nodes[0], ei);
1343 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1344 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1345 BUG_ON(compression);
1347 off = btrfs_file_extent_inline_start(ei);
1348 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1350 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1352 out:
1353 btrfs_free_path(path);
1354 return ret;
1358 * Helper function to generate a file name that is unique in the root of
1359 * send_root and parent_root. This is used to generate names for orphan inodes.
1361 static int gen_unique_name(struct send_ctx *sctx,
1362 u64 ino, u64 gen,
1363 struct fs_path *dest)
1365 int ret = 0;
1366 struct btrfs_path *path;
1367 struct btrfs_dir_item *di;
1368 char tmp[64];
1369 int len;
1370 u64 idx = 0;
1372 path = alloc_path_for_send();
1373 if (!path)
1374 return -ENOMEM;
1376 while (1) {
1377 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1378 ino, gen, idx);
1379 if (len >= sizeof(tmp)) {
1380 /* should really not happen */
1381 ret = -EOVERFLOW;
1382 goto out;
1385 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1386 path, BTRFS_FIRST_FREE_OBJECTID,
1387 tmp, strlen(tmp), 0);
1388 btrfs_release_path(path);
1389 if (IS_ERR(di)) {
1390 ret = PTR_ERR(di);
1391 goto out;
1393 if (di) {
1394 /* not unique, try again */
1395 idx++;
1396 continue;
1399 if (!sctx->parent_root) {
1400 /* unique */
1401 ret = 0;
1402 break;
1405 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1406 path, BTRFS_FIRST_FREE_OBJECTID,
1407 tmp, strlen(tmp), 0);
1408 btrfs_release_path(path);
1409 if (IS_ERR(di)) {
1410 ret = PTR_ERR(di);
1411 goto out;
1413 if (di) {
1414 /* not unique, try again */
1415 idx++;
1416 continue;
1418 /* unique */
1419 break;
1422 ret = fs_path_add(dest, tmp, strlen(tmp));
1424 out:
1425 btrfs_free_path(path);
1426 return ret;
1429 enum inode_state {
1430 inode_state_no_change,
1431 inode_state_will_create,
1432 inode_state_did_create,
1433 inode_state_will_delete,
1434 inode_state_did_delete,
1437 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1439 int ret;
1440 int left_ret;
1441 int right_ret;
1442 u64 left_gen;
1443 u64 right_gen;
1445 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1446 NULL, NULL);
1447 if (ret < 0 && ret != -ENOENT)
1448 goto out;
1449 left_ret = ret;
1451 if (!sctx->parent_root) {
1452 right_ret = -ENOENT;
1453 } else {
1454 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1455 NULL, NULL, NULL, NULL);
1456 if (ret < 0 && ret != -ENOENT)
1457 goto out;
1458 right_ret = ret;
1461 if (!left_ret && !right_ret) {
1462 if (left_gen == gen && right_gen == gen) {
1463 ret = inode_state_no_change;
1464 } else if (left_gen == gen) {
1465 if (ino < sctx->send_progress)
1466 ret = inode_state_did_create;
1467 else
1468 ret = inode_state_will_create;
1469 } else if (right_gen == gen) {
1470 if (ino < sctx->send_progress)
1471 ret = inode_state_did_delete;
1472 else
1473 ret = inode_state_will_delete;
1474 } else {
1475 ret = -ENOENT;
1477 } else if (!left_ret) {
1478 if (left_gen == gen) {
1479 if (ino < sctx->send_progress)
1480 ret = inode_state_did_create;
1481 else
1482 ret = inode_state_will_create;
1483 } else {
1484 ret = -ENOENT;
1486 } else if (!right_ret) {
1487 if (right_gen == gen) {
1488 if (ino < sctx->send_progress)
1489 ret = inode_state_did_delete;
1490 else
1491 ret = inode_state_will_delete;
1492 } else {
1493 ret = -ENOENT;
1495 } else {
1496 ret = -ENOENT;
1499 out:
1500 return ret;
1503 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1505 int ret;
1507 ret = get_cur_inode_state(sctx, ino, gen);
1508 if (ret < 0)
1509 goto out;
1511 if (ret == inode_state_no_change ||
1512 ret == inode_state_did_create ||
1513 ret == inode_state_will_delete)
1514 ret = 1;
1515 else
1516 ret = 0;
1518 out:
1519 return ret;
1523 * Helper function to lookup a dir item in a dir.
1525 static int lookup_dir_item_inode(struct btrfs_root *root,
1526 u64 dir, const char *name, int name_len,
1527 u64 *found_inode,
1528 u8 *found_type)
1530 int ret = 0;
1531 struct btrfs_dir_item *di;
1532 struct btrfs_key key;
1533 struct btrfs_path *path;
1535 path = alloc_path_for_send();
1536 if (!path)
1537 return -ENOMEM;
1539 di = btrfs_lookup_dir_item(NULL, root, path,
1540 dir, name, name_len, 0);
1541 if (!di) {
1542 ret = -ENOENT;
1543 goto out;
1545 if (IS_ERR(di)) {
1546 ret = PTR_ERR(di);
1547 goto out;
1549 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1550 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1551 ret = -ENOENT;
1552 goto out;
1554 *found_inode = key.objectid;
1555 *found_type = btrfs_dir_type(path->nodes[0], di);
1557 out:
1558 btrfs_free_path(path);
1559 return ret;
1563 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1564 * generation of the parent dir and the name of the dir entry.
1566 static int get_first_ref(struct btrfs_root *root, u64 ino,
1567 u64 *dir, u64 *dir_gen, struct fs_path *name)
1569 int ret;
1570 struct btrfs_key key;
1571 struct btrfs_key found_key;
1572 struct btrfs_path *path;
1573 int len;
1574 u64 parent_dir;
1576 path = alloc_path_for_send();
1577 if (!path)
1578 return -ENOMEM;
1580 key.objectid = ino;
1581 key.type = BTRFS_INODE_REF_KEY;
1582 key.offset = 0;
1584 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1585 if (ret < 0)
1586 goto out;
1587 if (!ret)
1588 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1589 path->slots[0]);
1590 if (ret || found_key.objectid != ino ||
1591 (found_key.type != BTRFS_INODE_REF_KEY &&
1592 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1593 ret = -ENOENT;
1594 goto out;
1597 if (key.type == BTRFS_INODE_REF_KEY) {
1598 struct btrfs_inode_ref *iref;
1599 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1600 struct btrfs_inode_ref);
1601 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1602 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1603 (unsigned long)(iref + 1),
1604 len);
1605 parent_dir = found_key.offset;
1606 } else {
1607 struct btrfs_inode_extref *extref;
1608 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1609 struct btrfs_inode_extref);
1610 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1611 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1612 (unsigned long)&extref->name, len);
1613 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1615 if (ret < 0)
1616 goto out;
1617 btrfs_release_path(path);
1619 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
1620 NULL, NULL);
1621 if (ret < 0)
1622 goto out;
1624 *dir = parent_dir;
1626 out:
1627 btrfs_free_path(path);
1628 return ret;
1631 static int is_first_ref(struct btrfs_root *root,
1632 u64 ino, u64 dir,
1633 const char *name, int name_len)
1635 int ret;
1636 struct fs_path *tmp_name;
1637 u64 tmp_dir;
1638 u64 tmp_dir_gen;
1640 tmp_name = fs_path_alloc();
1641 if (!tmp_name)
1642 return -ENOMEM;
1644 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1645 if (ret < 0)
1646 goto out;
1648 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1649 ret = 0;
1650 goto out;
1653 ret = !memcmp(tmp_name->start, name, name_len);
1655 out:
1656 fs_path_free(tmp_name);
1657 return ret;
1661 * Used by process_recorded_refs to determine if a new ref would overwrite an
1662 * already existing ref. In case it detects an overwrite, it returns the
1663 * inode/gen in who_ino/who_gen.
1664 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1665 * to make sure later references to the overwritten inode are possible.
1666 * Orphanizing is however only required for the first ref of an inode.
1667 * process_recorded_refs does an additional is_first_ref check to see if
1668 * orphanizing is really required.
1670 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1671 const char *name, int name_len,
1672 u64 *who_ino, u64 *who_gen)
1674 int ret = 0;
1675 u64 gen;
1676 u64 other_inode = 0;
1677 u8 other_type = 0;
1679 if (!sctx->parent_root)
1680 goto out;
1682 ret = is_inode_existent(sctx, dir, dir_gen);
1683 if (ret <= 0)
1684 goto out;
1687 * If we have a parent root we need to verify that the parent dir was
1688 * not delted and then re-created, if it was then we have no overwrite
1689 * and we can just unlink this entry.
1691 if (sctx->parent_root) {
1692 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1693 NULL, NULL, NULL);
1694 if (ret < 0 && ret != -ENOENT)
1695 goto out;
1696 if (ret) {
1697 ret = 0;
1698 goto out;
1700 if (gen != dir_gen)
1701 goto out;
1704 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1705 &other_inode, &other_type);
1706 if (ret < 0 && ret != -ENOENT)
1707 goto out;
1708 if (ret) {
1709 ret = 0;
1710 goto out;
1714 * Check if the overwritten ref was already processed. If yes, the ref
1715 * was already unlinked/moved, so we can safely assume that we will not
1716 * overwrite anything at this point in time.
1718 if (other_inode > sctx->send_progress) {
1719 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1720 who_gen, NULL, NULL, NULL, NULL);
1721 if (ret < 0)
1722 goto out;
1724 ret = 1;
1725 *who_ino = other_inode;
1726 } else {
1727 ret = 0;
1730 out:
1731 return ret;
1735 * Checks if the ref was overwritten by an already processed inode. This is
1736 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1737 * thus the orphan name needs be used.
1738 * process_recorded_refs also uses it to avoid unlinking of refs that were
1739 * overwritten.
1741 static int did_overwrite_ref(struct send_ctx *sctx,
1742 u64 dir, u64 dir_gen,
1743 u64 ino, u64 ino_gen,
1744 const char *name, int name_len)
1746 int ret = 0;
1747 u64 gen;
1748 u64 ow_inode;
1749 u8 other_type;
1751 if (!sctx->parent_root)
1752 goto out;
1754 ret = is_inode_existent(sctx, dir, dir_gen);
1755 if (ret <= 0)
1756 goto out;
1758 /* check if the ref was overwritten by another ref */
1759 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1760 &ow_inode, &other_type);
1761 if (ret < 0 && ret != -ENOENT)
1762 goto out;
1763 if (ret) {
1764 /* was never and will never be overwritten */
1765 ret = 0;
1766 goto out;
1769 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1770 NULL, NULL);
1771 if (ret < 0)
1772 goto out;
1774 if (ow_inode == ino && gen == ino_gen) {
1775 ret = 0;
1776 goto out;
1779 /* we know that it is or will be overwritten. check this now */
1780 if (ow_inode < sctx->send_progress)
1781 ret = 1;
1782 else
1783 ret = 0;
1785 out:
1786 return ret;
1790 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1791 * that got overwritten. This is used by process_recorded_refs to determine
1792 * if it has to use the path as returned by get_cur_path or the orphan name.
1794 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1796 int ret = 0;
1797 struct fs_path *name = NULL;
1798 u64 dir;
1799 u64 dir_gen;
1801 if (!sctx->parent_root)
1802 goto out;
1804 name = fs_path_alloc();
1805 if (!name)
1806 return -ENOMEM;
1808 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1809 if (ret < 0)
1810 goto out;
1812 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1813 name->start, fs_path_len(name));
1815 out:
1816 fs_path_free(name);
1817 return ret;
1821 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1822 * so we need to do some special handling in case we have clashes. This function
1823 * takes care of this with the help of name_cache_entry::radix_list.
1824 * In case of error, nce is kfreed.
1826 static int name_cache_insert(struct send_ctx *sctx,
1827 struct name_cache_entry *nce)
1829 int ret = 0;
1830 struct list_head *nce_head;
1832 nce_head = radix_tree_lookup(&sctx->name_cache,
1833 (unsigned long)nce->ino);
1834 if (!nce_head) {
1835 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1836 if (!nce_head) {
1837 kfree(nce);
1838 return -ENOMEM;
1840 INIT_LIST_HEAD(nce_head);
1842 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1843 if (ret < 0) {
1844 kfree(nce_head);
1845 kfree(nce);
1846 return ret;
1849 list_add_tail(&nce->radix_list, nce_head);
1850 list_add_tail(&nce->list, &sctx->name_cache_list);
1851 sctx->name_cache_size++;
1853 return ret;
1856 static void name_cache_delete(struct send_ctx *sctx,
1857 struct name_cache_entry *nce)
1859 struct list_head *nce_head;
1861 nce_head = radix_tree_lookup(&sctx->name_cache,
1862 (unsigned long)nce->ino);
1863 BUG_ON(!nce_head);
1865 list_del(&nce->radix_list);
1866 list_del(&nce->list);
1867 sctx->name_cache_size--;
1869 if (list_empty(nce_head)) {
1870 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1871 kfree(nce_head);
1875 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1876 u64 ino, u64 gen)
1878 struct list_head *nce_head;
1879 struct name_cache_entry *cur;
1881 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1882 if (!nce_head)
1883 return NULL;
1885 list_for_each_entry(cur, nce_head, radix_list) {
1886 if (cur->ino == ino && cur->gen == gen)
1887 return cur;
1889 return NULL;
1893 * Removes the entry from the list and adds it back to the end. This marks the
1894 * entry as recently used so that name_cache_clean_unused does not remove it.
1896 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1898 list_del(&nce->list);
1899 list_add_tail(&nce->list, &sctx->name_cache_list);
1903 * Remove some entries from the beginning of name_cache_list.
1905 static void name_cache_clean_unused(struct send_ctx *sctx)
1907 struct name_cache_entry *nce;
1909 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1910 return;
1912 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1913 nce = list_entry(sctx->name_cache_list.next,
1914 struct name_cache_entry, list);
1915 name_cache_delete(sctx, nce);
1916 kfree(nce);
1920 static void name_cache_free(struct send_ctx *sctx)
1922 struct name_cache_entry *nce;
1924 while (!list_empty(&sctx->name_cache_list)) {
1925 nce = list_entry(sctx->name_cache_list.next,
1926 struct name_cache_entry, list);
1927 name_cache_delete(sctx, nce);
1928 kfree(nce);
1933 * Used by get_cur_path for each ref up to the root.
1934 * Returns 0 if it succeeded.
1935 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1936 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1937 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1938 * Returns <0 in case of error.
1940 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1941 u64 ino, u64 gen,
1942 u64 *parent_ino,
1943 u64 *parent_gen,
1944 struct fs_path *dest)
1946 int ret;
1947 int nce_ret;
1948 struct btrfs_path *path = NULL;
1949 struct name_cache_entry *nce = NULL;
1952 * First check if we already did a call to this function with the same
1953 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1954 * return the cached result.
1956 nce = name_cache_search(sctx, ino, gen);
1957 if (nce) {
1958 if (ino < sctx->send_progress && nce->need_later_update) {
1959 name_cache_delete(sctx, nce);
1960 kfree(nce);
1961 nce = NULL;
1962 } else {
1963 name_cache_used(sctx, nce);
1964 *parent_ino = nce->parent_ino;
1965 *parent_gen = nce->parent_gen;
1966 ret = fs_path_add(dest, nce->name, nce->name_len);
1967 if (ret < 0)
1968 goto out;
1969 ret = nce->ret;
1970 goto out;
1974 path = alloc_path_for_send();
1975 if (!path)
1976 return -ENOMEM;
1979 * If the inode is not existent yet, add the orphan name and return 1.
1980 * This should only happen for the parent dir that we determine in
1981 * __record_new_ref
1983 ret = is_inode_existent(sctx, ino, gen);
1984 if (ret < 0)
1985 goto out;
1987 if (!ret) {
1988 ret = gen_unique_name(sctx, ino, gen, dest);
1989 if (ret < 0)
1990 goto out;
1991 ret = 1;
1992 goto out_cache;
1996 * Depending on whether the inode was already processed or not, use
1997 * send_root or parent_root for ref lookup.
1999 if (ino < sctx->send_progress)
2000 ret = get_first_ref(sctx->send_root, ino,
2001 parent_ino, parent_gen, dest);
2002 else
2003 ret = get_first_ref(sctx->parent_root, ino,
2004 parent_ino, parent_gen, dest);
2005 if (ret < 0)
2006 goto out;
2009 * Check if the ref was overwritten by an inode's ref that was processed
2010 * earlier. If yes, treat as orphan and return 1.
2012 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2013 dest->start, dest->end - dest->start);
2014 if (ret < 0)
2015 goto out;
2016 if (ret) {
2017 fs_path_reset(dest);
2018 ret = gen_unique_name(sctx, ino, gen, dest);
2019 if (ret < 0)
2020 goto out;
2021 ret = 1;
2024 out_cache:
2026 * Store the result of the lookup in the name cache.
2028 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2029 if (!nce) {
2030 ret = -ENOMEM;
2031 goto out;
2034 nce->ino = ino;
2035 nce->gen = gen;
2036 nce->parent_ino = *parent_ino;
2037 nce->parent_gen = *parent_gen;
2038 nce->name_len = fs_path_len(dest);
2039 nce->ret = ret;
2040 strcpy(nce->name, dest->start);
2042 if (ino < sctx->send_progress)
2043 nce->need_later_update = 0;
2044 else
2045 nce->need_later_update = 1;
2047 nce_ret = name_cache_insert(sctx, nce);
2048 if (nce_ret < 0)
2049 ret = nce_ret;
2050 name_cache_clean_unused(sctx);
2052 out:
2053 btrfs_free_path(path);
2054 return ret;
2058 * Magic happens here. This function returns the first ref to an inode as it
2059 * would look like while receiving the stream at this point in time.
2060 * We walk the path up to the root. For every inode in between, we check if it
2061 * was already processed/sent. If yes, we continue with the parent as found
2062 * in send_root. If not, we continue with the parent as found in parent_root.
2063 * If we encounter an inode that was deleted at this point in time, we use the
2064 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2065 * that were not created yet and overwritten inodes/refs.
2067 * When do we have have orphan inodes:
2068 * 1. When an inode is freshly created and thus no valid refs are available yet
2069 * 2. When a directory lost all it's refs (deleted) but still has dir items
2070 * inside which were not processed yet (pending for move/delete). If anyone
2071 * tried to get the path to the dir items, it would get a path inside that
2072 * orphan directory.
2073 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2074 * of an unprocessed inode. If in that case the first ref would be
2075 * overwritten, the overwritten inode gets "orphanized". Later when we
2076 * process this overwritten inode, it is restored at a new place by moving
2077 * the orphan inode.
2079 * sctx->send_progress tells this function at which point in time receiving
2080 * would be.
2082 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2083 struct fs_path *dest)
2085 int ret = 0;
2086 struct fs_path *name = NULL;
2087 u64 parent_inode = 0;
2088 u64 parent_gen = 0;
2089 int stop = 0;
2091 name = fs_path_alloc();
2092 if (!name) {
2093 ret = -ENOMEM;
2094 goto out;
2097 dest->reversed = 1;
2098 fs_path_reset(dest);
2100 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2101 fs_path_reset(name);
2103 ret = __get_cur_name_and_parent(sctx, ino, gen,
2104 &parent_inode, &parent_gen, name);
2105 if (ret < 0)
2106 goto out;
2107 if (ret)
2108 stop = 1;
2110 ret = fs_path_add_path(dest, name);
2111 if (ret < 0)
2112 goto out;
2114 ino = parent_inode;
2115 gen = parent_gen;
2118 out:
2119 fs_path_free(name);
2120 if (!ret)
2121 fs_path_unreverse(dest);
2122 return ret;
2126 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2128 static int send_subvol_begin(struct send_ctx *sctx)
2130 int ret;
2131 struct btrfs_root *send_root = sctx->send_root;
2132 struct btrfs_root *parent_root = sctx->parent_root;
2133 struct btrfs_path *path;
2134 struct btrfs_key key;
2135 struct btrfs_root_ref *ref;
2136 struct extent_buffer *leaf;
2137 char *name = NULL;
2138 int namelen;
2140 path = alloc_path_for_send();
2141 if (!path)
2142 return -ENOMEM;
2144 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2145 if (!name) {
2146 btrfs_free_path(path);
2147 return -ENOMEM;
2150 key.objectid = send_root->objectid;
2151 key.type = BTRFS_ROOT_BACKREF_KEY;
2152 key.offset = 0;
2154 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2155 &key, path, 1, 0);
2156 if (ret < 0)
2157 goto out;
2158 if (ret) {
2159 ret = -ENOENT;
2160 goto out;
2163 leaf = path->nodes[0];
2164 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2165 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2166 key.objectid != send_root->objectid) {
2167 ret = -ENOENT;
2168 goto out;
2170 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2171 namelen = btrfs_root_ref_name_len(leaf, ref);
2172 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2173 btrfs_release_path(path);
2175 if (parent_root) {
2176 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2177 if (ret < 0)
2178 goto out;
2179 } else {
2180 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2181 if (ret < 0)
2182 goto out;
2185 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2186 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2187 sctx->send_root->root_item.uuid);
2188 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2189 sctx->send_root->root_item.ctransid);
2190 if (parent_root) {
2191 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2192 sctx->parent_root->root_item.uuid);
2193 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2194 sctx->parent_root->root_item.ctransid);
2197 ret = send_cmd(sctx);
2199 tlv_put_failure:
2200 out:
2201 btrfs_free_path(path);
2202 kfree(name);
2203 return ret;
2206 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2208 int ret = 0;
2209 struct fs_path *p;
2211 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2213 p = fs_path_alloc();
2214 if (!p)
2215 return -ENOMEM;
2217 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2218 if (ret < 0)
2219 goto out;
2221 ret = get_cur_path(sctx, ino, gen, p);
2222 if (ret < 0)
2223 goto out;
2224 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2225 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2227 ret = send_cmd(sctx);
2229 tlv_put_failure:
2230 out:
2231 fs_path_free(p);
2232 return ret;
2235 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2237 int ret = 0;
2238 struct fs_path *p;
2240 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2242 p = fs_path_alloc();
2243 if (!p)
2244 return -ENOMEM;
2246 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2247 if (ret < 0)
2248 goto out;
2250 ret = get_cur_path(sctx, ino, gen, p);
2251 if (ret < 0)
2252 goto out;
2253 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2254 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2256 ret = send_cmd(sctx);
2258 tlv_put_failure:
2259 out:
2260 fs_path_free(p);
2261 return ret;
2264 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2266 int ret = 0;
2267 struct fs_path *p;
2269 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2271 p = fs_path_alloc();
2272 if (!p)
2273 return -ENOMEM;
2275 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2276 if (ret < 0)
2277 goto out;
2279 ret = get_cur_path(sctx, ino, gen, p);
2280 if (ret < 0)
2281 goto out;
2282 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2283 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2284 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2286 ret = send_cmd(sctx);
2288 tlv_put_failure:
2289 out:
2290 fs_path_free(p);
2291 return ret;
2294 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2296 int ret = 0;
2297 struct fs_path *p = NULL;
2298 struct btrfs_inode_item *ii;
2299 struct btrfs_path *path = NULL;
2300 struct extent_buffer *eb;
2301 struct btrfs_key key;
2302 int slot;
2304 verbose_printk("btrfs: send_utimes %llu\n", ino);
2306 p = fs_path_alloc();
2307 if (!p)
2308 return -ENOMEM;
2310 path = alloc_path_for_send();
2311 if (!path) {
2312 ret = -ENOMEM;
2313 goto out;
2316 key.objectid = ino;
2317 key.type = BTRFS_INODE_ITEM_KEY;
2318 key.offset = 0;
2319 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2320 if (ret < 0)
2321 goto out;
2323 eb = path->nodes[0];
2324 slot = path->slots[0];
2325 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2327 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2328 if (ret < 0)
2329 goto out;
2331 ret = get_cur_path(sctx, ino, gen, p);
2332 if (ret < 0)
2333 goto out;
2334 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2335 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2336 btrfs_inode_atime(ii));
2337 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2338 btrfs_inode_mtime(ii));
2339 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2340 btrfs_inode_ctime(ii));
2341 /* TODO Add otime support when the otime patches get into upstream */
2343 ret = send_cmd(sctx);
2345 tlv_put_failure:
2346 out:
2347 fs_path_free(p);
2348 btrfs_free_path(path);
2349 return ret;
2353 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2354 * a valid path yet because we did not process the refs yet. So, the inode
2355 * is created as orphan.
2357 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2359 int ret = 0;
2360 struct fs_path *p;
2361 int cmd;
2362 u64 gen;
2363 u64 mode;
2364 u64 rdev;
2366 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2368 p = fs_path_alloc();
2369 if (!p)
2370 return -ENOMEM;
2372 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2373 NULL, &rdev);
2374 if (ret < 0)
2375 goto out;
2377 if (S_ISREG(mode)) {
2378 cmd = BTRFS_SEND_C_MKFILE;
2379 } else if (S_ISDIR(mode)) {
2380 cmd = BTRFS_SEND_C_MKDIR;
2381 } else if (S_ISLNK(mode)) {
2382 cmd = BTRFS_SEND_C_SYMLINK;
2383 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2384 cmd = BTRFS_SEND_C_MKNOD;
2385 } else if (S_ISFIFO(mode)) {
2386 cmd = BTRFS_SEND_C_MKFIFO;
2387 } else if (S_ISSOCK(mode)) {
2388 cmd = BTRFS_SEND_C_MKSOCK;
2389 } else {
2390 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2391 (int)(mode & S_IFMT));
2392 ret = -ENOTSUPP;
2393 goto out;
2396 ret = begin_cmd(sctx, cmd);
2397 if (ret < 0)
2398 goto out;
2400 ret = gen_unique_name(sctx, ino, gen, p);
2401 if (ret < 0)
2402 goto out;
2404 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2405 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2407 if (S_ISLNK(mode)) {
2408 fs_path_reset(p);
2409 ret = read_symlink(sctx->send_root, ino, p);
2410 if (ret < 0)
2411 goto out;
2412 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2413 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2414 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2415 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2416 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2419 ret = send_cmd(sctx);
2420 if (ret < 0)
2421 goto out;
2424 tlv_put_failure:
2425 out:
2426 fs_path_free(p);
2427 return ret;
2431 * We need some special handling for inodes that get processed before the parent
2432 * directory got created. See process_recorded_refs for details.
2433 * This function does the check if we already created the dir out of order.
2435 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2437 int ret = 0;
2438 struct btrfs_path *path = NULL;
2439 struct btrfs_key key;
2440 struct btrfs_key found_key;
2441 struct btrfs_key di_key;
2442 struct extent_buffer *eb;
2443 struct btrfs_dir_item *di;
2444 int slot;
2446 path = alloc_path_for_send();
2447 if (!path) {
2448 ret = -ENOMEM;
2449 goto out;
2452 key.objectid = dir;
2453 key.type = BTRFS_DIR_INDEX_KEY;
2454 key.offset = 0;
2455 while (1) {
2456 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2457 1, 0);
2458 if (ret < 0)
2459 goto out;
2460 if (!ret) {
2461 eb = path->nodes[0];
2462 slot = path->slots[0];
2463 btrfs_item_key_to_cpu(eb, &found_key, slot);
2465 if (ret || found_key.objectid != key.objectid ||
2466 found_key.type != key.type) {
2467 ret = 0;
2468 goto out;
2471 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2472 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2474 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2475 di_key.objectid < sctx->send_progress) {
2476 ret = 1;
2477 goto out;
2480 key.offset = found_key.offset + 1;
2481 btrfs_release_path(path);
2484 out:
2485 btrfs_free_path(path);
2486 return ret;
2490 * Only creates the inode if it is:
2491 * 1. Not a directory
2492 * 2. Or a directory which was not created already due to out of order
2493 * directories. See did_create_dir and process_recorded_refs for details.
2495 static int send_create_inode_if_needed(struct send_ctx *sctx)
2497 int ret;
2499 if (S_ISDIR(sctx->cur_inode_mode)) {
2500 ret = did_create_dir(sctx, sctx->cur_ino);
2501 if (ret < 0)
2502 goto out;
2503 if (ret) {
2504 ret = 0;
2505 goto out;
2509 ret = send_create_inode(sctx, sctx->cur_ino);
2510 if (ret < 0)
2511 goto out;
2513 out:
2514 return ret;
2517 struct recorded_ref {
2518 struct list_head list;
2519 char *dir_path;
2520 char *name;
2521 struct fs_path *full_path;
2522 u64 dir;
2523 u64 dir_gen;
2524 int dir_path_len;
2525 int name_len;
2529 * We need to process new refs before deleted refs, but compare_tree gives us
2530 * everything mixed. So we first record all refs and later process them.
2531 * This function is a helper to record one ref.
2533 static int record_ref(struct list_head *head, u64 dir,
2534 u64 dir_gen, struct fs_path *path)
2536 struct recorded_ref *ref;
2538 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2539 if (!ref)
2540 return -ENOMEM;
2542 ref->dir = dir;
2543 ref->dir_gen = dir_gen;
2544 ref->full_path = path;
2546 ref->name = (char *)kbasename(ref->full_path->start);
2547 ref->name_len = ref->full_path->end - ref->name;
2548 ref->dir_path = ref->full_path->start;
2549 if (ref->name == ref->full_path->start)
2550 ref->dir_path_len = 0;
2551 else
2552 ref->dir_path_len = ref->full_path->end -
2553 ref->full_path->start - 1 - ref->name_len;
2555 list_add_tail(&ref->list, head);
2556 return 0;
2559 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2561 struct recorded_ref *new;
2563 new = kmalloc(sizeof(*ref), GFP_NOFS);
2564 if (!new)
2565 return -ENOMEM;
2567 new->dir = ref->dir;
2568 new->dir_gen = ref->dir_gen;
2569 new->full_path = NULL;
2570 INIT_LIST_HEAD(&new->list);
2571 list_add_tail(&new->list, list);
2572 return 0;
2575 static void __free_recorded_refs(struct list_head *head)
2577 struct recorded_ref *cur;
2579 while (!list_empty(head)) {
2580 cur = list_entry(head->next, struct recorded_ref, list);
2581 fs_path_free(cur->full_path);
2582 list_del(&cur->list);
2583 kfree(cur);
2587 static void free_recorded_refs(struct send_ctx *sctx)
2589 __free_recorded_refs(&sctx->new_refs);
2590 __free_recorded_refs(&sctx->deleted_refs);
2594 * Renames/moves a file/dir to its orphan name. Used when the first
2595 * ref of an unprocessed inode gets overwritten and for all non empty
2596 * directories.
2598 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2599 struct fs_path *path)
2601 int ret;
2602 struct fs_path *orphan;
2604 orphan = fs_path_alloc();
2605 if (!orphan)
2606 return -ENOMEM;
2608 ret = gen_unique_name(sctx, ino, gen, orphan);
2609 if (ret < 0)
2610 goto out;
2612 ret = send_rename(sctx, path, orphan);
2614 out:
2615 fs_path_free(orphan);
2616 return ret;
2620 * Returns 1 if a directory can be removed at this point in time.
2621 * We check this by iterating all dir items and checking if the inode behind
2622 * the dir item was already processed.
2624 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2626 int ret = 0;
2627 struct btrfs_root *root = sctx->parent_root;
2628 struct btrfs_path *path;
2629 struct btrfs_key key;
2630 struct btrfs_key found_key;
2631 struct btrfs_key loc;
2632 struct btrfs_dir_item *di;
2635 * Don't try to rmdir the top/root subvolume dir.
2637 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2638 return 0;
2640 path = alloc_path_for_send();
2641 if (!path)
2642 return -ENOMEM;
2644 key.objectid = dir;
2645 key.type = BTRFS_DIR_INDEX_KEY;
2646 key.offset = 0;
2648 while (1) {
2649 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2650 if (ret < 0)
2651 goto out;
2652 if (!ret) {
2653 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2654 path->slots[0]);
2656 if (ret || found_key.objectid != key.objectid ||
2657 found_key.type != key.type) {
2658 break;
2661 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2662 struct btrfs_dir_item);
2663 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2665 if (loc.objectid > send_progress) {
2666 ret = 0;
2667 goto out;
2670 btrfs_release_path(path);
2671 key.offset = found_key.offset + 1;
2674 ret = 1;
2676 out:
2677 btrfs_free_path(path);
2678 return ret;
2682 * This does all the move/link/unlink/rmdir magic.
2684 static int process_recorded_refs(struct send_ctx *sctx)
2686 int ret = 0;
2687 struct recorded_ref *cur;
2688 struct recorded_ref *cur2;
2689 struct list_head check_dirs;
2690 struct fs_path *valid_path = NULL;
2691 u64 ow_inode = 0;
2692 u64 ow_gen;
2693 int did_overwrite = 0;
2694 int is_orphan = 0;
2696 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2699 * This should never happen as the root dir always has the same ref
2700 * which is always '..'
2702 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
2703 INIT_LIST_HEAD(&check_dirs);
2705 valid_path = fs_path_alloc();
2706 if (!valid_path) {
2707 ret = -ENOMEM;
2708 goto out;
2712 * First, check if the first ref of the current inode was overwritten
2713 * before. If yes, we know that the current inode was already orphanized
2714 * and thus use the orphan name. If not, we can use get_cur_path to
2715 * get the path of the first ref as it would like while receiving at
2716 * this point in time.
2717 * New inodes are always orphan at the beginning, so force to use the
2718 * orphan name in this case.
2719 * The first ref is stored in valid_path and will be updated if it
2720 * gets moved around.
2722 if (!sctx->cur_inode_new) {
2723 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2724 sctx->cur_inode_gen);
2725 if (ret < 0)
2726 goto out;
2727 if (ret)
2728 did_overwrite = 1;
2730 if (sctx->cur_inode_new || did_overwrite) {
2731 ret = gen_unique_name(sctx, sctx->cur_ino,
2732 sctx->cur_inode_gen, valid_path);
2733 if (ret < 0)
2734 goto out;
2735 is_orphan = 1;
2736 } else {
2737 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2738 valid_path);
2739 if (ret < 0)
2740 goto out;
2743 list_for_each_entry(cur, &sctx->new_refs, list) {
2745 * We may have refs where the parent directory does not exist
2746 * yet. This happens if the parent directories inum is higher
2747 * the the current inum. To handle this case, we create the
2748 * parent directory out of order. But we need to check if this
2749 * did already happen before due to other refs in the same dir.
2751 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2752 if (ret < 0)
2753 goto out;
2754 if (ret == inode_state_will_create) {
2755 ret = 0;
2757 * First check if any of the current inodes refs did
2758 * already create the dir.
2760 list_for_each_entry(cur2, &sctx->new_refs, list) {
2761 if (cur == cur2)
2762 break;
2763 if (cur2->dir == cur->dir) {
2764 ret = 1;
2765 break;
2770 * If that did not happen, check if a previous inode
2771 * did already create the dir.
2773 if (!ret)
2774 ret = did_create_dir(sctx, cur->dir);
2775 if (ret < 0)
2776 goto out;
2777 if (!ret) {
2778 ret = send_create_inode(sctx, cur->dir);
2779 if (ret < 0)
2780 goto out;
2785 * Check if this new ref would overwrite the first ref of
2786 * another unprocessed inode. If yes, orphanize the
2787 * overwritten inode. If we find an overwritten ref that is
2788 * not the first ref, simply unlink it.
2790 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2791 cur->name, cur->name_len,
2792 &ow_inode, &ow_gen);
2793 if (ret < 0)
2794 goto out;
2795 if (ret) {
2796 ret = is_first_ref(sctx->parent_root,
2797 ow_inode, cur->dir, cur->name,
2798 cur->name_len);
2799 if (ret < 0)
2800 goto out;
2801 if (ret) {
2802 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2803 cur->full_path);
2804 if (ret < 0)
2805 goto out;
2806 } else {
2807 ret = send_unlink(sctx, cur->full_path);
2808 if (ret < 0)
2809 goto out;
2814 * link/move the ref to the new place. If we have an orphan
2815 * inode, move it and update valid_path. If not, link or move
2816 * it depending on the inode mode.
2818 if (is_orphan) {
2819 ret = send_rename(sctx, valid_path, cur->full_path);
2820 if (ret < 0)
2821 goto out;
2822 is_orphan = 0;
2823 ret = fs_path_copy(valid_path, cur->full_path);
2824 if (ret < 0)
2825 goto out;
2826 } else {
2827 if (S_ISDIR(sctx->cur_inode_mode)) {
2829 * Dirs can't be linked, so move it. For moved
2830 * dirs, we always have one new and one deleted
2831 * ref. The deleted ref is ignored later.
2833 ret = send_rename(sctx, valid_path,
2834 cur->full_path);
2835 if (ret < 0)
2836 goto out;
2837 ret = fs_path_copy(valid_path, cur->full_path);
2838 if (ret < 0)
2839 goto out;
2840 } else {
2841 ret = send_link(sctx, cur->full_path,
2842 valid_path);
2843 if (ret < 0)
2844 goto out;
2847 ret = dup_ref(cur, &check_dirs);
2848 if (ret < 0)
2849 goto out;
2852 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2854 * Check if we can already rmdir the directory. If not,
2855 * orphanize it. For every dir item inside that gets deleted
2856 * later, we do this check again and rmdir it then if possible.
2857 * See the use of check_dirs for more details.
2859 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2860 if (ret < 0)
2861 goto out;
2862 if (ret) {
2863 ret = send_rmdir(sctx, valid_path);
2864 if (ret < 0)
2865 goto out;
2866 } else if (!is_orphan) {
2867 ret = orphanize_inode(sctx, sctx->cur_ino,
2868 sctx->cur_inode_gen, valid_path);
2869 if (ret < 0)
2870 goto out;
2871 is_orphan = 1;
2874 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2875 ret = dup_ref(cur, &check_dirs);
2876 if (ret < 0)
2877 goto out;
2879 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2880 !list_empty(&sctx->deleted_refs)) {
2882 * We have a moved dir. Add the old parent to check_dirs
2884 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2885 list);
2886 ret = dup_ref(cur, &check_dirs);
2887 if (ret < 0)
2888 goto out;
2889 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2891 * We have a non dir inode. Go through all deleted refs and
2892 * unlink them if they were not already overwritten by other
2893 * inodes.
2895 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2896 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2897 sctx->cur_ino, sctx->cur_inode_gen,
2898 cur->name, cur->name_len);
2899 if (ret < 0)
2900 goto out;
2901 if (!ret) {
2902 ret = send_unlink(sctx, cur->full_path);
2903 if (ret < 0)
2904 goto out;
2906 ret = dup_ref(cur, &check_dirs);
2907 if (ret < 0)
2908 goto out;
2911 * If the inode is still orphan, unlink the orphan. This may
2912 * happen when a previous inode did overwrite the first ref
2913 * of this inode and no new refs were added for the current
2914 * inode. Unlinking does not mean that the inode is deleted in
2915 * all cases. There may still be links to this inode in other
2916 * places.
2918 if (is_orphan) {
2919 ret = send_unlink(sctx, valid_path);
2920 if (ret < 0)
2921 goto out;
2926 * We did collect all parent dirs where cur_inode was once located. We
2927 * now go through all these dirs and check if they are pending for
2928 * deletion and if it's finally possible to perform the rmdir now.
2929 * We also update the inode stats of the parent dirs here.
2931 list_for_each_entry(cur, &check_dirs, list) {
2933 * In case we had refs into dirs that were not processed yet,
2934 * we don't need to do the utime and rmdir logic for these dirs.
2935 * The dir will be processed later.
2937 if (cur->dir > sctx->cur_ino)
2938 continue;
2940 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2941 if (ret < 0)
2942 goto out;
2944 if (ret == inode_state_did_create ||
2945 ret == inode_state_no_change) {
2946 /* TODO delayed utimes */
2947 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
2948 if (ret < 0)
2949 goto out;
2950 } else if (ret == inode_state_did_delete) {
2951 ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
2952 if (ret < 0)
2953 goto out;
2954 if (ret) {
2955 ret = get_cur_path(sctx, cur->dir,
2956 cur->dir_gen, valid_path);
2957 if (ret < 0)
2958 goto out;
2959 ret = send_rmdir(sctx, valid_path);
2960 if (ret < 0)
2961 goto out;
2966 ret = 0;
2968 out:
2969 __free_recorded_refs(&check_dirs);
2970 free_recorded_refs(sctx);
2971 fs_path_free(valid_path);
2972 return ret;
2975 static int __record_new_ref(int num, u64 dir, int index,
2976 struct fs_path *name,
2977 void *ctx)
2979 int ret = 0;
2980 struct send_ctx *sctx = ctx;
2981 struct fs_path *p;
2982 u64 gen;
2984 p = fs_path_alloc();
2985 if (!p)
2986 return -ENOMEM;
2988 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
2989 NULL, NULL);
2990 if (ret < 0)
2991 goto out;
2993 ret = get_cur_path(sctx, dir, gen, p);
2994 if (ret < 0)
2995 goto out;
2996 ret = fs_path_add_path(p, name);
2997 if (ret < 0)
2998 goto out;
3000 ret = record_ref(&sctx->new_refs, dir, gen, p);
3002 out:
3003 if (ret)
3004 fs_path_free(p);
3005 return ret;
3008 static int __record_deleted_ref(int num, u64 dir, int index,
3009 struct fs_path *name,
3010 void *ctx)
3012 int ret = 0;
3013 struct send_ctx *sctx = ctx;
3014 struct fs_path *p;
3015 u64 gen;
3017 p = fs_path_alloc();
3018 if (!p)
3019 return -ENOMEM;
3021 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3022 NULL, NULL);
3023 if (ret < 0)
3024 goto out;
3026 ret = get_cur_path(sctx, dir, gen, p);
3027 if (ret < 0)
3028 goto out;
3029 ret = fs_path_add_path(p, name);
3030 if (ret < 0)
3031 goto out;
3033 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3035 out:
3036 if (ret)
3037 fs_path_free(p);
3038 return ret;
3041 static int record_new_ref(struct send_ctx *sctx)
3043 int ret;
3045 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3046 sctx->cmp_key, 0, __record_new_ref, sctx);
3047 if (ret < 0)
3048 goto out;
3049 ret = 0;
3051 out:
3052 return ret;
3055 static int record_deleted_ref(struct send_ctx *sctx)
3057 int ret;
3059 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3060 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3061 if (ret < 0)
3062 goto out;
3063 ret = 0;
3065 out:
3066 return ret;
3069 struct find_ref_ctx {
3070 u64 dir;
3071 u64 dir_gen;
3072 struct btrfs_root *root;
3073 struct fs_path *name;
3074 int found_idx;
3077 static int __find_iref(int num, u64 dir, int index,
3078 struct fs_path *name,
3079 void *ctx_)
3081 struct find_ref_ctx *ctx = ctx_;
3082 u64 dir_gen;
3083 int ret;
3085 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3086 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3088 * To avoid doing extra lookups we'll only do this if everything
3089 * else matches.
3091 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3092 NULL, NULL, NULL);
3093 if (ret)
3094 return ret;
3095 if (dir_gen != ctx->dir_gen)
3096 return 0;
3097 ctx->found_idx = num;
3098 return 1;
3100 return 0;
3103 static int find_iref(struct btrfs_root *root,
3104 struct btrfs_path *path,
3105 struct btrfs_key *key,
3106 u64 dir, u64 dir_gen, struct fs_path *name)
3108 int ret;
3109 struct find_ref_ctx ctx;
3111 ctx.dir = dir;
3112 ctx.name = name;
3113 ctx.dir_gen = dir_gen;
3114 ctx.found_idx = -1;
3115 ctx.root = root;
3117 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
3118 if (ret < 0)
3119 return ret;
3121 if (ctx.found_idx == -1)
3122 return -ENOENT;
3124 return ctx.found_idx;
3127 static int __record_changed_new_ref(int num, u64 dir, int index,
3128 struct fs_path *name,
3129 void *ctx)
3131 u64 dir_gen;
3132 int ret;
3133 struct send_ctx *sctx = ctx;
3135 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3136 NULL, NULL, NULL);
3137 if (ret)
3138 return ret;
3140 ret = find_iref(sctx->parent_root, sctx->right_path,
3141 sctx->cmp_key, dir, dir_gen, name);
3142 if (ret == -ENOENT)
3143 ret = __record_new_ref(num, dir, index, name, sctx);
3144 else if (ret > 0)
3145 ret = 0;
3147 return ret;
3150 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3151 struct fs_path *name,
3152 void *ctx)
3154 u64 dir_gen;
3155 int ret;
3156 struct send_ctx *sctx = ctx;
3158 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3159 NULL, NULL, NULL);
3160 if (ret)
3161 return ret;
3163 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
3164 dir, dir_gen, name);
3165 if (ret == -ENOENT)
3166 ret = __record_deleted_ref(num, dir, index, name, sctx);
3167 else if (ret > 0)
3168 ret = 0;
3170 return ret;
3173 static int record_changed_ref(struct send_ctx *sctx)
3175 int ret = 0;
3177 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3178 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3179 if (ret < 0)
3180 goto out;
3181 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3182 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3183 if (ret < 0)
3184 goto out;
3185 ret = 0;
3187 out:
3188 return ret;
3192 * Record and process all refs at once. Needed when an inode changes the
3193 * generation number, which means that it was deleted and recreated.
3195 static int process_all_refs(struct send_ctx *sctx,
3196 enum btrfs_compare_tree_result cmd)
3198 int ret;
3199 struct btrfs_root *root;
3200 struct btrfs_path *path;
3201 struct btrfs_key key;
3202 struct btrfs_key found_key;
3203 struct extent_buffer *eb;
3204 int slot;
3205 iterate_inode_ref_t cb;
3207 path = alloc_path_for_send();
3208 if (!path)
3209 return -ENOMEM;
3211 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3212 root = sctx->send_root;
3213 cb = __record_new_ref;
3214 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3215 root = sctx->parent_root;
3216 cb = __record_deleted_ref;
3217 } else {
3218 BUG();
3221 key.objectid = sctx->cmp_key->objectid;
3222 key.type = BTRFS_INODE_REF_KEY;
3223 key.offset = 0;
3224 while (1) {
3225 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3226 if (ret < 0)
3227 goto out;
3228 if (ret)
3229 break;
3231 eb = path->nodes[0];
3232 slot = path->slots[0];
3233 btrfs_item_key_to_cpu(eb, &found_key, slot);
3235 if (found_key.objectid != key.objectid ||
3236 (found_key.type != BTRFS_INODE_REF_KEY &&
3237 found_key.type != BTRFS_INODE_EXTREF_KEY))
3238 break;
3240 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
3241 btrfs_release_path(path);
3242 if (ret < 0)
3243 goto out;
3245 key.offset = found_key.offset + 1;
3247 btrfs_release_path(path);
3249 ret = process_recorded_refs(sctx);
3251 out:
3252 btrfs_free_path(path);
3253 return ret;
3256 static int send_set_xattr(struct send_ctx *sctx,
3257 struct fs_path *path,
3258 const char *name, int name_len,
3259 const char *data, int data_len)
3261 int ret = 0;
3263 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3264 if (ret < 0)
3265 goto out;
3267 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3268 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3269 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3271 ret = send_cmd(sctx);
3273 tlv_put_failure:
3274 out:
3275 return ret;
3278 static int send_remove_xattr(struct send_ctx *sctx,
3279 struct fs_path *path,
3280 const char *name, int name_len)
3282 int ret = 0;
3284 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3285 if (ret < 0)
3286 goto out;
3288 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3289 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3291 ret = send_cmd(sctx);
3293 tlv_put_failure:
3294 out:
3295 return ret;
3298 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3299 const char *name, int name_len,
3300 const char *data, int data_len,
3301 u8 type, void *ctx)
3303 int ret;
3304 struct send_ctx *sctx = ctx;
3305 struct fs_path *p;
3306 posix_acl_xattr_header dummy_acl;
3308 p = fs_path_alloc();
3309 if (!p)
3310 return -ENOMEM;
3313 * This hack is needed because empty acl's are stored as zero byte
3314 * data in xattrs. Problem with that is, that receiving these zero byte
3315 * acl's will fail later. To fix this, we send a dummy acl list that
3316 * only contains the version number and no entries.
3318 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3319 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3320 if (data_len == 0) {
3321 dummy_acl.a_version =
3322 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3323 data = (char *)&dummy_acl;
3324 data_len = sizeof(dummy_acl);
3328 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3329 if (ret < 0)
3330 goto out;
3332 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3334 out:
3335 fs_path_free(p);
3336 return ret;
3339 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3340 const char *name, int name_len,
3341 const char *data, int data_len,
3342 u8 type, void *ctx)
3344 int ret;
3345 struct send_ctx *sctx = ctx;
3346 struct fs_path *p;
3348 p = fs_path_alloc();
3349 if (!p)
3350 return -ENOMEM;
3352 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3353 if (ret < 0)
3354 goto out;
3356 ret = send_remove_xattr(sctx, p, name, name_len);
3358 out:
3359 fs_path_free(p);
3360 return ret;
3363 static int process_new_xattr(struct send_ctx *sctx)
3365 int ret = 0;
3367 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3368 sctx->cmp_key, __process_new_xattr, sctx);
3370 return ret;
3373 static int process_deleted_xattr(struct send_ctx *sctx)
3375 int ret;
3377 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3378 sctx->cmp_key, __process_deleted_xattr, sctx);
3380 return ret;
3383 struct find_xattr_ctx {
3384 const char *name;
3385 int name_len;
3386 int found_idx;
3387 char *found_data;
3388 int found_data_len;
3391 static int __find_xattr(int num, struct btrfs_key *di_key,
3392 const char *name, int name_len,
3393 const char *data, int data_len,
3394 u8 type, void *vctx)
3396 struct find_xattr_ctx *ctx = vctx;
3398 if (name_len == ctx->name_len &&
3399 strncmp(name, ctx->name, name_len) == 0) {
3400 ctx->found_idx = num;
3401 ctx->found_data_len = data_len;
3402 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
3403 if (!ctx->found_data)
3404 return -ENOMEM;
3405 return 1;
3407 return 0;
3410 static int find_xattr(struct btrfs_root *root,
3411 struct btrfs_path *path,
3412 struct btrfs_key *key,
3413 const char *name, int name_len,
3414 char **data, int *data_len)
3416 int ret;
3417 struct find_xattr_ctx ctx;
3419 ctx.name = name;
3420 ctx.name_len = name_len;
3421 ctx.found_idx = -1;
3422 ctx.found_data = NULL;
3423 ctx.found_data_len = 0;
3425 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
3426 if (ret < 0)
3427 return ret;
3429 if (ctx.found_idx == -1)
3430 return -ENOENT;
3431 if (data) {
3432 *data = ctx.found_data;
3433 *data_len = ctx.found_data_len;
3434 } else {
3435 kfree(ctx.found_data);
3437 return ctx.found_idx;
3441 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3442 const char *name, int name_len,
3443 const char *data, int data_len,
3444 u8 type, void *ctx)
3446 int ret;
3447 struct send_ctx *sctx = ctx;
3448 char *found_data = NULL;
3449 int found_data_len = 0;
3451 ret = find_xattr(sctx->parent_root, sctx->right_path,
3452 sctx->cmp_key, name, name_len, &found_data,
3453 &found_data_len);
3454 if (ret == -ENOENT) {
3455 ret = __process_new_xattr(num, di_key, name, name_len, data,
3456 data_len, type, ctx);
3457 } else if (ret >= 0) {
3458 if (data_len != found_data_len ||
3459 memcmp(data, found_data, data_len)) {
3460 ret = __process_new_xattr(num, di_key, name, name_len,
3461 data, data_len, type, ctx);
3462 } else {
3463 ret = 0;
3467 kfree(found_data);
3468 return ret;
3471 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3472 const char *name, int name_len,
3473 const char *data, int data_len,
3474 u8 type, void *ctx)
3476 int ret;
3477 struct send_ctx *sctx = ctx;
3479 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
3480 name, name_len, NULL, NULL);
3481 if (ret == -ENOENT)
3482 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3483 data_len, type, ctx);
3484 else if (ret >= 0)
3485 ret = 0;
3487 return ret;
3490 static int process_changed_xattr(struct send_ctx *sctx)
3492 int ret = 0;
3494 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3495 sctx->cmp_key, __process_changed_new_xattr, sctx);
3496 if (ret < 0)
3497 goto out;
3498 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3499 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3501 out:
3502 return ret;
3505 static int process_all_new_xattrs(struct send_ctx *sctx)
3507 int ret;
3508 struct btrfs_root *root;
3509 struct btrfs_path *path;
3510 struct btrfs_key key;
3511 struct btrfs_key found_key;
3512 struct extent_buffer *eb;
3513 int slot;
3515 path = alloc_path_for_send();
3516 if (!path)
3517 return -ENOMEM;
3519 root = sctx->send_root;
3521 key.objectid = sctx->cmp_key->objectid;
3522 key.type = BTRFS_XATTR_ITEM_KEY;
3523 key.offset = 0;
3524 while (1) {
3525 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3526 if (ret < 0)
3527 goto out;
3528 if (ret) {
3529 ret = 0;
3530 goto out;
3533 eb = path->nodes[0];
3534 slot = path->slots[0];
3535 btrfs_item_key_to_cpu(eb, &found_key, slot);
3537 if (found_key.objectid != key.objectid ||
3538 found_key.type != key.type) {
3539 ret = 0;
3540 goto out;
3543 ret = iterate_dir_item(root, path, &found_key,
3544 __process_new_xattr, sctx);
3545 if (ret < 0)
3546 goto out;
3548 btrfs_release_path(path);
3549 key.offset = found_key.offset + 1;
3552 out:
3553 btrfs_free_path(path);
3554 return ret;
3557 static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
3559 struct btrfs_root *root = sctx->send_root;
3560 struct btrfs_fs_info *fs_info = root->fs_info;
3561 struct inode *inode;
3562 struct page *page;
3563 char *addr;
3564 struct btrfs_key key;
3565 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
3566 pgoff_t last_index;
3567 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
3568 ssize_t ret = 0;
3570 key.objectid = sctx->cur_ino;
3571 key.type = BTRFS_INODE_ITEM_KEY;
3572 key.offset = 0;
3574 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3575 if (IS_ERR(inode))
3576 return PTR_ERR(inode);
3578 if (offset + len > i_size_read(inode)) {
3579 if (offset > i_size_read(inode))
3580 len = 0;
3581 else
3582 len = offset - i_size_read(inode);
3584 if (len == 0)
3585 goto out;
3587 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
3588 while (index <= last_index) {
3589 unsigned cur_len = min_t(unsigned, len,
3590 PAGE_CACHE_SIZE - pg_offset);
3591 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3592 if (!page) {
3593 ret = -ENOMEM;
3594 break;
3597 if (!PageUptodate(page)) {
3598 btrfs_readpage(NULL, page);
3599 lock_page(page);
3600 if (!PageUptodate(page)) {
3601 unlock_page(page);
3602 page_cache_release(page);
3603 ret = -EIO;
3604 break;
3608 addr = kmap(page);
3609 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
3610 kunmap(page);
3611 unlock_page(page);
3612 page_cache_release(page);
3613 index++;
3614 pg_offset = 0;
3615 len -= cur_len;
3616 ret += cur_len;
3618 out:
3619 iput(inode);
3620 return ret;
3624 * Read some bytes from the current inode/file and send a write command to
3625 * user space.
3627 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3629 int ret = 0;
3630 struct fs_path *p;
3631 ssize_t num_read = 0;
3633 p = fs_path_alloc();
3634 if (!p)
3635 return -ENOMEM;
3637 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3639 num_read = fill_read_buf(sctx, offset, len);
3640 if (num_read <= 0) {
3641 if (num_read < 0)
3642 ret = num_read;
3643 goto out;
3646 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3647 if (ret < 0)
3648 goto out;
3650 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3651 if (ret < 0)
3652 goto out;
3654 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3655 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3656 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
3658 ret = send_cmd(sctx);
3660 tlv_put_failure:
3661 out:
3662 fs_path_free(p);
3663 if (ret < 0)
3664 return ret;
3665 return num_read;
3669 * Send a clone command to user space.
3671 static int send_clone(struct send_ctx *sctx,
3672 u64 offset, u32 len,
3673 struct clone_root *clone_root)
3675 int ret = 0;
3676 struct fs_path *p;
3677 u64 gen;
3679 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3680 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3681 clone_root->root->objectid, clone_root->ino,
3682 clone_root->offset);
3684 p = fs_path_alloc();
3685 if (!p)
3686 return -ENOMEM;
3688 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3689 if (ret < 0)
3690 goto out;
3692 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3693 if (ret < 0)
3694 goto out;
3696 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3697 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3698 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3700 if (clone_root->root == sctx->send_root) {
3701 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3702 &gen, NULL, NULL, NULL, NULL);
3703 if (ret < 0)
3704 goto out;
3705 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3706 } else {
3707 ret = get_inode_path(clone_root->root, clone_root->ino, p);
3709 if (ret < 0)
3710 goto out;
3712 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3713 clone_root->root->root_item.uuid);
3714 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3715 clone_root->root->root_item.ctransid);
3716 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3717 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3718 clone_root->offset);
3720 ret = send_cmd(sctx);
3722 tlv_put_failure:
3723 out:
3724 fs_path_free(p);
3725 return ret;
3729 * Send an update extent command to user space.
3731 static int send_update_extent(struct send_ctx *sctx,
3732 u64 offset, u32 len)
3734 int ret = 0;
3735 struct fs_path *p;
3737 p = fs_path_alloc();
3738 if (!p)
3739 return -ENOMEM;
3741 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
3742 if (ret < 0)
3743 goto out;
3745 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3746 if (ret < 0)
3747 goto out;
3749 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3750 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3751 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
3753 ret = send_cmd(sctx);
3755 tlv_put_failure:
3756 out:
3757 fs_path_free(p);
3758 return ret;
3761 static int send_write_or_clone(struct send_ctx *sctx,
3762 struct btrfs_path *path,
3763 struct btrfs_key *key,
3764 struct clone_root *clone_root)
3766 int ret = 0;
3767 struct btrfs_file_extent_item *ei;
3768 u64 offset = key->offset;
3769 u64 pos = 0;
3770 u64 len;
3771 u32 l;
3772 u8 type;
3774 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3775 struct btrfs_file_extent_item);
3776 type = btrfs_file_extent_type(path->nodes[0], ei);
3777 if (type == BTRFS_FILE_EXTENT_INLINE) {
3778 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3780 * it is possible the inline item won't cover the whole page,
3781 * but there may be items after this page. Make
3782 * sure to send the whole thing
3784 len = PAGE_CACHE_ALIGN(len);
3785 } else {
3786 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3789 if (offset + len > sctx->cur_inode_size)
3790 len = sctx->cur_inode_size - offset;
3791 if (len == 0) {
3792 ret = 0;
3793 goto out;
3796 if (clone_root) {
3797 ret = send_clone(sctx, offset, len, clone_root);
3798 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
3799 ret = send_update_extent(sctx, offset, len);
3800 } else {
3801 while (pos < len) {
3802 l = len - pos;
3803 if (l > BTRFS_SEND_READ_SIZE)
3804 l = BTRFS_SEND_READ_SIZE;
3805 ret = send_write(sctx, pos + offset, l);
3806 if (ret < 0)
3807 goto out;
3808 if (!ret)
3809 break;
3810 pos += ret;
3812 ret = 0;
3814 out:
3815 return ret;
3818 static int is_extent_unchanged(struct send_ctx *sctx,
3819 struct btrfs_path *left_path,
3820 struct btrfs_key *ekey)
3822 int ret = 0;
3823 struct btrfs_key key;
3824 struct btrfs_path *path = NULL;
3825 struct extent_buffer *eb;
3826 int slot;
3827 struct btrfs_key found_key;
3828 struct btrfs_file_extent_item *ei;
3829 u64 left_disknr;
3830 u64 right_disknr;
3831 u64 left_offset;
3832 u64 right_offset;
3833 u64 left_offset_fixed;
3834 u64 left_len;
3835 u64 right_len;
3836 u64 left_gen;
3837 u64 right_gen;
3838 u8 left_type;
3839 u8 right_type;
3841 path = alloc_path_for_send();
3842 if (!path)
3843 return -ENOMEM;
3845 eb = left_path->nodes[0];
3846 slot = left_path->slots[0];
3847 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3848 left_type = btrfs_file_extent_type(eb, ei);
3850 if (left_type != BTRFS_FILE_EXTENT_REG) {
3851 ret = 0;
3852 goto out;
3854 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3855 left_len = btrfs_file_extent_num_bytes(eb, ei);
3856 left_offset = btrfs_file_extent_offset(eb, ei);
3857 left_gen = btrfs_file_extent_generation(eb, ei);
3860 * Following comments will refer to these graphics. L is the left
3861 * extents which we are checking at the moment. 1-8 are the right
3862 * extents that we iterate.
3864 * |-----L-----|
3865 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3867 * |-----L-----|
3868 * |--1--|-2b-|...(same as above)
3870 * Alternative situation. Happens on files where extents got split.
3871 * |-----L-----|
3872 * |-----------7-----------|-6-|
3874 * Alternative situation. Happens on files which got larger.
3875 * |-----L-----|
3876 * |-8-|
3877 * Nothing follows after 8.
3880 key.objectid = ekey->objectid;
3881 key.type = BTRFS_EXTENT_DATA_KEY;
3882 key.offset = ekey->offset;
3883 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3884 if (ret < 0)
3885 goto out;
3886 if (ret) {
3887 ret = 0;
3888 goto out;
3892 * Handle special case where the right side has no extents at all.
3894 eb = path->nodes[0];
3895 slot = path->slots[0];
3896 btrfs_item_key_to_cpu(eb, &found_key, slot);
3897 if (found_key.objectid != key.objectid ||
3898 found_key.type != key.type) {
3899 /* If we're a hole then just pretend nothing changed */
3900 ret = (left_disknr) ? 0 : 1;
3901 goto out;
3905 * We're now on 2a, 2b or 7.
3907 key = found_key;
3908 while (key.offset < ekey->offset + left_len) {
3909 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3910 right_type = btrfs_file_extent_type(eb, ei);
3911 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3912 right_len = btrfs_file_extent_num_bytes(eb, ei);
3913 right_offset = btrfs_file_extent_offset(eb, ei);
3914 right_gen = btrfs_file_extent_generation(eb, ei);
3916 if (right_type != BTRFS_FILE_EXTENT_REG) {
3917 ret = 0;
3918 goto out;
3922 * Are we at extent 8? If yes, we know the extent is changed.
3923 * This may only happen on the first iteration.
3925 if (found_key.offset + right_len <= ekey->offset) {
3926 /* If we're a hole just pretend nothing changed */
3927 ret = (left_disknr) ? 0 : 1;
3928 goto out;
3931 left_offset_fixed = left_offset;
3932 if (key.offset < ekey->offset) {
3933 /* Fix the right offset for 2a and 7. */
3934 right_offset += ekey->offset - key.offset;
3935 } else {
3936 /* Fix the left offset for all behind 2a and 2b */
3937 left_offset_fixed += key.offset - ekey->offset;
3941 * Check if we have the same extent.
3943 if (left_disknr != right_disknr ||
3944 left_offset_fixed != right_offset ||
3945 left_gen != right_gen) {
3946 ret = 0;
3947 goto out;
3951 * Go to the next extent.
3953 ret = btrfs_next_item(sctx->parent_root, path);
3954 if (ret < 0)
3955 goto out;
3956 if (!ret) {
3957 eb = path->nodes[0];
3958 slot = path->slots[0];
3959 btrfs_item_key_to_cpu(eb, &found_key, slot);
3961 if (ret || found_key.objectid != key.objectid ||
3962 found_key.type != key.type) {
3963 key.offset += right_len;
3964 break;
3966 if (found_key.offset != key.offset + right_len) {
3967 ret = 0;
3968 goto out;
3970 key = found_key;
3974 * We're now behind the left extent (treat as unchanged) or at the end
3975 * of the right side (treat as changed).
3977 if (key.offset >= ekey->offset + left_len)
3978 ret = 1;
3979 else
3980 ret = 0;
3983 out:
3984 btrfs_free_path(path);
3985 return ret;
3988 static int process_extent(struct send_ctx *sctx,
3989 struct btrfs_path *path,
3990 struct btrfs_key *key)
3992 struct clone_root *found_clone = NULL;
3993 int ret = 0;
3995 if (S_ISLNK(sctx->cur_inode_mode))
3996 return 0;
3998 if (sctx->parent_root && !sctx->cur_inode_new) {
3999 ret = is_extent_unchanged(sctx, path, key);
4000 if (ret < 0)
4001 goto out;
4002 if (ret) {
4003 ret = 0;
4004 goto out;
4006 } else {
4007 struct btrfs_file_extent_item *ei;
4008 u8 type;
4010 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4011 struct btrfs_file_extent_item);
4012 type = btrfs_file_extent_type(path->nodes[0], ei);
4013 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4014 type == BTRFS_FILE_EXTENT_REG) {
4016 * The send spec does not have a prealloc command yet,
4017 * so just leave a hole for prealloc'ed extents until
4018 * we have enough commands queued up to justify rev'ing
4019 * the send spec.
4021 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4022 ret = 0;
4023 goto out;
4026 /* Have a hole, just skip it. */
4027 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4028 ret = 0;
4029 goto out;
4034 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4035 sctx->cur_inode_size, &found_clone);
4036 if (ret != -ENOENT && ret < 0)
4037 goto out;
4039 ret = send_write_or_clone(sctx, path, key, found_clone);
4041 out:
4042 return ret;
4045 static int process_all_extents(struct send_ctx *sctx)
4047 int ret;
4048 struct btrfs_root *root;
4049 struct btrfs_path *path;
4050 struct btrfs_key key;
4051 struct btrfs_key found_key;
4052 struct extent_buffer *eb;
4053 int slot;
4055 root = sctx->send_root;
4056 path = alloc_path_for_send();
4057 if (!path)
4058 return -ENOMEM;
4060 key.objectid = sctx->cmp_key->objectid;
4061 key.type = BTRFS_EXTENT_DATA_KEY;
4062 key.offset = 0;
4063 while (1) {
4064 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
4065 if (ret < 0)
4066 goto out;
4067 if (ret) {
4068 ret = 0;
4069 goto out;
4072 eb = path->nodes[0];
4073 slot = path->slots[0];
4074 btrfs_item_key_to_cpu(eb, &found_key, slot);
4076 if (found_key.objectid != key.objectid ||
4077 found_key.type != key.type) {
4078 ret = 0;
4079 goto out;
4082 ret = process_extent(sctx, path, &found_key);
4083 if (ret < 0)
4084 goto out;
4086 btrfs_release_path(path);
4087 key.offset = found_key.offset + 1;
4090 out:
4091 btrfs_free_path(path);
4092 return ret;
4095 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
4097 int ret = 0;
4099 if (sctx->cur_ino == 0)
4100 goto out;
4101 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4102 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4103 goto out;
4104 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4105 goto out;
4107 ret = process_recorded_refs(sctx);
4108 if (ret < 0)
4109 goto out;
4112 * We have processed the refs and thus need to advance send_progress.
4113 * Now, calls to get_cur_xxx will take the updated refs of the current
4114 * inode into account.
4116 sctx->send_progress = sctx->cur_ino + 1;
4118 out:
4119 return ret;
4122 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4124 int ret = 0;
4125 u64 left_mode;
4126 u64 left_uid;
4127 u64 left_gid;
4128 u64 right_mode;
4129 u64 right_uid;
4130 u64 right_gid;
4131 int need_chmod = 0;
4132 int need_chown = 0;
4134 ret = process_recorded_refs_if_needed(sctx, at_end);
4135 if (ret < 0)
4136 goto out;
4138 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4139 goto out;
4140 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4141 goto out;
4143 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4144 &left_mode, &left_uid, &left_gid, NULL);
4145 if (ret < 0)
4146 goto out;
4148 if (!sctx->parent_root || sctx->cur_inode_new) {
4149 need_chown = 1;
4150 if (!S_ISLNK(sctx->cur_inode_mode))
4151 need_chmod = 1;
4152 } else {
4153 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4154 NULL, NULL, &right_mode, &right_uid,
4155 &right_gid, NULL);
4156 if (ret < 0)
4157 goto out;
4159 if (left_uid != right_uid || left_gid != right_gid)
4160 need_chown = 1;
4161 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4162 need_chmod = 1;
4165 if (S_ISREG(sctx->cur_inode_mode)) {
4166 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4167 sctx->cur_inode_size);
4168 if (ret < 0)
4169 goto out;
4172 if (need_chown) {
4173 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4174 left_uid, left_gid);
4175 if (ret < 0)
4176 goto out;
4178 if (need_chmod) {
4179 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4180 left_mode);
4181 if (ret < 0)
4182 goto out;
4186 * Need to send that every time, no matter if it actually changed
4187 * between the two trees as we have done changes to the inode before.
4189 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4190 if (ret < 0)
4191 goto out;
4193 out:
4194 return ret;
4197 static int changed_inode(struct send_ctx *sctx,
4198 enum btrfs_compare_tree_result result)
4200 int ret = 0;
4201 struct btrfs_key *key = sctx->cmp_key;
4202 struct btrfs_inode_item *left_ii = NULL;
4203 struct btrfs_inode_item *right_ii = NULL;
4204 u64 left_gen = 0;
4205 u64 right_gen = 0;
4207 sctx->cur_ino = key->objectid;
4208 sctx->cur_inode_new_gen = 0;
4211 * Set send_progress to current inode. This will tell all get_cur_xxx
4212 * functions that the current inode's refs are not updated yet. Later,
4213 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4215 sctx->send_progress = sctx->cur_ino;
4217 if (result == BTRFS_COMPARE_TREE_NEW ||
4218 result == BTRFS_COMPARE_TREE_CHANGED) {
4219 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4220 sctx->left_path->slots[0],
4221 struct btrfs_inode_item);
4222 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4223 left_ii);
4224 } else {
4225 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4226 sctx->right_path->slots[0],
4227 struct btrfs_inode_item);
4228 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4229 right_ii);
4231 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4232 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4233 sctx->right_path->slots[0],
4234 struct btrfs_inode_item);
4236 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4237 right_ii);
4240 * The cur_ino = root dir case is special here. We can't treat
4241 * the inode as deleted+reused because it would generate a
4242 * stream that tries to delete/mkdir the root dir.
4244 if (left_gen != right_gen &&
4245 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4246 sctx->cur_inode_new_gen = 1;
4249 if (result == BTRFS_COMPARE_TREE_NEW) {
4250 sctx->cur_inode_gen = left_gen;
4251 sctx->cur_inode_new = 1;
4252 sctx->cur_inode_deleted = 0;
4253 sctx->cur_inode_size = btrfs_inode_size(
4254 sctx->left_path->nodes[0], left_ii);
4255 sctx->cur_inode_mode = btrfs_inode_mode(
4256 sctx->left_path->nodes[0], left_ii);
4257 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4258 ret = send_create_inode_if_needed(sctx);
4259 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4260 sctx->cur_inode_gen = right_gen;
4261 sctx->cur_inode_new = 0;
4262 sctx->cur_inode_deleted = 1;
4263 sctx->cur_inode_size = btrfs_inode_size(
4264 sctx->right_path->nodes[0], right_ii);
4265 sctx->cur_inode_mode = btrfs_inode_mode(
4266 sctx->right_path->nodes[0], right_ii);
4267 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4269 * We need to do some special handling in case the inode was
4270 * reported as changed with a changed generation number. This
4271 * means that the original inode was deleted and new inode
4272 * reused the same inum. So we have to treat the old inode as
4273 * deleted and the new one as new.
4275 if (sctx->cur_inode_new_gen) {
4277 * First, process the inode as if it was deleted.
4279 sctx->cur_inode_gen = right_gen;
4280 sctx->cur_inode_new = 0;
4281 sctx->cur_inode_deleted = 1;
4282 sctx->cur_inode_size = btrfs_inode_size(
4283 sctx->right_path->nodes[0], right_ii);
4284 sctx->cur_inode_mode = btrfs_inode_mode(
4285 sctx->right_path->nodes[0], right_ii);
4286 ret = process_all_refs(sctx,
4287 BTRFS_COMPARE_TREE_DELETED);
4288 if (ret < 0)
4289 goto out;
4292 * Now process the inode as if it was new.
4294 sctx->cur_inode_gen = left_gen;
4295 sctx->cur_inode_new = 1;
4296 sctx->cur_inode_deleted = 0;
4297 sctx->cur_inode_size = btrfs_inode_size(
4298 sctx->left_path->nodes[0], left_ii);
4299 sctx->cur_inode_mode = btrfs_inode_mode(
4300 sctx->left_path->nodes[0], left_ii);
4301 ret = send_create_inode_if_needed(sctx);
4302 if (ret < 0)
4303 goto out;
4305 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4306 if (ret < 0)
4307 goto out;
4309 * Advance send_progress now as we did not get into
4310 * process_recorded_refs_if_needed in the new_gen case.
4312 sctx->send_progress = sctx->cur_ino + 1;
4315 * Now process all extents and xattrs of the inode as if
4316 * they were all new.
4318 ret = process_all_extents(sctx);
4319 if (ret < 0)
4320 goto out;
4321 ret = process_all_new_xattrs(sctx);
4322 if (ret < 0)
4323 goto out;
4324 } else {
4325 sctx->cur_inode_gen = left_gen;
4326 sctx->cur_inode_new = 0;
4327 sctx->cur_inode_new_gen = 0;
4328 sctx->cur_inode_deleted = 0;
4329 sctx->cur_inode_size = btrfs_inode_size(
4330 sctx->left_path->nodes[0], left_ii);
4331 sctx->cur_inode_mode = btrfs_inode_mode(
4332 sctx->left_path->nodes[0], left_ii);
4336 out:
4337 return ret;
4341 * We have to process new refs before deleted refs, but compare_trees gives us
4342 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4343 * first and later process them in process_recorded_refs.
4344 * For the cur_inode_new_gen case, we skip recording completely because
4345 * changed_inode did already initiate processing of refs. The reason for this is
4346 * that in this case, compare_tree actually compares the refs of 2 different
4347 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4348 * refs of the right tree as deleted and all refs of the left tree as new.
4350 static int changed_ref(struct send_ctx *sctx,
4351 enum btrfs_compare_tree_result result)
4353 int ret = 0;
4355 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4357 if (!sctx->cur_inode_new_gen &&
4358 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4359 if (result == BTRFS_COMPARE_TREE_NEW)
4360 ret = record_new_ref(sctx);
4361 else if (result == BTRFS_COMPARE_TREE_DELETED)
4362 ret = record_deleted_ref(sctx);
4363 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4364 ret = record_changed_ref(sctx);
4367 return ret;
4371 * Process new/deleted/changed xattrs. We skip processing in the
4372 * cur_inode_new_gen case because changed_inode did already initiate processing
4373 * of xattrs. The reason is the same as in changed_ref
4375 static int changed_xattr(struct send_ctx *sctx,
4376 enum btrfs_compare_tree_result result)
4378 int ret = 0;
4380 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4382 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4383 if (result == BTRFS_COMPARE_TREE_NEW)
4384 ret = process_new_xattr(sctx);
4385 else if (result == BTRFS_COMPARE_TREE_DELETED)
4386 ret = process_deleted_xattr(sctx);
4387 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4388 ret = process_changed_xattr(sctx);
4391 return ret;
4395 * Process new/deleted/changed extents. We skip processing in the
4396 * cur_inode_new_gen case because changed_inode did already initiate processing
4397 * of extents. The reason is the same as in changed_ref
4399 static int changed_extent(struct send_ctx *sctx,
4400 enum btrfs_compare_tree_result result)
4402 int ret = 0;
4404 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4406 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4407 if (result != BTRFS_COMPARE_TREE_DELETED)
4408 ret = process_extent(sctx, sctx->left_path,
4409 sctx->cmp_key);
4412 return ret;
4415 static int dir_changed(struct send_ctx *sctx, u64 dir)
4417 u64 orig_gen, new_gen;
4418 int ret;
4420 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
4421 NULL, NULL);
4422 if (ret)
4423 return ret;
4425 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
4426 NULL, NULL, NULL);
4427 if (ret)
4428 return ret;
4430 return (orig_gen != new_gen) ? 1 : 0;
4433 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
4434 struct btrfs_key *key)
4436 struct btrfs_inode_extref *extref;
4437 struct extent_buffer *leaf;
4438 u64 dirid = 0, last_dirid = 0;
4439 unsigned long ptr;
4440 u32 item_size;
4441 u32 cur_offset = 0;
4442 int ref_name_len;
4443 int ret = 0;
4445 /* Easy case, just check this one dirid */
4446 if (key->type == BTRFS_INODE_REF_KEY) {
4447 dirid = key->offset;
4449 ret = dir_changed(sctx, dirid);
4450 goto out;
4453 leaf = path->nodes[0];
4454 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4455 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4456 while (cur_offset < item_size) {
4457 extref = (struct btrfs_inode_extref *)(ptr +
4458 cur_offset);
4459 dirid = btrfs_inode_extref_parent(leaf, extref);
4460 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
4461 cur_offset += ref_name_len + sizeof(*extref);
4462 if (dirid == last_dirid)
4463 continue;
4464 ret = dir_changed(sctx, dirid);
4465 if (ret)
4466 break;
4467 last_dirid = dirid;
4469 out:
4470 return ret;
4474 * Updates compare related fields in sctx and simply forwards to the actual
4475 * changed_xxx functions.
4477 static int changed_cb(struct btrfs_root *left_root,
4478 struct btrfs_root *right_root,
4479 struct btrfs_path *left_path,
4480 struct btrfs_path *right_path,
4481 struct btrfs_key *key,
4482 enum btrfs_compare_tree_result result,
4483 void *ctx)
4485 int ret = 0;
4486 struct send_ctx *sctx = ctx;
4488 if (result == BTRFS_COMPARE_TREE_SAME) {
4489 if (key->type != BTRFS_INODE_REF_KEY &&
4490 key->type != BTRFS_INODE_EXTREF_KEY)
4491 return 0;
4492 ret = compare_refs(sctx, left_path, key);
4493 if (!ret)
4494 return 0;
4495 if (ret < 0)
4496 return ret;
4497 result = BTRFS_COMPARE_TREE_CHANGED;
4498 ret = 0;
4501 sctx->left_path = left_path;
4502 sctx->right_path = right_path;
4503 sctx->cmp_key = key;
4505 ret = finish_inode_if_needed(sctx, 0);
4506 if (ret < 0)
4507 goto out;
4509 /* Ignore non-FS objects */
4510 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4511 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4512 goto out;
4514 if (key->type == BTRFS_INODE_ITEM_KEY)
4515 ret = changed_inode(sctx, result);
4516 else if (key->type == BTRFS_INODE_REF_KEY ||
4517 key->type == BTRFS_INODE_EXTREF_KEY)
4518 ret = changed_ref(sctx, result);
4519 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4520 ret = changed_xattr(sctx, result);
4521 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4522 ret = changed_extent(sctx, result);
4524 out:
4525 return ret;
4528 static int full_send_tree(struct send_ctx *sctx)
4530 int ret;
4531 struct btrfs_trans_handle *trans = NULL;
4532 struct btrfs_root *send_root = sctx->send_root;
4533 struct btrfs_key key;
4534 struct btrfs_key found_key;
4535 struct btrfs_path *path;
4536 struct extent_buffer *eb;
4537 int slot;
4538 u64 start_ctransid;
4539 u64 ctransid;
4541 path = alloc_path_for_send();
4542 if (!path)
4543 return -ENOMEM;
4545 spin_lock(&send_root->root_item_lock);
4546 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4547 spin_unlock(&send_root->root_item_lock);
4549 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4550 key.type = BTRFS_INODE_ITEM_KEY;
4551 key.offset = 0;
4553 join_trans:
4555 * We need to make sure the transaction does not get committed
4556 * while we do anything on commit roots. Join a transaction to prevent
4557 * this.
4559 trans = btrfs_join_transaction(send_root);
4560 if (IS_ERR(trans)) {
4561 ret = PTR_ERR(trans);
4562 trans = NULL;
4563 goto out;
4567 * Make sure the tree has not changed after re-joining. We detect this
4568 * by comparing start_ctransid and ctransid. They should always match.
4570 spin_lock(&send_root->root_item_lock);
4571 ctransid = btrfs_root_ctransid(&send_root->root_item);
4572 spin_unlock(&send_root->root_item_lock);
4574 if (ctransid != start_ctransid) {
4575 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4576 "send was modified in between. This is "
4577 "probably a bug.\n");
4578 ret = -EIO;
4579 goto out;
4582 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4583 if (ret < 0)
4584 goto out;
4585 if (ret)
4586 goto out_finish;
4588 while (1) {
4590 * When someone want to commit while we iterate, end the
4591 * joined transaction and rejoin.
4593 if (btrfs_should_end_transaction(trans, send_root)) {
4594 ret = btrfs_end_transaction(trans, send_root);
4595 trans = NULL;
4596 if (ret < 0)
4597 goto out;
4598 btrfs_release_path(path);
4599 goto join_trans;
4602 eb = path->nodes[0];
4603 slot = path->slots[0];
4604 btrfs_item_key_to_cpu(eb, &found_key, slot);
4606 ret = changed_cb(send_root, NULL, path, NULL,
4607 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4608 if (ret < 0)
4609 goto out;
4611 key.objectid = found_key.objectid;
4612 key.type = found_key.type;
4613 key.offset = found_key.offset + 1;
4615 ret = btrfs_next_item(send_root, path);
4616 if (ret < 0)
4617 goto out;
4618 if (ret) {
4619 ret = 0;
4620 break;
4624 out_finish:
4625 ret = finish_inode_if_needed(sctx, 1);
4627 out:
4628 btrfs_free_path(path);
4629 if (trans) {
4630 if (!ret)
4631 ret = btrfs_end_transaction(trans, send_root);
4632 else
4633 btrfs_end_transaction(trans, send_root);
4635 return ret;
4638 static int send_subvol(struct send_ctx *sctx)
4640 int ret;
4642 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
4643 ret = send_header(sctx);
4644 if (ret < 0)
4645 goto out;
4648 ret = send_subvol_begin(sctx);
4649 if (ret < 0)
4650 goto out;
4652 if (sctx->parent_root) {
4653 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4654 changed_cb, sctx);
4655 if (ret < 0)
4656 goto out;
4657 ret = finish_inode_if_needed(sctx, 1);
4658 if (ret < 0)
4659 goto out;
4660 } else {
4661 ret = full_send_tree(sctx);
4662 if (ret < 0)
4663 goto out;
4666 out:
4667 free_recorded_refs(sctx);
4668 return ret;
4671 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4673 int ret = 0;
4674 struct btrfs_root *send_root;
4675 struct btrfs_root *clone_root;
4676 struct btrfs_fs_info *fs_info;
4677 struct btrfs_ioctl_send_args *arg = NULL;
4678 struct btrfs_key key;
4679 struct send_ctx *sctx = NULL;
4680 u32 i;
4681 u64 *clone_sources_tmp = NULL;
4683 if (!capable(CAP_SYS_ADMIN))
4684 return -EPERM;
4686 send_root = BTRFS_I(file_inode(mnt_file))->root;
4687 fs_info = send_root->fs_info;
4690 * This is done when we lookup the root, it should already be complete
4691 * by the time we get here.
4693 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
4696 * If we just created this root we need to make sure that the orphan
4697 * cleanup has been done and committed since we search the commit root,
4698 * so check its commit root transid with our otransid and if they match
4699 * commit the transaction to make sure everything is updated.
4701 down_read(&send_root->fs_info->extent_commit_sem);
4702 if (btrfs_header_generation(send_root->commit_root) ==
4703 btrfs_root_otransid(&send_root->root_item)) {
4704 struct btrfs_trans_handle *trans;
4706 up_read(&send_root->fs_info->extent_commit_sem);
4708 trans = btrfs_attach_transaction_barrier(send_root);
4709 if (IS_ERR(trans)) {
4710 if (PTR_ERR(trans) != -ENOENT) {
4711 ret = PTR_ERR(trans);
4712 goto out;
4714 /* ENOENT means theres no transaction */
4715 } else {
4716 ret = btrfs_commit_transaction(trans, send_root);
4717 if (ret)
4718 goto out;
4720 } else {
4721 up_read(&send_root->fs_info->extent_commit_sem);
4724 arg = memdup_user(arg_, sizeof(*arg));
4725 if (IS_ERR(arg)) {
4726 ret = PTR_ERR(arg);
4727 arg = NULL;
4728 goto out;
4731 if (!access_ok(VERIFY_READ, arg->clone_sources,
4732 sizeof(*arg->clone_sources) *
4733 arg->clone_sources_count)) {
4734 ret = -EFAULT;
4735 goto out;
4738 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
4739 ret = -EINVAL;
4740 goto out;
4743 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4744 if (!sctx) {
4745 ret = -ENOMEM;
4746 goto out;
4749 INIT_LIST_HEAD(&sctx->new_refs);
4750 INIT_LIST_HEAD(&sctx->deleted_refs);
4751 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4752 INIT_LIST_HEAD(&sctx->name_cache_list);
4754 sctx->flags = arg->flags;
4756 sctx->send_filp = fget(arg->send_fd);
4757 if (!sctx->send_filp) {
4758 ret = -EBADF;
4759 goto out;
4762 sctx->mnt = mnt_file->f_path.mnt;
4764 sctx->send_root = send_root;
4765 sctx->clone_roots_cnt = arg->clone_sources_count;
4767 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4768 sctx->send_buf = vmalloc(sctx->send_max_size);
4769 if (!sctx->send_buf) {
4770 ret = -ENOMEM;
4771 goto out;
4774 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4775 if (!sctx->read_buf) {
4776 ret = -ENOMEM;
4777 goto out;
4780 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4781 (arg->clone_sources_count + 1));
4782 if (!sctx->clone_roots) {
4783 ret = -ENOMEM;
4784 goto out;
4787 if (arg->clone_sources_count) {
4788 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4789 sizeof(*arg->clone_sources));
4790 if (!clone_sources_tmp) {
4791 ret = -ENOMEM;
4792 goto out;
4795 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4796 arg->clone_sources_count *
4797 sizeof(*arg->clone_sources));
4798 if (ret) {
4799 ret = -EFAULT;
4800 goto out;
4803 for (i = 0; i < arg->clone_sources_count; i++) {
4804 key.objectid = clone_sources_tmp[i];
4805 key.type = BTRFS_ROOT_ITEM_KEY;
4806 key.offset = (u64)-1;
4807 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4808 if (IS_ERR(clone_root)) {
4809 ret = PTR_ERR(clone_root);
4810 goto out;
4812 sctx->clone_roots[i].root = clone_root;
4814 vfree(clone_sources_tmp);
4815 clone_sources_tmp = NULL;
4818 if (arg->parent_root) {
4819 key.objectid = arg->parent_root;
4820 key.type = BTRFS_ROOT_ITEM_KEY;
4821 key.offset = (u64)-1;
4822 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4823 if (IS_ERR(sctx->parent_root)) {
4824 ret = PTR_ERR(sctx->parent_root);
4825 goto out;
4830 * Clones from send_root are allowed, but only if the clone source
4831 * is behind the current send position. This is checked while searching
4832 * for possible clone sources.
4834 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4836 /* We do a bsearch later */
4837 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4838 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4839 NULL);
4841 ret = send_subvol(sctx);
4842 if (ret < 0)
4843 goto out;
4845 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
4846 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4847 if (ret < 0)
4848 goto out;
4849 ret = send_cmd(sctx);
4850 if (ret < 0)
4851 goto out;
4854 out:
4855 kfree(arg);
4856 vfree(clone_sources_tmp);
4858 if (sctx) {
4859 if (sctx->send_filp)
4860 fput(sctx->send_filp);
4862 vfree(sctx->clone_roots);
4863 vfree(sctx->send_buf);
4864 vfree(sctx->read_buf);
4866 name_cache_free(sctx);
4868 kfree(sctx);
4871 return ret;