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