Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
31db9f7c AB |
2 | /* |
3 | * Copyright (C) 2012 Alexander Block. All rights reserved. | |
31db9f7c AB |
4 | */ |
5 | ||
6 | #include <linux/bsearch.h> | |
7 | #include <linux/fs.h> | |
8 | #include <linux/file.h> | |
9 | #include <linux/sort.h> | |
10 | #include <linux/mount.h> | |
11 | #include <linux/xattr.h> | |
12 | #include <linux/posix_acl_xattr.h> | |
5b8418b8 | 13 | #include <linux/radix-tree.h> |
a1857ebe | 14 | #include <linux/vmalloc.h> |
ed84885d | 15 | #include <linux/string.h> |
2351f431 | 16 | #include <linux/compat.h> |
9678c543 | 17 | #include <linux/crc32c.h> |
38622010 | 18 | #include <linux/fsverity.h> |
31db9f7c AB |
19 | |
20 | #include "send.h" | |
8234d3f6 | 21 | #include "ctree.h" |
31db9f7c AB |
22 | #include "backref.h" |
23 | #include "locking.h" | |
24 | #include "disk-io.h" | |
25 | #include "btrfs_inode.h" | |
26 | #include "transaction.h" | |
ebb8765b | 27 | #include "compression.h" |
89efda52 | 28 | #include "xattr.h" |
d96b3424 | 29 | #include "print-tree.h" |
07e81dc9 | 30 | #include "accessors.h" |
31db9f7c | 31 | |
fd0ddbe2 FM |
32 | /* |
33 | * Maximum number of references an extent can have in order for us to attempt to | |
34 | * issue clone operations instead of write operations. This currently exists to | |
35 | * avoid hitting limitations of the backreference walking code (taking a lot of | |
36 | * time and using too much memory for extents with large number of references). | |
37 | */ | |
38 | #define SEND_MAX_EXTENT_REFS 64 | |
39 | ||
31db9f7c AB |
40 | /* |
41 | * A fs_path is a helper to dynamically build path names with unknown size. | |
42 | * It reallocates the internal buffer on demand. | |
43 | * It allows fast adding of path elements on the right side (normal path) and | |
44 | * fast adding to the left side (reversed path). A reversed path can also be | |
45 | * unreversed if needed. | |
46 | */ | |
47 | struct fs_path { | |
48 | union { | |
49 | struct { | |
50 | char *start; | |
51 | char *end; | |
31db9f7c AB |
52 | |
53 | char *buf; | |
1f5a7ff9 DS |
54 | unsigned short buf_len:15; |
55 | unsigned short reversed:1; | |
31db9f7c AB |
56 | char inline_buf[]; |
57 | }; | |
ace01050 DS |
58 | /* |
59 | * Average path length does not exceed 200 bytes, we'll have | |
60 | * better packing in the slab and higher chance to satisfy | |
61 | * a allocation later during send. | |
62 | */ | |
63 | char pad[256]; | |
31db9f7c AB |
64 | }; |
65 | }; | |
66 | #define FS_PATH_INLINE_SIZE \ | |
67 | (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf)) | |
68 | ||
69 | ||
70 | /* reused for each extent */ | |
71 | struct clone_root { | |
72 | struct btrfs_root *root; | |
73 | u64 ino; | |
74 | u64 offset; | |
75 | ||
76 | u64 found_refs; | |
77 | }; | |
78 | ||
79 | #define SEND_CTX_MAX_NAME_CACHE_SIZE 128 | |
80 | #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2) | |
81 | ||
82 | struct send_ctx { | |
83 | struct file *send_filp; | |
84 | loff_t send_off; | |
85 | char *send_buf; | |
86 | u32 send_size; | |
87 | u32 send_max_size; | |
356bbbb6 OS |
88 | /* |
89 | * Whether BTRFS_SEND_A_DATA attribute was already added to current | |
90 | * command (since protocol v2, data must be the last attribute). | |
91 | */ | |
92 | bool put_data; | |
a4b333f2 | 93 | struct page **send_buf_pages; |
cb95e7bf | 94 | u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */ |
e77fbf99 DS |
95 | /* Protocol version compatibility requested */ |
96 | u32 proto; | |
31db9f7c | 97 | |
31db9f7c AB |
98 | struct btrfs_root *send_root; |
99 | struct btrfs_root *parent_root; | |
100 | struct clone_root *clone_roots; | |
101 | int clone_roots_cnt; | |
102 | ||
103 | /* current state of the compare_tree call */ | |
104 | struct btrfs_path *left_path; | |
105 | struct btrfs_path *right_path; | |
106 | struct btrfs_key *cmp_key; | |
107 | ||
d96b3424 FM |
108 | /* |
109 | * Keep track of the generation of the last transaction that was used | |
110 | * for relocating a block group. This is periodically checked in order | |
111 | * to detect if a relocation happened since the last check, so that we | |
112 | * don't operate on stale extent buffers for nodes (level >= 1) or on | |
113 | * stale disk_bytenr values of file extent items. | |
114 | */ | |
115 | u64 last_reloc_trans; | |
116 | ||
31db9f7c AB |
117 | /* |
118 | * infos of the currently processed inode. In case of deleted inodes, | |
119 | * these are the values from the deleted inode. | |
120 | */ | |
121 | u64 cur_ino; | |
122 | u64 cur_inode_gen; | |
31db9f7c AB |
123 | u64 cur_inode_size; |
124 | u64 cur_inode_mode; | |
644d1940 | 125 | u64 cur_inode_rdev; |
16e7549f | 126 | u64 cur_inode_last_extent; |
ffa7c429 | 127 | u64 cur_inode_next_write_offset; |
9555e1f1 DS |
128 | bool cur_inode_new; |
129 | bool cur_inode_new_gen; | |
130 | bool cur_inode_deleted; | |
46b2f459 | 131 | bool ignore_cur_inode; |
38622010 BB |
132 | bool cur_inode_needs_verity; |
133 | void *verity_descriptor; | |
31db9f7c AB |
134 | |
135 | u64 send_progress; | |
136 | ||
137 | struct list_head new_refs; | |
138 | struct list_head deleted_refs; | |
139 | ||
5b8418b8 | 140 | struct radix_tree_root name_cache; |
31db9f7c AB |
141 | struct list_head name_cache_list; |
142 | int name_cache_size; | |
143 | ||
521b6803 FM |
144 | /* |
145 | * The inode we are currently processing. It's not NULL only when we | |
146 | * need to issue write commands for data extents from this inode. | |
147 | */ | |
148 | struct inode *cur_inode; | |
2131bcd3 | 149 | struct file_ra_state ra; |
152555b3 FM |
150 | u64 page_cache_clear_start; |
151 | bool clean_page_cache; | |
2131bcd3 | 152 | |
9f03740a FDBM |
153 | /* |
154 | * We process inodes by their increasing order, so if before an | |
155 | * incremental send we reverse the parent/child relationship of | |
156 | * directories such that a directory with a lower inode number was | |
157 | * the parent of a directory with a higher inode number, and the one | |
158 | * becoming the new parent got renamed too, we can't rename/move the | |
159 | * directory with lower inode number when we finish processing it - we | |
160 | * must process the directory with higher inode number first, then | |
161 | * rename/move it and then rename/move the directory with lower inode | |
162 | * number. Example follows. | |
163 | * | |
164 | * Tree state when the first send was performed: | |
165 | * | |
166 | * . | |
167 | * |-- a (ino 257) | |
168 | * |-- b (ino 258) | |
169 | * | | |
170 | * | | |
171 | * |-- c (ino 259) | |
172 | * | |-- d (ino 260) | |
173 | * | | |
174 | * |-- c2 (ino 261) | |
175 | * | |
176 | * Tree state when the second (incremental) send is performed: | |
177 | * | |
178 | * . | |
179 | * |-- a (ino 257) | |
180 | * |-- b (ino 258) | |
181 | * |-- c2 (ino 261) | |
182 | * |-- d2 (ino 260) | |
183 | * |-- cc (ino 259) | |
184 | * | |
185 | * The sequence of steps that lead to the second state was: | |
186 | * | |
187 | * mv /a/b/c/d /a/b/c2/d2 | |
188 | * mv /a/b/c /a/b/c2/d2/cc | |
189 | * | |
190 | * "c" has lower inode number, but we can't move it (2nd mv operation) | |
191 | * before we move "d", which has higher inode number. | |
192 | * | |
193 | * So we just memorize which move/rename operations must be performed | |
194 | * later when their respective parent is processed and moved/renamed. | |
195 | */ | |
196 | ||
197 | /* Indexed by parent directory inode number. */ | |
198 | struct rb_root pending_dir_moves; | |
199 | ||
200 | /* | |
201 | * Reverse index, indexed by the inode number of a directory that | |
202 | * is waiting for the move/rename of its immediate parent before its | |
203 | * own move/rename can be performed. | |
204 | */ | |
205 | struct rb_root waiting_dir_moves; | |
9dc44214 FM |
206 | |
207 | /* | |
208 | * A directory that is going to be rm'ed might have a child directory | |
209 | * which is in the pending directory moves index above. In this case, | |
210 | * the directory can only be removed after the move/rename of its child | |
211 | * is performed. Example: | |
212 | * | |
213 | * Parent snapshot: | |
214 | * | |
215 | * . (ino 256) | |
216 | * |-- a/ (ino 257) | |
217 | * |-- b/ (ino 258) | |
218 | * |-- c/ (ino 259) | |
219 | * | |-- x/ (ino 260) | |
220 | * | | |
221 | * |-- y/ (ino 261) | |
222 | * | |
223 | * Send snapshot: | |
224 | * | |
225 | * . (ino 256) | |
226 | * |-- a/ (ino 257) | |
227 | * |-- b/ (ino 258) | |
228 | * |-- YY/ (ino 261) | |
229 | * |-- x/ (ino 260) | |
230 | * | |
231 | * Sequence of steps that lead to the send snapshot: | |
232 | * rm -f /a/b/c/foo.txt | |
233 | * mv /a/b/y /a/b/YY | |
234 | * mv /a/b/c/x /a/b/YY | |
235 | * rmdir /a/b/c | |
236 | * | |
237 | * When the child is processed, its move/rename is delayed until its | |
238 | * parent is processed (as explained above), but all other operations | |
239 | * like update utimes, chown, chgrp, etc, are performed and the paths | |
240 | * that it uses for those operations must use the orphanized name of | |
241 | * its parent (the directory we're going to rm later), so we need to | |
242 | * memorize that name. | |
243 | * | |
244 | * Indexed by the inode number of the directory to be deleted. | |
245 | */ | |
246 | struct rb_root orphan_dirs; | |
3aa5bd36 BC |
247 | |
248 | struct rb_root rbtree_new_refs; | |
249 | struct rb_root rbtree_deleted_refs; | |
9f03740a FDBM |
250 | }; |
251 | ||
252 | struct pending_dir_move { | |
253 | struct rb_node node; | |
254 | struct list_head list; | |
255 | u64 parent_ino; | |
256 | u64 ino; | |
257 | u64 gen; | |
258 | struct list_head update_refs; | |
259 | }; | |
260 | ||
261 | struct waiting_dir_move { | |
262 | struct rb_node node; | |
263 | u64 ino; | |
9dc44214 FM |
264 | /* |
265 | * There might be some directory that could not be removed because it | |
266 | * was waiting for this directory inode to be moved first. Therefore | |
267 | * after this directory is moved, we can try to rmdir the ino rmdir_ino. | |
268 | */ | |
269 | u64 rmdir_ino; | |
0b3f407e | 270 | u64 rmdir_gen; |
8b191a68 | 271 | bool orphanized; |
9dc44214 FM |
272 | }; |
273 | ||
274 | struct orphan_dir_info { | |
275 | struct rb_node node; | |
276 | u64 ino; | |
277 | u64 gen; | |
0f96f517 | 278 | u64 last_dir_index_offset; |
31db9f7c AB |
279 | }; |
280 | ||
281 | struct name_cache_entry { | |
282 | struct list_head list; | |
7e0926fe | 283 | /* |
5b8418b8 DS |
284 | * radix_tree has only 32bit entries but we need to handle 64bit inums. |
285 | * We use the lower 32bit of the 64bit inum to store it in the tree. If | |
286 | * more then one inum would fall into the same entry, we use radix_list | |
287 | * to store the additional entries. radix_list is also used to store | |
288 | * entries where two entries have the same inum but different | |
289 | * generations. | |
7e0926fe | 290 | */ |
5b8418b8 | 291 | struct list_head radix_list; |
31db9f7c AB |
292 | u64 ino; |
293 | u64 gen; | |
294 | u64 parent_ino; | |
295 | u64 parent_gen; | |
296 | int ret; | |
297 | int need_later_update; | |
298 | int name_len; | |
299 | char name[]; | |
300 | }; | |
301 | ||
18d0f5c6 DS |
302 | #define ADVANCE 1 |
303 | #define ADVANCE_ONLY_NEXT -1 | |
304 | ||
305 | enum btrfs_compare_tree_result { | |
306 | BTRFS_COMPARE_TREE_NEW, | |
307 | BTRFS_COMPARE_TREE_DELETED, | |
308 | BTRFS_COMPARE_TREE_CHANGED, | |
309 | BTRFS_COMPARE_TREE_SAME, | |
310 | }; | |
18d0f5c6 | 311 | |
e67c718b | 312 | __cold |
95155585 FM |
313 | static void inconsistent_snapshot_error(struct send_ctx *sctx, |
314 | enum btrfs_compare_tree_result result, | |
315 | const char *what) | |
316 | { | |
317 | const char *result_string; | |
318 | ||
319 | switch (result) { | |
320 | case BTRFS_COMPARE_TREE_NEW: | |
321 | result_string = "new"; | |
322 | break; | |
323 | case BTRFS_COMPARE_TREE_DELETED: | |
324 | result_string = "deleted"; | |
325 | break; | |
326 | case BTRFS_COMPARE_TREE_CHANGED: | |
327 | result_string = "updated"; | |
328 | break; | |
329 | case BTRFS_COMPARE_TREE_SAME: | |
330 | ASSERT(0); | |
331 | result_string = "unchanged"; | |
332 | break; | |
333 | default: | |
334 | ASSERT(0); | |
335 | result_string = "unexpected"; | |
336 | } | |
337 | ||
338 | btrfs_err(sctx->send_root->fs_info, | |
339 | "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu", | |
340 | result_string, what, sctx->cmp_key->objectid, | |
341 | sctx->send_root->root_key.objectid, | |
342 | (sctx->parent_root ? | |
343 | sctx->parent_root->root_key.objectid : 0)); | |
344 | } | |
345 | ||
e77fbf99 DS |
346 | __maybe_unused |
347 | static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd) | |
348 | { | |
349 | switch (sctx->proto) { | |
54cab6af OS |
350 | case 1: return cmd <= BTRFS_SEND_C_MAX_V1; |
351 | case 2: return cmd <= BTRFS_SEND_C_MAX_V2; | |
c86eab81 | 352 | case 3: return cmd <= BTRFS_SEND_C_MAX_V3; |
e77fbf99 DS |
353 | default: return false; |
354 | } | |
355 | } | |
356 | ||
9f03740a FDBM |
357 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino); |
358 | ||
9dc44214 FM |
359 | static struct waiting_dir_move * |
360 | get_waiting_dir_move(struct send_ctx *sctx, u64 ino); | |
361 | ||
0b3f407e | 362 | static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen); |
9dc44214 | 363 | |
16e7549f JB |
364 | static int need_send_hole(struct send_ctx *sctx) |
365 | { | |
366 | return (sctx->parent_root && !sctx->cur_inode_new && | |
367 | !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted && | |
368 | S_ISREG(sctx->cur_inode_mode)); | |
369 | } | |
370 | ||
31db9f7c AB |
371 | static void fs_path_reset(struct fs_path *p) |
372 | { | |
373 | if (p->reversed) { | |
374 | p->start = p->buf + p->buf_len - 1; | |
375 | p->end = p->start; | |
376 | *p->start = 0; | |
377 | } else { | |
378 | p->start = p->buf; | |
379 | p->end = p->start; | |
380 | *p->start = 0; | |
381 | } | |
382 | } | |
383 | ||
924794c9 | 384 | static struct fs_path *fs_path_alloc(void) |
31db9f7c AB |
385 | { |
386 | struct fs_path *p; | |
387 | ||
e780b0d1 | 388 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
31db9f7c AB |
389 | if (!p) |
390 | return NULL; | |
391 | p->reversed = 0; | |
31db9f7c AB |
392 | p->buf = p->inline_buf; |
393 | p->buf_len = FS_PATH_INLINE_SIZE; | |
394 | fs_path_reset(p); | |
395 | return p; | |
396 | } | |
397 | ||
924794c9 | 398 | static struct fs_path *fs_path_alloc_reversed(void) |
31db9f7c AB |
399 | { |
400 | struct fs_path *p; | |
401 | ||
924794c9 | 402 | p = fs_path_alloc(); |
31db9f7c AB |
403 | if (!p) |
404 | return NULL; | |
405 | p->reversed = 1; | |
406 | fs_path_reset(p); | |
407 | return p; | |
408 | } | |
409 | ||
924794c9 | 410 | static void fs_path_free(struct fs_path *p) |
31db9f7c AB |
411 | { |
412 | if (!p) | |
413 | return; | |
ace01050 DS |
414 | if (p->buf != p->inline_buf) |
415 | kfree(p->buf); | |
31db9f7c AB |
416 | kfree(p); |
417 | } | |
418 | ||
419 | static int fs_path_len(struct fs_path *p) | |
420 | { | |
421 | return p->end - p->start; | |
422 | } | |
423 | ||
424 | static int fs_path_ensure_buf(struct fs_path *p, int len) | |
425 | { | |
426 | char *tmp_buf; | |
427 | int path_len; | |
428 | int old_buf_len; | |
429 | ||
430 | len++; | |
431 | ||
432 | if (p->buf_len >= len) | |
433 | return 0; | |
434 | ||
cfd4a535 CM |
435 | if (len > PATH_MAX) { |
436 | WARN_ON(1); | |
437 | return -ENOMEM; | |
438 | } | |
439 | ||
1b2782c8 DS |
440 | path_len = p->end - p->start; |
441 | old_buf_len = p->buf_len; | |
442 | ||
ace01050 DS |
443 | /* |
444 | * First time the inline_buf does not suffice | |
445 | */ | |
01a9a8a9 | 446 | if (p->buf == p->inline_buf) { |
e780b0d1 | 447 | tmp_buf = kmalloc(len, GFP_KERNEL); |
01a9a8a9 FM |
448 | if (tmp_buf) |
449 | memcpy(tmp_buf, p->buf, old_buf_len); | |
450 | } else { | |
e780b0d1 | 451 | tmp_buf = krealloc(p->buf, len, GFP_KERNEL); |
01a9a8a9 | 452 | } |
9c9ca00b DS |
453 | if (!tmp_buf) |
454 | return -ENOMEM; | |
455 | p->buf = tmp_buf; | |
456 | /* | |
457 | * The real size of the buffer is bigger, this will let the fast path | |
458 | * happen most of the time | |
459 | */ | |
460 | p->buf_len = ksize(p->buf); | |
ace01050 | 461 | |
31db9f7c AB |
462 | if (p->reversed) { |
463 | tmp_buf = p->buf + old_buf_len - path_len - 1; | |
464 | p->end = p->buf + p->buf_len - 1; | |
465 | p->start = p->end - path_len; | |
466 | memmove(p->start, tmp_buf, path_len + 1); | |
467 | } else { | |
468 | p->start = p->buf; | |
469 | p->end = p->start + path_len; | |
470 | } | |
471 | return 0; | |
472 | } | |
473 | ||
b23ab57d DS |
474 | static int fs_path_prepare_for_add(struct fs_path *p, int name_len, |
475 | char **prepared) | |
31db9f7c AB |
476 | { |
477 | int ret; | |
478 | int new_len; | |
479 | ||
480 | new_len = p->end - p->start + name_len; | |
481 | if (p->start != p->end) | |
482 | new_len++; | |
483 | ret = fs_path_ensure_buf(p, new_len); | |
484 | if (ret < 0) | |
485 | goto out; | |
486 | ||
487 | if (p->reversed) { | |
488 | if (p->start != p->end) | |
489 | *--p->start = '/'; | |
490 | p->start -= name_len; | |
b23ab57d | 491 | *prepared = p->start; |
31db9f7c AB |
492 | } else { |
493 | if (p->start != p->end) | |
494 | *p->end++ = '/'; | |
b23ab57d | 495 | *prepared = p->end; |
31db9f7c AB |
496 | p->end += name_len; |
497 | *p->end = 0; | |
498 | } | |
499 | ||
500 | out: | |
501 | return ret; | |
502 | } | |
503 | ||
504 | static int fs_path_add(struct fs_path *p, const char *name, int name_len) | |
505 | { | |
506 | int ret; | |
b23ab57d | 507 | char *prepared; |
31db9f7c | 508 | |
b23ab57d | 509 | ret = fs_path_prepare_for_add(p, name_len, &prepared); |
31db9f7c AB |
510 | if (ret < 0) |
511 | goto out; | |
b23ab57d | 512 | memcpy(prepared, name, name_len); |
31db9f7c AB |
513 | |
514 | out: | |
515 | return ret; | |
516 | } | |
517 | ||
518 | static int fs_path_add_path(struct fs_path *p, struct fs_path *p2) | |
519 | { | |
520 | int ret; | |
b23ab57d | 521 | char *prepared; |
31db9f7c | 522 | |
b23ab57d | 523 | ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared); |
31db9f7c AB |
524 | if (ret < 0) |
525 | goto out; | |
b23ab57d | 526 | memcpy(prepared, p2->start, p2->end - p2->start); |
31db9f7c AB |
527 | |
528 | out: | |
529 | return ret; | |
530 | } | |
531 | ||
532 | static int fs_path_add_from_extent_buffer(struct fs_path *p, | |
533 | struct extent_buffer *eb, | |
534 | unsigned long off, int len) | |
535 | { | |
536 | int ret; | |
b23ab57d | 537 | char *prepared; |
31db9f7c | 538 | |
b23ab57d | 539 | ret = fs_path_prepare_for_add(p, len, &prepared); |
31db9f7c AB |
540 | if (ret < 0) |
541 | goto out; | |
542 | ||
b23ab57d | 543 | read_extent_buffer(eb, prepared, off, len); |
31db9f7c AB |
544 | |
545 | out: | |
546 | return ret; | |
547 | } | |
548 | ||
31db9f7c AB |
549 | static int fs_path_copy(struct fs_path *p, struct fs_path *from) |
550 | { | |
31db9f7c AB |
551 | p->reversed = from->reversed; |
552 | fs_path_reset(p); | |
553 | ||
0292ecf1 | 554 | return fs_path_add_path(p, from); |
31db9f7c AB |
555 | } |
556 | ||
31db9f7c AB |
557 | static void fs_path_unreverse(struct fs_path *p) |
558 | { | |
559 | char *tmp; | |
560 | int len; | |
561 | ||
562 | if (!p->reversed) | |
563 | return; | |
564 | ||
565 | tmp = p->start; | |
566 | len = p->end - p->start; | |
567 | p->start = p->buf; | |
568 | p->end = p->start + len; | |
569 | memmove(p->start, tmp, len + 1); | |
570 | p->reversed = 0; | |
571 | } | |
572 | ||
573 | static struct btrfs_path *alloc_path_for_send(void) | |
574 | { | |
575 | struct btrfs_path *path; | |
576 | ||
577 | path = btrfs_alloc_path(); | |
578 | if (!path) | |
579 | return NULL; | |
580 | path->search_commit_root = 1; | |
581 | path->skip_locking = 1; | |
3f8a18cc | 582 | path->need_commit_sem = 1; |
31db9f7c AB |
583 | return path; |
584 | } | |
585 | ||
48a3b636 | 586 | static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off) |
31db9f7c AB |
587 | { |
588 | int ret; | |
31db9f7c AB |
589 | u32 pos = 0; |
590 | ||
31db9f7c | 591 | while (pos < len) { |
8e93157b | 592 | ret = kernel_write(filp, buf + pos, len - pos, off); |
31db9f7c | 593 | if (ret < 0) |
8e93157b | 594 | return ret; |
cec3dad9 | 595 | if (ret == 0) |
8e93157b | 596 | return -EIO; |
31db9f7c AB |
597 | pos += ret; |
598 | } | |
599 | ||
8e93157b | 600 | return 0; |
31db9f7c AB |
601 | } |
602 | ||
603 | static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) | |
604 | { | |
605 | struct btrfs_tlv_header *hdr; | |
606 | int total_len = sizeof(*hdr) + len; | |
607 | int left = sctx->send_max_size - sctx->send_size; | |
608 | ||
356bbbb6 OS |
609 | if (WARN_ON_ONCE(sctx->put_data)) |
610 | return -EINVAL; | |
611 | ||
31db9f7c AB |
612 | if (unlikely(left < total_len)) |
613 | return -EOVERFLOW; | |
614 | ||
615 | hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); | |
e2f896b3 DS |
616 | put_unaligned_le16(attr, &hdr->tlv_type); |
617 | put_unaligned_le16(len, &hdr->tlv_len); | |
31db9f7c AB |
618 | memcpy(hdr + 1, data, len); |
619 | sctx->send_size += total_len; | |
620 | ||
621 | return 0; | |
622 | } | |
623 | ||
95bc79d5 DS |
624 | #define TLV_PUT_DEFINE_INT(bits) \ |
625 | static int tlv_put_u##bits(struct send_ctx *sctx, \ | |
626 | u##bits attr, u##bits value) \ | |
627 | { \ | |
628 | __le##bits __tmp = cpu_to_le##bits(value); \ | |
629 | return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \ | |
630 | } | |
31db9f7c | 631 | |
38622010 | 632 | TLV_PUT_DEFINE_INT(8) |
3ea4dc5b | 633 | TLV_PUT_DEFINE_INT(32) |
95bc79d5 | 634 | TLV_PUT_DEFINE_INT(64) |
31db9f7c AB |
635 | |
636 | static int tlv_put_string(struct send_ctx *sctx, u16 attr, | |
637 | const char *str, int len) | |
638 | { | |
639 | if (len == -1) | |
640 | len = strlen(str); | |
641 | return tlv_put(sctx, attr, str, len); | |
642 | } | |
643 | ||
644 | static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, | |
645 | const u8 *uuid) | |
646 | { | |
647 | return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); | |
648 | } | |
649 | ||
31db9f7c AB |
650 | static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, |
651 | struct extent_buffer *eb, | |
652 | struct btrfs_timespec *ts) | |
653 | { | |
654 | struct btrfs_timespec bts; | |
655 | read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts)); | |
656 | return tlv_put(sctx, attr, &bts, sizeof(bts)); | |
657 | } | |
658 | ||
659 | ||
895a72be | 660 | #define TLV_PUT(sctx, attrtype, data, attrlen) \ |
31db9f7c | 661 | do { \ |
895a72be | 662 | ret = tlv_put(sctx, attrtype, data, attrlen); \ |
31db9f7c AB |
663 | if (ret < 0) \ |
664 | goto tlv_put_failure; \ | |
665 | } while (0) | |
666 | ||
667 | #define TLV_PUT_INT(sctx, attrtype, bits, value) \ | |
668 | do { \ | |
669 | ret = tlv_put_u##bits(sctx, attrtype, value); \ | |
670 | if (ret < 0) \ | |
671 | goto tlv_put_failure; \ | |
672 | } while (0) | |
673 | ||
674 | #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) | |
675 | #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) | |
676 | #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) | |
677 | #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) | |
678 | #define TLV_PUT_STRING(sctx, attrtype, str, len) \ | |
679 | do { \ | |
680 | ret = tlv_put_string(sctx, attrtype, str, len); \ | |
681 | if (ret < 0) \ | |
682 | goto tlv_put_failure; \ | |
683 | } while (0) | |
684 | #define TLV_PUT_PATH(sctx, attrtype, p) \ | |
685 | do { \ | |
686 | ret = tlv_put_string(sctx, attrtype, p->start, \ | |
687 | p->end - p->start); \ | |
688 | if (ret < 0) \ | |
689 | goto tlv_put_failure; \ | |
690 | } while(0) | |
691 | #define TLV_PUT_UUID(sctx, attrtype, uuid) \ | |
692 | do { \ | |
693 | ret = tlv_put_uuid(sctx, attrtype, uuid); \ | |
694 | if (ret < 0) \ | |
695 | goto tlv_put_failure; \ | |
696 | } while (0) | |
31db9f7c AB |
697 | #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ |
698 | do { \ | |
699 | ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \ | |
700 | if (ret < 0) \ | |
701 | goto tlv_put_failure; \ | |
702 | } while (0) | |
703 | ||
704 | static int send_header(struct send_ctx *sctx) | |
705 | { | |
706 | struct btrfs_stream_header hdr; | |
707 | ||
708 | strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC); | |
d6815592 | 709 | hdr.version = cpu_to_le32(sctx->proto); |
1bcea355 AJ |
710 | return write_buf(sctx->send_filp, &hdr, sizeof(hdr), |
711 | &sctx->send_off); | |
31db9f7c AB |
712 | } |
713 | ||
714 | /* | |
715 | * For each command/item we want to send to userspace, we call this function. | |
716 | */ | |
717 | static int begin_cmd(struct send_ctx *sctx, int cmd) | |
718 | { | |
719 | struct btrfs_cmd_header *hdr; | |
720 | ||
fae7f21c | 721 | if (WARN_ON(!sctx->send_buf)) |
31db9f7c | 722 | return -EINVAL; |
31db9f7c AB |
723 | |
724 | BUG_ON(sctx->send_size); | |
725 | ||
726 | sctx->send_size += sizeof(*hdr); | |
727 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; | |
e2f896b3 | 728 | put_unaligned_le16(cmd, &hdr->cmd); |
31db9f7c AB |
729 | |
730 | return 0; | |
731 | } | |
732 | ||
733 | static int send_cmd(struct send_ctx *sctx) | |
734 | { | |
735 | int ret; | |
736 | struct btrfs_cmd_header *hdr; | |
737 | u32 crc; | |
738 | ||
739 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; | |
e2f896b3 DS |
740 | put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len); |
741 | put_unaligned_le32(0, &hdr->crc); | |
31db9f7c | 742 | |
65019df8 | 743 | crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); |
e2f896b3 | 744 | put_unaligned_le32(crc, &hdr->crc); |
31db9f7c | 745 | |
1bcea355 AJ |
746 | ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, |
747 | &sctx->send_off); | |
31db9f7c | 748 | |
31db9f7c | 749 | sctx->send_size = 0; |
356bbbb6 | 750 | sctx->put_data = false; |
31db9f7c AB |
751 | |
752 | return ret; | |
753 | } | |
754 | ||
755 | /* | |
756 | * Sends a move instruction to user space | |
757 | */ | |
758 | static int send_rename(struct send_ctx *sctx, | |
759 | struct fs_path *from, struct fs_path *to) | |
760 | { | |
04ab956e | 761 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
762 | int ret; |
763 | ||
04ab956e | 764 | btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start); |
31db9f7c AB |
765 | |
766 | ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); | |
767 | if (ret < 0) | |
768 | goto out; | |
769 | ||
770 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); | |
771 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); | |
772 | ||
773 | ret = send_cmd(sctx); | |
774 | ||
775 | tlv_put_failure: | |
776 | out: | |
777 | return ret; | |
778 | } | |
779 | ||
780 | /* | |
781 | * Sends a link instruction to user space | |
782 | */ | |
783 | static int send_link(struct send_ctx *sctx, | |
784 | struct fs_path *path, struct fs_path *lnk) | |
785 | { | |
04ab956e | 786 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
787 | int ret; |
788 | ||
04ab956e | 789 | btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start); |
31db9f7c AB |
790 | |
791 | ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); | |
792 | if (ret < 0) | |
793 | goto out; | |
794 | ||
795 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
796 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); | |
797 | ||
798 | ret = send_cmd(sctx); | |
799 | ||
800 | tlv_put_failure: | |
801 | out: | |
802 | return ret; | |
803 | } | |
804 | ||
805 | /* | |
806 | * Sends an unlink instruction to user space | |
807 | */ | |
808 | static int send_unlink(struct send_ctx *sctx, struct fs_path *path) | |
809 | { | |
04ab956e | 810 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
811 | int ret; |
812 | ||
04ab956e | 813 | btrfs_debug(fs_info, "send_unlink %s", path->start); |
31db9f7c AB |
814 | |
815 | ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); | |
816 | if (ret < 0) | |
817 | goto out; | |
818 | ||
819 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
820 | ||
821 | ret = send_cmd(sctx); | |
822 | ||
823 | tlv_put_failure: | |
824 | out: | |
825 | return ret; | |
826 | } | |
827 | ||
828 | /* | |
829 | * Sends a rmdir instruction to user space | |
830 | */ | |
831 | static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) | |
832 | { | |
04ab956e | 833 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
834 | int ret; |
835 | ||
04ab956e | 836 | btrfs_debug(fs_info, "send_rmdir %s", path->start); |
31db9f7c AB |
837 | |
838 | ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); | |
839 | if (ret < 0) | |
840 | goto out; | |
841 | ||
842 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
843 | ||
844 | ret = send_cmd(sctx); | |
845 | ||
846 | tlv_put_failure: | |
847 | out: | |
848 | return ret; | |
849 | } | |
850 | ||
7e93f6dc BC |
851 | struct btrfs_inode_info { |
852 | u64 size; | |
853 | u64 gen; | |
854 | u64 mode; | |
855 | u64 uid; | |
856 | u64 gid; | |
857 | u64 rdev; | |
858 | u64 fileattr; | |
9ed0a72e | 859 | u64 nlink; |
7e93f6dc BC |
860 | }; |
861 | ||
31db9f7c AB |
862 | /* |
863 | * Helper function to retrieve some fields from an inode item. | |
864 | */ | |
7e93f6dc BC |
865 | static int get_inode_info(struct btrfs_root *root, u64 ino, |
866 | struct btrfs_inode_info *info) | |
31db9f7c AB |
867 | { |
868 | int ret; | |
7e93f6dc | 869 | struct btrfs_path *path; |
31db9f7c AB |
870 | struct btrfs_inode_item *ii; |
871 | struct btrfs_key key; | |
31db9f7c | 872 | |
7e93f6dc BC |
873 | path = alloc_path_for_send(); |
874 | if (!path) | |
875 | return -ENOMEM; | |
876 | ||
31db9f7c AB |
877 | key.objectid = ino; |
878 | key.type = BTRFS_INODE_ITEM_KEY; | |
879 | key.offset = 0; | |
880 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
31db9f7c | 881 | if (ret) { |
3f8a18cc JB |
882 | if (ret > 0) |
883 | ret = -ENOENT; | |
7e93f6dc | 884 | goto out; |
31db9f7c AB |
885 | } |
886 | ||
7e93f6dc BC |
887 | if (!info) |
888 | goto out; | |
889 | ||
31db9f7c AB |
890 | ii = btrfs_item_ptr(path->nodes[0], path->slots[0], |
891 | struct btrfs_inode_item); | |
7e93f6dc BC |
892 | info->size = btrfs_inode_size(path->nodes[0], ii); |
893 | info->gen = btrfs_inode_generation(path->nodes[0], ii); | |
894 | info->mode = btrfs_inode_mode(path->nodes[0], ii); | |
895 | info->uid = btrfs_inode_uid(path->nodes[0], ii); | |
896 | info->gid = btrfs_inode_gid(path->nodes[0], ii); | |
897 | info->rdev = btrfs_inode_rdev(path->nodes[0], ii); | |
9ed0a72e | 898 | info->nlink = btrfs_inode_nlink(path->nodes[0], ii); |
48247359 DS |
899 | /* |
900 | * Transfer the unchanged u64 value of btrfs_inode_item::flags, that's | |
901 | * otherwise logically split to 32/32 parts. | |
902 | */ | |
7e93f6dc | 903 | info->fileattr = btrfs_inode_flags(path->nodes[0], ii); |
31db9f7c | 904 | |
7e93f6dc BC |
905 | out: |
906 | btrfs_free_path(path); | |
3f8a18cc JB |
907 | return ret; |
908 | } | |
909 | ||
7e93f6dc | 910 | static int get_inode_gen(struct btrfs_root *root, u64 ino, u64 *gen) |
3f8a18cc | 911 | { |
3f8a18cc | 912 | int ret; |
7e93f6dc | 913 | struct btrfs_inode_info info; |
3f8a18cc | 914 | |
7e93f6dc BC |
915 | if (!gen) |
916 | return -EPERM; | |
917 | ||
918 | ret = get_inode_info(root, ino, &info); | |
919 | if (!ret) | |
920 | *gen = info.gen; | |
31db9f7c AB |
921 | return ret; |
922 | } | |
923 | ||
924 | typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index, | |
925 | struct fs_path *p, | |
926 | void *ctx); | |
927 | ||
928 | /* | |
96b5bd77 JS |
929 | * Helper function to iterate the entries in ONE btrfs_inode_ref or |
930 | * btrfs_inode_extref. | |
31db9f7c AB |
931 | * The iterate callback may return a non zero value to stop iteration. This can |
932 | * be a negative value for error codes or 1 to simply stop it. | |
933 | * | |
96b5bd77 | 934 | * path must point to the INODE_REF or INODE_EXTREF when called. |
31db9f7c | 935 | */ |
924794c9 | 936 | static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path, |
31db9f7c AB |
937 | struct btrfs_key *found_key, int resolve, |
938 | iterate_inode_ref_t iterate, void *ctx) | |
939 | { | |
96b5bd77 | 940 | struct extent_buffer *eb = path->nodes[0]; |
31db9f7c | 941 | struct btrfs_inode_ref *iref; |
96b5bd77 | 942 | struct btrfs_inode_extref *extref; |
31db9f7c AB |
943 | struct btrfs_path *tmp_path; |
944 | struct fs_path *p; | |
96b5bd77 | 945 | u32 cur = 0; |
31db9f7c | 946 | u32 total; |
96b5bd77 | 947 | int slot = path->slots[0]; |
31db9f7c AB |
948 | u32 name_len; |
949 | char *start; | |
950 | int ret = 0; | |
96b5bd77 | 951 | int num = 0; |
31db9f7c | 952 | int index; |
96b5bd77 JS |
953 | u64 dir; |
954 | unsigned long name_off; | |
955 | unsigned long elem_size; | |
956 | unsigned long ptr; | |
31db9f7c | 957 | |
924794c9 | 958 | p = fs_path_alloc_reversed(); |
31db9f7c AB |
959 | if (!p) |
960 | return -ENOMEM; | |
961 | ||
962 | tmp_path = alloc_path_for_send(); | |
963 | if (!tmp_path) { | |
924794c9 | 964 | fs_path_free(p); |
31db9f7c AB |
965 | return -ENOMEM; |
966 | } | |
967 | ||
31db9f7c | 968 | |
96b5bd77 JS |
969 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
970 | ptr = (unsigned long)btrfs_item_ptr(eb, slot, | |
971 | struct btrfs_inode_ref); | |
3212fa14 | 972 | total = btrfs_item_size(eb, slot); |
96b5bd77 JS |
973 | elem_size = sizeof(*iref); |
974 | } else { | |
975 | ptr = btrfs_item_ptr_offset(eb, slot); | |
3212fa14 | 976 | total = btrfs_item_size(eb, slot); |
96b5bd77 JS |
977 | elem_size = sizeof(*extref); |
978 | } | |
979 | ||
31db9f7c AB |
980 | while (cur < total) { |
981 | fs_path_reset(p); | |
982 | ||
96b5bd77 JS |
983 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
984 | iref = (struct btrfs_inode_ref *)(ptr + cur); | |
985 | name_len = btrfs_inode_ref_name_len(eb, iref); | |
986 | name_off = (unsigned long)(iref + 1); | |
987 | index = btrfs_inode_ref_index(eb, iref); | |
988 | dir = found_key->offset; | |
989 | } else { | |
990 | extref = (struct btrfs_inode_extref *)(ptr + cur); | |
991 | name_len = btrfs_inode_extref_name_len(eb, extref); | |
992 | name_off = (unsigned long)&extref->name; | |
993 | index = btrfs_inode_extref_index(eb, extref); | |
994 | dir = btrfs_inode_extref_parent(eb, extref); | |
995 | } | |
996 | ||
31db9f7c | 997 | if (resolve) { |
96b5bd77 JS |
998 | start = btrfs_ref_to_path(root, tmp_path, name_len, |
999 | name_off, eb, dir, | |
1000 | p->buf, p->buf_len); | |
31db9f7c AB |
1001 | if (IS_ERR(start)) { |
1002 | ret = PTR_ERR(start); | |
1003 | goto out; | |
1004 | } | |
1005 | if (start < p->buf) { | |
1006 | /* overflow , try again with larger buffer */ | |
1007 | ret = fs_path_ensure_buf(p, | |
1008 | p->buf_len + p->buf - start); | |
1009 | if (ret < 0) | |
1010 | goto out; | |
96b5bd77 JS |
1011 | start = btrfs_ref_to_path(root, tmp_path, |
1012 | name_len, name_off, | |
1013 | eb, dir, | |
1014 | p->buf, p->buf_len); | |
31db9f7c AB |
1015 | if (IS_ERR(start)) { |
1016 | ret = PTR_ERR(start); | |
1017 | goto out; | |
1018 | } | |
1019 | BUG_ON(start < p->buf); | |
1020 | } | |
1021 | p->start = start; | |
1022 | } else { | |
96b5bd77 JS |
1023 | ret = fs_path_add_from_extent_buffer(p, eb, name_off, |
1024 | name_len); | |
31db9f7c AB |
1025 | if (ret < 0) |
1026 | goto out; | |
1027 | } | |
1028 | ||
96b5bd77 JS |
1029 | cur += elem_size + name_len; |
1030 | ret = iterate(num, dir, index, p, ctx); | |
31db9f7c AB |
1031 | if (ret) |
1032 | goto out; | |
31db9f7c AB |
1033 | num++; |
1034 | } | |
1035 | ||
1036 | out: | |
1037 | btrfs_free_path(tmp_path); | |
924794c9 | 1038 | fs_path_free(p); |
31db9f7c AB |
1039 | return ret; |
1040 | } | |
1041 | ||
1042 | typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key, | |
1043 | const char *name, int name_len, | |
1044 | const char *data, int data_len, | |
b1dea4e7 | 1045 | void *ctx); |
31db9f7c AB |
1046 | |
1047 | /* | |
1048 | * Helper function to iterate the entries in ONE btrfs_dir_item. | |
1049 | * The iterate callback may return a non zero value to stop iteration. This can | |
1050 | * be a negative value for error codes or 1 to simply stop it. | |
1051 | * | |
1052 | * path must point to the dir item when called. | |
1053 | */ | |
924794c9 | 1054 | static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path, |
31db9f7c AB |
1055 | iterate_dir_item_t iterate, void *ctx) |
1056 | { | |
1057 | int ret = 0; | |
1058 | struct extent_buffer *eb; | |
31db9f7c | 1059 | struct btrfs_dir_item *di; |
31db9f7c AB |
1060 | struct btrfs_key di_key; |
1061 | char *buf = NULL; | |
7e3ae33e | 1062 | int buf_len; |
31db9f7c AB |
1063 | u32 name_len; |
1064 | u32 data_len; | |
1065 | u32 cur; | |
1066 | u32 len; | |
1067 | u32 total; | |
1068 | int slot; | |
1069 | int num; | |
31db9f7c | 1070 | |
4395e0c4 FM |
1071 | /* |
1072 | * Start with a small buffer (1 page). If later we end up needing more | |
1073 | * space, which can happen for xattrs on a fs with a leaf size greater | |
1074 | * then the page size, attempt to increase the buffer. Typically xattr | |
1075 | * values are small. | |
1076 | */ | |
1077 | buf_len = PATH_MAX; | |
e780b0d1 | 1078 | buf = kmalloc(buf_len, GFP_KERNEL); |
31db9f7c AB |
1079 | if (!buf) { |
1080 | ret = -ENOMEM; | |
1081 | goto out; | |
1082 | } | |
1083 | ||
31db9f7c AB |
1084 | eb = path->nodes[0]; |
1085 | slot = path->slots[0]; | |
31db9f7c AB |
1086 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
1087 | cur = 0; | |
1088 | len = 0; | |
3212fa14 | 1089 | total = btrfs_item_size(eb, slot); |
31db9f7c AB |
1090 | |
1091 | num = 0; | |
1092 | while (cur < total) { | |
1093 | name_len = btrfs_dir_name_len(eb, di); | |
1094 | data_len = btrfs_dir_data_len(eb, di); | |
31db9f7c AB |
1095 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
1096 | ||
b1dea4e7 | 1097 | if (btrfs_dir_type(eb, di) == BTRFS_FT_XATTR) { |
7e3ae33e FM |
1098 | if (name_len > XATTR_NAME_MAX) { |
1099 | ret = -ENAMETOOLONG; | |
1100 | goto out; | |
1101 | } | |
da17066c JM |
1102 | if (name_len + data_len > |
1103 | BTRFS_MAX_XATTR_SIZE(root->fs_info)) { | |
7e3ae33e FM |
1104 | ret = -E2BIG; |
1105 | goto out; | |
1106 | } | |
1107 | } else { | |
1108 | /* | |
1109 | * Path too long | |
1110 | */ | |
4395e0c4 | 1111 | if (name_len + data_len > PATH_MAX) { |
7e3ae33e FM |
1112 | ret = -ENAMETOOLONG; |
1113 | goto out; | |
1114 | } | |
31db9f7c AB |
1115 | } |
1116 | ||
4395e0c4 FM |
1117 | if (name_len + data_len > buf_len) { |
1118 | buf_len = name_len + data_len; | |
1119 | if (is_vmalloc_addr(buf)) { | |
1120 | vfree(buf); | |
1121 | buf = NULL; | |
1122 | } else { | |
1123 | char *tmp = krealloc(buf, buf_len, | |
e780b0d1 | 1124 | GFP_KERNEL | __GFP_NOWARN); |
4395e0c4 FM |
1125 | |
1126 | if (!tmp) | |
1127 | kfree(buf); | |
1128 | buf = tmp; | |
1129 | } | |
1130 | if (!buf) { | |
f11f7441 | 1131 | buf = kvmalloc(buf_len, GFP_KERNEL); |
4395e0c4 FM |
1132 | if (!buf) { |
1133 | ret = -ENOMEM; | |
1134 | goto out; | |
1135 | } | |
1136 | } | |
1137 | } | |
1138 | ||
31db9f7c AB |
1139 | read_extent_buffer(eb, buf, (unsigned long)(di + 1), |
1140 | name_len + data_len); | |
1141 | ||
1142 | len = sizeof(*di) + name_len + data_len; | |
1143 | di = (struct btrfs_dir_item *)((char *)di + len); | |
1144 | cur += len; | |
1145 | ||
1146 | ret = iterate(num, &di_key, buf, name_len, buf + name_len, | |
b1dea4e7 | 1147 | data_len, ctx); |
31db9f7c AB |
1148 | if (ret < 0) |
1149 | goto out; | |
1150 | if (ret) { | |
1151 | ret = 0; | |
1152 | goto out; | |
1153 | } | |
1154 | ||
1155 | num++; | |
1156 | } | |
1157 | ||
1158 | out: | |
4395e0c4 | 1159 | kvfree(buf); |
31db9f7c AB |
1160 | return ret; |
1161 | } | |
1162 | ||
1163 | static int __copy_first_ref(int num, u64 dir, int index, | |
1164 | struct fs_path *p, void *ctx) | |
1165 | { | |
1166 | int ret; | |
1167 | struct fs_path *pt = ctx; | |
1168 | ||
1169 | ret = fs_path_copy(pt, p); | |
1170 | if (ret < 0) | |
1171 | return ret; | |
1172 | ||
1173 | /* we want the first only */ | |
1174 | return 1; | |
1175 | } | |
1176 | ||
1177 | /* | |
1178 | * Retrieve the first path of an inode. If an inode has more then one | |
1179 | * ref/hardlink, this is ignored. | |
1180 | */ | |
924794c9 | 1181 | static int get_inode_path(struct btrfs_root *root, |
31db9f7c AB |
1182 | u64 ino, struct fs_path *path) |
1183 | { | |
1184 | int ret; | |
1185 | struct btrfs_key key, found_key; | |
1186 | struct btrfs_path *p; | |
1187 | ||
1188 | p = alloc_path_for_send(); | |
1189 | if (!p) | |
1190 | return -ENOMEM; | |
1191 | ||
1192 | fs_path_reset(path); | |
1193 | ||
1194 | key.objectid = ino; | |
1195 | key.type = BTRFS_INODE_REF_KEY; | |
1196 | key.offset = 0; | |
1197 | ||
1198 | ret = btrfs_search_slot_for_read(root, &key, p, 1, 0); | |
1199 | if (ret < 0) | |
1200 | goto out; | |
1201 | if (ret) { | |
1202 | ret = 1; | |
1203 | goto out; | |
1204 | } | |
1205 | btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]); | |
1206 | if (found_key.objectid != ino || | |
96b5bd77 JS |
1207 | (found_key.type != BTRFS_INODE_REF_KEY && |
1208 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { | |
31db9f7c AB |
1209 | ret = -ENOENT; |
1210 | goto out; | |
1211 | } | |
1212 | ||
924794c9 TI |
1213 | ret = iterate_inode_ref(root, p, &found_key, 1, |
1214 | __copy_first_ref, path); | |
31db9f7c AB |
1215 | if (ret < 0) |
1216 | goto out; | |
1217 | ret = 0; | |
1218 | ||
1219 | out: | |
1220 | btrfs_free_path(p); | |
1221 | return ret; | |
1222 | } | |
1223 | ||
1224 | struct backref_ctx { | |
1225 | struct send_ctx *sctx; | |
1226 | ||
1227 | /* number of total found references */ | |
1228 | u64 found; | |
1229 | ||
1230 | /* | |
1231 | * used for clones found in send_root. clones found behind cur_objectid | |
1232 | * and cur_offset are not considered as allowed clones. | |
1233 | */ | |
1234 | u64 cur_objectid; | |
1235 | u64 cur_offset; | |
1236 | ||
1237 | /* may be truncated in case it's the last extent in a file */ | |
1238 | u64 extent_len; | |
1239 | ||
1240 | /* Just to check for bugs in backref resolving */ | |
ee849c04 | 1241 | int found_itself; |
31db9f7c AB |
1242 | }; |
1243 | ||
1244 | static int __clone_root_cmp_bsearch(const void *key, const void *elt) | |
1245 | { | |
995e01b7 | 1246 | u64 root = (u64)(uintptr_t)key; |
214cc184 | 1247 | const struct clone_root *cr = elt; |
31db9f7c | 1248 | |
4fd786e6 | 1249 | if (root < cr->root->root_key.objectid) |
31db9f7c | 1250 | return -1; |
4fd786e6 | 1251 | if (root > cr->root->root_key.objectid) |
31db9f7c AB |
1252 | return 1; |
1253 | return 0; | |
1254 | } | |
1255 | ||
1256 | static int __clone_root_cmp_sort(const void *e1, const void *e2) | |
1257 | { | |
214cc184 DS |
1258 | const struct clone_root *cr1 = e1; |
1259 | const struct clone_root *cr2 = e2; | |
31db9f7c | 1260 | |
4fd786e6 | 1261 | if (cr1->root->root_key.objectid < cr2->root->root_key.objectid) |
31db9f7c | 1262 | return -1; |
4fd786e6 | 1263 | if (cr1->root->root_key.objectid > cr2->root->root_key.objectid) |
31db9f7c AB |
1264 | return 1; |
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | /* | |
1269 | * Called for every backref that is found for the current extent. | |
766702ef | 1270 | * Results are collected in sctx->clone_roots->ino/offset/found_refs |
31db9f7c AB |
1271 | */ |
1272 | static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_) | |
1273 | { | |
1274 | struct backref_ctx *bctx = ctx_; | |
1275 | struct clone_root *found; | |
31db9f7c AB |
1276 | |
1277 | /* First check if the root is in the list of accepted clone sources */ | |
995e01b7 | 1278 | found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, |
31db9f7c AB |
1279 | bctx->sctx->clone_roots_cnt, |
1280 | sizeof(struct clone_root), | |
1281 | __clone_root_cmp_bsearch); | |
1282 | if (!found) | |
1283 | return 0; | |
1284 | ||
1285 | if (found->root == bctx->sctx->send_root && | |
1286 | ino == bctx->cur_objectid && | |
1287 | offset == bctx->cur_offset) { | |
ee849c04 | 1288 | bctx->found_itself = 1; |
31db9f7c AB |
1289 | } |
1290 | ||
31db9f7c AB |
1291 | /* |
1292 | * Make sure we don't consider clones from send_root that are | |
1293 | * behind the current inode/offset. | |
1294 | */ | |
1295 | if (found->root == bctx->sctx->send_root) { | |
1296 | /* | |
11f2069c FM |
1297 | * If the source inode was not yet processed we can't issue a |
1298 | * clone operation, as the source extent does not exist yet at | |
1299 | * the destination of the stream. | |
31db9f7c | 1300 | */ |
11f2069c FM |
1301 | if (ino > bctx->cur_objectid) |
1302 | return 0; | |
1303 | /* | |
1304 | * We clone from the inode currently being sent as long as the | |
1305 | * source extent is already processed, otherwise we could try | |
1306 | * to clone from an extent that does not exist yet at the | |
1307 | * destination of the stream. | |
1308 | */ | |
1309 | if (ino == bctx->cur_objectid && | |
9722b101 FM |
1310 | offset + bctx->extent_len > |
1311 | bctx->sctx->cur_inode_next_write_offset) | |
31db9f7c | 1312 | return 0; |
31db9f7c AB |
1313 | } |
1314 | ||
1315 | bctx->found++; | |
1316 | found->found_refs++; | |
1317 | if (ino < found->ino) { | |
1318 | found->ino = ino; | |
1319 | found->offset = offset; | |
1320 | } else if (found->ino == ino) { | |
1321 | /* | |
1322 | * same extent found more then once in the same file. | |
1323 | */ | |
1324 | if (found->offset > offset + bctx->extent_len) | |
1325 | found->offset = offset; | |
1326 | } | |
1327 | ||
1328 | return 0; | |
1329 | } | |
1330 | ||
1331 | /* | |
766702ef AB |
1332 | * Given an inode, offset and extent item, it finds a good clone for a clone |
1333 | * instruction. Returns -ENOENT when none could be found. The function makes | |
1334 | * sure that the returned clone is usable at the point where sending is at the | |
1335 | * moment. This means, that no clones are accepted which lie behind the current | |
1336 | * inode+offset. | |
1337 | * | |
31db9f7c AB |
1338 | * path must point to the extent item when called. |
1339 | */ | |
1340 | static int find_extent_clone(struct send_ctx *sctx, | |
1341 | struct btrfs_path *path, | |
1342 | u64 ino, u64 data_offset, | |
1343 | u64 ino_size, | |
1344 | struct clone_root **found) | |
1345 | { | |
04ab956e | 1346 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
1347 | int ret; |
1348 | int extent_type; | |
1349 | u64 logical; | |
74dd17fb | 1350 | u64 disk_byte; |
31db9f7c AB |
1351 | u64 num_bytes; |
1352 | u64 extent_item_pos; | |
69917e43 | 1353 | u64 flags = 0; |
31db9f7c AB |
1354 | struct btrfs_file_extent_item *fi; |
1355 | struct extent_buffer *eb = path->nodes[0]; | |
dce28150 | 1356 | struct backref_ctx backref_ctx = {0}; |
31db9f7c AB |
1357 | struct clone_root *cur_clone_root; |
1358 | struct btrfs_key found_key; | |
1359 | struct btrfs_path *tmp_path; | |
fd0ddbe2 | 1360 | struct btrfs_extent_item *ei; |
74dd17fb | 1361 | int compressed; |
31db9f7c AB |
1362 | u32 i; |
1363 | ||
1364 | tmp_path = alloc_path_for_send(); | |
1365 | if (!tmp_path) | |
1366 | return -ENOMEM; | |
1367 | ||
3f8a18cc JB |
1368 | /* We only use this path under the commit sem */ |
1369 | tmp_path->need_commit_sem = 0; | |
1370 | ||
31db9f7c AB |
1371 | if (data_offset >= ino_size) { |
1372 | /* | |
1373 | * There may be extents that lie behind the file's size. | |
1374 | * I at least had this in combination with snapshotting while | |
1375 | * writing large files. | |
1376 | */ | |
1377 | ret = 0; | |
1378 | goto out; | |
1379 | } | |
1380 | ||
1381 | fi = btrfs_item_ptr(eb, path->slots[0], | |
1382 | struct btrfs_file_extent_item); | |
1383 | extent_type = btrfs_file_extent_type(eb, fi); | |
1384 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1385 | ret = -ENOENT; | |
1386 | goto out; | |
1387 | } | |
74dd17fb | 1388 | compressed = btrfs_file_extent_compression(eb, fi); |
31db9f7c AB |
1389 | |
1390 | num_bytes = btrfs_file_extent_num_bytes(eb, fi); | |
74dd17fb CM |
1391 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
1392 | if (disk_byte == 0) { | |
31db9f7c AB |
1393 | ret = -ENOENT; |
1394 | goto out; | |
1395 | } | |
74dd17fb | 1396 | logical = disk_byte + btrfs_file_extent_offset(eb, fi); |
31db9f7c | 1397 | |
04ab956e JM |
1398 | down_read(&fs_info->commit_root_sem); |
1399 | ret = extent_from_logical(fs_info, disk_byte, tmp_path, | |
69917e43 | 1400 | &found_key, &flags); |
04ab956e | 1401 | up_read(&fs_info->commit_root_sem); |
31db9f7c AB |
1402 | |
1403 | if (ret < 0) | |
1404 | goto out; | |
69917e43 | 1405 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
31db9f7c AB |
1406 | ret = -EIO; |
1407 | goto out; | |
1408 | } | |
1409 | ||
fd0ddbe2 FM |
1410 | ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0], |
1411 | struct btrfs_extent_item); | |
1412 | /* | |
1413 | * Backreference walking (iterate_extent_inodes() below) is currently | |
1414 | * too expensive when an extent has a large number of references, both | |
1415 | * in time spent and used memory. So for now just fallback to write | |
1416 | * operations instead of clone operations when an extent has more than | |
1417 | * a certain amount of references. | |
1418 | */ | |
1419 | if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) { | |
1420 | ret = -ENOENT; | |
1421 | goto out; | |
1422 | } | |
1423 | btrfs_release_path(tmp_path); | |
1424 | ||
31db9f7c AB |
1425 | /* |
1426 | * Setup the clone roots. | |
1427 | */ | |
1428 | for (i = 0; i < sctx->clone_roots_cnt; i++) { | |
1429 | cur_clone_root = sctx->clone_roots + i; | |
1430 | cur_clone_root->ino = (u64)-1; | |
1431 | cur_clone_root->offset = 0; | |
1432 | cur_clone_root->found_refs = 0; | |
1433 | } | |
1434 | ||
dce28150 GR |
1435 | backref_ctx.sctx = sctx; |
1436 | backref_ctx.found = 0; | |
1437 | backref_ctx.cur_objectid = ino; | |
1438 | backref_ctx.cur_offset = data_offset; | |
1439 | backref_ctx.found_itself = 0; | |
1440 | backref_ctx.extent_len = num_bytes; | |
31db9f7c AB |
1441 | |
1442 | /* | |
1443 | * The last extent of a file may be too large due to page alignment. | |
1444 | * We need to adjust extent_len in this case so that the checks in | |
1445 | * __iterate_backrefs work. | |
1446 | */ | |
1447 | if (data_offset + num_bytes >= ino_size) | |
dce28150 | 1448 | backref_ctx.extent_len = ino_size - data_offset; |
31db9f7c AB |
1449 | |
1450 | /* | |
1451 | * Now collect all backrefs. | |
1452 | */ | |
74dd17fb CM |
1453 | if (compressed == BTRFS_COMPRESS_NONE) |
1454 | extent_item_pos = logical - found_key.objectid; | |
1455 | else | |
1456 | extent_item_pos = 0; | |
0b246afa JM |
1457 | ret = iterate_extent_inodes(fs_info, found_key.objectid, |
1458 | extent_item_pos, 1, __iterate_backrefs, | |
dce28150 | 1459 | &backref_ctx, false); |
74dd17fb | 1460 | |
31db9f7c AB |
1461 | if (ret < 0) |
1462 | goto out; | |
1463 | ||
d96b3424 FM |
1464 | down_read(&fs_info->commit_root_sem); |
1465 | if (fs_info->last_reloc_trans > sctx->last_reloc_trans) { | |
1466 | /* | |
1467 | * A transaction commit for a transaction in which block group | |
1468 | * relocation was done just happened. | |
1469 | * The disk_bytenr of the file extent item we processed is | |
1470 | * possibly stale, referring to the extent's location before | |
1471 | * relocation. So act as if we haven't found any clone sources | |
1472 | * and fallback to write commands, which will read the correct | |
1473 | * data from the new extent location. Otherwise we will fail | |
1474 | * below because we haven't found our own back reference or we | |
1475 | * could be getting incorrect sources in case the old extent | |
1476 | * was already reallocated after the relocation. | |
1477 | */ | |
1478 | up_read(&fs_info->commit_root_sem); | |
1479 | ret = -ENOENT; | |
1480 | goto out; | |
1481 | } | |
1482 | up_read(&fs_info->commit_root_sem); | |
1483 | ||
dce28150 | 1484 | if (!backref_ctx.found_itself) { |
31db9f7c AB |
1485 | /* found a bug in backref code? */ |
1486 | ret = -EIO; | |
04ab956e | 1487 | btrfs_err(fs_info, |
5d163e0e | 1488 | "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu", |
04ab956e | 1489 | ino, data_offset, disk_byte, found_key.objectid); |
31db9f7c AB |
1490 | goto out; |
1491 | } | |
1492 | ||
04ab956e JM |
1493 | btrfs_debug(fs_info, |
1494 | "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu", | |
1495 | data_offset, ino, num_bytes, logical); | |
31db9f7c | 1496 | |
dce28150 | 1497 | if (!backref_ctx.found) |
04ab956e | 1498 | btrfs_debug(fs_info, "no clones found"); |
31db9f7c AB |
1499 | |
1500 | cur_clone_root = NULL; | |
1501 | for (i = 0; i < sctx->clone_roots_cnt; i++) { | |
1502 | if (sctx->clone_roots[i].found_refs) { | |
1503 | if (!cur_clone_root) | |
1504 | cur_clone_root = sctx->clone_roots + i; | |
1505 | else if (sctx->clone_roots[i].root == sctx->send_root) | |
1506 | /* prefer clones from send_root over others */ | |
1507 | cur_clone_root = sctx->clone_roots + i; | |
31db9f7c AB |
1508 | } |
1509 | ||
1510 | } | |
1511 | ||
1512 | if (cur_clone_root) { | |
1513 | *found = cur_clone_root; | |
1514 | ret = 0; | |
1515 | } else { | |
1516 | ret = -ENOENT; | |
1517 | } | |
1518 | ||
1519 | out: | |
1520 | btrfs_free_path(tmp_path); | |
1521 | return ret; | |
1522 | } | |
1523 | ||
924794c9 | 1524 | static int read_symlink(struct btrfs_root *root, |
31db9f7c AB |
1525 | u64 ino, |
1526 | struct fs_path *dest) | |
1527 | { | |
1528 | int ret; | |
1529 | struct btrfs_path *path; | |
1530 | struct btrfs_key key; | |
1531 | struct btrfs_file_extent_item *ei; | |
1532 | u8 type; | |
1533 | u8 compression; | |
1534 | unsigned long off; | |
1535 | int len; | |
1536 | ||
1537 | path = alloc_path_for_send(); | |
1538 | if (!path) | |
1539 | return -ENOMEM; | |
1540 | ||
1541 | key.objectid = ino; | |
1542 | key.type = BTRFS_EXTENT_DATA_KEY; | |
1543 | key.offset = 0; | |
1544 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1545 | if (ret < 0) | |
1546 | goto out; | |
a879719b FM |
1547 | if (ret) { |
1548 | /* | |
1549 | * An empty symlink inode. Can happen in rare error paths when | |
1550 | * creating a symlink (transaction committed before the inode | |
1551 | * eviction handler removed the symlink inode items and a crash | |
1552 | * happened in between or the subvol was snapshoted in between). | |
1553 | * Print an informative message to dmesg/syslog so that the user | |
1554 | * can delete the symlink. | |
1555 | */ | |
1556 | btrfs_err(root->fs_info, | |
1557 | "Found empty symlink inode %llu at root %llu", | |
1558 | ino, root->root_key.objectid); | |
1559 | ret = -EIO; | |
1560 | goto out; | |
1561 | } | |
31db9f7c AB |
1562 | |
1563 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1564 | struct btrfs_file_extent_item); | |
1565 | type = btrfs_file_extent_type(path->nodes[0], ei); | |
1566 | compression = btrfs_file_extent_compression(path->nodes[0], ei); | |
1567 | BUG_ON(type != BTRFS_FILE_EXTENT_INLINE); | |
1568 | BUG_ON(compression); | |
1569 | ||
1570 | off = btrfs_file_extent_inline_start(ei); | |
e41ca589 | 1571 | len = btrfs_file_extent_ram_bytes(path->nodes[0], ei); |
31db9f7c AB |
1572 | |
1573 | ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len); | |
31db9f7c AB |
1574 | |
1575 | out: | |
1576 | btrfs_free_path(path); | |
1577 | return ret; | |
1578 | } | |
1579 | ||
1580 | /* | |
1581 | * Helper function to generate a file name that is unique in the root of | |
1582 | * send_root and parent_root. This is used to generate names for orphan inodes. | |
1583 | */ | |
1584 | static int gen_unique_name(struct send_ctx *sctx, | |
1585 | u64 ino, u64 gen, | |
1586 | struct fs_path *dest) | |
1587 | { | |
1588 | int ret = 0; | |
1589 | struct btrfs_path *path; | |
1590 | struct btrfs_dir_item *di; | |
1591 | char tmp[64]; | |
1592 | int len; | |
1593 | u64 idx = 0; | |
1594 | ||
1595 | path = alloc_path_for_send(); | |
1596 | if (!path) | |
1597 | return -ENOMEM; | |
1598 | ||
1599 | while (1) { | |
6db75318 | 1600 | struct fscrypt_str tmp_name; |
e43eec81 | 1601 | |
f74b86d8 | 1602 | len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu", |
31db9f7c | 1603 | ino, gen, idx); |
64792f25 | 1604 | ASSERT(len < sizeof(tmp)); |
e43eec81 STD |
1605 | tmp_name.name = tmp; |
1606 | tmp_name.len = strlen(tmp); | |
31db9f7c AB |
1607 | |
1608 | di = btrfs_lookup_dir_item(NULL, sctx->send_root, | |
1609 | path, BTRFS_FIRST_FREE_OBJECTID, | |
e43eec81 | 1610 | &tmp_name, 0); |
31db9f7c AB |
1611 | btrfs_release_path(path); |
1612 | if (IS_ERR(di)) { | |
1613 | ret = PTR_ERR(di); | |
1614 | goto out; | |
1615 | } | |
1616 | if (di) { | |
1617 | /* not unique, try again */ | |
1618 | idx++; | |
1619 | continue; | |
1620 | } | |
1621 | ||
1622 | if (!sctx->parent_root) { | |
1623 | /* unique */ | |
1624 | ret = 0; | |
1625 | break; | |
1626 | } | |
1627 | ||
1628 | di = btrfs_lookup_dir_item(NULL, sctx->parent_root, | |
1629 | path, BTRFS_FIRST_FREE_OBJECTID, | |
e43eec81 | 1630 | &tmp_name, 0); |
31db9f7c AB |
1631 | btrfs_release_path(path); |
1632 | if (IS_ERR(di)) { | |
1633 | ret = PTR_ERR(di); | |
1634 | goto out; | |
1635 | } | |
1636 | if (di) { | |
1637 | /* not unique, try again */ | |
1638 | idx++; | |
1639 | continue; | |
1640 | } | |
1641 | /* unique */ | |
1642 | break; | |
1643 | } | |
1644 | ||
1645 | ret = fs_path_add(dest, tmp, strlen(tmp)); | |
1646 | ||
1647 | out: | |
1648 | btrfs_free_path(path); | |
1649 | return ret; | |
1650 | } | |
1651 | ||
1652 | enum inode_state { | |
1653 | inode_state_no_change, | |
1654 | inode_state_will_create, | |
1655 | inode_state_did_create, | |
1656 | inode_state_will_delete, | |
1657 | inode_state_did_delete, | |
1658 | }; | |
1659 | ||
1660 | static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) | |
1661 | { | |
1662 | int ret; | |
1663 | int left_ret; | |
1664 | int right_ret; | |
1665 | u64 left_gen; | |
1666 | u64 right_gen; | |
9ed0a72e | 1667 | struct btrfs_inode_info info; |
31db9f7c | 1668 | |
9ed0a72e | 1669 | ret = get_inode_info(sctx->send_root, ino, &info); |
31db9f7c AB |
1670 | if (ret < 0 && ret != -ENOENT) |
1671 | goto out; | |
9ed0a72e BC |
1672 | left_ret = (info.nlink == 0) ? -ENOENT : ret; |
1673 | left_gen = info.gen; | |
31db9f7c AB |
1674 | |
1675 | if (!sctx->parent_root) { | |
1676 | right_ret = -ENOENT; | |
1677 | } else { | |
9ed0a72e | 1678 | ret = get_inode_info(sctx->parent_root, ino, &info); |
31db9f7c AB |
1679 | if (ret < 0 && ret != -ENOENT) |
1680 | goto out; | |
9ed0a72e BC |
1681 | right_ret = (info.nlink == 0) ? -ENOENT : ret; |
1682 | right_gen = info.gen; | |
31db9f7c AB |
1683 | } |
1684 | ||
1685 | if (!left_ret && !right_ret) { | |
e938c8ad | 1686 | if (left_gen == gen && right_gen == gen) { |
31db9f7c | 1687 | ret = inode_state_no_change; |
e938c8ad | 1688 | } else if (left_gen == gen) { |
31db9f7c AB |
1689 | if (ino < sctx->send_progress) |
1690 | ret = inode_state_did_create; | |
1691 | else | |
1692 | ret = inode_state_will_create; | |
1693 | } else if (right_gen == gen) { | |
1694 | if (ino < sctx->send_progress) | |
1695 | ret = inode_state_did_delete; | |
1696 | else | |
1697 | ret = inode_state_will_delete; | |
1698 | } else { | |
1699 | ret = -ENOENT; | |
1700 | } | |
1701 | } else if (!left_ret) { | |
1702 | if (left_gen == gen) { | |
1703 | if (ino < sctx->send_progress) | |
1704 | ret = inode_state_did_create; | |
1705 | else | |
1706 | ret = inode_state_will_create; | |
1707 | } else { | |
1708 | ret = -ENOENT; | |
1709 | } | |
1710 | } else if (!right_ret) { | |
1711 | if (right_gen == gen) { | |
1712 | if (ino < sctx->send_progress) | |
1713 | ret = inode_state_did_delete; | |
1714 | else | |
1715 | ret = inode_state_will_delete; | |
1716 | } else { | |
1717 | ret = -ENOENT; | |
1718 | } | |
1719 | } else { | |
1720 | ret = -ENOENT; | |
1721 | } | |
1722 | ||
1723 | out: | |
1724 | return ret; | |
1725 | } | |
1726 | ||
1727 | static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) | |
1728 | { | |
1729 | int ret; | |
1730 | ||
4dd9920d RK |
1731 | if (ino == BTRFS_FIRST_FREE_OBJECTID) |
1732 | return 1; | |
1733 | ||
31db9f7c AB |
1734 | ret = get_cur_inode_state(sctx, ino, gen); |
1735 | if (ret < 0) | |
1736 | goto out; | |
1737 | ||
1738 | if (ret == inode_state_no_change || | |
1739 | ret == inode_state_did_create || | |
1740 | ret == inode_state_will_delete) | |
1741 | ret = 1; | |
1742 | else | |
1743 | ret = 0; | |
1744 | ||
1745 | out: | |
1746 | return ret; | |
1747 | } | |
1748 | ||
1749 | /* | |
1750 | * Helper function to lookup a dir item in a dir. | |
1751 | */ | |
1752 | static int lookup_dir_item_inode(struct btrfs_root *root, | |
1753 | u64 dir, const char *name, int name_len, | |
eab67c06 | 1754 | u64 *found_inode) |
31db9f7c AB |
1755 | { |
1756 | int ret = 0; | |
1757 | struct btrfs_dir_item *di; | |
1758 | struct btrfs_key key; | |
1759 | struct btrfs_path *path; | |
6db75318 | 1760 | struct fscrypt_str name_str = FSTR_INIT((char *)name, name_len); |
31db9f7c AB |
1761 | |
1762 | path = alloc_path_for_send(); | |
1763 | if (!path) | |
1764 | return -ENOMEM; | |
1765 | ||
e43eec81 | 1766 | di = btrfs_lookup_dir_item(NULL, root, path, dir, &name_str, 0); |
3cf5068f LB |
1767 | if (IS_ERR_OR_NULL(di)) { |
1768 | ret = di ? PTR_ERR(di) : -ENOENT; | |
31db9f7c AB |
1769 | goto out; |
1770 | } | |
1771 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
1af56070 FM |
1772 | if (key.type == BTRFS_ROOT_ITEM_KEY) { |
1773 | ret = -ENOENT; | |
1774 | goto out; | |
1775 | } | |
31db9f7c | 1776 | *found_inode = key.objectid; |
31db9f7c AB |
1777 | |
1778 | out: | |
1779 | btrfs_free_path(path); | |
1780 | return ret; | |
1781 | } | |
1782 | ||
766702ef AB |
1783 | /* |
1784 | * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir, | |
1785 | * generation of the parent dir and the name of the dir entry. | |
1786 | */ | |
924794c9 | 1787 | static int get_first_ref(struct btrfs_root *root, u64 ino, |
31db9f7c AB |
1788 | u64 *dir, u64 *dir_gen, struct fs_path *name) |
1789 | { | |
1790 | int ret; | |
1791 | struct btrfs_key key; | |
1792 | struct btrfs_key found_key; | |
1793 | struct btrfs_path *path; | |
31db9f7c | 1794 | int len; |
96b5bd77 | 1795 | u64 parent_dir; |
31db9f7c AB |
1796 | |
1797 | path = alloc_path_for_send(); | |
1798 | if (!path) | |
1799 | return -ENOMEM; | |
1800 | ||
1801 | key.objectid = ino; | |
1802 | key.type = BTRFS_INODE_REF_KEY; | |
1803 | key.offset = 0; | |
1804 | ||
1805 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); | |
1806 | if (ret < 0) | |
1807 | goto out; | |
1808 | if (!ret) | |
1809 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1810 | path->slots[0]); | |
96b5bd77 JS |
1811 | if (ret || found_key.objectid != ino || |
1812 | (found_key.type != BTRFS_INODE_REF_KEY && | |
1813 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { | |
31db9f7c AB |
1814 | ret = -ENOENT; |
1815 | goto out; | |
1816 | } | |
1817 | ||
51a60253 | 1818 | if (found_key.type == BTRFS_INODE_REF_KEY) { |
96b5bd77 JS |
1819 | struct btrfs_inode_ref *iref; |
1820 | iref = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1821 | struct btrfs_inode_ref); | |
1822 | len = btrfs_inode_ref_name_len(path->nodes[0], iref); | |
1823 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], | |
1824 | (unsigned long)(iref + 1), | |
1825 | len); | |
1826 | parent_dir = found_key.offset; | |
1827 | } else { | |
1828 | struct btrfs_inode_extref *extref; | |
1829 | extref = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1830 | struct btrfs_inode_extref); | |
1831 | len = btrfs_inode_extref_name_len(path->nodes[0], extref); | |
1832 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], | |
1833 | (unsigned long)&extref->name, len); | |
1834 | parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref); | |
1835 | } | |
31db9f7c AB |
1836 | if (ret < 0) |
1837 | goto out; | |
1838 | btrfs_release_path(path); | |
1839 | ||
b46ab97b | 1840 | if (dir_gen) { |
7e93f6dc | 1841 | ret = get_inode_gen(root, parent_dir, dir_gen); |
b46ab97b FM |
1842 | if (ret < 0) |
1843 | goto out; | |
1844 | } | |
31db9f7c | 1845 | |
96b5bd77 | 1846 | *dir = parent_dir; |
31db9f7c AB |
1847 | |
1848 | out: | |
1849 | btrfs_free_path(path); | |
1850 | return ret; | |
1851 | } | |
1852 | ||
924794c9 | 1853 | static int is_first_ref(struct btrfs_root *root, |
31db9f7c AB |
1854 | u64 ino, u64 dir, |
1855 | const char *name, int name_len) | |
1856 | { | |
1857 | int ret; | |
1858 | struct fs_path *tmp_name; | |
1859 | u64 tmp_dir; | |
31db9f7c | 1860 | |
924794c9 | 1861 | tmp_name = fs_path_alloc(); |
31db9f7c AB |
1862 | if (!tmp_name) |
1863 | return -ENOMEM; | |
1864 | ||
b46ab97b | 1865 | ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name); |
31db9f7c AB |
1866 | if (ret < 0) |
1867 | goto out; | |
1868 | ||
b9291aff | 1869 | if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) { |
31db9f7c AB |
1870 | ret = 0; |
1871 | goto out; | |
1872 | } | |
1873 | ||
e938c8ad | 1874 | ret = !memcmp(tmp_name->start, name, name_len); |
31db9f7c AB |
1875 | |
1876 | out: | |
924794c9 | 1877 | fs_path_free(tmp_name); |
31db9f7c AB |
1878 | return ret; |
1879 | } | |
1880 | ||
766702ef AB |
1881 | /* |
1882 | * Used by process_recorded_refs to determine if a new ref would overwrite an | |
1883 | * already existing ref. In case it detects an overwrite, it returns the | |
1884 | * inode/gen in who_ino/who_gen. | |
1885 | * When an overwrite is detected, process_recorded_refs does proper orphanizing | |
1886 | * to make sure later references to the overwritten inode are possible. | |
1887 | * Orphanizing is however only required for the first ref of an inode. | |
1888 | * process_recorded_refs does an additional is_first_ref check to see if | |
1889 | * orphanizing is really required. | |
1890 | */ | |
31db9f7c AB |
1891 | static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
1892 | const char *name, int name_len, | |
f5962781 | 1893 | u64 *who_ino, u64 *who_gen, u64 *who_mode) |
31db9f7c AB |
1894 | { |
1895 | int ret = 0; | |
ebdad913 | 1896 | u64 gen; |
31db9f7c | 1897 | u64 other_inode = 0; |
7e93f6dc | 1898 | struct btrfs_inode_info info; |
31db9f7c AB |
1899 | |
1900 | if (!sctx->parent_root) | |
1901 | goto out; | |
1902 | ||
1903 | ret = is_inode_existent(sctx, dir, dir_gen); | |
1904 | if (ret <= 0) | |
1905 | goto out; | |
1906 | ||
ebdad913 JB |
1907 | /* |
1908 | * If we have a parent root we need to verify that the parent dir was | |
01327610 | 1909 | * not deleted and then re-created, if it was then we have no overwrite |
ebdad913 JB |
1910 | * and we can just unlink this entry. |
1911 | */ | |
4dd9920d | 1912 | if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) { |
7e93f6dc | 1913 | ret = get_inode_gen(sctx->parent_root, dir, &gen); |
ebdad913 JB |
1914 | if (ret < 0 && ret != -ENOENT) |
1915 | goto out; | |
1916 | if (ret) { | |
1917 | ret = 0; | |
1918 | goto out; | |
1919 | } | |
1920 | if (gen != dir_gen) | |
1921 | goto out; | |
1922 | } | |
1923 | ||
31db9f7c | 1924 | ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, |
eab67c06 | 1925 | &other_inode); |
31db9f7c AB |
1926 | if (ret < 0 && ret != -ENOENT) |
1927 | goto out; | |
1928 | if (ret) { | |
1929 | ret = 0; | |
1930 | goto out; | |
1931 | } | |
1932 | ||
766702ef AB |
1933 | /* |
1934 | * Check if the overwritten ref was already processed. If yes, the ref | |
1935 | * was already unlinked/moved, so we can safely assume that we will not | |
1936 | * overwrite anything at this point in time. | |
1937 | */ | |
801bec36 RK |
1938 | if (other_inode > sctx->send_progress || |
1939 | is_waiting_for_move(sctx, other_inode)) { | |
7e93f6dc | 1940 | ret = get_inode_info(sctx->parent_root, other_inode, &info); |
31db9f7c AB |
1941 | if (ret < 0) |
1942 | goto out; | |
1943 | ||
1944 | ret = 1; | |
1945 | *who_ino = other_inode; | |
7e93f6dc BC |
1946 | *who_gen = info.gen; |
1947 | *who_mode = info.mode; | |
31db9f7c AB |
1948 | } else { |
1949 | ret = 0; | |
1950 | } | |
1951 | ||
1952 | out: | |
1953 | return ret; | |
1954 | } | |
1955 | ||
766702ef AB |
1956 | /* |
1957 | * Checks if the ref was overwritten by an already processed inode. This is | |
1958 | * used by __get_cur_name_and_parent to find out if the ref was orphanized and | |
1959 | * thus the orphan name needs be used. | |
1960 | * process_recorded_refs also uses it to avoid unlinking of refs that were | |
1961 | * overwritten. | |
1962 | */ | |
31db9f7c AB |
1963 | static int did_overwrite_ref(struct send_ctx *sctx, |
1964 | u64 dir, u64 dir_gen, | |
1965 | u64 ino, u64 ino_gen, | |
1966 | const char *name, int name_len) | |
1967 | { | |
1968 | int ret = 0; | |
1969 | u64 gen; | |
1970 | u64 ow_inode; | |
31db9f7c AB |
1971 | |
1972 | if (!sctx->parent_root) | |
1973 | goto out; | |
1974 | ||
1975 | ret = is_inode_existent(sctx, dir, dir_gen); | |
1976 | if (ret <= 0) | |
1977 | goto out; | |
1978 | ||
01914101 | 1979 | if (dir != BTRFS_FIRST_FREE_OBJECTID) { |
7e93f6dc | 1980 | ret = get_inode_gen(sctx->send_root, dir, &gen); |
01914101 RK |
1981 | if (ret < 0 && ret != -ENOENT) |
1982 | goto out; | |
1983 | if (ret) { | |
1984 | ret = 0; | |
1985 | goto out; | |
1986 | } | |
1987 | if (gen != dir_gen) | |
1988 | goto out; | |
1989 | } | |
1990 | ||
31db9f7c AB |
1991 | /* check if the ref was overwritten by another ref */ |
1992 | ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, | |
eab67c06 | 1993 | &ow_inode); |
31db9f7c AB |
1994 | if (ret < 0 && ret != -ENOENT) |
1995 | goto out; | |
1996 | if (ret) { | |
1997 | /* was never and will never be overwritten */ | |
1998 | ret = 0; | |
1999 | goto out; | |
2000 | } | |
2001 | ||
7e93f6dc | 2002 | ret = get_inode_gen(sctx->send_root, ow_inode, &gen); |
31db9f7c AB |
2003 | if (ret < 0) |
2004 | goto out; | |
2005 | ||
2006 | if (ow_inode == ino && gen == ino_gen) { | |
2007 | ret = 0; | |
2008 | goto out; | |
2009 | } | |
2010 | ||
8b191a68 FM |
2011 | /* |
2012 | * We know that it is or will be overwritten. Check this now. | |
2013 | * The current inode being processed might have been the one that caused | |
b786f16a FM |
2014 | * inode 'ino' to be orphanized, therefore check if ow_inode matches |
2015 | * the current inode being processed. | |
8b191a68 | 2016 | */ |
b786f16a FM |
2017 | if ((ow_inode < sctx->send_progress) || |
2018 | (ino != sctx->cur_ino && ow_inode == sctx->cur_ino && | |
2019 | gen == sctx->cur_inode_gen)) | |
31db9f7c AB |
2020 | ret = 1; |
2021 | else | |
2022 | ret = 0; | |
2023 | ||
2024 | out: | |
2025 | return ret; | |
2026 | } | |
2027 | ||
766702ef AB |
2028 | /* |
2029 | * Same as did_overwrite_ref, but also checks if it is the first ref of an inode | |
2030 | * that got overwritten. This is used by process_recorded_refs to determine | |
2031 | * if it has to use the path as returned by get_cur_path or the orphan name. | |
2032 | */ | |
31db9f7c AB |
2033 | static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) |
2034 | { | |
2035 | int ret = 0; | |
2036 | struct fs_path *name = NULL; | |
2037 | u64 dir; | |
2038 | u64 dir_gen; | |
2039 | ||
2040 | if (!sctx->parent_root) | |
2041 | goto out; | |
2042 | ||
924794c9 | 2043 | name = fs_path_alloc(); |
31db9f7c AB |
2044 | if (!name) |
2045 | return -ENOMEM; | |
2046 | ||
924794c9 | 2047 | ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name); |
31db9f7c AB |
2048 | if (ret < 0) |
2049 | goto out; | |
2050 | ||
2051 | ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, | |
2052 | name->start, fs_path_len(name)); | |
31db9f7c AB |
2053 | |
2054 | out: | |
924794c9 | 2055 | fs_path_free(name); |
31db9f7c AB |
2056 | return ret; |
2057 | } | |
2058 | ||
766702ef | 2059 | /* |
5b8418b8 | 2060 | * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit, |
766702ef | 2061 | * so we need to do some special handling in case we have clashes. This function |
5b8418b8 | 2062 | * takes care of this with the help of name_cache_entry::radix_list. |
5dc67d0b | 2063 | * In case of error, nce is kfreed. |
766702ef | 2064 | */ |
31db9f7c AB |
2065 | static int name_cache_insert(struct send_ctx *sctx, |
2066 | struct name_cache_entry *nce) | |
2067 | { | |
2068 | int ret = 0; | |
7e0926fe AB |
2069 | struct list_head *nce_head; |
2070 | ||
5b8418b8 DS |
2071 | nce_head = radix_tree_lookup(&sctx->name_cache, |
2072 | (unsigned long)nce->ino); | |
7e0926fe | 2073 | if (!nce_head) { |
e780b0d1 | 2074 | nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL); |
cfa7a9cc TI |
2075 | if (!nce_head) { |
2076 | kfree(nce); | |
31db9f7c | 2077 | return -ENOMEM; |
cfa7a9cc | 2078 | } |
7e0926fe | 2079 | INIT_LIST_HEAD(nce_head); |
31db9f7c | 2080 | |
5b8418b8 | 2081 | ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); |
5dc67d0b AB |
2082 | if (ret < 0) { |
2083 | kfree(nce_head); | |
2084 | kfree(nce); | |
31db9f7c | 2085 | return ret; |
5dc67d0b | 2086 | } |
31db9f7c | 2087 | } |
5b8418b8 | 2088 | list_add_tail(&nce->radix_list, nce_head); |
31db9f7c AB |
2089 | list_add_tail(&nce->list, &sctx->name_cache_list); |
2090 | sctx->name_cache_size++; | |
2091 | ||
2092 | return ret; | |
2093 | } | |
2094 | ||
2095 | static void name_cache_delete(struct send_ctx *sctx, | |
2096 | struct name_cache_entry *nce) | |
2097 | { | |
7e0926fe | 2098 | struct list_head *nce_head; |
31db9f7c | 2099 | |
5b8418b8 DS |
2100 | nce_head = radix_tree_lookup(&sctx->name_cache, |
2101 | (unsigned long)nce->ino); | |
57fb8910 DS |
2102 | if (!nce_head) { |
2103 | btrfs_err(sctx->send_root->fs_info, | |
2104 | "name_cache_delete lookup failed ino %llu cache size %d, leaking memory", | |
2105 | nce->ino, sctx->name_cache_size); | |
2106 | } | |
31db9f7c | 2107 | |
5b8418b8 | 2108 | list_del(&nce->radix_list); |
31db9f7c | 2109 | list_del(&nce->list); |
31db9f7c | 2110 | sctx->name_cache_size--; |
7e0926fe | 2111 | |
57fb8910 DS |
2112 | /* |
2113 | * We may not get to the final release of nce_head if the lookup fails | |
2114 | */ | |
2115 | if (nce_head && list_empty(nce_head)) { | |
5b8418b8 | 2116 | radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); |
7e0926fe AB |
2117 | kfree(nce_head); |
2118 | } | |
31db9f7c AB |
2119 | } |
2120 | ||
2121 | static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, | |
2122 | u64 ino, u64 gen) | |
2123 | { | |
7e0926fe AB |
2124 | struct list_head *nce_head; |
2125 | struct name_cache_entry *cur; | |
31db9f7c | 2126 | |
5b8418b8 | 2127 | nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); |
7e0926fe | 2128 | if (!nce_head) |
31db9f7c AB |
2129 | return NULL; |
2130 | ||
5b8418b8 | 2131 | list_for_each_entry(cur, nce_head, radix_list) { |
7e0926fe AB |
2132 | if (cur->ino == ino && cur->gen == gen) |
2133 | return cur; | |
2134 | } | |
31db9f7c AB |
2135 | return NULL; |
2136 | } | |
2137 | ||
766702ef AB |
2138 | /* |
2139 | * Remove some entries from the beginning of name_cache_list. | |
2140 | */ | |
31db9f7c AB |
2141 | static void name_cache_clean_unused(struct send_ctx *sctx) |
2142 | { | |
2143 | struct name_cache_entry *nce; | |
2144 | ||
2145 | if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) | |
2146 | return; | |
2147 | ||
2148 | while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { | |
2149 | nce = list_entry(sctx->name_cache_list.next, | |
2150 | struct name_cache_entry, list); | |
2151 | name_cache_delete(sctx, nce); | |
2152 | kfree(nce); | |
2153 | } | |
2154 | } | |
2155 | ||
2156 | static void name_cache_free(struct send_ctx *sctx) | |
2157 | { | |
2158 | struct name_cache_entry *nce; | |
31db9f7c | 2159 | |
e938c8ad AB |
2160 | while (!list_empty(&sctx->name_cache_list)) { |
2161 | nce = list_entry(sctx->name_cache_list.next, | |
2162 | struct name_cache_entry, list); | |
31db9f7c | 2163 | name_cache_delete(sctx, nce); |
17589bd9 | 2164 | kfree(nce); |
31db9f7c AB |
2165 | } |
2166 | } | |
2167 | ||
766702ef AB |
2168 | /* |
2169 | * Used by get_cur_path for each ref up to the root. | |
2170 | * Returns 0 if it succeeded. | |
2171 | * Returns 1 if the inode is not existent or got overwritten. In that case, the | |
2172 | * name is an orphan name. This instructs get_cur_path to stop iterating. If 1 | |
2173 | * is returned, parent_ino/parent_gen are not guaranteed to be valid. | |
2174 | * Returns <0 in case of error. | |
2175 | */ | |
31db9f7c AB |
2176 | static int __get_cur_name_and_parent(struct send_ctx *sctx, |
2177 | u64 ino, u64 gen, | |
2178 | u64 *parent_ino, | |
2179 | u64 *parent_gen, | |
2180 | struct fs_path *dest) | |
2181 | { | |
2182 | int ret; | |
2183 | int nce_ret; | |
31db9f7c AB |
2184 | struct name_cache_entry *nce = NULL; |
2185 | ||
766702ef AB |
2186 | /* |
2187 | * First check if we already did a call to this function with the same | |
2188 | * ino/gen. If yes, check if the cache entry is still up-to-date. If yes | |
2189 | * return the cached result. | |
2190 | */ | |
31db9f7c AB |
2191 | nce = name_cache_search(sctx, ino, gen); |
2192 | if (nce) { | |
2193 | if (ino < sctx->send_progress && nce->need_later_update) { | |
2194 | name_cache_delete(sctx, nce); | |
2195 | kfree(nce); | |
2196 | nce = NULL; | |
2197 | } else { | |
bb930007 BL |
2198 | /* |
2199 | * Removes the entry from the list and adds it back to | |
2200 | * the end. This marks the entry as recently used so | |
2201 | * that name_cache_clean_unused does not remove it. | |
2202 | */ | |
2203 | list_move_tail(&nce->list, &sctx->name_cache_list); | |
2204 | ||
31db9f7c AB |
2205 | *parent_ino = nce->parent_ino; |
2206 | *parent_gen = nce->parent_gen; | |
2207 | ret = fs_path_add(dest, nce->name, nce->name_len); | |
2208 | if (ret < 0) | |
2209 | goto out; | |
2210 | ret = nce->ret; | |
2211 | goto out; | |
2212 | } | |
2213 | } | |
2214 | ||
766702ef AB |
2215 | /* |
2216 | * If the inode is not existent yet, add the orphan name and return 1. | |
2217 | * This should only happen for the parent dir that we determine in | |
0d8869fb | 2218 | * record_new_ref_if_needed(). |
766702ef | 2219 | */ |
31db9f7c AB |
2220 | ret = is_inode_existent(sctx, ino, gen); |
2221 | if (ret < 0) | |
2222 | goto out; | |
2223 | ||
2224 | if (!ret) { | |
2225 | ret = gen_unique_name(sctx, ino, gen, dest); | |
2226 | if (ret < 0) | |
2227 | goto out; | |
2228 | ret = 1; | |
2229 | goto out_cache; | |
2230 | } | |
2231 | ||
766702ef AB |
2232 | /* |
2233 | * Depending on whether the inode was already processed or not, use | |
2234 | * send_root or parent_root for ref lookup. | |
2235 | */ | |
bf0d1f44 | 2236 | if (ino < sctx->send_progress) |
924794c9 TI |
2237 | ret = get_first_ref(sctx->send_root, ino, |
2238 | parent_ino, parent_gen, dest); | |
31db9f7c | 2239 | else |
924794c9 TI |
2240 | ret = get_first_ref(sctx->parent_root, ino, |
2241 | parent_ino, parent_gen, dest); | |
31db9f7c AB |
2242 | if (ret < 0) |
2243 | goto out; | |
2244 | ||
766702ef AB |
2245 | /* |
2246 | * Check if the ref was overwritten by an inode's ref that was processed | |
2247 | * earlier. If yes, treat as orphan and return 1. | |
2248 | */ | |
31db9f7c AB |
2249 | ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, |
2250 | dest->start, dest->end - dest->start); | |
2251 | if (ret < 0) | |
2252 | goto out; | |
2253 | if (ret) { | |
2254 | fs_path_reset(dest); | |
2255 | ret = gen_unique_name(sctx, ino, gen, dest); | |
2256 | if (ret < 0) | |
2257 | goto out; | |
2258 | ret = 1; | |
2259 | } | |
2260 | ||
2261 | out_cache: | |
766702ef AB |
2262 | /* |
2263 | * Store the result of the lookup in the name cache. | |
2264 | */ | |
e780b0d1 | 2265 | nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL); |
31db9f7c AB |
2266 | if (!nce) { |
2267 | ret = -ENOMEM; | |
2268 | goto out; | |
2269 | } | |
2270 | ||
2271 | nce->ino = ino; | |
2272 | nce->gen = gen; | |
2273 | nce->parent_ino = *parent_ino; | |
2274 | nce->parent_gen = *parent_gen; | |
2275 | nce->name_len = fs_path_len(dest); | |
2276 | nce->ret = ret; | |
2277 | strcpy(nce->name, dest->start); | |
31db9f7c AB |
2278 | |
2279 | if (ino < sctx->send_progress) | |
2280 | nce->need_later_update = 0; | |
2281 | else | |
2282 | nce->need_later_update = 1; | |
2283 | ||
2284 | nce_ret = name_cache_insert(sctx, nce); | |
2285 | if (nce_ret < 0) | |
2286 | ret = nce_ret; | |
2287 | name_cache_clean_unused(sctx); | |
2288 | ||
2289 | out: | |
31db9f7c AB |
2290 | return ret; |
2291 | } | |
2292 | ||
2293 | /* | |
2294 | * Magic happens here. This function returns the first ref to an inode as it | |
2295 | * would look like while receiving the stream at this point in time. | |
2296 | * We walk the path up to the root. For every inode in between, we check if it | |
2297 | * was already processed/sent. If yes, we continue with the parent as found | |
2298 | * in send_root. If not, we continue with the parent as found in parent_root. | |
2299 | * If we encounter an inode that was deleted at this point in time, we use the | |
2300 | * inodes "orphan" name instead of the real name and stop. Same with new inodes | |
2301 | * that were not created yet and overwritten inodes/refs. | |
2302 | * | |
52042d8e | 2303 | * When do we have orphan inodes: |
31db9f7c AB |
2304 | * 1. When an inode is freshly created and thus no valid refs are available yet |
2305 | * 2. When a directory lost all it's refs (deleted) but still has dir items | |
2306 | * inside which were not processed yet (pending for move/delete). If anyone | |
2307 | * tried to get the path to the dir items, it would get a path inside that | |
2308 | * orphan directory. | |
2309 | * 3. When an inode is moved around or gets new links, it may overwrite the ref | |
2310 | * of an unprocessed inode. If in that case the first ref would be | |
2311 | * overwritten, the overwritten inode gets "orphanized". Later when we | |
2312 | * process this overwritten inode, it is restored at a new place by moving | |
2313 | * the orphan inode. | |
2314 | * | |
2315 | * sctx->send_progress tells this function at which point in time receiving | |
2316 | * would be. | |
2317 | */ | |
2318 | static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, | |
2319 | struct fs_path *dest) | |
2320 | { | |
2321 | int ret = 0; | |
2322 | struct fs_path *name = NULL; | |
2323 | u64 parent_inode = 0; | |
2324 | u64 parent_gen = 0; | |
2325 | int stop = 0; | |
2326 | ||
924794c9 | 2327 | name = fs_path_alloc(); |
31db9f7c AB |
2328 | if (!name) { |
2329 | ret = -ENOMEM; | |
2330 | goto out; | |
2331 | } | |
2332 | ||
2333 | dest->reversed = 1; | |
2334 | fs_path_reset(dest); | |
2335 | ||
2336 | while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) { | |
8b191a68 FM |
2337 | struct waiting_dir_move *wdm; |
2338 | ||
31db9f7c AB |
2339 | fs_path_reset(name); |
2340 | ||
0b3f407e | 2341 | if (is_waiting_for_rm(sctx, ino, gen)) { |
9dc44214 FM |
2342 | ret = gen_unique_name(sctx, ino, gen, name); |
2343 | if (ret < 0) | |
2344 | goto out; | |
2345 | ret = fs_path_add_path(dest, name); | |
2346 | break; | |
2347 | } | |
2348 | ||
8b191a68 FM |
2349 | wdm = get_waiting_dir_move(sctx, ino); |
2350 | if (wdm && wdm->orphanized) { | |
2351 | ret = gen_unique_name(sctx, ino, gen, name); | |
2352 | stop = 1; | |
2353 | } else if (wdm) { | |
bf0d1f44 FM |
2354 | ret = get_first_ref(sctx->parent_root, ino, |
2355 | &parent_inode, &parent_gen, name); | |
2356 | } else { | |
2357 | ret = __get_cur_name_and_parent(sctx, ino, gen, | |
2358 | &parent_inode, | |
2359 | &parent_gen, name); | |
2360 | if (ret) | |
2361 | stop = 1; | |
2362 | } | |
2363 | ||
31db9f7c AB |
2364 | if (ret < 0) |
2365 | goto out; | |
9f03740a | 2366 | |
31db9f7c AB |
2367 | ret = fs_path_add_path(dest, name); |
2368 | if (ret < 0) | |
2369 | goto out; | |
2370 | ||
2371 | ino = parent_inode; | |
2372 | gen = parent_gen; | |
2373 | } | |
2374 | ||
2375 | out: | |
924794c9 | 2376 | fs_path_free(name); |
31db9f7c AB |
2377 | if (!ret) |
2378 | fs_path_unreverse(dest); | |
2379 | return ret; | |
2380 | } | |
2381 | ||
31db9f7c AB |
2382 | /* |
2383 | * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace | |
2384 | */ | |
2385 | static int send_subvol_begin(struct send_ctx *sctx) | |
2386 | { | |
2387 | int ret; | |
2388 | struct btrfs_root *send_root = sctx->send_root; | |
2389 | struct btrfs_root *parent_root = sctx->parent_root; | |
2390 | struct btrfs_path *path; | |
2391 | struct btrfs_key key; | |
2392 | struct btrfs_root_ref *ref; | |
2393 | struct extent_buffer *leaf; | |
2394 | char *name = NULL; | |
2395 | int namelen; | |
2396 | ||
ffcfaf81 | 2397 | path = btrfs_alloc_path(); |
31db9f7c AB |
2398 | if (!path) |
2399 | return -ENOMEM; | |
2400 | ||
e780b0d1 | 2401 | name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL); |
31db9f7c AB |
2402 | if (!name) { |
2403 | btrfs_free_path(path); | |
2404 | return -ENOMEM; | |
2405 | } | |
2406 | ||
4fd786e6 | 2407 | key.objectid = send_root->root_key.objectid; |
31db9f7c AB |
2408 | key.type = BTRFS_ROOT_BACKREF_KEY; |
2409 | key.offset = 0; | |
2410 | ||
2411 | ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root, | |
2412 | &key, path, 1, 0); | |
2413 | if (ret < 0) | |
2414 | goto out; | |
2415 | if (ret) { | |
2416 | ret = -ENOENT; | |
2417 | goto out; | |
2418 | } | |
2419 | ||
2420 | leaf = path->nodes[0]; | |
2421 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
2422 | if (key.type != BTRFS_ROOT_BACKREF_KEY || | |
4fd786e6 | 2423 | key.objectid != send_root->root_key.objectid) { |
31db9f7c AB |
2424 | ret = -ENOENT; |
2425 | goto out; | |
2426 | } | |
2427 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
2428 | namelen = btrfs_root_ref_name_len(leaf, ref); | |
2429 | read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen); | |
2430 | btrfs_release_path(path); | |
2431 | ||
31db9f7c AB |
2432 | if (parent_root) { |
2433 | ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); | |
2434 | if (ret < 0) | |
2435 | goto out; | |
2436 | } else { | |
2437 | ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); | |
2438 | if (ret < 0) | |
2439 | goto out; | |
2440 | } | |
2441 | ||
2442 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); | |
b96b1db0 RR |
2443 | |
2444 | if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid)) | |
2445 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, | |
2446 | sctx->send_root->root_item.received_uuid); | |
2447 | else | |
2448 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, | |
2449 | sctx->send_root->root_item.uuid); | |
2450 | ||
31db9f7c | 2451 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, |
09e3a288 | 2452 | btrfs_root_ctransid(&sctx->send_root->root_item)); |
31db9f7c | 2453 | if (parent_root) { |
37b8d27d JB |
2454 | if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid)) |
2455 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, | |
2456 | parent_root->root_item.received_uuid); | |
2457 | else | |
2458 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, | |
2459 | parent_root->root_item.uuid); | |
31db9f7c | 2460 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
09e3a288 | 2461 | btrfs_root_ctransid(&sctx->parent_root->root_item)); |
31db9f7c AB |
2462 | } |
2463 | ||
2464 | ret = send_cmd(sctx); | |
2465 | ||
2466 | tlv_put_failure: | |
2467 | out: | |
2468 | btrfs_free_path(path); | |
2469 | kfree(name); | |
2470 | return ret; | |
2471 | } | |
2472 | ||
2473 | static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) | |
2474 | { | |
04ab956e | 2475 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
2476 | int ret = 0; |
2477 | struct fs_path *p; | |
2478 | ||
04ab956e | 2479 | btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size); |
31db9f7c | 2480 | |
924794c9 | 2481 | p = fs_path_alloc(); |
31db9f7c AB |
2482 | if (!p) |
2483 | return -ENOMEM; | |
2484 | ||
2485 | ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); | |
2486 | if (ret < 0) | |
2487 | goto out; | |
2488 | ||
2489 | ret = get_cur_path(sctx, ino, gen, p); | |
2490 | if (ret < 0) | |
2491 | goto out; | |
2492 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2493 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); | |
2494 | ||
2495 | ret = send_cmd(sctx); | |
2496 | ||
2497 | tlv_put_failure: | |
2498 | out: | |
924794c9 | 2499 | fs_path_free(p); |
31db9f7c AB |
2500 | return ret; |
2501 | } | |
2502 | ||
2503 | static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) | |
2504 | { | |
04ab956e | 2505 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
2506 | int ret = 0; |
2507 | struct fs_path *p; | |
2508 | ||
04ab956e | 2509 | btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode); |
31db9f7c | 2510 | |
924794c9 | 2511 | p = fs_path_alloc(); |
31db9f7c AB |
2512 | if (!p) |
2513 | return -ENOMEM; | |
2514 | ||
2515 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); | |
2516 | if (ret < 0) | |
2517 | goto out; | |
2518 | ||
2519 | ret = get_cur_path(sctx, ino, gen, p); | |
2520 | if (ret < 0) | |
2521 | goto out; | |
2522 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2523 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); | |
2524 | ||
2525 | ret = send_cmd(sctx); | |
2526 | ||
2527 | tlv_put_failure: | |
2528 | out: | |
924794c9 | 2529 | fs_path_free(p); |
31db9f7c AB |
2530 | return ret; |
2531 | } | |
2532 | ||
48247359 DS |
2533 | static int send_fileattr(struct send_ctx *sctx, u64 ino, u64 gen, u64 fileattr) |
2534 | { | |
2535 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; | |
2536 | int ret = 0; | |
2537 | struct fs_path *p; | |
2538 | ||
2539 | if (sctx->proto < 2) | |
2540 | return 0; | |
2541 | ||
2542 | btrfs_debug(fs_info, "send_fileattr %llu fileattr=%llu", ino, fileattr); | |
2543 | ||
2544 | p = fs_path_alloc(); | |
2545 | if (!p) | |
2546 | return -ENOMEM; | |
2547 | ||
2548 | ret = begin_cmd(sctx, BTRFS_SEND_C_FILEATTR); | |
2549 | if (ret < 0) | |
2550 | goto out; | |
2551 | ||
2552 | ret = get_cur_path(sctx, ino, gen, p); | |
2553 | if (ret < 0) | |
2554 | goto out; | |
2555 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2556 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILEATTR, fileattr); | |
2557 | ||
2558 | ret = send_cmd(sctx); | |
2559 | ||
2560 | tlv_put_failure: | |
2561 | out: | |
2562 | fs_path_free(p); | |
2563 | return ret; | |
2564 | } | |
2565 | ||
31db9f7c AB |
2566 | static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) |
2567 | { | |
04ab956e | 2568 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
2569 | int ret = 0; |
2570 | struct fs_path *p; | |
2571 | ||
04ab956e JM |
2572 | btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu", |
2573 | ino, uid, gid); | |
31db9f7c | 2574 | |
924794c9 | 2575 | p = fs_path_alloc(); |
31db9f7c AB |
2576 | if (!p) |
2577 | return -ENOMEM; | |
2578 | ||
2579 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); | |
2580 | if (ret < 0) | |
2581 | goto out; | |
2582 | ||
2583 | ret = get_cur_path(sctx, ino, gen, p); | |
2584 | if (ret < 0) | |
2585 | goto out; | |
2586 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
2587 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); | |
2588 | TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); | |
2589 | ||
2590 | ret = send_cmd(sctx); | |
2591 | ||
2592 | tlv_put_failure: | |
2593 | out: | |
924794c9 | 2594 | fs_path_free(p); |
31db9f7c AB |
2595 | return ret; |
2596 | } | |
2597 | ||
2598 | static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) | |
2599 | { | |
04ab956e | 2600 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
2601 | int ret = 0; |
2602 | struct fs_path *p = NULL; | |
2603 | struct btrfs_inode_item *ii; | |
2604 | struct btrfs_path *path = NULL; | |
2605 | struct extent_buffer *eb; | |
2606 | struct btrfs_key key; | |
2607 | int slot; | |
2608 | ||
04ab956e | 2609 | btrfs_debug(fs_info, "send_utimes %llu", ino); |
31db9f7c | 2610 | |
924794c9 | 2611 | p = fs_path_alloc(); |
31db9f7c AB |
2612 | if (!p) |
2613 | return -ENOMEM; | |
2614 | ||
2615 | path = alloc_path_for_send(); | |
2616 | if (!path) { | |
2617 | ret = -ENOMEM; | |
2618 | goto out; | |
2619 | } | |
2620 | ||
2621 | key.objectid = ino; | |
2622 | key.type = BTRFS_INODE_ITEM_KEY; | |
2623 | key.offset = 0; | |
2624 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); | |
15b253ea FM |
2625 | if (ret > 0) |
2626 | ret = -ENOENT; | |
31db9f7c AB |
2627 | if (ret < 0) |
2628 | goto out; | |
2629 | ||
2630 | eb = path->nodes[0]; | |
2631 | slot = path->slots[0]; | |
2632 | ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); | |
2633 | ||
2634 | ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); | |
2635 | if (ret < 0) | |
2636 | goto out; | |
2637 | ||
2638 | ret = get_cur_path(sctx, ino, gen, p); | |
2639 | if (ret < 0) | |
2640 | goto out; | |
2641 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
a937b979 DS |
2642 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime); |
2643 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime); | |
2644 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime); | |
22a5b2ab DS |
2645 | if (sctx->proto >= 2) |
2646 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_OTIME, eb, &ii->otime); | |
31db9f7c AB |
2647 | |
2648 | ret = send_cmd(sctx); | |
2649 | ||
2650 | tlv_put_failure: | |
2651 | out: | |
924794c9 | 2652 | fs_path_free(p); |
31db9f7c AB |
2653 | btrfs_free_path(path); |
2654 | return ret; | |
2655 | } | |
2656 | ||
2657 | /* | |
2658 | * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have | |
2659 | * a valid path yet because we did not process the refs yet. So, the inode | |
2660 | * is created as orphan. | |
2661 | */ | |
1f4692da | 2662 | static int send_create_inode(struct send_ctx *sctx, u64 ino) |
31db9f7c | 2663 | { |
04ab956e | 2664 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c | 2665 | int ret = 0; |
31db9f7c | 2666 | struct fs_path *p; |
31db9f7c | 2667 | int cmd; |
7e93f6dc | 2668 | struct btrfs_inode_info info; |
1f4692da | 2669 | u64 gen; |
31db9f7c | 2670 | u64 mode; |
1f4692da | 2671 | u64 rdev; |
31db9f7c | 2672 | |
04ab956e | 2673 | btrfs_debug(fs_info, "send_create_inode %llu", ino); |
31db9f7c | 2674 | |
924794c9 | 2675 | p = fs_path_alloc(); |
31db9f7c AB |
2676 | if (!p) |
2677 | return -ENOMEM; | |
2678 | ||
644d1940 | 2679 | if (ino != sctx->cur_ino) { |
7e93f6dc | 2680 | ret = get_inode_info(sctx->send_root, ino, &info); |
644d1940 LB |
2681 | if (ret < 0) |
2682 | goto out; | |
7e93f6dc BC |
2683 | gen = info.gen; |
2684 | mode = info.mode; | |
2685 | rdev = info.rdev; | |
644d1940 LB |
2686 | } else { |
2687 | gen = sctx->cur_inode_gen; | |
2688 | mode = sctx->cur_inode_mode; | |
2689 | rdev = sctx->cur_inode_rdev; | |
2690 | } | |
31db9f7c | 2691 | |
e938c8ad | 2692 | if (S_ISREG(mode)) { |
31db9f7c | 2693 | cmd = BTRFS_SEND_C_MKFILE; |
e938c8ad | 2694 | } else if (S_ISDIR(mode)) { |
31db9f7c | 2695 | cmd = BTRFS_SEND_C_MKDIR; |
e938c8ad | 2696 | } else if (S_ISLNK(mode)) { |
31db9f7c | 2697 | cmd = BTRFS_SEND_C_SYMLINK; |
e938c8ad | 2698 | } else if (S_ISCHR(mode) || S_ISBLK(mode)) { |
31db9f7c | 2699 | cmd = BTRFS_SEND_C_MKNOD; |
e938c8ad | 2700 | } else if (S_ISFIFO(mode)) { |
31db9f7c | 2701 | cmd = BTRFS_SEND_C_MKFIFO; |
e938c8ad | 2702 | } else if (S_ISSOCK(mode)) { |
31db9f7c | 2703 | cmd = BTRFS_SEND_C_MKSOCK; |
e938c8ad | 2704 | } else { |
f14d104d | 2705 | btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o", |
31db9f7c | 2706 | (int)(mode & S_IFMT)); |
ca6842bf | 2707 | ret = -EOPNOTSUPP; |
31db9f7c AB |
2708 | goto out; |
2709 | } | |
2710 | ||
2711 | ret = begin_cmd(sctx, cmd); | |
2712 | if (ret < 0) | |
2713 | goto out; | |
2714 | ||
1f4692da | 2715 | ret = gen_unique_name(sctx, ino, gen, p); |
31db9f7c AB |
2716 | if (ret < 0) |
2717 | goto out; | |
2718 | ||
2719 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
1f4692da | 2720 | TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); |
31db9f7c AB |
2721 | |
2722 | if (S_ISLNK(mode)) { | |
2723 | fs_path_reset(p); | |
924794c9 | 2724 | ret = read_symlink(sctx->send_root, ino, p); |
31db9f7c AB |
2725 | if (ret < 0) |
2726 | goto out; | |
2727 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); | |
2728 | } else if (S_ISCHR(mode) || S_ISBLK(mode) || | |
2729 | S_ISFIFO(mode) || S_ISSOCK(mode)) { | |
d79e5043 AJ |
2730 | TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev)); |
2731 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode); | |
31db9f7c AB |
2732 | } |
2733 | ||
2734 | ret = send_cmd(sctx); | |
2735 | if (ret < 0) | |
2736 | goto out; | |
2737 | ||
2738 | ||
2739 | tlv_put_failure: | |
2740 | out: | |
924794c9 | 2741 | fs_path_free(p); |
31db9f7c AB |
2742 | return ret; |
2743 | } | |
2744 | ||
1f4692da AB |
2745 | /* |
2746 | * We need some special handling for inodes that get processed before the parent | |
2747 | * directory got created. See process_recorded_refs for details. | |
2748 | * This function does the check if we already created the dir out of order. | |
2749 | */ | |
2750 | static int did_create_dir(struct send_ctx *sctx, u64 dir) | |
2751 | { | |
2752 | int ret = 0; | |
6dcee260 | 2753 | int iter_ret = 0; |
1f4692da AB |
2754 | struct btrfs_path *path = NULL; |
2755 | struct btrfs_key key; | |
2756 | struct btrfs_key found_key; | |
2757 | struct btrfs_key di_key; | |
1f4692da | 2758 | struct btrfs_dir_item *di; |
1f4692da AB |
2759 | |
2760 | path = alloc_path_for_send(); | |
6dcee260 GN |
2761 | if (!path) |
2762 | return -ENOMEM; | |
1f4692da AB |
2763 | |
2764 | key.objectid = dir; | |
2765 | key.type = BTRFS_DIR_INDEX_KEY; | |
2766 | key.offset = 0; | |
dff6d0ad | 2767 | |
6dcee260 GN |
2768 | btrfs_for_each_slot(sctx->send_root, &key, &found_key, path, iter_ret) { |
2769 | struct extent_buffer *eb = path->nodes[0]; | |
dff6d0ad | 2770 | |
dff6d0ad | 2771 | if (found_key.objectid != key.objectid || |
1f4692da AB |
2772 | found_key.type != key.type) { |
2773 | ret = 0; | |
6dcee260 | 2774 | break; |
1f4692da AB |
2775 | } |
2776 | ||
6dcee260 | 2777 | di = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dir_item); |
1f4692da AB |
2778 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
2779 | ||
a0525414 JB |
2780 | if (di_key.type != BTRFS_ROOT_ITEM_KEY && |
2781 | di_key.objectid < sctx->send_progress) { | |
1f4692da | 2782 | ret = 1; |
6dcee260 | 2783 | break; |
1f4692da | 2784 | } |
1f4692da | 2785 | } |
6dcee260 GN |
2786 | /* Catch error found during iteration */ |
2787 | if (iter_ret < 0) | |
2788 | ret = iter_ret; | |
1f4692da | 2789 | |
1f4692da AB |
2790 | btrfs_free_path(path); |
2791 | return ret; | |
2792 | } | |
2793 | ||
2794 | /* | |
2795 | * Only creates the inode if it is: | |
2796 | * 1. Not a directory | |
2797 | * 2. Or a directory which was not created already due to out of order | |
2798 | * directories. See did_create_dir and process_recorded_refs for details. | |
2799 | */ | |
2800 | static int send_create_inode_if_needed(struct send_ctx *sctx) | |
2801 | { | |
2802 | int ret; | |
2803 | ||
2804 | if (S_ISDIR(sctx->cur_inode_mode)) { | |
2805 | ret = did_create_dir(sctx, sctx->cur_ino); | |
2806 | if (ret < 0) | |
0e3dd5bc MPS |
2807 | return ret; |
2808 | else if (ret > 0) | |
2809 | return 0; | |
1f4692da AB |
2810 | } |
2811 | ||
0e3dd5bc | 2812 | return send_create_inode(sctx, sctx->cur_ino); |
1f4692da AB |
2813 | } |
2814 | ||
31db9f7c AB |
2815 | struct recorded_ref { |
2816 | struct list_head list; | |
31db9f7c AB |
2817 | char *name; |
2818 | struct fs_path *full_path; | |
2819 | u64 dir; | |
2820 | u64 dir_gen; | |
31db9f7c | 2821 | int name_len; |
3aa5bd36 BC |
2822 | struct rb_node node; |
2823 | struct rb_root *root; | |
31db9f7c AB |
2824 | }; |
2825 | ||
71ecfc13 BC |
2826 | static struct recorded_ref *recorded_ref_alloc(void) |
2827 | { | |
2828 | struct recorded_ref *ref; | |
2829 | ||
2830 | ref = kzalloc(sizeof(*ref), GFP_KERNEL); | |
2831 | if (!ref) | |
2832 | return NULL; | |
3aa5bd36 | 2833 | RB_CLEAR_NODE(&ref->node); |
71ecfc13 BC |
2834 | INIT_LIST_HEAD(&ref->list); |
2835 | return ref; | |
2836 | } | |
2837 | ||
2838 | static void recorded_ref_free(struct recorded_ref *ref) | |
2839 | { | |
2840 | if (!ref) | |
2841 | return; | |
3aa5bd36 BC |
2842 | if (!RB_EMPTY_NODE(&ref->node)) |
2843 | rb_erase(&ref->node, ref->root); | |
71ecfc13 BC |
2844 | list_del(&ref->list); |
2845 | fs_path_free(ref->full_path); | |
2846 | kfree(ref); | |
2847 | } | |
2848 | ||
fdb13889 FM |
2849 | static void set_ref_path(struct recorded_ref *ref, struct fs_path *path) |
2850 | { | |
2851 | ref->full_path = path; | |
2852 | ref->name = (char *)kbasename(ref->full_path->start); | |
2853 | ref->name_len = ref->full_path->end - ref->name; | |
2854 | } | |
2855 | ||
ba5e8f2e JB |
2856 | static int dup_ref(struct recorded_ref *ref, struct list_head *list) |
2857 | { | |
2858 | struct recorded_ref *new; | |
2859 | ||
71ecfc13 | 2860 | new = recorded_ref_alloc(); |
ba5e8f2e JB |
2861 | if (!new) |
2862 | return -ENOMEM; | |
2863 | ||
2864 | new->dir = ref->dir; | |
2865 | new->dir_gen = ref->dir_gen; | |
ba5e8f2e JB |
2866 | list_add_tail(&new->list, list); |
2867 | return 0; | |
2868 | } | |
2869 | ||
924794c9 | 2870 | static void __free_recorded_refs(struct list_head *head) |
31db9f7c AB |
2871 | { |
2872 | struct recorded_ref *cur; | |
31db9f7c | 2873 | |
e938c8ad AB |
2874 | while (!list_empty(head)) { |
2875 | cur = list_entry(head->next, struct recorded_ref, list); | |
71ecfc13 | 2876 | recorded_ref_free(cur); |
31db9f7c | 2877 | } |
31db9f7c AB |
2878 | } |
2879 | ||
2880 | static void free_recorded_refs(struct send_ctx *sctx) | |
2881 | { | |
924794c9 TI |
2882 | __free_recorded_refs(&sctx->new_refs); |
2883 | __free_recorded_refs(&sctx->deleted_refs); | |
31db9f7c AB |
2884 | } |
2885 | ||
2886 | /* | |
766702ef | 2887 | * Renames/moves a file/dir to its orphan name. Used when the first |
31db9f7c AB |
2888 | * ref of an unprocessed inode gets overwritten and for all non empty |
2889 | * directories. | |
2890 | */ | |
2891 | static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, | |
2892 | struct fs_path *path) | |
2893 | { | |
2894 | int ret; | |
2895 | struct fs_path *orphan; | |
2896 | ||
924794c9 | 2897 | orphan = fs_path_alloc(); |
31db9f7c AB |
2898 | if (!orphan) |
2899 | return -ENOMEM; | |
2900 | ||
2901 | ret = gen_unique_name(sctx, ino, gen, orphan); | |
2902 | if (ret < 0) | |
2903 | goto out; | |
2904 | ||
2905 | ret = send_rename(sctx, path, orphan); | |
2906 | ||
2907 | out: | |
924794c9 | 2908 | fs_path_free(orphan); |
31db9f7c AB |
2909 | return ret; |
2910 | } | |
2911 | ||
0b3f407e FM |
2912 | static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx, |
2913 | u64 dir_ino, u64 dir_gen) | |
9dc44214 FM |
2914 | { |
2915 | struct rb_node **p = &sctx->orphan_dirs.rb_node; | |
2916 | struct rb_node *parent = NULL; | |
2917 | struct orphan_dir_info *entry, *odi; | |
2918 | ||
9dc44214 FM |
2919 | while (*p) { |
2920 | parent = *p; | |
2921 | entry = rb_entry(parent, struct orphan_dir_info, node); | |
0b3f407e | 2922 | if (dir_ino < entry->ino) |
9dc44214 | 2923 | p = &(*p)->rb_left; |
0b3f407e | 2924 | else if (dir_ino > entry->ino) |
9dc44214 | 2925 | p = &(*p)->rb_right; |
0b3f407e FM |
2926 | else if (dir_gen < entry->gen) |
2927 | p = &(*p)->rb_left; | |
2928 | else if (dir_gen > entry->gen) | |
2929 | p = &(*p)->rb_right; | |
2930 | else | |
9dc44214 | 2931 | return entry; |
9dc44214 FM |
2932 | } |
2933 | ||
35c8eda1 RK |
2934 | odi = kmalloc(sizeof(*odi), GFP_KERNEL); |
2935 | if (!odi) | |
2936 | return ERR_PTR(-ENOMEM); | |
2937 | odi->ino = dir_ino; | |
0b3f407e | 2938 | odi->gen = dir_gen; |
0f96f517 | 2939 | odi->last_dir_index_offset = 0; |
35c8eda1 | 2940 | |
9dc44214 FM |
2941 | rb_link_node(&odi->node, parent, p); |
2942 | rb_insert_color(&odi->node, &sctx->orphan_dirs); | |
2943 | return odi; | |
2944 | } | |
2945 | ||
0b3f407e FM |
2946 | static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx, |
2947 | u64 dir_ino, u64 gen) | |
9dc44214 FM |
2948 | { |
2949 | struct rb_node *n = sctx->orphan_dirs.rb_node; | |
2950 | struct orphan_dir_info *entry; | |
2951 | ||
2952 | while (n) { | |
2953 | entry = rb_entry(n, struct orphan_dir_info, node); | |
2954 | if (dir_ino < entry->ino) | |
2955 | n = n->rb_left; | |
2956 | else if (dir_ino > entry->ino) | |
2957 | n = n->rb_right; | |
0b3f407e FM |
2958 | else if (gen < entry->gen) |
2959 | n = n->rb_left; | |
2960 | else if (gen > entry->gen) | |
2961 | n = n->rb_right; | |
9dc44214 FM |
2962 | else |
2963 | return entry; | |
2964 | } | |
2965 | return NULL; | |
2966 | } | |
2967 | ||
0b3f407e | 2968 | static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen) |
9dc44214 | 2969 | { |
0b3f407e | 2970 | struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen); |
9dc44214 FM |
2971 | |
2972 | return odi != NULL; | |
2973 | } | |
2974 | ||
2975 | static void free_orphan_dir_info(struct send_ctx *sctx, | |
2976 | struct orphan_dir_info *odi) | |
2977 | { | |
2978 | if (!odi) | |
2979 | return; | |
2980 | rb_erase(&odi->node, &sctx->orphan_dirs); | |
2981 | kfree(odi); | |
2982 | } | |
2983 | ||
31db9f7c AB |
2984 | /* |
2985 | * Returns 1 if a directory can be removed at this point in time. | |
2986 | * We check this by iterating all dir items and checking if the inode behind | |
2987 | * the dir item was already processed. | |
2988 | */ | |
9dc44214 FM |
2989 | static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
2990 | u64 send_progress) | |
31db9f7c AB |
2991 | { |
2992 | int ret = 0; | |
18f80f1f | 2993 | int iter_ret = 0; |
31db9f7c AB |
2994 | struct btrfs_root *root = sctx->parent_root; |
2995 | struct btrfs_path *path; | |
2996 | struct btrfs_key key; | |
2997 | struct btrfs_key found_key; | |
2998 | struct btrfs_key loc; | |
2999 | struct btrfs_dir_item *di; | |
0f96f517 | 3000 | struct orphan_dir_info *odi = NULL; |
31db9f7c | 3001 | |
6d85ed05 AB |
3002 | /* |
3003 | * Don't try to rmdir the top/root subvolume dir. | |
3004 | */ | |
3005 | if (dir == BTRFS_FIRST_FREE_OBJECTID) | |
3006 | return 0; | |
3007 | ||
31db9f7c AB |
3008 | path = alloc_path_for_send(); |
3009 | if (!path) | |
3010 | return -ENOMEM; | |
3011 | ||
3012 | key.objectid = dir; | |
3013 | key.type = BTRFS_DIR_INDEX_KEY; | |
3014 | key.offset = 0; | |
0f96f517 | 3015 | |
0b3f407e | 3016 | odi = get_orphan_dir_info(sctx, dir, dir_gen); |
0f96f517 RK |
3017 | if (odi) |
3018 | key.offset = odi->last_dir_index_offset; | |
3019 | ||
18f80f1f | 3020 | btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) { |
9dc44214 FM |
3021 | struct waiting_dir_move *dm; |
3022 | ||
dff6d0ad FDBM |
3023 | if (found_key.objectid != key.objectid || |
3024 | found_key.type != key.type) | |
31db9f7c | 3025 | break; |
31db9f7c AB |
3026 | |
3027 | di = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3028 | struct btrfs_dir_item); | |
3029 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc); | |
3030 | ||
9dc44214 FM |
3031 | dm = get_waiting_dir_move(sctx, loc.objectid); |
3032 | if (dm) { | |
0b3f407e | 3033 | odi = add_orphan_dir_info(sctx, dir, dir_gen); |
9dc44214 FM |
3034 | if (IS_ERR(odi)) { |
3035 | ret = PTR_ERR(odi); | |
3036 | goto out; | |
3037 | } | |
3038 | odi->gen = dir_gen; | |
0f96f517 | 3039 | odi->last_dir_index_offset = found_key.offset; |
9dc44214 | 3040 | dm->rmdir_ino = dir; |
0b3f407e | 3041 | dm->rmdir_gen = dir_gen; |
9dc44214 FM |
3042 | ret = 0; |
3043 | goto out; | |
3044 | } | |
3045 | ||
31db9f7c | 3046 | if (loc.objectid > send_progress) { |
0b3f407e | 3047 | odi = add_orphan_dir_info(sctx, dir, dir_gen); |
0f96f517 RK |
3048 | if (IS_ERR(odi)) { |
3049 | ret = PTR_ERR(odi); | |
3050 | goto out; | |
3051 | } | |
3052 | odi->gen = dir_gen; | |
3053 | odi->last_dir_index_offset = found_key.offset; | |
31db9f7c AB |
3054 | ret = 0; |
3055 | goto out; | |
3056 | } | |
18f80f1f GN |
3057 | } |
3058 | if (iter_ret < 0) { | |
3059 | ret = iter_ret; | |
3060 | goto out; | |
31db9f7c | 3061 | } |
0f96f517 | 3062 | free_orphan_dir_info(sctx, odi); |
31db9f7c AB |
3063 | |
3064 | ret = 1; | |
3065 | ||
3066 | out: | |
3067 | btrfs_free_path(path); | |
3068 | return ret; | |
3069 | } | |
3070 | ||
9f03740a FDBM |
3071 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino) |
3072 | { | |
9dc44214 | 3073 | struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino); |
9f03740a | 3074 | |
9dc44214 | 3075 | return entry != NULL; |
9f03740a FDBM |
3076 | } |
3077 | ||
8b191a68 | 3078 | static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized) |
9f03740a FDBM |
3079 | { |
3080 | struct rb_node **p = &sctx->waiting_dir_moves.rb_node; | |
3081 | struct rb_node *parent = NULL; | |
3082 | struct waiting_dir_move *entry, *dm; | |
3083 | ||
e780b0d1 | 3084 | dm = kmalloc(sizeof(*dm), GFP_KERNEL); |
9f03740a FDBM |
3085 | if (!dm) |
3086 | return -ENOMEM; | |
3087 | dm->ino = ino; | |
9dc44214 | 3088 | dm->rmdir_ino = 0; |
0b3f407e | 3089 | dm->rmdir_gen = 0; |
8b191a68 | 3090 | dm->orphanized = orphanized; |
9f03740a FDBM |
3091 | |
3092 | while (*p) { | |
3093 | parent = *p; | |
3094 | entry = rb_entry(parent, struct waiting_dir_move, node); | |
3095 | if (ino < entry->ino) { | |
3096 | p = &(*p)->rb_left; | |
3097 | } else if (ino > entry->ino) { | |
3098 | p = &(*p)->rb_right; | |
3099 | } else { | |
3100 | kfree(dm); | |
3101 | return -EEXIST; | |
3102 | } | |
3103 | } | |
3104 | ||
3105 | rb_link_node(&dm->node, parent, p); | |
3106 | rb_insert_color(&dm->node, &sctx->waiting_dir_moves); | |
3107 | return 0; | |
3108 | } | |
3109 | ||
9dc44214 FM |
3110 | static struct waiting_dir_move * |
3111 | get_waiting_dir_move(struct send_ctx *sctx, u64 ino) | |
9f03740a FDBM |
3112 | { |
3113 | struct rb_node *n = sctx->waiting_dir_moves.rb_node; | |
3114 | struct waiting_dir_move *entry; | |
3115 | ||
3116 | while (n) { | |
3117 | entry = rb_entry(n, struct waiting_dir_move, node); | |
9dc44214 | 3118 | if (ino < entry->ino) |
9f03740a | 3119 | n = n->rb_left; |
9dc44214 | 3120 | else if (ino > entry->ino) |
9f03740a | 3121 | n = n->rb_right; |
9dc44214 FM |
3122 | else |
3123 | return entry; | |
9f03740a | 3124 | } |
9dc44214 FM |
3125 | return NULL; |
3126 | } | |
3127 | ||
3128 | static void free_waiting_dir_move(struct send_ctx *sctx, | |
3129 | struct waiting_dir_move *dm) | |
3130 | { | |
3131 | if (!dm) | |
3132 | return; | |
3133 | rb_erase(&dm->node, &sctx->waiting_dir_moves); | |
3134 | kfree(dm); | |
9f03740a FDBM |
3135 | } |
3136 | ||
bfa7e1f8 FM |
3137 | static int add_pending_dir_move(struct send_ctx *sctx, |
3138 | u64 ino, | |
3139 | u64 ino_gen, | |
f959492f FM |
3140 | u64 parent_ino, |
3141 | struct list_head *new_refs, | |
84471e24 FM |
3142 | struct list_head *deleted_refs, |
3143 | const bool is_orphan) | |
9f03740a FDBM |
3144 | { |
3145 | struct rb_node **p = &sctx->pending_dir_moves.rb_node; | |
3146 | struct rb_node *parent = NULL; | |
73b802f4 | 3147 | struct pending_dir_move *entry = NULL, *pm; |
9f03740a FDBM |
3148 | struct recorded_ref *cur; |
3149 | int exists = 0; | |
3150 | int ret; | |
3151 | ||
e780b0d1 | 3152 | pm = kmalloc(sizeof(*pm), GFP_KERNEL); |
9f03740a FDBM |
3153 | if (!pm) |
3154 | return -ENOMEM; | |
3155 | pm->parent_ino = parent_ino; | |
bfa7e1f8 FM |
3156 | pm->ino = ino; |
3157 | pm->gen = ino_gen; | |
9f03740a FDBM |
3158 | INIT_LIST_HEAD(&pm->list); |
3159 | INIT_LIST_HEAD(&pm->update_refs); | |
3160 | RB_CLEAR_NODE(&pm->node); | |
3161 | ||
3162 | while (*p) { | |
3163 | parent = *p; | |
3164 | entry = rb_entry(parent, struct pending_dir_move, node); | |
3165 | if (parent_ino < entry->parent_ino) { | |
3166 | p = &(*p)->rb_left; | |
3167 | } else if (parent_ino > entry->parent_ino) { | |
3168 | p = &(*p)->rb_right; | |
3169 | } else { | |
3170 | exists = 1; | |
3171 | break; | |
3172 | } | |
3173 | } | |
3174 | ||
f959492f | 3175 | list_for_each_entry(cur, deleted_refs, list) { |
9f03740a FDBM |
3176 | ret = dup_ref(cur, &pm->update_refs); |
3177 | if (ret < 0) | |
3178 | goto out; | |
3179 | } | |
f959492f | 3180 | list_for_each_entry(cur, new_refs, list) { |
9f03740a FDBM |
3181 | ret = dup_ref(cur, &pm->update_refs); |
3182 | if (ret < 0) | |
3183 | goto out; | |
3184 | } | |
3185 | ||
8b191a68 | 3186 | ret = add_waiting_dir_move(sctx, pm->ino, is_orphan); |
9f03740a FDBM |
3187 | if (ret) |
3188 | goto out; | |
3189 | ||
3190 | if (exists) { | |
3191 | list_add_tail(&pm->list, &entry->list); | |
3192 | } else { | |
3193 | rb_link_node(&pm->node, parent, p); | |
3194 | rb_insert_color(&pm->node, &sctx->pending_dir_moves); | |
3195 | } | |
3196 | ret = 0; | |
3197 | out: | |
3198 | if (ret) { | |
3199 | __free_recorded_refs(&pm->update_refs); | |
3200 | kfree(pm); | |
3201 | } | |
3202 | return ret; | |
3203 | } | |
3204 | ||
3205 | static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx, | |
3206 | u64 parent_ino) | |
3207 | { | |
3208 | struct rb_node *n = sctx->pending_dir_moves.rb_node; | |
3209 | struct pending_dir_move *entry; | |
3210 | ||
3211 | while (n) { | |
3212 | entry = rb_entry(n, struct pending_dir_move, node); | |
3213 | if (parent_ino < entry->parent_ino) | |
3214 | n = n->rb_left; | |
3215 | else if (parent_ino > entry->parent_ino) | |
3216 | n = n->rb_right; | |
3217 | else | |
3218 | return entry; | |
3219 | } | |
3220 | return NULL; | |
3221 | } | |
3222 | ||
801bec36 RK |
3223 | static int path_loop(struct send_ctx *sctx, struct fs_path *name, |
3224 | u64 ino, u64 gen, u64 *ancestor_ino) | |
3225 | { | |
3226 | int ret = 0; | |
3227 | u64 parent_inode = 0; | |
3228 | u64 parent_gen = 0; | |
3229 | u64 start_ino = ino; | |
3230 | ||
3231 | *ancestor_ino = 0; | |
3232 | while (ino != BTRFS_FIRST_FREE_OBJECTID) { | |
3233 | fs_path_reset(name); | |
3234 | ||
0b3f407e | 3235 | if (is_waiting_for_rm(sctx, ino, gen)) |
801bec36 RK |
3236 | break; |
3237 | if (is_waiting_for_move(sctx, ino)) { | |
3238 | if (*ancestor_ino == 0) | |
3239 | *ancestor_ino = ino; | |
3240 | ret = get_first_ref(sctx->parent_root, ino, | |
3241 | &parent_inode, &parent_gen, name); | |
3242 | } else { | |
3243 | ret = __get_cur_name_and_parent(sctx, ino, gen, | |
3244 | &parent_inode, | |
3245 | &parent_gen, name); | |
3246 | if (ret > 0) { | |
3247 | ret = 0; | |
3248 | break; | |
3249 | } | |
3250 | } | |
3251 | if (ret < 0) | |
3252 | break; | |
3253 | if (parent_inode == start_ino) { | |
3254 | ret = 1; | |
3255 | if (*ancestor_ino == 0) | |
3256 | *ancestor_ino = ino; | |
3257 | break; | |
3258 | } | |
3259 | ino = parent_inode; | |
3260 | gen = parent_gen; | |
3261 | } | |
3262 | return ret; | |
3263 | } | |
3264 | ||
9f03740a FDBM |
3265 | static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm) |
3266 | { | |
3267 | struct fs_path *from_path = NULL; | |
3268 | struct fs_path *to_path = NULL; | |
2b863a13 | 3269 | struct fs_path *name = NULL; |
9f03740a FDBM |
3270 | u64 orig_progress = sctx->send_progress; |
3271 | struct recorded_ref *cur; | |
2b863a13 | 3272 | u64 parent_ino, parent_gen; |
9dc44214 FM |
3273 | struct waiting_dir_move *dm = NULL; |
3274 | u64 rmdir_ino = 0; | |
0b3f407e | 3275 | u64 rmdir_gen; |
801bec36 RK |
3276 | u64 ancestor; |
3277 | bool is_orphan; | |
9f03740a FDBM |
3278 | int ret; |
3279 | ||
2b863a13 | 3280 | name = fs_path_alloc(); |
9f03740a | 3281 | from_path = fs_path_alloc(); |
2b863a13 FM |
3282 | if (!name || !from_path) { |
3283 | ret = -ENOMEM; | |
3284 | goto out; | |
3285 | } | |
9f03740a | 3286 | |
9dc44214 FM |
3287 | dm = get_waiting_dir_move(sctx, pm->ino); |
3288 | ASSERT(dm); | |
3289 | rmdir_ino = dm->rmdir_ino; | |
0b3f407e | 3290 | rmdir_gen = dm->rmdir_gen; |
801bec36 | 3291 | is_orphan = dm->orphanized; |
9dc44214 | 3292 | free_waiting_dir_move(sctx, dm); |
2b863a13 | 3293 | |
801bec36 | 3294 | if (is_orphan) { |
84471e24 FM |
3295 | ret = gen_unique_name(sctx, pm->ino, |
3296 | pm->gen, from_path); | |
3297 | } else { | |
3298 | ret = get_first_ref(sctx->parent_root, pm->ino, | |
3299 | &parent_ino, &parent_gen, name); | |
3300 | if (ret < 0) | |
3301 | goto out; | |
3302 | ret = get_cur_path(sctx, parent_ino, parent_gen, | |
3303 | from_path); | |
3304 | if (ret < 0) | |
3305 | goto out; | |
3306 | ret = fs_path_add_path(from_path, name); | |
3307 | } | |
c992ec94 FM |
3308 | if (ret < 0) |
3309 | goto out; | |
2b863a13 | 3310 | |
f959492f | 3311 | sctx->send_progress = sctx->cur_ino + 1; |
801bec36 | 3312 | ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor); |
7969e77a FM |
3313 | if (ret < 0) |
3314 | goto out; | |
801bec36 RK |
3315 | if (ret) { |
3316 | LIST_HEAD(deleted_refs); | |
3317 | ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID); | |
3318 | ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor, | |
3319 | &pm->update_refs, &deleted_refs, | |
3320 | is_orphan); | |
3321 | if (ret < 0) | |
3322 | goto out; | |
3323 | if (rmdir_ino) { | |
3324 | dm = get_waiting_dir_move(sctx, pm->ino); | |
3325 | ASSERT(dm); | |
3326 | dm->rmdir_ino = rmdir_ino; | |
0b3f407e | 3327 | dm->rmdir_gen = rmdir_gen; |
801bec36 RK |
3328 | } |
3329 | goto out; | |
3330 | } | |
c992ec94 FM |
3331 | fs_path_reset(name); |
3332 | to_path = name; | |
2b863a13 | 3333 | name = NULL; |
9f03740a FDBM |
3334 | ret = get_cur_path(sctx, pm->ino, pm->gen, to_path); |
3335 | if (ret < 0) | |
3336 | goto out; | |
3337 | ||
3338 | ret = send_rename(sctx, from_path, to_path); | |
3339 | if (ret < 0) | |
3340 | goto out; | |
3341 | ||
9dc44214 FM |
3342 | if (rmdir_ino) { |
3343 | struct orphan_dir_info *odi; | |
0f96f517 | 3344 | u64 gen; |
9dc44214 | 3345 | |
0b3f407e | 3346 | odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen); |
9dc44214 FM |
3347 | if (!odi) { |
3348 | /* already deleted */ | |
3349 | goto finish; | |
3350 | } | |
0f96f517 RK |
3351 | gen = odi->gen; |
3352 | ||
3353 | ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino); | |
9dc44214 FM |
3354 | if (ret < 0) |
3355 | goto out; | |
3356 | if (!ret) | |
3357 | goto finish; | |
3358 | ||
3359 | name = fs_path_alloc(); | |
3360 | if (!name) { | |
3361 | ret = -ENOMEM; | |
3362 | goto out; | |
3363 | } | |
0f96f517 | 3364 | ret = get_cur_path(sctx, rmdir_ino, gen, name); |
9dc44214 FM |
3365 | if (ret < 0) |
3366 | goto out; | |
3367 | ret = send_rmdir(sctx, name); | |
3368 | if (ret < 0) | |
3369 | goto out; | |
9dc44214 FM |
3370 | } |
3371 | ||
3372 | finish: | |
9f03740a FDBM |
3373 | ret = send_utimes(sctx, pm->ino, pm->gen); |
3374 | if (ret < 0) | |
3375 | goto out; | |
3376 | ||
3377 | /* | |
3378 | * After rename/move, need to update the utimes of both new parent(s) | |
3379 | * and old parent(s). | |
3380 | */ | |
3381 | list_for_each_entry(cur, &pm->update_refs, list) { | |
764433a1 RK |
3382 | /* |
3383 | * The parent inode might have been deleted in the send snapshot | |
3384 | */ | |
7e93f6dc | 3385 | ret = get_inode_info(sctx->send_root, cur->dir, NULL); |
764433a1 RK |
3386 | if (ret == -ENOENT) { |
3387 | ret = 0; | |
9dc44214 | 3388 | continue; |
764433a1 RK |
3389 | } |
3390 | if (ret < 0) | |
3391 | goto out; | |
3392 | ||
9f03740a FDBM |
3393 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); |
3394 | if (ret < 0) | |
3395 | goto out; | |
3396 | } | |
3397 | ||
3398 | out: | |
2b863a13 | 3399 | fs_path_free(name); |
9f03740a FDBM |
3400 | fs_path_free(from_path); |
3401 | fs_path_free(to_path); | |
3402 | sctx->send_progress = orig_progress; | |
3403 | ||
3404 | return ret; | |
3405 | } | |
3406 | ||
3407 | static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m) | |
3408 | { | |
3409 | if (!list_empty(&m->list)) | |
3410 | list_del(&m->list); | |
3411 | if (!RB_EMPTY_NODE(&m->node)) | |
3412 | rb_erase(&m->node, &sctx->pending_dir_moves); | |
3413 | __free_recorded_refs(&m->update_refs); | |
3414 | kfree(m); | |
3415 | } | |
3416 | ||
a4390aee RK |
3417 | static void tail_append_pending_moves(struct send_ctx *sctx, |
3418 | struct pending_dir_move *moves, | |
9f03740a FDBM |
3419 | struct list_head *stack) |
3420 | { | |
3421 | if (list_empty(&moves->list)) { | |
3422 | list_add_tail(&moves->list, stack); | |
3423 | } else { | |
3424 | LIST_HEAD(list); | |
3425 | list_splice_init(&moves->list, &list); | |
3426 | list_add_tail(&moves->list, stack); | |
3427 | list_splice_tail(&list, stack); | |
3428 | } | |
a4390aee RK |
3429 | if (!RB_EMPTY_NODE(&moves->node)) { |
3430 | rb_erase(&moves->node, &sctx->pending_dir_moves); | |
3431 | RB_CLEAR_NODE(&moves->node); | |
3432 | } | |
9f03740a FDBM |
3433 | } |
3434 | ||
3435 | static int apply_children_dir_moves(struct send_ctx *sctx) | |
3436 | { | |
3437 | struct pending_dir_move *pm; | |
3438 | struct list_head stack; | |
3439 | u64 parent_ino = sctx->cur_ino; | |
3440 | int ret = 0; | |
3441 | ||
3442 | pm = get_pending_dir_moves(sctx, parent_ino); | |
3443 | if (!pm) | |
3444 | return 0; | |
3445 | ||
3446 | INIT_LIST_HEAD(&stack); | |
a4390aee | 3447 | tail_append_pending_moves(sctx, pm, &stack); |
9f03740a FDBM |
3448 | |
3449 | while (!list_empty(&stack)) { | |
3450 | pm = list_first_entry(&stack, struct pending_dir_move, list); | |
3451 | parent_ino = pm->ino; | |
3452 | ret = apply_dir_move(sctx, pm); | |
3453 | free_pending_move(sctx, pm); | |
3454 | if (ret) | |
3455 | goto out; | |
3456 | pm = get_pending_dir_moves(sctx, parent_ino); | |
3457 | if (pm) | |
a4390aee | 3458 | tail_append_pending_moves(sctx, pm, &stack); |
9f03740a FDBM |
3459 | } |
3460 | return 0; | |
3461 | ||
3462 | out: | |
3463 | while (!list_empty(&stack)) { | |
3464 | pm = list_first_entry(&stack, struct pending_dir_move, list); | |
3465 | free_pending_move(sctx, pm); | |
3466 | } | |
3467 | return ret; | |
3468 | } | |
3469 | ||
84471e24 FM |
3470 | /* |
3471 | * We might need to delay a directory rename even when no ancestor directory | |
3472 | * (in the send root) with a higher inode number than ours (sctx->cur_ino) was | |
3473 | * renamed. This happens when we rename a directory to the old name (the name | |
3474 | * in the parent root) of some other unrelated directory that got its rename | |
3475 | * delayed due to some ancestor with higher number that got renamed. | |
3476 | * | |
3477 | * Example: | |
3478 | * | |
3479 | * Parent snapshot: | |
3480 | * . (ino 256) | |
3481 | * |---- a/ (ino 257) | |
3482 | * | |---- file (ino 260) | |
3483 | * | | |
3484 | * |---- b/ (ino 258) | |
3485 | * |---- c/ (ino 259) | |
3486 | * | |
3487 | * Send snapshot: | |
3488 | * . (ino 256) | |
3489 | * |---- a/ (ino 258) | |
3490 | * |---- x/ (ino 259) | |
3491 | * |---- y/ (ino 257) | |
3492 | * |----- file (ino 260) | |
3493 | * | |
3494 | * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257 | |
3495 | * from 'a' to 'x/y' happening first, which in turn depends on the rename of | |
3496 | * inode 259 from 'c' to 'x'. So the order of rename commands the send stream | |
3497 | * must issue is: | |
3498 | * | |
3499 | * 1 - rename 259 from 'c' to 'x' | |
3500 | * 2 - rename 257 from 'a' to 'x/y' | |
3501 | * 3 - rename 258 from 'b' to 'a' | |
3502 | * | |
3503 | * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can | |
3504 | * be done right away and < 0 on error. | |
3505 | */ | |
3506 | static int wait_for_dest_dir_move(struct send_ctx *sctx, | |
3507 | struct recorded_ref *parent_ref, | |
3508 | const bool is_orphan) | |
3509 | { | |
2ff7e61e | 3510 | struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info; |
84471e24 FM |
3511 | struct btrfs_path *path; |
3512 | struct btrfs_key key; | |
3513 | struct btrfs_key di_key; | |
3514 | struct btrfs_dir_item *di; | |
3515 | u64 left_gen; | |
3516 | u64 right_gen; | |
3517 | int ret = 0; | |
801bec36 | 3518 | struct waiting_dir_move *wdm; |
84471e24 FM |
3519 | |
3520 | if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) | |
3521 | return 0; | |
3522 | ||
3523 | path = alloc_path_for_send(); | |
3524 | if (!path) | |
3525 | return -ENOMEM; | |
3526 | ||
3527 | key.objectid = parent_ref->dir; | |
3528 | key.type = BTRFS_DIR_ITEM_KEY; | |
3529 | key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len); | |
3530 | ||
3531 | ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); | |
3532 | if (ret < 0) { | |
3533 | goto out; | |
3534 | } else if (ret > 0) { | |
3535 | ret = 0; | |
3536 | goto out; | |
3537 | } | |
3538 | ||
2ff7e61e JM |
3539 | di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name, |
3540 | parent_ref->name_len); | |
84471e24 FM |
3541 | if (!di) { |
3542 | ret = 0; | |
3543 | goto out; | |
3544 | } | |
3545 | /* | |
3546 | * di_key.objectid has the number of the inode that has a dentry in the | |
3547 | * parent directory with the same name that sctx->cur_ino is being | |
3548 | * renamed to. We need to check if that inode is in the send root as | |
3549 | * well and if it is currently marked as an inode with a pending rename, | |
3550 | * if it is, we need to delay the rename of sctx->cur_ino as well, so | |
3551 | * that it happens after that other inode is renamed. | |
3552 | */ | |
3553 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key); | |
3554 | if (di_key.type != BTRFS_INODE_ITEM_KEY) { | |
3555 | ret = 0; | |
3556 | goto out; | |
3557 | } | |
3558 | ||
7e93f6dc | 3559 | ret = get_inode_gen(sctx->parent_root, di_key.objectid, &left_gen); |
84471e24 FM |
3560 | if (ret < 0) |
3561 | goto out; | |
7e93f6dc | 3562 | ret = get_inode_gen(sctx->send_root, di_key.objectid, &right_gen); |
84471e24 FM |
3563 | if (ret < 0) { |
3564 | if (ret == -ENOENT) | |
3565 | ret = 0; | |
3566 | goto out; | |
3567 | } | |
3568 | ||
3569 | /* Different inode, no need to delay the rename of sctx->cur_ino */ | |
3570 | if (right_gen != left_gen) { | |
3571 | ret = 0; | |
3572 | goto out; | |
3573 | } | |
3574 | ||
801bec36 RK |
3575 | wdm = get_waiting_dir_move(sctx, di_key.objectid); |
3576 | if (wdm && !wdm->orphanized) { | |
84471e24 FM |
3577 | ret = add_pending_dir_move(sctx, |
3578 | sctx->cur_ino, | |
3579 | sctx->cur_inode_gen, | |
3580 | di_key.objectid, | |
3581 | &sctx->new_refs, | |
3582 | &sctx->deleted_refs, | |
3583 | is_orphan); | |
3584 | if (!ret) | |
3585 | ret = 1; | |
3586 | } | |
3587 | out: | |
3588 | btrfs_free_path(path); | |
3589 | return ret; | |
3590 | } | |
3591 | ||
80aa6027 | 3592 | /* |
ea37d599 FM |
3593 | * Check if inode ino2, or any of its ancestors, is inode ino1. |
3594 | * Return 1 if true, 0 if false and < 0 on error. | |
3595 | */ | |
3596 | static int check_ino_in_path(struct btrfs_root *root, | |
3597 | const u64 ino1, | |
3598 | const u64 ino1_gen, | |
3599 | const u64 ino2, | |
3600 | const u64 ino2_gen, | |
3601 | struct fs_path *fs_path) | |
3602 | { | |
3603 | u64 ino = ino2; | |
3604 | ||
3605 | if (ino1 == ino2) | |
3606 | return ino1_gen == ino2_gen; | |
3607 | ||
3608 | while (ino > BTRFS_FIRST_FREE_OBJECTID) { | |
3609 | u64 parent; | |
3610 | u64 parent_gen; | |
3611 | int ret; | |
3612 | ||
3613 | fs_path_reset(fs_path); | |
3614 | ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path); | |
3615 | if (ret < 0) | |
3616 | return ret; | |
3617 | if (parent == ino1) | |
3618 | return parent_gen == ino1_gen; | |
3619 | ino = parent; | |
3620 | } | |
3621 | return 0; | |
3622 | } | |
3623 | ||
3624 | /* | |
35a68080 | 3625 | * Check if inode ino1 is an ancestor of inode ino2 in the given root for any |
ea37d599 | 3626 | * possible path (in case ino2 is not a directory and has multiple hard links). |
80aa6027 FM |
3627 | * Return 1 if true, 0 if false and < 0 on error. |
3628 | */ | |
3629 | static int is_ancestor(struct btrfs_root *root, | |
3630 | const u64 ino1, | |
3631 | const u64 ino1_gen, | |
3632 | const u64 ino2, | |
3633 | struct fs_path *fs_path) | |
3634 | { | |
ea37d599 | 3635 | bool free_fs_path = false; |
72c3668f | 3636 | int ret = 0; |
35a68080 | 3637 | int iter_ret = 0; |
ea37d599 FM |
3638 | struct btrfs_path *path = NULL; |
3639 | struct btrfs_key key; | |
72c3668f FM |
3640 | |
3641 | if (!fs_path) { | |
3642 | fs_path = fs_path_alloc(); | |
3643 | if (!fs_path) | |
3644 | return -ENOMEM; | |
ea37d599 | 3645 | free_fs_path = true; |
72c3668f | 3646 | } |
80aa6027 | 3647 | |
ea37d599 FM |
3648 | path = alloc_path_for_send(); |
3649 | if (!path) { | |
3650 | ret = -ENOMEM; | |
3651 | goto out; | |
3652 | } | |
80aa6027 | 3653 | |
ea37d599 FM |
3654 | key.objectid = ino2; |
3655 | key.type = BTRFS_INODE_REF_KEY; | |
3656 | key.offset = 0; | |
3657 | ||
35a68080 | 3658 | btrfs_for_each_slot(root, &key, &key, path, iter_ret) { |
ea37d599 FM |
3659 | struct extent_buffer *leaf = path->nodes[0]; |
3660 | int slot = path->slots[0]; | |
3661 | u32 cur_offset = 0; | |
3662 | u32 item_size; | |
3663 | ||
ea37d599 FM |
3664 | if (key.objectid != ino2) |
3665 | break; | |
3666 | if (key.type != BTRFS_INODE_REF_KEY && | |
3667 | key.type != BTRFS_INODE_EXTREF_KEY) | |
3668 | break; | |
3669 | ||
3212fa14 | 3670 | item_size = btrfs_item_size(leaf, slot); |
ea37d599 FM |
3671 | while (cur_offset < item_size) { |
3672 | u64 parent; | |
3673 | u64 parent_gen; | |
3674 | ||
3675 | if (key.type == BTRFS_INODE_EXTREF_KEY) { | |
3676 | unsigned long ptr; | |
3677 | struct btrfs_inode_extref *extref; | |
3678 | ||
3679 | ptr = btrfs_item_ptr_offset(leaf, slot); | |
3680 | extref = (struct btrfs_inode_extref *) | |
3681 | (ptr + cur_offset); | |
3682 | parent = btrfs_inode_extref_parent(leaf, | |
3683 | extref); | |
3684 | cur_offset += sizeof(*extref); | |
3685 | cur_offset += btrfs_inode_extref_name_len(leaf, | |
3686 | extref); | |
3687 | } else { | |
3688 | parent = key.offset; | |
3689 | cur_offset = item_size; | |
3690 | } | |
3691 | ||
7e93f6dc | 3692 | ret = get_inode_gen(root, parent, &parent_gen); |
ea37d599 FM |
3693 | if (ret < 0) |
3694 | goto out; | |
3695 | ret = check_ino_in_path(root, ino1, ino1_gen, | |
3696 | parent, parent_gen, fs_path); | |
3697 | if (ret) | |
3698 | goto out; | |
80aa6027 | 3699 | } |
80aa6027 | 3700 | } |
ea37d599 | 3701 | ret = 0; |
35a68080 GN |
3702 | if (iter_ret < 0) |
3703 | ret = iter_ret; | |
3704 | ||
3705 | out: | |
ea37d599 FM |
3706 | btrfs_free_path(path); |
3707 | if (free_fs_path) | |
72c3668f FM |
3708 | fs_path_free(fs_path); |
3709 | return ret; | |
80aa6027 FM |
3710 | } |
3711 | ||
9f03740a | 3712 | static int wait_for_parent_move(struct send_ctx *sctx, |
8b191a68 FM |
3713 | struct recorded_ref *parent_ref, |
3714 | const bool is_orphan) | |
9f03740a | 3715 | { |
f959492f | 3716 | int ret = 0; |
9f03740a | 3717 | u64 ino = parent_ref->dir; |
fe9c798d | 3718 | u64 ino_gen = parent_ref->dir_gen; |
9f03740a | 3719 | u64 parent_ino_before, parent_ino_after; |
9f03740a FDBM |
3720 | struct fs_path *path_before = NULL; |
3721 | struct fs_path *path_after = NULL; | |
3722 | int len1, len2; | |
9f03740a FDBM |
3723 | |
3724 | path_after = fs_path_alloc(); | |
f959492f FM |
3725 | path_before = fs_path_alloc(); |
3726 | if (!path_after || !path_before) { | |
9f03740a FDBM |
3727 | ret = -ENOMEM; |
3728 | goto out; | |
3729 | } | |
3730 | ||
bfa7e1f8 | 3731 | /* |
f959492f FM |
3732 | * Our current directory inode may not yet be renamed/moved because some |
3733 | * ancestor (immediate or not) has to be renamed/moved first. So find if | |
3734 | * such ancestor exists and make sure our own rename/move happens after | |
80aa6027 FM |
3735 | * that ancestor is processed to avoid path build infinite loops (done |
3736 | * at get_cur_path()). | |
bfa7e1f8 | 3737 | */ |
f959492f | 3738 | while (ino > BTRFS_FIRST_FREE_OBJECTID) { |
fe9c798d FM |
3739 | u64 parent_ino_after_gen; |
3740 | ||
f959492f | 3741 | if (is_waiting_for_move(sctx, ino)) { |
80aa6027 FM |
3742 | /* |
3743 | * If the current inode is an ancestor of ino in the | |
3744 | * parent root, we need to delay the rename of the | |
3745 | * current inode, otherwise don't delayed the rename | |
3746 | * because we can end up with a circular dependency | |
3747 | * of renames, resulting in some directories never | |
3748 | * getting the respective rename operations issued in | |
3749 | * the send stream or getting into infinite path build | |
3750 | * loops. | |
3751 | */ | |
3752 | ret = is_ancestor(sctx->parent_root, | |
3753 | sctx->cur_ino, sctx->cur_inode_gen, | |
3754 | ino, path_before); | |
4122ea64 FM |
3755 | if (ret) |
3756 | break; | |
f959492f | 3757 | } |
bfa7e1f8 FM |
3758 | |
3759 | fs_path_reset(path_before); | |
3760 | fs_path_reset(path_after); | |
3761 | ||
3762 | ret = get_first_ref(sctx->send_root, ino, &parent_ino_after, | |
fe9c798d | 3763 | &parent_ino_after_gen, path_after); |
bfa7e1f8 FM |
3764 | if (ret < 0) |
3765 | goto out; | |
3766 | ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before, | |
3767 | NULL, path_before); | |
f959492f | 3768 | if (ret < 0 && ret != -ENOENT) { |
bfa7e1f8 | 3769 | goto out; |
f959492f | 3770 | } else if (ret == -ENOENT) { |
bf8e8ca6 | 3771 | ret = 0; |
f959492f | 3772 | break; |
bfa7e1f8 FM |
3773 | } |
3774 | ||
3775 | len1 = fs_path_len(path_before); | |
3776 | len2 = fs_path_len(path_after); | |
f959492f FM |
3777 | if (ino > sctx->cur_ino && |
3778 | (parent_ino_before != parent_ino_after || len1 != len2 || | |
3779 | memcmp(path_before->start, path_after->start, len1))) { | |
fe9c798d FM |
3780 | u64 parent_ino_gen; |
3781 | ||
7e93f6dc | 3782 | ret = get_inode_gen(sctx->parent_root, ino, &parent_ino_gen); |
fe9c798d FM |
3783 | if (ret < 0) |
3784 | goto out; | |
3785 | if (ino_gen == parent_ino_gen) { | |
3786 | ret = 1; | |
3787 | break; | |
3788 | } | |
bfa7e1f8 | 3789 | } |
bfa7e1f8 | 3790 | ino = parent_ino_after; |
fe9c798d | 3791 | ino_gen = parent_ino_after_gen; |
bfa7e1f8 FM |
3792 | } |
3793 | ||
9f03740a FDBM |
3794 | out: |
3795 | fs_path_free(path_before); | |
3796 | fs_path_free(path_after); | |
3797 | ||
f959492f FM |
3798 | if (ret == 1) { |
3799 | ret = add_pending_dir_move(sctx, | |
3800 | sctx->cur_ino, | |
3801 | sctx->cur_inode_gen, | |
3802 | ino, | |
3803 | &sctx->new_refs, | |
84471e24 | 3804 | &sctx->deleted_refs, |
8b191a68 | 3805 | is_orphan); |
f959492f FM |
3806 | if (!ret) |
3807 | ret = 1; | |
3808 | } | |
3809 | ||
9f03740a FDBM |
3810 | return ret; |
3811 | } | |
3812 | ||
f5962781 FM |
3813 | static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) |
3814 | { | |
3815 | int ret; | |
3816 | struct fs_path *new_path; | |
3817 | ||
3818 | /* | |
3819 | * Our reference's name member points to its full_path member string, so | |
3820 | * we use here a new path. | |
3821 | */ | |
3822 | new_path = fs_path_alloc(); | |
3823 | if (!new_path) | |
3824 | return -ENOMEM; | |
3825 | ||
3826 | ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path); | |
3827 | if (ret < 0) { | |
3828 | fs_path_free(new_path); | |
3829 | return ret; | |
3830 | } | |
3831 | ret = fs_path_add(new_path, ref->name, ref->name_len); | |
3832 | if (ret < 0) { | |
3833 | fs_path_free(new_path); | |
3834 | return ret; | |
3835 | } | |
3836 | ||
3837 | fs_path_free(ref->full_path); | |
3838 | set_ref_path(ref, new_path); | |
3839 | ||
3840 | return 0; | |
3841 | } | |
3842 | ||
9c2b4e03 FM |
3843 | /* |
3844 | * When processing the new references for an inode we may orphanize an existing | |
3845 | * directory inode because its old name conflicts with one of the new references | |
3846 | * of the current inode. Later, when processing another new reference of our | |
3847 | * inode, we might need to orphanize another inode, but the path we have in the | |
3848 | * reference reflects the pre-orphanization name of the directory we previously | |
3849 | * orphanized. For example: | |
3850 | * | |
3851 | * parent snapshot looks like: | |
3852 | * | |
3853 | * . (ino 256) | |
3854 | * |----- f1 (ino 257) | |
3855 | * |----- f2 (ino 258) | |
3856 | * |----- d1/ (ino 259) | |
3857 | * |----- d2/ (ino 260) | |
3858 | * | |
3859 | * send snapshot looks like: | |
3860 | * | |
3861 | * . (ino 256) | |
3862 | * |----- d1 (ino 258) | |
3863 | * |----- f2/ (ino 259) | |
3864 | * |----- f2_link/ (ino 260) | |
3865 | * | |----- f1 (ino 257) | |
3866 | * | | |
3867 | * |----- d2 (ino 258) | |
3868 | * | |
3869 | * When processing inode 257 we compute the name for inode 259 as "d1", and we | |
3870 | * cache it in the name cache. Later when we start processing inode 258, when | |
3871 | * collecting all its new references we set a full path of "d1/d2" for its new | |
3872 | * reference with name "d2". When we start processing the new references we | |
3873 | * start by processing the new reference with name "d1", and this results in | |
3874 | * orphanizing inode 259, since its old reference causes a conflict. Then we | |
3875 | * move on the next new reference, with name "d2", and we find out we must | |
3876 | * orphanize inode 260, as its old reference conflicts with ours - but for the | |
3877 | * orphanization we use a source path corresponding to the path we stored in the | |
3878 | * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the | |
3879 | * receiver fail since the path component "d1/" no longer exists, it was renamed | |
3880 | * to "o259-6-0/" when processing the previous new reference. So in this case we | |
3881 | * must recompute the path in the new reference and use it for the new | |
3882 | * orphanization operation. | |
3883 | */ | |
3884 | static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) | |
3885 | { | |
3886 | char *name; | |
3887 | int ret; | |
3888 | ||
3889 | name = kmemdup(ref->name, ref->name_len, GFP_KERNEL); | |
3890 | if (!name) | |
3891 | return -ENOMEM; | |
3892 | ||
3893 | fs_path_reset(ref->full_path); | |
3894 | ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path); | |
3895 | if (ret < 0) | |
3896 | goto out; | |
3897 | ||
3898 | ret = fs_path_add(ref->full_path, name, ref->name_len); | |
3899 | if (ret < 0) | |
3900 | goto out; | |
3901 | ||
3902 | /* Update the reference's base name pointer. */ | |
3903 | set_ref_path(ref, ref->full_path); | |
3904 | out: | |
3905 | kfree(name); | |
3906 | return ret; | |
3907 | } | |
3908 | ||
31db9f7c AB |
3909 | /* |
3910 | * This does all the move/link/unlink/rmdir magic. | |
3911 | */ | |
9f03740a | 3912 | static int process_recorded_refs(struct send_ctx *sctx, int *pending_move) |
31db9f7c | 3913 | { |
04ab956e | 3914 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
3915 | int ret = 0; |
3916 | struct recorded_ref *cur; | |
1f4692da | 3917 | struct recorded_ref *cur2; |
ba5e8f2e | 3918 | struct list_head check_dirs; |
31db9f7c | 3919 | struct fs_path *valid_path = NULL; |
b24baf69 | 3920 | u64 ow_inode = 0; |
31db9f7c | 3921 | u64 ow_gen; |
f5962781 | 3922 | u64 ow_mode; |
31db9f7c AB |
3923 | int did_overwrite = 0; |
3924 | int is_orphan = 0; | |
29d6d30f | 3925 | u64 last_dir_ino_rm = 0; |
84471e24 | 3926 | bool can_rename = true; |
f5962781 | 3927 | bool orphanized_dir = false; |
fdb13889 | 3928 | bool orphanized_ancestor = false; |
31db9f7c | 3929 | |
04ab956e | 3930 | btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino); |
31db9f7c | 3931 | |
6d85ed05 AB |
3932 | /* |
3933 | * This should never happen as the root dir always has the same ref | |
3934 | * which is always '..' | |
3935 | */ | |
3936 | BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID); | |
ba5e8f2e | 3937 | INIT_LIST_HEAD(&check_dirs); |
6d85ed05 | 3938 | |
924794c9 | 3939 | valid_path = fs_path_alloc(); |
31db9f7c AB |
3940 | if (!valid_path) { |
3941 | ret = -ENOMEM; | |
3942 | goto out; | |
3943 | } | |
3944 | ||
31db9f7c AB |
3945 | /* |
3946 | * First, check if the first ref of the current inode was overwritten | |
3947 | * before. If yes, we know that the current inode was already orphanized | |
3948 | * and thus use the orphan name. If not, we can use get_cur_path to | |
3949 | * get the path of the first ref as it would like while receiving at | |
3950 | * this point in time. | |
3951 | * New inodes are always orphan at the beginning, so force to use the | |
3952 | * orphan name in this case. | |
3953 | * The first ref is stored in valid_path and will be updated if it | |
3954 | * gets moved around. | |
3955 | */ | |
3956 | if (!sctx->cur_inode_new) { | |
3957 | ret = did_overwrite_first_ref(sctx, sctx->cur_ino, | |
3958 | sctx->cur_inode_gen); | |
3959 | if (ret < 0) | |
3960 | goto out; | |
3961 | if (ret) | |
3962 | did_overwrite = 1; | |
3963 | } | |
3964 | if (sctx->cur_inode_new || did_overwrite) { | |
3965 | ret = gen_unique_name(sctx, sctx->cur_ino, | |
3966 | sctx->cur_inode_gen, valid_path); | |
3967 | if (ret < 0) | |
3968 | goto out; | |
3969 | is_orphan = 1; | |
3970 | } else { | |
3971 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
3972 | valid_path); | |
3973 | if (ret < 0) | |
3974 | goto out; | |
3975 | } | |
3976 | ||
98272bb7 FM |
3977 | /* |
3978 | * Before doing any rename and link operations, do a first pass on the | |
3979 | * new references to orphanize any unprocessed inodes that may have a | |
3980 | * reference that conflicts with one of the new references of the current | |
3981 | * inode. This needs to happen first because a new reference may conflict | |
3982 | * with the old reference of a parent directory, so we must make sure | |
3983 | * that the path used for link and rename commands don't use an | |
3984 | * orphanized name when an ancestor was not yet orphanized. | |
3985 | * | |
3986 | * Example: | |
3987 | * | |
3988 | * Parent snapshot: | |
3989 | * | |
3990 | * . (ino 256) | |
3991 | * |----- testdir/ (ino 259) | |
3992 | * | |----- a (ino 257) | |
3993 | * | | |
3994 | * |----- b (ino 258) | |
3995 | * | |
3996 | * Send snapshot: | |
3997 | * | |
3998 | * . (ino 256) | |
3999 | * |----- testdir_2/ (ino 259) | |
4000 | * | |----- a (ino 260) | |
4001 | * | | |
4002 | * |----- testdir (ino 257) | |
4003 | * |----- b (ino 257) | |
4004 | * |----- b2 (ino 258) | |
4005 | * | |
4006 | * Processing the new reference for inode 257 with name "b" may happen | |
4007 | * before processing the new reference with name "testdir". If so, we | |
4008 | * must make sure that by the time we send a link command to create the | |
4009 | * hard link "b", inode 259 was already orphanized, since the generated | |
4010 | * path in "valid_path" already contains the orphanized name for 259. | |
4011 | * We are processing inode 257, so only later when processing 259 we do | |
4012 | * the rename operation to change its temporary (orphanized) name to | |
4013 | * "testdir_2". | |
4014 | */ | |
31db9f7c | 4015 | list_for_each_entry(cur, &sctx->new_refs, list) { |
1f4692da AB |
4016 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
4017 | if (ret < 0) | |
4018 | goto out; | |
98272bb7 FM |
4019 | if (ret == inode_state_will_create) |
4020 | continue; | |
1f4692da | 4021 | |
31db9f7c | 4022 | /* |
98272bb7 FM |
4023 | * Check if this new ref would overwrite the first ref of another |
4024 | * unprocessed inode. If yes, orphanize the overwritten inode. | |
4025 | * If we find an overwritten ref that is not the first ref, | |
4026 | * simply unlink it. | |
31db9f7c AB |
4027 | */ |
4028 | ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, | |
4029 | cur->name, cur->name_len, | |
f5962781 | 4030 | &ow_inode, &ow_gen, &ow_mode); |
31db9f7c AB |
4031 | if (ret < 0) |
4032 | goto out; | |
4033 | if (ret) { | |
924794c9 TI |
4034 | ret = is_first_ref(sctx->parent_root, |
4035 | ow_inode, cur->dir, cur->name, | |
4036 | cur->name_len); | |
31db9f7c AB |
4037 | if (ret < 0) |
4038 | goto out; | |
4039 | if (ret) { | |
8996a48c | 4040 | struct name_cache_entry *nce; |
801bec36 | 4041 | struct waiting_dir_move *wdm; |
8996a48c | 4042 | |
9c2b4e03 FM |
4043 | if (orphanized_dir) { |
4044 | ret = refresh_ref_path(sctx, cur); | |
4045 | if (ret < 0) | |
4046 | goto out; | |
4047 | } | |
4048 | ||
31db9f7c AB |
4049 | ret = orphanize_inode(sctx, ow_inode, ow_gen, |
4050 | cur->full_path); | |
4051 | if (ret < 0) | |
4052 | goto out; | |
f5962781 FM |
4053 | if (S_ISDIR(ow_mode)) |
4054 | orphanized_dir = true; | |
801bec36 RK |
4055 | |
4056 | /* | |
4057 | * If ow_inode has its rename operation delayed | |
4058 | * make sure that its orphanized name is used in | |
4059 | * the source path when performing its rename | |
4060 | * operation. | |
4061 | */ | |
4062 | if (is_waiting_for_move(sctx, ow_inode)) { | |
4063 | wdm = get_waiting_dir_move(sctx, | |
4064 | ow_inode); | |
4065 | ASSERT(wdm); | |
4066 | wdm->orphanized = true; | |
4067 | } | |
4068 | ||
8996a48c FM |
4069 | /* |
4070 | * Make sure we clear our orphanized inode's | |
4071 | * name from the name cache. This is because the | |
4072 | * inode ow_inode might be an ancestor of some | |
4073 | * other inode that will be orphanized as well | |
4074 | * later and has an inode number greater than | |
4075 | * sctx->send_progress. We need to prevent | |
4076 | * future name lookups from using the old name | |
4077 | * and get instead the orphan name. | |
4078 | */ | |
4079 | nce = name_cache_search(sctx, ow_inode, ow_gen); | |
4080 | if (nce) { | |
4081 | name_cache_delete(sctx, nce); | |
4082 | kfree(nce); | |
4083 | } | |
801bec36 RK |
4084 | |
4085 | /* | |
4086 | * ow_inode might currently be an ancestor of | |
4087 | * cur_ino, therefore compute valid_path (the | |
4088 | * current path of cur_ino) again because it | |
4089 | * might contain the pre-orphanization name of | |
4090 | * ow_inode, which is no longer valid. | |
4091 | */ | |
72c3668f FM |
4092 | ret = is_ancestor(sctx->parent_root, |
4093 | ow_inode, ow_gen, | |
4094 | sctx->cur_ino, NULL); | |
4095 | if (ret > 0) { | |
fdb13889 | 4096 | orphanized_ancestor = true; |
72c3668f FM |
4097 | fs_path_reset(valid_path); |
4098 | ret = get_cur_path(sctx, sctx->cur_ino, | |
4099 | sctx->cur_inode_gen, | |
4100 | valid_path); | |
4101 | } | |
801bec36 RK |
4102 | if (ret < 0) |
4103 | goto out; | |
31db9f7c | 4104 | } else { |
d8ac76cd FM |
4105 | /* |
4106 | * If we previously orphanized a directory that | |
4107 | * collided with a new reference that we already | |
4108 | * processed, recompute the current path because | |
4109 | * that directory may be part of the path. | |
4110 | */ | |
4111 | if (orphanized_dir) { | |
4112 | ret = refresh_ref_path(sctx, cur); | |
4113 | if (ret < 0) | |
4114 | goto out; | |
4115 | } | |
31db9f7c AB |
4116 | ret = send_unlink(sctx, cur->full_path); |
4117 | if (ret < 0) | |
4118 | goto out; | |
4119 | } | |
4120 | } | |
4121 | ||
98272bb7 FM |
4122 | } |
4123 | ||
4124 | list_for_each_entry(cur, &sctx->new_refs, list) { | |
4125 | /* | |
4126 | * We may have refs where the parent directory does not exist | |
4127 | * yet. This happens if the parent directories inum is higher | |
4128 | * than the current inum. To handle this case, we create the | |
4129 | * parent directory out of order. But we need to check if this | |
4130 | * did already happen before due to other refs in the same dir. | |
4131 | */ | |
4132 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); | |
4133 | if (ret < 0) | |
4134 | goto out; | |
4135 | if (ret == inode_state_will_create) { | |
4136 | ret = 0; | |
4137 | /* | |
4138 | * First check if any of the current inodes refs did | |
4139 | * already create the dir. | |
4140 | */ | |
4141 | list_for_each_entry(cur2, &sctx->new_refs, list) { | |
4142 | if (cur == cur2) | |
4143 | break; | |
4144 | if (cur2->dir == cur->dir) { | |
4145 | ret = 1; | |
4146 | break; | |
4147 | } | |
4148 | } | |
4149 | ||
4150 | /* | |
4151 | * If that did not happen, check if a previous inode | |
4152 | * did already create the dir. | |
4153 | */ | |
4154 | if (!ret) | |
4155 | ret = did_create_dir(sctx, cur->dir); | |
4156 | if (ret < 0) | |
4157 | goto out; | |
4158 | if (!ret) { | |
4159 | ret = send_create_inode(sctx, cur->dir); | |
4160 | if (ret < 0) | |
4161 | goto out; | |
4162 | } | |
4163 | } | |
4164 | ||
84471e24 FM |
4165 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) { |
4166 | ret = wait_for_dest_dir_move(sctx, cur, is_orphan); | |
4167 | if (ret < 0) | |
4168 | goto out; | |
4169 | if (ret == 1) { | |
4170 | can_rename = false; | |
4171 | *pending_move = 1; | |
4172 | } | |
4173 | } | |
4174 | ||
8b191a68 FM |
4175 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root && |
4176 | can_rename) { | |
4177 | ret = wait_for_parent_move(sctx, cur, is_orphan); | |
4178 | if (ret < 0) | |
4179 | goto out; | |
4180 | if (ret == 1) { | |
4181 | can_rename = false; | |
4182 | *pending_move = 1; | |
4183 | } | |
4184 | } | |
4185 | ||
31db9f7c AB |
4186 | /* |
4187 | * link/move the ref to the new place. If we have an orphan | |
4188 | * inode, move it and update valid_path. If not, link or move | |
4189 | * it depending on the inode mode. | |
4190 | */ | |
84471e24 | 4191 | if (is_orphan && can_rename) { |
31db9f7c AB |
4192 | ret = send_rename(sctx, valid_path, cur->full_path); |
4193 | if (ret < 0) | |
4194 | goto out; | |
4195 | is_orphan = 0; | |
4196 | ret = fs_path_copy(valid_path, cur->full_path); | |
4197 | if (ret < 0) | |
4198 | goto out; | |
84471e24 | 4199 | } else if (can_rename) { |
31db9f7c AB |
4200 | if (S_ISDIR(sctx->cur_inode_mode)) { |
4201 | /* | |
4202 | * Dirs can't be linked, so move it. For moved | |
4203 | * dirs, we always have one new and one deleted | |
4204 | * ref. The deleted ref is ignored later. | |
4205 | */ | |
8b191a68 FM |
4206 | ret = send_rename(sctx, valid_path, |
4207 | cur->full_path); | |
4208 | if (!ret) | |
4209 | ret = fs_path_copy(valid_path, | |
4210 | cur->full_path); | |
31db9f7c AB |
4211 | if (ret < 0) |
4212 | goto out; | |
4213 | } else { | |
f5962781 FM |
4214 | /* |
4215 | * We might have previously orphanized an inode | |
4216 | * which is an ancestor of our current inode, | |
4217 | * so our reference's full path, which was | |
4218 | * computed before any such orphanizations, must | |
4219 | * be updated. | |
4220 | */ | |
4221 | if (orphanized_dir) { | |
4222 | ret = update_ref_path(sctx, cur); | |
4223 | if (ret < 0) | |
4224 | goto out; | |
4225 | } | |
31db9f7c AB |
4226 | ret = send_link(sctx, cur->full_path, |
4227 | valid_path); | |
4228 | if (ret < 0) | |
4229 | goto out; | |
4230 | } | |
4231 | } | |
ba5e8f2e | 4232 | ret = dup_ref(cur, &check_dirs); |
31db9f7c AB |
4233 | if (ret < 0) |
4234 | goto out; | |
4235 | } | |
4236 | ||
4237 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { | |
4238 | /* | |
4239 | * Check if we can already rmdir the directory. If not, | |
4240 | * orphanize it. For every dir item inside that gets deleted | |
4241 | * later, we do this check again and rmdir it then if possible. | |
4242 | * See the use of check_dirs for more details. | |
4243 | */ | |
9dc44214 FM |
4244 | ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
4245 | sctx->cur_ino); | |
31db9f7c AB |
4246 | if (ret < 0) |
4247 | goto out; | |
4248 | if (ret) { | |
4249 | ret = send_rmdir(sctx, valid_path); | |
4250 | if (ret < 0) | |
4251 | goto out; | |
4252 | } else if (!is_orphan) { | |
4253 | ret = orphanize_inode(sctx, sctx->cur_ino, | |
4254 | sctx->cur_inode_gen, valid_path); | |
4255 | if (ret < 0) | |
4256 | goto out; | |
4257 | is_orphan = 1; | |
4258 | } | |
4259 | ||
4260 | list_for_each_entry(cur, &sctx->deleted_refs, list) { | |
ba5e8f2e | 4261 | ret = dup_ref(cur, &check_dirs); |
31db9f7c AB |
4262 | if (ret < 0) |
4263 | goto out; | |
4264 | } | |
ccf1626b AB |
4265 | } else if (S_ISDIR(sctx->cur_inode_mode) && |
4266 | !list_empty(&sctx->deleted_refs)) { | |
4267 | /* | |
4268 | * We have a moved dir. Add the old parent to check_dirs | |
4269 | */ | |
4270 | cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, | |
4271 | list); | |
ba5e8f2e | 4272 | ret = dup_ref(cur, &check_dirs); |
ccf1626b AB |
4273 | if (ret < 0) |
4274 | goto out; | |
31db9f7c AB |
4275 | } else if (!S_ISDIR(sctx->cur_inode_mode)) { |
4276 | /* | |
4277 | * We have a non dir inode. Go through all deleted refs and | |
4278 | * unlink them if they were not already overwritten by other | |
4279 | * inodes. | |
4280 | */ | |
4281 | list_for_each_entry(cur, &sctx->deleted_refs, list) { | |
4282 | ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, | |
4283 | sctx->cur_ino, sctx->cur_inode_gen, | |
4284 | cur->name, cur->name_len); | |
4285 | if (ret < 0) | |
4286 | goto out; | |
4287 | if (!ret) { | |
fdb13889 FM |
4288 | /* |
4289 | * If we orphanized any ancestor before, we need | |
4290 | * to recompute the full path for deleted names, | |
4291 | * since any such path was computed before we | |
4292 | * processed any references and orphanized any | |
4293 | * ancestor inode. | |
4294 | */ | |
4295 | if (orphanized_ancestor) { | |
f5962781 FM |
4296 | ret = update_ref_path(sctx, cur); |
4297 | if (ret < 0) | |
fdb13889 | 4298 | goto out; |
fdb13889 | 4299 | } |
1f4692da AB |
4300 | ret = send_unlink(sctx, cur->full_path); |
4301 | if (ret < 0) | |
4302 | goto out; | |
31db9f7c | 4303 | } |
ba5e8f2e | 4304 | ret = dup_ref(cur, &check_dirs); |
31db9f7c AB |
4305 | if (ret < 0) |
4306 | goto out; | |
4307 | } | |
31db9f7c AB |
4308 | /* |
4309 | * If the inode is still orphan, unlink the orphan. This may | |
4310 | * happen when a previous inode did overwrite the first ref | |
4311 | * of this inode and no new refs were added for the current | |
766702ef AB |
4312 | * inode. Unlinking does not mean that the inode is deleted in |
4313 | * all cases. There may still be links to this inode in other | |
4314 | * places. | |
31db9f7c | 4315 | */ |
1f4692da | 4316 | if (is_orphan) { |
31db9f7c AB |
4317 | ret = send_unlink(sctx, valid_path); |
4318 | if (ret < 0) | |
4319 | goto out; | |
4320 | } | |
4321 | } | |
4322 | ||
4323 | /* | |
4324 | * We did collect all parent dirs where cur_inode was once located. We | |
4325 | * now go through all these dirs and check if they are pending for | |
4326 | * deletion and if it's finally possible to perform the rmdir now. | |
4327 | * We also update the inode stats of the parent dirs here. | |
4328 | */ | |
ba5e8f2e | 4329 | list_for_each_entry(cur, &check_dirs, list) { |
766702ef AB |
4330 | /* |
4331 | * In case we had refs into dirs that were not processed yet, | |
4332 | * we don't need to do the utime and rmdir logic for these dirs. | |
4333 | * The dir will be processed later. | |
4334 | */ | |
ba5e8f2e | 4335 | if (cur->dir > sctx->cur_ino) |
31db9f7c AB |
4336 | continue; |
4337 | ||
ba5e8f2e | 4338 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
31db9f7c AB |
4339 | if (ret < 0) |
4340 | goto out; | |
4341 | ||
4342 | if (ret == inode_state_did_create || | |
4343 | ret == inode_state_no_change) { | |
4344 | /* TODO delayed utimes */ | |
ba5e8f2e | 4345 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); |
31db9f7c AB |
4346 | if (ret < 0) |
4347 | goto out; | |
29d6d30f FM |
4348 | } else if (ret == inode_state_did_delete && |
4349 | cur->dir != last_dir_ino_rm) { | |
9dc44214 FM |
4350 | ret = can_rmdir(sctx, cur->dir, cur->dir_gen, |
4351 | sctx->cur_ino); | |
31db9f7c AB |
4352 | if (ret < 0) |
4353 | goto out; | |
4354 | if (ret) { | |
ba5e8f2e JB |
4355 | ret = get_cur_path(sctx, cur->dir, |
4356 | cur->dir_gen, valid_path); | |
31db9f7c AB |
4357 | if (ret < 0) |
4358 | goto out; | |
4359 | ret = send_rmdir(sctx, valid_path); | |
4360 | if (ret < 0) | |
4361 | goto out; | |
29d6d30f | 4362 | last_dir_ino_rm = cur->dir; |
31db9f7c AB |
4363 | } |
4364 | } | |
4365 | } | |
4366 | ||
31db9f7c AB |
4367 | ret = 0; |
4368 | ||
4369 | out: | |
ba5e8f2e | 4370 | __free_recorded_refs(&check_dirs); |
31db9f7c | 4371 | free_recorded_refs(sctx); |
924794c9 | 4372 | fs_path_free(valid_path); |
31db9f7c AB |
4373 | return ret; |
4374 | } | |
4375 | ||
3aa5bd36 BC |
4376 | static int rbtree_ref_comp(const void *k, const struct rb_node *node) |
4377 | { | |
4378 | const struct recorded_ref *data = k; | |
4379 | const struct recorded_ref *ref = rb_entry(node, struct recorded_ref, node); | |
4380 | int result; | |
4381 | ||
4382 | if (data->dir > ref->dir) | |
4383 | return 1; | |
4384 | if (data->dir < ref->dir) | |
4385 | return -1; | |
4386 | if (data->dir_gen > ref->dir_gen) | |
4387 | return 1; | |
4388 | if (data->dir_gen < ref->dir_gen) | |
4389 | return -1; | |
4390 | if (data->name_len > ref->name_len) | |
4391 | return 1; | |
4392 | if (data->name_len < ref->name_len) | |
4393 | return -1; | |
4394 | result = strcmp(data->name, ref->name); | |
4395 | if (result > 0) | |
4396 | return 1; | |
4397 | if (result < 0) | |
4398 | return -1; | |
4399 | return 0; | |
4400 | } | |
4401 | ||
4402 | static bool rbtree_ref_less(struct rb_node *node, const struct rb_node *parent) | |
4403 | { | |
4404 | const struct recorded_ref *entry = rb_entry(node, struct recorded_ref, node); | |
4405 | ||
4406 | return rbtree_ref_comp(entry, parent) < 0; | |
4407 | } | |
4408 | ||
4409 | static int record_ref_in_tree(struct rb_root *root, struct list_head *refs, | |
4410 | struct fs_path *name, u64 dir, u64 dir_gen, | |
4411 | struct send_ctx *sctx) | |
4412 | { | |
4413 | int ret = 0; | |
4414 | struct fs_path *path = NULL; | |
4415 | struct recorded_ref *ref = NULL; | |
4416 | ||
4417 | path = fs_path_alloc(); | |
4418 | if (!path) { | |
4419 | ret = -ENOMEM; | |
4420 | goto out; | |
4421 | } | |
4422 | ||
4423 | ref = recorded_ref_alloc(); | |
4424 | if (!ref) { | |
4425 | ret = -ENOMEM; | |
4426 | goto out; | |
4427 | } | |
4428 | ||
4429 | ret = get_cur_path(sctx, dir, dir_gen, path); | |
4430 | if (ret < 0) | |
4431 | goto out; | |
4432 | ret = fs_path_add_path(path, name); | |
4433 | if (ret < 0) | |
4434 | goto out; | |
4435 | ||
4436 | ref->dir = dir; | |
4437 | ref->dir_gen = dir_gen; | |
4438 | set_ref_path(ref, path); | |
4439 | list_add_tail(&ref->list, refs); | |
4440 | rb_add(&ref->node, root, rbtree_ref_less); | |
4441 | ref->root = root; | |
4442 | out: | |
4443 | if (ret) { | |
4444 | if (path && (!ref || !ref->full_path)) | |
4445 | fs_path_free(path); | |
4446 | recorded_ref_free(ref); | |
4447 | } | |
4448 | return ret; | |
4449 | } | |
4450 | ||
4451 | static int record_new_ref_if_needed(int num, u64 dir, int index, | |
4452 | struct fs_path *name, void *ctx) | |
4453 | { | |
4454 | int ret = 0; | |
4455 | struct send_ctx *sctx = ctx; | |
4456 | struct rb_node *node = NULL; | |
4457 | struct recorded_ref data; | |
4458 | struct recorded_ref *ref; | |
4459 | u64 dir_gen; | |
4460 | ||
7e93f6dc | 4461 | ret = get_inode_gen(sctx->send_root, dir, &dir_gen); |
3aa5bd36 BC |
4462 | if (ret < 0) |
4463 | goto out; | |
4464 | ||
4465 | data.dir = dir; | |
4466 | data.dir_gen = dir_gen; | |
4467 | set_ref_path(&data, name); | |
4468 | node = rb_find(&data, &sctx->rbtree_deleted_refs, rbtree_ref_comp); | |
4469 | if (node) { | |
4470 | ref = rb_entry(node, struct recorded_ref, node); | |
4471 | recorded_ref_free(ref); | |
4472 | } else { | |
4473 | ret = record_ref_in_tree(&sctx->rbtree_new_refs, | |
4474 | &sctx->new_refs, name, dir, dir_gen, | |
4475 | sctx); | |
4476 | } | |
4477 | out: | |
4478 | return ret; | |
4479 | } | |
4480 | ||
4481 | static int record_deleted_ref_if_needed(int num, u64 dir, int index, | |
4482 | struct fs_path *name, void *ctx) | |
4483 | { | |
4484 | int ret = 0; | |
4485 | struct send_ctx *sctx = ctx; | |
4486 | struct rb_node *node = NULL; | |
4487 | struct recorded_ref data; | |
4488 | struct recorded_ref *ref; | |
4489 | u64 dir_gen; | |
4490 | ||
7e93f6dc | 4491 | ret = get_inode_gen(sctx->parent_root, dir, &dir_gen); |
3aa5bd36 BC |
4492 | if (ret < 0) |
4493 | goto out; | |
4494 | ||
4495 | data.dir = dir; | |
4496 | data.dir_gen = dir_gen; | |
4497 | set_ref_path(&data, name); | |
4498 | node = rb_find(&data, &sctx->rbtree_new_refs, rbtree_ref_comp); | |
4499 | if (node) { | |
4500 | ref = rb_entry(node, struct recorded_ref, node); | |
4501 | recorded_ref_free(ref); | |
4502 | } else { | |
4503 | ret = record_ref_in_tree(&sctx->rbtree_deleted_refs, | |
4504 | &sctx->deleted_refs, name, dir, | |
4505 | dir_gen, sctx); | |
4506 | } | |
4507 | out: | |
4508 | return ret; | |
4509 | } | |
4510 | ||
31db9f7c AB |
4511 | static int record_new_ref(struct send_ctx *sctx) |
4512 | { | |
4513 | int ret; | |
4514 | ||
924794c9 | 4515 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
3aa5bd36 | 4516 | sctx->cmp_key, 0, record_new_ref_if_needed, sctx); |
31db9f7c AB |
4517 | if (ret < 0) |
4518 | goto out; | |
4519 | ret = 0; | |
4520 | ||
4521 | out: | |
4522 | return ret; | |
4523 | } | |
4524 | ||
4525 | static int record_deleted_ref(struct send_ctx *sctx) | |
4526 | { | |
4527 | int ret; | |
4528 | ||
924794c9 | 4529 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
3aa5bd36 BC |
4530 | sctx->cmp_key, 0, record_deleted_ref_if_needed, |
4531 | sctx); | |
31db9f7c AB |
4532 | if (ret < 0) |
4533 | goto out; | |
4534 | ret = 0; | |
4535 | ||
4536 | out: | |
4537 | return ret; | |
4538 | } | |
4539 | ||
31db9f7c AB |
4540 | static int record_changed_ref(struct send_ctx *sctx) |
4541 | { | |
4542 | int ret = 0; | |
4543 | ||
924794c9 | 4544 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
0d8869fb | 4545 | sctx->cmp_key, 0, record_new_ref_if_needed, sctx); |
31db9f7c AB |
4546 | if (ret < 0) |
4547 | goto out; | |
924794c9 | 4548 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
0d8869fb | 4549 | sctx->cmp_key, 0, record_deleted_ref_if_needed, sctx); |
31db9f7c AB |
4550 | if (ret < 0) |
4551 | goto out; | |
4552 | ret = 0; | |
4553 | ||
4554 | out: | |
4555 | return ret; | |
4556 | } | |
4557 | ||
4558 | /* | |
4559 | * Record and process all refs at once. Needed when an inode changes the | |
4560 | * generation number, which means that it was deleted and recreated. | |
4561 | */ | |
4562 | static int process_all_refs(struct send_ctx *sctx, | |
4563 | enum btrfs_compare_tree_result cmd) | |
4564 | { | |
649b9635 GN |
4565 | int ret = 0; |
4566 | int iter_ret = 0; | |
31db9f7c AB |
4567 | struct btrfs_root *root; |
4568 | struct btrfs_path *path; | |
4569 | struct btrfs_key key; | |
4570 | struct btrfs_key found_key; | |
31db9f7c | 4571 | iterate_inode_ref_t cb; |
9f03740a | 4572 | int pending_move = 0; |
31db9f7c AB |
4573 | |
4574 | path = alloc_path_for_send(); | |
4575 | if (!path) | |
4576 | return -ENOMEM; | |
4577 | ||
4578 | if (cmd == BTRFS_COMPARE_TREE_NEW) { | |
4579 | root = sctx->send_root; | |
0d8869fb | 4580 | cb = record_new_ref_if_needed; |
31db9f7c AB |
4581 | } else if (cmd == BTRFS_COMPARE_TREE_DELETED) { |
4582 | root = sctx->parent_root; | |
0d8869fb | 4583 | cb = record_deleted_ref_if_needed; |
31db9f7c | 4584 | } else { |
4d1a63b2 DS |
4585 | btrfs_err(sctx->send_root->fs_info, |
4586 | "Wrong command %d in process_all_refs", cmd); | |
4587 | ret = -EINVAL; | |
4588 | goto out; | |
31db9f7c AB |
4589 | } |
4590 | ||
4591 | key.objectid = sctx->cmp_key->objectid; | |
4592 | key.type = BTRFS_INODE_REF_KEY; | |
4593 | key.offset = 0; | |
649b9635 | 4594 | btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) { |
31db9f7c | 4595 | if (found_key.objectid != key.objectid || |
96b5bd77 JS |
4596 | (found_key.type != BTRFS_INODE_REF_KEY && |
4597 | found_key.type != BTRFS_INODE_EXTREF_KEY)) | |
31db9f7c | 4598 | break; |
31db9f7c | 4599 | |
924794c9 | 4600 | ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx); |
31db9f7c AB |
4601 | if (ret < 0) |
4602 | goto out; | |
649b9635 GN |
4603 | } |
4604 | /* Catch error found during iteration */ | |
4605 | if (iter_ret < 0) { | |
4606 | ret = iter_ret; | |
4607 | goto out; | |
31db9f7c | 4608 | } |
e938c8ad | 4609 | btrfs_release_path(path); |
31db9f7c | 4610 | |
3dc09ec8 JB |
4611 | /* |
4612 | * We don't actually care about pending_move as we are simply | |
4613 | * re-creating this inode and will be rename'ing it into place once we | |
4614 | * rename the parent directory. | |
4615 | */ | |
9f03740a | 4616 | ret = process_recorded_refs(sctx, &pending_move); |
31db9f7c AB |
4617 | out: |
4618 | btrfs_free_path(path); | |
4619 | return ret; | |
4620 | } | |
4621 | ||
4622 | static int send_set_xattr(struct send_ctx *sctx, | |
4623 | struct fs_path *path, | |
4624 | const char *name, int name_len, | |
4625 | const char *data, int data_len) | |
4626 | { | |
4627 | int ret = 0; | |
4628 | ||
4629 | ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); | |
4630 | if (ret < 0) | |
4631 | goto out; | |
4632 | ||
4633 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
4634 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); | |
4635 | TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); | |
4636 | ||
4637 | ret = send_cmd(sctx); | |
4638 | ||
4639 | tlv_put_failure: | |
4640 | out: | |
4641 | return ret; | |
4642 | } | |
4643 | ||
4644 | static int send_remove_xattr(struct send_ctx *sctx, | |
4645 | struct fs_path *path, | |
4646 | const char *name, int name_len) | |
4647 | { | |
4648 | int ret = 0; | |
4649 | ||
4650 | ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); | |
4651 | if (ret < 0) | |
4652 | goto out; | |
4653 | ||
4654 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
4655 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); | |
4656 | ||
4657 | ret = send_cmd(sctx); | |
4658 | ||
4659 | tlv_put_failure: | |
4660 | out: | |
4661 | return ret; | |
4662 | } | |
4663 | ||
4664 | static int __process_new_xattr(int num, struct btrfs_key *di_key, | |
b1dea4e7 OS |
4665 | const char *name, int name_len, const char *data, |
4666 | int data_len, void *ctx) | |
31db9f7c AB |
4667 | { |
4668 | int ret; | |
4669 | struct send_ctx *sctx = ctx; | |
4670 | struct fs_path *p; | |
2211d5ba | 4671 | struct posix_acl_xattr_header dummy_acl; |
31db9f7c | 4672 | |
89efda52 MPS |
4673 | /* Capabilities are emitted by finish_inode_if_needed */ |
4674 | if (!strncmp(name, XATTR_NAME_CAPS, name_len)) | |
4675 | return 0; | |
4676 | ||
924794c9 | 4677 | p = fs_path_alloc(); |
31db9f7c AB |
4678 | if (!p) |
4679 | return -ENOMEM; | |
4680 | ||
4681 | /* | |
01327610 | 4682 | * This hack is needed because empty acls are stored as zero byte |
31db9f7c | 4683 | * data in xattrs. Problem with that is, that receiving these zero byte |
01327610 | 4684 | * acls will fail later. To fix this, we send a dummy acl list that |
31db9f7c AB |
4685 | * only contains the version number and no entries. |
4686 | */ | |
4687 | if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) || | |
4688 | !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) { | |
4689 | if (data_len == 0) { | |
4690 | dummy_acl.a_version = | |
4691 | cpu_to_le32(POSIX_ACL_XATTR_VERSION); | |
4692 | data = (char *)&dummy_acl; | |
4693 | data_len = sizeof(dummy_acl); | |
4694 | } | |
4695 | } | |
4696 | ||
4697 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
4698 | if (ret < 0) | |
4699 | goto out; | |
4700 | ||
4701 | ret = send_set_xattr(sctx, p, name, name_len, data, data_len); | |
4702 | ||
4703 | out: | |
924794c9 | 4704 | fs_path_free(p); |
31db9f7c AB |
4705 | return ret; |
4706 | } | |
4707 | ||
4708 | static int __process_deleted_xattr(int num, struct btrfs_key *di_key, | |
4709 | const char *name, int name_len, | |
b1dea4e7 | 4710 | const char *data, int data_len, void *ctx) |
31db9f7c AB |
4711 | { |
4712 | int ret; | |
4713 | struct send_ctx *sctx = ctx; | |
4714 | struct fs_path *p; | |
4715 | ||
924794c9 | 4716 | p = fs_path_alloc(); |
31db9f7c AB |
4717 | if (!p) |
4718 | return -ENOMEM; | |
4719 | ||
4720 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
4721 | if (ret < 0) | |
4722 | goto out; | |
4723 | ||
4724 | ret = send_remove_xattr(sctx, p, name, name_len); | |
4725 | ||
4726 | out: | |
924794c9 | 4727 | fs_path_free(p); |
31db9f7c AB |
4728 | return ret; |
4729 | } | |
4730 | ||
4731 | static int process_new_xattr(struct send_ctx *sctx) | |
4732 | { | |
4733 | int ret = 0; | |
4734 | ||
924794c9 | 4735 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
a0357511 | 4736 | __process_new_xattr, sctx); |
31db9f7c AB |
4737 | |
4738 | return ret; | |
4739 | } | |
4740 | ||
4741 | static int process_deleted_xattr(struct send_ctx *sctx) | |
4742 | { | |
e2c89907 | 4743 | return iterate_dir_item(sctx->parent_root, sctx->right_path, |
a0357511 | 4744 | __process_deleted_xattr, sctx); |
31db9f7c AB |
4745 | } |
4746 | ||
4747 | struct find_xattr_ctx { | |
4748 | const char *name; | |
4749 | int name_len; | |
4750 | int found_idx; | |
4751 | char *found_data; | |
4752 | int found_data_len; | |
4753 | }; | |
4754 | ||
b1dea4e7 OS |
4755 | static int __find_xattr(int num, struct btrfs_key *di_key, const char *name, |
4756 | int name_len, const char *data, int data_len, void *vctx) | |
31db9f7c AB |
4757 | { |
4758 | struct find_xattr_ctx *ctx = vctx; | |
4759 | ||
4760 | if (name_len == ctx->name_len && | |
4761 | strncmp(name, ctx->name, name_len) == 0) { | |
4762 | ctx->found_idx = num; | |
4763 | ctx->found_data_len = data_len; | |
e780b0d1 | 4764 | ctx->found_data = kmemdup(data, data_len, GFP_KERNEL); |
31db9f7c AB |
4765 | if (!ctx->found_data) |
4766 | return -ENOMEM; | |
31db9f7c AB |
4767 | return 1; |
4768 | } | |
4769 | return 0; | |
4770 | } | |
4771 | ||
924794c9 | 4772 | static int find_xattr(struct btrfs_root *root, |
31db9f7c AB |
4773 | struct btrfs_path *path, |
4774 | struct btrfs_key *key, | |
4775 | const char *name, int name_len, | |
4776 | char **data, int *data_len) | |
4777 | { | |
4778 | int ret; | |
4779 | struct find_xattr_ctx ctx; | |
4780 | ||
4781 | ctx.name = name; | |
4782 | ctx.name_len = name_len; | |
4783 | ctx.found_idx = -1; | |
4784 | ctx.found_data = NULL; | |
4785 | ctx.found_data_len = 0; | |
4786 | ||
a0357511 | 4787 | ret = iterate_dir_item(root, path, __find_xattr, &ctx); |
31db9f7c AB |
4788 | if (ret < 0) |
4789 | return ret; | |
4790 | ||
4791 | if (ctx.found_idx == -1) | |
4792 | return -ENOENT; | |
4793 | if (data) { | |
4794 | *data = ctx.found_data; | |
4795 | *data_len = ctx.found_data_len; | |
4796 | } else { | |
4797 | kfree(ctx.found_data); | |
4798 | } | |
4799 | return ctx.found_idx; | |
4800 | } | |
4801 | ||
4802 | ||
4803 | static int __process_changed_new_xattr(int num, struct btrfs_key *di_key, | |
4804 | const char *name, int name_len, | |
4805 | const char *data, int data_len, | |
b1dea4e7 | 4806 | void *ctx) |
31db9f7c AB |
4807 | { |
4808 | int ret; | |
4809 | struct send_ctx *sctx = ctx; | |
4810 | char *found_data = NULL; | |
4811 | int found_data_len = 0; | |
31db9f7c | 4812 | |
924794c9 TI |
4813 | ret = find_xattr(sctx->parent_root, sctx->right_path, |
4814 | sctx->cmp_key, name, name_len, &found_data, | |
4815 | &found_data_len); | |
31db9f7c AB |
4816 | if (ret == -ENOENT) { |
4817 | ret = __process_new_xattr(num, di_key, name, name_len, data, | |
b1dea4e7 | 4818 | data_len, ctx); |
31db9f7c AB |
4819 | } else if (ret >= 0) { |
4820 | if (data_len != found_data_len || | |
4821 | memcmp(data, found_data, data_len)) { | |
4822 | ret = __process_new_xattr(num, di_key, name, name_len, | |
b1dea4e7 | 4823 | data, data_len, ctx); |
31db9f7c AB |
4824 | } else { |
4825 | ret = 0; | |
4826 | } | |
4827 | } | |
4828 | ||
4829 | kfree(found_data); | |
31db9f7c AB |
4830 | return ret; |
4831 | } | |
4832 | ||
4833 | static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key, | |
4834 | const char *name, int name_len, | |
4835 | const char *data, int data_len, | |
b1dea4e7 | 4836 | void *ctx) |
31db9f7c AB |
4837 | { |
4838 | int ret; | |
4839 | struct send_ctx *sctx = ctx; | |
4840 | ||
924794c9 TI |
4841 | ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key, |
4842 | name, name_len, NULL, NULL); | |
31db9f7c AB |
4843 | if (ret == -ENOENT) |
4844 | ret = __process_deleted_xattr(num, di_key, name, name_len, data, | |
b1dea4e7 | 4845 | data_len, ctx); |
31db9f7c AB |
4846 | else if (ret >= 0) |
4847 | ret = 0; | |
4848 | ||
4849 | return ret; | |
4850 | } | |
4851 | ||
4852 | static int process_changed_xattr(struct send_ctx *sctx) | |
4853 | { | |
4854 | int ret = 0; | |
4855 | ||
924794c9 | 4856 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
a0357511 | 4857 | __process_changed_new_xattr, sctx); |
31db9f7c AB |
4858 | if (ret < 0) |
4859 | goto out; | |
924794c9 | 4860 | ret = iterate_dir_item(sctx->parent_root, sctx->right_path, |
a0357511 | 4861 | __process_changed_deleted_xattr, sctx); |
31db9f7c AB |
4862 | |
4863 | out: | |
4864 | return ret; | |
4865 | } | |
4866 | ||
4867 | static int process_all_new_xattrs(struct send_ctx *sctx) | |
4868 | { | |
69e43177 GN |
4869 | int ret = 0; |
4870 | int iter_ret = 0; | |
31db9f7c AB |
4871 | struct btrfs_root *root; |
4872 | struct btrfs_path *path; | |
4873 | struct btrfs_key key; | |
4874 | struct btrfs_key found_key; | |
31db9f7c AB |
4875 | |
4876 | path = alloc_path_for_send(); | |
4877 | if (!path) | |
4878 | return -ENOMEM; | |
4879 | ||
4880 | root = sctx->send_root; | |
4881 | ||
4882 | key.objectid = sctx->cmp_key->objectid; | |
4883 | key.type = BTRFS_XATTR_ITEM_KEY; | |
4884 | key.offset = 0; | |
69e43177 | 4885 | btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) { |
31db9f7c AB |
4886 | if (found_key.objectid != key.objectid || |
4887 | found_key.type != key.type) { | |
4888 | ret = 0; | |
69e43177 | 4889 | break; |
31db9f7c AB |
4890 | } |
4891 | ||
a0357511 | 4892 | ret = iterate_dir_item(root, path, __process_new_xattr, sctx); |
31db9f7c | 4893 | if (ret < 0) |
69e43177 | 4894 | break; |
31db9f7c | 4895 | } |
69e43177 GN |
4896 | /* Catch error found during iteration */ |
4897 | if (iter_ret < 0) | |
4898 | ret = iter_ret; | |
31db9f7c | 4899 | |
31db9f7c AB |
4900 | btrfs_free_path(path); |
4901 | return ret; | |
4902 | } | |
4903 | ||
38622010 BB |
4904 | static int send_verity(struct send_ctx *sctx, struct fs_path *path, |
4905 | struct fsverity_descriptor *desc) | |
4906 | { | |
4907 | int ret; | |
4908 | ||
4909 | ret = begin_cmd(sctx, BTRFS_SEND_C_ENABLE_VERITY); | |
4910 | if (ret < 0) | |
4911 | goto out; | |
4912 | ||
4913 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); | |
4914 | TLV_PUT_U8(sctx, BTRFS_SEND_A_VERITY_ALGORITHM, | |
4915 | le8_to_cpu(desc->hash_algorithm)); | |
4916 | TLV_PUT_U32(sctx, BTRFS_SEND_A_VERITY_BLOCK_SIZE, | |
4917 | 1U << le8_to_cpu(desc->log_blocksize)); | |
4918 | TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SALT_DATA, desc->salt, | |
4919 | le8_to_cpu(desc->salt_size)); | |
4920 | TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SIG_DATA, desc->signature, | |
4921 | le32_to_cpu(desc->sig_size)); | |
4922 | ||
4923 | ret = send_cmd(sctx); | |
4924 | ||
4925 | tlv_put_failure: | |
4926 | out: | |
4927 | return ret; | |
4928 | } | |
4929 | ||
4930 | static int process_verity(struct send_ctx *sctx) | |
4931 | { | |
4932 | int ret = 0; | |
4933 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; | |
4934 | struct inode *inode; | |
4935 | struct fs_path *p; | |
4936 | ||
4937 | inode = btrfs_iget(fs_info->sb, sctx->cur_ino, sctx->send_root); | |
4938 | if (IS_ERR(inode)) | |
4939 | return PTR_ERR(inode); | |
4940 | ||
4941 | ret = btrfs_get_verity_descriptor(inode, NULL, 0); | |
4942 | if (ret < 0) | |
4943 | goto iput; | |
4944 | ||
4945 | if (ret > FS_VERITY_MAX_DESCRIPTOR_SIZE) { | |
4946 | ret = -EMSGSIZE; | |
4947 | goto iput; | |
4948 | } | |
4949 | if (!sctx->verity_descriptor) { | |
4950 | sctx->verity_descriptor = kvmalloc(FS_VERITY_MAX_DESCRIPTOR_SIZE, | |
4951 | GFP_KERNEL); | |
4952 | if (!sctx->verity_descriptor) { | |
4953 | ret = -ENOMEM; | |
4954 | goto iput; | |
4955 | } | |
4956 | } | |
4957 | ||
4958 | ret = btrfs_get_verity_descriptor(inode, sctx->verity_descriptor, ret); | |
4959 | if (ret < 0) | |
4960 | goto iput; | |
4961 | ||
4962 | p = fs_path_alloc(); | |
4963 | if (!p) { | |
4964 | ret = -ENOMEM; | |
4965 | goto iput; | |
4966 | } | |
4967 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
4968 | if (ret < 0) | |
4969 | goto free_path; | |
4970 | ||
4971 | ret = send_verity(sctx, p, sctx->verity_descriptor); | |
4972 | if (ret < 0) | |
4973 | goto free_path; | |
4974 | ||
4975 | free_path: | |
4976 | fs_path_free(p); | |
4977 | iput: | |
4978 | iput(inode); | |
4979 | return ret; | |
4980 | } | |
4981 | ||
8c7d9fe0 OS |
4982 | static inline u64 max_send_read_size(const struct send_ctx *sctx) |
4983 | { | |
4984 | return sctx->send_max_size - SZ_16K; | |
4985 | } | |
4986 | ||
4987 | static int put_data_header(struct send_ctx *sctx, u32 len) | |
4988 | { | |
356bbbb6 OS |
4989 | if (WARN_ON_ONCE(sctx->put_data)) |
4990 | return -EINVAL; | |
4991 | sctx->put_data = true; | |
4992 | if (sctx->proto >= 2) { | |
4993 | /* | |
4994 | * Since v2, the data attribute header doesn't include a length, | |
4995 | * it is implicitly to the end of the command. | |
4996 | */ | |
4997 | if (sctx->send_max_size - sctx->send_size < sizeof(__le16) + len) | |
4998 | return -EOVERFLOW; | |
4999 | put_unaligned_le16(BTRFS_SEND_A_DATA, sctx->send_buf + sctx->send_size); | |
5000 | sctx->send_size += sizeof(__le16); | |
5001 | } else { | |
5002 | struct btrfs_tlv_header *hdr; | |
8c7d9fe0 | 5003 | |
356bbbb6 OS |
5004 | if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len) |
5005 | return -EOVERFLOW; | |
5006 | hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size); | |
5007 | put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type); | |
5008 | put_unaligned_le16(len, &hdr->tlv_len); | |
5009 | sctx->send_size += sizeof(*hdr); | |
5010 | } | |
8c7d9fe0 OS |
5011 | return 0; |
5012 | } | |
5013 | ||
5014 | static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len) | |
ed259095 JB |
5015 | { |
5016 | struct btrfs_root *root = sctx->send_root; | |
5017 | struct btrfs_fs_info *fs_info = root->fs_info; | |
ed259095 | 5018 | struct page *page; |
09cbfeaf | 5019 | pgoff_t index = offset >> PAGE_SHIFT; |
ed259095 | 5020 | pgoff_t last_index; |
7073017a | 5021 | unsigned pg_offset = offset_in_page(offset); |
8c7d9fe0 OS |
5022 | int ret; |
5023 | ||
5024 | ret = put_data_header(sctx, len); | |
5025 | if (ret) | |
5026 | return ret; | |
ed259095 | 5027 | |
09cbfeaf | 5028 | last_index = (offset + len - 1) >> PAGE_SHIFT; |
2131bcd3 | 5029 | |
ed259095 JB |
5030 | while (index <= last_index) { |
5031 | unsigned cur_len = min_t(unsigned, len, | |
09cbfeaf | 5032 | PAGE_SIZE - pg_offset); |
eef16ba2 | 5033 | |
521b6803 | 5034 | page = find_lock_page(sctx->cur_inode->i_mapping, index); |
ed259095 | 5035 | if (!page) { |
521b6803 FM |
5036 | page_cache_sync_readahead(sctx->cur_inode->i_mapping, |
5037 | &sctx->ra, NULL, index, | |
5038 | last_index + 1 - index); | |
eef16ba2 | 5039 | |
521b6803 FM |
5040 | page = find_or_create_page(sctx->cur_inode->i_mapping, |
5041 | index, GFP_KERNEL); | |
eef16ba2 KH |
5042 | if (!page) { |
5043 | ret = -ENOMEM; | |
5044 | break; | |
5045 | } | |
5046 | } | |
5047 | ||
521b6803 FM |
5048 | if (PageReadahead(page)) |
5049 | page_cache_async_readahead(sctx->cur_inode->i_mapping, | |
fdaf9a58 LT |
5050 | &sctx->ra, NULL, page_folio(page), |
5051 | index, last_index + 1 - index); | |
ed259095 JB |
5052 | |
5053 | if (!PageUptodate(page)) { | |
fb12489b | 5054 | btrfs_read_folio(NULL, page_folio(page)); |
ed259095 JB |
5055 | lock_page(page); |
5056 | if (!PageUptodate(page)) { | |
5057 | unlock_page(page); | |
2e7be9db DM |
5058 | btrfs_err(fs_info, |
5059 | "send: IO error at offset %llu for inode %llu root %llu", | |
5060 | page_offset(page), sctx->cur_ino, | |
5061 | sctx->send_root->root_key.objectid); | |
09cbfeaf | 5062 | put_page(page); |
ed259095 JB |
5063 | ret = -EIO; |
5064 | break; | |
5065 | } | |
5066 | } | |
5067 | ||
3590ec58 IW |
5068 | memcpy_from_page(sctx->send_buf + sctx->send_size, page, |
5069 | pg_offset, cur_len); | |
ed259095 | 5070 | unlock_page(page); |
09cbfeaf | 5071 | put_page(page); |
ed259095 JB |
5072 | index++; |
5073 | pg_offset = 0; | |
5074 | len -= cur_len; | |
8c7d9fe0 | 5075 | sctx->send_size += cur_len; |
ed259095 | 5076 | } |
521b6803 | 5077 | |
ed259095 JB |
5078 | return ret; |
5079 | } | |
5080 | ||
31db9f7c AB |
5081 | /* |
5082 | * Read some bytes from the current inode/file and send a write command to | |
5083 | * user space. | |
5084 | */ | |
5085 | static int send_write(struct send_ctx *sctx, u64 offset, u32 len) | |
5086 | { | |
04ab956e | 5087 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
31db9f7c AB |
5088 | int ret = 0; |
5089 | struct fs_path *p; | |
31db9f7c | 5090 | |
924794c9 | 5091 | p = fs_path_alloc(); |
31db9f7c AB |
5092 | if (!p) |
5093 | return -ENOMEM; | |
5094 | ||
04ab956e | 5095 | btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len); |
31db9f7c | 5096 | |
31db9f7c AB |
5097 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); |
5098 | if (ret < 0) | |
5099 | goto out; | |
5100 | ||
5101 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
5102 | if (ret < 0) | |
5103 | goto out; | |
5104 | ||
5105 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
5106 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
8c7d9fe0 OS |
5107 | ret = put_file_data(sctx, offset, len); |
5108 | if (ret < 0) | |
5109 | goto out; | |
31db9f7c AB |
5110 | |
5111 | ret = send_cmd(sctx); | |
5112 | ||
5113 | tlv_put_failure: | |
5114 | out: | |
924794c9 | 5115 | fs_path_free(p); |
a9b2e0de | 5116 | return ret; |
31db9f7c AB |
5117 | } |
5118 | ||
5119 | /* | |
5120 | * Send a clone command to user space. | |
5121 | */ | |
5122 | static int send_clone(struct send_ctx *sctx, | |
5123 | u64 offset, u32 len, | |
5124 | struct clone_root *clone_root) | |
5125 | { | |
5126 | int ret = 0; | |
31db9f7c AB |
5127 | struct fs_path *p; |
5128 | u64 gen; | |
5129 | ||
04ab956e JM |
5130 | btrfs_debug(sctx->send_root->fs_info, |
5131 | "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu", | |
4fd786e6 MT |
5132 | offset, len, clone_root->root->root_key.objectid, |
5133 | clone_root->ino, clone_root->offset); | |
31db9f7c | 5134 | |
924794c9 | 5135 | p = fs_path_alloc(); |
31db9f7c AB |
5136 | if (!p) |
5137 | return -ENOMEM; | |
5138 | ||
5139 | ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); | |
5140 | if (ret < 0) | |
5141 | goto out; | |
5142 | ||
5143 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
5144 | if (ret < 0) | |
5145 | goto out; | |
5146 | ||
5147 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
5148 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); | |
5149 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
5150 | ||
e938c8ad | 5151 | if (clone_root->root == sctx->send_root) { |
7e93f6dc | 5152 | ret = get_inode_gen(sctx->send_root, clone_root->ino, &gen); |
31db9f7c AB |
5153 | if (ret < 0) |
5154 | goto out; | |
5155 | ret = get_cur_path(sctx, clone_root->ino, gen, p); | |
5156 | } else { | |
924794c9 | 5157 | ret = get_inode_path(clone_root->root, clone_root->ino, p); |
31db9f7c AB |
5158 | } |
5159 | if (ret < 0) | |
5160 | goto out; | |
5161 | ||
37b8d27d JB |
5162 | /* |
5163 | * If the parent we're using has a received_uuid set then use that as | |
5164 | * our clone source as that is what we will look for when doing a | |
5165 | * receive. | |
5166 | * | |
5167 | * This covers the case that we create a snapshot off of a received | |
5168 | * subvolume and then use that as the parent and try to receive on a | |
5169 | * different host. | |
5170 | */ | |
5171 | if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid)) | |
5172 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, | |
5173 | clone_root->root->root_item.received_uuid); | |
5174 | else | |
5175 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, | |
5176 | clone_root->root->root_item.uuid); | |
31db9f7c | 5177 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
09e3a288 | 5178 | btrfs_root_ctransid(&clone_root->root->root_item)); |
31db9f7c AB |
5179 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); |
5180 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, | |
5181 | clone_root->offset); | |
5182 | ||
5183 | ret = send_cmd(sctx); | |
5184 | ||
5185 | tlv_put_failure: | |
5186 | out: | |
924794c9 | 5187 | fs_path_free(p); |
31db9f7c AB |
5188 | return ret; |
5189 | } | |
5190 | ||
cb95e7bf MF |
5191 | /* |
5192 | * Send an update extent command to user space. | |
5193 | */ | |
5194 | static int send_update_extent(struct send_ctx *sctx, | |
5195 | u64 offset, u32 len) | |
5196 | { | |
5197 | int ret = 0; | |
5198 | struct fs_path *p; | |
5199 | ||
924794c9 | 5200 | p = fs_path_alloc(); |
cb95e7bf MF |
5201 | if (!p) |
5202 | return -ENOMEM; | |
5203 | ||
5204 | ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT); | |
5205 | if (ret < 0) | |
5206 | goto out; | |
5207 | ||
5208 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); | |
5209 | if (ret < 0) | |
5210 | goto out; | |
5211 | ||
5212 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
5213 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
5214 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len); | |
5215 | ||
5216 | ret = send_cmd(sctx); | |
5217 | ||
5218 | tlv_put_failure: | |
5219 | out: | |
924794c9 | 5220 | fs_path_free(p); |
cb95e7bf MF |
5221 | return ret; |
5222 | } | |
5223 | ||
16e7549f JB |
5224 | static int send_hole(struct send_ctx *sctx, u64 end) |
5225 | { | |
5226 | struct fs_path *p = NULL; | |
8c7d9fe0 | 5227 | u64 read_size = max_send_read_size(sctx); |
16e7549f | 5228 | u64 offset = sctx->cur_inode_last_extent; |
16e7549f JB |
5229 | int ret = 0; |
5230 | ||
22d3151c FM |
5231 | /* |
5232 | * A hole that starts at EOF or beyond it. Since we do not yet support | |
5233 | * fallocate (for extent preallocation and hole punching), sending a | |
5234 | * write of zeroes starting at EOF or beyond would later require issuing | |
5235 | * a truncate operation which would undo the write and achieve nothing. | |
5236 | */ | |
5237 | if (offset >= sctx->cur_inode_size) | |
5238 | return 0; | |
5239 | ||
6b1f72e5 FM |
5240 | /* |
5241 | * Don't go beyond the inode's i_size due to prealloc extents that start | |
5242 | * after the i_size. | |
5243 | */ | |
5244 | end = min_t(u64, end, sctx->cur_inode_size); | |
5245 | ||
d4dfc0f4 FM |
5246 | if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) |
5247 | return send_update_extent(sctx, offset, end - offset); | |
5248 | ||
16e7549f JB |
5249 | p = fs_path_alloc(); |
5250 | if (!p) | |
5251 | return -ENOMEM; | |
c715e155 FM |
5252 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
5253 | if (ret < 0) | |
5254 | goto tlv_put_failure; | |
16e7549f | 5255 | while (offset < end) { |
8c7d9fe0 | 5256 | u64 len = min(end - offset, read_size); |
16e7549f JB |
5257 | |
5258 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); | |
16e7549f JB |
5259 | if (ret < 0) |
5260 | break; | |
5261 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); | |
5262 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
8c7d9fe0 OS |
5263 | ret = put_data_header(sctx, len); |
5264 | if (ret < 0) | |
5265 | break; | |
5266 | memset(sctx->send_buf + sctx->send_size, 0, len); | |
5267 | sctx->send_size += len; | |
16e7549f JB |
5268 | ret = send_cmd(sctx); |
5269 | if (ret < 0) | |
5270 | break; | |
5271 | offset += len; | |
5272 | } | |
ffa7c429 | 5273 | sctx->cur_inode_next_write_offset = offset; |
16e7549f JB |
5274 | tlv_put_failure: |
5275 | fs_path_free(p); | |
5276 | return ret; | |
5277 | } | |
5278 | ||
3ea4dc5b OS |
5279 | static int send_encoded_inline_extent(struct send_ctx *sctx, |
5280 | struct btrfs_path *path, u64 offset, | |
5281 | u64 len) | |
5282 | { | |
5283 | struct btrfs_root *root = sctx->send_root; | |
5284 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5285 | struct inode *inode; | |
5286 | struct fs_path *fspath; | |
5287 | struct extent_buffer *leaf = path->nodes[0]; | |
5288 | struct btrfs_key key; | |
5289 | struct btrfs_file_extent_item *ei; | |
5290 | u64 ram_bytes; | |
5291 | size_t inline_size; | |
5292 | int ret; | |
5293 | ||
5294 | inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root); | |
5295 | if (IS_ERR(inode)) | |
5296 | return PTR_ERR(inode); | |
5297 | ||
5298 | fspath = fs_path_alloc(); | |
5299 | if (!fspath) { | |
5300 | ret = -ENOMEM; | |
5301 | goto out; | |
5302 | } | |
5303 | ||
5304 | ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE); | |
5305 | if (ret < 0) | |
5306 | goto out; | |
5307 | ||
5308 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath); | |
5309 | if (ret < 0) | |
5310 | goto out; | |
5311 | ||
5312 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
5313 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); | |
5314 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, ei); | |
5315 | inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]); | |
5316 | ||
5317 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath); | |
5318 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
5319 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN, | |
5320 | min(key.offset + ram_bytes - offset, len)); | |
5321 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN, ram_bytes); | |
5322 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET, offset - key.offset); | |
5323 | ret = btrfs_encoded_io_compression_from_extent(fs_info, | |
5324 | btrfs_file_extent_compression(leaf, ei)); | |
5325 | if (ret < 0) | |
5326 | goto out; | |
5327 | TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret); | |
5328 | ||
5329 | ret = put_data_header(sctx, inline_size); | |
5330 | if (ret < 0) | |
5331 | goto out; | |
5332 | read_extent_buffer(leaf, sctx->send_buf + sctx->send_size, | |
5333 | btrfs_file_extent_inline_start(ei), inline_size); | |
5334 | sctx->send_size += inline_size; | |
5335 | ||
5336 | ret = send_cmd(sctx); | |
5337 | ||
5338 | tlv_put_failure: | |
5339 | out: | |
5340 | fs_path_free(fspath); | |
5341 | iput(inode); | |
5342 | return ret; | |
5343 | } | |
5344 | ||
5345 | static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path, | |
5346 | u64 offset, u64 len) | |
5347 | { | |
5348 | struct btrfs_root *root = sctx->send_root; | |
5349 | struct btrfs_fs_info *fs_info = root->fs_info; | |
5350 | struct inode *inode; | |
5351 | struct fs_path *fspath; | |
5352 | struct extent_buffer *leaf = path->nodes[0]; | |
5353 | struct btrfs_key key; | |
5354 | struct btrfs_file_extent_item *ei; | |
5355 | u64 disk_bytenr, disk_num_bytes; | |
5356 | u32 data_offset; | |
5357 | struct btrfs_cmd_header *hdr; | |
5358 | u32 crc; | |
5359 | int ret; | |
5360 | ||
5361 | inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root); | |
5362 | if (IS_ERR(inode)) | |
5363 | return PTR_ERR(inode); | |
5364 | ||
5365 | fspath = fs_path_alloc(); | |
5366 | if (!fspath) { | |
5367 | ret = -ENOMEM; | |
5368 | goto out; | |
5369 | } | |
5370 | ||
5371 | ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE); | |
5372 | if (ret < 0) | |
5373 | goto out; | |
5374 | ||
5375 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath); | |
5376 | if (ret < 0) | |
5377 | goto out; | |
5378 | ||
5379 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
5380 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); | |
5381 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei); | |
5382 | disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, ei); | |
5383 | ||
5384 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath); | |
5385 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); | |
5386 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN, | |
5387 | min(key.offset + btrfs_file_extent_num_bytes(leaf, ei) - offset, | |
5388 | len)); | |
5389 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN, | |
5390 | btrfs_file_extent_ram_bytes(leaf, ei)); | |
5391 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET, | |
5392 | offset - key.offset + btrfs_file_extent_offset(leaf, ei)); | |
5393 | ret = btrfs_encoded_io_compression_from_extent(fs_info, | |
5394 | btrfs_file_extent_compression(leaf, ei)); | |
5395 | if (ret < 0) | |
5396 | goto out; | |
5397 | TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret); | |
5398 | TLV_PUT_U32(sctx, BTRFS_SEND_A_ENCRYPTION, 0); | |
5399 | ||
5400 | ret = put_data_header(sctx, disk_num_bytes); | |
5401 | if (ret < 0) | |
5402 | goto out; | |
5403 | ||
5404 | /* | |
5405 | * We want to do I/O directly into the send buffer, so get the next page | |
5406 | * boundary in the send buffer. This means that there may be a gap | |
5407 | * between the beginning of the command and the file data. | |
5408 | */ | |
5409 | data_offset = ALIGN(sctx->send_size, PAGE_SIZE); | |
5410 | if (data_offset > sctx->send_max_size || | |
5411 | sctx->send_max_size - data_offset < disk_num_bytes) { | |
5412 | ret = -EOVERFLOW; | |
5413 | goto out; | |
5414 | } | |
5415 | ||
5416 | /* | |
5417 | * Note that send_buf is a mapping of send_buf_pages, so this is really | |
5418 | * reading into send_buf. | |
5419 | */ | |
5420 | ret = btrfs_encoded_read_regular_fill_pages(BTRFS_I(inode), offset, | |
5421 | disk_bytenr, disk_num_bytes, | |
5422 | sctx->send_buf_pages + | |
5423 | (data_offset >> PAGE_SHIFT)); | |
5424 | if (ret) | |
5425 | goto out; | |
5426 | ||
5427 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; | |
5428 | hdr->len = cpu_to_le32(sctx->send_size + disk_num_bytes - sizeof(*hdr)); | |
5429 | hdr->crc = 0; | |
5430 | crc = btrfs_crc32c(0, sctx->send_buf, sctx->send_size); | |
5431 | crc = btrfs_crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes); | |
5432 | hdr->crc = cpu_to_le32(crc); | |
5433 | ||
5434 | ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, | |
5435 | &sctx->send_off); | |
5436 | if (!ret) { | |
5437 | ret = write_buf(sctx->send_filp, sctx->send_buf + data_offset, | |
5438 | disk_num_bytes, &sctx->send_off); | |
5439 | } | |
5440 | sctx->send_size = 0; | |
5441 | sctx->put_data = false; | |
5442 | ||
5443 | tlv_put_failure: | |
5444 | out: | |
5445 | fs_path_free(fspath); | |
5446 | iput(inode); | |
5447 | return ret; | |
5448 | } | |
5449 | ||
5450 | static int send_extent_data(struct send_ctx *sctx, struct btrfs_path *path, | |
5451 | const u64 offset, const u64 len) | |
d906d49f | 5452 | { |
152555b3 | 5453 | const u64 end = offset + len; |
3ea4dc5b OS |
5454 | struct extent_buffer *leaf = path->nodes[0]; |
5455 | struct btrfs_file_extent_item *ei; | |
8c7d9fe0 | 5456 | u64 read_size = max_send_read_size(sctx); |
d906d49f FM |
5457 | u64 sent = 0; |
5458 | ||
5459 | if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) | |
5460 | return send_update_extent(sctx, offset, len); | |
5461 | ||
3ea4dc5b OS |
5462 | ei = btrfs_item_ptr(leaf, path->slots[0], |
5463 | struct btrfs_file_extent_item); | |
5464 | if ((sctx->flags & BTRFS_SEND_FLAG_COMPRESSED) && | |
5465 | btrfs_file_extent_compression(leaf, ei) != BTRFS_COMPRESS_NONE) { | |
5466 | bool is_inline = (btrfs_file_extent_type(leaf, ei) == | |
5467 | BTRFS_FILE_EXTENT_INLINE); | |
5468 | ||
5469 | /* | |
5470 | * Send the compressed extent unless the compressed data is | |
5471 | * larger than the decompressed data. This can happen if we're | |
5472 | * not sending the entire extent, either because it has been | |
5473 | * partially overwritten/truncated or because this is a part of | |
5474 | * the extent that we couldn't clone in clone_range(). | |
5475 | */ | |
5476 | if (is_inline && | |
5477 | btrfs_file_extent_inline_item_len(leaf, | |
5478 | path->slots[0]) <= len) { | |
5479 | return send_encoded_inline_extent(sctx, path, offset, | |
5480 | len); | |
5481 | } else if (!is_inline && | |
5482 | btrfs_file_extent_disk_num_bytes(leaf, ei) <= len) { | |
5483 | return send_encoded_extent(sctx, path, offset, len); | |
5484 | } | |
5485 | } | |
5486 | ||
521b6803 FM |
5487 | if (sctx->cur_inode == NULL) { |
5488 | struct btrfs_root *root = sctx->send_root; | |
5489 | ||
5490 | sctx->cur_inode = btrfs_iget(root->fs_info->sb, sctx->cur_ino, root); | |
5491 | if (IS_ERR(sctx->cur_inode)) { | |
5492 | int err = PTR_ERR(sctx->cur_inode); | |
5493 | ||
5494 | sctx->cur_inode = NULL; | |
5495 | return err; | |
5496 | } | |
5497 | memset(&sctx->ra, 0, sizeof(struct file_ra_state)); | |
5498 | file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping); | |
152555b3 FM |
5499 | |
5500 | /* | |
5501 | * It's very likely there are no pages from this inode in the page | |
5502 | * cache, so after reading extents and sending their data, we clean | |
5503 | * the page cache to avoid trashing the page cache (adding pressure | |
5504 | * to the page cache and forcing eviction of other data more useful | |
5505 | * for applications). | |
5506 | * | |
5507 | * We decide if we should clean the page cache simply by checking | |
5508 | * if the inode's mapping nrpages is 0 when we first open it, and | |
5509 | * not by using something like filemap_range_has_page() before | |
5510 | * reading an extent because when we ask the readahead code to | |
5511 | * read a given file range, it may (and almost always does) read | |
5512 | * pages from beyond that range (see the documentation for | |
5513 | * page_cache_sync_readahead()), so it would not be reliable, | |
5514 | * because after reading the first extent future calls to | |
5515 | * filemap_range_has_page() would return true because the readahead | |
5516 | * on the previous extent resulted in reading pages of the current | |
5517 | * extent as well. | |
5518 | */ | |
5519 | sctx->clean_page_cache = (sctx->cur_inode->i_mapping->nrpages == 0); | |
5520 | sctx->page_cache_clear_start = round_down(offset, PAGE_SIZE); | |
521b6803 FM |
5521 | } |
5522 | ||
d906d49f | 5523 | while (sent < len) { |
8c7d9fe0 | 5524 | u64 size = min(len - sent, read_size); |
d906d49f FM |
5525 | int ret; |
5526 | ||
d906d49f FM |
5527 | ret = send_write(sctx, offset + sent, size); |
5528 | if (ret < 0) | |
5529 | return ret; | |
a9b2e0de | 5530 | sent += size; |
d906d49f | 5531 | } |
152555b3 FM |
5532 | |
5533 | if (sctx->clean_page_cache && IS_ALIGNED(end, PAGE_SIZE)) { | |
5534 | /* | |
5535 | * Always operate only on ranges that are a multiple of the page | |
5536 | * size. This is not only to prevent zeroing parts of a page in | |
5537 | * the case of subpage sector size, but also to guarantee we evict | |
5538 | * pages, as passing a range that is smaller than page size does | |
5539 | * not evict the respective page (only zeroes part of its content). | |
5540 | * | |
5541 | * Always start from the end offset of the last range cleared. | |
5542 | * This is because the readahead code may (and very often does) | |
5543 | * reads pages beyond the range we request for readahead. So if | |
5544 | * we have an extent layout like this: | |
5545 | * | |
5546 | * [ extent A ] [ extent B ] [ extent C ] | |
5547 | * | |
5548 | * When we ask page_cache_sync_readahead() to read extent A, it | |
5549 | * may also trigger reads for pages of extent B. If we are doing | |
5550 | * an incremental send and extent B has not changed between the | |
5551 | * parent and send snapshots, some or all of its pages may end | |
5552 | * up being read and placed in the page cache. So when truncating | |
5553 | * the page cache we always start from the end offset of the | |
5554 | * previously processed extent up to the end of the current | |
5555 | * extent. | |
5556 | */ | |
5557 | truncate_inode_pages_range(&sctx->cur_inode->i_data, | |
5558 | sctx->page_cache_clear_start, | |
5559 | end - 1); | |
5560 | sctx->page_cache_clear_start = end; | |
5561 | } | |
5562 | ||
d906d49f FM |
5563 | return 0; |
5564 | } | |
5565 | ||
89efda52 MPS |
5566 | /* |
5567 | * Search for a capability xattr related to sctx->cur_ino. If the capability is | |
5568 | * found, call send_set_xattr function to emit it. | |
5569 | * | |
5570 | * Return 0 if there isn't a capability, or when the capability was emitted | |
5571 | * successfully, or < 0 if an error occurred. | |
5572 | */ | |
5573 | static int send_capabilities(struct send_ctx *sctx) | |
5574 | { | |
5575 | struct fs_path *fspath = NULL; | |
5576 | struct btrfs_path *path; | |
5577 | struct btrfs_dir_item *di; | |
5578 | struct extent_buffer *leaf; | |
5579 | unsigned long data_ptr; | |
5580 | char *buf = NULL; | |
5581 | int buf_len; | |
5582 | int ret = 0; | |
5583 | ||
5584 | path = alloc_path_for_send(); | |
5585 | if (!path) | |
5586 | return -ENOMEM; | |
5587 | ||
5588 | di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino, | |
5589 | XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0); | |
5590 | if (!di) { | |
5591 | /* There is no xattr for this inode */ | |
5592 | goto out; | |
5593 | } else if (IS_ERR(di)) { | |
5594 | ret = PTR_ERR(di); | |
5595 | goto out; | |
5596 | } | |
5597 | ||
5598 | leaf = path->nodes[0]; | |
5599 | buf_len = btrfs_dir_data_len(leaf, di); | |
5600 | ||
5601 | fspath = fs_path_alloc(); | |
5602 | buf = kmalloc(buf_len, GFP_KERNEL); | |
5603 | if (!fspath || !buf) { | |
5604 | ret = -ENOMEM; | |
5605 | goto out; | |
5606 | } | |
5607 | ||
5608 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath); | |
5609 | if (ret < 0) | |
5610 | goto out; | |
5611 | ||
5612 | data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di); | |
5613 | read_extent_buffer(leaf, buf, data_ptr, buf_len); | |
5614 | ||
5615 | ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS, | |
5616 | strlen(XATTR_NAME_CAPS), buf, buf_len); | |
5617 | out: | |
5618 | kfree(buf); | |
5619 | fs_path_free(fspath); | |
5620 | btrfs_free_path(path); | |
5621 | return ret; | |
5622 | } | |
5623 | ||
3ea4dc5b OS |
5624 | static int clone_range(struct send_ctx *sctx, struct btrfs_path *dst_path, |
5625 | struct clone_root *clone_root, const u64 disk_byte, | |
5626 | u64 data_offset, u64 offset, u64 len) | |
d906d49f FM |
5627 | { |
5628 | struct btrfs_path *path; | |
5629 | struct btrfs_key key; | |
5630 | int ret; | |
7e93f6dc | 5631 | struct btrfs_inode_info info; |
431d3988 | 5632 | u64 clone_src_i_size = 0; |
d906d49f | 5633 | |
72610b1b FM |
5634 | /* |
5635 | * Prevent cloning from a zero offset with a length matching the sector | |
5636 | * size because in some scenarios this will make the receiver fail. | |
5637 | * | |
5638 | * For example, if in the source filesystem the extent at offset 0 | |
5639 | * has a length of sectorsize and it was written using direct IO, then | |
5640 | * it can never be an inline extent (even if compression is enabled). | |
5641 | * Then this extent can be cloned in the original filesystem to a non | |
5642 | * zero file offset, but it may not be possible to clone in the | |
5643 | * destination filesystem because it can be inlined due to compression | |
5644 | * on the destination filesystem (as the receiver's write operations are | |
5645 | * always done using buffered IO). The same happens when the original | |
5646 | * filesystem does not have compression enabled but the destination | |
5647 | * filesystem has. | |
5648 | */ | |
5649 | if (clone_root->offset == 0 && | |
5650 | len == sctx->send_root->fs_info->sectorsize) | |
3ea4dc5b | 5651 | return send_extent_data(sctx, dst_path, offset, len); |
72610b1b | 5652 | |
d906d49f FM |
5653 | path = alloc_path_for_send(); |
5654 | if (!path) | |
5655 | return -ENOMEM; | |
5656 | ||
040ee612 RK |
5657 | /* |
5658 | * There are inodes that have extents that lie behind its i_size. Don't | |
5659 | * accept clones from these extents. | |
5660 | */ | |
7e93f6dc | 5661 | ret = get_inode_info(clone_root->root, clone_root->ino, &info); |
040ee612 RK |
5662 | btrfs_release_path(path); |
5663 | if (ret < 0) | |
5664 | goto out; | |
7e93f6dc | 5665 | clone_src_i_size = info.size; |
040ee612 | 5666 | |
d906d49f FM |
5667 | /* |
5668 | * We can't send a clone operation for the entire range if we find | |
5669 | * extent items in the respective range in the source file that | |
5670 | * refer to different extents or if we find holes. | |
5671 | * So check for that and do a mix of clone and regular write/copy | |
5672 | * operations if needed. | |
5673 | * | |
5674 | * Example: | |
5675 | * | |
5676 | * mkfs.btrfs -f /dev/sda | |
5677 | * mount /dev/sda /mnt | |
5678 | * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo | |
5679 | * cp --reflink=always /mnt/foo /mnt/bar | |
5680 | * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo | |
5681 | * btrfs subvolume snapshot -r /mnt /mnt/snap | |
5682 | * | |
5683 | * If when we send the snapshot and we are processing file bar (which | |
5684 | * has a higher inode number than foo) we blindly send a clone operation | |
5685 | * for the [0, 100K[ range from foo to bar, the receiver ends up getting | |
5686 | * a file bar that matches the content of file foo - iow, doesn't match | |
5687 | * the content from bar in the original filesystem. | |
5688 | */ | |
5689 | key.objectid = clone_root->ino; | |
5690 | key.type = BTRFS_EXTENT_DATA_KEY; | |
5691 | key.offset = clone_root->offset; | |
5692 | ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0); | |
5693 | if (ret < 0) | |
5694 | goto out; | |
5695 | if (ret > 0 && path->slots[0] > 0) { | |
5696 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); | |
5697 | if (key.objectid == clone_root->ino && | |
5698 | key.type == BTRFS_EXTENT_DATA_KEY) | |
5699 | path->slots[0]--; | |
5700 | } | |
5701 | ||
5702 | while (true) { | |
5703 | struct extent_buffer *leaf = path->nodes[0]; | |
5704 | int slot = path->slots[0]; | |
5705 | struct btrfs_file_extent_item *ei; | |
5706 | u8 type; | |
5707 | u64 ext_len; | |
5708 | u64 clone_len; | |
040ee612 | 5709 | u64 clone_data_offset; |
a11452a3 | 5710 | bool crossed_src_i_size = false; |
d906d49f FM |
5711 | |
5712 | if (slot >= btrfs_header_nritems(leaf)) { | |
5713 | ret = btrfs_next_leaf(clone_root->root, path); | |
5714 | if (ret < 0) | |
5715 | goto out; | |
5716 | else if (ret > 0) | |
5717 | break; | |
5718 | continue; | |
5719 | } | |
5720 | ||
5721 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
5722 | ||
5723 | /* | |
5724 | * We might have an implicit trailing hole (NO_HOLES feature | |
5725 | * enabled). We deal with it after leaving this loop. | |
5726 | */ | |
5727 | if (key.objectid != clone_root->ino || | |
5728 | key.type != BTRFS_EXTENT_DATA_KEY) | |
5729 | break; | |
5730 | ||
5731 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
5732 | type = btrfs_file_extent_type(leaf, ei); | |
5733 | if (type == BTRFS_FILE_EXTENT_INLINE) { | |
e41ca589 | 5734 | ext_len = btrfs_file_extent_ram_bytes(leaf, ei); |
09cbfeaf | 5735 | ext_len = PAGE_ALIGN(ext_len); |
d906d49f FM |
5736 | } else { |
5737 | ext_len = btrfs_file_extent_num_bytes(leaf, ei); | |
5738 | } | |
5739 | ||
5740 | if (key.offset + ext_len <= clone_root->offset) | |
5741 | goto next; | |
5742 | ||
5743 | if (key.offset > clone_root->offset) { | |
5744 | /* Implicit hole, NO_HOLES feature enabled. */ | |
5745 | u64 hole_len = key.offset - clone_root->offset; | |
5746 | ||
5747 | if (hole_len > len) | |
5748 | hole_len = len; | |
3ea4dc5b OS |
5749 | ret = send_extent_data(sctx, dst_path, offset, |
5750 | hole_len); | |
d906d49f FM |
5751 | if (ret < 0) |
5752 | goto out; | |
5753 | ||
5754 | len -= hole_len; | |
5755 | if (len == 0) | |
5756 | break; | |
5757 | offset += hole_len; | |
5758 | clone_root->offset += hole_len; | |
5759 | data_offset += hole_len; | |
5760 | } | |
5761 | ||
5762 | if (key.offset >= clone_root->offset + len) | |
5763 | break; | |
5764 | ||
040ee612 RK |
5765 | if (key.offset >= clone_src_i_size) |
5766 | break; | |
5767 | ||
a11452a3 | 5768 | if (key.offset + ext_len > clone_src_i_size) { |
040ee612 | 5769 | ext_len = clone_src_i_size - key.offset; |
a11452a3 FM |
5770 | crossed_src_i_size = true; |
5771 | } | |
040ee612 RK |
5772 | |
5773 | clone_data_offset = btrfs_file_extent_offset(leaf, ei); | |
5774 | if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) { | |
5775 | clone_root->offset = key.offset; | |
5776 | if (clone_data_offset < data_offset && | |
5777 | clone_data_offset + ext_len > data_offset) { | |
5778 | u64 extent_offset; | |
5779 | ||
5780 | extent_offset = data_offset - clone_data_offset; | |
5781 | ext_len -= extent_offset; | |
5782 | clone_data_offset += extent_offset; | |
5783 | clone_root->offset += extent_offset; | |
5784 | } | |
5785 | } | |
5786 | ||
d906d49f FM |
5787 | clone_len = min_t(u64, ext_len, len); |
5788 | ||
5789 | if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte && | |
3c850b45 FM |
5790 | clone_data_offset == data_offset) { |
5791 | const u64 src_end = clone_root->offset + clone_len; | |
5792 | const u64 sectorsize = SZ_64K; | |
5793 | ||
5794 | /* | |
5795 | * We can't clone the last block, when its size is not | |
5796 | * sector size aligned, into the middle of a file. If we | |
5797 | * do so, the receiver will get a failure (-EINVAL) when | |
5798 | * trying to clone or will silently corrupt the data in | |
5799 | * the destination file if it's on a kernel without the | |
5800 | * fix introduced by commit ac765f83f1397646 | |
5801 | * ("Btrfs: fix data corruption due to cloning of eof | |
5802 | * block). | |
5803 | * | |
5804 | * So issue a clone of the aligned down range plus a | |
5805 | * regular write for the eof block, if we hit that case. | |
5806 | * | |
5807 | * Also, we use the maximum possible sector size, 64K, | |
5808 | * because we don't know what's the sector size of the | |
5809 | * filesystem that receives the stream, so we have to | |
5810 | * assume the largest possible sector size. | |
5811 | */ | |
5812 | if (src_end == clone_src_i_size && | |
5813 | !IS_ALIGNED(src_end, sectorsize) && | |
5814 | offset + clone_len < sctx->cur_inode_size) { | |
5815 | u64 slen; | |
5816 | ||
5817 | slen = ALIGN_DOWN(src_end - clone_root->offset, | |
5818 | sectorsize); | |
5819 | if (slen > 0) { | |
5820 | ret = send_clone(sctx, offset, slen, | |
5821 | clone_root); | |
5822 | if (ret < 0) | |
5823 | goto out; | |
5824 | } | |
3ea4dc5b OS |
5825 | ret = send_extent_data(sctx, dst_path, |
5826 | offset + slen, | |
3c850b45 FM |
5827 | clone_len - slen); |
5828 | } else { | |
5829 | ret = send_clone(sctx, offset, clone_len, | |
5830 | clone_root); | |
5831 | } | |
a11452a3 FM |
5832 | } else if (crossed_src_i_size && clone_len < len) { |
5833 | /* | |
5834 | * If we are at i_size of the clone source inode and we | |
5835 | * can not clone from it, terminate the loop. This is | |
5836 | * to avoid sending two write operations, one with a | |
5837 | * length matching clone_len and the final one after | |
5838 | * this loop with a length of len - clone_len. | |
5839 | * | |
5840 | * When using encoded writes (BTRFS_SEND_FLAG_COMPRESSED | |
5841 | * was passed to the send ioctl), this helps avoid | |
5842 | * sending an encoded write for an offset that is not | |
5843 | * sector size aligned, in case the i_size of the source | |
5844 | * inode is not sector size aligned. That will make the | |
5845 | * receiver fallback to decompression of the data and | |
5846 | * writing it using regular buffered IO, therefore while | |
5847 | * not incorrect, it's not optimal due decompression and | |
5848 | * possible re-compression at the receiver. | |
5849 | */ | |
5850 | break; | |
3c850b45 | 5851 | } else { |
3ea4dc5b OS |
5852 | ret = send_extent_data(sctx, dst_path, offset, |
5853 | clone_len); | |
3c850b45 | 5854 | } |
d906d49f FM |
5855 | |
5856 | if (ret < 0) | |
5857 | goto out; | |
5858 | ||
5859 | len -= clone_len; | |
5860 | if (len == 0) | |
5861 | break; | |
5862 | offset += clone_len; | |
5863 | clone_root->offset += clone_len; | |
518837e6 FM |
5864 | |
5865 | /* | |
5866 | * If we are cloning from the file we are currently processing, | |
5867 | * and using the send root as the clone root, we must stop once | |
5868 | * the current clone offset reaches the current eof of the file | |
5869 | * at the receiver, otherwise we would issue an invalid clone | |
5870 | * operation (source range going beyond eof) and cause the | |
5871 | * receiver to fail. So if we reach the current eof, bail out | |
5872 | * and fallback to a regular write. | |
5873 | */ | |
5874 | if (clone_root->root == sctx->send_root && | |
5875 | clone_root->ino == sctx->cur_ino && | |
5876 | clone_root->offset >= sctx->cur_inode_next_write_offset) | |
5877 | break; | |
5878 | ||
d906d49f FM |
5879 | data_offset += clone_len; |
5880 | next: | |
5881 | path->slots[0]++; | |
5882 | } | |
5883 | ||
5884 | if (len > 0) | |
3ea4dc5b | 5885 | ret = send_extent_data(sctx, dst_path, offset, len); |
d906d49f FM |
5886 | else |
5887 | ret = 0; | |
5888 | out: | |
5889 | btrfs_free_path(path); | |
5890 | return ret; | |
5891 | } | |
5892 | ||
31db9f7c AB |
5893 | static int send_write_or_clone(struct send_ctx *sctx, |
5894 | struct btrfs_path *path, | |
5895 | struct btrfs_key *key, | |
5896 | struct clone_root *clone_root) | |
5897 | { | |
5898 | int ret = 0; | |
31db9f7c | 5899 | u64 offset = key->offset; |
c9a949af | 5900 | u64 end; |
28e5dd8f | 5901 | u64 bs = sctx->send_root->fs_info->sb->s_blocksize; |
31db9f7c | 5902 | |
c9a949af OS |
5903 | end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size); |
5904 | if (offset >= end) | |
5905 | return 0; | |
31db9f7c | 5906 | |
c9a949af OS |
5907 | if (clone_root && IS_ALIGNED(end, bs)) { |
5908 | struct btrfs_file_extent_item *ei; | |
d906d49f FM |
5909 | u64 disk_byte; |
5910 | u64 data_offset; | |
5911 | ||
c9a949af OS |
5912 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
5913 | struct btrfs_file_extent_item); | |
d906d49f FM |
5914 | disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei); |
5915 | data_offset = btrfs_file_extent_offset(path->nodes[0], ei); | |
3ea4dc5b OS |
5916 | ret = clone_range(sctx, path, clone_root, disk_byte, |
5917 | data_offset, offset, end - offset); | |
cb95e7bf | 5918 | } else { |
3ea4dc5b | 5919 | ret = send_extent_data(sctx, path, offset, end - offset); |
31db9f7c | 5920 | } |
c9a949af | 5921 | sctx->cur_inode_next_write_offset = end; |
31db9f7c AB |
5922 | return ret; |
5923 | } | |
5924 | ||
5925 | static int is_extent_unchanged(struct send_ctx *sctx, | |
5926 | struct btrfs_path *left_path, | |
5927 | struct btrfs_key *ekey) | |
5928 | { | |
5929 | int ret = 0; | |
5930 | struct btrfs_key key; | |
5931 | struct btrfs_path *path = NULL; | |
5932 | struct extent_buffer *eb; | |
5933 | int slot; | |
5934 | struct btrfs_key found_key; | |
5935 | struct btrfs_file_extent_item *ei; | |
5936 | u64 left_disknr; | |
5937 | u64 right_disknr; | |
5938 | u64 left_offset; | |
5939 | u64 right_offset; | |
5940 | u64 left_offset_fixed; | |
5941 | u64 left_len; | |
5942 | u64 right_len; | |
74dd17fb CM |
5943 | u64 left_gen; |
5944 | u64 right_gen; | |
31db9f7c AB |
5945 | u8 left_type; |
5946 | u8 right_type; | |
5947 | ||
5948 | path = alloc_path_for_send(); | |
5949 | if (!path) | |
5950 | return -ENOMEM; | |
5951 | ||
5952 | eb = left_path->nodes[0]; | |
5953 | slot = left_path->slots[0]; | |
31db9f7c AB |
5954 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
5955 | left_type = btrfs_file_extent_type(eb, ei); | |
31db9f7c AB |
5956 | |
5957 | if (left_type != BTRFS_FILE_EXTENT_REG) { | |
5958 | ret = 0; | |
5959 | goto out; | |
5960 | } | |
74dd17fb CM |
5961 | left_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
5962 | left_len = btrfs_file_extent_num_bytes(eb, ei); | |
5963 | left_offset = btrfs_file_extent_offset(eb, ei); | |
5964 | left_gen = btrfs_file_extent_generation(eb, ei); | |
31db9f7c AB |
5965 | |
5966 | /* | |
5967 | * Following comments will refer to these graphics. L is the left | |
5968 | * extents which we are checking at the moment. 1-8 are the right | |
5969 | * extents that we iterate. | |
5970 | * | |
5971 | * |-----L-----| | |
5972 | * |-1-|-2a-|-3-|-4-|-5-|-6-| | |
5973 | * | |
5974 | * |-----L-----| | |
5975 | * |--1--|-2b-|...(same as above) | |
5976 | * | |
5977 | * Alternative situation. Happens on files where extents got split. | |
5978 | * |-----L-----| | |
5979 | * |-----------7-----------|-6-| | |
5980 | * | |
5981 | * Alternative situation. Happens on files which got larger. | |
5982 | * |-----L-----| | |
5983 | * |-8-| | |
5984 | * Nothing follows after 8. | |
5985 | */ | |
5986 | ||
5987 | key.objectid = ekey->objectid; | |
5988 | key.type = BTRFS_EXTENT_DATA_KEY; | |
5989 | key.offset = ekey->offset; | |
5990 | ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); | |
5991 | if (ret < 0) | |
5992 | goto out; | |
5993 | if (ret) { | |
5994 | ret = 0; | |
5995 | goto out; | |
5996 | } | |
5997 | ||
5998 | /* | |
5999 | * Handle special case where the right side has no extents at all. | |
6000 | */ | |
6001 | eb = path->nodes[0]; | |
6002 | slot = path->slots[0]; | |
6003 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
6004 | if (found_key.objectid != key.objectid || | |
6005 | found_key.type != key.type) { | |
57cfd462 JB |
6006 | /* If we're a hole then just pretend nothing changed */ |
6007 | ret = (left_disknr) ? 0 : 1; | |
31db9f7c AB |
6008 | goto out; |
6009 | } | |
6010 | ||
6011 | /* | |
6012 | * We're now on 2a, 2b or 7. | |
6013 | */ | |
6014 | key = found_key; | |
6015 | while (key.offset < ekey->offset + left_len) { | |
6016 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); | |
6017 | right_type = btrfs_file_extent_type(eb, ei); | |
e1cbfd7b FM |
6018 | if (right_type != BTRFS_FILE_EXTENT_REG && |
6019 | right_type != BTRFS_FILE_EXTENT_INLINE) { | |
31db9f7c AB |
6020 | ret = 0; |
6021 | goto out; | |
6022 | } | |
6023 | ||
e1cbfd7b | 6024 | if (right_type == BTRFS_FILE_EXTENT_INLINE) { |
e41ca589 | 6025 | right_len = btrfs_file_extent_ram_bytes(eb, ei); |
e1cbfd7b FM |
6026 | right_len = PAGE_ALIGN(right_len); |
6027 | } else { | |
6028 | right_len = btrfs_file_extent_num_bytes(eb, ei); | |
6029 | } | |
007d31f7 | 6030 | |
31db9f7c AB |
6031 | /* |
6032 | * Are we at extent 8? If yes, we know the extent is changed. | |
6033 | * This may only happen on the first iteration. | |
6034 | */ | |
d8347fa4 | 6035 | if (found_key.offset + right_len <= ekey->offset) { |
57cfd462 JB |
6036 | /* If we're a hole just pretend nothing changed */ |
6037 | ret = (left_disknr) ? 0 : 1; | |
31db9f7c AB |
6038 | goto out; |
6039 | } | |
6040 | ||
e1cbfd7b FM |
6041 | /* |
6042 | * We just wanted to see if when we have an inline extent, what | |
6043 | * follows it is a regular extent (wanted to check the above | |
6044 | * condition for inline extents too). This should normally not | |
6045 | * happen but it's possible for example when we have an inline | |
6046 | * compressed extent representing data with a size matching | |
6047 | * the page size (currently the same as sector size). | |
6048 | */ | |
6049 | if (right_type == BTRFS_FILE_EXTENT_INLINE) { | |
6050 | ret = 0; | |
6051 | goto out; | |
6052 | } | |
6053 | ||
24e52b11 FM |
6054 | right_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
6055 | right_offset = btrfs_file_extent_offset(eb, ei); | |
6056 | right_gen = btrfs_file_extent_generation(eb, ei); | |
6057 | ||
31db9f7c AB |
6058 | left_offset_fixed = left_offset; |
6059 | if (key.offset < ekey->offset) { | |
6060 | /* Fix the right offset for 2a and 7. */ | |
6061 | right_offset += ekey->offset - key.offset; | |
6062 | } else { | |
6063 | /* Fix the left offset for all behind 2a and 2b */ | |
6064 | left_offset_fixed += key.offset - ekey->offset; | |
6065 | } | |
6066 | ||
6067 | /* | |
6068 | * Check if we have the same extent. | |
6069 | */ | |
3954096d | 6070 | if (left_disknr != right_disknr || |
74dd17fb CM |
6071 | left_offset_fixed != right_offset || |
6072 | left_gen != right_gen) { | |
31db9f7c AB |
6073 | ret = 0; |
6074 | goto out; | |
6075 | } | |
6076 | ||
6077 | /* | |
6078 | * Go to the next extent. | |
6079 | */ | |
6080 | ret = btrfs_next_item(sctx->parent_root, path); | |
6081 | if (ret < 0) | |
6082 | goto out; | |
6083 | if (!ret) { | |
6084 | eb = path->nodes[0]; | |
6085 | slot = path->slots[0]; | |
6086 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
6087 | } | |
6088 | if (ret || found_key.objectid != key.objectid || | |
6089 | found_key.type != key.type) { | |
6090 | key.offset += right_len; | |
6091 | break; | |
adaa4b8e JS |
6092 | } |
6093 | if (found_key.offset != key.offset + right_len) { | |
6094 | ret = 0; | |
6095 | goto out; | |
31db9f7c AB |
6096 | } |
6097 | key = found_key; | |
6098 | } | |
6099 | ||
6100 | /* | |
6101 | * We're now behind the left extent (treat as unchanged) or at the end | |
6102 | * of the right side (treat as changed). | |
6103 | */ | |
6104 | if (key.offset >= ekey->offset + left_len) | |
6105 | ret = 1; | |
6106 | else | |
6107 | ret = 0; | |
6108 | ||
6109 | ||
6110 | out: | |
6111 | btrfs_free_path(path); | |
6112 | return ret; | |
6113 | } | |
6114 | ||
16e7549f JB |
6115 | static int get_last_extent(struct send_ctx *sctx, u64 offset) |
6116 | { | |
6117 | struct btrfs_path *path; | |
6118 | struct btrfs_root *root = sctx->send_root; | |
16e7549f | 6119 | struct btrfs_key key; |
16e7549f JB |
6120 | int ret; |
6121 | ||
6122 | path = alloc_path_for_send(); | |
6123 | if (!path) | |
6124 | return -ENOMEM; | |
6125 | ||
6126 | sctx->cur_inode_last_extent = 0; | |
6127 | ||
6128 | key.objectid = sctx->cur_ino; | |
6129 | key.type = BTRFS_EXTENT_DATA_KEY; | |
6130 | key.offset = offset; | |
6131 | ret = btrfs_search_slot_for_read(root, &key, path, 0, 1); | |
6132 | if (ret < 0) | |
6133 | goto out; | |
6134 | ret = 0; | |
6135 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
6136 | if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY) | |
6137 | goto out; | |
6138 | ||
a5eeb3d1 | 6139 | sctx->cur_inode_last_extent = btrfs_file_extent_end(path); |
16e7549f JB |
6140 | out: |
6141 | btrfs_free_path(path); | |
6142 | return ret; | |
6143 | } | |
6144 | ||
82bfb2e7 FM |
6145 | static int range_is_hole_in_parent(struct send_ctx *sctx, |
6146 | const u64 start, | |
6147 | const u64 end) | |
6148 | { | |
6149 | struct btrfs_path *path; | |
6150 | struct btrfs_key key; | |
6151 | struct btrfs_root *root = sctx->parent_root; | |
6152 | u64 search_start = start; | |
6153 | int ret; | |
6154 | ||
6155 | path = alloc_path_for_send(); | |
6156 | if (!path) | |
6157 | return -ENOMEM; | |
6158 | ||
6159 | key.objectid = sctx->cur_ino; | |
6160 | key.type = BTRFS_EXTENT_DATA_KEY; | |
6161 | key.offset = search_start; | |
6162 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6163 | if (ret < 0) | |
6164 | goto out; | |
6165 | if (ret > 0 && path->slots[0] > 0) | |
6166 | path->slots[0]--; | |
6167 | ||
6168 | while (search_start < end) { | |
6169 | struct extent_buffer *leaf = path->nodes[0]; | |
6170 | int slot = path->slots[0]; | |
6171 | struct btrfs_file_extent_item *fi; | |
6172 | u64 extent_end; | |
6173 | ||
6174 | if (slot >= btrfs_header_nritems(leaf)) { | |
6175 | ret = btrfs_next_leaf(root, path); | |
6176 | if (ret < 0) | |
6177 | goto out; | |
6178 | else if (ret > 0) | |
6179 | break; | |
6180 | continue; | |
6181 | } | |
6182 | ||
6183 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
6184 | if (key.objectid < sctx->cur_ino || | |
6185 | key.type < BTRFS_EXTENT_DATA_KEY) | |
6186 | goto next; | |
6187 | if (key.objectid > sctx->cur_ino || | |
6188 | key.type > BTRFS_EXTENT_DATA_KEY || | |
6189 | key.offset >= end) | |
6190 | break; | |
6191 | ||
6192 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
a5eeb3d1 | 6193 | extent_end = btrfs_file_extent_end(path); |
82bfb2e7 FM |
6194 | if (extent_end <= start) |
6195 | goto next; | |
6196 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) { | |
6197 | search_start = extent_end; | |
6198 | goto next; | |
6199 | } | |
6200 | ret = 0; | |
6201 | goto out; | |
6202 | next: | |
6203 | path->slots[0]++; | |
6204 | } | |
6205 | ret = 1; | |
6206 | out: | |
6207 | btrfs_free_path(path); | |
6208 | return ret; | |
6209 | } | |
6210 | ||
16e7549f JB |
6211 | static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path, |
6212 | struct btrfs_key *key) | |
6213 | { | |
16e7549f JB |
6214 | int ret = 0; |
6215 | ||
6216 | if (sctx->cur_ino != key->objectid || !need_send_hole(sctx)) | |
6217 | return 0; | |
6218 | ||
6219 | if (sctx->cur_inode_last_extent == (u64)-1) { | |
6220 | ret = get_last_extent(sctx, key->offset - 1); | |
6221 | if (ret) | |
6222 | return ret; | |
6223 | } | |
6224 | ||
bf54f412 FDBM |
6225 | if (path->slots[0] == 0 && |
6226 | sctx->cur_inode_last_extent < key->offset) { | |
6227 | /* | |
6228 | * We might have skipped entire leafs that contained only | |
6229 | * file extent items for our current inode. These leafs have | |
6230 | * a generation number smaller (older) than the one in the | |
6231 | * current leaf and the leaf our last extent came from, and | |
6232 | * are located between these 2 leafs. | |
6233 | */ | |
6234 | ret = get_last_extent(sctx, key->offset - 1); | |
6235 | if (ret) | |
6236 | return ret; | |
6237 | } | |
6238 | ||
82bfb2e7 FM |
6239 | if (sctx->cur_inode_last_extent < key->offset) { |
6240 | ret = range_is_hole_in_parent(sctx, | |
6241 | sctx->cur_inode_last_extent, | |
6242 | key->offset); | |
6243 | if (ret < 0) | |
6244 | return ret; | |
6245 | else if (ret == 0) | |
6246 | ret = send_hole(sctx, key->offset); | |
6247 | else | |
6248 | ret = 0; | |
6249 | } | |
a5eeb3d1 | 6250 | sctx->cur_inode_last_extent = btrfs_file_extent_end(path); |
16e7549f JB |
6251 | return ret; |
6252 | } | |
6253 | ||
31db9f7c AB |
6254 | static int process_extent(struct send_ctx *sctx, |
6255 | struct btrfs_path *path, | |
6256 | struct btrfs_key *key) | |
6257 | { | |
31db9f7c | 6258 | struct clone_root *found_clone = NULL; |
57cfd462 | 6259 | int ret = 0; |
31db9f7c AB |
6260 | |
6261 | if (S_ISLNK(sctx->cur_inode_mode)) | |
6262 | return 0; | |
6263 | ||
6264 | if (sctx->parent_root && !sctx->cur_inode_new) { | |
6265 | ret = is_extent_unchanged(sctx, path, key); | |
6266 | if (ret < 0) | |
6267 | goto out; | |
6268 | if (ret) { | |
6269 | ret = 0; | |
16e7549f | 6270 | goto out_hole; |
31db9f7c | 6271 | } |
57cfd462 JB |
6272 | } else { |
6273 | struct btrfs_file_extent_item *ei; | |
6274 | u8 type; | |
6275 | ||
6276 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
6277 | struct btrfs_file_extent_item); | |
6278 | type = btrfs_file_extent_type(path->nodes[0], ei); | |
6279 | if (type == BTRFS_FILE_EXTENT_PREALLOC || | |
6280 | type == BTRFS_FILE_EXTENT_REG) { | |
6281 | /* | |
6282 | * The send spec does not have a prealloc command yet, | |
6283 | * so just leave a hole for prealloc'ed extents until | |
6284 | * we have enough commands queued up to justify rev'ing | |
6285 | * the send spec. | |
6286 | */ | |
6287 | if (type == BTRFS_FILE_EXTENT_PREALLOC) { | |
6288 | ret = 0; | |
6289 | goto out; | |
6290 | } | |
6291 | ||
6292 | /* Have a hole, just skip it. */ | |
6293 | if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) { | |
6294 | ret = 0; | |
6295 | goto out; | |
6296 | } | |
6297 | } | |
31db9f7c AB |
6298 | } |
6299 | ||
6300 | ret = find_extent_clone(sctx, path, key->objectid, key->offset, | |
6301 | sctx->cur_inode_size, &found_clone); | |
6302 | if (ret != -ENOENT && ret < 0) | |
6303 | goto out; | |
6304 | ||
6305 | ret = send_write_or_clone(sctx, path, key, found_clone); | |
16e7549f JB |
6306 | if (ret) |
6307 | goto out; | |
6308 | out_hole: | |
6309 | ret = maybe_send_hole(sctx, path, key); | |
31db9f7c AB |
6310 | out: |
6311 | return ret; | |
6312 | } | |
6313 | ||
6314 | static int process_all_extents(struct send_ctx *sctx) | |
6315 | { | |
9930e9d4 GN |
6316 | int ret = 0; |
6317 | int iter_ret = 0; | |
31db9f7c AB |
6318 | struct btrfs_root *root; |
6319 | struct btrfs_path *path; | |
6320 | struct btrfs_key key; | |
6321 | struct btrfs_key found_key; | |
31db9f7c AB |
6322 | |
6323 | root = sctx->send_root; | |
6324 | path = alloc_path_for_send(); | |
6325 | if (!path) | |
6326 | return -ENOMEM; | |
6327 | ||
6328 | key.objectid = sctx->cmp_key->objectid; | |
6329 | key.type = BTRFS_EXTENT_DATA_KEY; | |
6330 | key.offset = 0; | |
9930e9d4 | 6331 | btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) { |
31db9f7c AB |
6332 | if (found_key.objectid != key.objectid || |
6333 | found_key.type != key.type) { | |
6334 | ret = 0; | |
9930e9d4 | 6335 | break; |
31db9f7c AB |
6336 | } |
6337 | ||
6338 | ret = process_extent(sctx, path, &found_key); | |
6339 | if (ret < 0) | |
9930e9d4 | 6340 | break; |
31db9f7c | 6341 | } |
9930e9d4 GN |
6342 | /* Catch error found during iteration */ |
6343 | if (iter_ret < 0) | |
6344 | ret = iter_ret; | |
31db9f7c | 6345 | |
31db9f7c AB |
6346 | btrfs_free_path(path); |
6347 | return ret; | |
6348 | } | |
6349 | ||
9f03740a FDBM |
6350 | static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end, |
6351 | int *pending_move, | |
6352 | int *refs_processed) | |
31db9f7c AB |
6353 | { |
6354 | int ret = 0; | |
6355 | ||
6356 | if (sctx->cur_ino == 0) | |
6357 | goto out; | |
6358 | if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && | |
96b5bd77 | 6359 | sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY) |
31db9f7c AB |
6360 | goto out; |
6361 | if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) | |
6362 | goto out; | |
6363 | ||
9f03740a | 6364 | ret = process_recorded_refs(sctx, pending_move); |
e479d9bb AB |
6365 | if (ret < 0) |
6366 | goto out; | |
6367 | ||
9f03740a | 6368 | *refs_processed = 1; |
31db9f7c AB |
6369 | out: |
6370 | return ret; | |
6371 | } | |
6372 | ||
6373 | static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) | |
6374 | { | |
6375 | int ret = 0; | |
7e93f6dc | 6376 | struct btrfs_inode_info info; |
31db9f7c AB |
6377 | u64 left_mode; |
6378 | u64 left_uid; | |
6379 | u64 left_gid; | |
48247359 | 6380 | u64 left_fileattr; |
31db9f7c AB |
6381 | u64 right_mode; |
6382 | u64 right_uid; | |
6383 | u64 right_gid; | |
48247359 | 6384 | u64 right_fileattr; |
31db9f7c AB |
6385 | int need_chmod = 0; |
6386 | int need_chown = 0; | |
48247359 | 6387 | bool need_fileattr = false; |
ffa7c429 | 6388 | int need_truncate = 1; |
9f03740a FDBM |
6389 | int pending_move = 0; |
6390 | int refs_processed = 0; | |
31db9f7c | 6391 | |
46b2f459 FM |
6392 | if (sctx->ignore_cur_inode) |
6393 | return 0; | |
6394 | ||
9f03740a FDBM |
6395 | ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move, |
6396 | &refs_processed); | |
31db9f7c AB |
6397 | if (ret < 0) |
6398 | goto out; | |
6399 | ||
9f03740a FDBM |
6400 | /* |
6401 | * We have processed the refs and thus need to advance send_progress. | |
6402 | * Now, calls to get_cur_xxx will take the updated refs of the current | |
6403 | * inode into account. | |
6404 | * | |
6405 | * On the other hand, if our current inode is a directory and couldn't | |
6406 | * be moved/renamed because its parent was renamed/moved too and it has | |
6407 | * a higher inode number, we can only move/rename our current inode | |
6408 | * after we moved/renamed its parent. Therefore in this case operate on | |
6409 | * the old path (pre move/rename) of our current inode, and the | |
6410 | * move/rename will be performed later. | |
6411 | */ | |
6412 | if (refs_processed && !pending_move) | |
6413 | sctx->send_progress = sctx->cur_ino + 1; | |
6414 | ||
31db9f7c AB |
6415 | if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) |
6416 | goto out; | |
6417 | if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) | |
6418 | goto out; | |
7e93f6dc | 6419 | ret = get_inode_info(sctx->send_root, sctx->cur_ino, &info); |
31db9f7c AB |
6420 | if (ret < 0) |
6421 | goto out; | |
7e93f6dc BC |
6422 | left_mode = info.mode; |
6423 | left_uid = info.uid; | |
6424 | left_gid = info.gid; | |
6425 | left_fileattr = info.fileattr; | |
31db9f7c | 6426 | |
e2d044fe AL |
6427 | if (!sctx->parent_root || sctx->cur_inode_new) { |
6428 | need_chown = 1; | |
6429 | if (!S_ISLNK(sctx->cur_inode_mode)) | |
31db9f7c | 6430 | need_chmod = 1; |
ffa7c429 FM |
6431 | if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size) |
6432 | need_truncate = 0; | |
e2d044fe | 6433 | } else { |
ffa7c429 FM |
6434 | u64 old_size; |
6435 | ||
7e93f6dc | 6436 | ret = get_inode_info(sctx->parent_root, sctx->cur_ino, &info); |
e2d044fe AL |
6437 | if (ret < 0) |
6438 | goto out; | |
7e93f6dc BC |
6439 | old_size = info.size; |
6440 | right_mode = info.mode; | |
6441 | right_uid = info.uid; | |
6442 | right_gid = info.gid; | |
6443 | right_fileattr = info.fileattr; | |
31db9f7c | 6444 | |
e2d044fe AL |
6445 | if (left_uid != right_uid || left_gid != right_gid) |
6446 | need_chown = 1; | |
6447 | if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode) | |
6448 | need_chmod = 1; | |
48247359 DS |
6449 | if (!S_ISLNK(sctx->cur_inode_mode) && left_fileattr != right_fileattr) |
6450 | need_fileattr = true; | |
ffa7c429 FM |
6451 | if ((old_size == sctx->cur_inode_size) || |
6452 | (sctx->cur_inode_size > old_size && | |
6453 | sctx->cur_inode_next_write_offset == sctx->cur_inode_size)) | |
6454 | need_truncate = 0; | |
31db9f7c AB |
6455 | } |
6456 | ||
6457 | if (S_ISREG(sctx->cur_inode_mode)) { | |
16e7549f | 6458 | if (need_send_hole(sctx)) { |
766b5e5a FM |
6459 | if (sctx->cur_inode_last_extent == (u64)-1 || |
6460 | sctx->cur_inode_last_extent < | |
6461 | sctx->cur_inode_size) { | |
16e7549f JB |
6462 | ret = get_last_extent(sctx, (u64)-1); |
6463 | if (ret) | |
6464 | goto out; | |
6465 | } | |
6466 | if (sctx->cur_inode_last_extent < | |
6467 | sctx->cur_inode_size) { | |
6468 | ret = send_hole(sctx, sctx->cur_inode_size); | |
6469 | if (ret) | |
6470 | goto out; | |
6471 | } | |
6472 | } | |
ffa7c429 FM |
6473 | if (need_truncate) { |
6474 | ret = send_truncate(sctx, sctx->cur_ino, | |
6475 | sctx->cur_inode_gen, | |
6476 | sctx->cur_inode_size); | |
6477 | if (ret < 0) | |
6478 | goto out; | |
6479 | } | |
31db9f7c AB |
6480 | } |
6481 | ||
6482 | if (need_chown) { | |
6483 | ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
6484 | left_uid, left_gid); | |
6485 | if (ret < 0) | |
6486 | goto out; | |
6487 | } | |
6488 | if (need_chmod) { | |
6489 | ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
6490 | left_mode); | |
6491 | if (ret < 0) | |
6492 | goto out; | |
6493 | } | |
48247359 DS |
6494 | if (need_fileattr) { |
6495 | ret = send_fileattr(sctx, sctx->cur_ino, sctx->cur_inode_gen, | |
6496 | left_fileattr); | |
6497 | if (ret < 0) | |
6498 | goto out; | |
6499 | } | |
c86eab81 DS |
6500 | |
6501 | if (proto_cmd_ok(sctx, BTRFS_SEND_C_ENABLE_VERITY) | |
6502 | && sctx->cur_inode_needs_verity) { | |
38622010 BB |
6503 | ret = process_verity(sctx); |
6504 | if (ret < 0) | |
6505 | goto out; | |
6506 | } | |
31db9f7c | 6507 | |
89efda52 MPS |
6508 | ret = send_capabilities(sctx); |
6509 | if (ret < 0) | |
6510 | goto out; | |
6511 | ||
31db9f7c | 6512 | /* |
9f03740a FDBM |
6513 | * If other directory inodes depended on our current directory |
6514 | * inode's move/rename, now do their move/rename operations. | |
31db9f7c | 6515 | */ |
9f03740a FDBM |
6516 | if (!is_waiting_for_move(sctx, sctx->cur_ino)) { |
6517 | ret = apply_children_dir_moves(sctx); | |
6518 | if (ret) | |
6519 | goto out; | |
fcbd2154 FM |
6520 | /* |
6521 | * Need to send that every time, no matter if it actually | |
6522 | * changed between the two trees as we have done changes to | |
6523 | * the inode before. If our inode is a directory and it's | |
6524 | * waiting to be moved/renamed, we will send its utimes when | |
6525 | * it's moved/renamed, therefore we don't need to do it here. | |
6526 | */ | |
6527 | sctx->send_progress = sctx->cur_ino + 1; | |
6528 | ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); | |
6529 | if (ret < 0) | |
6530 | goto out; | |
9f03740a FDBM |
6531 | } |
6532 | ||
31db9f7c AB |
6533 | out: |
6534 | return ret; | |
6535 | } | |
6536 | ||
152555b3 FM |
6537 | static void close_current_inode(struct send_ctx *sctx) |
6538 | { | |
6539 | u64 i_size; | |
6540 | ||
6541 | if (sctx->cur_inode == NULL) | |
6542 | return; | |
6543 | ||
6544 | i_size = i_size_read(sctx->cur_inode); | |
6545 | ||
6546 | /* | |
6547 | * If we are doing an incremental send, we may have extents between the | |
6548 | * last processed extent and the i_size that have not been processed | |
6549 | * because they haven't changed but we may have read some of their pages | |
6550 | * through readahead, see the comments at send_extent_data(). | |
6551 | */ | |
6552 | if (sctx->clean_page_cache && sctx->page_cache_clear_start < i_size) | |
6553 | truncate_inode_pages_range(&sctx->cur_inode->i_data, | |
6554 | sctx->page_cache_clear_start, | |
6555 | round_up(i_size, PAGE_SIZE) - 1); | |
6556 | ||
6557 | iput(sctx->cur_inode); | |
6558 | sctx->cur_inode = NULL; | |
6559 | } | |
6560 | ||
31db9f7c AB |
6561 | static int changed_inode(struct send_ctx *sctx, |
6562 | enum btrfs_compare_tree_result result) | |
6563 | { | |
6564 | int ret = 0; | |
6565 | struct btrfs_key *key = sctx->cmp_key; | |
6566 | struct btrfs_inode_item *left_ii = NULL; | |
6567 | struct btrfs_inode_item *right_ii = NULL; | |
6568 | u64 left_gen = 0; | |
6569 | u64 right_gen = 0; | |
6570 | ||
152555b3 | 6571 | close_current_inode(sctx); |
521b6803 | 6572 | |
31db9f7c | 6573 | sctx->cur_ino = key->objectid; |
9555e1f1 | 6574 | sctx->cur_inode_new_gen = false; |
16e7549f | 6575 | sctx->cur_inode_last_extent = (u64)-1; |
ffa7c429 | 6576 | sctx->cur_inode_next_write_offset = 0; |
46b2f459 | 6577 | sctx->ignore_cur_inode = false; |
e479d9bb AB |
6578 | |
6579 | /* | |
6580 | * Set send_progress to current inode. This will tell all get_cur_xxx | |
6581 | * functions that the current inode's refs are not updated yet. Later, | |
6582 | * when process_recorded_refs is finished, it is set to cur_ino + 1. | |
6583 | */ | |
31db9f7c AB |
6584 | sctx->send_progress = sctx->cur_ino; |
6585 | ||
6586 | if (result == BTRFS_COMPARE_TREE_NEW || | |
6587 | result == BTRFS_COMPARE_TREE_CHANGED) { | |
6588 | left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], | |
6589 | sctx->left_path->slots[0], | |
6590 | struct btrfs_inode_item); | |
6591 | left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], | |
6592 | left_ii); | |
6593 | } else { | |
6594 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], | |
6595 | sctx->right_path->slots[0], | |
6596 | struct btrfs_inode_item); | |
6597 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], | |
6598 | right_ii); | |
6599 | } | |
6600 | if (result == BTRFS_COMPARE_TREE_CHANGED) { | |
6601 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], | |
6602 | sctx->right_path->slots[0], | |
6603 | struct btrfs_inode_item); | |
6604 | ||
6605 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], | |
6606 | right_ii); | |
6d85ed05 AB |
6607 | |
6608 | /* | |
6609 | * The cur_ino = root dir case is special here. We can't treat | |
6610 | * the inode as deleted+reused because it would generate a | |
6611 | * stream that tries to delete/mkdir the root dir. | |
6612 | */ | |
6613 | if (left_gen != right_gen && | |
6614 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) | |
9555e1f1 | 6615 | sctx->cur_inode_new_gen = true; |
31db9f7c AB |
6616 | } |
6617 | ||
46b2f459 FM |
6618 | /* |
6619 | * Normally we do not find inodes with a link count of zero (orphans) | |
6620 | * because the most common case is to create a snapshot and use it | |
6621 | * for a send operation. However other less common use cases involve | |
6622 | * using a subvolume and send it after turning it to RO mode just | |
6623 | * after deleting all hard links of a file while holding an open | |
6624 | * file descriptor against it or turning a RO snapshot into RW mode, | |
6625 | * keep an open file descriptor against a file, delete it and then | |
6626 | * turn the snapshot back to RO mode before using it for a send | |
9ed0a72e BC |
6627 | * operation. The former is what the receiver operation does. |
6628 | * Therefore, if we want to send these snapshots soon after they're | |
6629 | * received, we need to handle orphan inodes as well. Moreover, orphans | |
6630 | * can appear not only in the send snapshot but also in the parent | |
6631 | * snapshot. Here are several cases: | |
6632 | * | |
6633 | * Case 1: BTRFS_COMPARE_TREE_NEW | |
6634 | * | send snapshot | action | |
6635 | * -------------------------------- | |
6636 | * nlink | 0 | ignore | |
6637 | * | |
6638 | * Case 2: BTRFS_COMPARE_TREE_DELETED | |
6639 | * | parent snapshot | action | |
6640 | * ---------------------------------- | |
6641 | * nlink | 0 | as usual | |
6642 | * Note: No unlinks will be sent because there're no paths for it. | |
6643 | * | |
6644 | * Case 3: BTRFS_COMPARE_TREE_CHANGED | |
6645 | * | | parent snapshot | send snapshot | action | |
6646 | * ----------------------------------------------------------------------- | |
6647 | * subcase 1 | nlink | 0 | 0 | ignore | |
6648 | * subcase 2 | nlink | >0 | 0 | new_gen(deletion) | |
6649 | * subcase 3 | nlink | 0 | >0 | new_gen(creation) | |
6650 | * | |
46b2f459 | 6651 | */ |
9ed0a72e BC |
6652 | if (result == BTRFS_COMPARE_TREE_NEW) { |
6653 | if (btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii) == 0) { | |
46b2f459 | 6654 | sctx->ignore_cur_inode = true; |
46b2f459 FM |
6655 | goto out; |
6656 | } | |
31db9f7c | 6657 | sctx->cur_inode_gen = left_gen; |
9555e1f1 DS |
6658 | sctx->cur_inode_new = true; |
6659 | sctx->cur_inode_deleted = false; | |
31db9f7c AB |
6660 | sctx->cur_inode_size = btrfs_inode_size( |
6661 | sctx->left_path->nodes[0], left_ii); | |
6662 | sctx->cur_inode_mode = btrfs_inode_mode( | |
6663 | sctx->left_path->nodes[0], left_ii); | |
644d1940 LB |
6664 | sctx->cur_inode_rdev = btrfs_inode_rdev( |
6665 | sctx->left_path->nodes[0], left_ii); | |
31db9f7c | 6666 | if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) |
1f4692da | 6667 | ret = send_create_inode_if_needed(sctx); |
31db9f7c AB |
6668 | } else if (result == BTRFS_COMPARE_TREE_DELETED) { |
6669 | sctx->cur_inode_gen = right_gen; | |
9555e1f1 DS |
6670 | sctx->cur_inode_new = false; |
6671 | sctx->cur_inode_deleted = true; | |
31db9f7c AB |
6672 | sctx->cur_inode_size = btrfs_inode_size( |
6673 | sctx->right_path->nodes[0], right_ii); | |
6674 | sctx->cur_inode_mode = btrfs_inode_mode( | |
6675 | sctx->right_path->nodes[0], right_ii); | |
6676 | } else if (result == BTRFS_COMPARE_TREE_CHANGED) { | |
9ed0a72e BC |
6677 | u32 new_nlinks, old_nlinks; |
6678 | ||
6679 | new_nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii); | |
6680 | old_nlinks = btrfs_inode_nlink(sctx->right_path->nodes[0], right_ii); | |
6681 | if (new_nlinks == 0 && old_nlinks == 0) { | |
6682 | sctx->ignore_cur_inode = true; | |
6683 | goto out; | |
6684 | } else if (new_nlinks == 0 || old_nlinks == 0) { | |
6685 | sctx->cur_inode_new_gen = 1; | |
6686 | } | |
766702ef AB |
6687 | /* |
6688 | * We need to do some special handling in case the inode was | |
6689 | * reported as changed with a changed generation number. This | |
6690 | * means that the original inode was deleted and new inode | |
6691 | * reused the same inum. So we have to treat the old inode as | |
6692 | * deleted and the new one as new. | |
6693 | */ | |
31db9f7c | 6694 | if (sctx->cur_inode_new_gen) { |
766702ef AB |
6695 | /* |
6696 | * First, process the inode as if it was deleted. | |
6697 | */ | |
9b8be45f BC |
6698 | if (old_nlinks > 0) { |
6699 | sctx->cur_inode_gen = right_gen; | |
6700 | sctx->cur_inode_new = false; | |
6701 | sctx->cur_inode_deleted = true; | |
6702 | sctx->cur_inode_size = btrfs_inode_size( | |
6703 | sctx->right_path->nodes[0], right_ii); | |
6704 | sctx->cur_inode_mode = btrfs_inode_mode( | |
6705 | sctx->right_path->nodes[0], right_ii); | |
6706 | ret = process_all_refs(sctx, | |
6707 | BTRFS_COMPARE_TREE_DELETED); | |
6708 | if (ret < 0) | |
6709 | goto out; | |
6710 | } | |
31db9f7c | 6711 | |
766702ef AB |
6712 | /* |
6713 | * Now process the inode as if it was new. | |
6714 | */ | |
9ed0a72e BC |
6715 | if (new_nlinks > 0) { |
6716 | sctx->cur_inode_gen = left_gen; | |
6717 | sctx->cur_inode_new = true; | |
6718 | sctx->cur_inode_deleted = false; | |
6719 | sctx->cur_inode_size = btrfs_inode_size( | |
6720 | sctx->left_path->nodes[0], | |
6721 | left_ii); | |
6722 | sctx->cur_inode_mode = btrfs_inode_mode( | |
6723 | sctx->left_path->nodes[0], | |
6724 | left_ii); | |
6725 | sctx->cur_inode_rdev = btrfs_inode_rdev( | |
6726 | sctx->left_path->nodes[0], | |
6727 | left_ii); | |
6728 | ret = send_create_inode_if_needed(sctx); | |
6729 | if (ret < 0) | |
6730 | goto out; | |
31db9f7c | 6731 | |
9ed0a72e BC |
6732 | ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); |
6733 | if (ret < 0) | |
6734 | goto out; | |
6735 | /* | |
6736 | * Advance send_progress now as we did not get | |
6737 | * into process_recorded_refs_if_needed in the | |
6738 | * new_gen case. | |
6739 | */ | |
6740 | sctx->send_progress = sctx->cur_ino + 1; | |
766702ef | 6741 | |
9ed0a72e BC |
6742 | /* |
6743 | * Now process all extents and xattrs of the | |
6744 | * inode as if they were all new. | |
6745 | */ | |
6746 | ret = process_all_extents(sctx); | |
6747 | if (ret < 0) | |
6748 | goto out; | |
6749 | ret = process_all_new_xattrs(sctx); | |
6750 | if (ret < 0) | |
6751 | goto out; | |
6752 | } | |
31db9f7c AB |
6753 | } else { |
6754 | sctx->cur_inode_gen = left_gen; | |
9555e1f1 DS |
6755 | sctx->cur_inode_new = false; |
6756 | sctx->cur_inode_new_gen = false; | |
6757 | sctx->cur_inode_deleted = false; | |
31db9f7c AB |
6758 | sctx->cur_inode_size = btrfs_inode_size( |
6759 | sctx->left_path->nodes[0], left_ii); | |
6760 | sctx->cur_inode_mode = btrfs_inode_mode( | |
6761 | sctx->left_path->nodes[0], left_ii); | |
6762 | } | |
6763 | } | |
6764 | ||
6765 | out: | |
6766 | return ret; | |
6767 | } | |
6768 | ||
766702ef AB |
6769 | /* |
6770 | * We have to process new refs before deleted refs, but compare_trees gives us | |
6771 | * the new and deleted refs mixed. To fix this, we record the new/deleted refs | |
6772 | * first and later process them in process_recorded_refs. | |
6773 | * For the cur_inode_new_gen case, we skip recording completely because | |
6774 | * changed_inode did already initiate processing of refs. The reason for this is | |
6775 | * that in this case, compare_tree actually compares the refs of 2 different | |
6776 | * inodes. To fix this, process_all_refs is used in changed_inode to handle all | |
6777 | * refs of the right tree as deleted and all refs of the left tree as new. | |
6778 | */ | |
31db9f7c AB |
6779 | static int changed_ref(struct send_ctx *sctx, |
6780 | enum btrfs_compare_tree_result result) | |
6781 | { | |
6782 | int ret = 0; | |
6783 | ||
95155585 FM |
6784 | if (sctx->cur_ino != sctx->cmp_key->objectid) { |
6785 | inconsistent_snapshot_error(sctx, result, "reference"); | |
6786 | return -EIO; | |
6787 | } | |
31db9f7c AB |
6788 | |
6789 | if (!sctx->cur_inode_new_gen && | |
6790 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { | |
6791 | if (result == BTRFS_COMPARE_TREE_NEW) | |
6792 | ret = record_new_ref(sctx); | |
6793 | else if (result == BTRFS_COMPARE_TREE_DELETED) | |
6794 | ret = record_deleted_ref(sctx); | |
6795 | else if (result == BTRFS_COMPARE_TREE_CHANGED) | |
6796 | ret = record_changed_ref(sctx); | |
6797 | } | |
6798 | ||
6799 | return ret; | |
6800 | } | |
6801 | ||
766702ef AB |
6802 | /* |
6803 | * Process new/deleted/changed xattrs. We skip processing in the | |
6804 | * cur_inode_new_gen case because changed_inode did already initiate processing | |
6805 | * of xattrs. The reason is the same as in changed_ref | |
6806 | */ | |
31db9f7c AB |
6807 | static int changed_xattr(struct send_ctx *sctx, |
6808 | enum btrfs_compare_tree_result result) | |
6809 | { | |
6810 | int ret = 0; | |
6811 | ||
95155585 FM |
6812 | if (sctx->cur_ino != sctx->cmp_key->objectid) { |
6813 | inconsistent_snapshot_error(sctx, result, "xattr"); | |
6814 | return -EIO; | |
6815 | } | |
31db9f7c AB |
6816 | |
6817 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { | |
6818 | if (result == BTRFS_COMPARE_TREE_NEW) | |
6819 | ret = process_new_xattr(sctx); | |
6820 | else if (result == BTRFS_COMPARE_TREE_DELETED) | |
6821 | ret = process_deleted_xattr(sctx); | |
6822 | else if (result == BTRFS_COMPARE_TREE_CHANGED) | |
6823 | ret = process_changed_xattr(sctx); | |
6824 | } | |
6825 | ||
6826 | return ret; | |
6827 | } | |
6828 | ||
766702ef AB |
6829 | /* |
6830 | * Process new/deleted/changed extents. We skip processing in the | |
6831 | * cur_inode_new_gen case because changed_inode did already initiate processing | |
6832 | * of extents. The reason is the same as in changed_ref | |
6833 | */ | |
31db9f7c AB |
6834 | static int changed_extent(struct send_ctx *sctx, |
6835 | enum btrfs_compare_tree_result result) | |
6836 | { | |
6837 | int ret = 0; | |
6838 | ||
b4f9a1a8 FM |
6839 | /* |
6840 | * We have found an extent item that changed without the inode item | |
6841 | * having changed. This can happen either after relocation (where the | |
6842 | * disk_bytenr of an extent item is replaced at | |
6843 | * relocation.c:replace_file_extents()) or after deduplication into a | |
6844 | * file in both the parent and send snapshots (where an extent item can | |
6845 | * get modified or replaced with a new one). Note that deduplication | |
6846 | * updates the inode item, but it only changes the iversion (sequence | |
6847 | * field in the inode item) of the inode, so if a file is deduplicated | |
6848 | * the same amount of times in both the parent and send snapshots, its | |
1a9fd417 | 6849 | * iversion becomes the same in both snapshots, whence the inode item is |
b4f9a1a8 FM |
6850 | * the same on both snapshots. |
6851 | */ | |
6852 | if (sctx->cur_ino != sctx->cmp_key->objectid) | |
6853 | return 0; | |
31db9f7c AB |
6854 | |
6855 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { | |
6856 | if (result != BTRFS_COMPARE_TREE_DELETED) | |
6857 | ret = process_extent(sctx, sctx->left_path, | |
6858 | sctx->cmp_key); | |
6859 | } | |
6860 | ||
6861 | return ret; | |
6862 | } | |
6863 | ||
38622010 BB |
6864 | static int changed_verity(struct send_ctx *sctx, enum btrfs_compare_tree_result result) |
6865 | { | |
6866 | int ret = 0; | |
6867 | ||
6868 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { | |
6869 | if (result == BTRFS_COMPARE_TREE_NEW) | |
6870 | sctx->cur_inode_needs_verity = true; | |
6871 | } | |
6872 | return ret; | |
6873 | } | |
6874 | ||
ba5e8f2e JB |
6875 | static int dir_changed(struct send_ctx *sctx, u64 dir) |
6876 | { | |
6877 | u64 orig_gen, new_gen; | |
6878 | int ret; | |
6879 | ||
7e93f6dc | 6880 | ret = get_inode_gen(sctx->send_root, dir, &new_gen); |
ba5e8f2e JB |
6881 | if (ret) |
6882 | return ret; | |
6883 | ||
7e93f6dc | 6884 | ret = get_inode_gen(sctx->parent_root, dir, &orig_gen); |
ba5e8f2e JB |
6885 | if (ret) |
6886 | return ret; | |
6887 | ||
6888 | return (orig_gen != new_gen) ? 1 : 0; | |
6889 | } | |
6890 | ||
6891 | static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path, | |
6892 | struct btrfs_key *key) | |
6893 | { | |
6894 | struct btrfs_inode_extref *extref; | |
6895 | struct extent_buffer *leaf; | |
6896 | u64 dirid = 0, last_dirid = 0; | |
6897 | unsigned long ptr; | |
6898 | u32 item_size; | |
6899 | u32 cur_offset = 0; | |
6900 | int ref_name_len; | |
6901 | int ret = 0; | |
6902 | ||
6903 | /* Easy case, just check this one dirid */ | |
6904 | if (key->type == BTRFS_INODE_REF_KEY) { | |
6905 | dirid = key->offset; | |
6906 | ||
6907 | ret = dir_changed(sctx, dirid); | |
6908 | goto out; | |
6909 | } | |
6910 | ||
6911 | leaf = path->nodes[0]; | |
3212fa14 | 6912 | item_size = btrfs_item_size(leaf, path->slots[0]); |
ba5e8f2e JB |
6913 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
6914 | while (cur_offset < item_size) { | |
6915 | extref = (struct btrfs_inode_extref *)(ptr + | |
6916 | cur_offset); | |
6917 | dirid = btrfs_inode_extref_parent(leaf, extref); | |
6918 | ref_name_len = btrfs_inode_extref_name_len(leaf, extref); | |
6919 | cur_offset += ref_name_len + sizeof(*extref); | |
6920 | if (dirid == last_dirid) | |
6921 | continue; | |
6922 | ret = dir_changed(sctx, dirid); | |
6923 | if (ret) | |
6924 | break; | |
6925 | last_dirid = dirid; | |
6926 | } | |
6927 | out: | |
6928 | return ret; | |
6929 | } | |
6930 | ||
766702ef AB |
6931 | /* |
6932 | * Updates compare related fields in sctx and simply forwards to the actual | |
6933 | * changed_xxx functions. | |
6934 | */ | |
ee8c494f | 6935 | static int changed_cb(struct btrfs_path *left_path, |
31db9f7c AB |
6936 | struct btrfs_path *right_path, |
6937 | struct btrfs_key *key, | |
6938 | enum btrfs_compare_tree_result result, | |
88980383 | 6939 | struct send_ctx *sctx) |
31db9f7c AB |
6940 | { |
6941 | int ret = 0; | |
31db9f7c | 6942 | |
d96b3424 FM |
6943 | /* |
6944 | * We can not hold the commit root semaphore here. This is because in | |
6945 | * the case of sending and receiving to the same filesystem, using a | |
6946 | * pipe, could result in a deadlock: | |
6947 | * | |
6948 | * 1) The task running send blocks on the pipe because it's full; | |
6949 | * | |
6950 | * 2) The task running receive, which is the only consumer of the pipe, | |
6951 | * is waiting for a transaction commit (for example due to a space | |
6952 | * reservation when doing a write or triggering a transaction commit | |
6953 | * when creating a subvolume); | |
6954 | * | |
6955 | * 3) The transaction is waiting to write lock the commit root semaphore, | |
6956 | * but can not acquire it since it's being held at 1). | |
6957 | * | |
6958 | * Down this call chain we write to the pipe through kernel_write(). | |
6959 | * The same type of problem can also happen when sending to a file that | |
6960 | * is stored in the same filesystem - when reserving space for a write | |
6961 | * into the file, we can trigger a transaction commit. | |
6962 | * | |
6963 | * Our caller has supplied us with clones of leaves from the send and | |
6964 | * parent roots, so we're safe here from a concurrent relocation and | |
6965 | * further reallocation of metadata extents while we are here. Below we | |
6966 | * also assert that the leaves are clones. | |
6967 | */ | |
6968 | lockdep_assert_not_held(&sctx->send_root->fs_info->commit_root_sem); | |
6969 | ||
6970 | /* | |
6971 | * We always have a send root, so left_path is never NULL. We will not | |
6972 | * have a leaf when we have reached the end of the send root but have | |
6973 | * not yet reached the end of the parent root. | |
6974 | */ | |
6975 | if (left_path->nodes[0]) | |
6976 | ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED, | |
6977 | &left_path->nodes[0]->bflags)); | |
6978 | /* | |
6979 | * When doing a full send we don't have a parent root, so right_path is | |
6980 | * NULL. When doing an incremental send, we may have reached the end of | |
6981 | * the parent root already, so we don't have a leaf at right_path. | |
6982 | */ | |
6983 | if (right_path && right_path->nodes[0]) | |
6984 | ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED, | |
6985 | &right_path->nodes[0]->bflags)); | |
6986 | ||
ba5e8f2e | 6987 | if (result == BTRFS_COMPARE_TREE_SAME) { |
16e7549f JB |
6988 | if (key->type == BTRFS_INODE_REF_KEY || |
6989 | key->type == BTRFS_INODE_EXTREF_KEY) { | |
6990 | ret = compare_refs(sctx, left_path, key); | |
6991 | if (!ret) | |
6992 | return 0; | |
6993 | if (ret < 0) | |
6994 | return ret; | |
6995 | } else if (key->type == BTRFS_EXTENT_DATA_KEY) { | |
6996 | return maybe_send_hole(sctx, left_path, key); | |
6997 | } else { | |
ba5e8f2e | 6998 | return 0; |
16e7549f | 6999 | } |
ba5e8f2e JB |
7000 | result = BTRFS_COMPARE_TREE_CHANGED; |
7001 | ret = 0; | |
7002 | } | |
7003 | ||
31db9f7c AB |
7004 | sctx->left_path = left_path; |
7005 | sctx->right_path = right_path; | |
7006 | sctx->cmp_key = key; | |
7007 | ||
7008 | ret = finish_inode_if_needed(sctx, 0); | |
7009 | if (ret < 0) | |
7010 | goto out; | |
7011 | ||
2981e225 AB |
7012 | /* Ignore non-FS objects */ |
7013 | if (key->objectid == BTRFS_FREE_INO_OBJECTID || | |
7014 | key->objectid == BTRFS_FREE_SPACE_OBJECTID) | |
7015 | goto out; | |
7016 | ||
46b2f459 | 7017 | if (key->type == BTRFS_INODE_ITEM_KEY) { |
31db9f7c | 7018 | ret = changed_inode(sctx, result); |
46b2f459 FM |
7019 | } else if (!sctx->ignore_cur_inode) { |
7020 | if (key->type == BTRFS_INODE_REF_KEY || | |
7021 | key->type == BTRFS_INODE_EXTREF_KEY) | |
7022 | ret = changed_ref(sctx, result); | |
7023 | else if (key->type == BTRFS_XATTR_ITEM_KEY) | |
7024 | ret = changed_xattr(sctx, result); | |
7025 | else if (key->type == BTRFS_EXTENT_DATA_KEY) | |
7026 | ret = changed_extent(sctx, result); | |
38622010 BB |
7027 | else if (key->type == BTRFS_VERITY_DESC_ITEM_KEY && |
7028 | key->offset == 0) | |
7029 | ret = changed_verity(sctx, result); | |
46b2f459 | 7030 | } |
31db9f7c AB |
7031 | |
7032 | out: | |
7033 | return ret; | |
7034 | } | |
7035 | ||
d96b3424 FM |
7036 | static int search_key_again(const struct send_ctx *sctx, |
7037 | struct btrfs_root *root, | |
7038 | struct btrfs_path *path, | |
7039 | const struct btrfs_key *key) | |
7040 | { | |
7041 | int ret; | |
7042 | ||
7043 | if (!path->need_commit_sem) | |
7044 | lockdep_assert_held_read(&root->fs_info->commit_root_sem); | |
7045 | ||
7046 | /* | |
7047 | * Roots used for send operations are readonly and no one can add, | |
7048 | * update or remove keys from them, so we should be able to find our | |
7049 | * key again. The only exception is deduplication, which can operate on | |
7050 | * readonly roots and add, update or remove keys to/from them - but at | |
7051 | * the moment we don't allow it to run in parallel with send. | |
7052 | */ | |
7053 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | |
7054 | ASSERT(ret <= 0); | |
7055 | if (ret > 0) { | |
7056 | btrfs_print_tree(path->nodes[path->lowest_level], false); | |
7057 | btrfs_err(root->fs_info, | |
7058 | "send: key (%llu %u %llu) not found in %s root %llu, lowest_level %d, slot %d", | |
7059 | key->objectid, key->type, key->offset, | |
7060 | (root == sctx->parent_root ? "parent" : "send"), | |
7061 | root->root_key.objectid, path->lowest_level, | |
7062 | path->slots[path->lowest_level]); | |
7063 | return -EUCLEAN; | |
7064 | } | |
7065 | ||
7066 | return ret; | |
7067 | } | |
7068 | ||
31db9f7c AB |
7069 | static int full_send_tree(struct send_ctx *sctx) |
7070 | { | |
7071 | int ret; | |
31db9f7c AB |
7072 | struct btrfs_root *send_root = sctx->send_root; |
7073 | struct btrfs_key key; | |
d96b3424 | 7074 | struct btrfs_fs_info *fs_info = send_root->fs_info; |
31db9f7c | 7075 | struct btrfs_path *path; |
31db9f7c AB |
7076 | |
7077 | path = alloc_path_for_send(); | |
7078 | if (!path) | |
7079 | return -ENOMEM; | |
ace75066 | 7080 | path->reada = READA_FORWARD_ALWAYS; |
31db9f7c | 7081 | |
31db9f7c AB |
7082 | key.objectid = BTRFS_FIRST_FREE_OBJECTID; |
7083 | key.type = BTRFS_INODE_ITEM_KEY; | |
7084 | key.offset = 0; | |
7085 | ||
d96b3424 FM |
7086 | down_read(&fs_info->commit_root_sem); |
7087 | sctx->last_reloc_trans = fs_info->last_reloc_trans; | |
7088 | up_read(&fs_info->commit_root_sem); | |
7089 | ||
31db9f7c AB |
7090 | ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0); |
7091 | if (ret < 0) | |
7092 | goto out; | |
7093 | if (ret) | |
7094 | goto out_finish; | |
7095 | ||
7096 | while (1) { | |
d96b3424 | 7097 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
31db9f7c | 7098 | |
ca5d2ba1 | 7099 | ret = changed_cb(path, NULL, &key, |
ee8c494f | 7100 | BTRFS_COMPARE_TREE_NEW, sctx); |
31db9f7c AB |
7101 | if (ret < 0) |
7102 | goto out; | |
7103 | ||
d96b3424 FM |
7104 | down_read(&fs_info->commit_root_sem); |
7105 | if (fs_info->last_reloc_trans > sctx->last_reloc_trans) { | |
7106 | sctx->last_reloc_trans = fs_info->last_reloc_trans; | |
7107 | up_read(&fs_info->commit_root_sem); | |
7108 | /* | |
7109 | * A transaction used for relocating a block group was | |
7110 | * committed or is about to finish its commit. Release | |
7111 | * our path (leaf) and restart the search, so that we | |
7112 | * avoid operating on any file extent items that are | |
7113 | * stale, with a disk_bytenr that reflects a pre | |
7114 | * relocation value. This way we avoid as much as | |
7115 | * possible to fallback to regular writes when checking | |
7116 | * if we can clone file ranges. | |
7117 | */ | |
7118 | btrfs_release_path(path); | |
7119 | ret = search_key_again(sctx, send_root, path, &key); | |
7120 | if (ret < 0) | |
7121 | goto out; | |
7122 | } else { | |
7123 | up_read(&fs_info->commit_root_sem); | |
7124 | } | |
7125 | ||
31db9f7c AB |
7126 | ret = btrfs_next_item(send_root, path); |
7127 | if (ret < 0) | |
7128 | goto out; | |
7129 | if (ret) { | |
7130 | ret = 0; | |
7131 | break; | |
7132 | } | |
7133 | } | |
7134 | ||
7135 | out_finish: | |
7136 | ret = finish_inode_if_needed(sctx, 1); | |
7137 | ||
7138 | out: | |
7139 | btrfs_free_path(path); | |
31db9f7c AB |
7140 | return ret; |
7141 | } | |
7142 | ||
d96b3424 FM |
7143 | static int replace_node_with_clone(struct btrfs_path *path, int level) |
7144 | { | |
7145 | struct extent_buffer *clone; | |
7146 | ||
7147 | clone = btrfs_clone_extent_buffer(path->nodes[level]); | |
7148 | if (!clone) | |
7149 | return -ENOMEM; | |
7150 | ||
7151 | free_extent_buffer(path->nodes[level]); | |
7152 | path->nodes[level] = clone; | |
7153 | ||
7154 | return 0; | |
7155 | } | |
7156 | ||
2ce73c63 | 7157 | static int tree_move_down(struct btrfs_path *path, int *level, u64 reada_min_gen) |
18d0f5c6 DS |
7158 | { |
7159 | struct extent_buffer *eb; | |
2ce73c63 FM |
7160 | struct extent_buffer *parent = path->nodes[*level]; |
7161 | int slot = path->slots[*level]; | |
7162 | const int nritems = btrfs_header_nritems(parent); | |
7163 | u64 reada_max; | |
7164 | u64 reada_done = 0; | |
18d0f5c6 | 7165 | |
d96b3424 FM |
7166 | lockdep_assert_held_read(&parent->fs_info->commit_root_sem); |
7167 | ||
18d0f5c6 | 7168 | BUG_ON(*level == 0); |
2ce73c63 | 7169 | eb = btrfs_read_node_slot(parent, slot); |
18d0f5c6 DS |
7170 | if (IS_ERR(eb)) |
7171 | return PTR_ERR(eb); | |
7172 | ||
2ce73c63 FM |
7173 | /* |
7174 | * Trigger readahead for the next leaves we will process, so that it is | |
7175 | * very likely that when we need them they are already in memory and we | |
7176 | * will not block on disk IO. For nodes we only do readahead for one, | |
7177 | * since the time window between processing nodes is typically larger. | |
7178 | */ | |
7179 | reada_max = (*level == 1 ? SZ_128K : eb->fs_info->nodesize); | |
7180 | ||
7181 | for (slot++; slot < nritems && reada_done < reada_max; slot++) { | |
7182 | if (btrfs_node_ptr_generation(parent, slot) > reada_min_gen) { | |
7183 | btrfs_readahead_node_child(parent, slot); | |
7184 | reada_done += eb->fs_info->nodesize; | |
7185 | } | |
7186 | } | |
7187 | ||
18d0f5c6 DS |
7188 | path->nodes[*level - 1] = eb; |
7189 | path->slots[*level - 1] = 0; | |
7190 | (*level)--; | |
d96b3424 FM |
7191 | |
7192 | if (*level == 0) | |
7193 | return replace_node_with_clone(path, 0); | |
7194 | ||
18d0f5c6 DS |
7195 | return 0; |
7196 | } | |
7197 | ||
7198 | static int tree_move_next_or_upnext(struct btrfs_path *path, | |
7199 | int *level, int root_level) | |
7200 | { | |
7201 | int ret = 0; | |
7202 | int nritems; | |
7203 | nritems = btrfs_header_nritems(path->nodes[*level]); | |
7204 | ||
7205 | path->slots[*level]++; | |
7206 | ||
7207 | while (path->slots[*level] >= nritems) { | |
d96b3424 FM |
7208 | if (*level == root_level) { |
7209 | path->slots[*level] = nritems - 1; | |
18d0f5c6 | 7210 | return -1; |
d96b3424 | 7211 | } |
18d0f5c6 DS |
7212 | |
7213 | /* move upnext */ | |
7214 | path->slots[*level] = 0; | |
7215 | free_extent_buffer(path->nodes[*level]); | |
7216 | path->nodes[*level] = NULL; | |
7217 | (*level)++; | |
7218 | path->slots[*level]++; | |
7219 | ||
7220 | nritems = btrfs_header_nritems(path->nodes[*level]); | |
7221 | ret = 1; | |
7222 | } | |
7223 | return ret; | |
7224 | } | |
7225 | ||
7226 | /* | |
7227 | * Returns 1 if it had to move up and next. 0 is returned if it moved only next | |
7228 | * or down. | |
7229 | */ | |
7230 | static int tree_advance(struct btrfs_path *path, | |
7231 | int *level, int root_level, | |
7232 | int allow_down, | |
2ce73c63 FM |
7233 | struct btrfs_key *key, |
7234 | u64 reada_min_gen) | |
18d0f5c6 DS |
7235 | { |
7236 | int ret; | |
7237 | ||
7238 | if (*level == 0 || !allow_down) { | |
7239 | ret = tree_move_next_or_upnext(path, level, root_level); | |
7240 | } else { | |
2ce73c63 | 7241 | ret = tree_move_down(path, level, reada_min_gen); |
18d0f5c6 | 7242 | } |
d96b3424 FM |
7243 | |
7244 | /* | |
7245 | * Even if we have reached the end of a tree, ret is -1, update the key | |
7246 | * anyway, so that in case we need to restart due to a block group | |
7247 | * relocation, we can assert that the last key of the root node still | |
7248 | * exists in the tree. | |
7249 | */ | |
7250 | if (*level == 0) | |
7251 | btrfs_item_key_to_cpu(path->nodes[*level], key, | |
7252 | path->slots[*level]); | |
7253 | else | |
7254 | btrfs_node_key_to_cpu(path->nodes[*level], key, | |
7255 | path->slots[*level]); | |
7256 | ||
18d0f5c6 DS |
7257 | return ret; |
7258 | } | |
7259 | ||
7260 | static int tree_compare_item(struct btrfs_path *left_path, | |
7261 | struct btrfs_path *right_path, | |
7262 | char *tmp_buf) | |
7263 | { | |
7264 | int cmp; | |
7265 | int len1, len2; | |
7266 | unsigned long off1, off2; | |
7267 | ||
3212fa14 JB |
7268 | len1 = btrfs_item_size(left_path->nodes[0], left_path->slots[0]); |
7269 | len2 = btrfs_item_size(right_path->nodes[0], right_path->slots[0]); | |
18d0f5c6 DS |
7270 | if (len1 != len2) |
7271 | return 1; | |
7272 | ||
7273 | off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); | |
7274 | off2 = btrfs_item_ptr_offset(right_path->nodes[0], | |
7275 | right_path->slots[0]); | |
7276 | ||
7277 | read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); | |
7278 | ||
7279 | cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); | |
7280 | if (cmp) | |
7281 | return 1; | |
7282 | return 0; | |
7283 | } | |
7284 | ||
d96b3424 FM |
7285 | /* |
7286 | * A transaction used for relocating a block group was committed or is about to | |
7287 | * finish its commit. Release our paths and restart the search, so that we are | |
7288 | * not using stale extent buffers: | |
7289 | * | |
7290 | * 1) For levels > 0, we are only holding references of extent buffers, without | |
7291 | * any locks on them, which does not prevent them from having been relocated | |
7292 | * and reallocated after the last time we released the commit root semaphore. | |
7293 | * The exception are the root nodes, for which we always have a clone, see | |
7294 | * the comment at btrfs_compare_trees(); | |
7295 | * | |
7296 | * 2) For leaves, level 0, we are holding copies (clones) of extent buffers, so | |
7297 | * we are safe from the concurrent relocation and reallocation. However they | |
7298 | * can have file extent items with a pre relocation disk_bytenr value, so we | |
7299 | * restart the start from the current commit roots and clone the new leaves so | |
7300 | * that we get the post relocation disk_bytenr values. Not doing so, could | |
7301 | * make us clone the wrong data in case there are new extents using the old | |
7302 | * disk_bytenr that happen to be shared. | |
7303 | */ | |
7304 | static int restart_after_relocation(struct btrfs_path *left_path, | |
7305 | struct btrfs_path *right_path, | |
7306 | const struct btrfs_key *left_key, | |
7307 | const struct btrfs_key *right_key, | |
7308 | int left_level, | |
7309 | int right_level, | |
7310 | const struct send_ctx *sctx) | |
7311 | { | |
7312 | int root_level; | |
7313 | int ret; | |
7314 | ||
7315 | lockdep_assert_held_read(&sctx->send_root->fs_info->commit_root_sem); | |
7316 | ||
7317 | btrfs_release_path(left_path); | |
7318 | btrfs_release_path(right_path); | |
7319 | ||
7320 | /* | |
7321 | * Since keys can not be added or removed to/from our roots because they | |
7322 | * are readonly and we do not allow deduplication to run in parallel | |
7323 | * (which can add, remove or change keys), the layout of the trees should | |
7324 | * not change. | |
7325 | */ | |
7326 | left_path->lowest_level = left_level; | |
7327 | ret = search_key_again(sctx, sctx->send_root, left_path, left_key); | |
7328 | if (ret < 0) | |
7329 | return ret; | |
7330 | ||
7331 | right_path->lowest_level = right_level; | |
7332 | ret = search_key_again(sctx, sctx->parent_root, right_path, right_key); | |
7333 | if (ret < 0) | |
7334 | return ret; | |
7335 | ||
7336 | /* | |
7337 | * If the lowest level nodes are leaves, clone them so that they can be | |
7338 | * safely used by changed_cb() while not under the protection of the | |
7339 | * commit root semaphore, even if relocation and reallocation happens in | |
7340 | * parallel. | |
7341 | */ | |
7342 | if (left_level == 0) { | |
7343 | ret = replace_node_with_clone(left_path, 0); | |
7344 | if (ret < 0) | |
7345 | return ret; | |
7346 | } | |
7347 | ||
7348 | if (right_level == 0) { | |
7349 | ret = replace_node_with_clone(right_path, 0); | |
7350 | if (ret < 0) | |
7351 | return ret; | |
7352 | } | |
7353 | ||
7354 | /* | |
7355 | * Now clone the root nodes (unless they happen to be the leaves we have | |
7356 | * already cloned). This is to protect against concurrent snapshotting of | |
7357 | * the send and parent roots (see the comment at btrfs_compare_trees()). | |
7358 | */ | |
7359 | root_level = btrfs_header_level(sctx->send_root->commit_root); | |
7360 | if (root_level > 0) { | |
7361 | ret = replace_node_with_clone(left_path, root_level); | |
7362 | if (ret < 0) | |
7363 | return ret; | |
7364 | } | |
7365 | ||
7366 | root_level = btrfs_header_level(sctx->parent_root->commit_root); | |
7367 | if (root_level > 0) { | |
7368 | ret = replace_node_with_clone(right_path, root_level); | |
7369 | if (ret < 0) | |
7370 | return ret; | |
7371 | } | |
7372 | ||
7373 | return 0; | |
7374 | } | |
7375 | ||
18d0f5c6 DS |
7376 | /* |
7377 | * This function compares two trees and calls the provided callback for | |
7378 | * every changed/new/deleted item it finds. | |
7379 | * If shared tree blocks are encountered, whole subtrees are skipped, making | |
7380 | * the compare pretty fast on snapshotted subvolumes. | |
7381 | * | |
7382 | * This currently works on commit roots only. As commit roots are read only, | |
7383 | * we don't do any locking. The commit roots are protected with transactions. | |
7384 | * Transactions are ended and rejoined when a commit is tried in between. | |
7385 | * | |
7386 | * This function checks for modifications done to the trees while comparing. | |
7387 | * If it detects a change, it aborts immediately. | |
7388 | */ | |
7389 | static int btrfs_compare_trees(struct btrfs_root *left_root, | |
88980383 | 7390 | struct btrfs_root *right_root, struct send_ctx *sctx) |
18d0f5c6 DS |
7391 | { |
7392 | struct btrfs_fs_info *fs_info = left_root->fs_info; | |
7393 | int ret; | |
7394 | int cmp; | |
7395 | struct btrfs_path *left_path = NULL; | |
7396 | struct btrfs_path *right_path = NULL; | |
7397 | struct btrfs_key left_key; | |
7398 | struct btrfs_key right_key; | |
7399 | char *tmp_buf = NULL; | |
7400 | int left_root_level; | |
7401 | int right_root_level; | |
7402 | int left_level; | |
7403 | int right_level; | |
d96b3424 FM |
7404 | int left_end_reached = 0; |
7405 | int right_end_reached = 0; | |
7406 | int advance_left = 0; | |
7407 | int advance_right = 0; | |
18d0f5c6 DS |
7408 | u64 left_blockptr; |
7409 | u64 right_blockptr; | |
7410 | u64 left_gen; | |
7411 | u64 right_gen; | |
2ce73c63 | 7412 | u64 reada_min_gen; |
18d0f5c6 DS |
7413 | |
7414 | left_path = btrfs_alloc_path(); | |
7415 | if (!left_path) { | |
7416 | ret = -ENOMEM; | |
7417 | goto out; | |
7418 | } | |
7419 | right_path = btrfs_alloc_path(); | |
7420 | if (!right_path) { | |
7421 | ret = -ENOMEM; | |
7422 | goto out; | |
7423 | } | |
7424 | ||
7425 | tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); | |
7426 | if (!tmp_buf) { | |
7427 | ret = -ENOMEM; | |
7428 | goto out; | |
7429 | } | |
7430 | ||
7431 | left_path->search_commit_root = 1; | |
7432 | left_path->skip_locking = 1; | |
7433 | right_path->search_commit_root = 1; | |
7434 | right_path->skip_locking = 1; | |
7435 | ||
7436 | /* | |
7437 | * Strategy: Go to the first items of both trees. Then do | |
7438 | * | |
7439 | * If both trees are at level 0 | |
7440 | * Compare keys of current items | |
7441 | * If left < right treat left item as new, advance left tree | |
7442 | * and repeat | |
7443 | * If left > right treat right item as deleted, advance right tree | |
7444 | * and repeat | |
7445 | * If left == right do deep compare of items, treat as changed if | |
7446 | * needed, advance both trees and repeat | |
7447 | * If both trees are at the same level but not at level 0 | |
7448 | * Compare keys of current nodes/leafs | |
7449 | * If left < right advance left tree and repeat | |
7450 | * If left > right advance right tree and repeat | |
7451 | * If left == right compare blockptrs of the next nodes/leafs | |
7452 | * If they match advance both trees but stay at the same level | |
7453 | * and repeat | |
7454 | * If they don't match advance both trees while allowing to go | |
7455 | * deeper and repeat | |
7456 | * If tree levels are different | |
7457 | * Advance the tree that needs it and repeat | |
7458 | * | |
7459 | * Advancing a tree means: | |
7460 | * If we are at level 0, try to go to the next slot. If that's not | |
7461 | * possible, go one level up and repeat. Stop when we found a level | |
7462 | * where we could go to the next slot. We may at this point be on a | |
7463 | * node or a leaf. | |
7464 | * | |
7465 | * If we are not at level 0 and not on shared tree blocks, go one | |
7466 | * level deeper. | |
7467 | * | |
7468 | * If we are not at level 0 and on shared tree blocks, go one slot to | |
7469 | * the right if possible or go up and right. | |
7470 | */ | |
7471 | ||
7472 | down_read(&fs_info->commit_root_sem); | |
7473 | left_level = btrfs_header_level(left_root->commit_root); | |
7474 | left_root_level = left_level; | |
d96b3424 FM |
7475 | /* |
7476 | * We clone the root node of the send and parent roots to prevent races | |
7477 | * with snapshot creation of these roots. Snapshot creation COWs the | |
7478 | * root node of a tree, so after the transaction is committed the old | |
7479 | * extent can be reallocated while this send operation is still ongoing. | |
7480 | * So we clone them, under the commit root semaphore, to be race free. | |
7481 | */ | |
18d0f5c6 DS |
7482 | left_path->nodes[left_level] = |
7483 | btrfs_clone_extent_buffer(left_root->commit_root); | |
7484 | if (!left_path->nodes[left_level]) { | |
18d0f5c6 | 7485 | ret = -ENOMEM; |
d96b3424 | 7486 | goto out_unlock; |
18d0f5c6 DS |
7487 | } |
7488 | ||
7489 | right_level = btrfs_header_level(right_root->commit_root); | |
7490 | right_root_level = right_level; | |
7491 | right_path->nodes[right_level] = | |
7492 | btrfs_clone_extent_buffer(right_root->commit_root); | |
7493 | if (!right_path->nodes[right_level]) { | |
18d0f5c6 | 7494 | ret = -ENOMEM; |
d96b3424 | 7495 | goto out_unlock; |
18d0f5c6 | 7496 | } |
2ce73c63 FM |
7497 | /* |
7498 | * Our right root is the parent root, while the left root is the "send" | |
7499 | * root. We know that all new nodes/leaves in the left root must have | |
7500 | * a generation greater than the right root's generation, so we trigger | |
7501 | * readahead for those nodes and leaves of the left root, as we know we | |
7502 | * will need to read them at some point. | |
7503 | */ | |
7504 | reada_min_gen = btrfs_header_generation(right_root->commit_root); | |
18d0f5c6 DS |
7505 | |
7506 | if (left_level == 0) | |
7507 | btrfs_item_key_to_cpu(left_path->nodes[left_level], | |
7508 | &left_key, left_path->slots[left_level]); | |
7509 | else | |
7510 | btrfs_node_key_to_cpu(left_path->nodes[left_level], | |
7511 | &left_key, left_path->slots[left_level]); | |
7512 | if (right_level == 0) | |
7513 | btrfs_item_key_to_cpu(right_path->nodes[right_level], | |
7514 | &right_key, right_path->slots[right_level]); | |
7515 | else | |
7516 | btrfs_node_key_to_cpu(right_path->nodes[right_level], | |
7517 | &right_key, right_path->slots[right_level]); | |
7518 | ||
d96b3424 | 7519 | sctx->last_reloc_trans = fs_info->last_reloc_trans; |
18d0f5c6 DS |
7520 | |
7521 | while (1) { | |
d96b3424 FM |
7522 | if (need_resched() || |
7523 | rwsem_is_contended(&fs_info->commit_root_sem)) { | |
7524 | up_read(&fs_info->commit_root_sem); | |
7525 | cond_resched(); | |
7526 | down_read(&fs_info->commit_root_sem); | |
7527 | } | |
7528 | ||
7529 | if (fs_info->last_reloc_trans > sctx->last_reloc_trans) { | |
7530 | ret = restart_after_relocation(left_path, right_path, | |
7531 | &left_key, &right_key, | |
7532 | left_level, right_level, | |
7533 | sctx); | |
7534 | if (ret < 0) | |
7535 | goto out_unlock; | |
7536 | sctx->last_reloc_trans = fs_info->last_reloc_trans; | |
7537 | } | |
7538 | ||
18d0f5c6 DS |
7539 | if (advance_left && !left_end_reached) { |
7540 | ret = tree_advance(left_path, &left_level, | |
7541 | left_root_level, | |
7542 | advance_left != ADVANCE_ONLY_NEXT, | |
2ce73c63 | 7543 | &left_key, reada_min_gen); |
18d0f5c6 DS |
7544 | if (ret == -1) |
7545 | left_end_reached = ADVANCE; | |
7546 | else if (ret < 0) | |
d96b3424 | 7547 | goto out_unlock; |
18d0f5c6 DS |
7548 | advance_left = 0; |
7549 | } | |
7550 | if (advance_right && !right_end_reached) { | |
7551 | ret = tree_advance(right_path, &right_level, | |
7552 | right_root_level, | |
7553 | advance_right != ADVANCE_ONLY_NEXT, | |
2ce73c63 | 7554 | &right_key, reada_min_gen); |
18d0f5c6 DS |
7555 | if (ret == -1) |
7556 | right_end_reached = ADVANCE; | |
7557 | else if (ret < 0) | |
d96b3424 | 7558 | goto out_unlock; |
18d0f5c6 DS |
7559 | advance_right = 0; |
7560 | } | |
7561 | ||
7562 | if (left_end_reached && right_end_reached) { | |
7563 | ret = 0; | |
d96b3424 | 7564 | goto out_unlock; |
18d0f5c6 DS |
7565 | } else if (left_end_reached) { |
7566 | if (right_level == 0) { | |
d96b3424 | 7567 | up_read(&fs_info->commit_root_sem); |
18d0f5c6 DS |
7568 | ret = changed_cb(left_path, right_path, |
7569 | &right_key, | |
7570 | BTRFS_COMPARE_TREE_DELETED, | |
88980383 | 7571 | sctx); |
18d0f5c6 DS |
7572 | if (ret < 0) |
7573 | goto out; | |
d96b3424 | 7574 | down_read(&fs_info->commit_root_sem); |
18d0f5c6 DS |
7575 | } |
7576 | advance_right = ADVANCE; | |
7577 | continue; | |
7578 | } else if (right_end_reached) { | |
7579 | if (left_level == 0) { | |
d96b3424 | 7580 | up_read(&fs_info->commit_root_sem); |
18d0f5c6 DS |
7581 | ret = changed_cb(left_path, right_path, |
7582 | &left_key, | |
7583 | BTRFS_COMPARE_TREE_NEW, | |
88980383 | 7584 | sctx); |
18d0f5c6 DS |
7585 | if (ret < 0) |
7586 | goto out; | |
d96b3424 | 7587 | down_read(&fs_info->commit_root_sem); |
18d0f5c6 DS |
7588 | } |
7589 | advance_left = ADVANCE; | |
7590 | continue; | |
7591 | } | |
7592 | ||
7593 | if (left_level == 0 && right_level == 0) { | |
d96b3424 | 7594 | up_read(&fs_info->commit_root_sem); |
18d0f5c6 DS |
7595 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
7596 | if (cmp < 0) { | |
7597 | ret = changed_cb(left_path, right_path, | |
7598 | &left_key, | |
7599 | BTRFS_COMPARE_TREE_NEW, | |
88980383 | 7600 | sctx); |
18d0f5c6 DS |
7601 | advance_left = ADVANCE; |
7602 | } else if (cmp > 0) { | |
7603 | ret = changed_cb(left_path, right_path, | |
7604 | &right_key, | |
7605 | BTRFS_COMPARE_TREE_DELETED, | |
88980383 | 7606 | sctx); |
18d0f5c6 DS |
7607 | advance_right = ADVANCE; |
7608 | } else { | |
7609 | enum btrfs_compare_tree_result result; | |
7610 | ||
7611 | WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); | |
7612 | ret = tree_compare_item(left_path, right_path, | |
7613 | tmp_buf); | |
7614 | if (ret) | |
7615 | result = BTRFS_COMPARE_TREE_CHANGED; | |
7616 | else | |
7617 | result = BTRFS_COMPARE_TREE_SAME; | |
7618 | ret = changed_cb(left_path, right_path, | |
88980383 | 7619 | &left_key, result, sctx); |
18d0f5c6 DS |
7620 | advance_left = ADVANCE; |
7621 | advance_right = ADVANCE; | |
7622 | } | |
d96b3424 FM |
7623 | |
7624 | if (ret < 0) | |
7625 | goto out; | |
7626 | down_read(&fs_info->commit_root_sem); | |
18d0f5c6 DS |
7627 | } else if (left_level == right_level) { |
7628 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); | |
7629 | if (cmp < 0) { | |
7630 | advance_left = ADVANCE; | |
7631 | } else if (cmp > 0) { | |
7632 | advance_right = ADVANCE; | |
7633 | } else { | |
7634 | left_blockptr = btrfs_node_blockptr( | |
7635 | left_path->nodes[left_level], | |
7636 | left_path->slots[left_level]); | |
7637 | right_blockptr = btrfs_node_blockptr( | |
7638 | right_path->nodes[right_level], | |
7639 | right_path->slots[right_level]); | |
7640 | left_gen = btrfs_node_ptr_generation( | |
7641 | left_path->nodes[left_level], | |
7642 | left_path->slots[left_level]); | |
7643 | right_gen = btrfs_node_ptr_generation( | |
7644 | right_path->nodes[right_level], | |
7645 | right_path->slots[right_level]); | |
7646 | if (left_blockptr == right_blockptr && | |
7647 | left_gen == right_gen) { | |
7648 | /* | |
7649 | * As we're on a shared block, don't | |
7650 | * allow to go deeper. | |
7651 | */ | |
7652 | advance_left = ADVANCE_ONLY_NEXT; | |
7653 | advance_right = ADVANCE_ONLY_NEXT; | |
7654 | } else { | |
7655 | advance_left = ADVANCE; | |
7656 | advance_right = ADVANCE; | |
7657 | } | |
7658 | } | |
7659 | } else if (left_level < right_level) { | |
7660 | advance_right = ADVANCE; | |
7661 | } else { | |
7662 | advance_left = ADVANCE; | |
7663 | } | |
7664 | } | |
7665 | ||
d96b3424 FM |
7666 | out_unlock: |
7667 | up_read(&fs_info->commit_root_sem); | |
18d0f5c6 DS |
7668 | out: |
7669 | btrfs_free_path(left_path); | |
7670 | btrfs_free_path(right_path); | |
7671 | kvfree(tmp_buf); | |
7672 | return ret; | |
7673 | } | |
7674 | ||
31db9f7c AB |
7675 | static int send_subvol(struct send_ctx *sctx) |
7676 | { | |
7677 | int ret; | |
7678 | ||
c2c71324 SB |
7679 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) { |
7680 | ret = send_header(sctx); | |
7681 | if (ret < 0) | |
7682 | goto out; | |
7683 | } | |
31db9f7c AB |
7684 | |
7685 | ret = send_subvol_begin(sctx); | |
7686 | if (ret < 0) | |
7687 | goto out; | |
7688 | ||
7689 | if (sctx->parent_root) { | |
1b51d6fc | 7690 | ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx); |
31db9f7c AB |
7691 | if (ret < 0) |
7692 | goto out; | |
7693 | ret = finish_inode_if_needed(sctx, 1); | |
7694 | if (ret < 0) | |
7695 | goto out; | |
7696 | } else { | |
7697 | ret = full_send_tree(sctx); | |
7698 | if (ret < 0) | |
7699 | goto out; | |
7700 | } | |
7701 | ||
7702 | out: | |
31db9f7c AB |
7703 | free_recorded_refs(sctx); |
7704 | return ret; | |
7705 | } | |
7706 | ||
e5fa8f86 FM |
7707 | /* |
7708 | * If orphan cleanup did remove any orphans from a root, it means the tree | |
7709 | * was modified and therefore the commit root is not the same as the current | |
7710 | * root anymore. This is a problem, because send uses the commit root and | |
7711 | * therefore can see inode items that don't exist in the current root anymore, | |
7712 | * and for example make calls to btrfs_iget, which will do tree lookups based | |
7713 | * on the current root and not on the commit root. Those lookups will fail, | |
7714 | * returning a -ESTALE error, and making send fail with that error. So make | |
7715 | * sure a send does not see any orphans we have just removed, and that it will | |
7716 | * see the same inodes regardless of whether a transaction commit happened | |
7717 | * before it started (meaning that the commit root will be the same as the | |
7718 | * current root) or not. | |
7719 | */ | |
7720 | static int ensure_commit_roots_uptodate(struct send_ctx *sctx) | |
7721 | { | |
7722 | int i; | |
7723 | struct btrfs_trans_handle *trans = NULL; | |
7724 | ||
7725 | again: | |
7726 | if (sctx->parent_root && | |
7727 | sctx->parent_root->node != sctx->parent_root->commit_root) | |
7728 | goto commit_trans; | |
7729 | ||
7730 | for (i = 0; i < sctx->clone_roots_cnt; i++) | |
7731 | if (sctx->clone_roots[i].root->node != | |
7732 | sctx->clone_roots[i].root->commit_root) | |
7733 | goto commit_trans; | |
7734 | ||
7735 | if (trans) | |
3a45bb20 | 7736 | return btrfs_end_transaction(trans); |
e5fa8f86 FM |
7737 | |
7738 | return 0; | |
7739 | ||
7740 | commit_trans: | |
7741 | /* Use any root, all fs roots will get their commit roots updated. */ | |
7742 | if (!trans) { | |
7743 | trans = btrfs_join_transaction(sctx->send_root); | |
7744 | if (IS_ERR(trans)) | |
7745 | return PTR_ERR(trans); | |
7746 | goto again; | |
7747 | } | |
7748 | ||
3a45bb20 | 7749 | return btrfs_commit_transaction(trans); |
e5fa8f86 FM |
7750 | } |
7751 | ||
9f89d5de FM |
7752 | /* |
7753 | * Make sure any existing dellaloc is flushed for any root used by a send | |
7754 | * operation so that we do not miss any data and we do not race with writeback | |
7755 | * finishing and changing a tree while send is using the tree. This could | |
7756 | * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and | |
7757 | * a send operation then uses the subvolume. | |
7758 | * After flushing delalloc ensure_commit_roots_uptodate() must be called. | |
7759 | */ | |
7760 | static int flush_delalloc_roots(struct send_ctx *sctx) | |
7761 | { | |
7762 | struct btrfs_root *root = sctx->parent_root; | |
7763 | int ret; | |
7764 | int i; | |
7765 | ||
7766 | if (root) { | |
f9baa501 | 7767 | ret = btrfs_start_delalloc_snapshot(root, false); |
9f89d5de FM |
7768 | if (ret) |
7769 | return ret; | |
7770 | btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX); | |
7771 | } | |
7772 | ||
7773 | for (i = 0; i < sctx->clone_roots_cnt; i++) { | |
7774 | root = sctx->clone_roots[i].root; | |
f9baa501 | 7775 | ret = btrfs_start_delalloc_snapshot(root, false); |
9f89d5de FM |
7776 | if (ret) |
7777 | return ret; | |
7778 | btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX); | |
7779 | } | |
7780 | ||
7781 | return 0; | |
7782 | } | |
7783 | ||
66ef7d65 DS |
7784 | static void btrfs_root_dec_send_in_progress(struct btrfs_root* root) |
7785 | { | |
7786 | spin_lock(&root->root_item_lock); | |
7787 | root->send_in_progress--; | |
7788 | /* | |
7789 | * Not much left to do, we don't know why it's unbalanced and | |
7790 | * can't blindly reset it to 0. | |
7791 | */ | |
7792 | if (root->send_in_progress < 0) | |
7793 | btrfs_err(root->fs_info, | |
f5686e3a | 7794 | "send_in_progress unbalanced %d root %llu", |
0b246afa | 7795 | root->send_in_progress, root->root_key.objectid); |
66ef7d65 DS |
7796 | spin_unlock(&root->root_item_lock); |
7797 | } | |
7798 | ||
62d54f3a FM |
7799 | static void dedupe_in_progress_warn(const struct btrfs_root *root) |
7800 | { | |
7801 | btrfs_warn_rl(root->fs_info, | |
7802 | "cannot use root %llu for send while deduplications on it are in progress (%d in progress)", | |
7803 | root->root_key.objectid, root->dedupe_in_progress); | |
7804 | } | |
7805 | ||
9ad12305 | 7806 | long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg) |
31db9f7c AB |
7807 | { |
7808 | int ret = 0; | |
9ad12305 | 7809 | struct btrfs_root *send_root = BTRFS_I(inode)->root; |
0b246afa | 7810 | struct btrfs_fs_info *fs_info = send_root->fs_info; |
31db9f7c | 7811 | struct btrfs_root *clone_root; |
31db9f7c AB |
7812 | struct send_ctx *sctx = NULL; |
7813 | u32 i; | |
7814 | u64 *clone_sources_tmp = NULL; | |
2c686537 | 7815 | int clone_sources_to_rollback = 0; |
bae12df9 | 7816 | size_t alloc_size; |
896c14f9 | 7817 | int sort_clone_roots = 0; |
31db9f7c AB |
7818 | |
7819 | if (!capable(CAP_SYS_ADMIN)) | |
7820 | return -EPERM; | |
7821 | ||
2c686537 DS |
7822 | /* |
7823 | * The subvolume must remain read-only during send, protect against | |
521e0546 | 7824 | * making it RW. This also protects against deletion. |
2c686537 DS |
7825 | */ |
7826 | spin_lock(&send_root->root_item_lock); | |
62d54f3a FM |
7827 | if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) { |
7828 | dedupe_in_progress_warn(send_root); | |
7829 | spin_unlock(&send_root->root_item_lock); | |
7830 | return -EAGAIN; | |
7831 | } | |
2c686537 DS |
7832 | send_root->send_in_progress++; |
7833 | spin_unlock(&send_root->root_item_lock); | |
7834 | ||
2c686537 DS |
7835 | /* |
7836 | * Userspace tools do the checks and warn the user if it's | |
7837 | * not RO. | |
7838 | */ | |
7839 | if (!btrfs_root_readonly(send_root)) { | |
7840 | ret = -EPERM; | |
7841 | goto out; | |
7842 | } | |
7843 | ||
457ae726 DC |
7844 | /* |
7845 | * Check that we don't overflow at later allocations, we request | |
7846 | * clone_sources_count + 1 items, and compare to unsigned long inside | |
7847 | * access_ok. | |
7848 | */ | |
f5ecec3c | 7849 | if (arg->clone_sources_count > |
457ae726 | 7850 | ULONG_MAX / sizeof(struct clone_root) - 1) { |
f5ecec3c DC |
7851 | ret = -EINVAL; |
7852 | goto out; | |
7853 | } | |
7854 | ||
c2c71324 | 7855 | if (arg->flags & ~BTRFS_SEND_FLAG_MASK) { |
cb95e7bf MF |
7856 | ret = -EINVAL; |
7857 | goto out; | |
7858 | } | |
7859 | ||
e780b0d1 | 7860 | sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL); |
31db9f7c AB |
7861 | if (!sctx) { |
7862 | ret = -ENOMEM; | |
7863 | goto out; | |
7864 | } | |
7865 | ||
7866 | INIT_LIST_HEAD(&sctx->new_refs); | |
7867 | INIT_LIST_HEAD(&sctx->deleted_refs); | |
5b8418b8 | 7868 | INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL); |
31db9f7c AB |
7869 | INIT_LIST_HEAD(&sctx->name_cache_list); |
7870 | ||
cb95e7bf MF |
7871 | sctx->flags = arg->flags; |
7872 | ||
e77fbf99 DS |
7873 | if (arg->flags & BTRFS_SEND_FLAG_VERSION) { |
7874 | if (arg->version > BTRFS_SEND_STREAM_VERSION) { | |
7875 | ret = -EPROTO; | |
7876 | goto out; | |
7877 | } | |
7878 | /* Zero means "use the highest version" */ | |
7879 | sctx->proto = arg->version ?: BTRFS_SEND_STREAM_VERSION; | |
7880 | } else { | |
7881 | sctx->proto = 1; | |
7882 | } | |
d6815592 OS |
7883 | if ((arg->flags & BTRFS_SEND_FLAG_COMPRESSED) && sctx->proto < 2) { |
7884 | ret = -EINVAL; | |
7885 | goto out; | |
7886 | } | |
e77fbf99 | 7887 | |
31db9f7c | 7888 | sctx->send_filp = fget(arg->send_fd); |
ecc7ada7 TI |
7889 | if (!sctx->send_filp) { |
7890 | ret = -EBADF; | |
31db9f7c AB |
7891 | goto out; |
7892 | } | |
7893 | ||
31db9f7c | 7894 | sctx->send_root = send_root; |
521e0546 DS |
7895 | /* |
7896 | * Unlikely but possible, if the subvolume is marked for deletion but | |
7897 | * is slow to remove the directory entry, send can still be started | |
7898 | */ | |
7899 | if (btrfs_root_dead(sctx->send_root)) { | |
7900 | ret = -EPERM; | |
7901 | goto out; | |
7902 | } | |
7903 | ||
31db9f7c AB |
7904 | sctx->clone_roots_cnt = arg->clone_sources_count; |
7905 | ||
a4b333f2 OS |
7906 | if (sctx->proto >= 2) { |
7907 | u32 send_buf_num_pages; | |
7908 | ||
875c627c | 7909 | sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V2; |
a4b333f2 OS |
7910 | sctx->send_buf = vmalloc(sctx->send_max_size); |
7911 | if (!sctx->send_buf) { | |
7912 | ret = -ENOMEM; | |
7913 | goto out; | |
7914 | } | |
7915 | send_buf_num_pages = sctx->send_max_size >> PAGE_SHIFT; | |
7916 | sctx->send_buf_pages = kcalloc(send_buf_num_pages, | |
7917 | sizeof(*sctx->send_buf_pages), | |
7918 | GFP_KERNEL); | |
7919 | if (!sctx->send_buf_pages) { | |
7920 | ret = -ENOMEM; | |
7921 | goto out; | |
7922 | } | |
7923 | for (i = 0; i < send_buf_num_pages; i++) { | |
7924 | sctx->send_buf_pages[i] = | |
7925 | vmalloc_to_page(sctx->send_buf + (i << PAGE_SHIFT)); | |
7926 | } | |
7927 | } else { | |
356bbbb6 | 7928 | sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V1; |
a4b333f2 OS |
7929 | sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL); |
7930 | } | |
31db9f7c | 7931 | if (!sctx->send_buf) { |
752ade68 MH |
7932 | ret = -ENOMEM; |
7933 | goto out; | |
31db9f7c AB |
7934 | } |
7935 | ||
9f03740a FDBM |
7936 | sctx->pending_dir_moves = RB_ROOT; |
7937 | sctx->waiting_dir_moves = RB_ROOT; | |
9dc44214 | 7938 | sctx->orphan_dirs = RB_ROOT; |
3aa5bd36 BC |
7939 | sctx->rbtree_new_refs = RB_ROOT; |
7940 | sctx->rbtree_deleted_refs = RB_ROOT; | |
9f03740a | 7941 | |
bae12df9 DE |
7942 | sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots), |
7943 | arg->clone_sources_count + 1, | |
7944 | GFP_KERNEL); | |
31db9f7c | 7945 | if (!sctx->clone_roots) { |
818e010b DS |
7946 | ret = -ENOMEM; |
7947 | goto out; | |
31db9f7c AB |
7948 | } |
7949 | ||
bae12df9 DE |
7950 | alloc_size = array_size(sizeof(*arg->clone_sources), |
7951 | arg->clone_sources_count); | |
e55d1153 | 7952 | |
31db9f7c | 7953 | if (arg->clone_sources_count) { |
752ade68 | 7954 | clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL); |
31db9f7c | 7955 | if (!clone_sources_tmp) { |
752ade68 MH |
7956 | ret = -ENOMEM; |
7957 | goto out; | |
31db9f7c AB |
7958 | } |
7959 | ||
7960 | ret = copy_from_user(clone_sources_tmp, arg->clone_sources, | |
e55d1153 | 7961 | alloc_size); |
31db9f7c AB |
7962 | if (ret) { |
7963 | ret = -EFAULT; | |
7964 | goto out; | |
7965 | } | |
7966 | ||
7967 | for (i = 0; i < arg->clone_sources_count; i++) { | |
56e9357a DS |
7968 | clone_root = btrfs_get_fs_root(fs_info, |
7969 | clone_sources_tmp[i], true); | |
31db9f7c AB |
7970 | if (IS_ERR(clone_root)) { |
7971 | ret = PTR_ERR(clone_root); | |
7972 | goto out; | |
7973 | } | |
2c686537 | 7974 | spin_lock(&clone_root->root_item_lock); |
5cc2b17e FM |
7975 | if (!btrfs_root_readonly(clone_root) || |
7976 | btrfs_root_dead(clone_root)) { | |
2c686537 | 7977 | spin_unlock(&clone_root->root_item_lock); |
00246528 | 7978 | btrfs_put_root(clone_root); |
2c686537 DS |
7979 | ret = -EPERM; |
7980 | goto out; | |
7981 | } | |
62d54f3a FM |
7982 | if (clone_root->dedupe_in_progress) { |
7983 | dedupe_in_progress_warn(clone_root); | |
7984 | spin_unlock(&clone_root->root_item_lock); | |
00246528 | 7985 | btrfs_put_root(clone_root); |
62d54f3a FM |
7986 | ret = -EAGAIN; |
7987 | goto out; | |
7988 | } | |
2f1f465a | 7989 | clone_root->send_in_progress++; |
2c686537 | 7990 | spin_unlock(&clone_root->root_item_lock); |
18f687d5 | 7991 | |
31db9f7c | 7992 | sctx->clone_roots[i].root = clone_root; |
2f1f465a | 7993 | clone_sources_to_rollback = i + 1; |
31db9f7c | 7994 | } |
2f91306a | 7995 | kvfree(clone_sources_tmp); |
31db9f7c AB |
7996 | clone_sources_tmp = NULL; |
7997 | } | |
7998 | ||
7999 | if (arg->parent_root) { | |
56e9357a DS |
8000 | sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root, |
8001 | true); | |
b1b19596 SB |
8002 | if (IS_ERR(sctx->parent_root)) { |
8003 | ret = PTR_ERR(sctx->parent_root); | |
31db9f7c AB |
8004 | goto out; |
8005 | } | |
18f687d5 | 8006 | |
2c686537 DS |
8007 | spin_lock(&sctx->parent_root->root_item_lock); |
8008 | sctx->parent_root->send_in_progress++; | |
521e0546 DS |
8009 | if (!btrfs_root_readonly(sctx->parent_root) || |
8010 | btrfs_root_dead(sctx->parent_root)) { | |
2c686537 DS |
8011 | spin_unlock(&sctx->parent_root->root_item_lock); |
8012 | ret = -EPERM; | |
8013 | goto out; | |
8014 | } | |
62d54f3a FM |
8015 | if (sctx->parent_root->dedupe_in_progress) { |
8016 | dedupe_in_progress_warn(sctx->parent_root); | |
8017 | spin_unlock(&sctx->parent_root->root_item_lock); | |
62d54f3a FM |
8018 | ret = -EAGAIN; |
8019 | goto out; | |
8020 | } | |
2c686537 | 8021 | spin_unlock(&sctx->parent_root->root_item_lock); |
31db9f7c AB |
8022 | } |
8023 | ||
8024 | /* | |
8025 | * Clones from send_root are allowed, but only if the clone source | |
8026 | * is behind the current send position. This is checked while searching | |
8027 | * for possible clone sources. | |
8028 | */ | |
6f9a3da5 | 8029 | sctx->clone_roots[sctx->clone_roots_cnt++].root = |
00246528 | 8030 | btrfs_grab_root(sctx->send_root); |
31db9f7c AB |
8031 | |
8032 | /* We do a bsearch later */ | |
8033 | sort(sctx->clone_roots, sctx->clone_roots_cnt, | |
8034 | sizeof(*sctx->clone_roots), __clone_root_cmp_sort, | |
8035 | NULL); | |
896c14f9 | 8036 | sort_clone_roots = 1; |
31db9f7c | 8037 | |
9f89d5de FM |
8038 | ret = flush_delalloc_roots(sctx); |
8039 | if (ret) | |
8040 | goto out; | |
8041 | ||
e5fa8f86 FM |
8042 | ret = ensure_commit_roots_uptodate(sctx); |
8043 | if (ret) | |
8044 | goto out; | |
8045 | ||
31db9f7c AB |
8046 | ret = send_subvol(sctx); |
8047 | if (ret < 0) | |
8048 | goto out; | |
8049 | ||
c2c71324 SB |
8050 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) { |
8051 | ret = begin_cmd(sctx, BTRFS_SEND_C_END); | |
8052 | if (ret < 0) | |
8053 | goto out; | |
8054 | ret = send_cmd(sctx); | |
8055 | if (ret < 0) | |
8056 | goto out; | |
8057 | } | |
31db9f7c AB |
8058 | |
8059 | out: | |
9f03740a FDBM |
8060 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)); |
8061 | while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) { | |
8062 | struct rb_node *n; | |
8063 | struct pending_dir_move *pm; | |
8064 | ||
8065 | n = rb_first(&sctx->pending_dir_moves); | |
8066 | pm = rb_entry(n, struct pending_dir_move, node); | |
8067 | while (!list_empty(&pm->list)) { | |
8068 | struct pending_dir_move *pm2; | |
8069 | ||
8070 | pm2 = list_first_entry(&pm->list, | |
8071 | struct pending_dir_move, list); | |
8072 | free_pending_move(sctx, pm2); | |
8073 | } | |
8074 | free_pending_move(sctx, pm); | |
8075 | } | |
8076 | ||
8077 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)); | |
8078 | while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) { | |
8079 | struct rb_node *n; | |
8080 | struct waiting_dir_move *dm; | |
8081 | ||
8082 | n = rb_first(&sctx->waiting_dir_moves); | |
8083 | dm = rb_entry(n, struct waiting_dir_move, node); | |
8084 | rb_erase(&dm->node, &sctx->waiting_dir_moves); | |
8085 | kfree(dm); | |
8086 | } | |
8087 | ||
9dc44214 FM |
8088 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs)); |
8089 | while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) { | |
8090 | struct rb_node *n; | |
8091 | struct orphan_dir_info *odi; | |
8092 | ||
8093 | n = rb_first(&sctx->orphan_dirs); | |
8094 | odi = rb_entry(n, struct orphan_dir_info, node); | |
8095 | free_orphan_dir_info(sctx, odi); | |
8096 | } | |
8097 | ||
896c14f9 | 8098 | if (sort_clone_roots) { |
6f9a3da5 | 8099 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
896c14f9 WS |
8100 | btrfs_root_dec_send_in_progress( |
8101 | sctx->clone_roots[i].root); | |
00246528 | 8102 | btrfs_put_root(sctx->clone_roots[i].root); |
6f9a3da5 | 8103 | } |
896c14f9 | 8104 | } else { |
6f9a3da5 | 8105 | for (i = 0; sctx && i < clone_sources_to_rollback; i++) { |
896c14f9 WS |
8106 | btrfs_root_dec_send_in_progress( |
8107 | sctx->clone_roots[i].root); | |
00246528 | 8108 | btrfs_put_root(sctx->clone_roots[i].root); |
6f9a3da5 | 8109 | } |
896c14f9 WS |
8110 | |
8111 | btrfs_root_dec_send_in_progress(send_root); | |
8112 | } | |
6f9a3da5 | 8113 | if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) { |
66ef7d65 | 8114 | btrfs_root_dec_send_in_progress(sctx->parent_root); |
00246528 | 8115 | btrfs_put_root(sctx->parent_root); |
6f9a3da5 | 8116 | } |
2c686537 | 8117 | |
2f91306a | 8118 | kvfree(clone_sources_tmp); |
31db9f7c AB |
8119 | |
8120 | if (sctx) { | |
8121 | if (sctx->send_filp) | |
8122 | fput(sctx->send_filp); | |
8123 | ||
c03d01f3 | 8124 | kvfree(sctx->clone_roots); |
a4b333f2 | 8125 | kfree(sctx->send_buf_pages); |
6ff48ce0 | 8126 | kvfree(sctx->send_buf); |
38622010 | 8127 | kvfree(sctx->verity_descriptor); |
31db9f7c AB |
8128 | |
8129 | name_cache_free(sctx); | |
8130 | ||
152555b3 | 8131 | close_current_inode(sctx); |
521b6803 | 8132 | |
31db9f7c AB |
8133 | kfree(sctx); |
8134 | } | |
8135 | ||
8136 | return ret; | |
8137 | } |