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