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