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