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