blob: 8230d11f2ccac4b450241127101677ed288ca586 [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>
Andy Shevchenkoed848852013-08-21 10:32:13 +030029#include <linux/string.h>
Alexander Block31db9f72012-07-25 23:19:24 +020030
31#include "send.h"
32#include "backref.h"
33#include "locking.h"
34#include "disk-io.h"
35#include "btrfs_inode.h"
36#include "transaction.h"
37
38static int g_verbose = 0;
39
40#define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
41
42/*
43 * A fs_path is a helper to dynamically build path names with unknown size.
44 * It reallocates the internal buffer on demand.
45 * It allows fast adding of path elements on the right side (normal path) and
46 * fast adding to the left side (reversed path). A reversed path can also be
47 * unreversed if needed.
48 */
49struct fs_path {
50 union {
51 struct {
52 char *start;
53 char *end;
54 char *prepared;
55
56 char *buf;
57 int buf_len;
Stefan Behrens35a36212013-08-14 18:12:25 +020058 unsigned int reversed:1;
59 unsigned int virtual_mem:1;
Alexander Block31db9f72012-07-25 23:19:24 +020060 char inline_buf[];
61 };
62 char pad[PAGE_SIZE];
63 };
64};
65#define FS_PATH_INLINE_SIZE \
66 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
67
68
69/* reused for each extent */
70struct clone_root {
71 struct btrfs_root *root;
72 u64 ino;
73 u64 offset;
74
75 u64 found_refs;
76};
77
78#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
79#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
80
81struct send_ctx {
82 struct file *send_filp;
83 loff_t send_off;
84 char *send_buf;
85 u32 send_size;
86 u32 send_max_size;
87 u64 total_send_size;
88 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
Mark Fashehcb95e7b2013-02-04 20:54:57 +000089 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
Alexander Block31db9f72012-07-25 23:19:24 +020090
91 struct vfsmount *mnt;
92
93 struct btrfs_root *send_root;
94 struct btrfs_root *parent_root;
95 struct clone_root *clone_roots;
96 int clone_roots_cnt;
97
98 /* current state of the compare_tree call */
99 struct btrfs_path *left_path;
100 struct btrfs_path *right_path;
101 struct btrfs_key *cmp_key;
102
103 /*
104 * infos of the currently processed inode. In case of deleted inodes,
105 * these are the values from the deleted inode.
106 */
107 u64 cur_ino;
108 u64 cur_inode_gen;
109 int cur_inode_new;
110 int cur_inode_new_gen;
111 int cur_inode_deleted;
Alexander Block31db9f72012-07-25 23:19:24 +0200112 u64 cur_inode_size;
113 u64 cur_inode_mode;
Josef Bacik16e75492013-10-22 12:18:51 -0400114 u64 cur_inode_last_extent;
Alexander Block31db9f72012-07-25 23:19:24 +0200115
116 u64 send_progress;
117
118 struct list_head new_refs;
119 struct list_head deleted_refs;
120
121 struct radix_tree_root name_cache;
122 struct list_head name_cache_list;
123 int name_cache_size;
124
Alexander Block31db9f72012-07-25 23:19:24 +0200125 char *read_buf;
126};
127
128struct name_cache_entry {
129 struct list_head list;
Alexander Block7e0926f2012-07-28 14:20:58 +0200130 /*
131 * radix_tree has only 32bit entries but we need to handle 64bit inums.
132 * We use the lower 32bit of the 64bit inum to store it in the tree. If
133 * more then one inum would fall into the same entry, we use radix_list
134 * to store the additional entries. radix_list is also used to store
135 * entries where two entries have the same inum but different
136 * generations.
137 */
138 struct list_head radix_list;
Alexander Block31db9f72012-07-25 23:19:24 +0200139 u64 ino;
140 u64 gen;
141 u64 parent_ino;
142 u64 parent_gen;
143 int ret;
144 int need_later_update;
145 int name_len;
146 char name[];
147};
148
Josef Bacik16e75492013-10-22 12:18:51 -0400149static int need_send_hole(struct send_ctx *sctx)
150{
151 return (sctx->parent_root && !sctx->cur_inode_new &&
152 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
153 S_ISREG(sctx->cur_inode_mode));
154}
155
Alexander Block31db9f72012-07-25 23:19:24 +0200156static void fs_path_reset(struct fs_path *p)
157{
158 if (p->reversed) {
159 p->start = p->buf + p->buf_len - 1;
160 p->end = p->start;
161 *p->start = 0;
162 } else {
163 p->start = p->buf;
164 p->end = p->start;
165 *p->start = 0;
166 }
167}
168
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000169static struct fs_path *fs_path_alloc(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200170{
171 struct fs_path *p;
172
173 p = kmalloc(sizeof(*p), GFP_NOFS);
174 if (!p)
175 return NULL;
176 p->reversed = 0;
177 p->virtual_mem = 0;
178 p->buf = p->inline_buf;
179 p->buf_len = FS_PATH_INLINE_SIZE;
180 fs_path_reset(p);
181 return p;
182}
183
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000184static struct fs_path *fs_path_alloc_reversed(void)
Alexander Block31db9f72012-07-25 23:19:24 +0200185{
186 struct fs_path *p;
187
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000188 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +0200189 if (!p)
190 return NULL;
191 p->reversed = 1;
192 fs_path_reset(p);
193 return p;
194}
195
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000196static void fs_path_free(struct fs_path *p)
Alexander Block31db9f72012-07-25 23:19:24 +0200197{
198 if (!p)
199 return;
200 if (p->buf != p->inline_buf) {
201 if (p->virtual_mem)
202 vfree(p->buf);
203 else
204 kfree(p->buf);
205 }
206 kfree(p);
207}
208
209static int fs_path_len(struct fs_path *p)
210{
211 return p->end - p->start;
212}
213
214static int fs_path_ensure_buf(struct fs_path *p, int len)
215{
216 char *tmp_buf;
217 int path_len;
218 int old_buf_len;
219
220 len++;
221
222 if (p->buf_len >= len)
223 return 0;
224
225 path_len = p->end - p->start;
226 old_buf_len = p->buf_len;
227 len = PAGE_ALIGN(len);
228
229 if (p->buf == p->inline_buf) {
Joe Perches8be04b92013-06-19 12:15:53 -0700230 tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN);
Alexander Block31db9f72012-07-25 23:19:24 +0200231 if (!tmp_buf) {
232 tmp_buf = vmalloc(len);
233 if (!tmp_buf)
234 return -ENOMEM;
235 p->virtual_mem = 1;
236 }
237 memcpy(tmp_buf, p->buf, p->buf_len);
238 p->buf = tmp_buf;
239 p->buf_len = len;
240 } else {
241 if (p->virtual_mem) {
242 tmp_buf = vmalloc(len);
243 if (!tmp_buf)
244 return -ENOMEM;
245 memcpy(tmp_buf, p->buf, p->buf_len);
246 vfree(p->buf);
247 } else {
248 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
249 if (!tmp_buf) {
250 tmp_buf = vmalloc(len);
251 if (!tmp_buf)
252 return -ENOMEM;
253 memcpy(tmp_buf, p->buf, p->buf_len);
254 kfree(p->buf);
255 p->virtual_mem = 1;
256 }
257 }
258 p->buf = tmp_buf;
259 p->buf_len = len;
260 }
261 if (p->reversed) {
262 tmp_buf = p->buf + old_buf_len - path_len - 1;
263 p->end = p->buf + p->buf_len - 1;
264 p->start = p->end - path_len;
265 memmove(p->start, tmp_buf, path_len + 1);
266 } else {
267 p->start = p->buf;
268 p->end = p->start + path_len;
269 }
270 return 0;
271}
272
273static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
274{
275 int ret;
276 int new_len;
277
278 new_len = p->end - p->start + name_len;
279 if (p->start != p->end)
280 new_len++;
281 ret = fs_path_ensure_buf(p, new_len);
282 if (ret < 0)
283 goto out;
284
285 if (p->reversed) {
286 if (p->start != p->end)
287 *--p->start = '/';
288 p->start -= name_len;
289 p->prepared = p->start;
290 } else {
291 if (p->start != p->end)
292 *p->end++ = '/';
293 p->prepared = p->end;
294 p->end += name_len;
295 *p->end = 0;
296 }
297
298out:
299 return ret;
300}
301
302static int fs_path_add(struct fs_path *p, const char *name, int name_len)
303{
304 int ret;
305
306 ret = fs_path_prepare_for_add(p, name_len);
307 if (ret < 0)
308 goto out;
309 memcpy(p->prepared, name, name_len);
310 p->prepared = NULL;
311
312out:
313 return ret;
314}
315
316static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
317{
318 int ret;
319
320 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
321 if (ret < 0)
322 goto out;
323 memcpy(p->prepared, p2->start, p2->end - p2->start);
324 p->prepared = NULL;
325
326out:
327 return ret;
328}
329
330static int fs_path_add_from_extent_buffer(struct fs_path *p,
331 struct extent_buffer *eb,
332 unsigned long off, int len)
333{
334 int ret;
335
336 ret = fs_path_prepare_for_add(p, len);
337 if (ret < 0)
338 goto out;
339
340 read_extent_buffer(eb, p->prepared, off, len);
341 p->prepared = NULL;
342
343out:
344 return ret;
345}
346
Alexander Block31db9f72012-07-25 23:19:24 +0200347static int fs_path_copy(struct fs_path *p, struct fs_path *from)
348{
349 int ret;
350
351 p->reversed = from->reversed;
352 fs_path_reset(p);
353
354 ret = fs_path_add_path(p, from);
355
356 return ret;
357}
358
359
360static void fs_path_unreverse(struct fs_path *p)
361{
362 char *tmp;
363 int len;
364
365 if (!p->reversed)
366 return;
367
368 tmp = p->start;
369 len = p->end - p->start;
370 p->start = p->buf;
371 p->end = p->start + len;
372 memmove(p->start, tmp, len + 1);
373 p->reversed = 0;
374}
375
376static struct btrfs_path *alloc_path_for_send(void)
377{
378 struct btrfs_path *path;
379
380 path = btrfs_alloc_path();
381 if (!path)
382 return NULL;
383 path->search_commit_root = 1;
384 path->skip_locking = 1;
385 return path;
386}
387
Eric Sandeen48a3b632013-04-25 20:41:01 +0000388static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
Alexander Block31db9f72012-07-25 23:19:24 +0200389{
390 int ret;
391 mm_segment_t old_fs;
392 u32 pos = 0;
393
394 old_fs = get_fs();
395 set_fs(KERNEL_DS);
396
397 while (pos < len) {
Anand Jain1bcea352012-09-14 00:04:21 -0600398 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
Alexander Block31db9f72012-07-25 23:19:24 +0200399 /* TODO handle that correctly */
400 /*if (ret == -ERESTARTSYS) {
401 continue;
402 }*/
403 if (ret < 0)
404 goto out;
405 if (ret == 0) {
406 ret = -EIO;
407 goto out;
408 }
409 pos += ret;
410 }
411
412 ret = 0;
413
414out:
415 set_fs(old_fs);
416 return ret;
417}
418
419static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
420{
421 struct btrfs_tlv_header *hdr;
422 int total_len = sizeof(*hdr) + len;
423 int left = sctx->send_max_size - sctx->send_size;
424
425 if (unlikely(left < total_len))
426 return -EOVERFLOW;
427
428 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
429 hdr->tlv_type = cpu_to_le16(attr);
430 hdr->tlv_len = cpu_to_le16(len);
431 memcpy(hdr + 1, data, len);
432 sctx->send_size += total_len;
433
434 return 0;
435}
436
David Sterba95bc79d2013-12-16 17:34:10 +0100437#define TLV_PUT_DEFINE_INT(bits) \
438 static int tlv_put_u##bits(struct send_ctx *sctx, \
439 u##bits attr, u##bits value) \
440 { \
441 __le##bits __tmp = cpu_to_le##bits(value); \
442 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
443 }
Alexander Block31db9f72012-07-25 23:19:24 +0200444
David Sterba95bc79d2013-12-16 17:34:10 +0100445TLV_PUT_DEFINE_INT(64)
Alexander Block31db9f72012-07-25 23:19:24 +0200446
447static int tlv_put_string(struct send_ctx *sctx, u16 attr,
448 const char *str, int len)
449{
450 if (len == -1)
451 len = strlen(str);
452 return tlv_put(sctx, attr, str, len);
453}
454
455static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
456 const u8 *uuid)
457{
458 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
459}
460
Alexander Block31db9f72012-07-25 23:19:24 +0200461static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
462 struct extent_buffer *eb,
463 struct btrfs_timespec *ts)
464{
465 struct btrfs_timespec bts;
466 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
467 return tlv_put(sctx, attr, &bts, sizeof(bts));
468}
469
470
471#define TLV_PUT(sctx, attrtype, attrlen, data) \
472 do { \
473 ret = tlv_put(sctx, attrtype, attrlen, data); \
474 if (ret < 0) \
475 goto tlv_put_failure; \
476 } while (0)
477
478#define TLV_PUT_INT(sctx, attrtype, bits, value) \
479 do { \
480 ret = tlv_put_u##bits(sctx, attrtype, value); \
481 if (ret < 0) \
482 goto tlv_put_failure; \
483 } while (0)
484
485#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
486#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
487#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
488#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
489#define TLV_PUT_STRING(sctx, attrtype, str, len) \
490 do { \
491 ret = tlv_put_string(sctx, attrtype, str, len); \
492 if (ret < 0) \
493 goto tlv_put_failure; \
494 } while (0)
495#define TLV_PUT_PATH(sctx, attrtype, p) \
496 do { \
497 ret = tlv_put_string(sctx, attrtype, p->start, \
498 p->end - p->start); \
499 if (ret < 0) \
500 goto tlv_put_failure; \
501 } while(0)
502#define TLV_PUT_UUID(sctx, attrtype, uuid) \
503 do { \
504 ret = tlv_put_uuid(sctx, attrtype, uuid); \
505 if (ret < 0) \
506 goto tlv_put_failure; \
507 } while (0)
Alexander Block31db9f72012-07-25 23:19:24 +0200508#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
509 do { \
510 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
511 if (ret < 0) \
512 goto tlv_put_failure; \
513 } while (0)
514
515static int send_header(struct send_ctx *sctx)
516{
517 struct btrfs_stream_header hdr;
518
519 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
520 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
521
Anand Jain1bcea352012-09-14 00:04:21 -0600522 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
523 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200524}
525
526/*
527 * For each command/item we want to send to userspace, we call this function.
528 */
529static int begin_cmd(struct send_ctx *sctx, int cmd)
530{
531 struct btrfs_cmd_header *hdr;
532
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530533 if (WARN_ON(!sctx->send_buf))
Alexander Block31db9f72012-07-25 23:19:24 +0200534 return -EINVAL;
Alexander Block31db9f72012-07-25 23:19:24 +0200535
536 BUG_ON(sctx->send_size);
537
538 sctx->send_size += sizeof(*hdr);
539 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
540 hdr->cmd = cpu_to_le16(cmd);
541
542 return 0;
543}
544
545static int send_cmd(struct send_ctx *sctx)
546{
547 int ret;
548 struct btrfs_cmd_header *hdr;
549 u32 crc;
550
551 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
552 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
553 hdr->crc = 0;
554
555 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
556 hdr->crc = cpu_to_le32(crc);
557
Anand Jain1bcea352012-09-14 00:04:21 -0600558 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
559 &sctx->send_off);
Alexander Block31db9f72012-07-25 23:19:24 +0200560
561 sctx->total_send_size += sctx->send_size;
562 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
563 sctx->send_size = 0;
564
565 return ret;
566}
567
568/*
569 * Sends a move instruction to user space
570 */
571static int send_rename(struct send_ctx *sctx,
572 struct fs_path *from, struct fs_path *to)
573{
574 int ret;
575
576verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
577
578 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
579 if (ret < 0)
580 goto out;
581
582 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
583 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
584
585 ret = send_cmd(sctx);
586
587tlv_put_failure:
588out:
589 return ret;
590}
591
592/*
593 * Sends a link instruction to user space
594 */
595static int send_link(struct send_ctx *sctx,
596 struct fs_path *path, struct fs_path *lnk)
597{
598 int ret;
599
600verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
601
602 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
603 if (ret < 0)
604 goto out;
605
606 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
607 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
608
609 ret = send_cmd(sctx);
610
611tlv_put_failure:
612out:
613 return ret;
614}
615
616/*
617 * Sends an unlink instruction to user space
618 */
619static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
620{
621 int ret;
622
623verbose_printk("btrfs: send_unlink %s\n", path->start);
624
625 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
626 if (ret < 0)
627 goto out;
628
629 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
630
631 ret = send_cmd(sctx);
632
633tlv_put_failure:
634out:
635 return ret;
636}
637
638/*
639 * Sends a rmdir instruction to user space
640 */
641static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
642{
643 int ret;
644
645verbose_printk("btrfs: send_rmdir %s\n", path->start);
646
647 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
648 if (ret < 0)
649 goto out;
650
651 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
652
653 ret = send_cmd(sctx);
654
655tlv_put_failure:
656out:
657 return ret;
658}
659
660/*
661 * Helper function to retrieve some fields from an inode item.
662 */
663static int get_inode_info(struct btrfs_root *root,
664 u64 ino, u64 *size, u64 *gen,
Alexander Block85a7b332012-07-26 23:39:10 +0200665 u64 *mode, u64 *uid, u64 *gid,
666 u64 *rdev)
Alexander Block31db9f72012-07-25 23:19:24 +0200667{
668 int ret;
669 struct btrfs_inode_item *ii;
670 struct btrfs_key key;
671 struct btrfs_path *path;
672
673 path = alloc_path_for_send();
674 if (!path)
675 return -ENOMEM;
676
677 key.objectid = ino;
678 key.type = BTRFS_INODE_ITEM_KEY;
679 key.offset = 0;
680 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
681 if (ret < 0)
682 goto out;
683 if (ret) {
684 ret = -ENOENT;
685 goto out;
686 }
687
688 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
689 struct btrfs_inode_item);
690 if (size)
691 *size = btrfs_inode_size(path->nodes[0], ii);
692 if (gen)
693 *gen = btrfs_inode_generation(path->nodes[0], ii);
694 if (mode)
695 *mode = btrfs_inode_mode(path->nodes[0], ii);
696 if (uid)
697 *uid = btrfs_inode_uid(path->nodes[0], ii);
698 if (gid)
699 *gid = btrfs_inode_gid(path->nodes[0], ii);
Alexander Block85a7b332012-07-26 23:39:10 +0200700 if (rdev)
701 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
Alexander Block31db9f72012-07-25 23:19:24 +0200702
703out:
704 btrfs_free_path(path);
705 return ret;
706}
707
708typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
709 struct fs_path *p,
710 void *ctx);
711
712/*
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000713 * Helper function to iterate the entries in ONE btrfs_inode_ref or
714 * btrfs_inode_extref.
Alexander Block31db9f72012-07-25 23:19:24 +0200715 * The iterate callback may return a non zero value to stop iteration. This can
716 * be a negative value for error codes or 1 to simply stop it.
717 *
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000718 * path must point to the INODE_REF or INODE_EXTREF when called.
Alexander Block31db9f72012-07-25 23:19:24 +0200719 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000720static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200721 struct btrfs_key *found_key, int resolve,
722 iterate_inode_ref_t iterate, void *ctx)
723{
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000724 struct extent_buffer *eb = path->nodes[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200725 struct btrfs_item *item;
726 struct btrfs_inode_ref *iref;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000727 struct btrfs_inode_extref *extref;
Alexander Block31db9f72012-07-25 23:19:24 +0200728 struct btrfs_path *tmp_path;
729 struct fs_path *p;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000730 u32 cur = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200731 u32 total;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000732 int slot = path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +0200733 u32 name_len;
734 char *start;
735 int ret = 0;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000736 int num = 0;
Alexander Block31db9f72012-07-25 23:19:24 +0200737 int index;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000738 u64 dir;
739 unsigned long name_off;
740 unsigned long elem_size;
741 unsigned long ptr;
Alexander Block31db9f72012-07-25 23:19:24 +0200742
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000743 p = fs_path_alloc_reversed();
Alexander Block31db9f72012-07-25 23:19:24 +0200744 if (!p)
745 return -ENOMEM;
746
747 tmp_path = alloc_path_for_send();
748 if (!tmp_path) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000749 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200750 return -ENOMEM;
751 }
752
Alexander Block31db9f72012-07-25 23:19:24 +0200753
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000754 if (found_key->type == BTRFS_INODE_REF_KEY) {
755 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
756 struct btrfs_inode_ref);
Ross Kirkdd3cc162013-09-16 15:58:09 +0100757 item = btrfs_item_nr(slot);
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000758 total = btrfs_item_size(eb, item);
759 elem_size = sizeof(*iref);
760 } else {
761 ptr = btrfs_item_ptr_offset(eb, slot);
762 total = btrfs_item_size_nr(eb, slot);
763 elem_size = sizeof(*extref);
764 }
765
Alexander Block31db9f72012-07-25 23:19:24 +0200766 while (cur < total) {
767 fs_path_reset(p);
768
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000769 if (found_key->type == BTRFS_INODE_REF_KEY) {
770 iref = (struct btrfs_inode_ref *)(ptr + cur);
771 name_len = btrfs_inode_ref_name_len(eb, iref);
772 name_off = (unsigned long)(iref + 1);
773 index = btrfs_inode_ref_index(eb, iref);
774 dir = found_key->offset;
775 } else {
776 extref = (struct btrfs_inode_extref *)(ptr + cur);
777 name_len = btrfs_inode_extref_name_len(eb, extref);
778 name_off = (unsigned long)&extref->name;
779 index = btrfs_inode_extref_index(eb, extref);
780 dir = btrfs_inode_extref_parent(eb, extref);
781 }
782
Alexander Block31db9f72012-07-25 23:19:24 +0200783 if (resolve) {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000784 start = btrfs_ref_to_path(root, tmp_path, name_len,
785 name_off, eb, dir,
786 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200787 if (IS_ERR(start)) {
788 ret = PTR_ERR(start);
789 goto out;
790 }
791 if (start < p->buf) {
792 /* overflow , try again with larger buffer */
793 ret = fs_path_ensure_buf(p,
794 p->buf_len + p->buf - start);
795 if (ret < 0)
796 goto out;
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000797 start = btrfs_ref_to_path(root, tmp_path,
798 name_len, name_off,
799 eb, dir,
800 p->buf, p->buf_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200801 if (IS_ERR(start)) {
802 ret = PTR_ERR(start);
803 goto out;
804 }
805 BUG_ON(start < p->buf);
806 }
807 p->start = start;
808 } else {
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000809 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
810 name_len);
Alexander Block31db9f72012-07-25 23:19:24 +0200811 if (ret < 0)
812 goto out;
813 }
814
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000815 cur += elem_size + name_len;
816 ret = iterate(num, dir, index, p, ctx);
Alexander Block31db9f72012-07-25 23:19:24 +0200817 if (ret)
818 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +0200819 num++;
820 }
821
822out:
823 btrfs_free_path(tmp_path);
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000824 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +0200825 return ret;
826}
827
828typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
829 const char *name, int name_len,
830 const char *data, int data_len,
831 u8 type, void *ctx);
832
833/*
834 * Helper function to iterate the entries in ONE btrfs_dir_item.
835 * The iterate callback may return a non zero value to stop iteration. This can
836 * be a negative value for error codes or 1 to simply stop it.
837 *
838 * path must point to the dir item when called.
839 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000840static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
Alexander Block31db9f72012-07-25 23:19:24 +0200841 struct btrfs_key *found_key,
842 iterate_dir_item_t iterate, void *ctx)
843{
844 int ret = 0;
845 struct extent_buffer *eb;
846 struct btrfs_item *item;
847 struct btrfs_dir_item *di;
Alexander Block31db9f72012-07-25 23:19:24 +0200848 struct btrfs_key di_key;
849 char *buf = NULL;
850 char *buf2 = NULL;
851 int buf_len;
852 int buf_virtual = 0;
853 u32 name_len;
854 u32 data_len;
855 u32 cur;
856 u32 len;
857 u32 total;
858 int slot;
859 int num;
860 u8 type;
861
862 buf_len = PAGE_SIZE;
863 buf = kmalloc(buf_len, GFP_NOFS);
864 if (!buf) {
865 ret = -ENOMEM;
866 goto out;
867 }
868
Alexander Block31db9f72012-07-25 23:19:24 +0200869 eb = path->nodes[0];
870 slot = path->slots[0];
Ross Kirkdd3cc162013-09-16 15:58:09 +0100871 item = btrfs_item_nr(slot);
Alexander Block31db9f72012-07-25 23:19:24 +0200872 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
873 cur = 0;
874 len = 0;
875 total = btrfs_item_size(eb, item);
876
877 num = 0;
878 while (cur < total) {
879 name_len = btrfs_dir_name_len(eb, di);
880 data_len = btrfs_dir_data_len(eb, di);
881 type = btrfs_dir_type(eb, di);
882 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
883
884 if (name_len + data_len > buf_len) {
885 buf_len = PAGE_ALIGN(name_len + data_len);
886 if (buf_virtual) {
887 buf2 = vmalloc(buf_len);
888 if (!buf2) {
889 ret = -ENOMEM;
890 goto out;
891 }
892 vfree(buf);
893 } else {
894 buf2 = krealloc(buf, buf_len, GFP_NOFS);
895 if (!buf2) {
896 buf2 = vmalloc(buf_len);
897 if (!buf2) {
898 ret = -ENOMEM;
899 goto out;
900 }
901 kfree(buf);
902 buf_virtual = 1;
903 }
904 }
905
906 buf = buf2;
907 buf2 = NULL;
908 }
909
910 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
911 name_len + data_len);
912
913 len = sizeof(*di) + name_len + data_len;
914 di = (struct btrfs_dir_item *)((char *)di + len);
915 cur += len;
916
917 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
918 data_len, type, ctx);
919 if (ret < 0)
920 goto out;
921 if (ret) {
922 ret = 0;
923 goto out;
924 }
925
926 num++;
927 }
928
929out:
Alexander Block31db9f72012-07-25 23:19:24 +0200930 if (buf_virtual)
931 vfree(buf);
932 else
933 kfree(buf);
934 return ret;
935}
936
937static int __copy_first_ref(int num, u64 dir, int index,
938 struct fs_path *p, void *ctx)
939{
940 int ret;
941 struct fs_path *pt = ctx;
942
943 ret = fs_path_copy(pt, p);
944 if (ret < 0)
945 return ret;
946
947 /* we want the first only */
948 return 1;
949}
950
951/*
952 * Retrieve the first path of an inode. If an inode has more then one
953 * ref/hardlink, this is ignored.
954 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000955static int get_inode_path(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +0200956 u64 ino, struct fs_path *path)
957{
958 int ret;
959 struct btrfs_key key, found_key;
960 struct btrfs_path *p;
961
962 p = alloc_path_for_send();
963 if (!p)
964 return -ENOMEM;
965
966 fs_path_reset(path);
967
968 key.objectid = ino;
969 key.type = BTRFS_INODE_REF_KEY;
970 key.offset = 0;
971
972 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
973 if (ret < 0)
974 goto out;
975 if (ret) {
976 ret = 1;
977 goto out;
978 }
979 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
980 if (found_key.objectid != ino ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +0000981 (found_key.type != BTRFS_INODE_REF_KEY &&
982 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +0200983 ret = -ENOENT;
984 goto out;
985 }
986
Tsutomu Itoh924794c2013-05-08 07:51:52 +0000987 ret = iterate_inode_ref(root, p, &found_key, 1,
988 __copy_first_ref, path);
Alexander Block31db9f72012-07-25 23:19:24 +0200989 if (ret < 0)
990 goto out;
991 ret = 0;
992
993out:
994 btrfs_free_path(p);
995 return ret;
996}
997
998struct backref_ctx {
999 struct send_ctx *sctx;
1000
1001 /* number of total found references */
1002 u64 found;
1003
1004 /*
1005 * used for clones found in send_root. clones found behind cur_objectid
1006 * and cur_offset are not considered as allowed clones.
1007 */
1008 u64 cur_objectid;
1009 u64 cur_offset;
1010
1011 /* may be truncated in case it's the last extent in a file */
1012 u64 extent_len;
1013
1014 /* Just to check for bugs in backref resolving */
Alexander Blockee849c02012-07-28 12:42:05 +02001015 int found_itself;
Alexander Block31db9f72012-07-25 23:19:24 +02001016};
1017
1018static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1019{
Jan Schmidt995e01b2012-08-13 02:52:38 -06001020 u64 root = (u64)(uintptr_t)key;
Alexander Block31db9f72012-07-25 23:19:24 +02001021 struct clone_root *cr = (struct clone_root *)elt;
1022
1023 if (root < cr->root->objectid)
1024 return -1;
1025 if (root > cr->root->objectid)
1026 return 1;
1027 return 0;
1028}
1029
1030static int __clone_root_cmp_sort(const void *e1, const void *e2)
1031{
1032 struct clone_root *cr1 = (struct clone_root *)e1;
1033 struct clone_root *cr2 = (struct clone_root *)e2;
1034
1035 if (cr1->root->objectid < cr2->root->objectid)
1036 return -1;
1037 if (cr1->root->objectid > cr2->root->objectid)
1038 return 1;
1039 return 0;
1040}
1041
1042/*
1043 * Called for every backref that is found for the current extent.
Alexander Block766702e2012-07-28 14:11:31 +02001044 * Results are collected in sctx->clone_roots->ino/offset/found_refs
Alexander Block31db9f72012-07-25 23:19:24 +02001045 */
1046static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1047{
1048 struct backref_ctx *bctx = ctx_;
1049 struct clone_root *found;
1050 int ret;
1051 u64 i_size;
1052
1053 /* First check if the root is in the list of accepted clone sources */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001054 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
Alexander Block31db9f72012-07-25 23:19:24 +02001055 bctx->sctx->clone_roots_cnt,
1056 sizeof(struct clone_root),
1057 __clone_root_cmp_bsearch);
1058 if (!found)
1059 return 0;
1060
1061 if (found->root == bctx->sctx->send_root &&
1062 ino == bctx->cur_objectid &&
1063 offset == bctx->cur_offset) {
Alexander Blockee849c02012-07-28 12:42:05 +02001064 bctx->found_itself = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02001065 }
1066
1067 /*
Alexander Block766702e2012-07-28 14:11:31 +02001068 * There are inodes that have extents that lie behind its i_size. Don't
Alexander Block31db9f72012-07-25 23:19:24 +02001069 * accept clones from these extents.
1070 */
Alexander Block85a7b332012-07-26 23:39:10 +02001071 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1072 NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001073 if (ret < 0)
1074 return ret;
1075
1076 if (offset + bctx->extent_len > i_size)
1077 return 0;
1078
1079 /*
1080 * Make sure we don't consider clones from send_root that are
1081 * behind the current inode/offset.
1082 */
1083 if (found->root == bctx->sctx->send_root) {
1084 /*
1085 * TODO for the moment we don't accept clones from the inode
1086 * that is currently send. We may change this when
1087 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1088 * file.
1089 */
1090 if (ino >= bctx->cur_objectid)
1091 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001092#if 0
1093 if (ino > bctx->cur_objectid)
Alexander Block31db9f72012-07-25 23:19:24 +02001094 return 0;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001095 if (offset + bctx->extent_len > bctx->cur_offset)
1096 return 0;
1097#endif
Alexander Block31db9f72012-07-25 23:19:24 +02001098 }
1099
1100 bctx->found++;
1101 found->found_refs++;
1102 if (ino < found->ino) {
1103 found->ino = ino;
1104 found->offset = offset;
1105 } else if (found->ino == ino) {
1106 /*
1107 * same extent found more then once in the same file.
1108 */
1109 if (found->offset > offset + bctx->extent_len)
1110 found->offset = offset;
1111 }
1112
1113 return 0;
1114}
1115
1116/*
Alexander Block766702e2012-07-28 14:11:31 +02001117 * Given an inode, offset and extent item, it finds a good clone for a clone
1118 * instruction. Returns -ENOENT when none could be found. The function makes
1119 * sure that the returned clone is usable at the point where sending is at the
1120 * moment. This means, that no clones are accepted which lie behind the current
1121 * inode+offset.
1122 *
Alexander Block31db9f72012-07-25 23:19:24 +02001123 * path must point to the extent item when called.
1124 */
1125static int find_extent_clone(struct send_ctx *sctx,
1126 struct btrfs_path *path,
1127 u64 ino, u64 data_offset,
1128 u64 ino_size,
1129 struct clone_root **found)
1130{
1131 int ret;
1132 int extent_type;
1133 u64 logical;
Chris Mason74dd17f2012-08-07 16:25:13 -04001134 u64 disk_byte;
Alexander Block31db9f72012-07-25 23:19:24 +02001135 u64 num_bytes;
1136 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -06001137 u64 flags = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02001138 struct btrfs_file_extent_item *fi;
1139 struct extent_buffer *eb = path->nodes[0];
Alexander Block35075bb2012-07-28 12:44:34 +02001140 struct backref_ctx *backref_ctx = NULL;
Alexander Block31db9f72012-07-25 23:19:24 +02001141 struct clone_root *cur_clone_root;
1142 struct btrfs_key found_key;
1143 struct btrfs_path *tmp_path;
Chris Mason74dd17f2012-08-07 16:25:13 -04001144 int compressed;
Alexander Block31db9f72012-07-25 23:19:24 +02001145 u32 i;
1146
1147 tmp_path = alloc_path_for_send();
1148 if (!tmp_path)
1149 return -ENOMEM;
1150
Alexander Block35075bb2012-07-28 12:44:34 +02001151 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1152 if (!backref_ctx) {
1153 ret = -ENOMEM;
1154 goto out;
1155 }
1156
Alexander Block31db9f72012-07-25 23:19:24 +02001157 if (data_offset >= ino_size) {
1158 /*
1159 * There may be extents that lie behind the file's size.
1160 * I at least had this in combination with snapshotting while
1161 * writing large files.
1162 */
1163 ret = 0;
1164 goto out;
1165 }
1166
1167 fi = btrfs_item_ptr(eb, path->slots[0],
1168 struct btrfs_file_extent_item);
1169 extent_type = btrfs_file_extent_type(eb, fi);
1170 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1171 ret = -ENOENT;
1172 goto out;
1173 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001174 compressed = btrfs_file_extent_compression(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001175
1176 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
Chris Mason74dd17f2012-08-07 16:25:13 -04001177 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1178 if (disk_byte == 0) {
Alexander Block31db9f72012-07-25 23:19:24 +02001179 ret = -ENOENT;
1180 goto out;
1181 }
Chris Mason74dd17f2012-08-07 16:25:13 -04001182 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
Alexander Block31db9f72012-07-25 23:19:24 +02001183
Liu Bo69917e42012-09-07 20:01:28 -06001184 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1185 &found_key, &flags);
Alexander Block31db9f72012-07-25 23:19:24 +02001186 btrfs_release_path(tmp_path);
1187
1188 if (ret < 0)
1189 goto out;
Liu Bo69917e42012-09-07 20:01:28 -06001190 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Alexander Block31db9f72012-07-25 23:19:24 +02001191 ret = -EIO;
1192 goto out;
1193 }
1194
1195 /*
1196 * Setup the clone roots.
1197 */
1198 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1199 cur_clone_root = sctx->clone_roots + i;
1200 cur_clone_root->ino = (u64)-1;
1201 cur_clone_root->offset = 0;
1202 cur_clone_root->found_refs = 0;
1203 }
1204
Alexander Block35075bb2012-07-28 12:44:34 +02001205 backref_ctx->sctx = sctx;
1206 backref_ctx->found = 0;
1207 backref_ctx->cur_objectid = ino;
1208 backref_ctx->cur_offset = data_offset;
1209 backref_ctx->found_itself = 0;
1210 backref_ctx->extent_len = num_bytes;
Alexander Block31db9f72012-07-25 23:19:24 +02001211
1212 /*
1213 * The last extent of a file may be too large due to page alignment.
1214 * We need to adjust extent_len in this case so that the checks in
1215 * __iterate_backrefs work.
1216 */
1217 if (data_offset + num_bytes >= ino_size)
Alexander Block35075bb2012-07-28 12:44:34 +02001218 backref_ctx->extent_len = ino_size - data_offset;
Alexander Block31db9f72012-07-25 23:19:24 +02001219
1220 /*
1221 * Now collect all backrefs.
1222 */
Chris Mason74dd17f2012-08-07 16:25:13 -04001223 if (compressed == BTRFS_COMPRESS_NONE)
1224 extent_item_pos = logical - found_key.objectid;
1225 else
1226 extent_item_pos = 0;
1227
Alexander Block31db9f72012-07-25 23:19:24 +02001228 extent_item_pos = logical - found_key.objectid;
1229 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1230 found_key.objectid, extent_item_pos, 1,
Alexander Block35075bb2012-07-28 12:44:34 +02001231 __iterate_backrefs, backref_ctx);
Chris Mason74dd17f2012-08-07 16:25:13 -04001232
Alexander Block31db9f72012-07-25 23:19:24 +02001233 if (ret < 0)
1234 goto out;
1235
Alexander Block35075bb2012-07-28 12:44:34 +02001236 if (!backref_ctx->found_itself) {
Alexander Block31db9f72012-07-25 23:19:24 +02001237 /* found a bug in backref code? */
1238 ret = -EIO;
1239 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1240 "send_root. inode=%llu, offset=%llu, "
Chris Mason74dd17f2012-08-07 16:25:13 -04001241 "disk_byte=%llu found extent=%llu\n",
1242 ino, data_offset, disk_byte, found_key.objectid);
Alexander Block31db9f72012-07-25 23:19:24 +02001243 goto out;
1244 }
1245
1246verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1247 "ino=%llu, "
1248 "num_bytes=%llu, logical=%llu\n",
1249 data_offset, ino, num_bytes, logical);
1250
Alexander Block35075bb2012-07-28 12:44:34 +02001251 if (!backref_ctx->found)
Alexander Block31db9f72012-07-25 23:19:24 +02001252 verbose_printk("btrfs: no clones found\n");
1253
1254 cur_clone_root = NULL;
1255 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1256 if (sctx->clone_roots[i].found_refs) {
1257 if (!cur_clone_root)
1258 cur_clone_root = sctx->clone_roots + i;
1259 else if (sctx->clone_roots[i].root == sctx->send_root)
1260 /* prefer clones from send_root over others */
1261 cur_clone_root = sctx->clone_roots + i;
Alexander Block31db9f72012-07-25 23:19:24 +02001262 }
1263
1264 }
1265
1266 if (cur_clone_root) {
1267 *found = cur_clone_root;
1268 ret = 0;
1269 } else {
1270 ret = -ENOENT;
1271 }
1272
1273out:
1274 btrfs_free_path(tmp_path);
Alexander Block35075bb2012-07-28 12:44:34 +02001275 kfree(backref_ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02001276 return ret;
1277}
1278
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001279static int read_symlink(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001280 u64 ino,
1281 struct fs_path *dest)
1282{
1283 int ret;
1284 struct btrfs_path *path;
1285 struct btrfs_key key;
1286 struct btrfs_file_extent_item *ei;
1287 u8 type;
1288 u8 compression;
1289 unsigned long off;
1290 int len;
1291
1292 path = alloc_path_for_send();
1293 if (!path)
1294 return -ENOMEM;
1295
1296 key.objectid = ino;
1297 key.type = BTRFS_EXTENT_DATA_KEY;
1298 key.offset = 0;
1299 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1300 if (ret < 0)
1301 goto out;
1302 BUG_ON(ret);
1303
1304 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1305 struct btrfs_file_extent_item);
1306 type = btrfs_file_extent_type(path->nodes[0], ei);
1307 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1308 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1309 BUG_ON(compression);
1310
1311 off = btrfs_file_extent_inline_start(ei);
1312 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1313
1314 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
Alexander Block31db9f72012-07-25 23:19:24 +02001315
1316out:
1317 btrfs_free_path(path);
1318 return ret;
1319}
1320
1321/*
1322 * Helper function to generate a file name that is unique in the root of
1323 * send_root and parent_root. This is used to generate names for orphan inodes.
1324 */
1325static int gen_unique_name(struct send_ctx *sctx,
1326 u64 ino, u64 gen,
1327 struct fs_path *dest)
1328{
1329 int ret = 0;
1330 struct btrfs_path *path;
1331 struct btrfs_dir_item *di;
1332 char tmp[64];
1333 int len;
1334 u64 idx = 0;
1335
1336 path = alloc_path_for_send();
1337 if (!path)
1338 return -ENOMEM;
1339
1340 while (1) {
1341 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1342 ino, gen, idx);
1343 if (len >= sizeof(tmp)) {
1344 /* should really not happen */
1345 ret = -EOVERFLOW;
1346 goto out;
1347 }
1348
1349 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1350 path, BTRFS_FIRST_FREE_OBJECTID,
1351 tmp, strlen(tmp), 0);
1352 btrfs_release_path(path);
1353 if (IS_ERR(di)) {
1354 ret = PTR_ERR(di);
1355 goto out;
1356 }
1357 if (di) {
1358 /* not unique, try again */
1359 idx++;
1360 continue;
1361 }
1362
1363 if (!sctx->parent_root) {
1364 /* unique */
1365 ret = 0;
1366 break;
1367 }
1368
1369 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1370 path, BTRFS_FIRST_FREE_OBJECTID,
1371 tmp, strlen(tmp), 0);
1372 btrfs_release_path(path);
1373 if (IS_ERR(di)) {
1374 ret = PTR_ERR(di);
1375 goto out;
1376 }
1377 if (di) {
1378 /* not unique, try again */
1379 idx++;
1380 continue;
1381 }
1382 /* unique */
1383 break;
1384 }
1385
1386 ret = fs_path_add(dest, tmp, strlen(tmp));
1387
1388out:
1389 btrfs_free_path(path);
1390 return ret;
1391}
1392
1393enum inode_state {
1394 inode_state_no_change,
1395 inode_state_will_create,
1396 inode_state_did_create,
1397 inode_state_will_delete,
1398 inode_state_did_delete,
1399};
1400
1401static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1402{
1403 int ret;
1404 int left_ret;
1405 int right_ret;
1406 u64 left_gen;
1407 u64 right_gen;
1408
1409 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001410 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001411 if (ret < 0 && ret != -ENOENT)
1412 goto out;
1413 left_ret = ret;
1414
1415 if (!sctx->parent_root) {
1416 right_ret = -ENOENT;
1417 } else {
1418 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
Alexander Block85a7b332012-07-26 23:39:10 +02001419 NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001420 if (ret < 0 && ret != -ENOENT)
1421 goto out;
1422 right_ret = ret;
1423 }
1424
1425 if (!left_ret && !right_ret) {
Alexander Blocke938c8a2012-07-28 16:33:49 +02001426 if (left_gen == gen && right_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001427 ret = inode_state_no_change;
Alexander Blocke938c8a2012-07-28 16:33:49 +02001428 } else if (left_gen == gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02001429 if (ino < sctx->send_progress)
1430 ret = inode_state_did_create;
1431 else
1432 ret = inode_state_will_create;
1433 } else if (right_gen == gen) {
1434 if (ino < sctx->send_progress)
1435 ret = inode_state_did_delete;
1436 else
1437 ret = inode_state_will_delete;
1438 } else {
1439 ret = -ENOENT;
1440 }
1441 } else if (!left_ret) {
1442 if (left_gen == gen) {
1443 if (ino < sctx->send_progress)
1444 ret = inode_state_did_create;
1445 else
1446 ret = inode_state_will_create;
1447 } else {
1448 ret = -ENOENT;
1449 }
1450 } else if (!right_ret) {
1451 if (right_gen == gen) {
1452 if (ino < sctx->send_progress)
1453 ret = inode_state_did_delete;
1454 else
1455 ret = inode_state_will_delete;
1456 } else {
1457 ret = -ENOENT;
1458 }
1459 } else {
1460 ret = -ENOENT;
1461 }
1462
1463out:
1464 return ret;
1465}
1466
1467static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1468{
1469 int ret;
1470
1471 ret = get_cur_inode_state(sctx, ino, gen);
1472 if (ret < 0)
1473 goto out;
1474
1475 if (ret == inode_state_no_change ||
1476 ret == inode_state_did_create ||
1477 ret == inode_state_will_delete)
1478 ret = 1;
1479 else
1480 ret = 0;
1481
1482out:
1483 return ret;
1484}
1485
1486/*
1487 * Helper function to lookup a dir item in a dir.
1488 */
1489static int lookup_dir_item_inode(struct btrfs_root *root,
1490 u64 dir, const char *name, int name_len,
1491 u64 *found_inode,
1492 u8 *found_type)
1493{
1494 int ret = 0;
1495 struct btrfs_dir_item *di;
1496 struct btrfs_key key;
1497 struct btrfs_path *path;
1498
1499 path = alloc_path_for_send();
1500 if (!path)
1501 return -ENOMEM;
1502
1503 di = btrfs_lookup_dir_item(NULL, root, path,
1504 dir, name, name_len, 0);
1505 if (!di) {
1506 ret = -ENOENT;
1507 goto out;
1508 }
1509 if (IS_ERR(di)) {
1510 ret = PTR_ERR(di);
1511 goto out;
1512 }
1513 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1514 *found_inode = key.objectid;
1515 *found_type = btrfs_dir_type(path->nodes[0], di);
1516
1517out:
1518 btrfs_free_path(path);
1519 return ret;
1520}
1521
Alexander Block766702e2012-07-28 14:11:31 +02001522/*
1523 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1524 * generation of the parent dir and the name of the dir entry.
1525 */
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001526static int get_first_ref(struct btrfs_root *root, u64 ino,
Alexander Block31db9f72012-07-25 23:19:24 +02001527 u64 *dir, u64 *dir_gen, struct fs_path *name)
1528{
1529 int ret;
1530 struct btrfs_key key;
1531 struct btrfs_key found_key;
1532 struct btrfs_path *path;
Alexander Block31db9f72012-07-25 23:19:24 +02001533 int len;
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001534 u64 parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001535
1536 path = alloc_path_for_send();
1537 if (!path)
1538 return -ENOMEM;
1539
1540 key.objectid = ino;
1541 key.type = BTRFS_INODE_REF_KEY;
1542 key.offset = 0;
1543
1544 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1545 if (ret < 0)
1546 goto out;
1547 if (!ret)
1548 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1549 path->slots[0]);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001550 if (ret || found_key.objectid != ino ||
1551 (found_key.type != BTRFS_INODE_REF_KEY &&
1552 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001553 ret = -ENOENT;
1554 goto out;
1555 }
1556
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001557 if (key.type == BTRFS_INODE_REF_KEY) {
1558 struct btrfs_inode_ref *iref;
1559 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1560 struct btrfs_inode_ref);
1561 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1562 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1563 (unsigned long)(iref + 1),
1564 len);
1565 parent_dir = found_key.offset;
1566 } else {
1567 struct btrfs_inode_extref *extref;
1568 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1569 struct btrfs_inode_extref);
1570 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1571 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1572 (unsigned long)&extref->name, len);
1573 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1574 }
Alexander Block31db9f72012-07-25 23:19:24 +02001575 if (ret < 0)
1576 goto out;
1577 btrfs_release_path(path);
1578
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001579 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001580 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001581 if (ret < 0)
1582 goto out;
1583
Jan Schmidt96b5bd72012-10-15 08:30:45 +00001584 *dir = parent_dir;
Alexander Block31db9f72012-07-25 23:19:24 +02001585
1586out:
1587 btrfs_free_path(path);
1588 return ret;
1589}
1590
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001591static int is_first_ref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02001592 u64 ino, u64 dir,
1593 const char *name, int name_len)
1594{
1595 int ret;
1596 struct fs_path *tmp_name;
1597 u64 tmp_dir;
1598 u64 tmp_dir_gen;
1599
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001600 tmp_name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001601 if (!tmp_name)
1602 return -ENOMEM;
1603
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001604 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001605 if (ret < 0)
1606 goto out;
1607
Alexander Blockb9291af2012-07-28 11:07:18 +02001608 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
Alexander Block31db9f72012-07-25 23:19:24 +02001609 ret = 0;
1610 goto out;
1611 }
1612
Alexander Blocke938c8a2012-07-28 16:33:49 +02001613 ret = !memcmp(tmp_name->start, name, name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02001614
1615out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001616 fs_path_free(tmp_name);
Alexander Block31db9f72012-07-25 23:19:24 +02001617 return ret;
1618}
1619
Alexander Block766702e2012-07-28 14:11:31 +02001620/*
1621 * Used by process_recorded_refs to determine if a new ref would overwrite an
1622 * already existing ref. In case it detects an overwrite, it returns the
1623 * inode/gen in who_ino/who_gen.
1624 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1625 * to make sure later references to the overwritten inode are possible.
1626 * Orphanizing is however only required for the first ref of an inode.
1627 * process_recorded_refs does an additional is_first_ref check to see if
1628 * orphanizing is really required.
1629 */
Alexander Block31db9f72012-07-25 23:19:24 +02001630static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1631 const char *name, int name_len,
1632 u64 *who_ino, u64 *who_gen)
1633{
1634 int ret = 0;
Josef Bacikebdad912013-08-06 16:47:48 -04001635 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02001636 u64 other_inode = 0;
1637 u8 other_type = 0;
1638
1639 if (!sctx->parent_root)
1640 goto out;
1641
1642 ret = is_inode_existent(sctx, dir, dir_gen);
1643 if (ret <= 0)
1644 goto out;
1645
Josef Bacikebdad912013-08-06 16:47:48 -04001646 /*
1647 * If we have a parent root we need to verify that the parent dir was
1648 * not delted and then re-created, if it was then we have no overwrite
1649 * and we can just unlink this entry.
1650 */
1651 if (sctx->parent_root) {
1652 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1653 NULL, NULL, NULL);
1654 if (ret < 0 && ret != -ENOENT)
1655 goto out;
1656 if (ret) {
1657 ret = 0;
1658 goto out;
1659 }
1660 if (gen != dir_gen)
1661 goto out;
1662 }
1663
Alexander Block31db9f72012-07-25 23:19:24 +02001664 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1665 &other_inode, &other_type);
1666 if (ret < 0 && ret != -ENOENT)
1667 goto out;
1668 if (ret) {
1669 ret = 0;
1670 goto out;
1671 }
1672
Alexander Block766702e2012-07-28 14:11:31 +02001673 /*
1674 * Check if the overwritten ref was already processed. If yes, the ref
1675 * was already unlinked/moved, so we can safely assume that we will not
1676 * overwrite anything at this point in time.
1677 */
Alexander Block31db9f72012-07-25 23:19:24 +02001678 if (other_inode > sctx->send_progress) {
1679 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001680 who_gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001681 if (ret < 0)
1682 goto out;
1683
1684 ret = 1;
1685 *who_ino = other_inode;
1686 } else {
1687 ret = 0;
1688 }
1689
1690out:
1691 return ret;
1692}
1693
Alexander Block766702e2012-07-28 14:11:31 +02001694/*
1695 * Checks if the ref was overwritten by an already processed inode. This is
1696 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1697 * thus the orphan name needs be used.
1698 * process_recorded_refs also uses it to avoid unlinking of refs that were
1699 * overwritten.
1700 */
Alexander Block31db9f72012-07-25 23:19:24 +02001701static int did_overwrite_ref(struct send_ctx *sctx,
1702 u64 dir, u64 dir_gen,
1703 u64 ino, u64 ino_gen,
1704 const char *name, int name_len)
1705{
1706 int ret = 0;
1707 u64 gen;
1708 u64 ow_inode;
1709 u8 other_type;
1710
1711 if (!sctx->parent_root)
1712 goto out;
1713
1714 ret = is_inode_existent(sctx, dir, dir_gen);
1715 if (ret <= 0)
1716 goto out;
1717
1718 /* check if the ref was overwritten by another ref */
1719 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1720 &ow_inode, &other_type);
1721 if (ret < 0 && ret != -ENOENT)
1722 goto out;
1723 if (ret) {
1724 /* was never and will never be overwritten */
1725 ret = 0;
1726 goto out;
1727 }
1728
1729 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02001730 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02001731 if (ret < 0)
1732 goto out;
1733
1734 if (ow_inode == ino && gen == ino_gen) {
1735 ret = 0;
1736 goto out;
1737 }
1738
1739 /* we know that it is or will be overwritten. check this now */
1740 if (ow_inode < sctx->send_progress)
1741 ret = 1;
1742 else
1743 ret = 0;
1744
1745out:
1746 return ret;
1747}
1748
Alexander Block766702e2012-07-28 14:11:31 +02001749/*
1750 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1751 * that got overwritten. This is used by process_recorded_refs to determine
1752 * if it has to use the path as returned by get_cur_path or the orphan name.
1753 */
Alexander Block31db9f72012-07-25 23:19:24 +02001754static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1755{
1756 int ret = 0;
1757 struct fs_path *name = NULL;
1758 u64 dir;
1759 u64 dir_gen;
1760
1761 if (!sctx->parent_root)
1762 goto out;
1763
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001764 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02001765 if (!name)
1766 return -ENOMEM;
1767
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001768 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02001769 if (ret < 0)
1770 goto out;
1771
1772 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1773 name->start, fs_path_len(name));
Alexander Block31db9f72012-07-25 23:19:24 +02001774
1775out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001776 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02001777 return ret;
1778}
1779
Alexander Block766702e2012-07-28 14:11:31 +02001780/*
1781 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1782 * so we need to do some special handling in case we have clashes. This function
1783 * takes care of this with the help of name_cache_entry::radix_list.
Alexander Block5dc67d02012-08-01 12:07:43 +02001784 * In case of error, nce is kfreed.
Alexander Block766702e2012-07-28 14:11:31 +02001785 */
Alexander Block31db9f72012-07-25 23:19:24 +02001786static int name_cache_insert(struct send_ctx *sctx,
1787 struct name_cache_entry *nce)
1788{
1789 int ret = 0;
Alexander Block7e0926f2012-07-28 14:20:58 +02001790 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001791
Alexander Block7e0926f2012-07-28 14:20:58 +02001792 nce_head = radix_tree_lookup(&sctx->name_cache,
1793 (unsigned long)nce->ino);
1794 if (!nce_head) {
1795 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001796 if (!nce_head) {
1797 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001798 return -ENOMEM;
Tsutomu Itohcfa7a9c2012-12-17 06:38:51 +00001799 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001800 INIT_LIST_HEAD(nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001801
Alexander Block7e0926f2012-07-28 14:20:58 +02001802 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
Alexander Block5dc67d02012-08-01 12:07:43 +02001803 if (ret < 0) {
1804 kfree(nce_head);
1805 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001806 return ret;
Alexander Block5dc67d02012-08-01 12:07:43 +02001807 }
Alexander Block31db9f72012-07-25 23:19:24 +02001808 }
Alexander Block7e0926f2012-07-28 14:20:58 +02001809 list_add_tail(&nce->radix_list, nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001810 list_add_tail(&nce->list, &sctx->name_cache_list);
1811 sctx->name_cache_size++;
1812
1813 return ret;
1814}
1815
1816static void name_cache_delete(struct send_ctx *sctx,
1817 struct name_cache_entry *nce)
1818{
Alexander Block7e0926f2012-07-28 14:20:58 +02001819 struct list_head *nce_head;
Alexander Block31db9f72012-07-25 23:19:24 +02001820
Alexander Block7e0926f2012-07-28 14:20:58 +02001821 nce_head = radix_tree_lookup(&sctx->name_cache,
1822 (unsigned long)nce->ino);
1823 BUG_ON(!nce_head);
Alexander Block31db9f72012-07-25 23:19:24 +02001824
Alexander Block7e0926f2012-07-28 14:20:58 +02001825 list_del(&nce->radix_list);
Alexander Block31db9f72012-07-25 23:19:24 +02001826 list_del(&nce->list);
Alexander Block31db9f72012-07-25 23:19:24 +02001827 sctx->name_cache_size--;
Alexander Block7e0926f2012-07-28 14:20:58 +02001828
1829 if (list_empty(nce_head)) {
1830 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1831 kfree(nce_head);
1832 }
Alexander Block31db9f72012-07-25 23:19:24 +02001833}
1834
1835static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1836 u64 ino, u64 gen)
1837{
Alexander Block7e0926f2012-07-28 14:20:58 +02001838 struct list_head *nce_head;
1839 struct name_cache_entry *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02001840
Alexander Block7e0926f2012-07-28 14:20:58 +02001841 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1842 if (!nce_head)
Alexander Block31db9f72012-07-25 23:19:24 +02001843 return NULL;
1844
Alexander Block7e0926f2012-07-28 14:20:58 +02001845 list_for_each_entry(cur, nce_head, radix_list) {
1846 if (cur->ino == ino && cur->gen == gen)
1847 return cur;
1848 }
Alexander Block31db9f72012-07-25 23:19:24 +02001849 return NULL;
1850}
1851
Alexander Block766702e2012-07-28 14:11:31 +02001852/*
1853 * Removes the entry from the list and adds it back to the end. This marks the
1854 * entry as recently used so that name_cache_clean_unused does not remove it.
1855 */
Alexander Block31db9f72012-07-25 23:19:24 +02001856static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1857{
1858 list_del(&nce->list);
1859 list_add_tail(&nce->list, &sctx->name_cache_list);
1860}
1861
Alexander Block766702e2012-07-28 14:11:31 +02001862/*
1863 * Remove some entries from the beginning of name_cache_list.
1864 */
Alexander Block31db9f72012-07-25 23:19:24 +02001865static void name_cache_clean_unused(struct send_ctx *sctx)
1866{
1867 struct name_cache_entry *nce;
1868
1869 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1870 return;
1871
1872 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1873 nce = list_entry(sctx->name_cache_list.next,
1874 struct name_cache_entry, list);
1875 name_cache_delete(sctx, nce);
1876 kfree(nce);
1877 }
1878}
1879
1880static void name_cache_free(struct send_ctx *sctx)
1881{
1882 struct name_cache_entry *nce;
Alexander Block31db9f72012-07-25 23:19:24 +02001883
Alexander Blocke938c8a2012-07-28 16:33:49 +02001884 while (!list_empty(&sctx->name_cache_list)) {
1885 nce = list_entry(sctx->name_cache_list.next,
1886 struct name_cache_entry, list);
Alexander Block31db9f72012-07-25 23:19:24 +02001887 name_cache_delete(sctx, nce);
Alexander Block17589bd2012-07-28 14:13:35 +02001888 kfree(nce);
Alexander Block31db9f72012-07-25 23:19:24 +02001889 }
1890}
1891
Alexander Block766702e2012-07-28 14:11:31 +02001892/*
1893 * Used by get_cur_path for each ref up to the root.
1894 * Returns 0 if it succeeded.
1895 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1896 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1897 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1898 * Returns <0 in case of error.
1899 */
Alexander Block31db9f72012-07-25 23:19:24 +02001900static int __get_cur_name_and_parent(struct send_ctx *sctx,
1901 u64 ino, u64 gen,
1902 u64 *parent_ino,
1903 u64 *parent_gen,
1904 struct fs_path *dest)
1905{
1906 int ret;
1907 int nce_ret;
1908 struct btrfs_path *path = NULL;
1909 struct name_cache_entry *nce = NULL;
1910
Alexander Block766702e2012-07-28 14:11:31 +02001911 /*
1912 * First check if we already did a call to this function with the same
1913 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1914 * return the cached result.
1915 */
Alexander Block31db9f72012-07-25 23:19:24 +02001916 nce = name_cache_search(sctx, ino, gen);
1917 if (nce) {
1918 if (ino < sctx->send_progress && nce->need_later_update) {
1919 name_cache_delete(sctx, nce);
1920 kfree(nce);
1921 nce = NULL;
1922 } else {
1923 name_cache_used(sctx, nce);
1924 *parent_ino = nce->parent_ino;
1925 *parent_gen = nce->parent_gen;
1926 ret = fs_path_add(dest, nce->name, nce->name_len);
1927 if (ret < 0)
1928 goto out;
1929 ret = nce->ret;
1930 goto out;
1931 }
1932 }
1933
1934 path = alloc_path_for_send();
1935 if (!path)
1936 return -ENOMEM;
1937
Alexander Block766702e2012-07-28 14:11:31 +02001938 /*
1939 * If the inode is not existent yet, add the orphan name and return 1.
1940 * This should only happen for the parent dir that we determine in
1941 * __record_new_ref
1942 */
Alexander Block31db9f72012-07-25 23:19:24 +02001943 ret = is_inode_existent(sctx, ino, gen);
1944 if (ret < 0)
1945 goto out;
1946
1947 if (!ret) {
1948 ret = gen_unique_name(sctx, ino, gen, dest);
1949 if (ret < 0)
1950 goto out;
1951 ret = 1;
1952 goto out_cache;
1953 }
1954
Alexander Block766702e2012-07-28 14:11:31 +02001955 /*
1956 * Depending on whether the inode was already processed or not, use
1957 * send_root or parent_root for ref lookup.
1958 */
Alexander Block31db9f72012-07-25 23:19:24 +02001959 if (ino < sctx->send_progress)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001960 ret = get_first_ref(sctx->send_root, ino,
1961 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02001962 else
Tsutomu Itoh924794c2013-05-08 07:51:52 +00001963 ret = get_first_ref(sctx->parent_root, ino,
1964 parent_ino, parent_gen, dest);
Alexander Block31db9f72012-07-25 23:19:24 +02001965 if (ret < 0)
1966 goto out;
1967
Alexander Block766702e2012-07-28 14:11:31 +02001968 /*
1969 * Check if the ref was overwritten by an inode's ref that was processed
1970 * earlier. If yes, treat as orphan and return 1.
1971 */
Alexander Block31db9f72012-07-25 23:19:24 +02001972 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1973 dest->start, dest->end - dest->start);
1974 if (ret < 0)
1975 goto out;
1976 if (ret) {
1977 fs_path_reset(dest);
1978 ret = gen_unique_name(sctx, ino, gen, dest);
1979 if (ret < 0)
1980 goto out;
1981 ret = 1;
1982 }
1983
1984out_cache:
Alexander Block766702e2012-07-28 14:11:31 +02001985 /*
1986 * Store the result of the lookup in the name cache.
1987 */
Alexander Block31db9f72012-07-25 23:19:24 +02001988 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
1989 if (!nce) {
1990 ret = -ENOMEM;
1991 goto out;
1992 }
1993
1994 nce->ino = ino;
1995 nce->gen = gen;
1996 nce->parent_ino = *parent_ino;
1997 nce->parent_gen = *parent_gen;
1998 nce->name_len = fs_path_len(dest);
1999 nce->ret = ret;
2000 strcpy(nce->name, dest->start);
Alexander Block31db9f72012-07-25 23:19:24 +02002001
2002 if (ino < sctx->send_progress)
2003 nce->need_later_update = 0;
2004 else
2005 nce->need_later_update = 1;
2006
2007 nce_ret = name_cache_insert(sctx, nce);
2008 if (nce_ret < 0)
2009 ret = nce_ret;
2010 name_cache_clean_unused(sctx);
2011
2012out:
2013 btrfs_free_path(path);
2014 return ret;
2015}
2016
2017/*
2018 * Magic happens here. This function returns the first ref to an inode as it
2019 * would look like while receiving the stream at this point in time.
2020 * We walk the path up to the root. For every inode in between, we check if it
2021 * was already processed/sent. If yes, we continue with the parent as found
2022 * in send_root. If not, we continue with the parent as found in parent_root.
2023 * If we encounter an inode that was deleted at this point in time, we use the
2024 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2025 * that were not created yet and overwritten inodes/refs.
2026 *
2027 * When do we have have orphan inodes:
2028 * 1. When an inode is freshly created and thus no valid refs are available yet
2029 * 2. When a directory lost all it's refs (deleted) but still has dir items
2030 * inside which were not processed yet (pending for move/delete). If anyone
2031 * tried to get the path to the dir items, it would get a path inside that
2032 * orphan directory.
2033 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2034 * of an unprocessed inode. If in that case the first ref would be
2035 * overwritten, the overwritten inode gets "orphanized". Later when we
2036 * process this overwritten inode, it is restored at a new place by moving
2037 * the orphan inode.
2038 *
2039 * sctx->send_progress tells this function at which point in time receiving
2040 * would be.
2041 */
2042static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2043 struct fs_path *dest)
2044{
2045 int ret = 0;
2046 struct fs_path *name = NULL;
2047 u64 parent_inode = 0;
2048 u64 parent_gen = 0;
2049 int stop = 0;
2050
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002051 name = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002052 if (!name) {
2053 ret = -ENOMEM;
2054 goto out;
2055 }
2056
2057 dest->reversed = 1;
2058 fs_path_reset(dest);
2059
2060 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2061 fs_path_reset(name);
2062
2063 ret = __get_cur_name_and_parent(sctx, ino, gen,
2064 &parent_inode, &parent_gen, name);
2065 if (ret < 0)
2066 goto out;
2067 if (ret)
2068 stop = 1;
2069
2070 ret = fs_path_add_path(dest, name);
2071 if (ret < 0)
2072 goto out;
2073
2074 ino = parent_inode;
2075 gen = parent_gen;
2076 }
2077
2078out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002079 fs_path_free(name);
Alexander Block31db9f72012-07-25 23:19:24 +02002080 if (!ret)
2081 fs_path_unreverse(dest);
2082 return ret;
2083}
2084
2085/*
Alexander Block31db9f72012-07-25 23:19:24 +02002086 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2087 */
2088static int send_subvol_begin(struct send_ctx *sctx)
2089{
2090 int ret;
2091 struct btrfs_root *send_root = sctx->send_root;
2092 struct btrfs_root *parent_root = sctx->parent_root;
2093 struct btrfs_path *path;
2094 struct btrfs_key key;
2095 struct btrfs_root_ref *ref;
2096 struct extent_buffer *leaf;
2097 char *name = NULL;
2098 int namelen;
2099
2100 path = alloc_path_for_send();
2101 if (!path)
2102 return -ENOMEM;
2103
2104 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2105 if (!name) {
2106 btrfs_free_path(path);
2107 return -ENOMEM;
2108 }
2109
2110 key.objectid = send_root->objectid;
2111 key.type = BTRFS_ROOT_BACKREF_KEY;
2112 key.offset = 0;
2113
2114 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2115 &key, path, 1, 0);
2116 if (ret < 0)
2117 goto out;
2118 if (ret) {
2119 ret = -ENOENT;
2120 goto out;
2121 }
2122
2123 leaf = path->nodes[0];
2124 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2125 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2126 key.objectid != send_root->objectid) {
2127 ret = -ENOENT;
2128 goto out;
2129 }
2130 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2131 namelen = btrfs_root_ref_name_len(leaf, ref);
2132 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2133 btrfs_release_path(path);
2134
Alexander Block31db9f72012-07-25 23:19:24 +02002135 if (parent_root) {
2136 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2137 if (ret < 0)
2138 goto out;
2139 } else {
2140 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2141 if (ret < 0)
2142 goto out;
2143 }
2144
2145 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2146 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2147 sctx->send_root->root_item.uuid);
2148 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002149 le64_to_cpu(sctx->send_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002150 if (parent_root) {
2151 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2152 sctx->parent_root->root_item.uuid);
2153 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00002154 le64_to_cpu(sctx->parent_root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02002155 }
2156
2157 ret = send_cmd(sctx);
2158
2159tlv_put_failure:
2160out:
2161 btrfs_free_path(path);
2162 kfree(name);
2163 return ret;
2164}
2165
2166static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2167{
2168 int ret = 0;
2169 struct fs_path *p;
2170
2171verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2172
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002173 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002174 if (!p)
2175 return -ENOMEM;
2176
2177 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2178 if (ret < 0)
2179 goto out;
2180
2181 ret = get_cur_path(sctx, ino, gen, p);
2182 if (ret < 0)
2183 goto out;
2184 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2185 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2186
2187 ret = send_cmd(sctx);
2188
2189tlv_put_failure:
2190out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002191 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002192 return ret;
2193}
2194
2195static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2196{
2197 int ret = 0;
2198 struct fs_path *p;
2199
2200verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2201
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002202 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002203 if (!p)
2204 return -ENOMEM;
2205
2206 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2207 if (ret < 0)
2208 goto out;
2209
2210 ret = get_cur_path(sctx, ino, gen, p);
2211 if (ret < 0)
2212 goto out;
2213 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2214 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2215
2216 ret = send_cmd(sctx);
2217
2218tlv_put_failure:
2219out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002220 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002221 return ret;
2222}
2223
2224static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2225{
2226 int ret = 0;
2227 struct fs_path *p;
2228
2229verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2230
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002231 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002232 if (!p)
2233 return -ENOMEM;
2234
2235 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2236 if (ret < 0)
2237 goto out;
2238
2239 ret = get_cur_path(sctx, ino, gen, p);
2240 if (ret < 0)
2241 goto out;
2242 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2243 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2244 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2245
2246 ret = send_cmd(sctx);
2247
2248tlv_put_failure:
2249out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002250 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002251 return ret;
2252}
2253
2254static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2255{
2256 int ret = 0;
2257 struct fs_path *p = NULL;
2258 struct btrfs_inode_item *ii;
2259 struct btrfs_path *path = NULL;
2260 struct extent_buffer *eb;
2261 struct btrfs_key key;
2262 int slot;
2263
2264verbose_printk("btrfs: send_utimes %llu\n", ino);
2265
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002266 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002267 if (!p)
2268 return -ENOMEM;
2269
2270 path = alloc_path_for_send();
2271 if (!path) {
2272 ret = -ENOMEM;
2273 goto out;
2274 }
2275
2276 key.objectid = ino;
2277 key.type = BTRFS_INODE_ITEM_KEY;
2278 key.offset = 0;
2279 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2280 if (ret < 0)
2281 goto out;
2282
2283 eb = path->nodes[0];
2284 slot = path->slots[0];
2285 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2286
2287 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2288 if (ret < 0)
2289 goto out;
2290
2291 ret = get_cur_path(sctx, ino, gen, p);
2292 if (ret < 0)
2293 goto out;
2294 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2295 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2296 btrfs_inode_atime(ii));
2297 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2298 btrfs_inode_mtime(ii));
2299 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2300 btrfs_inode_ctime(ii));
Alexander Block766702e2012-07-28 14:11:31 +02002301 /* TODO Add otime support when the otime patches get into upstream */
Alexander Block31db9f72012-07-25 23:19:24 +02002302
2303 ret = send_cmd(sctx);
2304
2305tlv_put_failure:
2306out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002307 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002308 btrfs_free_path(path);
2309 return ret;
2310}
2311
2312/*
2313 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2314 * a valid path yet because we did not process the refs yet. So, the inode
2315 * is created as orphan.
2316 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002317static int send_create_inode(struct send_ctx *sctx, u64 ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002318{
2319 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002320 struct fs_path *p;
Alexander Block31db9f72012-07-25 23:19:24 +02002321 int cmd;
Alexander Block1f4692d2012-07-28 10:42:24 +02002322 u64 gen;
Alexander Block31db9f72012-07-25 23:19:24 +02002323 u64 mode;
Alexander Block1f4692d2012-07-28 10:42:24 +02002324 u64 rdev;
Alexander Block31db9f72012-07-25 23:19:24 +02002325
Alexander Block1f4692d2012-07-28 10:42:24 +02002326verbose_printk("btrfs: send_create_inode %llu\n", ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002327
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002328 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002329 if (!p)
2330 return -ENOMEM;
2331
Alexander Block1f4692d2012-07-28 10:42:24 +02002332 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2333 NULL, &rdev);
2334 if (ret < 0)
2335 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002336
Alexander Blocke938c8a2012-07-28 16:33:49 +02002337 if (S_ISREG(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002338 cmd = BTRFS_SEND_C_MKFILE;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002339 } else if (S_ISDIR(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002340 cmd = BTRFS_SEND_C_MKDIR;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002341 } else if (S_ISLNK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002342 cmd = BTRFS_SEND_C_SYMLINK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002343 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002344 cmd = BTRFS_SEND_C_MKNOD;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002345 } else if (S_ISFIFO(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002346 cmd = BTRFS_SEND_C_MKFIFO;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002347 } else if (S_ISSOCK(mode)) {
Alexander Block31db9f72012-07-25 23:19:24 +02002348 cmd = BTRFS_SEND_C_MKSOCK;
Alexander Blocke938c8a2012-07-28 16:33:49 +02002349 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02002350 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2351 (int)(mode & S_IFMT));
2352 ret = -ENOTSUPP;
2353 goto out;
2354 }
2355
2356 ret = begin_cmd(sctx, cmd);
2357 if (ret < 0)
2358 goto out;
2359
Alexander Block1f4692d2012-07-28 10:42:24 +02002360 ret = gen_unique_name(sctx, ino, gen, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002361 if (ret < 0)
2362 goto out;
2363
2364 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
Alexander Block1f4692d2012-07-28 10:42:24 +02002365 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002366
2367 if (S_ISLNK(mode)) {
2368 fs_path_reset(p);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002369 ret = read_symlink(sctx->send_root, ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02002370 if (ret < 0)
2371 goto out;
2372 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2373 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2374 S_ISFIFO(mode) || S_ISSOCK(mode)) {
Arne Jansend79e5042012-10-15 18:28:46 +00002375 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2376 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
Alexander Block31db9f72012-07-25 23:19:24 +02002377 }
2378
2379 ret = send_cmd(sctx);
2380 if (ret < 0)
2381 goto out;
2382
2383
2384tlv_put_failure:
2385out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002386 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002387 return ret;
2388}
2389
Alexander Block1f4692d2012-07-28 10:42:24 +02002390/*
2391 * We need some special handling for inodes that get processed before the parent
2392 * directory got created. See process_recorded_refs for details.
2393 * This function does the check if we already created the dir out of order.
2394 */
2395static int did_create_dir(struct send_ctx *sctx, u64 dir)
2396{
2397 int ret = 0;
2398 struct btrfs_path *path = NULL;
2399 struct btrfs_key key;
2400 struct btrfs_key found_key;
2401 struct btrfs_key di_key;
2402 struct extent_buffer *eb;
2403 struct btrfs_dir_item *di;
2404 int slot;
2405
2406 path = alloc_path_for_send();
2407 if (!path) {
2408 ret = -ENOMEM;
2409 goto out;
2410 }
2411
2412 key.objectid = dir;
2413 key.type = BTRFS_DIR_INDEX_KEY;
2414 key.offset = 0;
2415 while (1) {
2416 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2417 1, 0);
2418 if (ret < 0)
2419 goto out;
2420 if (!ret) {
2421 eb = path->nodes[0];
2422 slot = path->slots[0];
2423 btrfs_item_key_to_cpu(eb, &found_key, slot);
2424 }
2425 if (ret || found_key.objectid != key.objectid ||
2426 found_key.type != key.type) {
2427 ret = 0;
2428 goto out;
2429 }
2430
2431 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2432 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2433
Josef Bacika0525412013-08-12 10:56:14 -04002434 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2435 di_key.objectid < sctx->send_progress) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002436 ret = 1;
2437 goto out;
2438 }
2439
2440 key.offset = found_key.offset + 1;
2441 btrfs_release_path(path);
2442 }
2443
2444out:
2445 btrfs_free_path(path);
2446 return ret;
2447}
2448
2449/*
2450 * Only creates the inode if it is:
2451 * 1. Not a directory
2452 * 2. Or a directory which was not created already due to out of order
2453 * directories. See did_create_dir and process_recorded_refs for details.
2454 */
2455static int send_create_inode_if_needed(struct send_ctx *sctx)
2456{
2457 int ret;
2458
2459 if (S_ISDIR(sctx->cur_inode_mode)) {
2460 ret = did_create_dir(sctx, sctx->cur_ino);
2461 if (ret < 0)
2462 goto out;
2463 if (ret) {
2464 ret = 0;
2465 goto out;
2466 }
2467 }
2468
2469 ret = send_create_inode(sctx, sctx->cur_ino);
2470 if (ret < 0)
2471 goto out;
2472
2473out:
2474 return ret;
2475}
2476
Alexander Block31db9f72012-07-25 23:19:24 +02002477struct recorded_ref {
2478 struct list_head list;
2479 char *dir_path;
2480 char *name;
2481 struct fs_path *full_path;
2482 u64 dir;
2483 u64 dir_gen;
2484 int dir_path_len;
2485 int name_len;
2486};
2487
2488/*
2489 * We need to process new refs before deleted refs, but compare_tree gives us
2490 * everything mixed. So we first record all refs and later process them.
2491 * This function is a helper to record one ref.
2492 */
2493static int record_ref(struct list_head *head, u64 dir,
2494 u64 dir_gen, struct fs_path *path)
2495{
2496 struct recorded_ref *ref;
Alexander Block31db9f72012-07-25 23:19:24 +02002497
2498 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2499 if (!ref)
2500 return -ENOMEM;
2501
2502 ref->dir = dir;
2503 ref->dir_gen = dir_gen;
2504 ref->full_path = path;
2505
Andy Shevchenkoed848852013-08-21 10:32:13 +03002506 ref->name = (char *)kbasename(ref->full_path->start);
2507 ref->name_len = ref->full_path->end - ref->name;
2508 ref->dir_path = ref->full_path->start;
2509 if (ref->name == ref->full_path->start)
Alexander Block31db9f72012-07-25 23:19:24 +02002510 ref->dir_path_len = 0;
Andy Shevchenkoed848852013-08-21 10:32:13 +03002511 else
Alexander Block31db9f72012-07-25 23:19:24 +02002512 ref->dir_path_len = ref->full_path->end -
2513 ref->full_path->start - 1 - ref->name_len;
Alexander Block31db9f72012-07-25 23:19:24 +02002514
2515 list_add_tail(&ref->list, head);
2516 return 0;
2517}
2518
Josef Bacikba5e8f22013-08-16 16:52:55 -04002519static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2520{
2521 struct recorded_ref *new;
2522
2523 new = kmalloc(sizeof(*ref), GFP_NOFS);
2524 if (!new)
2525 return -ENOMEM;
2526
2527 new->dir = ref->dir;
2528 new->dir_gen = ref->dir_gen;
2529 new->full_path = NULL;
2530 INIT_LIST_HEAD(&new->list);
2531 list_add_tail(&new->list, list);
2532 return 0;
2533}
2534
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002535static void __free_recorded_refs(struct list_head *head)
Alexander Block31db9f72012-07-25 23:19:24 +02002536{
2537 struct recorded_ref *cur;
Alexander Block31db9f72012-07-25 23:19:24 +02002538
Alexander Blocke938c8a2012-07-28 16:33:49 +02002539 while (!list_empty(head)) {
2540 cur = list_entry(head->next, struct recorded_ref, list);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002541 fs_path_free(cur->full_path);
Alexander Blocke938c8a2012-07-28 16:33:49 +02002542 list_del(&cur->list);
Alexander Block31db9f72012-07-25 23:19:24 +02002543 kfree(cur);
2544 }
Alexander Block31db9f72012-07-25 23:19:24 +02002545}
2546
2547static void free_recorded_refs(struct send_ctx *sctx)
2548{
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002549 __free_recorded_refs(&sctx->new_refs);
2550 __free_recorded_refs(&sctx->deleted_refs);
Alexander Block31db9f72012-07-25 23:19:24 +02002551}
2552
2553/*
Alexander Block766702e2012-07-28 14:11:31 +02002554 * Renames/moves a file/dir to its orphan name. Used when the first
Alexander Block31db9f72012-07-25 23:19:24 +02002555 * ref of an unprocessed inode gets overwritten and for all non empty
2556 * directories.
2557 */
2558static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2559 struct fs_path *path)
2560{
2561 int ret;
2562 struct fs_path *orphan;
2563
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002564 orphan = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002565 if (!orphan)
2566 return -ENOMEM;
2567
2568 ret = gen_unique_name(sctx, ino, gen, orphan);
2569 if (ret < 0)
2570 goto out;
2571
2572 ret = send_rename(sctx, path, orphan);
2573
2574out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002575 fs_path_free(orphan);
Alexander Block31db9f72012-07-25 23:19:24 +02002576 return ret;
2577}
2578
2579/*
2580 * Returns 1 if a directory can be removed at this point in time.
2581 * We check this by iterating all dir items and checking if the inode behind
2582 * the dir item was already processed.
2583 */
2584static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2585{
2586 int ret = 0;
2587 struct btrfs_root *root = sctx->parent_root;
2588 struct btrfs_path *path;
2589 struct btrfs_key key;
2590 struct btrfs_key found_key;
2591 struct btrfs_key loc;
2592 struct btrfs_dir_item *di;
2593
Alexander Block6d85ed02012-08-01 14:48:59 +02002594 /*
2595 * Don't try to rmdir the top/root subvolume dir.
2596 */
2597 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2598 return 0;
2599
Alexander Block31db9f72012-07-25 23:19:24 +02002600 path = alloc_path_for_send();
2601 if (!path)
2602 return -ENOMEM;
2603
2604 key.objectid = dir;
2605 key.type = BTRFS_DIR_INDEX_KEY;
2606 key.offset = 0;
2607
2608 while (1) {
2609 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2610 if (ret < 0)
2611 goto out;
2612 if (!ret) {
2613 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2614 path->slots[0]);
2615 }
2616 if (ret || found_key.objectid != key.objectid ||
2617 found_key.type != key.type) {
2618 break;
2619 }
2620
2621 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2622 struct btrfs_dir_item);
2623 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2624
2625 if (loc.objectid > send_progress) {
2626 ret = 0;
2627 goto out;
2628 }
2629
2630 btrfs_release_path(path);
2631 key.offset = found_key.offset + 1;
2632 }
2633
2634 ret = 1;
2635
2636out:
2637 btrfs_free_path(path);
2638 return ret;
2639}
2640
Alexander Block31db9f72012-07-25 23:19:24 +02002641/*
2642 * This does all the move/link/unlink/rmdir magic.
2643 */
2644static int process_recorded_refs(struct send_ctx *sctx)
2645{
2646 int ret = 0;
2647 struct recorded_ref *cur;
Alexander Block1f4692d2012-07-28 10:42:24 +02002648 struct recorded_ref *cur2;
Josef Bacikba5e8f22013-08-16 16:52:55 -04002649 struct list_head check_dirs;
Alexander Block31db9f72012-07-25 23:19:24 +02002650 struct fs_path *valid_path = NULL;
Chris Masonb24baf62012-07-25 19:21:10 -04002651 u64 ow_inode = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02002652 u64 ow_gen;
2653 int did_overwrite = 0;
2654 int is_orphan = 0;
2655
2656verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2657
Alexander Block6d85ed02012-08-01 14:48:59 +02002658 /*
2659 * This should never happen as the root dir always has the same ref
2660 * which is always '..'
2661 */
2662 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002663 INIT_LIST_HEAD(&check_dirs);
Alexander Block6d85ed02012-08-01 14:48:59 +02002664
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002665 valid_path = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002666 if (!valid_path) {
2667 ret = -ENOMEM;
2668 goto out;
2669 }
2670
Alexander Block31db9f72012-07-25 23:19:24 +02002671 /*
2672 * First, check if the first ref of the current inode was overwritten
2673 * before. If yes, we know that the current inode was already orphanized
2674 * and thus use the orphan name. If not, we can use get_cur_path to
2675 * get the path of the first ref as it would like while receiving at
2676 * this point in time.
2677 * New inodes are always orphan at the beginning, so force to use the
2678 * orphan name in this case.
2679 * The first ref is stored in valid_path and will be updated if it
2680 * gets moved around.
2681 */
2682 if (!sctx->cur_inode_new) {
2683 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2684 sctx->cur_inode_gen);
2685 if (ret < 0)
2686 goto out;
2687 if (ret)
2688 did_overwrite = 1;
2689 }
2690 if (sctx->cur_inode_new || did_overwrite) {
2691 ret = gen_unique_name(sctx, sctx->cur_ino,
2692 sctx->cur_inode_gen, valid_path);
2693 if (ret < 0)
2694 goto out;
2695 is_orphan = 1;
2696 } else {
2697 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2698 valid_path);
2699 if (ret < 0)
2700 goto out;
2701 }
2702
2703 list_for_each_entry(cur, &sctx->new_refs, list) {
2704 /*
Alexander Block1f4692d2012-07-28 10:42:24 +02002705 * We may have refs where the parent directory does not exist
2706 * yet. This happens if the parent directories inum is higher
2707 * the the current inum. To handle this case, we create the
2708 * parent directory out of order. But we need to check if this
2709 * did already happen before due to other refs in the same dir.
2710 */
2711 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2712 if (ret < 0)
2713 goto out;
2714 if (ret == inode_state_will_create) {
2715 ret = 0;
2716 /*
2717 * First check if any of the current inodes refs did
2718 * already create the dir.
2719 */
2720 list_for_each_entry(cur2, &sctx->new_refs, list) {
2721 if (cur == cur2)
2722 break;
2723 if (cur2->dir == cur->dir) {
2724 ret = 1;
2725 break;
2726 }
2727 }
2728
2729 /*
2730 * If that did not happen, check if a previous inode
2731 * did already create the dir.
2732 */
2733 if (!ret)
2734 ret = did_create_dir(sctx, cur->dir);
2735 if (ret < 0)
2736 goto out;
2737 if (!ret) {
2738 ret = send_create_inode(sctx, cur->dir);
2739 if (ret < 0)
2740 goto out;
2741 }
2742 }
2743
2744 /*
Alexander Block31db9f72012-07-25 23:19:24 +02002745 * Check if this new ref would overwrite the first ref of
2746 * another unprocessed inode. If yes, orphanize the
2747 * overwritten inode. If we find an overwritten ref that is
2748 * not the first ref, simply unlink it.
2749 */
2750 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2751 cur->name, cur->name_len,
2752 &ow_inode, &ow_gen);
2753 if (ret < 0)
2754 goto out;
2755 if (ret) {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002756 ret = is_first_ref(sctx->parent_root,
2757 ow_inode, cur->dir, cur->name,
2758 cur->name_len);
Alexander Block31db9f72012-07-25 23:19:24 +02002759 if (ret < 0)
2760 goto out;
2761 if (ret) {
2762 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2763 cur->full_path);
2764 if (ret < 0)
2765 goto out;
2766 } else {
2767 ret = send_unlink(sctx, cur->full_path);
2768 if (ret < 0)
2769 goto out;
2770 }
2771 }
2772
2773 /*
2774 * link/move the ref to the new place. If we have an orphan
2775 * inode, move it and update valid_path. If not, link or move
2776 * it depending on the inode mode.
2777 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002778 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02002779 ret = send_rename(sctx, valid_path, cur->full_path);
2780 if (ret < 0)
2781 goto out;
2782 is_orphan = 0;
2783 ret = fs_path_copy(valid_path, cur->full_path);
2784 if (ret < 0)
2785 goto out;
2786 } else {
2787 if (S_ISDIR(sctx->cur_inode_mode)) {
2788 /*
2789 * Dirs can't be linked, so move it. For moved
2790 * dirs, we always have one new and one deleted
2791 * ref. The deleted ref is ignored later.
2792 */
2793 ret = send_rename(sctx, valid_path,
2794 cur->full_path);
2795 if (ret < 0)
2796 goto out;
2797 ret = fs_path_copy(valid_path, cur->full_path);
2798 if (ret < 0)
2799 goto out;
2800 } else {
2801 ret = send_link(sctx, cur->full_path,
2802 valid_path);
2803 if (ret < 0)
2804 goto out;
2805 }
2806 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04002807 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02002808 if (ret < 0)
2809 goto out;
2810 }
2811
2812 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2813 /*
2814 * Check if we can already rmdir the directory. If not,
2815 * orphanize it. For every dir item inside that gets deleted
2816 * later, we do this check again and rmdir it then if possible.
2817 * See the use of check_dirs for more details.
2818 */
2819 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2820 if (ret < 0)
2821 goto out;
2822 if (ret) {
2823 ret = send_rmdir(sctx, valid_path);
2824 if (ret < 0)
2825 goto out;
2826 } else if (!is_orphan) {
2827 ret = orphanize_inode(sctx, sctx->cur_ino,
2828 sctx->cur_inode_gen, valid_path);
2829 if (ret < 0)
2830 goto out;
2831 is_orphan = 1;
2832 }
2833
2834 list_for_each_entry(cur, &sctx->deleted_refs, list) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04002835 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02002836 if (ret < 0)
2837 goto out;
2838 }
Alexander Blockccf16262012-07-28 11:46:29 +02002839 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2840 !list_empty(&sctx->deleted_refs)) {
2841 /*
2842 * We have a moved dir. Add the old parent to check_dirs
2843 */
2844 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2845 list);
Josef Bacikba5e8f22013-08-16 16:52:55 -04002846 ret = dup_ref(cur, &check_dirs);
Alexander Blockccf16262012-07-28 11:46:29 +02002847 if (ret < 0)
2848 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002849 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2850 /*
2851 * We have a non dir inode. Go through all deleted refs and
2852 * unlink them if they were not already overwritten by other
2853 * inodes.
2854 */
2855 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2856 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2857 sctx->cur_ino, sctx->cur_inode_gen,
2858 cur->name, cur->name_len);
2859 if (ret < 0)
2860 goto out;
2861 if (!ret) {
Alexander Block1f4692d2012-07-28 10:42:24 +02002862 ret = send_unlink(sctx, cur->full_path);
2863 if (ret < 0)
2864 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02002865 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04002866 ret = dup_ref(cur, &check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02002867 if (ret < 0)
2868 goto out;
2869 }
Alexander Block31db9f72012-07-25 23:19:24 +02002870 /*
2871 * If the inode is still orphan, unlink the orphan. This may
2872 * happen when a previous inode did overwrite the first ref
2873 * of this inode and no new refs were added for the current
Alexander Block766702e2012-07-28 14:11:31 +02002874 * inode. Unlinking does not mean that the inode is deleted in
2875 * all cases. There may still be links to this inode in other
2876 * places.
Alexander Block31db9f72012-07-25 23:19:24 +02002877 */
Alexander Block1f4692d2012-07-28 10:42:24 +02002878 if (is_orphan) {
Alexander Block31db9f72012-07-25 23:19:24 +02002879 ret = send_unlink(sctx, valid_path);
2880 if (ret < 0)
2881 goto out;
2882 }
2883 }
2884
2885 /*
2886 * We did collect all parent dirs where cur_inode was once located. We
2887 * now go through all these dirs and check if they are pending for
2888 * deletion and if it's finally possible to perform the rmdir now.
2889 * We also update the inode stats of the parent dirs here.
2890 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04002891 list_for_each_entry(cur, &check_dirs, list) {
Alexander Block766702e2012-07-28 14:11:31 +02002892 /*
2893 * In case we had refs into dirs that were not processed yet,
2894 * we don't need to do the utime and rmdir logic for these dirs.
2895 * The dir will be processed later.
2896 */
Josef Bacikba5e8f22013-08-16 16:52:55 -04002897 if (cur->dir > sctx->cur_ino)
Alexander Block31db9f72012-07-25 23:19:24 +02002898 continue;
2899
Josef Bacikba5e8f22013-08-16 16:52:55 -04002900 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02002901 if (ret < 0)
2902 goto out;
2903
2904 if (ret == inode_state_did_create ||
2905 ret == inode_state_no_change) {
2906 /* TODO delayed utimes */
Josef Bacikba5e8f22013-08-16 16:52:55 -04002907 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
Alexander Block31db9f72012-07-25 23:19:24 +02002908 if (ret < 0)
2909 goto out;
2910 } else if (ret == inode_state_did_delete) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04002911 ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
Alexander Block31db9f72012-07-25 23:19:24 +02002912 if (ret < 0)
2913 goto out;
2914 if (ret) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04002915 ret = get_cur_path(sctx, cur->dir,
2916 cur->dir_gen, valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02002917 if (ret < 0)
2918 goto out;
2919 ret = send_rmdir(sctx, valid_path);
2920 if (ret < 0)
2921 goto out;
2922 }
2923 }
2924 }
2925
Alexander Block31db9f72012-07-25 23:19:24 +02002926 ret = 0;
2927
2928out:
Josef Bacikba5e8f22013-08-16 16:52:55 -04002929 __free_recorded_refs(&check_dirs);
Alexander Block31db9f72012-07-25 23:19:24 +02002930 free_recorded_refs(sctx);
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002931 fs_path_free(valid_path);
Alexander Block31db9f72012-07-25 23:19:24 +02002932 return ret;
2933}
2934
2935static int __record_new_ref(int num, u64 dir, int index,
2936 struct fs_path *name,
2937 void *ctx)
2938{
2939 int ret = 0;
2940 struct send_ctx *sctx = ctx;
2941 struct fs_path *p;
2942 u64 gen;
2943
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002944 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002945 if (!p)
2946 return -ENOMEM;
2947
2948 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02002949 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02002950 if (ret < 0)
2951 goto out;
2952
Alexander Block31db9f72012-07-25 23:19:24 +02002953 ret = get_cur_path(sctx, dir, gen, p);
2954 if (ret < 0)
2955 goto out;
2956 ret = fs_path_add_path(p, name);
2957 if (ret < 0)
2958 goto out;
2959
2960 ret = record_ref(&sctx->new_refs, dir, gen, p);
2961
2962out:
2963 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002964 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002965 return ret;
2966}
2967
2968static int __record_deleted_ref(int num, u64 dir, int index,
2969 struct fs_path *name,
2970 void *ctx)
2971{
2972 int ret = 0;
2973 struct send_ctx *sctx = ctx;
2974 struct fs_path *p;
2975 u64 gen;
2976
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002977 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02002978 if (!p)
2979 return -ENOMEM;
2980
2981 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02002982 NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02002983 if (ret < 0)
2984 goto out;
2985
2986 ret = get_cur_path(sctx, dir, gen, p);
2987 if (ret < 0)
2988 goto out;
2989 ret = fs_path_add_path(p, name);
2990 if (ret < 0)
2991 goto out;
2992
2993 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
2994
2995out:
2996 if (ret)
Tsutomu Itoh924794c2013-05-08 07:51:52 +00002997 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02002998 return ret;
2999}
3000
3001static int record_new_ref(struct send_ctx *sctx)
3002{
3003 int ret;
3004
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003005 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3006 sctx->cmp_key, 0, __record_new_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003007 if (ret < 0)
3008 goto out;
3009 ret = 0;
3010
3011out:
3012 return ret;
3013}
3014
3015static int record_deleted_ref(struct send_ctx *sctx)
3016{
3017 int ret;
3018
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003019 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3020 sctx->cmp_key, 0, __record_deleted_ref, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003021 if (ret < 0)
3022 goto out;
3023 ret = 0;
3024
3025out:
3026 return ret;
3027}
3028
3029struct find_ref_ctx {
3030 u64 dir;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003031 u64 dir_gen;
3032 struct btrfs_root *root;
Alexander Block31db9f72012-07-25 23:19:24 +02003033 struct fs_path *name;
3034 int found_idx;
3035};
3036
3037static int __find_iref(int num, u64 dir, int index,
3038 struct fs_path *name,
3039 void *ctx_)
3040{
3041 struct find_ref_ctx *ctx = ctx_;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003042 u64 dir_gen;
3043 int ret;
Alexander Block31db9f72012-07-25 23:19:24 +02003044
3045 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3046 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
Josef Bacikba5e8f22013-08-16 16:52:55 -04003047 /*
3048 * To avoid doing extra lookups we'll only do this if everything
3049 * else matches.
3050 */
3051 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3052 NULL, NULL, NULL);
3053 if (ret)
3054 return ret;
3055 if (dir_gen != ctx->dir_gen)
3056 return 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003057 ctx->found_idx = num;
3058 return 1;
3059 }
3060 return 0;
3061}
3062
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003063static int find_iref(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003064 struct btrfs_path *path,
3065 struct btrfs_key *key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003066 u64 dir, u64 dir_gen, struct fs_path *name)
Alexander Block31db9f72012-07-25 23:19:24 +02003067{
3068 int ret;
3069 struct find_ref_ctx ctx;
3070
3071 ctx.dir = dir;
3072 ctx.name = name;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003073 ctx.dir_gen = dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003074 ctx.found_idx = -1;
Josef Bacikba5e8f22013-08-16 16:52:55 -04003075 ctx.root = root;
Alexander Block31db9f72012-07-25 23:19:24 +02003076
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003077 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003078 if (ret < 0)
3079 return ret;
3080
3081 if (ctx.found_idx == -1)
3082 return -ENOENT;
3083
3084 return ctx.found_idx;
3085}
3086
3087static int __record_changed_new_ref(int num, u64 dir, int index,
3088 struct fs_path *name,
3089 void *ctx)
3090{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003091 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003092 int ret;
3093 struct send_ctx *sctx = ctx;
3094
Josef Bacikba5e8f22013-08-16 16:52:55 -04003095 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3096 NULL, NULL, NULL);
3097 if (ret)
3098 return ret;
3099
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003100 ret = find_iref(sctx->parent_root, sctx->right_path,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003101 sctx->cmp_key, dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003102 if (ret == -ENOENT)
3103 ret = __record_new_ref(num, dir, index, name, sctx);
3104 else if (ret > 0)
3105 ret = 0;
3106
3107 return ret;
3108}
3109
3110static int __record_changed_deleted_ref(int num, u64 dir, int index,
3111 struct fs_path *name,
3112 void *ctx)
3113{
Josef Bacikba5e8f22013-08-16 16:52:55 -04003114 u64 dir_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003115 int ret;
3116 struct send_ctx *sctx = ctx;
3117
Josef Bacikba5e8f22013-08-16 16:52:55 -04003118 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3119 NULL, NULL, NULL);
3120 if (ret)
3121 return ret;
3122
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003123 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
Josef Bacikba5e8f22013-08-16 16:52:55 -04003124 dir, dir_gen, name);
Alexander Block31db9f72012-07-25 23:19:24 +02003125 if (ret == -ENOENT)
3126 ret = __record_deleted_ref(num, dir, index, name, sctx);
3127 else if (ret > 0)
3128 ret = 0;
3129
3130 return ret;
3131}
3132
3133static int record_changed_ref(struct send_ctx *sctx)
3134{
3135 int ret = 0;
3136
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003137 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003138 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3139 if (ret < 0)
3140 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003141 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003142 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3143 if (ret < 0)
3144 goto out;
3145 ret = 0;
3146
3147out:
3148 return ret;
3149}
3150
3151/*
3152 * Record and process all refs at once. Needed when an inode changes the
3153 * generation number, which means that it was deleted and recreated.
3154 */
3155static int process_all_refs(struct send_ctx *sctx,
3156 enum btrfs_compare_tree_result cmd)
3157{
3158 int ret;
3159 struct btrfs_root *root;
3160 struct btrfs_path *path;
3161 struct btrfs_key key;
3162 struct btrfs_key found_key;
3163 struct extent_buffer *eb;
3164 int slot;
3165 iterate_inode_ref_t cb;
3166
3167 path = alloc_path_for_send();
3168 if (!path)
3169 return -ENOMEM;
3170
3171 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3172 root = sctx->send_root;
3173 cb = __record_new_ref;
3174 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3175 root = sctx->parent_root;
3176 cb = __record_deleted_ref;
3177 } else {
3178 BUG();
3179 }
3180
3181 key.objectid = sctx->cmp_key->objectid;
3182 key.type = BTRFS_INODE_REF_KEY;
3183 key.offset = 0;
3184 while (1) {
3185 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
Alexander Blocke938c8a2012-07-28 16:33:49 +02003186 if (ret < 0)
Alexander Block31db9f72012-07-25 23:19:24 +02003187 goto out;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003188 if (ret)
Alexander Block31db9f72012-07-25 23:19:24 +02003189 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003190
3191 eb = path->nodes[0];
3192 slot = path->slots[0];
3193 btrfs_item_key_to_cpu(eb, &found_key, slot);
3194
3195 if (found_key.objectid != key.objectid ||
Jan Schmidt96b5bd72012-10-15 08:30:45 +00003196 (found_key.type != BTRFS_INODE_REF_KEY &&
3197 found_key.type != BTRFS_INODE_EXTREF_KEY))
Alexander Block31db9f72012-07-25 23:19:24 +02003198 break;
Alexander Block31db9f72012-07-25 23:19:24 +02003199
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003200 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003201 btrfs_release_path(path);
3202 if (ret < 0)
3203 goto out;
3204
3205 key.offset = found_key.offset + 1;
3206 }
Alexander Blocke938c8a2012-07-28 16:33:49 +02003207 btrfs_release_path(path);
Alexander Block31db9f72012-07-25 23:19:24 +02003208
3209 ret = process_recorded_refs(sctx);
3210
3211out:
3212 btrfs_free_path(path);
3213 return ret;
3214}
3215
3216static int send_set_xattr(struct send_ctx *sctx,
3217 struct fs_path *path,
3218 const char *name, int name_len,
3219 const char *data, int data_len)
3220{
3221 int ret = 0;
3222
3223 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3224 if (ret < 0)
3225 goto out;
3226
3227 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3228 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3229 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3230
3231 ret = send_cmd(sctx);
3232
3233tlv_put_failure:
3234out:
3235 return ret;
3236}
3237
3238static int send_remove_xattr(struct send_ctx *sctx,
3239 struct fs_path *path,
3240 const char *name, int name_len)
3241{
3242 int ret = 0;
3243
3244 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3245 if (ret < 0)
3246 goto out;
3247
3248 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3249 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3250
3251 ret = send_cmd(sctx);
3252
3253tlv_put_failure:
3254out:
3255 return ret;
3256}
3257
3258static int __process_new_xattr(int num, struct btrfs_key *di_key,
3259 const char *name, int name_len,
3260 const char *data, int data_len,
3261 u8 type, void *ctx)
3262{
3263 int ret;
3264 struct send_ctx *sctx = ctx;
3265 struct fs_path *p;
3266 posix_acl_xattr_header dummy_acl;
3267
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003268 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003269 if (!p)
3270 return -ENOMEM;
3271
3272 /*
3273 * This hack is needed because empty acl's are stored as zero byte
3274 * data in xattrs. Problem with that is, that receiving these zero byte
3275 * acl's will fail later. To fix this, we send a dummy acl list that
3276 * only contains the version number and no entries.
3277 */
3278 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3279 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3280 if (data_len == 0) {
3281 dummy_acl.a_version =
3282 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3283 data = (char *)&dummy_acl;
3284 data_len = sizeof(dummy_acl);
3285 }
3286 }
3287
3288 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3289 if (ret < 0)
3290 goto out;
3291
3292 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3293
3294out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003295 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003296 return ret;
3297}
3298
3299static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3300 const char *name, int name_len,
3301 const char *data, int data_len,
3302 u8 type, void *ctx)
3303{
3304 int ret;
3305 struct send_ctx *sctx = ctx;
3306 struct fs_path *p;
3307
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003308 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003309 if (!p)
3310 return -ENOMEM;
3311
3312 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3313 if (ret < 0)
3314 goto out;
3315
3316 ret = send_remove_xattr(sctx, p, name, name_len);
3317
3318out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003319 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003320 return ret;
3321}
3322
3323static int process_new_xattr(struct send_ctx *sctx)
3324{
3325 int ret = 0;
3326
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003327 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3328 sctx->cmp_key, __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003329
3330 return ret;
3331}
3332
3333static int process_deleted_xattr(struct send_ctx *sctx)
3334{
3335 int ret;
3336
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003337 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3338 sctx->cmp_key, __process_deleted_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003339
3340 return ret;
3341}
3342
3343struct find_xattr_ctx {
3344 const char *name;
3345 int name_len;
3346 int found_idx;
3347 char *found_data;
3348 int found_data_len;
3349};
3350
3351static int __find_xattr(int num, struct btrfs_key *di_key,
3352 const char *name, int name_len,
3353 const char *data, int data_len,
3354 u8 type, void *vctx)
3355{
3356 struct find_xattr_ctx *ctx = vctx;
3357
3358 if (name_len == ctx->name_len &&
3359 strncmp(name, ctx->name, name_len) == 0) {
3360 ctx->found_idx = num;
3361 ctx->found_data_len = data_len;
Thomas Meyera5959bc2013-06-01 09:37:50 +00003362 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
Alexander Block31db9f72012-07-25 23:19:24 +02003363 if (!ctx->found_data)
3364 return -ENOMEM;
Alexander Block31db9f72012-07-25 23:19:24 +02003365 return 1;
3366 }
3367 return 0;
3368}
3369
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003370static int find_xattr(struct btrfs_root *root,
Alexander Block31db9f72012-07-25 23:19:24 +02003371 struct btrfs_path *path,
3372 struct btrfs_key *key,
3373 const char *name, int name_len,
3374 char **data, int *data_len)
3375{
3376 int ret;
3377 struct find_xattr_ctx ctx;
3378
3379 ctx.name = name;
3380 ctx.name_len = name_len;
3381 ctx.found_idx = -1;
3382 ctx.found_data = NULL;
3383 ctx.found_data_len = 0;
3384
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003385 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003386 if (ret < 0)
3387 return ret;
3388
3389 if (ctx.found_idx == -1)
3390 return -ENOENT;
3391 if (data) {
3392 *data = ctx.found_data;
3393 *data_len = ctx.found_data_len;
3394 } else {
3395 kfree(ctx.found_data);
3396 }
3397 return ctx.found_idx;
3398}
3399
3400
3401static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3402 const char *name, int name_len,
3403 const char *data, int data_len,
3404 u8 type, void *ctx)
3405{
3406 int ret;
3407 struct send_ctx *sctx = ctx;
3408 char *found_data = NULL;
3409 int found_data_len = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003410
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003411 ret = find_xattr(sctx->parent_root, sctx->right_path,
3412 sctx->cmp_key, name, name_len, &found_data,
3413 &found_data_len);
Alexander Block31db9f72012-07-25 23:19:24 +02003414 if (ret == -ENOENT) {
3415 ret = __process_new_xattr(num, di_key, name, name_len, data,
3416 data_len, type, ctx);
3417 } else if (ret >= 0) {
3418 if (data_len != found_data_len ||
3419 memcmp(data, found_data, data_len)) {
3420 ret = __process_new_xattr(num, di_key, name, name_len,
3421 data, data_len, type, ctx);
3422 } else {
3423 ret = 0;
3424 }
3425 }
3426
3427 kfree(found_data);
Alexander Block31db9f72012-07-25 23:19:24 +02003428 return ret;
3429}
3430
3431static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3432 const char *name, int name_len,
3433 const char *data, int data_len,
3434 u8 type, void *ctx)
3435{
3436 int ret;
3437 struct send_ctx *sctx = ctx;
3438
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003439 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
3440 name, name_len, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003441 if (ret == -ENOENT)
3442 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3443 data_len, type, ctx);
3444 else if (ret >= 0)
3445 ret = 0;
3446
3447 return ret;
3448}
3449
3450static int process_changed_xattr(struct send_ctx *sctx)
3451{
3452 int ret = 0;
3453
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003454 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003455 sctx->cmp_key, __process_changed_new_xattr, sctx);
3456 if (ret < 0)
3457 goto out;
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003458 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
Alexander Block31db9f72012-07-25 23:19:24 +02003459 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3460
3461out:
3462 return ret;
3463}
3464
3465static int process_all_new_xattrs(struct send_ctx *sctx)
3466{
3467 int ret;
3468 struct btrfs_root *root;
3469 struct btrfs_path *path;
3470 struct btrfs_key key;
3471 struct btrfs_key found_key;
3472 struct extent_buffer *eb;
3473 int slot;
3474
3475 path = alloc_path_for_send();
3476 if (!path)
3477 return -ENOMEM;
3478
3479 root = sctx->send_root;
3480
3481 key.objectid = sctx->cmp_key->objectid;
3482 key.type = BTRFS_XATTR_ITEM_KEY;
3483 key.offset = 0;
3484 while (1) {
3485 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3486 if (ret < 0)
3487 goto out;
3488 if (ret) {
3489 ret = 0;
3490 goto out;
3491 }
3492
3493 eb = path->nodes[0];
3494 slot = path->slots[0];
3495 btrfs_item_key_to_cpu(eb, &found_key, slot);
3496
3497 if (found_key.objectid != key.objectid ||
3498 found_key.type != key.type) {
3499 ret = 0;
3500 goto out;
3501 }
3502
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003503 ret = iterate_dir_item(root, path, &found_key,
3504 __process_new_xattr, sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02003505 if (ret < 0)
3506 goto out;
3507
3508 btrfs_release_path(path);
3509 key.offset = found_key.offset + 1;
3510 }
3511
3512out:
3513 btrfs_free_path(path);
3514 return ret;
3515}
3516
Josef Baciked259092013-10-25 11:36:01 -04003517static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
3518{
3519 struct btrfs_root *root = sctx->send_root;
3520 struct btrfs_fs_info *fs_info = root->fs_info;
3521 struct inode *inode;
3522 struct page *page;
3523 char *addr;
3524 struct btrfs_key key;
3525 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
3526 pgoff_t last_index;
3527 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
3528 ssize_t ret = 0;
3529
3530 key.objectid = sctx->cur_ino;
3531 key.type = BTRFS_INODE_ITEM_KEY;
3532 key.offset = 0;
3533
3534 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3535 if (IS_ERR(inode))
3536 return PTR_ERR(inode);
3537
3538 if (offset + len > i_size_read(inode)) {
3539 if (offset > i_size_read(inode))
3540 len = 0;
3541 else
3542 len = offset - i_size_read(inode);
3543 }
3544 if (len == 0)
3545 goto out;
3546
3547 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
3548 while (index <= last_index) {
3549 unsigned cur_len = min_t(unsigned, len,
3550 PAGE_CACHE_SIZE - pg_offset);
3551 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3552 if (!page) {
3553 ret = -ENOMEM;
3554 break;
3555 }
3556
3557 if (!PageUptodate(page)) {
3558 btrfs_readpage(NULL, page);
3559 lock_page(page);
3560 if (!PageUptodate(page)) {
3561 unlock_page(page);
3562 page_cache_release(page);
3563 ret = -EIO;
3564 break;
3565 }
3566 }
3567
3568 addr = kmap(page);
3569 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
3570 kunmap(page);
3571 unlock_page(page);
3572 page_cache_release(page);
3573 index++;
3574 pg_offset = 0;
3575 len -= cur_len;
3576 ret += cur_len;
3577 }
3578out:
3579 iput(inode);
3580 return ret;
3581}
3582
Alexander Block31db9f72012-07-25 23:19:24 +02003583/*
3584 * Read some bytes from the current inode/file and send a write command to
3585 * user space.
3586 */
3587static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3588{
3589 int ret = 0;
3590 struct fs_path *p;
Josef Baciked259092013-10-25 11:36:01 -04003591 ssize_t num_read = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003592
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
Alexander Block31db9f72012-07-25 23:19:24 +02003597verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3598
Josef Baciked259092013-10-25 11:36:01 -04003599 num_read = fill_read_buf(sctx, offset, len);
3600 if (num_read <= 0) {
3601 if (num_read < 0)
3602 ret = num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02003603 goto out;
Josef Baciked259092013-10-25 11:36:01 -04003604 }
Alexander Block31db9f72012-07-25 23:19:24 +02003605
3606 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3607 if (ret < 0)
3608 goto out;
3609
3610 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3611 if (ret < 0)
3612 goto out;
3613
3614 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3615 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
Alexander Blocke938c8a2012-07-28 16:33:49 +02003616 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
Alexander Block31db9f72012-07-25 23:19:24 +02003617
3618 ret = send_cmd(sctx);
3619
3620tlv_put_failure:
3621out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003622 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003623 if (ret < 0)
3624 return ret;
Alexander Blocke938c8a2012-07-28 16:33:49 +02003625 return num_read;
Alexander Block31db9f72012-07-25 23:19:24 +02003626}
3627
3628/*
3629 * Send a clone command to user space.
3630 */
3631static int send_clone(struct send_ctx *sctx,
3632 u64 offset, u32 len,
3633 struct clone_root *clone_root)
3634{
3635 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003636 struct fs_path *p;
3637 u64 gen;
3638
3639verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3640 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3641 clone_root->root->objectid, clone_root->ino,
3642 clone_root->offset);
3643
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003644 p = fs_path_alloc();
Alexander Block31db9f72012-07-25 23:19:24 +02003645 if (!p)
3646 return -ENOMEM;
3647
3648 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3649 if (ret < 0)
3650 goto out;
3651
3652 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3653 if (ret < 0)
3654 goto out;
3655
3656 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3657 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3658 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3659
Alexander Blocke938c8a2012-07-28 16:33:49 +02003660 if (clone_root->root == sctx->send_root) {
Alexander Block31db9f72012-07-25 23:19:24 +02003661 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02003662 &gen, NULL, NULL, NULL, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02003663 if (ret < 0)
3664 goto out;
3665 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3666 } else {
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003667 ret = get_inode_path(clone_root->root, clone_root->ino, p);
Alexander Block31db9f72012-07-25 23:19:24 +02003668 }
3669 if (ret < 0)
3670 goto out;
3671
3672 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
Alexander Blocke938c8a2012-07-28 16:33:49 +02003673 clone_root->root->root_item.uuid);
Alexander Block31db9f72012-07-25 23:19:24 +02003674 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
Filipe David Borba Manana5a0f4e22013-12-03 15:55:48 +00003675 le64_to_cpu(clone_root->root->root_item.ctransid));
Alexander Block31db9f72012-07-25 23:19:24 +02003676 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3677 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3678 clone_root->offset);
3679
3680 ret = send_cmd(sctx);
3681
3682tlv_put_failure:
3683out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003684 fs_path_free(p);
Alexander Block31db9f72012-07-25 23:19:24 +02003685 return ret;
3686}
3687
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003688/*
3689 * Send an update extent command to user space.
3690 */
3691static int send_update_extent(struct send_ctx *sctx,
3692 u64 offset, u32 len)
3693{
3694 int ret = 0;
3695 struct fs_path *p;
3696
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003697 p = fs_path_alloc();
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003698 if (!p)
3699 return -ENOMEM;
3700
3701 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
3702 if (ret < 0)
3703 goto out;
3704
3705 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3706 if (ret < 0)
3707 goto out;
3708
3709 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3710 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3711 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
3712
3713 ret = send_cmd(sctx);
3714
3715tlv_put_failure:
3716out:
Tsutomu Itoh924794c2013-05-08 07:51:52 +00003717 fs_path_free(p);
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003718 return ret;
3719}
3720
Josef Bacik16e75492013-10-22 12:18:51 -04003721static int send_hole(struct send_ctx *sctx, u64 end)
3722{
3723 struct fs_path *p = NULL;
3724 u64 offset = sctx->cur_inode_last_extent;
3725 u64 len;
3726 int ret = 0;
3727
3728 p = fs_path_alloc();
3729 if (!p)
3730 return -ENOMEM;
3731 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
3732 while (offset < end) {
3733 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
3734
3735 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3736 if (ret < 0)
3737 break;
3738 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3739 if (ret < 0)
3740 break;
3741 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3742 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3743 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
3744 ret = send_cmd(sctx);
3745 if (ret < 0)
3746 break;
3747 offset += len;
3748 }
3749tlv_put_failure:
3750 fs_path_free(p);
3751 return ret;
3752}
3753
Alexander Block31db9f72012-07-25 23:19:24 +02003754static int send_write_or_clone(struct send_ctx *sctx,
3755 struct btrfs_path *path,
3756 struct btrfs_key *key,
3757 struct clone_root *clone_root)
3758{
3759 int ret = 0;
3760 struct btrfs_file_extent_item *ei;
3761 u64 offset = key->offset;
3762 u64 pos = 0;
3763 u64 len;
3764 u32 l;
3765 u8 type;
3766
3767 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3768 struct btrfs_file_extent_item);
3769 type = btrfs_file_extent_type(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003770 if (type == BTRFS_FILE_EXTENT_INLINE) {
Alexander Block31db9f72012-07-25 23:19:24 +02003771 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003772 /*
3773 * it is possible the inline item won't cover the whole page,
3774 * but there may be items after this page. Make
3775 * sure to send the whole thing
3776 */
3777 len = PAGE_CACHE_ALIGN(len);
3778 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02003779 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
Chris Mason74dd17f2012-08-07 16:25:13 -04003780 }
Alexander Block31db9f72012-07-25 23:19:24 +02003781
3782 if (offset + len > sctx->cur_inode_size)
3783 len = sctx->cur_inode_size - offset;
3784 if (len == 0) {
3785 ret = 0;
3786 goto out;
3787 }
3788
Mark Fashehcb95e7b2013-02-04 20:54:57 +00003789 if (clone_root) {
3790 ret = send_clone(sctx, offset, len, clone_root);
3791 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
3792 ret = send_update_extent(sctx, offset, len);
3793 } else {
Alexander Block31db9f72012-07-25 23:19:24 +02003794 while (pos < len) {
3795 l = len - pos;
3796 if (l > BTRFS_SEND_READ_SIZE)
3797 l = BTRFS_SEND_READ_SIZE;
3798 ret = send_write(sctx, pos + offset, l);
3799 if (ret < 0)
3800 goto out;
3801 if (!ret)
3802 break;
3803 pos += ret;
3804 }
3805 ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02003806 }
Alexander Block31db9f72012-07-25 23:19:24 +02003807out:
3808 return ret;
3809}
3810
3811static int is_extent_unchanged(struct send_ctx *sctx,
3812 struct btrfs_path *left_path,
3813 struct btrfs_key *ekey)
3814{
3815 int ret = 0;
3816 struct btrfs_key key;
3817 struct btrfs_path *path = NULL;
3818 struct extent_buffer *eb;
3819 int slot;
3820 struct btrfs_key found_key;
3821 struct btrfs_file_extent_item *ei;
3822 u64 left_disknr;
3823 u64 right_disknr;
3824 u64 left_offset;
3825 u64 right_offset;
3826 u64 left_offset_fixed;
3827 u64 left_len;
3828 u64 right_len;
Chris Mason74dd17f2012-08-07 16:25:13 -04003829 u64 left_gen;
3830 u64 right_gen;
Alexander Block31db9f72012-07-25 23:19:24 +02003831 u8 left_type;
3832 u8 right_type;
3833
3834 path = alloc_path_for_send();
3835 if (!path)
3836 return -ENOMEM;
3837
3838 eb = left_path->nodes[0];
3839 slot = left_path->slots[0];
Alexander Block31db9f72012-07-25 23:19:24 +02003840 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3841 left_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02003842
3843 if (left_type != BTRFS_FILE_EXTENT_REG) {
3844 ret = 0;
3845 goto out;
3846 }
Chris Mason74dd17f2012-08-07 16:25:13 -04003847 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3848 left_len = btrfs_file_extent_num_bytes(eb, ei);
3849 left_offset = btrfs_file_extent_offset(eb, ei);
3850 left_gen = btrfs_file_extent_generation(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02003851
3852 /*
3853 * Following comments will refer to these graphics. L is the left
3854 * extents which we are checking at the moment. 1-8 are the right
3855 * extents that we iterate.
3856 *
3857 * |-----L-----|
3858 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3859 *
3860 * |-----L-----|
3861 * |--1--|-2b-|...(same as above)
3862 *
3863 * Alternative situation. Happens on files where extents got split.
3864 * |-----L-----|
3865 * |-----------7-----------|-6-|
3866 *
3867 * Alternative situation. Happens on files which got larger.
3868 * |-----L-----|
3869 * |-8-|
3870 * Nothing follows after 8.
3871 */
3872
3873 key.objectid = ekey->objectid;
3874 key.type = BTRFS_EXTENT_DATA_KEY;
3875 key.offset = ekey->offset;
3876 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3877 if (ret < 0)
3878 goto out;
3879 if (ret) {
3880 ret = 0;
3881 goto out;
3882 }
3883
3884 /*
3885 * Handle special case where the right side has no extents at all.
3886 */
3887 eb = path->nodes[0];
3888 slot = path->slots[0];
3889 btrfs_item_key_to_cpu(eb, &found_key, slot);
3890 if (found_key.objectid != key.objectid ||
3891 found_key.type != key.type) {
Josef Bacik57cfd462013-08-20 15:55:39 -04003892 /* If we're a hole then just pretend nothing changed */
3893 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02003894 goto out;
3895 }
3896
3897 /*
3898 * We're now on 2a, 2b or 7.
3899 */
3900 key = found_key;
3901 while (key.offset < ekey->offset + left_len) {
3902 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3903 right_type = btrfs_file_extent_type(eb, ei);
Alexander Block31db9f72012-07-25 23:19:24 +02003904 if (right_type != BTRFS_FILE_EXTENT_REG) {
3905 ret = 0;
3906 goto out;
3907 }
3908
Josef Bacik007d31f2013-10-31 16:49:02 -04003909 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3910 right_len = btrfs_file_extent_num_bytes(eb, ei);
3911 right_offset = btrfs_file_extent_offset(eb, ei);
3912 right_gen = btrfs_file_extent_generation(eb, ei);
3913
Alexander Block31db9f72012-07-25 23:19:24 +02003914 /*
3915 * Are we at extent 8? If yes, we know the extent is changed.
3916 * This may only happen on the first iteration.
3917 */
Alexander Blockd8347fa2012-08-01 12:49:15 +02003918 if (found_key.offset + right_len <= ekey->offset) {
Josef Bacik57cfd462013-08-20 15:55:39 -04003919 /* If we're a hole just pretend nothing changed */
3920 ret = (left_disknr) ? 0 : 1;
Alexander Block31db9f72012-07-25 23:19:24 +02003921 goto out;
3922 }
3923
3924 left_offset_fixed = left_offset;
3925 if (key.offset < ekey->offset) {
3926 /* Fix the right offset for 2a and 7. */
3927 right_offset += ekey->offset - key.offset;
3928 } else {
3929 /* Fix the left offset for all behind 2a and 2b */
3930 left_offset_fixed += key.offset - ekey->offset;
3931 }
3932
3933 /*
3934 * Check if we have the same extent.
3935 */
Alexander Block39540962012-08-01 12:46:05 +02003936 if (left_disknr != right_disknr ||
Chris Mason74dd17f2012-08-07 16:25:13 -04003937 left_offset_fixed != right_offset ||
3938 left_gen != right_gen) {
Alexander Block31db9f72012-07-25 23:19:24 +02003939 ret = 0;
3940 goto out;
3941 }
3942
3943 /*
3944 * Go to the next extent.
3945 */
3946 ret = btrfs_next_item(sctx->parent_root, path);
3947 if (ret < 0)
3948 goto out;
3949 if (!ret) {
3950 eb = path->nodes[0];
3951 slot = path->slots[0];
3952 btrfs_item_key_to_cpu(eb, &found_key, slot);
3953 }
3954 if (ret || found_key.objectid != key.objectid ||
3955 found_key.type != key.type) {
3956 key.offset += right_len;
3957 break;
Jan Schmidtadaa4b82013-03-21 14:30:23 +00003958 }
3959 if (found_key.offset != key.offset + right_len) {
3960 ret = 0;
3961 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02003962 }
3963 key = found_key;
3964 }
3965
3966 /*
3967 * We're now behind the left extent (treat as unchanged) or at the end
3968 * of the right side (treat as changed).
3969 */
3970 if (key.offset >= ekey->offset + left_len)
3971 ret = 1;
3972 else
3973 ret = 0;
3974
3975
3976out:
3977 btrfs_free_path(path);
3978 return ret;
3979}
3980
Josef Bacik16e75492013-10-22 12:18:51 -04003981static int get_last_extent(struct send_ctx *sctx, u64 offset)
3982{
3983 struct btrfs_path *path;
3984 struct btrfs_root *root = sctx->send_root;
3985 struct btrfs_file_extent_item *fi;
3986 struct btrfs_key key;
3987 u64 extent_end;
3988 u8 type;
3989 int ret;
3990
3991 path = alloc_path_for_send();
3992 if (!path)
3993 return -ENOMEM;
3994
3995 sctx->cur_inode_last_extent = 0;
3996
3997 key.objectid = sctx->cur_ino;
3998 key.type = BTRFS_EXTENT_DATA_KEY;
3999 key.offset = offset;
4000 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4001 if (ret < 0)
4002 goto out;
4003 ret = 0;
4004 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4005 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4006 goto out;
4007
4008 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4009 struct btrfs_file_extent_item);
4010 type = btrfs_file_extent_type(path->nodes[0], fi);
4011 if (type == BTRFS_FILE_EXTENT_INLINE) {
4012 u64 size = btrfs_file_extent_inline_len(path->nodes[0], fi);
4013 extent_end = ALIGN(key.offset + size,
4014 sctx->send_root->sectorsize);
4015 } else {
4016 extent_end = key.offset +
4017 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4018 }
4019 sctx->cur_inode_last_extent = extent_end;
4020out:
4021 btrfs_free_path(path);
4022 return ret;
4023}
4024
4025static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4026 struct btrfs_key *key)
4027{
4028 struct btrfs_file_extent_item *fi;
4029 u64 extent_end;
4030 u8 type;
4031 int ret = 0;
4032
4033 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4034 return 0;
4035
4036 if (sctx->cur_inode_last_extent == (u64)-1) {
4037 ret = get_last_extent(sctx, key->offset - 1);
4038 if (ret)
4039 return ret;
4040 }
4041
4042 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4043 struct btrfs_file_extent_item);
4044 type = btrfs_file_extent_type(path->nodes[0], fi);
4045 if (type == BTRFS_FILE_EXTENT_INLINE) {
4046 u64 size = btrfs_file_extent_inline_len(path->nodes[0], fi);
4047 extent_end = ALIGN(key->offset + size,
4048 sctx->send_root->sectorsize);
4049 } else {
4050 extent_end = key->offset +
4051 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4052 }
4053 if (sctx->cur_inode_last_extent < key->offset)
4054 ret = send_hole(sctx, key->offset);
4055 sctx->cur_inode_last_extent = extent_end;
4056 return ret;
4057}
4058
Alexander Block31db9f72012-07-25 23:19:24 +02004059static int process_extent(struct send_ctx *sctx,
4060 struct btrfs_path *path,
4061 struct btrfs_key *key)
4062{
Alexander Block31db9f72012-07-25 23:19:24 +02004063 struct clone_root *found_clone = NULL;
Josef Bacik57cfd462013-08-20 15:55:39 -04004064 int ret = 0;
Alexander Block31db9f72012-07-25 23:19:24 +02004065
4066 if (S_ISLNK(sctx->cur_inode_mode))
4067 return 0;
4068
4069 if (sctx->parent_root && !sctx->cur_inode_new) {
4070 ret = is_extent_unchanged(sctx, path, key);
4071 if (ret < 0)
4072 goto out;
4073 if (ret) {
4074 ret = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004075 goto out_hole;
Alexander Block31db9f72012-07-25 23:19:24 +02004076 }
Josef Bacik57cfd462013-08-20 15:55:39 -04004077 } else {
4078 struct btrfs_file_extent_item *ei;
4079 u8 type;
4080
4081 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4082 struct btrfs_file_extent_item);
4083 type = btrfs_file_extent_type(path->nodes[0], ei);
4084 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4085 type == BTRFS_FILE_EXTENT_REG) {
4086 /*
4087 * The send spec does not have a prealloc command yet,
4088 * so just leave a hole for prealloc'ed extents until
4089 * we have enough commands queued up to justify rev'ing
4090 * the send spec.
4091 */
4092 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4093 ret = 0;
4094 goto out;
4095 }
4096
4097 /* Have a hole, just skip it. */
4098 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4099 ret = 0;
4100 goto out;
4101 }
4102 }
Alexander Block31db9f72012-07-25 23:19:24 +02004103 }
4104
4105 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4106 sctx->cur_inode_size, &found_clone);
4107 if (ret != -ENOENT && ret < 0)
4108 goto out;
4109
4110 ret = send_write_or_clone(sctx, path, key, found_clone);
Josef Bacik16e75492013-10-22 12:18:51 -04004111 if (ret)
4112 goto out;
4113out_hole:
4114 ret = maybe_send_hole(sctx, path, key);
Alexander Block31db9f72012-07-25 23:19:24 +02004115out:
4116 return ret;
4117}
4118
4119static int process_all_extents(struct send_ctx *sctx)
4120{
4121 int ret;
4122 struct btrfs_root *root;
4123 struct btrfs_path *path;
4124 struct btrfs_key key;
4125 struct btrfs_key found_key;
4126 struct extent_buffer *eb;
4127 int slot;
4128
4129 root = sctx->send_root;
4130 path = alloc_path_for_send();
4131 if (!path)
4132 return -ENOMEM;
4133
4134 key.objectid = sctx->cmp_key->objectid;
4135 key.type = BTRFS_EXTENT_DATA_KEY;
4136 key.offset = 0;
4137 while (1) {
4138 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
4139 if (ret < 0)
4140 goto out;
4141 if (ret) {
4142 ret = 0;
4143 goto out;
4144 }
4145
4146 eb = path->nodes[0];
4147 slot = path->slots[0];
4148 btrfs_item_key_to_cpu(eb, &found_key, slot);
4149
4150 if (found_key.objectid != key.objectid ||
4151 found_key.type != key.type) {
4152 ret = 0;
4153 goto out;
4154 }
4155
4156 ret = process_extent(sctx, path, &found_key);
4157 if (ret < 0)
4158 goto out;
4159
4160 btrfs_release_path(path);
4161 key.offset = found_key.offset + 1;
4162 }
4163
4164out:
4165 btrfs_free_path(path);
4166 return ret;
4167}
4168
4169static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
4170{
4171 int ret = 0;
4172
4173 if (sctx->cur_ino == 0)
4174 goto out;
4175 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004176 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004177 goto out;
4178 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4179 goto out;
4180
4181 ret = process_recorded_refs(sctx);
Alexander Blocke479d9b2012-07-28 16:09:35 +02004182 if (ret < 0)
4183 goto out;
4184
4185 /*
4186 * We have processed the refs and thus need to advance send_progress.
4187 * Now, calls to get_cur_xxx will take the updated refs of the current
4188 * inode into account.
4189 */
4190 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004191
4192out:
4193 return ret;
4194}
4195
4196static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4197{
4198 int ret = 0;
4199 u64 left_mode;
4200 u64 left_uid;
4201 u64 left_gid;
4202 u64 right_mode;
4203 u64 right_uid;
4204 u64 right_gid;
4205 int need_chmod = 0;
4206 int need_chown = 0;
4207
4208 ret = process_recorded_refs_if_needed(sctx, at_end);
4209 if (ret < 0)
4210 goto out;
4211
4212 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4213 goto out;
4214 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4215 goto out;
4216
4217 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
Alexander Block85a7b332012-07-26 23:39:10 +02004218 &left_mode, &left_uid, &left_gid, NULL);
Alexander Block31db9f72012-07-25 23:19:24 +02004219 if (ret < 0)
4220 goto out;
4221
Alex Lyakase2d044f2012-10-17 13:52:47 +00004222 if (!sctx->parent_root || sctx->cur_inode_new) {
4223 need_chown = 1;
4224 if (!S_ISLNK(sctx->cur_inode_mode))
Alexander Block31db9f72012-07-25 23:19:24 +02004225 need_chmod = 1;
Alex Lyakase2d044f2012-10-17 13:52:47 +00004226 } else {
4227 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4228 NULL, NULL, &right_mode, &right_uid,
4229 &right_gid, NULL);
4230 if (ret < 0)
4231 goto out;
Alexander Block31db9f72012-07-25 23:19:24 +02004232
Alex Lyakase2d044f2012-10-17 13:52:47 +00004233 if (left_uid != right_uid || left_gid != right_gid)
4234 need_chown = 1;
4235 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4236 need_chmod = 1;
Alexander Block31db9f72012-07-25 23:19:24 +02004237 }
4238
4239 if (S_ISREG(sctx->cur_inode_mode)) {
Josef Bacik16e75492013-10-22 12:18:51 -04004240 if (need_send_hole(sctx)) {
4241 if (sctx->cur_inode_last_extent == (u64)-1) {
4242 ret = get_last_extent(sctx, (u64)-1);
4243 if (ret)
4244 goto out;
4245 }
4246 if (sctx->cur_inode_last_extent <
4247 sctx->cur_inode_size) {
4248 ret = send_hole(sctx, sctx->cur_inode_size);
4249 if (ret)
4250 goto out;
4251 }
4252 }
Alexander Block31db9f72012-07-25 23:19:24 +02004253 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4254 sctx->cur_inode_size);
4255 if (ret < 0)
4256 goto out;
4257 }
4258
4259 if (need_chown) {
4260 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4261 left_uid, left_gid);
4262 if (ret < 0)
4263 goto out;
4264 }
4265 if (need_chmod) {
4266 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4267 left_mode);
4268 if (ret < 0)
4269 goto out;
4270 }
4271
4272 /*
4273 * Need to send that every time, no matter if it actually changed
4274 * between the two trees as we have done changes to the inode before.
4275 */
4276 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4277 if (ret < 0)
4278 goto out;
4279
4280out:
4281 return ret;
4282}
4283
4284static int changed_inode(struct send_ctx *sctx,
4285 enum btrfs_compare_tree_result result)
4286{
4287 int ret = 0;
4288 struct btrfs_key *key = sctx->cmp_key;
4289 struct btrfs_inode_item *left_ii = NULL;
4290 struct btrfs_inode_item *right_ii = NULL;
4291 u64 left_gen = 0;
4292 u64 right_gen = 0;
4293
Alexander Block31db9f72012-07-25 23:19:24 +02004294 sctx->cur_ino = key->objectid;
4295 sctx->cur_inode_new_gen = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004296 sctx->cur_inode_last_extent = (u64)-1;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004297
4298 /*
4299 * Set send_progress to current inode. This will tell all get_cur_xxx
4300 * functions that the current inode's refs are not updated yet. Later,
4301 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4302 */
Alexander Block31db9f72012-07-25 23:19:24 +02004303 sctx->send_progress = sctx->cur_ino;
4304
4305 if (result == BTRFS_COMPARE_TREE_NEW ||
4306 result == BTRFS_COMPARE_TREE_CHANGED) {
4307 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4308 sctx->left_path->slots[0],
4309 struct btrfs_inode_item);
4310 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4311 left_ii);
4312 } else {
4313 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4314 sctx->right_path->slots[0],
4315 struct btrfs_inode_item);
4316 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4317 right_ii);
4318 }
4319 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4320 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4321 sctx->right_path->slots[0],
4322 struct btrfs_inode_item);
4323
4324 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4325 right_ii);
Alexander Block6d85ed02012-08-01 14:48:59 +02004326
4327 /*
4328 * The cur_ino = root dir case is special here. We can't treat
4329 * the inode as deleted+reused because it would generate a
4330 * stream that tries to delete/mkdir the root dir.
4331 */
4332 if (left_gen != right_gen &&
4333 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block31db9f72012-07-25 23:19:24 +02004334 sctx->cur_inode_new_gen = 1;
4335 }
4336
4337 if (result == BTRFS_COMPARE_TREE_NEW) {
4338 sctx->cur_inode_gen = left_gen;
4339 sctx->cur_inode_new = 1;
4340 sctx->cur_inode_deleted = 0;
4341 sctx->cur_inode_size = btrfs_inode_size(
4342 sctx->left_path->nodes[0], left_ii);
4343 sctx->cur_inode_mode = btrfs_inode_mode(
4344 sctx->left_path->nodes[0], left_ii);
4345 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
Alexander Block1f4692d2012-07-28 10:42:24 +02004346 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004347 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4348 sctx->cur_inode_gen = right_gen;
4349 sctx->cur_inode_new = 0;
4350 sctx->cur_inode_deleted = 1;
4351 sctx->cur_inode_size = btrfs_inode_size(
4352 sctx->right_path->nodes[0], right_ii);
4353 sctx->cur_inode_mode = btrfs_inode_mode(
4354 sctx->right_path->nodes[0], right_ii);
4355 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
Alexander Block766702e2012-07-28 14:11:31 +02004356 /*
4357 * We need to do some special handling in case the inode was
4358 * reported as changed with a changed generation number. This
4359 * means that the original inode was deleted and new inode
4360 * reused the same inum. So we have to treat the old inode as
4361 * deleted and the new one as new.
4362 */
Alexander Block31db9f72012-07-25 23:19:24 +02004363 if (sctx->cur_inode_new_gen) {
Alexander Block766702e2012-07-28 14:11:31 +02004364 /*
4365 * First, process the inode as if it was deleted.
4366 */
Alexander Block31db9f72012-07-25 23:19:24 +02004367 sctx->cur_inode_gen = right_gen;
4368 sctx->cur_inode_new = 0;
4369 sctx->cur_inode_deleted = 1;
4370 sctx->cur_inode_size = btrfs_inode_size(
4371 sctx->right_path->nodes[0], right_ii);
4372 sctx->cur_inode_mode = btrfs_inode_mode(
4373 sctx->right_path->nodes[0], right_ii);
4374 ret = process_all_refs(sctx,
4375 BTRFS_COMPARE_TREE_DELETED);
4376 if (ret < 0)
4377 goto out;
4378
Alexander Block766702e2012-07-28 14:11:31 +02004379 /*
4380 * Now process the inode as if it was new.
4381 */
Alexander Block31db9f72012-07-25 23:19:24 +02004382 sctx->cur_inode_gen = left_gen;
4383 sctx->cur_inode_new = 1;
4384 sctx->cur_inode_deleted = 0;
4385 sctx->cur_inode_size = btrfs_inode_size(
4386 sctx->left_path->nodes[0], left_ii);
4387 sctx->cur_inode_mode = btrfs_inode_mode(
4388 sctx->left_path->nodes[0], left_ii);
Alexander Block1f4692d2012-07-28 10:42:24 +02004389 ret = send_create_inode_if_needed(sctx);
Alexander Block31db9f72012-07-25 23:19:24 +02004390 if (ret < 0)
4391 goto out;
4392
4393 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4394 if (ret < 0)
4395 goto out;
Alexander Blocke479d9b2012-07-28 16:09:35 +02004396 /*
4397 * Advance send_progress now as we did not get into
4398 * process_recorded_refs_if_needed in the new_gen case.
4399 */
4400 sctx->send_progress = sctx->cur_ino + 1;
Alexander Block766702e2012-07-28 14:11:31 +02004401
4402 /*
4403 * Now process all extents and xattrs of the inode as if
4404 * they were all new.
4405 */
Alexander Block31db9f72012-07-25 23:19:24 +02004406 ret = process_all_extents(sctx);
4407 if (ret < 0)
4408 goto out;
4409 ret = process_all_new_xattrs(sctx);
4410 if (ret < 0)
4411 goto out;
4412 } else {
4413 sctx->cur_inode_gen = left_gen;
4414 sctx->cur_inode_new = 0;
4415 sctx->cur_inode_new_gen = 0;
4416 sctx->cur_inode_deleted = 0;
4417 sctx->cur_inode_size = btrfs_inode_size(
4418 sctx->left_path->nodes[0], left_ii);
4419 sctx->cur_inode_mode = btrfs_inode_mode(
4420 sctx->left_path->nodes[0], left_ii);
4421 }
4422 }
4423
4424out:
4425 return ret;
4426}
4427
Alexander Block766702e2012-07-28 14:11:31 +02004428/*
4429 * We have to process new refs before deleted refs, but compare_trees gives us
4430 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4431 * first and later process them in process_recorded_refs.
4432 * For the cur_inode_new_gen case, we skip recording completely because
4433 * changed_inode did already initiate processing of refs. The reason for this is
4434 * that in this case, compare_tree actually compares the refs of 2 different
4435 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4436 * refs of the right tree as deleted and all refs of the left tree as new.
4437 */
Alexander Block31db9f72012-07-25 23:19:24 +02004438static int changed_ref(struct send_ctx *sctx,
4439 enum btrfs_compare_tree_result result)
4440{
4441 int ret = 0;
4442
4443 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4444
4445 if (!sctx->cur_inode_new_gen &&
4446 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4447 if (result == BTRFS_COMPARE_TREE_NEW)
4448 ret = record_new_ref(sctx);
4449 else if (result == BTRFS_COMPARE_TREE_DELETED)
4450 ret = record_deleted_ref(sctx);
4451 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4452 ret = record_changed_ref(sctx);
4453 }
4454
4455 return ret;
4456}
4457
Alexander Block766702e2012-07-28 14:11:31 +02004458/*
4459 * Process new/deleted/changed xattrs. We skip processing in the
4460 * cur_inode_new_gen case because changed_inode did already initiate processing
4461 * of xattrs. The reason is the same as in changed_ref
4462 */
Alexander Block31db9f72012-07-25 23:19:24 +02004463static int changed_xattr(struct send_ctx *sctx,
4464 enum btrfs_compare_tree_result result)
4465{
4466 int ret = 0;
4467
4468 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4469
4470 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4471 if (result == BTRFS_COMPARE_TREE_NEW)
4472 ret = process_new_xattr(sctx);
4473 else if (result == BTRFS_COMPARE_TREE_DELETED)
4474 ret = process_deleted_xattr(sctx);
4475 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4476 ret = process_changed_xattr(sctx);
4477 }
4478
4479 return ret;
4480}
4481
Alexander Block766702e2012-07-28 14:11:31 +02004482/*
4483 * Process new/deleted/changed extents. We skip processing in the
4484 * cur_inode_new_gen case because changed_inode did already initiate processing
4485 * of extents. The reason is the same as in changed_ref
4486 */
Alexander Block31db9f72012-07-25 23:19:24 +02004487static int changed_extent(struct send_ctx *sctx,
4488 enum btrfs_compare_tree_result result)
4489{
4490 int ret = 0;
4491
4492 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4493
4494 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4495 if (result != BTRFS_COMPARE_TREE_DELETED)
4496 ret = process_extent(sctx, sctx->left_path,
4497 sctx->cmp_key);
4498 }
4499
4500 return ret;
4501}
4502
Josef Bacikba5e8f22013-08-16 16:52:55 -04004503static int dir_changed(struct send_ctx *sctx, u64 dir)
4504{
4505 u64 orig_gen, new_gen;
4506 int ret;
4507
4508 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
4509 NULL, NULL);
4510 if (ret)
4511 return ret;
4512
4513 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
4514 NULL, NULL, NULL);
4515 if (ret)
4516 return ret;
4517
4518 return (orig_gen != new_gen) ? 1 : 0;
4519}
4520
4521static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
4522 struct btrfs_key *key)
4523{
4524 struct btrfs_inode_extref *extref;
4525 struct extent_buffer *leaf;
4526 u64 dirid = 0, last_dirid = 0;
4527 unsigned long ptr;
4528 u32 item_size;
4529 u32 cur_offset = 0;
4530 int ref_name_len;
4531 int ret = 0;
4532
4533 /* Easy case, just check this one dirid */
4534 if (key->type == BTRFS_INODE_REF_KEY) {
4535 dirid = key->offset;
4536
4537 ret = dir_changed(sctx, dirid);
4538 goto out;
4539 }
4540
4541 leaf = path->nodes[0];
4542 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4543 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4544 while (cur_offset < item_size) {
4545 extref = (struct btrfs_inode_extref *)(ptr +
4546 cur_offset);
4547 dirid = btrfs_inode_extref_parent(leaf, extref);
4548 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
4549 cur_offset += ref_name_len + sizeof(*extref);
4550 if (dirid == last_dirid)
4551 continue;
4552 ret = dir_changed(sctx, dirid);
4553 if (ret)
4554 break;
4555 last_dirid = dirid;
4556 }
4557out:
4558 return ret;
4559}
4560
Alexander Block766702e2012-07-28 14:11:31 +02004561/*
4562 * Updates compare related fields in sctx and simply forwards to the actual
4563 * changed_xxx functions.
4564 */
Alexander Block31db9f72012-07-25 23:19:24 +02004565static int changed_cb(struct btrfs_root *left_root,
4566 struct btrfs_root *right_root,
4567 struct btrfs_path *left_path,
4568 struct btrfs_path *right_path,
4569 struct btrfs_key *key,
4570 enum btrfs_compare_tree_result result,
4571 void *ctx)
4572{
4573 int ret = 0;
4574 struct send_ctx *sctx = ctx;
4575
Josef Bacikba5e8f22013-08-16 16:52:55 -04004576 if (result == BTRFS_COMPARE_TREE_SAME) {
Josef Bacik16e75492013-10-22 12:18:51 -04004577 if (key->type == BTRFS_INODE_REF_KEY ||
4578 key->type == BTRFS_INODE_EXTREF_KEY) {
4579 ret = compare_refs(sctx, left_path, key);
4580 if (!ret)
4581 return 0;
4582 if (ret < 0)
4583 return ret;
4584 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
4585 return maybe_send_hole(sctx, left_path, key);
4586 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04004587 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004588 }
Josef Bacikba5e8f22013-08-16 16:52:55 -04004589 result = BTRFS_COMPARE_TREE_CHANGED;
4590 ret = 0;
4591 }
4592
Alexander Block31db9f72012-07-25 23:19:24 +02004593 sctx->left_path = left_path;
4594 sctx->right_path = right_path;
4595 sctx->cmp_key = key;
4596
4597 ret = finish_inode_if_needed(sctx, 0);
4598 if (ret < 0)
4599 goto out;
4600
Alexander Block2981e222012-08-01 14:47:03 +02004601 /* Ignore non-FS objects */
4602 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4603 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4604 goto out;
4605
Alexander Block31db9f72012-07-25 23:19:24 +02004606 if (key->type == BTRFS_INODE_ITEM_KEY)
4607 ret = changed_inode(sctx, result);
Jan Schmidt96b5bd72012-10-15 08:30:45 +00004608 else if (key->type == BTRFS_INODE_REF_KEY ||
4609 key->type == BTRFS_INODE_EXTREF_KEY)
Alexander Block31db9f72012-07-25 23:19:24 +02004610 ret = changed_ref(sctx, result);
4611 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4612 ret = changed_xattr(sctx, result);
4613 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4614 ret = changed_extent(sctx, result);
4615
4616out:
4617 return ret;
4618}
4619
4620static int full_send_tree(struct send_ctx *sctx)
4621{
4622 int ret;
4623 struct btrfs_trans_handle *trans = NULL;
4624 struct btrfs_root *send_root = sctx->send_root;
4625 struct btrfs_key key;
4626 struct btrfs_key found_key;
4627 struct btrfs_path *path;
4628 struct extent_buffer *eb;
4629 int slot;
4630 u64 start_ctransid;
4631 u64 ctransid;
4632
4633 path = alloc_path_for_send();
4634 if (!path)
4635 return -ENOMEM;
4636
Anand Jain5f3ab902012-12-07 09:28:54 +00004637 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004638 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00004639 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004640
4641 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4642 key.type = BTRFS_INODE_ITEM_KEY;
4643 key.offset = 0;
4644
4645join_trans:
4646 /*
4647 * We need to make sure the transaction does not get committed
4648 * while we do anything on commit roots. Join a transaction to prevent
4649 * this.
4650 */
4651 trans = btrfs_join_transaction(send_root);
4652 if (IS_ERR(trans)) {
4653 ret = PTR_ERR(trans);
4654 trans = NULL;
4655 goto out;
4656 }
4657
4658 /*
Alexander Block766702e2012-07-28 14:11:31 +02004659 * Make sure the tree has not changed after re-joining. We detect this
4660 * by comparing start_ctransid and ctransid. They should always match.
Alexander Block31db9f72012-07-25 23:19:24 +02004661 */
Anand Jain5f3ab902012-12-07 09:28:54 +00004662 spin_lock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004663 ctransid = btrfs_root_ctransid(&send_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00004664 spin_unlock(&send_root->root_item_lock);
Alexander Block31db9f72012-07-25 23:19:24 +02004665
4666 if (ctransid != start_ctransid) {
4667 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4668 "send was modified in between. This is "
4669 "probably a bug.\n");
4670 ret = -EIO;
4671 goto out;
4672 }
4673
4674 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4675 if (ret < 0)
4676 goto out;
4677 if (ret)
4678 goto out_finish;
4679
4680 while (1) {
4681 /*
4682 * When someone want to commit while we iterate, end the
4683 * joined transaction and rejoin.
4684 */
4685 if (btrfs_should_end_transaction(trans, send_root)) {
4686 ret = btrfs_end_transaction(trans, send_root);
4687 trans = NULL;
4688 if (ret < 0)
4689 goto out;
4690 btrfs_release_path(path);
4691 goto join_trans;
4692 }
4693
4694 eb = path->nodes[0];
4695 slot = path->slots[0];
4696 btrfs_item_key_to_cpu(eb, &found_key, slot);
4697
4698 ret = changed_cb(send_root, NULL, path, NULL,
4699 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4700 if (ret < 0)
4701 goto out;
4702
4703 key.objectid = found_key.objectid;
4704 key.type = found_key.type;
4705 key.offset = found_key.offset + 1;
4706
4707 ret = btrfs_next_item(send_root, path);
4708 if (ret < 0)
4709 goto out;
4710 if (ret) {
4711 ret = 0;
4712 break;
4713 }
4714 }
4715
4716out_finish:
4717 ret = finish_inode_if_needed(sctx, 1);
4718
4719out:
4720 btrfs_free_path(path);
4721 if (trans) {
4722 if (!ret)
4723 ret = btrfs_end_transaction(trans, send_root);
4724 else
4725 btrfs_end_transaction(trans, send_root);
4726 }
4727 return ret;
4728}
4729
4730static int send_subvol(struct send_ctx *sctx)
4731{
4732 int ret;
4733
Stefan Behrensc2c71322013-04-10 17:10:52 +00004734 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
4735 ret = send_header(sctx);
4736 if (ret < 0)
4737 goto out;
4738 }
Alexander Block31db9f72012-07-25 23:19:24 +02004739
4740 ret = send_subvol_begin(sctx);
4741 if (ret < 0)
4742 goto out;
4743
4744 if (sctx->parent_root) {
4745 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4746 changed_cb, sctx);
4747 if (ret < 0)
4748 goto out;
4749 ret = finish_inode_if_needed(sctx, 1);
4750 if (ret < 0)
4751 goto out;
4752 } else {
4753 ret = full_send_tree(sctx);
4754 if (ret < 0)
4755 goto out;
4756 }
4757
4758out:
Alexander Block31db9f72012-07-25 23:19:24 +02004759 free_recorded_refs(sctx);
4760 return ret;
4761}
4762
4763long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4764{
4765 int ret = 0;
4766 struct btrfs_root *send_root;
4767 struct btrfs_root *clone_root;
4768 struct btrfs_fs_info *fs_info;
4769 struct btrfs_ioctl_send_args *arg = NULL;
4770 struct btrfs_key key;
Alexander Block31db9f72012-07-25 23:19:24 +02004771 struct send_ctx *sctx = NULL;
4772 u32 i;
4773 u64 *clone_sources_tmp = NULL;
4774
4775 if (!capable(CAP_SYS_ADMIN))
4776 return -EPERM;
4777
Al Viro496ad9a2013-01-23 17:07:38 -05004778 send_root = BTRFS_I(file_inode(mnt_file))->root;
Alexander Block31db9f72012-07-25 23:19:24 +02004779 fs_info = send_root->fs_info;
4780
Josef Bacik139f8072013-05-20 11:26:50 -04004781 /*
4782 * This is done when we lookup the root, it should already be complete
4783 * by the time we get here.
4784 */
4785 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
4786
4787 /*
4788 * If we just created this root we need to make sure that the orphan
4789 * cleanup has been done and committed since we search the commit root,
4790 * so check its commit root transid with our otransid and if they match
4791 * commit the transaction to make sure everything is updated.
4792 */
4793 down_read(&send_root->fs_info->extent_commit_sem);
4794 if (btrfs_header_generation(send_root->commit_root) ==
4795 btrfs_root_otransid(&send_root->root_item)) {
4796 struct btrfs_trans_handle *trans;
4797
4798 up_read(&send_root->fs_info->extent_commit_sem);
4799
4800 trans = btrfs_attach_transaction_barrier(send_root);
4801 if (IS_ERR(trans)) {
4802 if (PTR_ERR(trans) != -ENOENT) {
4803 ret = PTR_ERR(trans);
4804 goto out;
4805 }
4806 /* ENOENT means theres no transaction */
4807 } else {
4808 ret = btrfs_commit_transaction(trans, send_root);
4809 if (ret)
4810 goto out;
4811 }
4812 } else {
4813 up_read(&send_root->fs_info->extent_commit_sem);
4814 }
4815
Alexander Block31db9f72012-07-25 23:19:24 +02004816 arg = memdup_user(arg_, sizeof(*arg));
4817 if (IS_ERR(arg)) {
4818 ret = PTR_ERR(arg);
4819 arg = NULL;
4820 goto out;
4821 }
4822
4823 if (!access_ok(VERIFY_READ, arg->clone_sources,
Dan Carpenter700ff4f2013-01-10 03:57:25 -05004824 sizeof(*arg->clone_sources) *
4825 arg->clone_sources_count)) {
Alexander Block31db9f72012-07-25 23:19:24 +02004826 ret = -EFAULT;
4827 goto out;
4828 }
4829
Stefan Behrensc2c71322013-04-10 17:10:52 +00004830 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004831 ret = -EINVAL;
4832 goto out;
4833 }
4834
Alexander Block31db9f72012-07-25 23:19:24 +02004835 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4836 if (!sctx) {
4837 ret = -ENOMEM;
4838 goto out;
4839 }
4840
4841 INIT_LIST_HEAD(&sctx->new_refs);
4842 INIT_LIST_HEAD(&sctx->deleted_refs);
4843 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4844 INIT_LIST_HEAD(&sctx->name_cache_list);
4845
Mark Fashehcb95e7b2013-02-04 20:54:57 +00004846 sctx->flags = arg->flags;
4847
Alexander Block31db9f72012-07-25 23:19:24 +02004848 sctx->send_filp = fget(arg->send_fd);
Tsutomu Itohecc7ada2013-04-19 01:04:46 +00004849 if (!sctx->send_filp) {
4850 ret = -EBADF;
Alexander Block31db9f72012-07-25 23:19:24 +02004851 goto out;
4852 }
4853
4854 sctx->mnt = mnt_file->f_path.mnt;
4855
4856 sctx->send_root = send_root;
4857 sctx->clone_roots_cnt = arg->clone_sources_count;
4858
4859 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4860 sctx->send_buf = vmalloc(sctx->send_max_size);
4861 if (!sctx->send_buf) {
4862 ret = -ENOMEM;
4863 goto out;
4864 }
4865
4866 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4867 if (!sctx->read_buf) {
4868 ret = -ENOMEM;
4869 goto out;
4870 }
4871
4872 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4873 (arg->clone_sources_count + 1));
4874 if (!sctx->clone_roots) {
4875 ret = -ENOMEM;
4876 goto out;
4877 }
4878
4879 if (arg->clone_sources_count) {
4880 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4881 sizeof(*arg->clone_sources));
4882 if (!clone_sources_tmp) {
4883 ret = -ENOMEM;
4884 goto out;
4885 }
4886
4887 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4888 arg->clone_sources_count *
4889 sizeof(*arg->clone_sources));
4890 if (ret) {
4891 ret = -EFAULT;
4892 goto out;
4893 }
4894
4895 for (i = 0; i < arg->clone_sources_count; i++) {
4896 key.objectid = clone_sources_tmp[i];
4897 key.type = BTRFS_ROOT_ITEM_KEY;
4898 key.offset = (u64)-1;
4899 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
Alexander Block31db9f72012-07-25 23:19:24 +02004900 if (IS_ERR(clone_root)) {
4901 ret = PTR_ERR(clone_root);
4902 goto out;
4903 }
4904 sctx->clone_roots[i].root = clone_root;
4905 }
4906 vfree(clone_sources_tmp);
4907 clone_sources_tmp = NULL;
4908 }
4909
4910 if (arg->parent_root) {
4911 key.objectid = arg->parent_root;
4912 key.type = BTRFS_ROOT_ITEM_KEY;
4913 key.offset = (u64)-1;
4914 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
Stefan Behrensb1b19592013-05-13 14:42:57 +00004915 if (IS_ERR(sctx->parent_root)) {
4916 ret = PTR_ERR(sctx->parent_root);
Alexander Block31db9f72012-07-25 23:19:24 +02004917 goto out;
4918 }
4919 }
4920
4921 /*
4922 * Clones from send_root are allowed, but only if the clone source
4923 * is behind the current send position. This is checked while searching
4924 * for possible clone sources.
4925 */
4926 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4927
4928 /* We do a bsearch later */
4929 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4930 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4931 NULL);
4932
4933 ret = send_subvol(sctx);
4934 if (ret < 0)
4935 goto out;
4936
Stefan Behrensc2c71322013-04-10 17:10:52 +00004937 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
4938 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4939 if (ret < 0)
4940 goto out;
4941 ret = send_cmd(sctx);
4942 if (ret < 0)
4943 goto out;
4944 }
Alexander Block31db9f72012-07-25 23:19:24 +02004945
4946out:
Alexander Block31db9f72012-07-25 23:19:24 +02004947 kfree(arg);
4948 vfree(clone_sources_tmp);
4949
4950 if (sctx) {
4951 if (sctx->send_filp)
4952 fput(sctx->send_filp);
4953
4954 vfree(sctx->clone_roots);
4955 vfree(sctx->send_buf);
4956 vfree(sctx->read_buf);
4957
4958 name_cache_free(sctx);
4959
4960 kfree(sctx);
4961 }
4962
4963 return ret;
4964}