blob: dc43fed3f4bbdfd2ef4c3ccf768e36693ad37bf6 [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
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000161static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200162{
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
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000176static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200177{
178 struct fs_path *p;
179
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000180 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200181 if (!p)
182 return NULL;
183 p->reversed = 1;
184 fs_path_reset(p);
185 return p;
186}
187
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000188static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200189{
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 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000756static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200757 struct btrfs_key *found_key, int resolve,
758 iterate_inode_ref_t iterate, void *ctx)
759{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000760 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200761 struct btrfs_item *item;
762 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000763 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200764 struct btrfs_path *tmp_path;
765 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000766 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200767 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000768 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200769 u32 name_len;
770 char *start;
771 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000772 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200773 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000774 u64 dir;
775 unsigned long name_off;
776 unsigned long elem_size;
777 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200778
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000779 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200780 if (!p)
781 return -ENOMEM;
782
783 tmp_path = alloc_path_for_send();
784 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000785 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200786 return -ENOMEM;
787 }
788
Alexander Block31db9f72012-07-25 23:19:24 +0200789
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000790 if (found_key->type == BTRFS_INODE_REF_KEY) {
791 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
792 struct btrfs_inode_ref);
793 item = btrfs_item_nr(eb, slot);
794 total = btrfs_item_size(eb, item);
795 elem_size = sizeof(*iref);
796 } else {
797 ptr = btrfs_item_ptr_offset(eb, slot);
798 total = btrfs_item_size_nr(eb, slot);
799 elem_size = sizeof(*extref);
800 }
801
Alexander Block31db9f72012-07-25 23:19:24 +0200802 while (cur < total) {
803 fs_path_reset(p);
804
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000805 if (found_key->type == BTRFS_INODE_REF_KEY) {
806 iref = (struct btrfs_inode_ref *)(ptr + cur);
807 name_len = btrfs_inode_ref_name_len(eb, iref);
808 name_off = (unsigned long)(iref + 1);
809 index = btrfs_inode_ref_index(eb, iref);
810 dir = found_key->offset;
811 } else {
812 extref = (struct btrfs_inode_extref *)(ptr + cur);
813 name_len = btrfs_inode_extref_name_len(eb, extref);
814 name_off = (unsigned long)&extref->name;
815 index = btrfs_inode_extref_index(eb, extref);
816 dir = btrfs_inode_extref_parent(eb, extref);
817 }
818
Alexander Block31db9f72012-07-25 23:19:24 +0200819 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000820 start = btrfs_ref_to_path(root, tmp_path, name_len,
821 name_off, eb, dir,
822 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200823 if (IS_ERR(start)) {
824 ret = PTR_ERR(start);
825 goto out;
826 }
827 if (start < p->buf) {
828 /* overflow , try again with larger buffer */
829 ret = fs_path_ensure_buf(p,
830 p->buf_len + p->buf - start);
831 if (ret < 0)
832 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000833 start = btrfs_ref_to_path(root, tmp_path,
834 name_len, name_off,
835 eb, dir,
836 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200837 if (IS_ERR(start)) {
838 ret = PTR_ERR(start);
839 goto out;
840 }
841 BUG_ON(start < p->buf);
842 }
843 p->start = start;
844 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000845 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
846 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200847 if (ret < 0)
848 goto out;
849 }
850
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000851 cur += elem_size + name_len;
852 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200853 if (ret)
854 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200855 num++;
856 }
857
858out:
859 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000860 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200861 return ret;
862}
863
864typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
865 const char *name, int name_len,
866 const char *data, int data_len,
867 u8 type, void *ctx);
868
869/*
870 * Helper function to iterate the entries in ONE btrfs_dir_item.
871 * The iterate callback may return a non zero value to stop iteration. This can
872 * be a negative value for error codes or 1 to simply stop it.
873 *
874 * path must point to the dir item when called.
875 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000876static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200877 struct btrfs_key *found_key,
878 iterate_dir_item_t iterate, void *ctx)
879{
880 int ret = 0;
881 struct extent_buffer *eb;
882 struct btrfs_item *item;
883 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200884 struct btrfs_key di_key;
885 char *buf = NULL;
886 char *buf2 = NULL;
887 int buf_len;
888 int buf_virtual = 0;
889 u32 name_len;
890 u32 data_len;
891 u32 cur;
892 u32 len;
893 u32 total;
894 int slot;
895 int num;
896 u8 type;
897
898 buf_len = PAGE_SIZE;
899 buf = kmalloc(buf_len, GFP_NOFS);
900 if (!buf) {
901 ret = -ENOMEM;
902 goto out;
903 }
904
Alexander Block31db9f72012-07-25 23:19:24 +0200905 eb = path->nodes[0];
906 slot = path->slots[0];
907 item = btrfs_item_nr(eb, slot);
908 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
909 cur = 0;
910 len = 0;
911 total = btrfs_item_size(eb, item);
912
913 num = 0;
914 while (cur < total) {
915 name_len = btrfs_dir_name_len(eb, di);
916 data_len = btrfs_dir_data_len(eb, di);
917 type = btrfs_dir_type(eb, di);
918 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
919
920 if (name_len + data_len > buf_len) {
921 buf_len = PAGE_ALIGN(name_len + data_len);
922 if (buf_virtual) {
923 buf2 = vmalloc(buf_len);
924 if (!buf2) {
925 ret = -ENOMEM;
926 goto out;
927 }
928 vfree(buf);
929 } else {
930 buf2 = krealloc(buf, buf_len, GFP_NOFS);
931 if (!buf2) {
932 buf2 = vmalloc(buf_len);
933 if (!buf2) {
934 ret = -ENOMEM;
935 goto out;
936 }
937 kfree(buf);
938 buf_virtual = 1;
939 }
940 }
941
942 buf = buf2;
943 buf2 = NULL;
944 }
945
946 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
947 name_len + data_len);
948
949 len = sizeof(*di) + name_len + data_len;
950 di = (struct btrfs_dir_item *)((char *)di + len);
951 cur += len;
952
953 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
954 data_len, type, ctx);
955 if (ret < 0)
956 goto out;
957 if (ret) {
958 ret = 0;
959 goto out;
960 }
961
962 num++;
963 }
964
965out:
Alexander Block31db9f72012-07-25 23:19:24 +0200966 if (buf_virtual)
967 vfree(buf);
968 else
969 kfree(buf);
970 return ret;
971}
972
973static int __copy_first_ref(int num, u64 dir, int index,
974 struct fs_path *p, void *ctx)
975{
976 int ret;
977 struct fs_path *pt = ctx;
978
979 ret = fs_path_copy(pt, p);
980 if (ret < 0)
981 return ret;
982
983 /* we want the first only */
984 return 1;
985}
986
987/*
988 * Retrieve the first path of an inode. If an inode has more then one
989 * ref/hardlink, this is ignored.
990 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000991static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +0200992 u64 ino, struct fs_path *path)
993{
994 int ret;
995 struct btrfs_key key, found_key;
996 struct btrfs_path *p;
997
998 p = alloc_path_for_send();
999 if (!p)
1000 return -ENOMEM;
1001
1002 fs_path_reset(path);
1003
1004 key.objectid = ino;
1005 key.type = BTRFS_INODE_REF_KEY;
1006 key.offset = 0;
1007
1008 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1009 if (ret < 0)
1010 goto out;
1011 if (ret) {
1012 ret = 1;
1013 goto out;
1014 }
1015 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1016 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001017 (found_key.type != BTRFS_INODE_REF_KEY &&
1018 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001019 ret = -ENOENT;
1020 goto out;
1021 }
1022
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001023 ret = iterate_inode_ref(root, p, &found_key, 1,
1024 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +02001025 if (ret < 0)
1026 goto out;
1027 ret = 0;
1028
1029out:
1030 btrfs_free_path(p);
1031 return ret;
1032}
1033
1034struct backref_ctx {
1035 struct send_ctx *sctx;
1036
1037 /* number of total found references */
1038 u64 found;
1039
1040 /*
1041 * used for clones found in send_root. clones found behind cur_objectid
1042 * and cur_offset are not considered as allowed clones.
1043 */
1044 u64 cur_objectid;
1045 u64 cur_offset;
1046
1047 /* may be truncated in case it's the last extent in a file */
1048 u64 extent_len;
1049
1050 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001051 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001052};
1053
1054static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1055{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001056 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001057 struct clone_root *cr = (struct clone_root *)elt;
1058
1059 if (root < cr->root->objectid)
1060 return -1;
1061 if (root > cr->root->objectid)
1062 return 1;
1063 return 0;
1064}
1065
1066static int __clone_root_cmp_sort(const void *e1, const void *e2)
1067{
1068 struct clone_root *cr1 = (struct clone_root *)e1;
1069 struct clone_root *cr2 = (struct clone_root *)e2;
1070
1071 if (cr1->root->objectid < cr2->root->objectid)
1072 return -1;
1073 if (cr1->root->objectid > cr2->root->objectid)
1074 return 1;
1075 return 0;
1076}
1077
1078/*
1079 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001080 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001081 */
1082static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1083{
1084 struct backref_ctx *bctx = ctx_;
1085 struct clone_root *found;
1086 int ret;
1087 u64 i_size;
1088
1089 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001090 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001091 bctx->sctx->clone_roots_cnt,
1092 sizeof(struct clone_root),
1093 __clone_root_cmp_bsearch);
1094 if (!found)
1095 return 0;
1096
1097 if (found->root == bctx->sctx->send_root &&
1098 ino == bctx->cur_objectid &&
1099 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001100 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001101 }
1102
1103 /*
Alexander Block766702e2012-07-28 14:11:31 +02001104 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001105 * accept clones from these extents.
1106 */
Alexander Block85a7b332012-07-26 23:39:10 +02001107 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1108 NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001109 if (ret < 0)
1110 return ret;
1111
1112 if (offset + bctx->extent_len > i_size)
1113 return 0;
1114
1115 /*
1116 * Make sure we don't consider clones from send_root that are
1117 * behind the current inode/offset.
1118 */
1119 if (found->root == bctx->sctx->send_root) {
1120 /*
1121 * TODO for the moment we don't accept clones from the inode
1122 * that is currently send. We may change this when
1123 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1124 * file.
1125 */
1126 if (ino >= bctx->cur_objectid)
1127 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001128#if 0
1129 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001130 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001131 if (offset + bctx->extent_len > bctx->cur_offset)
1132 return 0;
1133#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001134 }
1135
1136 bctx->found++;
1137 found->found_refs++;
1138 if (ino < found->ino) {
1139 found->ino = ino;
1140 found->offset = offset;
1141 } else if (found->ino == ino) {
1142 /*
1143 * same extent found more then once in the same file.
1144 */
1145 if (found->offset > offset + bctx->extent_len)
1146 found->offset = offset;
1147 }
1148
1149 return 0;
1150}
1151
1152/*
Alexander Block766702e2012-07-28 14:11:31 +02001153 * Given an inode, offset and extent item, it finds a good clone for a clone
1154 * instruction. Returns -ENOENT when none could be found. The function makes
1155 * sure that the returned clone is usable at the point where sending is at the
1156 * moment. This means, that no clones are accepted which lie behind the current
1157 * inode+offset.
1158 *
Alexander Block31db9f72012-07-25 23:19:24 +02001159 * path must point to the extent item when called.
1160 */
1161static int find_extent_clone(struct send_ctx *sctx,
1162 struct btrfs_path *path,
1163 u64 ino, u64 data_offset,
1164 u64 ino_size,
1165 struct clone_root **found)
1166{
1167 int ret;
1168 int extent_type;
1169 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001170 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001171 u64 num_bytes;
1172 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001173 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001174 struct btrfs_file_extent_item *fi;
1175 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001176 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001177 struct clone_root *cur_clone_root;
1178 struct btrfs_key found_key;
1179 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001180 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001181 u32 i;
1182
1183 tmp_path = alloc_path_for_send();
1184 if (!tmp_path)
1185 return -ENOMEM;
1186
Alexander Block35075bb2012-07-28 12:44:34 +02001187 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1188 if (!backref_ctx) {
1189 ret = -ENOMEM;
1190 goto out;
1191 }
1192
Alexander Block31db9f72012-07-25 23:19:24 +02001193 if (data_offset >= ino_size) {
1194 /*
1195 * There may be extents that lie behind the file's size.
1196 * I at least had this in combination with snapshotting while
1197 * writing large files.
1198 */
1199 ret = 0;
1200 goto out;
1201 }
1202
1203 fi = btrfs_item_ptr(eb, path->slots[0],
1204 struct btrfs_file_extent_item);
1205 extent_type = btrfs_file_extent_type(eb, fi);
1206 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1207 ret = -ENOENT;
1208 goto out;
1209 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001210 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001211
1212 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001213 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1214 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001215 ret = -ENOENT;
1216 goto out;
1217 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001218 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001219
Liu Bo69917e42012-09-07 20:01:28 -06001220 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1221 &found_key, &flags);
Alexander Block31db9f72012-07-25 23:19:24 +02001222 btrfs_release_path(tmp_path);
1223
1224 if (ret < 0)
1225 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001226 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001227 ret = -EIO;
1228 goto out;
1229 }
1230
1231 /*
1232 * Setup the clone roots.
1233 */
1234 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1235 cur_clone_root = sctx->clone_roots + i;
1236 cur_clone_root->ino = (u64)-1;
1237 cur_clone_root->offset = 0;
1238 cur_clone_root->found_refs = 0;
1239 }
1240
Alexander Block35075bb2012-07-28 12:44:34 +02001241 backref_ctx->sctx = sctx;
1242 backref_ctx->found = 0;
1243 backref_ctx->cur_objectid = ino;
1244 backref_ctx->cur_offset = data_offset;
1245 backref_ctx->found_itself = 0;
1246 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001247
1248 /*
1249 * The last extent of a file may be too large due to page alignment.
1250 * We need to adjust extent_len in this case so that the checks in
1251 * __iterate_backrefs work.
1252 */
1253 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001254 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001255
1256 /*
1257 * Now collect all backrefs.
1258 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001259 if (compressed == BTRFS_COMPRESS_NONE)
1260 extent_item_pos = logical - found_key.objectid;
1261 else
1262 extent_item_pos = 0;
1263
Alexander Block31db9f72012-07-25 23:19:24 +02001264 extent_item_pos = logical - found_key.objectid;
1265 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1266 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001267 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001268
Alexander Block31db9f72012-07-25 23:19:24 +02001269 if (ret < 0)
1270 goto out;
1271
Alexander Block35075bb2012-07-28 12:44:34 +02001272 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001273 /* found a bug in backref code? */
1274 ret = -EIO;
1275 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1276 "send_root. inode=%llu, offset=%llu, "
Chris Mason74dd17f2012-08-07 16:25:13 -04001277 "disk_byte=%llu found extent=%llu\n",
1278 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001279 goto out;
1280 }
1281
1282verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1283 "ino=%llu, "
1284 "num_bytes=%llu, logical=%llu\n",
1285 data_offset, ino, num_bytes, logical);
1286
Alexander Block35075bb2012-07-28 12:44:34 +02001287 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001288 verbose_printk("btrfs: no clones found\n");
1289
1290 cur_clone_root = NULL;
1291 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1292 if (sctx->clone_roots[i].found_refs) {
1293 if (!cur_clone_root)
1294 cur_clone_root = sctx->clone_roots + i;
1295 else if (sctx->clone_roots[i].root == sctx->send_root)
1296 /* prefer clones from send_root over others */
1297 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001298 }
1299
1300 }
1301
1302 if (cur_clone_root) {
1303 *found = cur_clone_root;
1304 ret = 0;
1305 } else {
1306 ret = -ENOENT;
1307 }
1308
1309out:
1310 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001311 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001312 return ret;
1313}
1314
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001315static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001316 u64 ino,
1317 struct fs_path *dest)
1318{
1319 int ret;
1320 struct btrfs_path *path;
1321 struct btrfs_key key;
1322 struct btrfs_file_extent_item *ei;
1323 u8 type;
1324 u8 compression;
1325 unsigned long off;
1326 int len;
1327
1328 path = alloc_path_for_send();
1329 if (!path)
1330 return -ENOMEM;
1331
1332 key.objectid = ino;
1333 key.type = BTRFS_EXTENT_DATA_KEY;
1334 key.offset = 0;
1335 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1336 if (ret < 0)
1337 goto out;
1338 BUG_ON(ret);
1339
1340 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1341 struct btrfs_file_extent_item);
1342 type = btrfs_file_extent_type(path->nodes[0], ei);
1343 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1344 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1345 BUG_ON(compression);
1346
1347 off = btrfs_file_extent_inline_start(ei);
1348 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1349
1350 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001351
1352out:
1353 btrfs_free_path(path);
1354 return ret;
1355}
1356
1357/*
1358 * Helper function to generate a file name that is unique in the root of
1359 * send_root and parent_root. This is used to generate names for orphan inodes.
1360 */
1361static int gen_unique_name(struct send_ctx *sctx,
1362 u64 ino, u64 gen,
1363 struct fs_path *dest)
1364{
1365 int ret = 0;
1366 struct btrfs_path *path;
1367 struct btrfs_dir_item *di;
1368 char tmp[64];
1369 int len;
1370 u64 idx = 0;
1371
1372 path = alloc_path_for_send();
1373 if (!path)
1374 return -ENOMEM;
1375
1376 while (1) {
1377 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1378 ino, gen, idx);
1379 if (len >= sizeof(tmp)) {
1380 /* should really not happen */
1381 ret = -EOVERFLOW;
1382 goto out;
1383 }
1384
1385 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1386 path, BTRFS_FIRST_FREE_OBJECTID,
1387 tmp, strlen(tmp), 0);
1388 btrfs_release_path(path);
1389 if (IS_ERR(di)) {
1390 ret = PTR_ERR(di);
1391 goto out;
1392 }
1393 if (di) {
1394 /* not unique, try again */
1395 idx++;
1396 continue;
1397 }
1398
1399 if (!sctx->parent_root) {
1400 /* unique */
1401 ret = 0;
1402 break;
1403 }
1404
1405 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1406 path, BTRFS_FIRST_FREE_OBJECTID,
1407 tmp, strlen(tmp), 0);
1408 btrfs_release_path(path);
1409 if (IS_ERR(di)) {
1410 ret = PTR_ERR(di);
1411 goto out;
1412 }
1413 if (di) {
1414 /* not unique, try again */
1415 idx++;
1416 continue;
1417 }
1418 /* unique */
1419 break;
1420 }
1421
1422 ret = fs_path_add(dest, tmp, strlen(tmp));
1423
1424out:
1425 btrfs_free_path(path);
1426 return ret;
1427}
1428
1429enum inode_state {
1430 inode_state_no_change,
1431 inode_state_will_create,
1432 inode_state_did_create,
1433 inode_state_will_delete,
1434 inode_state_did_delete,
1435};
1436
1437static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1438{
1439 int ret;
1440 int left_ret;
1441 int right_ret;
1442 u64 left_gen;
1443 u64 right_gen;
1444
1445 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001446 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001447 if (ret < 0 && ret != -ENOENT)
1448 goto out;
1449 left_ret = ret;
1450
1451 if (!sctx->parent_root) {
1452 right_ret = -ENOENT;
1453 } else {
1454 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001455 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001456 if (ret < 0 && ret != -ENOENT)
1457 goto out;
1458 right_ret = ret;
1459 }
1460
1461 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001462 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001463 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001464 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001465 if (ino < sctx->send_progress)
1466 ret = inode_state_did_create;
1467 else
1468 ret = inode_state_will_create;
1469 } else if (right_gen == gen) {
1470 if (ino < sctx->send_progress)
1471 ret = inode_state_did_delete;
1472 else
1473 ret = inode_state_will_delete;
1474 } else {
1475 ret = -ENOENT;
1476 }
1477 } else if (!left_ret) {
1478 if (left_gen == gen) {
1479 if (ino < sctx->send_progress)
1480 ret = inode_state_did_create;
1481 else
1482 ret = inode_state_will_create;
1483 } else {
1484 ret = -ENOENT;
1485 }
1486 } else if (!right_ret) {
1487 if (right_gen == gen) {
1488 if (ino < sctx->send_progress)
1489 ret = inode_state_did_delete;
1490 else
1491 ret = inode_state_will_delete;
1492 } else {
1493 ret = -ENOENT;
1494 }
1495 } else {
1496 ret = -ENOENT;
1497 }
1498
1499out:
1500 return ret;
1501}
1502
1503static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1504{
1505 int ret;
1506
1507 ret = get_cur_inode_state(sctx, ino, gen);
1508 if (ret < 0)
1509 goto out;
1510
1511 if (ret == inode_state_no_change ||
1512 ret == inode_state_did_create ||
1513 ret == inode_state_will_delete)
1514 ret = 1;
1515 else
1516 ret = 0;
1517
1518out:
1519 return ret;
1520}
1521
1522/*
1523 * Helper function to lookup a dir item in a dir.
1524 */
1525static int lookup_dir_item_inode(struct btrfs_root *root,
1526 u64 dir, const char *name, int name_len,
1527 u64 *found_inode,
1528 u8 *found_type)
1529{
1530 int ret = 0;
1531 struct btrfs_dir_item *di;
1532 struct btrfs_key key;
1533 struct btrfs_path *path;
1534
1535 path = alloc_path_for_send();
1536 if (!path)
1537 return -ENOMEM;
1538
1539 di = btrfs_lookup_dir_item(NULL, root, path,
1540 dir, name, name_len, 0);
1541 if (!di) {
1542 ret = -ENOENT;
1543 goto out;
1544 }
1545 if (IS_ERR(di)) {
1546 ret = PTR_ERR(di);
1547 goto out;
1548 }
1549 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1550 *found_inode = key.objectid;
1551 *found_type = btrfs_dir_type(path->nodes[0], di);
1552
1553out:
1554 btrfs_free_path(path);
1555 return ret;
1556}
1557
Alexander Block766702e2012-07-28 14:11:31 +02001558/*
1559 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1560 * generation of the parent dir and the name of the dir entry.
1561 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001562static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001563 u64 *dir, u64 *dir_gen, struct fs_path *name)
1564{
1565 int ret;
1566 struct btrfs_key key;
1567 struct btrfs_key found_key;
1568 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001569 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001570 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001571
1572 path = alloc_path_for_send();
1573 if (!path)
1574 return -ENOMEM;
1575
1576 key.objectid = ino;
1577 key.type = BTRFS_INODE_REF_KEY;
1578 key.offset = 0;
1579
1580 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1581 if (ret < 0)
1582 goto out;
1583 if (!ret)
1584 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1585 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001586 if (ret || found_key.objectid != ino ||
1587 (found_key.type != BTRFS_INODE_REF_KEY &&
1588 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001589 ret = -ENOENT;
1590 goto out;
1591 }
1592
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001593 if (key.type == BTRFS_INODE_REF_KEY) {
1594 struct btrfs_inode_ref *iref;
1595 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1596 struct btrfs_inode_ref);
1597 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1598 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1599 (unsigned long)(iref + 1),
1600 len);
1601 parent_dir = found_key.offset;
1602 } else {
1603 struct btrfs_inode_extref *extref;
1604 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1605 struct btrfs_inode_extref);
1606 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1607 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1608 (unsigned long)&extref->name, len);
1609 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1610 }
Alexander Block31db9f72012-07-25 23:19:24 +02001611 if (ret < 0)
1612 goto out;
1613 btrfs_release_path(path);
1614
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001615 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001616 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001617 if (ret < 0)
1618 goto out;
1619
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001620 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001621
1622out:
1623 btrfs_free_path(path);
1624 return ret;
1625}
1626
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001627static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001628 u64 ino, u64 dir,
1629 const char *name, int name_len)
1630{
1631 int ret;
1632 struct fs_path *tmp_name;
1633 u64 tmp_dir;
1634 u64 tmp_dir_gen;
1635
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001636 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001637 if (!tmp_name)
1638 return -ENOMEM;
1639
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001640 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001641 if (ret < 0)
1642 goto out;
1643
Alexander Blockb9291af2012-07-28 11:07:18 +02001644 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001645 ret = 0;
1646 goto out;
1647 }
1648
Alexander Blocke938c8a2012-07-28 16:33:49 +02001649 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001650
1651out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001652 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001653 return ret;
1654}
1655
Alexander Block766702e2012-07-28 14:11:31 +02001656/*
1657 * Used by process_recorded_refs to determine if a new ref would overwrite an
1658 * already existing ref. In case it detects an overwrite, it returns the
1659 * inode/gen in who_ino/who_gen.
1660 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1661 * to make sure later references to the overwritten inode are possible.
1662 * Orphanizing is however only required for the first ref of an inode.
1663 * process_recorded_refs does an additional is_first_ref check to see if
1664 * orphanizing is really required.
1665 */
Alexander Block31db9f72012-07-25 23:19:24 +02001666static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1667 const char *name, int name_len,
1668 u64 *who_ino, u64 *who_gen)
1669{
1670 int ret = 0;
1671 u64 other_inode = 0;
1672 u8 other_type = 0;
1673
1674 if (!sctx->parent_root)
1675 goto out;
1676
1677 ret = is_inode_existent(sctx, dir, dir_gen);
1678 if (ret <= 0)
1679 goto out;
1680
1681 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1682 &other_inode, &other_type);
1683 if (ret < 0 && ret != -ENOENT)
1684 goto out;
1685 if (ret) {
1686 ret = 0;
1687 goto out;
1688 }
1689
Alexander Block766702e2012-07-28 14:11:31 +02001690 /*
1691 * Check if the overwritten ref was already processed. If yes, the ref
1692 * was already unlinked/moved, so we can safely assume that we will not
1693 * overwrite anything at this point in time.
1694 */
Alexander Block31db9f72012-07-25 23:19:24 +02001695 if (other_inode > sctx->send_progress) {
1696 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001697 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001698 if (ret < 0)
1699 goto out;
1700
1701 ret = 1;
1702 *who_ino = other_inode;
1703 } else {
1704 ret = 0;
1705 }
1706
1707out:
1708 return ret;
1709}
1710
Alexander Block766702e2012-07-28 14:11:31 +02001711/*
1712 * Checks if the ref was overwritten by an already processed inode. This is
1713 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1714 * thus the orphan name needs be used.
1715 * process_recorded_refs also uses it to avoid unlinking of refs that were
1716 * overwritten.
1717 */
Alexander Block31db9f72012-07-25 23:19:24 +02001718static int did_overwrite_ref(struct send_ctx *sctx,
1719 u64 dir, u64 dir_gen,
1720 u64 ino, u64 ino_gen,
1721 const char *name, int name_len)
1722{
1723 int ret = 0;
1724 u64 gen;
1725 u64 ow_inode;
1726 u8 other_type;
1727
1728 if (!sctx->parent_root)
1729 goto out;
1730
1731 ret = is_inode_existent(sctx, dir, dir_gen);
1732 if (ret <= 0)
1733 goto out;
1734
1735 /* check if the ref was overwritten by another ref */
1736 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1737 &ow_inode, &other_type);
1738 if (ret < 0 && ret != -ENOENT)
1739 goto out;
1740 if (ret) {
1741 /* was never and will never be overwritten */
1742 ret = 0;
1743 goto out;
1744 }
1745
1746 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001747 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001748 if (ret < 0)
1749 goto out;
1750
1751 if (ow_inode == ino && gen == ino_gen) {
1752 ret = 0;
1753 goto out;
1754 }
1755
1756 /* we know that it is or will be overwritten. check this now */
1757 if (ow_inode < sctx->send_progress)
1758 ret = 1;
1759 else
1760 ret = 0;
1761
1762out:
1763 return ret;
1764}
1765
Alexander Block766702e2012-07-28 14:11:31 +02001766/*
1767 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1768 * that got overwritten. This is used by process_recorded_refs to determine
1769 * if it has to use the path as returned by get_cur_path or the orphan name.
1770 */
Alexander Block31db9f72012-07-25 23:19:24 +02001771static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1772{
1773 int ret = 0;
1774 struct fs_path *name = NULL;
1775 u64 dir;
1776 u64 dir_gen;
1777
1778 if (!sctx->parent_root)
1779 goto out;
1780
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001781 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001782 if (!name)
1783 return -ENOMEM;
1784
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001785 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001786 if (ret < 0)
1787 goto out;
1788
1789 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1790 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001791
1792out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001793 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001794 return ret;
1795}
1796
Alexander Block766702e2012-07-28 14:11:31 +02001797/*
1798 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1799 * so we need to do some special handling in case we have clashes. This function
1800 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001801 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001802 */
Alexander Block31db9f72012-07-25 23:19:24 +02001803static int name_cache_insert(struct send_ctx *sctx,
1804 struct name_cache_entry *nce)
1805{
1806 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001807 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001808
Alexander Block7e0926f2012-07-28 14:20:58 +02001809 nce_head = radix_tree_lookup(&sctx->name_cache,
1810 (unsigned long)nce->ino);
1811 if (!nce_head) {
1812 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001813 if (!nce_head) {
1814 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001815 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001816 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001817 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001818
Alexander Block7e0926f2012-07-28 14:20:58 +02001819 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001820 if (ret < 0) {
1821 kfree(nce_head);
1822 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001823 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001824 }
Alexander Block31db9f72012-07-25 23:19:24 +02001825 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001826 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001827 list_add_tail(&nce->list, &sctx->name_cache_list);
1828 sctx->name_cache_size++;
1829
1830 return ret;
1831}
1832
1833static void name_cache_delete(struct send_ctx *sctx,
1834 struct name_cache_entry *nce)
1835{
Alexander Block7e0926f2012-07-28 14:20:58 +02001836 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001837
Alexander Block7e0926f2012-07-28 14:20:58 +02001838 nce_head = radix_tree_lookup(&sctx->name_cache,
1839 (unsigned long)nce->ino);
1840 BUG_ON(!nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001841
Alexander Block7e0926f2012-07-28 14:20:58 +02001842 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001843 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001844 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001845
1846 if (list_empty(nce_head)) {
1847 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1848 kfree(nce_head);
1849 }
Alexander Block31db9f72012-07-25 23:19:24 +02001850}
1851
1852static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1853 u64 ino, u64 gen)
1854{
Alexander Block7e0926f2012-07-28 14:20:58 +02001855 struct list_head *nce_head;
1856 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001857
Alexander Block7e0926f2012-07-28 14:20:58 +02001858 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1859 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001860 return NULL;
1861
Alexander Block7e0926f2012-07-28 14:20:58 +02001862 list_for_each_entry(cur, nce_head, radix_list) {
1863 if (cur->ino == ino && cur->gen == gen)
1864 return cur;
1865 }
Alexander Block31db9f72012-07-25 23:19:24 +02001866 return NULL;
1867}
1868
Alexander Block766702e2012-07-28 14:11:31 +02001869/*
1870 * Removes the entry from the list and adds it back to the end. This marks the
1871 * entry as recently used so that name_cache_clean_unused does not remove it.
1872 */
Alexander Block31db9f72012-07-25 23:19:24 +02001873static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1874{
1875 list_del(&nce->list);
1876 list_add_tail(&nce->list, &sctx->name_cache_list);
1877}
1878
Alexander Block766702e2012-07-28 14:11:31 +02001879/*
1880 * Remove some entries from the beginning of name_cache_list.
1881 */
Alexander Block31db9f72012-07-25 23:19:24 +02001882static void name_cache_clean_unused(struct send_ctx *sctx)
1883{
1884 struct name_cache_entry *nce;
1885
1886 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1887 return;
1888
1889 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1890 nce = list_entry(sctx->name_cache_list.next,
1891 struct name_cache_entry, list);
1892 name_cache_delete(sctx, nce);
1893 kfree(nce);
1894 }
1895}
1896
1897static void name_cache_free(struct send_ctx *sctx)
1898{
1899 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02001900
Alexander Blocke938c8a2012-07-28 16:33:49 +02001901 while (!list_empty(&sctx->name_cache_list)) {
1902 nce = list_entry(sctx->name_cache_list.next,
1903 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02001904 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02001905 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001906 }
1907}
1908
Alexander Block766702e2012-07-28 14:11:31 +02001909/*
1910 * Used by get_cur_path for each ref up to the root.
1911 * Returns 0 if it succeeded.
1912 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1913 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1914 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1915 * Returns <0 in case of error.
1916 */
Alexander Block31db9f72012-07-25 23:19:24 +02001917static int __get_cur_name_and_parent(struct send_ctx *sctx,
1918 u64 ino, u64 gen,
1919 u64 *parent_ino,
1920 u64 *parent_gen,
1921 struct fs_path *dest)
1922{
1923 int ret;
1924 int nce_ret;
1925 struct btrfs_path *path = NULL;
1926 struct name_cache_entry *nce = NULL;
1927
Alexander Block766702e2012-07-28 14:11:31 +02001928 /*
1929 * First check if we already did a call to this function with the same
1930 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1931 * return the cached result.
1932 */
Alexander Block31db9f72012-07-25 23:19:24 +02001933 nce = name_cache_search(sctx, ino, gen);
1934 if (nce) {
1935 if (ino < sctx->send_progress && nce->need_later_update) {
1936 name_cache_delete(sctx, nce);
1937 kfree(nce);
1938 nce = NULL;
1939 } else {
1940 name_cache_used(sctx, nce);
1941 *parent_ino = nce->parent_ino;
1942 *parent_gen = nce->parent_gen;
1943 ret = fs_path_add(dest, nce->name, nce->name_len);
1944 if (ret < 0)
1945 goto out;
1946 ret = nce->ret;
1947 goto out;
1948 }
1949 }
1950
1951 path = alloc_path_for_send();
1952 if (!path)
1953 return -ENOMEM;
1954
Alexander Block766702e2012-07-28 14:11:31 +02001955 /*
1956 * If the inode is not existent yet, add the orphan name and return 1.
1957 * This should only happen for the parent dir that we determine in
1958 * __record_new_ref
1959 */
Alexander Block31db9f72012-07-25 23:19:24 +02001960 ret = is_inode_existent(sctx, ino, gen);
1961 if (ret < 0)
1962 goto out;
1963
1964 if (!ret) {
1965 ret = gen_unique_name(sctx, ino, gen, dest);
1966 if (ret < 0)
1967 goto out;
1968 ret = 1;
1969 goto out_cache;
1970 }
1971
Alexander Block766702e2012-07-28 14:11:31 +02001972 /*
1973 * Depending on whether the inode was already processed or not, use
1974 * send_root or parent_root for ref lookup.
1975 */
Alexander Block31db9f72012-07-25 23:19:24 +02001976 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001977 ret = get_first_ref(sctx->send_root, ino,
1978 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02001979 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001980 ret = get_first_ref(sctx->parent_root, ino,
1981 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02001982 if (ret < 0)
1983 goto out;
1984
Alexander Block766702e2012-07-28 14:11:31 +02001985 /*
1986 * Check if the ref was overwritten by an inode's ref that was processed
1987 * earlier. If yes, treat as orphan and return 1.
1988 */
Alexander Block31db9f72012-07-25 23:19:24 +02001989 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1990 dest->start, dest->end - dest->start);
1991 if (ret < 0)
1992 goto out;
1993 if (ret) {
1994 fs_path_reset(dest);
1995 ret = gen_unique_name(sctx, ino, gen, dest);
1996 if (ret < 0)
1997 goto out;
1998 ret = 1;
1999 }
2000
2001out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02002002 /*
2003 * Store the result of the lookup in the name cache.
2004 */
Alexander Block31db9f72012-07-25 23:19:24 +02002005 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2006 if (!nce) {
2007 ret = -ENOMEM;
2008 goto out;
2009 }
2010
2011 nce->ino = ino;
2012 nce->gen = gen;
2013 nce->parent_ino = *parent_ino;
2014 nce->parent_gen = *parent_gen;
2015 nce->name_len = fs_path_len(dest);
2016 nce->ret = ret;
2017 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002018
2019 if (ino < sctx->send_progress)
2020 nce->need_later_update = 0;
2021 else
2022 nce->need_later_update = 1;
2023
2024 nce_ret = name_cache_insert(sctx, nce);
2025 if (nce_ret < 0)
2026 ret = nce_ret;
2027 name_cache_clean_unused(sctx);
2028
2029out:
2030 btrfs_free_path(path);
2031 return ret;
2032}
2033
2034/*
2035 * Magic happens here. This function returns the first ref to an inode as it
2036 * would look like while receiving the stream at this point in time.
2037 * We walk the path up to the root. For every inode in between, we check if it
2038 * was already processed/sent. If yes, we continue with the parent as found
2039 * in send_root. If not, we continue with the parent as found in parent_root.
2040 * If we encounter an inode that was deleted at this point in time, we use the
2041 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2042 * that were not created yet and overwritten inodes/refs.
2043 *
2044 * When do we have have orphan inodes:
2045 * 1. When an inode is freshly created and thus no valid refs are available yet
2046 * 2. When a directory lost all it's refs (deleted) but still has dir items
2047 * inside which were not processed yet (pending for move/delete). If anyone
2048 * tried to get the path to the dir items, it would get a path inside that
2049 * orphan directory.
2050 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2051 * of an unprocessed inode. If in that case the first ref would be
2052 * overwritten, the overwritten inode gets "orphanized". Later when we
2053 * process this overwritten inode, it is restored at a new place by moving
2054 * the orphan inode.
2055 *
2056 * sctx->send_progress tells this function at which point in time receiving
2057 * would be.
2058 */
2059static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2060 struct fs_path *dest)
2061{
2062 int ret = 0;
2063 struct fs_path *name = NULL;
2064 u64 parent_inode = 0;
2065 u64 parent_gen = 0;
2066 int stop = 0;
2067
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002068 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002069 if (!name) {
2070 ret = -ENOMEM;
2071 goto out;
2072 }
2073
2074 dest->reversed = 1;
2075 fs_path_reset(dest);
2076
2077 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2078 fs_path_reset(name);
2079
2080 ret = __get_cur_name_and_parent(sctx, ino, gen,
2081 &parent_inode, &parent_gen, name);
2082 if (ret < 0)
2083 goto out;
2084 if (ret)
2085 stop = 1;
2086
2087 ret = fs_path_add_path(dest, name);
2088 if (ret < 0)
2089 goto out;
2090
2091 ino = parent_inode;
2092 gen = parent_gen;
2093 }
2094
2095out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002096 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002097 if (!ret)
2098 fs_path_unreverse(dest);
2099 return ret;
2100}
2101
2102/*
2103 * Called for regular files when sending extents data. Opens a struct file
2104 * to read from the file.
2105 */
2106static int open_cur_inode_file(struct send_ctx *sctx)
2107{
2108 int ret = 0;
2109 struct btrfs_key key;
Linus Torvaldse2aed8d2012-07-26 14:48:55 -07002110 struct path path;
Alexander Block31db9f72012-07-25 23:19:24 +02002111 struct inode *inode;
2112 struct dentry *dentry;
2113 struct file *filp;
2114 int new = 0;
2115
2116 if (sctx->cur_inode_filp)
2117 goto out;
2118
2119 key.objectid = sctx->cur_ino;
2120 key.type = BTRFS_INODE_ITEM_KEY;
2121 key.offset = 0;
2122
2123 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2124 &new);
2125 if (IS_ERR(inode)) {
2126 ret = PTR_ERR(inode);
2127 goto out;
2128 }
2129
2130 dentry = d_obtain_alias(inode);
2131 inode = NULL;
2132 if (IS_ERR(dentry)) {
2133 ret = PTR_ERR(dentry);
2134 goto out;
2135 }
2136
Linus Torvaldse2aed8d2012-07-26 14:48:55 -07002137 path.mnt = sctx->mnt;
2138 path.dentry = dentry;
2139 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2140 dput(dentry);
Alexander Block31db9f72012-07-25 23:19:24 +02002141 dentry = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02002142 if (IS_ERR(filp)) {
2143 ret = PTR_ERR(filp);
2144 goto out;
2145 }
2146 sctx->cur_inode_filp = filp;
2147
2148out:
2149 /*
2150 * no xxxput required here as every vfs op
2151 * does it by itself on failure
2152 */
2153 return ret;
2154}
2155
2156/*
2157 * Closes the struct file that was created in open_cur_inode_file
2158 */
2159static int close_cur_inode_file(struct send_ctx *sctx)
2160{
2161 int ret = 0;
2162
2163 if (!sctx->cur_inode_filp)
2164 goto out;
2165
2166 ret = filp_close(sctx->cur_inode_filp, NULL);
2167 sctx->cur_inode_filp = NULL;
2168
2169out:
2170 return ret;
2171}
2172
2173/*
2174 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2175 */
2176static int send_subvol_begin(struct send_ctx *sctx)
2177{
2178 int ret;
2179 struct btrfs_root *send_root = sctx->send_root;
2180 struct btrfs_root *parent_root = sctx->parent_root;
2181 struct btrfs_path *path;
2182 struct btrfs_key key;
2183 struct btrfs_root_ref *ref;
2184 struct extent_buffer *leaf;
2185 char *name = NULL;
2186 int namelen;
2187
2188 path = alloc_path_for_send();
2189 if (!path)
2190 return -ENOMEM;
2191
2192 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2193 if (!name) {
2194 btrfs_free_path(path);
2195 return -ENOMEM;
2196 }
2197
2198 key.objectid = send_root->objectid;
2199 key.type = BTRFS_ROOT_BACKREF_KEY;
2200 key.offset = 0;
2201
2202 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2203 &key, path, 1, 0);
2204 if (ret < 0)
2205 goto out;
2206 if (ret) {
2207 ret = -ENOENT;
2208 goto out;
2209 }
2210
2211 leaf = path->nodes[0];
2212 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2213 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2214 key.objectid != send_root->objectid) {
2215 ret = -ENOENT;
2216 goto out;
2217 }
2218 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2219 namelen = btrfs_root_ref_name_len(leaf, ref);
2220 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2221 btrfs_release_path(path);
2222
Alexander Block31db9f72012-07-25 23:19:24 +02002223 if (parent_root) {
2224 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2225 if (ret < 0)
2226 goto out;
2227 } else {
2228 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2229 if (ret < 0)
2230 goto out;
2231 }
2232
2233 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2234 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2235 sctx->send_root->root_item.uuid);
2236 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2237 sctx->send_root->root_item.ctransid);
2238 if (parent_root) {
2239 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2240 sctx->parent_root->root_item.uuid);
2241 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2242 sctx->parent_root->root_item.ctransid);
2243 }
2244
2245 ret = send_cmd(sctx);
2246
2247tlv_put_failure:
2248out:
2249 btrfs_free_path(path);
2250 kfree(name);
2251 return ret;
2252}
2253
2254static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2255{
2256 int ret = 0;
2257 struct fs_path *p;
2258
2259verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2260
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002261 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002262 if (!p)
2263 return -ENOMEM;
2264
2265 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2266 if (ret < 0)
2267 goto out;
2268
2269 ret = get_cur_path(sctx, ino, gen, p);
2270 if (ret < 0)
2271 goto out;
2272 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2273 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2274
2275 ret = send_cmd(sctx);
2276
2277tlv_put_failure:
2278out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002279 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002280 return ret;
2281}
2282
2283static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2284{
2285 int ret = 0;
2286 struct fs_path *p;
2287
2288verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2289
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002290 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002291 if (!p)
2292 return -ENOMEM;
2293
2294 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2295 if (ret < 0)
2296 goto out;
2297
2298 ret = get_cur_path(sctx, ino, gen, p);
2299 if (ret < 0)
2300 goto out;
2301 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2302 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2303
2304 ret = send_cmd(sctx);
2305
2306tlv_put_failure:
2307out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002308 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002309 return ret;
2310}
2311
2312static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2313{
2314 int ret = 0;
2315 struct fs_path *p;
2316
2317verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2318
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002319 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002320 if (!p)
2321 return -ENOMEM;
2322
2323 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2324 if (ret < 0)
2325 goto out;
2326
2327 ret = get_cur_path(sctx, ino, gen, p);
2328 if (ret < 0)
2329 goto out;
2330 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2331 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2332 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2333
2334 ret = send_cmd(sctx);
2335
2336tlv_put_failure:
2337out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002338 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002339 return ret;
2340}
2341
2342static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2343{
2344 int ret = 0;
2345 struct fs_path *p = NULL;
2346 struct btrfs_inode_item *ii;
2347 struct btrfs_path *path = NULL;
2348 struct extent_buffer *eb;
2349 struct btrfs_key key;
2350 int slot;
2351
2352verbose_printk("btrfs: send_utimes %llu\n", ino);
2353
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002354 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002355 if (!p)
2356 return -ENOMEM;
2357
2358 path = alloc_path_for_send();
2359 if (!path) {
2360 ret = -ENOMEM;
2361 goto out;
2362 }
2363
2364 key.objectid = ino;
2365 key.type = BTRFS_INODE_ITEM_KEY;
2366 key.offset = 0;
2367 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2368 if (ret < 0)
2369 goto out;
2370
2371 eb = path->nodes[0];
2372 slot = path->slots[0];
2373 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2374
2375 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2376 if (ret < 0)
2377 goto out;
2378
2379 ret = get_cur_path(sctx, ino, gen, p);
2380 if (ret < 0)
2381 goto out;
2382 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2383 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2384 btrfs_inode_atime(ii));
2385 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2386 btrfs_inode_mtime(ii));
2387 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2388 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002389 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002390
2391 ret = send_cmd(sctx);
2392
2393tlv_put_failure:
2394out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002395 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002396 btrfs_free_path(path);
2397 return ret;
2398}
2399
2400/*
2401 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2402 * a valid path yet because we did not process the refs yet. So, the inode
2403 * is created as orphan.
2404 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002405static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002406{
2407 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002408 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002409 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002410 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002411 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002412 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002413
Alexander Block1f4692d2012-07-28 10:42:24 +02002414verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002415
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002416 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002417 if (!p)
2418 return -ENOMEM;
2419
Alexander Block1f4692d2012-07-28 10:42:24 +02002420 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2421 NULL, &rdev);
2422 if (ret < 0)
2423 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002424
Alexander Blocke938c8a2012-07-28 16:33:49 +02002425 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002426 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002427 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002428 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002429 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002430 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002431 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002432 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002433 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002434 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002435 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002436 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002437 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002438 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2439 (int)(mode & S_IFMT));
2440 ret = -ENOTSUPP;
2441 goto out;
2442 }
2443
2444 ret = begin_cmd(sctx, cmd);
2445 if (ret < 0)
2446 goto out;
2447
Alexander Block1f4692d2012-07-28 10:42:24 +02002448 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002449 if (ret < 0)
2450 goto out;
2451
2452 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002453 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002454
2455 if (S_ISLNK(mode)) {
2456 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002457 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002458 if (ret < 0)
2459 goto out;
2460 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2461 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2462 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002463 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2464 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002465 }
2466
2467 ret = send_cmd(sctx);
2468 if (ret < 0)
2469 goto out;
2470
2471
2472tlv_put_failure:
2473out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002474 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002475 return ret;
2476}
2477
Alexander Block1f4692d2012-07-28 10:42:24 +02002478/*
2479 * We need some special handling for inodes that get processed before the parent
2480 * directory got created. See process_recorded_refs for details.
2481 * This function does the check if we already created the dir out of order.
2482 */
2483static int did_create_dir(struct send_ctx *sctx, u64 dir)
2484{
2485 int ret = 0;
2486 struct btrfs_path *path = NULL;
2487 struct btrfs_key key;
2488 struct btrfs_key found_key;
2489 struct btrfs_key di_key;
2490 struct extent_buffer *eb;
2491 struct btrfs_dir_item *di;
2492 int slot;
2493
2494 path = alloc_path_for_send();
2495 if (!path) {
2496 ret = -ENOMEM;
2497 goto out;
2498 }
2499
2500 key.objectid = dir;
2501 key.type = BTRFS_DIR_INDEX_KEY;
2502 key.offset = 0;
2503 while (1) {
2504 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2505 1, 0);
2506 if (ret < 0)
2507 goto out;
2508 if (!ret) {
2509 eb = path->nodes[0];
2510 slot = path->slots[0];
2511 btrfs_item_key_to_cpu(eb, &found_key, slot);
2512 }
2513 if (ret || found_key.objectid != key.objectid ||
2514 found_key.type != key.type) {
2515 ret = 0;
2516 goto out;
2517 }
2518
2519 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2520 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2521
2522 if (di_key.objectid < sctx->send_progress) {
2523 ret = 1;
2524 goto out;
2525 }
2526
2527 key.offset = found_key.offset + 1;
2528 btrfs_release_path(path);
2529 }
2530
2531out:
2532 btrfs_free_path(path);
2533 return ret;
2534}
2535
2536/*
2537 * Only creates the inode if it is:
2538 * 1. Not a directory
2539 * 2. Or a directory which was not created already due to out of order
2540 * directories. See did_create_dir and process_recorded_refs for details.
2541 */
2542static int send_create_inode_if_needed(struct send_ctx *sctx)
2543{
2544 int ret;
2545
2546 if (S_ISDIR(sctx->cur_inode_mode)) {
2547 ret = did_create_dir(sctx, sctx->cur_ino);
2548 if (ret < 0)
2549 goto out;
2550 if (ret) {
2551 ret = 0;
2552 goto out;
2553 }
2554 }
2555
2556 ret = send_create_inode(sctx, sctx->cur_ino);
2557 if (ret < 0)
2558 goto out;
2559
2560out:
2561 return ret;
2562}
2563
Alexander Block31db9f72012-07-25 23:19:24 +02002564struct recorded_ref {
2565 struct list_head list;
2566 char *dir_path;
2567 char *name;
2568 struct fs_path *full_path;
2569 u64 dir;
2570 u64 dir_gen;
2571 int dir_path_len;
2572 int name_len;
2573};
2574
2575/*
2576 * We need to process new refs before deleted refs, but compare_tree gives us
2577 * everything mixed. So we first record all refs and later process them.
2578 * This function is a helper to record one ref.
2579 */
2580static int record_ref(struct list_head *head, u64 dir,
2581 u64 dir_gen, struct fs_path *path)
2582{
2583 struct recorded_ref *ref;
2584 char *tmp;
2585
2586 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2587 if (!ref)
2588 return -ENOMEM;
2589
2590 ref->dir = dir;
2591 ref->dir_gen = dir_gen;
2592 ref->full_path = path;
2593
2594 tmp = strrchr(ref->full_path->start, '/');
2595 if (!tmp) {
2596 ref->name_len = ref->full_path->end - ref->full_path->start;
2597 ref->name = ref->full_path->start;
2598 ref->dir_path_len = 0;
2599 ref->dir_path = ref->full_path->start;
2600 } else {
2601 tmp++;
2602 ref->name_len = ref->full_path->end - tmp;
2603 ref->name = tmp;
2604 ref->dir_path = ref->full_path->start;
2605 ref->dir_path_len = ref->full_path->end -
2606 ref->full_path->start - 1 - ref->name_len;
2607 }
2608
2609 list_add_tail(&ref->list, head);
2610 return 0;
2611}
2612
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002613static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002614{
2615 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002616
Alexander Blocke938c8a2012-07-28 16:33:49 +02002617 while (!list_empty(head)) {
2618 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002619 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002620 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002621 kfree(cur);
2622 }
Alexander Block31db9f72012-07-25 23:19:24 +02002623}
2624
2625static void free_recorded_refs(struct send_ctx *sctx)
2626{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002627 __free_recorded_refs(&sctx->new_refs);
2628 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002629}
2630
2631/*
Alexander Block766702e2012-07-28 14:11:31 +02002632 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002633 * ref of an unprocessed inode gets overwritten and for all non empty
2634 * directories.
2635 */
2636static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2637 struct fs_path *path)
2638{
2639 int ret;
2640 struct fs_path *orphan;
2641
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002642 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002643 if (!orphan)
2644 return -ENOMEM;
2645
2646 ret = gen_unique_name(sctx, ino, gen, orphan);
2647 if (ret < 0)
2648 goto out;
2649
2650 ret = send_rename(sctx, path, orphan);
2651
2652out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002653 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002654 return ret;
2655}
2656
2657/*
2658 * Returns 1 if a directory can be removed at this point in time.
2659 * We check this by iterating all dir items and checking if the inode behind
2660 * the dir item was already processed.
2661 */
2662static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2663{
2664 int ret = 0;
2665 struct btrfs_root *root = sctx->parent_root;
2666 struct btrfs_path *path;
2667 struct btrfs_key key;
2668 struct btrfs_key found_key;
2669 struct btrfs_key loc;
2670 struct btrfs_dir_item *di;
2671
Alexander Block6d85ed02012-08-01 14:48:59 +02002672 /*
2673 * Don't try to rmdir the top/root subvolume dir.
2674 */
2675 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2676 return 0;
2677
Alexander Block31db9f72012-07-25 23:19:24 +02002678 path = alloc_path_for_send();
2679 if (!path)
2680 return -ENOMEM;
2681
2682 key.objectid = dir;
2683 key.type = BTRFS_DIR_INDEX_KEY;
2684 key.offset = 0;
2685
2686 while (1) {
2687 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2688 if (ret < 0)
2689 goto out;
2690 if (!ret) {
2691 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2692 path->slots[0]);
2693 }
2694 if (ret || found_key.objectid != key.objectid ||
2695 found_key.type != key.type) {
2696 break;
2697 }
2698
2699 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2700 struct btrfs_dir_item);
2701 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2702
2703 if (loc.objectid > send_progress) {
2704 ret = 0;
2705 goto out;
2706 }
2707
2708 btrfs_release_path(path);
2709 key.offset = found_key.offset + 1;
2710 }
2711
2712 ret = 1;
2713
2714out:
2715 btrfs_free_path(path);
2716 return ret;
2717}
2718
Alexander Block31db9f72012-07-25 23:19:24 +02002719/*
2720 * This does all the move/link/unlink/rmdir magic.
2721 */
2722static int process_recorded_refs(struct send_ctx *sctx)
2723{
2724 int ret = 0;
2725 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02002726 struct recorded_ref *cur2;
Alexander Block31db9f72012-07-25 23:19:24 +02002727 struct ulist *check_dirs = NULL;
2728 struct ulist_iterator uit;
2729 struct ulist_node *un;
2730 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04002731 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002732 u64 ow_gen;
2733 int did_overwrite = 0;
2734 int is_orphan = 0;
2735
2736verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2737
Alexander Block6d85ed02012-08-01 14:48:59 +02002738 /*
2739 * This should never happen as the root dir always has the same ref
2740 * which is always '..'
2741 */
2742 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
2743
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002744 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002745 if (!valid_path) {
2746 ret = -ENOMEM;
2747 goto out;
2748 }
2749
2750 check_dirs = ulist_alloc(GFP_NOFS);
2751 if (!check_dirs) {
2752 ret = -ENOMEM;
2753 goto out;
2754 }
2755
2756 /*
2757 * First, check if the first ref of the current inode was overwritten
2758 * before. If yes, we know that the current inode was already orphanized
2759 * and thus use the orphan name. If not, we can use get_cur_path to
2760 * get the path of the first ref as it would like while receiving at
2761 * this point in time.
2762 * New inodes are always orphan at the beginning, so force to use the
2763 * orphan name in this case.
2764 * The first ref is stored in valid_path and will be updated if it
2765 * gets moved around.
2766 */
2767 if (!sctx->cur_inode_new) {
2768 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2769 sctx->cur_inode_gen);
2770 if (ret < 0)
2771 goto out;
2772 if (ret)
2773 did_overwrite = 1;
2774 }
2775 if (sctx->cur_inode_new || did_overwrite) {
2776 ret = gen_unique_name(sctx, sctx->cur_ino,
2777 sctx->cur_inode_gen, valid_path);
2778 if (ret < 0)
2779 goto out;
2780 is_orphan = 1;
2781 } else {
2782 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2783 valid_path);
2784 if (ret < 0)
2785 goto out;
2786 }
2787
2788 list_for_each_entry(cur, &sctx->new_refs, list) {
2789 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02002790 * We may have refs where the parent directory does not exist
2791 * yet. This happens if the parent directories inum is higher
2792 * the the current inum. To handle this case, we create the
2793 * parent directory out of order. But we need to check if this
2794 * did already happen before due to other refs in the same dir.
2795 */
2796 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2797 if (ret < 0)
2798 goto out;
2799 if (ret == inode_state_will_create) {
2800 ret = 0;
2801 /*
2802 * First check if any of the current inodes refs did
2803 * already create the dir.
2804 */
2805 list_for_each_entry(cur2, &sctx->new_refs, list) {
2806 if (cur == cur2)
2807 break;
2808 if (cur2->dir == cur->dir) {
2809 ret = 1;
2810 break;
2811 }
2812 }
2813
2814 /*
2815 * If that did not happen, check if a previous inode
2816 * did already create the dir.
2817 */
2818 if (!ret)
2819 ret = did_create_dir(sctx, cur->dir);
2820 if (ret < 0)
2821 goto out;
2822 if (!ret) {
2823 ret = send_create_inode(sctx, cur->dir);
2824 if (ret < 0)
2825 goto out;
2826 }
2827 }
2828
2829 /*
Alexander Block31db9f72012-07-25 23:19:24 +02002830 * Check if this new ref would overwrite the first ref of
2831 * another unprocessed inode. If yes, orphanize the
2832 * overwritten inode. If we find an overwritten ref that is
2833 * not the first ref, simply unlink it.
2834 */
2835 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2836 cur->name, cur->name_len,
2837 &ow_inode, &ow_gen);
2838 if (ret < 0)
2839 goto out;
2840 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002841 ret = is_first_ref(sctx->parent_root,
2842 ow_inode, cur->dir, cur->name,
2843 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02002844 if (ret < 0)
2845 goto out;
2846 if (ret) {
2847 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2848 cur->full_path);
2849 if (ret < 0)
2850 goto out;
2851 } else {
2852 ret = send_unlink(sctx, cur->full_path);
2853 if (ret < 0)
2854 goto out;
2855 }
2856 }
2857
2858 /*
2859 * link/move the ref to the new place. If we have an orphan
2860 * inode, move it and update valid_path. If not, link or move
2861 * it depending on the inode mode.
2862 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002863 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02002864 ret = send_rename(sctx, valid_path, cur->full_path);
2865 if (ret < 0)
2866 goto out;
2867 is_orphan = 0;
2868 ret = fs_path_copy(valid_path, cur->full_path);
2869 if (ret < 0)
2870 goto out;
2871 } else {
2872 if (S_ISDIR(sctx->cur_inode_mode)) {
2873 /*
2874 * Dirs can't be linked, so move it. For moved
2875 * dirs, we always have one new and one deleted
2876 * ref. The deleted ref is ignored later.
2877 */
2878 ret = send_rename(sctx, valid_path,
2879 cur->full_path);
2880 if (ret < 0)
2881 goto out;
2882 ret = fs_path_copy(valid_path, cur->full_path);
2883 if (ret < 0)
2884 goto out;
2885 } else {
2886 ret = send_link(sctx, cur->full_path,
2887 valid_path);
2888 if (ret < 0)
2889 goto out;
2890 }
2891 }
2892 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2893 GFP_NOFS);
2894 if (ret < 0)
2895 goto out;
2896 }
2897
2898 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2899 /*
2900 * Check if we can already rmdir the directory. If not,
2901 * orphanize it. For every dir item inside that gets deleted
2902 * later, we do this check again and rmdir it then if possible.
2903 * See the use of check_dirs for more details.
2904 */
2905 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2906 if (ret < 0)
2907 goto out;
2908 if (ret) {
2909 ret = send_rmdir(sctx, valid_path);
2910 if (ret < 0)
2911 goto out;
2912 } else if (!is_orphan) {
2913 ret = orphanize_inode(sctx, sctx->cur_ino,
2914 sctx->cur_inode_gen, valid_path);
2915 if (ret < 0)
2916 goto out;
2917 is_orphan = 1;
2918 }
2919
2920 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2921 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2922 GFP_NOFS);
2923 if (ret < 0)
2924 goto out;
2925 }
Alexander Blockccf16262012-07-28 11:46:29 +02002926 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2927 !list_empty(&sctx->deleted_refs)) {
2928 /*
2929 * We have a moved dir. Add the old parent to check_dirs
2930 */
2931 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2932 list);
2933 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2934 GFP_NOFS);
2935 if (ret < 0)
2936 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002937 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2938 /*
2939 * We have a non dir inode. Go through all deleted refs and
2940 * unlink them if they were not already overwritten by other
2941 * inodes.
2942 */
2943 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2944 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2945 sctx->cur_ino, sctx->cur_inode_gen,
2946 cur->name, cur->name_len);
2947 if (ret < 0)
2948 goto out;
2949 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002950 ret = send_unlink(sctx, cur->full_path);
2951 if (ret < 0)
2952 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002953 }
2954 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2955 GFP_NOFS);
2956 if (ret < 0)
2957 goto out;
2958 }
2959
2960 /*
2961 * If the inode is still orphan, unlink the orphan. This may
2962 * happen when a previous inode did overwrite the first ref
2963 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02002964 * inode. Unlinking does not mean that the inode is deleted in
2965 * all cases. There may still be links to this inode in other
2966 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02002967 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002968 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02002969 ret = send_unlink(sctx, valid_path);
2970 if (ret < 0)
2971 goto out;
2972 }
2973 }
2974
2975 /*
2976 * We did collect all parent dirs where cur_inode was once located. We
2977 * now go through all these dirs and check if they are pending for
2978 * deletion and if it's finally possible to perform the rmdir now.
2979 * We also update the inode stats of the parent dirs here.
2980 */
2981 ULIST_ITER_INIT(&uit);
2982 while ((un = ulist_next(check_dirs, &uit))) {
Alexander Block766702e2012-07-28 14:11:31 +02002983 /*
2984 * In case we had refs into dirs that were not processed yet,
2985 * we don't need to do the utime and rmdir logic for these dirs.
2986 * The dir will be processed later.
2987 */
Alexander Block31db9f72012-07-25 23:19:24 +02002988 if (un->val > sctx->cur_ino)
2989 continue;
2990
2991 ret = get_cur_inode_state(sctx, un->val, un->aux);
2992 if (ret < 0)
2993 goto out;
2994
2995 if (ret == inode_state_did_create ||
2996 ret == inode_state_no_change) {
2997 /* TODO delayed utimes */
2998 ret = send_utimes(sctx, un->val, un->aux);
2999 if (ret < 0)
3000 goto out;
3001 } else if (ret == inode_state_did_delete) {
3002 ret = can_rmdir(sctx, un->val, sctx->cur_ino);
3003 if (ret < 0)
3004 goto out;
3005 if (ret) {
3006 ret = get_cur_path(sctx, un->val, un->aux,
3007 valid_path);
3008 if (ret < 0)
3009 goto out;
3010 ret = send_rmdir(sctx, valid_path);
3011 if (ret < 0)
3012 goto out;
3013 }
3014 }
3015 }
3016
Alexander Block31db9f72012-07-25 23:19:24 +02003017 ret = 0;
3018
3019out:
3020 free_recorded_refs(sctx);
3021 ulist_free(check_dirs);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003022 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02003023 return ret;
3024}
3025
3026static int __record_new_ref(int num, u64 dir, int index,
3027 struct fs_path *name,
3028 void *ctx)
3029{
3030 int ret = 0;
3031 struct send_ctx *sctx = ctx;
3032 struct fs_path *p;
3033 u64 gen;
3034
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003035 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003036 if (!p)
3037 return -ENOMEM;
3038
3039 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003040 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003041 if (ret < 0)
3042 goto out;
3043
Alexander Block31db9f72012-07-25 23:19:24 +02003044 ret = get_cur_path(sctx, dir, gen, p);
3045 if (ret < 0)
3046 goto out;
3047 ret = fs_path_add_path(p, name);
3048 if (ret < 0)
3049 goto out;
3050
3051 ret = record_ref(&sctx->new_refs, dir, gen, p);
3052
3053out:
3054 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003055 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003056 return ret;
3057}
3058
3059static int __record_deleted_ref(int num, u64 dir, int index,
3060 struct fs_path *name,
3061 void *ctx)
3062{
3063 int ret = 0;
3064 struct send_ctx *sctx = ctx;
3065 struct fs_path *p;
3066 u64 gen;
3067
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003068 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003069 if (!p)
3070 return -ENOMEM;
3071
3072 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003073 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003074 if (ret < 0)
3075 goto out;
3076
3077 ret = get_cur_path(sctx, dir, gen, p);
3078 if (ret < 0)
3079 goto out;
3080 ret = fs_path_add_path(p, name);
3081 if (ret < 0)
3082 goto out;
3083
3084 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3085
3086out:
3087 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003088 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003089 return ret;
3090}
3091
3092static int record_new_ref(struct send_ctx *sctx)
3093{
3094 int ret;
3095
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003096 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3097 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003098 if (ret < 0)
3099 goto out;
3100 ret = 0;
3101
3102out:
3103 return ret;
3104}
3105
3106static int record_deleted_ref(struct send_ctx *sctx)
3107{
3108 int ret;
3109
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003110 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3111 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003112 if (ret < 0)
3113 goto out;
3114 ret = 0;
3115
3116out:
3117 return ret;
3118}
3119
3120struct find_ref_ctx {
3121 u64 dir;
3122 struct fs_path *name;
3123 int found_idx;
3124};
3125
3126static int __find_iref(int num, u64 dir, int index,
3127 struct fs_path *name,
3128 void *ctx_)
3129{
3130 struct find_ref_ctx *ctx = ctx_;
3131
3132 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3133 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3134 ctx->found_idx = num;
3135 return 1;
3136 }
3137 return 0;
3138}
3139
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003140static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003141 struct btrfs_path *path,
3142 struct btrfs_key *key,
3143 u64 dir, struct fs_path *name)
3144{
3145 int ret;
3146 struct find_ref_ctx ctx;
3147
3148 ctx.dir = dir;
3149 ctx.name = name;
3150 ctx.found_idx = -1;
3151
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003152 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003153 if (ret < 0)
3154 return ret;
3155
3156 if (ctx.found_idx == -1)
3157 return -ENOENT;
3158
3159 return ctx.found_idx;
3160}
3161
3162static int __record_changed_new_ref(int num, u64 dir, int index,
3163 struct fs_path *name,
3164 void *ctx)
3165{
3166 int ret;
3167 struct send_ctx *sctx = ctx;
3168
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003169 ret = find_iref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003170 sctx->cmp_key, dir, name);
3171 if (ret == -ENOENT)
3172 ret = __record_new_ref(num, dir, index, name, sctx);
3173 else if (ret > 0)
3174 ret = 0;
3175
3176 return ret;
3177}
3178
3179static int __record_changed_deleted_ref(int num, u64 dir, int index,
3180 struct fs_path *name,
3181 void *ctx)
3182{
3183 int ret;
3184 struct send_ctx *sctx = ctx;
3185
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003186 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Alexander Block31db9f72012-07-25 23:19:24 +02003187 dir, name);
3188 if (ret == -ENOENT)
3189 ret = __record_deleted_ref(num, dir, index, name, sctx);
3190 else if (ret > 0)
3191 ret = 0;
3192
3193 return ret;
3194}
3195
3196static int record_changed_ref(struct send_ctx *sctx)
3197{
3198 int ret = 0;
3199
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003200 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003201 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3202 if (ret < 0)
3203 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003204 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003205 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3206 if (ret < 0)
3207 goto out;
3208 ret = 0;
3209
3210out:
3211 return ret;
3212}
3213
3214/*
3215 * Record and process all refs at once. Needed when an inode changes the
3216 * generation number, which means that it was deleted and recreated.
3217 */
3218static int process_all_refs(struct send_ctx *sctx,
3219 enum btrfs_compare_tree_result cmd)
3220{
3221 int ret;
3222 struct btrfs_root *root;
3223 struct btrfs_path *path;
3224 struct btrfs_key key;
3225 struct btrfs_key found_key;
3226 struct extent_buffer *eb;
3227 int slot;
3228 iterate_inode_ref_t cb;
3229
3230 path = alloc_path_for_send();
3231 if (!path)
3232 return -ENOMEM;
3233
3234 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3235 root = sctx->send_root;
3236 cb = __record_new_ref;
3237 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3238 root = sctx->parent_root;
3239 cb = __record_deleted_ref;
3240 } else {
3241 BUG();
3242 }
3243
3244 key.objectid = sctx->cmp_key->objectid;
3245 key.type = BTRFS_INODE_REF_KEY;
3246 key.offset = 0;
3247 while (1) {
3248 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
Alexander Blocke938c8a2012-07-28 16:33:49 +02003249 if (ret < 0)
Alexander Block31db9f72012-07-25 23:19:24 +02003250 goto out;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003251 if (ret)
Alexander Block31db9f72012-07-25 23:19:24 +02003252 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003253
3254 eb = path->nodes[0];
3255 slot = path->slots[0];
3256 btrfs_item_key_to_cpu(eb, &found_key, slot);
3257
3258 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003259 (found_key.type != BTRFS_INODE_REF_KEY &&
3260 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003261 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003262
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003263 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003264 btrfs_release_path(path);
3265 if (ret < 0)
3266 goto out;
3267
3268 key.offset = found_key.offset + 1;
3269 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003270 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003271
3272 ret = process_recorded_refs(sctx);
3273
3274out:
3275 btrfs_free_path(path);
3276 return ret;
3277}
3278
3279static int send_set_xattr(struct send_ctx *sctx,
3280 struct fs_path *path,
3281 const char *name, int name_len,
3282 const char *data, int data_len)
3283{
3284 int ret = 0;
3285
3286 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3287 if (ret < 0)
3288 goto out;
3289
3290 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3291 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3292 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3293
3294 ret = send_cmd(sctx);
3295
3296tlv_put_failure:
3297out:
3298 return ret;
3299}
3300
3301static int send_remove_xattr(struct send_ctx *sctx,
3302 struct fs_path *path,
3303 const char *name, int name_len)
3304{
3305 int ret = 0;
3306
3307 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3308 if (ret < 0)
3309 goto out;
3310
3311 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3312 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3313
3314 ret = send_cmd(sctx);
3315
3316tlv_put_failure:
3317out:
3318 return ret;
3319}
3320
3321static int __process_new_xattr(int num, struct btrfs_key *di_key,
3322 const char *name, int name_len,
3323 const char *data, int data_len,
3324 u8 type, void *ctx)
3325{
3326 int ret;
3327 struct send_ctx *sctx = ctx;
3328 struct fs_path *p;
3329 posix_acl_xattr_header dummy_acl;
3330
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003331 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003332 if (!p)
3333 return -ENOMEM;
3334
3335 /*
3336 * This hack is needed because empty acl's are stored as zero byte
3337 * data in xattrs. Problem with that is, that receiving these zero byte
3338 * acl's will fail later. To fix this, we send a dummy acl list that
3339 * only contains the version number and no entries.
3340 */
3341 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3342 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3343 if (data_len == 0) {
3344 dummy_acl.a_version =
3345 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3346 data = (char *)&dummy_acl;
3347 data_len = sizeof(dummy_acl);
3348 }
3349 }
3350
3351 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3352 if (ret < 0)
3353 goto out;
3354
3355 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3356
3357out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003358 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003359 return ret;
3360}
3361
3362static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3363 const char *name, int name_len,
3364 const char *data, int data_len,
3365 u8 type, void *ctx)
3366{
3367 int ret;
3368 struct send_ctx *sctx = ctx;
3369 struct fs_path *p;
3370
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003371 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003372 if (!p)
3373 return -ENOMEM;
3374
3375 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3376 if (ret < 0)
3377 goto out;
3378
3379 ret = send_remove_xattr(sctx, p, name, name_len);
3380
3381out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003382 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003383 return ret;
3384}
3385
3386static int process_new_xattr(struct send_ctx *sctx)
3387{
3388 int ret = 0;
3389
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003390 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3391 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003392
3393 return ret;
3394}
3395
3396static int process_deleted_xattr(struct send_ctx *sctx)
3397{
3398 int ret;
3399
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003400 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3401 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003402
3403 return ret;
3404}
3405
3406struct find_xattr_ctx {
3407 const char *name;
3408 int name_len;
3409 int found_idx;
3410 char *found_data;
3411 int found_data_len;
3412};
3413
3414static int __find_xattr(int num, struct btrfs_key *di_key,
3415 const char *name, int name_len,
3416 const char *data, int data_len,
3417 u8 type, void *vctx)
3418{
3419 struct find_xattr_ctx *ctx = vctx;
3420
3421 if (name_len == ctx->name_len &&
3422 strncmp(name, ctx->name, name_len) == 0) {
3423 ctx->found_idx = num;
3424 ctx->found_data_len = data_len;
3425 ctx->found_data = kmalloc(data_len, GFP_NOFS);
3426 if (!ctx->found_data)
3427 return -ENOMEM;
3428 memcpy(ctx->found_data, data, data_len);
3429 return 1;
3430 }
3431 return 0;
3432}
3433
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003434static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003435 struct btrfs_path *path,
3436 struct btrfs_key *key,
3437 const char *name, int name_len,
3438 char **data, int *data_len)
3439{
3440 int ret;
3441 struct find_xattr_ctx ctx;
3442
3443 ctx.name = name;
3444 ctx.name_len = name_len;
3445 ctx.found_idx = -1;
3446 ctx.found_data = NULL;
3447 ctx.found_data_len = 0;
3448
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003449 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003450 if (ret < 0)
3451 return ret;
3452
3453 if (ctx.found_idx == -1)
3454 return -ENOENT;
3455 if (data) {
3456 *data = ctx.found_data;
3457 *data_len = ctx.found_data_len;
3458 } else {
3459 kfree(ctx.found_data);
3460 }
3461 return ctx.found_idx;
3462}
3463
3464
3465static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3466 const char *name, int name_len,
3467 const char *data, int data_len,
3468 u8 type, void *ctx)
3469{
3470 int ret;
3471 struct send_ctx *sctx = ctx;
3472 char *found_data = NULL;
3473 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003474
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003475 ret = find_xattr(sctx->parent_root, sctx->right_path,
3476 sctx->cmp_key, name, name_len, &found_data,
3477 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003478 if (ret == -ENOENT) {
3479 ret = __process_new_xattr(num, di_key, name, name_len, data,
3480 data_len, type, ctx);
3481 } else if (ret >= 0) {
3482 if (data_len != found_data_len ||
3483 memcmp(data, found_data, data_len)) {
3484 ret = __process_new_xattr(num, di_key, name, name_len,
3485 data, data_len, type, ctx);
3486 } else {
3487 ret = 0;
3488 }
3489 }
3490
3491 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02003492 return ret;
3493}
3494
3495static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3496 const char *name, int name_len,
3497 const char *data, int data_len,
3498 u8 type, void *ctx)
3499{
3500 int ret;
3501 struct send_ctx *sctx = ctx;
3502
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003503 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
3504 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003505 if (ret == -ENOENT)
3506 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3507 data_len, type, ctx);
3508 else if (ret >= 0)
3509 ret = 0;
3510
3511 return ret;
3512}
3513
3514static int process_changed_xattr(struct send_ctx *sctx)
3515{
3516 int ret = 0;
3517
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003518 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003519 sctx->cmp_key, __process_changed_new_xattr, sctx);
3520 if (ret < 0)
3521 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003522 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003523 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3524
3525out:
3526 return ret;
3527}
3528
3529static int process_all_new_xattrs(struct send_ctx *sctx)
3530{
3531 int ret;
3532 struct btrfs_root *root;
3533 struct btrfs_path *path;
3534 struct btrfs_key key;
3535 struct btrfs_key found_key;
3536 struct extent_buffer *eb;
3537 int slot;
3538
3539 path = alloc_path_for_send();
3540 if (!path)
3541 return -ENOMEM;
3542
3543 root = sctx->send_root;
3544
3545 key.objectid = sctx->cmp_key->objectid;
3546 key.type = BTRFS_XATTR_ITEM_KEY;
3547 key.offset = 0;
3548 while (1) {
3549 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3550 if (ret < 0)
3551 goto out;
3552 if (ret) {
3553 ret = 0;
3554 goto out;
3555 }
3556
3557 eb = path->nodes[0];
3558 slot = path->slots[0];
3559 btrfs_item_key_to_cpu(eb, &found_key, slot);
3560
3561 if (found_key.objectid != key.objectid ||
3562 found_key.type != key.type) {
3563 ret = 0;
3564 goto out;
3565 }
3566
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003567 ret = iterate_dir_item(root, path, &found_key,
3568 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003569 if (ret < 0)
3570 goto out;
3571
3572 btrfs_release_path(path);
3573 key.offset = found_key.offset + 1;
3574 }
3575
3576out:
3577 btrfs_free_path(path);
3578 return ret;
3579}
3580
3581/*
3582 * Read some bytes from the current inode/file and send a write command to
3583 * user space.
3584 */
3585static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3586{
3587 int ret = 0;
3588 struct fs_path *p;
3589 loff_t pos = offset;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003590 int num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003591 mm_segment_t old_fs;
3592
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003593 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003594 if (!p)
3595 return -ENOMEM;
3596
3597 /*
3598 * vfs normally only accepts user space buffers for security reasons.
3599 * we only read from the file and also only provide the read_buf buffer
3600 * to vfs. As this buffer does not come from a user space call, it's
3601 * ok to temporary allow kernel space buffers.
3602 */
3603 old_fs = get_fs();
3604 set_fs(KERNEL_DS);
3605
3606verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3607
3608 ret = open_cur_inode_file(sctx);
3609 if (ret < 0)
3610 goto out;
3611
3612 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3613 if (ret < 0)
3614 goto out;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003615 num_read = ret;
3616 if (!num_read)
Alexander Block31db9f72012-07-25 23:19:24 +02003617 goto out;
3618
3619 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3620 if (ret < 0)
3621 goto out;
3622
3623 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3624 if (ret < 0)
3625 goto out;
3626
3627 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3628 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02003629 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02003630
3631 ret = send_cmd(sctx);
3632
3633tlv_put_failure:
3634out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003635 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003636 set_fs(old_fs);
3637 if (ret < 0)
3638 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003639 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02003640}
3641
3642/*
3643 * Send a clone command to user space.
3644 */
3645static int send_clone(struct send_ctx *sctx,
3646 u64 offset, u32 len,
3647 struct clone_root *clone_root)
3648{
3649 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003650 struct fs_path *p;
3651 u64 gen;
3652
3653verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3654 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3655 clone_root->root->objectid, clone_root->ino,
3656 clone_root->offset);
3657
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003658 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003659 if (!p)
3660 return -ENOMEM;
3661
3662 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3663 if (ret < 0)
3664 goto out;
3665
3666 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3667 if (ret < 0)
3668 goto out;
3669
3670 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3671 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3672 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3673
Alexander Blocke938c8a2012-07-28 16:33:49 +02003674 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02003675 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003676 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003677 if (ret < 0)
3678 goto out;
3679 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3680 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003681 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003682 }
3683 if (ret < 0)
3684 goto out;
3685
3686 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02003687 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02003688 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02003689 clone_root->root->root_item.ctransid);
Alexander Block31db9f72012-07-25 23:19:24 +02003690 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3691 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3692 clone_root->offset);
3693
3694 ret = send_cmd(sctx);
3695
3696tlv_put_failure:
3697out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003698 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003699 return ret;
3700}
3701
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003702/*
3703 * Send an update extent command to user space.
3704 */
3705static int send_update_extent(struct send_ctx *sctx,
3706 u64 offset, u32 len)
3707{
3708 int ret = 0;
3709 struct fs_path *p;
3710
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003711 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003712 if (!p)
3713 return -ENOMEM;
3714
3715 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
3716 if (ret < 0)
3717 goto out;
3718
3719 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3720 if (ret < 0)
3721 goto out;
3722
3723 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3724 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3725 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
3726
3727 ret = send_cmd(sctx);
3728
3729tlv_put_failure:
3730out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003731 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003732 return ret;
3733}
3734
Alexander Block31db9f72012-07-25 23:19:24 +02003735static int send_write_or_clone(struct send_ctx *sctx,
3736 struct btrfs_path *path,
3737 struct btrfs_key *key,
3738 struct clone_root *clone_root)
3739{
3740 int ret = 0;
3741 struct btrfs_file_extent_item *ei;
3742 u64 offset = key->offset;
3743 u64 pos = 0;
3744 u64 len;
3745 u32 l;
3746 u8 type;
3747
3748 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3749 struct btrfs_file_extent_item);
3750 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003751 if (type == BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02003752 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003753 /*
3754 * it is possible the inline item won't cover the whole page,
3755 * but there may be items after this page. Make
3756 * sure to send the whole thing
3757 */
3758 len = PAGE_CACHE_ALIGN(len);
3759 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02003760 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003761 }
Alexander Block31db9f72012-07-25 23:19:24 +02003762
3763 if (offset + len > sctx->cur_inode_size)
3764 len = sctx->cur_inode_size - offset;
3765 if (len == 0) {
3766 ret = 0;
3767 goto out;
3768 }
3769
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003770 if (clone_root) {
3771 ret = send_clone(sctx, offset, len, clone_root);
3772 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
3773 ret = send_update_extent(sctx, offset, len);
3774 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02003775 while (pos < len) {
3776 l = len - pos;
3777 if (l > BTRFS_SEND_READ_SIZE)
3778 l = BTRFS_SEND_READ_SIZE;
3779 ret = send_write(sctx, pos + offset, l);
3780 if (ret < 0)
3781 goto out;
3782 if (!ret)
3783 break;
3784 pos += ret;
3785 }
3786 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003787 }
Alexander Block31db9f72012-07-25 23:19:24 +02003788out:
3789 return ret;
3790}
3791
3792static int is_extent_unchanged(struct send_ctx *sctx,
3793 struct btrfs_path *left_path,
3794 struct btrfs_key *ekey)
3795{
3796 int ret = 0;
3797 struct btrfs_key key;
3798 struct btrfs_path *path = NULL;
3799 struct extent_buffer *eb;
3800 int slot;
3801 struct btrfs_key found_key;
3802 struct btrfs_file_extent_item *ei;
3803 u64 left_disknr;
3804 u64 right_disknr;
3805 u64 left_offset;
3806 u64 right_offset;
3807 u64 left_offset_fixed;
3808 u64 left_len;
3809 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04003810 u64 left_gen;
3811 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003812 u8 left_type;
3813 u8 right_type;
3814
3815 path = alloc_path_for_send();
3816 if (!path)
3817 return -ENOMEM;
3818
3819 eb = left_path->nodes[0];
3820 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02003821 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3822 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02003823
3824 if (left_type != BTRFS_FILE_EXTENT_REG) {
3825 ret = 0;
3826 goto out;
3827 }
Chris Mason74dd17f2012-08-07 16:25:13 -04003828 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3829 left_len = btrfs_file_extent_num_bytes(eb, ei);
3830 left_offset = btrfs_file_extent_offset(eb, ei);
3831 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02003832
3833 /*
3834 * Following comments will refer to these graphics. L is the left
3835 * extents which we are checking at the moment. 1-8 are the right
3836 * extents that we iterate.
3837 *
3838 * |-----L-----|
3839 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3840 *
3841 * |-----L-----|
3842 * |--1--|-2b-|...(same as above)
3843 *
3844 * Alternative situation. Happens on files where extents got split.
3845 * |-----L-----|
3846 * |-----------7-----------|-6-|
3847 *
3848 * Alternative situation. Happens on files which got larger.
3849 * |-----L-----|
3850 * |-8-|
3851 * Nothing follows after 8.
3852 */
3853
3854 key.objectid = ekey->objectid;
3855 key.type = BTRFS_EXTENT_DATA_KEY;
3856 key.offset = ekey->offset;
3857 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3858 if (ret < 0)
3859 goto out;
3860 if (ret) {
3861 ret = 0;
3862 goto out;
3863 }
3864
3865 /*
3866 * Handle special case where the right side has no extents at all.
3867 */
3868 eb = path->nodes[0];
3869 slot = path->slots[0];
3870 btrfs_item_key_to_cpu(eb, &found_key, slot);
3871 if (found_key.objectid != key.objectid ||
3872 found_key.type != key.type) {
3873 ret = 0;
3874 goto out;
3875 }
3876
3877 /*
3878 * We're now on 2a, 2b or 7.
3879 */
3880 key = found_key;
3881 while (key.offset < ekey->offset + left_len) {
3882 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3883 right_type = btrfs_file_extent_type(eb, ei);
3884 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3885 right_len = btrfs_file_extent_num_bytes(eb, ei);
3886 right_offset = btrfs_file_extent_offset(eb, ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003887 right_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02003888
3889 if (right_type != BTRFS_FILE_EXTENT_REG) {
3890 ret = 0;
3891 goto out;
3892 }
3893
3894 /*
3895 * Are we at extent 8? If yes, we know the extent is changed.
3896 * This may only happen on the first iteration.
3897 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02003898 if (found_key.offset + right_len <= ekey->offset) {
Alexander Block31db9f72012-07-25 23:19:24 +02003899 ret = 0;
3900 goto out;
3901 }
3902
3903 left_offset_fixed = left_offset;
3904 if (key.offset < ekey->offset) {
3905 /* Fix the right offset for 2a and 7. */
3906 right_offset += ekey->offset - key.offset;
3907 } else {
3908 /* Fix the left offset for all behind 2a and 2b */
3909 left_offset_fixed += key.offset - ekey->offset;
3910 }
3911
3912 /*
3913 * Check if we have the same extent.
3914 */
Alexander Block39540962012-08-01 12:46:05 +02003915 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04003916 left_offset_fixed != right_offset ||
3917 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02003918 ret = 0;
3919 goto out;
3920 }
3921
3922 /*
3923 * Go to the next extent.
3924 */
3925 ret = btrfs_next_item(sctx->parent_root, path);
3926 if (ret < 0)
3927 goto out;
3928 if (!ret) {
3929 eb = path->nodes[0];
3930 slot = path->slots[0];
3931 btrfs_item_key_to_cpu(eb, &found_key, slot);
3932 }
3933 if (ret || found_key.objectid != key.objectid ||
3934 found_key.type != key.type) {
3935 key.offset += right_len;
3936 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00003937 }
3938 if (found_key.offset != key.offset + right_len) {
3939 ret = 0;
3940 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003941 }
3942 key = found_key;
3943 }
3944
3945 /*
3946 * We're now behind the left extent (treat as unchanged) or at the end
3947 * of the right side (treat as changed).
3948 */
3949 if (key.offset >= ekey->offset + left_len)
3950 ret = 1;
3951 else
3952 ret = 0;
3953
3954
3955out:
3956 btrfs_free_path(path);
3957 return ret;
3958}
3959
3960static int process_extent(struct send_ctx *sctx,
3961 struct btrfs_path *path,
3962 struct btrfs_key *key)
3963{
3964 int ret = 0;
3965 struct clone_root *found_clone = NULL;
3966
3967 if (S_ISLNK(sctx->cur_inode_mode))
3968 return 0;
3969
3970 if (sctx->parent_root && !sctx->cur_inode_new) {
3971 ret = is_extent_unchanged(sctx, path, key);
3972 if (ret < 0)
3973 goto out;
3974 if (ret) {
3975 ret = 0;
3976 goto out;
3977 }
3978 }
3979
3980 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
3981 sctx->cur_inode_size, &found_clone);
3982 if (ret != -ENOENT && ret < 0)
3983 goto out;
3984
3985 ret = send_write_or_clone(sctx, path, key, found_clone);
3986
3987out:
3988 return ret;
3989}
3990
3991static int process_all_extents(struct send_ctx *sctx)
3992{
3993 int ret;
3994 struct btrfs_root *root;
3995 struct btrfs_path *path;
3996 struct btrfs_key key;
3997 struct btrfs_key found_key;
3998 struct extent_buffer *eb;
3999 int slot;
4000
4001 root = sctx->send_root;
4002 path = alloc_path_for_send();
4003 if (!path)
4004 return -ENOMEM;
4005
4006 key.objectid = sctx->cmp_key->objectid;
4007 key.type = BTRFS_EXTENT_DATA_KEY;
4008 key.offset = 0;
4009 while (1) {
4010 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
4011 if (ret < 0)
4012 goto out;
4013 if (ret) {
4014 ret = 0;
4015 goto out;
4016 }
4017
4018 eb = path->nodes[0];
4019 slot = path->slots[0];
4020 btrfs_item_key_to_cpu(eb, &found_key, slot);
4021
4022 if (found_key.objectid != key.objectid ||
4023 found_key.type != key.type) {
4024 ret = 0;
4025 goto out;
4026 }
4027
4028 ret = process_extent(sctx, path, &found_key);
4029 if (ret < 0)
4030 goto out;
4031
4032 btrfs_release_path(path);
4033 key.offset = found_key.offset + 1;
4034 }
4035
4036out:
4037 btrfs_free_path(path);
4038 return ret;
4039}
4040
4041static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
4042{
4043 int ret = 0;
4044
4045 if (sctx->cur_ino == 0)
4046 goto out;
4047 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004048 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004049 goto out;
4050 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4051 goto out;
4052
4053 ret = process_recorded_refs(sctx);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004054 if (ret < 0)
4055 goto out;
4056
4057 /*
4058 * We have processed the refs and thus need to advance send_progress.
4059 * Now, calls to get_cur_xxx will take the updated refs of the current
4060 * inode into account.
4061 */
4062 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004063
4064out:
4065 return ret;
4066}
4067
4068static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4069{
4070 int ret = 0;
4071 u64 left_mode;
4072 u64 left_uid;
4073 u64 left_gid;
4074 u64 right_mode;
4075 u64 right_uid;
4076 u64 right_gid;
4077 int need_chmod = 0;
4078 int need_chown = 0;
4079
4080 ret = process_recorded_refs_if_needed(sctx, at_end);
4081 if (ret < 0)
4082 goto out;
4083
4084 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4085 goto out;
4086 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4087 goto out;
4088
4089 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004090 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004091 if (ret < 0)
4092 goto out;
4093
Alex Lyakase2d044f2012-10-17 13:52:47 +00004094 if (!sctx->parent_root || sctx->cur_inode_new) {
4095 need_chown = 1;
4096 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004097 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004098 } else {
4099 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4100 NULL, NULL, &right_mode, &right_uid,
4101 &right_gid, NULL);
4102 if (ret < 0)
4103 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004104
Alex Lyakase2d044f2012-10-17 13:52:47 +00004105 if (left_uid != right_uid || left_gid != right_gid)
4106 need_chown = 1;
4107 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4108 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004109 }
4110
4111 if (S_ISREG(sctx->cur_inode_mode)) {
4112 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4113 sctx->cur_inode_size);
4114 if (ret < 0)
4115 goto out;
4116 }
4117
4118 if (need_chown) {
4119 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4120 left_uid, left_gid);
4121 if (ret < 0)
4122 goto out;
4123 }
4124 if (need_chmod) {
4125 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4126 left_mode);
4127 if (ret < 0)
4128 goto out;
4129 }
4130
4131 /*
4132 * Need to send that every time, no matter if it actually changed
4133 * between the two trees as we have done changes to the inode before.
4134 */
4135 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4136 if (ret < 0)
4137 goto out;
4138
4139out:
4140 return ret;
4141}
4142
4143static int changed_inode(struct send_ctx *sctx,
4144 enum btrfs_compare_tree_result result)
4145{
4146 int ret = 0;
4147 struct btrfs_key *key = sctx->cmp_key;
4148 struct btrfs_inode_item *left_ii = NULL;
4149 struct btrfs_inode_item *right_ii = NULL;
4150 u64 left_gen = 0;
4151 u64 right_gen = 0;
4152
4153 ret = close_cur_inode_file(sctx);
4154 if (ret < 0)
4155 goto out;
4156
4157 sctx->cur_ino = key->objectid;
4158 sctx->cur_inode_new_gen = 0;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004159
4160 /*
4161 * Set send_progress to current inode. This will tell all get_cur_xxx
4162 * functions that the current inode's refs are not updated yet. Later,
4163 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4164 */
Alexander Block31db9f72012-07-25 23:19:24 +02004165 sctx->send_progress = sctx->cur_ino;
4166
4167 if (result == BTRFS_COMPARE_TREE_NEW ||
4168 result == BTRFS_COMPARE_TREE_CHANGED) {
4169 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4170 sctx->left_path->slots[0],
4171 struct btrfs_inode_item);
4172 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4173 left_ii);
4174 } else {
4175 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4176 sctx->right_path->slots[0],
4177 struct btrfs_inode_item);
4178 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4179 right_ii);
4180 }
4181 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4182 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4183 sctx->right_path->slots[0],
4184 struct btrfs_inode_item);
4185
4186 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4187 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02004188
4189 /*
4190 * The cur_ino = root dir case is special here. We can't treat
4191 * the inode as deleted+reused because it would generate a
4192 * stream that tries to delete/mkdir the root dir.
4193 */
4194 if (left_gen != right_gen &&
4195 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02004196 sctx->cur_inode_new_gen = 1;
4197 }
4198
4199 if (result == BTRFS_COMPARE_TREE_NEW) {
4200 sctx->cur_inode_gen = left_gen;
4201 sctx->cur_inode_new = 1;
4202 sctx->cur_inode_deleted = 0;
4203 sctx->cur_inode_size = btrfs_inode_size(
4204 sctx->left_path->nodes[0], left_ii);
4205 sctx->cur_inode_mode = btrfs_inode_mode(
4206 sctx->left_path->nodes[0], left_ii);
4207 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02004208 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004209 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4210 sctx->cur_inode_gen = right_gen;
4211 sctx->cur_inode_new = 0;
4212 sctx->cur_inode_deleted = 1;
4213 sctx->cur_inode_size = btrfs_inode_size(
4214 sctx->right_path->nodes[0], right_ii);
4215 sctx->cur_inode_mode = btrfs_inode_mode(
4216 sctx->right_path->nodes[0], right_ii);
4217 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02004218 /*
4219 * We need to do some special handling in case the inode was
4220 * reported as changed with a changed generation number. This
4221 * means that the original inode was deleted and new inode
4222 * reused the same inum. So we have to treat the old inode as
4223 * deleted and the new one as new.
4224 */
Alexander Block31db9f72012-07-25 23:19:24 +02004225 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02004226 /*
4227 * First, process the inode as if it was deleted.
4228 */
Alexander Block31db9f72012-07-25 23:19:24 +02004229 sctx->cur_inode_gen = right_gen;
4230 sctx->cur_inode_new = 0;
4231 sctx->cur_inode_deleted = 1;
4232 sctx->cur_inode_size = btrfs_inode_size(
4233 sctx->right_path->nodes[0], right_ii);
4234 sctx->cur_inode_mode = btrfs_inode_mode(
4235 sctx->right_path->nodes[0], right_ii);
4236 ret = process_all_refs(sctx,
4237 BTRFS_COMPARE_TREE_DELETED);
4238 if (ret < 0)
4239 goto out;
4240
Alexander Block766702e2012-07-28 14:11:31 +02004241 /*
4242 * Now process the inode as if it was new.
4243 */
Alexander Block31db9f72012-07-25 23:19:24 +02004244 sctx->cur_inode_gen = left_gen;
4245 sctx->cur_inode_new = 1;
4246 sctx->cur_inode_deleted = 0;
4247 sctx->cur_inode_size = btrfs_inode_size(
4248 sctx->left_path->nodes[0], left_ii);
4249 sctx->cur_inode_mode = btrfs_inode_mode(
4250 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02004251 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004252 if (ret < 0)
4253 goto out;
4254
4255 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4256 if (ret < 0)
4257 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004258 /*
4259 * Advance send_progress now as we did not get into
4260 * process_recorded_refs_if_needed in the new_gen case.
4261 */
4262 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02004263
4264 /*
4265 * Now process all extents and xattrs of the inode as if
4266 * they were all new.
4267 */
Alexander Block31db9f72012-07-25 23:19:24 +02004268 ret = process_all_extents(sctx);
4269 if (ret < 0)
4270 goto out;
4271 ret = process_all_new_xattrs(sctx);
4272 if (ret < 0)
4273 goto out;
4274 } else {
4275 sctx->cur_inode_gen = left_gen;
4276 sctx->cur_inode_new = 0;
4277 sctx->cur_inode_new_gen = 0;
4278 sctx->cur_inode_deleted = 0;
4279 sctx->cur_inode_size = btrfs_inode_size(
4280 sctx->left_path->nodes[0], left_ii);
4281 sctx->cur_inode_mode = btrfs_inode_mode(
4282 sctx->left_path->nodes[0], left_ii);
4283 }
4284 }
4285
4286out:
4287 return ret;
4288}
4289
Alexander Block766702e2012-07-28 14:11:31 +02004290/*
4291 * We have to process new refs before deleted refs, but compare_trees gives us
4292 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4293 * first and later process them in process_recorded_refs.
4294 * For the cur_inode_new_gen case, we skip recording completely because
4295 * changed_inode did already initiate processing of refs. The reason for this is
4296 * that in this case, compare_tree actually compares the refs of 2 different
4297 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4298 * refs of the right tree as deleted and all refs of the left tree as new.
4299 */
Alexander Block31db9f72012-07-25 23:19:24 +02004300static int changed_ref(struct send_ctx *sctx,
4301 enum btrfs_compare_tree_result result)
4302{
4303 int ret = 0;
4304
4305 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4306
4307 if (!sctx->cur_inode_new_gen &&
4308 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4309 if (result == BTRFS_COMPARE_TREE_NEW)
4310 ret = record_new_ref(sctx);
4311 else if (result == BTRFS_COMPARE_TREE_DELETED)
4312 ret = record_deleted_ref(sctx);
4313 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4314 ret = record_changed_ref(sctx);
4315 }
4316
4317 return ret;
4318}
4319
Alexander Block766702e2012-07-28 14:11:31 +02004320/*
4321 * Process new/deleted/changed xattrs. We skip processing in the
4322 * cur_inode_new_gen case because changed_inode did already initiate processing
4323 * of xattrs. The reason is the same as in changed_ref
4324 */
Alexander Block31db9f72012-07-25 23:19:24 +02004325static int changed_xattr(struct send_ctx *sctx,
4326 enum btrfs_compare_tree_result result)
4327{
4328 int ret = 0;
4329
4330 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4331
4332 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4333 if (result == BTRFS_COMPARE_TREE_NEW)
4334 ret = process_new_xattr(sctx);
4335 else if (result == BTRFS_COMPARE_TREE_DELETED)
4336 ret = process_deleted_xattr(sctx);
4337 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4338 ret = process_changed_xattr(sctx);
4339 }
4340
4341 return ret;
4342}
4343
Alexander Block766702e2012-07-28 14:11:31 +02004344/*
4345 * Process new/deleted/changed extents. We skip processing in the
4346 * cur_inode_new_gen case because changed_inode did already initiate processing
4347 * of extents. The reason is the same as in changed_ref
4348 */
Alexander Block31db9f72012-07-25 23:19:24 +02004349static int changed_extent(struct send_ctx *sctx,
4350 enum btrfs_compare_tree_result result)
4351{
4352 int ret = 0;
4353
4354 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4355
4356 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4357 if (result != BTRFS_COMPARE_TREE_DELETED)
4358 ret = process_extent(sctx, sctx->left_path,
4359 sctx->cmp_key);
4360 }
4361
4362 return ret;
4363}
4364
Alexander Block766702e2012-07-28 14:11:31 +02004365/*
4366 * Updates compare related fields in sctx and simply forwards to the actual
4367 * changed_xxx functions.
4368 */
Alexander Block31db9f72012-07-25 23:19:24 +02004369static int changed_cb(struct btrfs_root *left_root,
4370 struct btrfs_root *right_root,
4371 struct btrfs_path *left_path,
4372 struct btrfs_path *right_path,
4373 struct btrfs_key *key,
4374 enum btrfs_compare_tree_result result,
4375 void *ctx)
4376{
4377 int ret = 0;
4378 struct send_ctx *sctx = ctx;
4379
4380 sctx->left_path = left_path;
4381 sctx->right_path = right_path;
4382 sctx->cmp_key = key;
4383
4384 ret = finish_inode_if_needed(sctx, 0);
4385 if (ret < 0)
4386 goto out;
4387
Alexander Block2981e222012-08-01 14:47:03 +02004388 /* Ignore non-FS objects */
4389 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4390 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4391 goto out;
4392
Alexander Block31db9f72012-07-25 23:19:24 +02004393 if (key->type == BTRFS_INODE_ITEM_KEY)
4394 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004395 else if (key->type == BTRFS_INODE_REF_KEY ||
4396 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004397 ret = changed_ref(sctx, result);
4398 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4399 ret = changed_xattr(sctx, result);
4400 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4401 ret = changed_extent(sctx, result);
4402
4403out:
4404 return ret;
4405}
4406
4407static int full_send_tree(struct send_ctx *sctx)
4408{
4409 int ret;
4410 struct btrfs_trans_handle *trans = NULL;
4411 struct btrfs_root *send_root = sctx->send_root;
4412 struct btrfs_key key;
4413 struct btrfs_key found_key;
4414 struct btrfs_path *path;
4415 struct extent_buffer *eb;
4416 int slot;
4417 u64 start_ctransid;
4418 u64 ctransid;
4419
4420 path = alloc_path_for_send();
4421 if (!path)
4422 return -ENOMEM;
4423
Anand Jain5f3ab902012-12-07 09:28:54 +00004424 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004425 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00004426 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004427
4428 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4429 key.type = BTRFS_INODE_ITEM_KEY;
4430 key.offset = 0;
4431
4432join_trans:
4433 /*
4434 * We need to make sure the transaction does not get committed
4435 * while we do anything on commit roots. Join a transaction to prevent
4436 * this.
4437 */
4438 trans = btrfs_join_transaction(send_root);
4439 if (IS_ERR(trans)) {
4440 ret = PTR_ERR(trans);
4441 trans = NULL;
4442 goto out;
4443 }
4444
4445 /*
Alexander Block766702e2012-07-28 14:11:31 +02004446 * Make sure the tree has not changed after re-joining. We detect this
4447 * by comparing start_ctransid and ctransid. They should always match.
Alexander Block31db9f72012-07-25 23:19:24 +02004448 */
Anand Jain5f3ab902012-12-07 09:28:54 +00004449 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004450 ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00004451 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004452
4453 if (ctransid != start_ctransid) {
4454 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4455 "send was modified in between. This is "
4456 "probably a bug.\n");
4457 ret = -EIO;
4458 goto out;
4459 }
4460
4461 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4462 if (ret < 0)
4463 goto out;
4464 if (ret)
4465 goto out_finish;
4466
4467 while (1) {
4468 /*
4469 * When someone want to commit while we iterate, end the
4470 * joined transaction and rejoin.
4471 */
4472 if (btrfs_should_end_transaction(trans, send_root)) {
4473 ret = btrfs_end_transaction(trans, send_root);
4474 trans = NULL;
4475 if (ret < 0)
4476 goto out;
4477 btrfs_release_path(path);
4478 goto join_trans;
4479 }
4480
4481 eb = path->nodes[0];
4482 slot = path->slots[0];
4483 btrfs_item_key_to_cpu(eb, &found_key, slot);
4484
4485 ret = changed_cb(send_root, NULL, path, NULL,
4486 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4487 if (ret < 0)
4488 goto out;
4489
4490 key.objectid = found_key.objectid;
4491 key.type = found_key.type;
4492 key.offset = found_key.offset + 1;
4493
4494 ret = btrfs_next_item(send_root, path);
4495 if (ret < 0)
4496 goto out;
4497 if (ret) {
4498 ret = 0;
4499 break;
4500 }
4501 }
4502
4503out_finish:
4504 ret = finish_inode_if_needed(sctx, 1);
4505
4506out:
4507 btrfs_free_path(path);
4508 if (trans) {
4509 if (!ret)
4510 ret = btrfs_end_transaction(trans, send_root);
4511 else
4512 btrfs_end_transaction(trans, send_root);
4513 }
4514 return ret;
4515}
4516
4517static int send_subvol(struct send_ctx *sctx)
4518{
4519 int ret;
4520
Stefan Behrensc2c71322013-04-10 17:10:52 +00004521 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
4522 ret = send_header(sctx);
4523 if (ret < 0)
4524 goto out;
4525 }
Alexander Block31db9f72012-07-25 23:19:24 +02004526
4527 ret = send_subvol_begin(sctx);
4528 if (ret < 0)
4529 goto out;
4530
4531 if (sctx->parent_root) {
4532 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4533 changed_cb, sctx);
4534 if (ret < 0)
4535 goto out;
4536 ret = finish_inode_if_needed(sctx, 1);
4537 if (ret < 0)
4538 goto out;
4539 } else {
4540 ret = full_send_tree(sctx);
4541 if (ret < 0)
4542 goto out;
4543 }
4544
4545out:
4546 if (!ret)
4547 ret = close_cur_inode_file(sctx);
4548 else
4549 close_cur_inode_file(sctx);
4550
4551 free_recorded_refs(sctx);
4552 return ret;
4553}
4554
4555long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4556{
4557 int ret = 0;
4558 struct btrfs_root *send_root;
4559 struct btrfs_root *clone_root;
4560 struct btrfs_fs_info *fs_info;
4561 struct btrfs_ioctl_send_args *arg = NULL;
4562 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02004563 struct send_ctx *sctx = NULL;
4564 u32 i;
4565 u64 *clone_sources_tmp = NULL;
4566
4567 if (!capable(CAP_SYS_ADMIN))
4568 return -EPERM;
4569
Al Viro496ad9a2013-01-23 17:07:38 -05004570 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02004571 fs_info = send_root->fs_info;
4572
4573 arg = memdup_user(arg_, sizeof(*arg));
4574 if (IS_ERR(arg)) {
4575 ret = PTR_ERR(arg);
4576 arg = NULL;
4577 goto out;
4578 }
4579
4580 if (!access_ok(VERIFY_READ, arg->clone_sources,
4581 sizeof(*arg->clone_sources *
4582 arg->clone_sources_count))) {
4583 ret = -EFAULT;
4584 goto out;
4585 }
4586
Stefan Behrensc2c71322013-04-10 17:10:52 +00004587 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004588 ret = -EINVAL;
4589 goto out;
4590 }
4591
Alexander Block31db9f72012-07-25 23:19:24 +02004592 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4593 if (!sctx) {
4594 ret = -ENOMEM;
4595 goto out;
4596 }
4597
4598 INIT_LIST_HEAD(&sctx->new_refs);
4599 INIT_LIST_HEAD(&sctx->deleted_refs);
4600 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4601 INIT_LIST_HEAD(&sctx->name_cache_list);
4602
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004603 sctx->flags = arg->flags;
4604
Alexander Block31db9f72012-07-25 23:19:24 +02004605 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00004606 if (!sctx->send_filp) {
4607 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02004608 goto out;
4609 }
4610
4611 sctx->mnt = mnt_file->f_path.mnt;
4612
4613 sctx->send_root = send_root;
4614 sctx->clone_roots_cnt = arg->clone_sources_count;
4615
4616 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4617 sctx->send_buf = vmalloc(sctx->send_max_size);
4618 if (!sctx->send_buf) {
4619 ret = -ENOMEM;
4620 goto out;
4621 }
4622
4623 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4624 if (!sctx->read_buf) {
4625 ret = -ENOMEM;
4626 goto out;
4627 }
4628
4629 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4630 (arg->clone_sources_count + 1));
4631 if (!sctx->clone_roots) {
4632 ret = -ENOMEM;
4633 goto out;
4634 }
4635
4636 if (arg->clone_sources_count) {
4637 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4638 sizeof(*arg->clone_sources));
4639 if (!clone_sources_tmp) {
4640 ret = -ENOMEM;
4641 goto out;
4642 }
4643
4644 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4645 arg->clone_sources_count *
4646 sizeof(*arg->clone_sources));
4647 if (ret) {
4648 ret = -EFAULT;
4649 goto out;
4650 }
4651
4652 for (i = 0; i < arg->clone_sources_count; i++) {
4653 key.objectid = clone_sources_tmp[i];
4654 key.type = BTRFS_ROOT_ITEM_KEY;
4655 key.offset = (u64)-1;
4656 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02004657 if (IS_ERR(clone_root)) {
4658 ret = PTR_ERR(clone_root);
4659 goto out;
4660 }
4661 sctx->clone_roots[i].root = clone_root;
4662 }
4663 vfree(clone_sources_tmp);
4664 clone_sources_tmp = NULL;
4665 }
4666
4667 if (arg->parent_root) {
4668 key.objectid = arg->parent_root;
4669 key.type = BTRFS_ROOT_ITEM_KEY;
4670 key.offset = (u64)-1;
4671 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00004672 if (IS_ERR(sctx->parent_root)) {
4673 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02004674 goto out;
4675 }
4676 }
4677
4678 /*
4679 * Clones from send_root are allowed, but only if the clone source
4680 * is behind the current send position. This is checked while searching
4681 * for possible clone sources.
4682 */
4683 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4684
4685 /* We do a bsearch later */
4686 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4687 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4688 NULL);
4689
4690 ret = send_subvol(sctx);
4691 if (ret < 0)
4692 goto out;
4693
Stefan Behrensc2c71322013-04-10 17:10:52 +00004694 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
4695 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4696 if (ret < 0)
4697 goto out;
4698 ret = send_cmd(sctx);
4699 if (ret < 0)
4700 goto out;
4701 }
Alexander Block31db9f72012-07-25 23:19:24 +02004702
4703out:
Alexander Block31db9f72012-07-25 23:19:24 +02004704 kfree(arg);
4705 vfree(clone_sources_tmp);
4706
4707 if (sctx) {
4708 if (sctx->send_filp)
4709 fput(sctx->send_filp);
4710
4711 vfree(sctx->clone_roots);
4712 vfree(sctx->send_buf);
4713 vfree(sctx->read_buf);
4714
4715 name_cache_free(sctx);
4716
4717 kfree(sctx);
4718 }
4719
4720 return ret;
4721}