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