David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Alexander Block. All rights reserved. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 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> |
Stephen Rothwell | a1857eb | 2012-07-27 10:11:13 +1000 | [diff] [blame] | 14 | #include <linux/vmalloc.h> |
Andy Shevchenko | ed84885 | 2013-08-21 10:32:13 +0300 | [diff] [blame] | 15 | #include <linux/string.h> |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 16 | #include <linux/compat.h> |
Nikolay Borisov | 9678c54 | 2018-01-08 11:45:05 +0200 | [diff] [blame] | 17 | #include <linux/crc32c.h> |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 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" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 25 | #include "compression.h" |
Marcos Paulo de Souza | 89efda5 | 2020-05-10 23:15:07 -0300 | [diff] [blame] | 26 | #include "xattr.h" |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 27 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 28 | /* |
Filipe Manana | fd0ddbe | 2019-10-30 12:23:01 +0000 | [diff] [blame] | 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 |
| 35 | |
| 36 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 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; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 48 | |
| 49 | char *buf; |
David Sterba | 1f5a7ff | 2014-02-03 19:23:47 +0100 | [diff] [blame] | 50 | unsigned short buf_len:15; |
| 51 | unsigned short reversed:1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 52 | char inline_buf[]; |
| 53 | }; |
David Sterba | ace0105 | 2014-02-05 16:17:34 +0100 | [diff] [blame] | 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]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 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]; |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 86 | u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 87 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 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; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 107 | u64 cur_inode_size; |
| 108 | u64 cur_inode_mode; |
Liu Bo | 644d194 | 2014-02-27 17:29:01 +0800 | [diff] [blame] | 109 | u64 cur_inode_rdev; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 110 | u64 cur_inode_last_extent; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 111 | u64 cur_inode_next_write_offset; |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 112 | bool ignore_cur_inode; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 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 | |
Liu Bo | 2131bcd | 2014-03-05 10:07:35 +0800 | [diff] [blame] | 123 | struct file_ra_state ra; |
| 124 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 125 | /* |
| 126 | * We process inodes by their increasing order, so if before an |
| 127 | * incremental send we reverse the parent/child relationship of |
| 128 | * directories such that a directory with a lower inode number was |
| 129 | * the parent of a directory with a higher inode number, and the one |
| 130 | * becoming the new parent got renamed too, we can't rename/move the |
| 131 | * directory with lower inode number when we finish processing it - we |
| 132 | * must process the directory with higher inode number first, then |
| 133 | * rename/move it and then rename/move the directory with lower inode |
| 134 | * number. Example follows. |
| 135 | * |
| 136 | * Tree state when the first send was performed: |
| 137 | * |
| 138 | * . |
| 139 | * |-- a (ino 257) |
| 140 | * |-- b (ino 258) |
| 141 | * | |
| 142 | * | |
| 143 | * |-- c (ino 259) |
| 144 | * | |-- d (ino 260) |
| 145 | * | |
| 146 | * |-- c2 (ino 261) |
| 147 | * |
| 148 | * Tree state when the second (incremental) send is performed: |
| 149 | * |
| 150 | * . |
| 151 | * |-- a (ino 257) |
| 152 | * |-- b (ino 258) |
| 153 | * |-- c2 (ino 261) |
| 154 | * |-- d2 (ino 260) |
| 155 | * |-- cc (ino 259) |
| 156 | * |
| 157 | * The sequence of steps that lead to the second state was: |
| 158 | * |
| 159 | * mv /a/b/c/d /a/b/c2/d2 |
| 160 | * mv /a/b/c /a/b/c2/d2/cc |
| 161 | * |
| 162 | * "c" has lower inode number, but we can't move it (2nd mv operation) |
| 163 | * before we move "d", which has higher inode number. |
| 164 | * |
| 165 | * So we just memorize which move/rename operations must be performed |
| 166 | * later when their respective parent is processed and moved/renamed. |
| 167 | */ |
| 168 | |
| 169 | /* Indexed by parent directory inode number. */ |
| 170 | struct rb_root pending_dir_moves; |
| 171 | |
| 172 | /* |
| 173 | * Reverse index, indexed by the inode number of a directory that |
| 174 | * is waiting for the move/rename of its immediate parent before its |
| 175 | * own move/rename can be performed. |
| 176 | */ |
| 177 | struct rb_root waiting_dir_moves; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * A directory that is going to be rm'ed might have a child directory |
| 181 | * which is in the pending directory moves index above. In this case, |
| 182 | * the directory can only be removed after the move/rename of its child |
| 183 | * is performed. Example: |
| 184 | * |
| 185 | * Parent snapshot: |
| 186 | * |
| 187 | * . (ino 256) |
| 188 | * |-- a/ (ino 257) |
| 189 | * |-- b/ (ino 258) |
| 190 | * |-- c/ (ino 259) |
| 191 | * | |-- x/ (ino 260) |
| 192 | * | |
| 193 | * |-- y/ (ino 261) |
| 194 | * |
| 195 | * Send snapshot: |
| 196 | * |
| 197 | * . (ino 256) |
| 198 | * |-- a/ (ino 257) |
| 199 | * |-- b/ (ino 258) |
| 200 | * |-- YY/ (ino 261) |
| 201 | * |-- x/ (ino 260) |
| 202 | * |
| 203 | * Sequence of steps that lead to the send snapshot: |
| 204 | * rm -f /a/b/c/foo.txt |
| 205 | * mv /a/b/y /a/b/YY |
| 206 | * mv /a/b/c/x /a/b/YY |
| 207 | * rmdir /a/b/c |
| 208 | * |
| 209 | * When the child is processed, its move/rename is delayed until its |
| 210 | * parent is processed (as explained above), but all other operations |
| 211 | * like update utimes, chown, chgrp, etc, are performed and the paths |
| 212 | * that it uses for those operations must use the orphanized name of |
| 213 | * its parent (the directory we're going to rm later), so we need to |
| 214 | * memorize that name. |
| 215 | * |
| 216 | * Indexed by the inode number of the directory to be deleted. |
| 217 | */ |
| 218 | struct rb_root orphan_dirs; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | struct pending_dir_move { |
| 222 | struct rb_node node; |
| 223 | struct list_head list; |
| 224 | u64 parent_ino; |
| 225 | u64 ino; |
| 226 | u64 gen; |
| 227 | struct list_head update_refs; |
| 228 | }; |
| 229 | |
| 230 | struct waiting_dir_move { |
| 231 | struct rb_node node; |
| 232 | u64 ino; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 233 | /* |
| 234 | * There might be some directory that could not be removed because it |
| 235 | * was waiting for this directory inode to be moved first. Therefore |
| 236 | * after this directory is moved, we can try to rmdir the ino rmdir_ino. |
| 237 | */ |
| 238 | u64 rmdir_ino; |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 239 | bool orphanized; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | struct orphan_dir_info { |
| 243 | struct rb_node node; |
| 244 | u64 ino; |
| 245 | u64 gen; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 246 | u64 last_dir_index_offset; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | struct name_cache_entry { |
| 250 | struct list_head list; |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 251 | /* |
| 252 | * radix_tree has only 32bit entries but we need to handle 64bit inums. |
| 253 | * We use the lower 32bit of the 64bit inum to store it in the tree. If |
| 254 | * more then one inum would fall into the same entry, we use radix_list |
| 255 | * to store the additional entries. radix_list is also used to store |
| 256 | * entries where two entries have the same inum but different |
| 257 | * generations. |
| 258 | */ |
| 259 | struct list_head radix_list; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 260 | u64 ino; |
| 261 | u64 gen; |
| 262 | u64 parent_ino; |
| 263 | u64 parent_gen; |
| 264 | int ret; |
| 265 | int need_later_update; |
| 266 | int name_len; |
| 267 | char name[]; |
| 268 | }; |
| 269 | |
David Sterba | 18d0f5c | 2019-08-21 19:12:59 +0200 | [diff] [blame] | 270 | #define ADVANCE 1 |
| 271 | #define ADVANCE_ONLY_NEXT -1 |
| 272 | |
| 273 | enum btrfs_compare_tree_result { |
| 274 | BTRFS_COMPARE_TREE_NEW, |
| 275 | BTRFS_COMPARE_TREE_DELETED, |
| 276 | BTRFS_COMPARE_TREE_CHANGED, |
| 277 | BTRFS_COMPARE_TREE_SAME, |
| 278 | }; |
David Sterba | 18d0f5c | 2019-08-21 19:12:59 +0200 | [diff] [blame] | 279 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 280 | __cold |
Filipe Manana | 9515558 | 2016-08-01 01:50:37 +0100 | [diff] [blame] | 281 | static void inconsistent_snapshot_error(struct send_ctx *sctx, |
| 282 | enum btrfs_compare_tree_result result, |
| 283 | const char *what) |
| 284 | { |
| 285 | const char *result_string; |
| 286 | |
| 287 | switch (result) { |
| 288 | case BTRFS_COMPARE_TREE_NEW: |
| 289 | result_string = "new"; |
| 290 | break; |
| 291 | case BTRFS_COMPARE_TREE_DELETED: |
| 292 | result_string = "deleted"; |
| 293 | break; |
| 294 | case BTRFS_COMPARE_TREE_CHANGED: |
| 295 | result_string = "updated"; |
| 296 | break; |
| 297 | case BTRFS_COMPARE_TREE_SAME: |
| 298 | ASSERT(0); |
| 299 | result_string = "unchanged"; |
| 300 | break; |
| 301 | default: |
| 302 | ASSERT(0); |
| 303 | result_string = "unexpected"; |
| 304 | } |
| 305 | |
| 306 | btrfs_err(sctx->send_root->fs_info, |
| 307 | "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu", |
| 308 | result_string, what, sctx->cmp_key->objectid, |
| 309 | sctx->send_root->root_key.objectid, |
| 310 | (sctx->parent_root ? |
| 311 | sctx->parent_root->root_key.objectid : 0)); |
| 312 | } |
| 313 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 314 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino); |
| 315 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 316 | static struct waiting_dir_move * |
| 317 | get_waiting_dir_move(struct send_ctx *sctx, u64 ino); |
| 318 | |
| 319 | static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino); |
| 320 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 321 | static int need_send_hole(struct send_ctx *sctx) |
| 322 | { |
| 323 | return (sctx->parent_root && !sctx->cur_inode_new && |
| 324 | !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted && |
| 325 | S_ISREG(sctx->cur_inode_mode)); |
| 326 | } |
| 327 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 328 | static void fs_path_reset(struct fs_path *p) |
| 329 | { |
| 330 | if (p->reversed) { |
| 331 | p->start = p->buf + p->buf_len - 1; |
| 332 | p->end = p->start; |
| 333 | *p->start = 0; |
| 334 | } else { |
| 335 | p->start = p->buf; |
| 336 | p->end = p->start; |
| 337 | *p->start = 0; |
| 338 | } |
| 339 | } |
| 340 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 341 | static struct fs_path *fs_path_alloc(void) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 342 | { |
| 343 | struct fs_path *p; |
| 344 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 345 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 346 | if (!p) |
| 347 | return NULL; |
| 348 | p->reversed = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 349 | p->buf = p->inline_buf; |
| 350 | p->buf_len = FS_PATH_INLINE_SIZE; |
| 351 | fs_path_reset(p); |
| 352 | return p; |
| 353 | } |
| 354 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 355 | static struct fs_path *fs_path_alloc_reversed(void) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 356 | { |
| 357 | struct fs_path *p; |
| 358 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 359 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 360 | if (!p) |
| 361 | return NULL; |
| 362 | p->reversed = 1; |
| 363 | fs_path_reset(p); |
| 364 | return p; |
| 365 | } |
| 366 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 367 | static void fs_path_free(struct fs_path *p) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 368 | { |
| 369 | if (!p) |
| 370 | return; |
David Sterba | ace0105 | 2014-02-05 16:17:34 +0100 | [diff] [blame] | 371 | if (p->buf != p->inline_buf) |
| 372 | kfree(p->buf); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 373 | kfree(p); |
| 374 | } |
| 375 | |
| 376 | static int fs_path_len(struct fs_path *p) |
| 377 | { |
| 378 | return p->end - p->start; |
| 379 | } |
| 380 | |
| 381 | static int fs_path_ensure_buf(struct fs_path *p, int len) |
| 382 | { |
| 383 | char *tmp_buf; |
| 384 | int path_len; |
| 385 | int old_buf_len; |
| 386 | |
| 387 | len++; |
| 388 | |
| 389 | if (p->buf_len >= len) |
| 390 | return 0; |
| 391 | |
Chris Mason | cfd4a53 | 2014-04-26 05:02:03 -0700 | [diff] [blame] | 392 | if (len > PATH_MAX) { |
| 393 | WARN_ON(1); |
| 394 | return -ENOMEM; |
| 395 | } |
| 396 | |
David Sterba | 1b2782c | 2014-02-25 19:32:59 +0100 | [diff] [blame] | 397 | path_len = p->end - p->start; |
| 398 | old_buf_len = p->buf_len; |
| 399 | |
David Sterba | ace0105 | 2014-02-05 16:17:34 +0100 | [diff] [blame] | 400 | /* |
| 401 | * First time the inline_buf does not suffice |
| 402 | */ |
Filipe Manana | 01a9a8a | 2014-05-21 17:38:13 +0100 | [diff] [blame] | 403 | if (p->buf == p->inline_buf) { |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 404 | tmp_buf = kmalloc(len, GFP_KERNEL); |
Filipe Manana | 01a9a8a | 2014-05-21 17:38:13 +0100 | [diff] [blame] | 405 | if (tmp_buf) |
| 406 | memcpy(tmp_buf, p->buf, old_buf_len); |
| 407 | } else { |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 408 | tmp_buf = krealloc(p->buf, len, GFP_KERNEL); |
Filipe Manana | 01a9a8a | 2014-05-21 17:38:13 +0100 | [diff] [blame] | 409 | } |
David Sterba | 9c9ca00 | 2014-02-25 19:33:08 +0100 | [diff] [blame] | 410 | if (!tmp_buf) |
| 411 | return -ENOMEM; |
| 412 | p->buf = tmp_buf; |
| 413 | /* |
| 414 | * The real size of the buffer is bigger, this will let the fast path |
| 415 | * happen most of the time |
| 416 | */ |
| 417 | p->buf_len = ksize(p->buf); |
David Sterba | ace0105 | 2014-02-05 16:17:34 +0100 | [diff] [blame] | 418 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 419 | if (p->reversed) { |
| 420 | tmp_buf = p->buf + old_buf_len - path_len - 1; |
| 421 | p->end = p->buf + p->buf_len - 1; |
| 422 | p->start = p->end - path_len; |
| 423 | memmove(p->start, tmp_buf, path_len + 1); |
| 424 | } else { |
| 425 | p->start = p->buf; |
| 426 | p->end = p->start + path_len; |
| 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 431 | static int fs_path_prepare_for_add(struct fs_path *p, int name_len, |
| 432 | char **prepared) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 433 | { |
| 434 | int ret; |
| 435 | int new_len; |
| 436 | |
| 437 | new_len = p->end - p->start + name_len; |
| 438 | if (p->start != p->end) |
| 439 | new_len++; |
| 440 | ret = fs_path_ensure_buf(p, new_len); |
| 441 | if (ret < 0) |
| 442 | goto out; |
| 443 | |
| 444 | if (p->reversed) { |
| 445 | if (p->start != p->end) |
| 446 | *--p->start = '/'; |
| 447 | p->start -= name_len; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 448 | *prepared = p->start; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 449 | } else { |
| 450 | if (p->start != p->end) |
| 451 | *p->end++ = '/'; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 452 | *prepared = p->end; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 453 | p->end += name_len; |
| 454 | *p->end = 0; |
| 455 | } |
| 456 | |
| 457 | out: |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | static int fs_path_add(struct fs_path *p, const char *name, int name_len) |
| 462 | { |
| 463 | int ret; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 464 | char *prepared; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 465 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 466 | ret = fs_path_prepare_for_add(p, name_len, &prepared); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 467 | if (ret < 0) |
| 468 | goto out; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 469 | memcpy(prepared, name, name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 470 | |
| 471 | out: |
| 472 | return ret; |
| 473 | } |
| 474 | |
| 475 | static int fs_path_add_path(struct fs_path *p, struct fs_path *p2) |
| 476 | { |
| 477 | int ret; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 478 | char *prepared; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 479 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 480 | ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 481 | if (ret < 0) |
| 482 | goto out; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 483 | memcpy(prepared, p2->start, p2->end - p2->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 484 | |
| 485 | out: |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | static int fs_path_add_from_extent_buffer(struct fs_path *p, |
| 490 | struct extent_buffer *eb, |
| 491 | unsigned long off, int len) |
| 492 | { |
| 493 | int ret; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 494 | char *prepared; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 495 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 496 | ret = fs_path_prepare_for_add(p, len, &prepared); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 497 | if (ret < 0) |
| 498 | goto out; |
| 499 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 500 | read_extent_buffer(eb, prepared, off, len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 501 | |
| 502 | out: |
| 503 | return ret; |
| 504 | } |
| 505 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 506 | static int fs_path_copy(struct fs_path *p, struct fs_path *from) |
| 507 | { |
| 508 | int ret; |
| 509 | |
| 510 | p->reversed = from->reversed; |
| 511 | fs_path_reset(p); |
| 512 | |
| 513 | ret = fs_path_add_path(p, from); |
| 514 | |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | static void fs_path_unreverse(struct fs_path *p) |
| 520 | { |
| 521 | char *tmp; |
| 522 | int len; |
| 523 | |
| 524 | if (!p->reversed) |
| 525 | return; |
| 526 | |
| 527 | tmp = p->start; |
| 528 | len = p->end - p->start; |
| 529 | p->start = p->buf; |
| 530 | p->end = p->start + len; |
| 531 | memmove(p->start, tmp, len + 1); |
| 532 | p->reversed = 0; |
| 533 | } |
| 534 | |
| 535 | static struct btrfs_path *alloc_path_for_send(void) |
| 536 | { |
| 537 | struct btrfs_path *path; |
| 538 | |
| 539 | path = btrfs_alloc_path(); |
| 540 | if (!path) |
| 541 | return NULL; |
| 542 | path->search_commit_root = 1; |
| 543 | path->skip_locking = 1; |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 544 | path->need_commit_sem = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 545 | return path; |
| 546 | } |
| 547 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 548 | static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 549 | { |
| 550 | int ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 551 | u32 pos = 0; |
| 552 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 553 | while (pos < len) { |
Christoph Hellwig | 8e93157 | 2017-09-01 17:39:19 +0200 | [diff] [blame] | 554 | ret = kernel_write(filp, buf + pos, len - pos, off); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 555 | /* TODO handle that correctly */ |
| 556 | /*if (ret == -ERESTARTSYS) { |
| 557 | continue; |
| 558 | }*/ |
| 559 | if (ret < 0) |
Christoph Hellwig | 8e93157 | 2017-09-01 17:39:19 +0200 | [diff] [blame] | 560 | return ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 561 | if (ret == 0) { |
Christoph Hellwig | 8e93157 | 2017-09-01 17:39:19 +0200 | [diff] [blame] | 562 | return -EIO; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 563 | } |
| 564 | pos += ret; |
| 565 | } |
| 566 | |
Christoph Hellwig | 8e93157 | 2017-09-01 17:39:19 +0200 | [diff] [blame] | 567 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) |
| 571 | { |
| 572 | struct btrfs_tlv_header *hdr; |
| 573 | int total_len = sizeof(*hdr) + len; |
| 574 | int left = sctx->send_max_size - sctx->send_size; |
| 575 | |
| 576 | if (unlikely(left < total_len)) |
| 577 | return -EOVERFLOW; |
| 578 | |
| 579 | hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); |
| 580 | hdr->tlv_type = cpu_to_le16(attr); |
| 581 | hdr->tlv_len = cpu_to_le16(len); |
| 582 | memcpy(hdr + 1, data, len); |
| 583 | sctx->send_size += total_len; |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
David Sterba | 95bc79d | 2013-12-16 17:34:10 +0100 | [diff] [blame] | 588 | #define TLV_PUT_DEFINE_INT(bits) \ |
| 589 | static int tlv_put_u##bits(struct send_ctx *sctx, \ |
| 590 | u##bits attr, u##bits value) \ |
| 591 | { \ |
| 592 | __le##bits __tmp = cpu_to_le##bits(value); \ |
| 593 | return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \ |
| 594 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 595 | |
David Sterba | 95bc79d | 2013-12-16 17:34:10 +0100 | [diff] [blame] | 596 | TLV_PUT_DEFINE_INT(64) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 597 | |
| 598 | static int tlv_put_string(struct send_ctx *sctx, u16 attr, |
| 599 | const char *str, int len) |
| 600 | { |
| 601 | if (len == -1) |
| 602 | len = strlen(str); |
| 603 | return tlv_put(sctx, attr, str, len); |
| 604 | } |
| 605 | |
| 606 | static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, |
| 607 | const u8 *uuid) |
| 608 | { |
| 609 | return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); |
| 610 | } |
| 611 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 612 | static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, |
| 613 | struct extent_buffer *eb, |
| 614 | struct btrfs_timespec *ts) |
| 615 | { |
| 616 | struct btrfs_timespec bts; |
| 617 | read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts)); |
| 618 | return tlv_put(sctx, attr, &bts, sizeof(bts)); |
| 619 | } |
| 620 | |
| 621 | |
Liu Bo | 895a72b | 2018-03-02 18:05:49 -0700 | [diff] [blame] | 622 | #define TLV_PUT(sctx, attrtype, data, attrlen) \ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 623 | do { \ |
Liu Bo | 895a72b | 2018-03-02 18:05:49 -0700 | [diff] [blame] | 624 | ret = tlv_put(sctx, attrtype, data, attrlen); \ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 625 | if (ret < 0) \ |
| 626 | goto tlv_put_failure; \ |
| 627 | } while (0) |
| 628 | |
| 629 | #define TLV_PUT_INT(sctx, attrtype, bits, value) \ |
| 630 | do { \ |
| 631 | ret = tlv_put_u##bits(sctx, attrtype, value); \ |
| 632 | if (ret < 0) \ |
| 633 | goto tlv_put_failure; \ |
| 634 | } while (0) |
| 635 | |
| 636 | #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) |
| 637 | #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) |
| 638 | #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) |
| 639 | #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) |
| 640 | #define TLV_PUT_STRING(sctx, attrtype, str, len) \ |
| 641 | do { \ |
| 642 | ret = tlv_put_string(sctx, attrtype, str, len); \ |
| 643 | if (ret < 0) \ |
| 644 | goto tlv_put_failure; \ |
| 645 | } while (0) |
| 646 | #define TLV_PUT_PATH(sctx, attrtype, p) \ |
| 647 | do { \ |
| 648 | ret = tlv_put_string(sctx, attrtype, p->start, \ |
| 649 | p->end - p->start); \ |
| 650 | if (ret < 0) \ |
| 651 | goto tlv_put_failure; \ |
| 652 | } while(0) |
| 653 | #define TLV_PUT_UUID(sctx, attrtype, uuid) \ |
| 654 | do { \ |
| 655 | ret = tlv_put_uuid(sctx, attrtype, uuid); \ |
| 656 | if (ret < 0) \ |
| 657 | goto tlv_put_failure; \ |
| 658 | } while (0) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 659 | #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ |
| 660 | do { \ |
| 661 | ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \ |
| 662 | if (ret < 0) \ |
| 663 | goto tlv_put_failure; \ |
| 664 | } while (0) |
| 665 | |
| 666 | static int send_header(struct send_ctx *sctx) |
| 667 | { |
| 668 | struct btrfs_stream_header hdr; |
| 669 | |
| 670 | strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC); |
| 671 | hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION); |
| 672 | |
Anand Jain | 1bcea35 | 2012-09-14 00:04:21 -0600 | [diff] [blame] | 673 | return write_buf(sctx->send_filp, &hdr, sizeof(hdr), |
| 674 | &sctx->send_off); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* |
| 678 | * For each command/item we want to send to userspace, we call this function. |
| 679 | */ |
| 680 | static int begin_cmd(struct send_ctx *sctx, int cmd) |
| 681 | { |
| 682 | struct btrfs_cmd_header *hdr; |
| 683 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 684 | if (WARN_ON(!sctx->send_buf)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 685 | return -EINVAL; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 686 | |
| 687 | BUG_ON(sctx->send_size); |
| 688 | |
| 689 | sctx->send_size += sizeof(*hdr); |
| 690 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| 691 | hdr->cmd = cpu_to_le16(cmd); |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | static int send_cmd(struct send_ctx *sctx) |
| 697 | { |
| 698 | int ret; |
| 699 | struct btrfs_cmd_header *hdr; |
| 700 | u32 crc; |
| 701 | |
| 702 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| 703 | hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr)); |
| 704 | hdr->crc = 0; |
| 705 | |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 706 | crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 707 | hdr->crc = cpu_to_le32(crc); |
| 708 | |
Anand Jain | 1bcea35 | 2012-09-14 00:04:21 -0600 | [diff] [blame] | 709 | ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, |
| 710 | &sctx->send_off); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 711 | |
| 712 | sctx->total_send_size += sctx->send_size; |
| 713 | sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size; |
| 714 | sctx->send_size = 0; |
| 715 | |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Sends a move instruction to user space |
| 721 | */ |
| 722 | static int send_rename(struct send_ctx *sctx, |
| 723 | struct fs_path *from, struct fs_path *to) |
| 724 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 725 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 726 | int ret; |
| 727 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 728 | btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 729 | |
| 730 | ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); |
| 731 | if (ret < 0) |
| 732 | goto out; |
| 733 | |
| 734 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); |
| 735 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); |
| 736 | |
| 737 | ret = send_cmd(sctx); |
| 738 | |
| 739 | tlv_put_failure: |
| 740 | out: |
| 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * Sends a link instruction to user space |
| 746 | */ |
| 747 | static int send_link(struct send_ctx *sctx, |
| 748 | struct fs_path *path, struct fs_path *lnk) |
| 749 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 750 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 751 | int ret; |
| 752 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 753 | btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 754 | |
| 755 | ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); |
| 756 | if (ret < 0) |
| 757 | goto out; |
| 758 | |
| 759 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 760 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); |
| 761 | |
| 762 | ret = send_cmd(sctx); |
| 763 | |
| 764 | tlv_put_failure: |
| 765 | out: |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * Sends an unlink instruction to user space |
| 771 | */ |
| 772 | static int send_unlink(struct send_ctx *sctx, struct fs_path *path) |
| 773 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 774 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 775 | int ret; |
| 776 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 777 | btrfs_debug(fs_info, "send_unlink %s", path->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 778 | |
| 779 | ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); |
| 780 | if (ret < 0) |
| 781 | goto out; |
| 782 | |
| 783 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 784 | |
| 785 | ret = send_cmd(sctx); |
| 786 | |
| 787 | tlv_put_failure: |
| 788 | out: |
| 789 | return ret; |
| 790 | } |
| 791 | |
| 792 | /* |
| 793 | * Sends a rmdir instruction to user space |
| 794 | */ |
| 795 | static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) |
| 796 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 797 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 798 | int ret; |
| 799 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 800 | btrfs_debug(fs_info, "send_rmdir %s", path->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 801 | |
| 802 | ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); |
| 803 | if (ret < 0) |
| 804 | goto out; |
| 805 | |
| 806 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 807 | |
| 808 | ret = send_cmd(sctx); |
| 809 | |
| 810 | tlv_put_failure: |
| 811 | out: |
| 812 | return ret; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Helper function to retrieve some fields from an inode item. |
| 817 | */ |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 818 | static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path, |
| 819 | u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid, |
| 820 | u64 *gid, u64 *rdev) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 821 | { |
| 822 | int ret; |
| 823 | struct btrfs_inode_item *ii; |
| 824 | struct btrfs_key key; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 825 | |
| 826 | key.objectid = ino; |
| 827 | key.type = BTRFS_INODE_ITEM_KEY; |
| 828 | key.offset = 0; |
| 829 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 830 | if (ret) { |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 831 | if (ret > 0) |
| 832 | ret = -ENOENT; |
| 833 | return ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | ii = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 837 | struct btrfs_inode_item); |
| 838 | if (size) |
| 839 | *size = btrfs_inode_size(path->nodes[0], ii); |
| 840 | if (gen) |
| 841 | *gen = btrfs_inode_generation(path->nodes[0], ii); |
| 842 | if (mode) |
| 843 | *mode = btrfs_inode_mode(path->nodes[0], ii); |
| 844 | if (uid) |
| 845 | *uid = btrfs_inode_uid(path->nodes[0], ii); |
| 846 | if (gid) |
| 847 | *gid = btrfs_inode_gid(path->nodes[0], ii); |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 848 | if (rdev) |
| 849 | *rdev = btrfs_inode_rdev(path->nodes[0], ii); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 850 | |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | static int get_inode_info(struct btrfs_root *root, |
| 855 | u64 ino, u64 *size, u64 *gen, |
| 856 | u64 *mode, u64 *uid, u64 *gid, |
| 857 | u64 *rdev) |
| 858 | { |
| 859 | struct btrfs_path *path; |
| 860 | int ret; |
| 861 | |
| 862 | path = alloc_path_for_send(); |
| 863 | if (!path) |
| 864 | return -ENOMEM; |
| 865 | ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid, |
| 866 | rdev); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 867 | btrfs_free_path(path); |
| 868 | return ret; |
| 869 | } |
| 870 | |
| 871 | typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index, |
| 872 | struct fs_path *p, |
| 873 | void *ctx); |
| 874 | |
| 875 | /* |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 876 | * Helper function to iterate the entries in ONE btrfs_inode_ref or |
| 877 | * btrfs_inode_extref. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 878 | * The iterate callback may return a non zero value to stop iteration. This can |
| 879 | * be a negative value for error codes or 1 to simply stop it. |
| 880 | * |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 881 | * path must point to the INODE_REF or INODE_EXTREF when called. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 882 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 883 | static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 884 | struct btrfs_key *found_key, int resolve, |
| 885 | iterate_inode_ref_t iterate, void *ctx) |
| 886 | { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 887 | struct extent_buffer *eb = path->nodes[0]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 888 | struct btrfs_item *item; |
| 889 | struct btrfs_inode_ref *iref; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 890 | struct btrfs_inode_extref *extref; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 891 | struct btrfs_path *tmp_path; |
| 892 | struct fs_path *p; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 893 | u32 cur = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 894 | u32 total; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 895 | int slot = path->slots[0]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 896 | u32 name_len; |
| 897 | char *start; |
| 898 | int ret = 0; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 899 | int num = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 900 | int index; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 901 | u64 dir; |
| 902 | unsigned long name_off; |
| 903 | unsigned long elem_size; |
| 904 | unsigned long ptr; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 905 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 906 | p = fs_path_alloc_reversed(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 907 | if (!p) |
| 908 | return -ENOMEM; |
| 909 | |
| 910 | tmp_path = alloc_path_for_send(); |
| 911 | if (!tmp_path) { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 912 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 913 | return -ENOMEM; |
| 914 | } |
| 915 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 916 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 917 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
| 918 | ptr = (unsigned long)btrfs_item_ptr(eb, slot, |
| 919 | struct btrfs_inode_ref); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 920 | item = btrfs_item_nr(slot); |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 921 | total = btrfs_item_size(eb, item); |
| 922 | elem_size = sizeof(*iref); |
| 923 | } else { |
| 924 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 925 | total = btrfs_item_size_nr(eb, slot); |
| 926 | elem_size = sizeof(*extref); |
| 927 | } |
| 928 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 929 | while (cur < total) { |
| 930 | fs_path_reset(p); |
| 931 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 932 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
| 933 | iref = (struct btrfs_inode_ref *)(ptr + cur); |
| 934 | name_len = btrfs_inode_ref_name_len(eb, iref); |
| 935 | name_off = (unsigned long)(iref + 1); |
| 936 | index = btrfs_inode_ref_index(eb, iref); |
| 937 | dir = found_key->offset; |
| 938 | } else { |
| 939 | extref = (struct btrfs_inode_extref *)(ptr + cur); |
| 940 | name_len = btrfs_inode_extref_name_len(eb, extref); |
| 941 | name_off = (unsigned long)&extref->name; |
| 942 | index = btrfs_inode_extref_index(eb, extref); |
| 943 | dir = btrfs_inode_extref_parent(eb, extref); |
| 944 | } |
| 945 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 946 | if (resolve) { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 947 | start = btrfs_ref_to_path(root, tmp_path, name_len, |
| 948 | name_off, eb, dir, |
| 949 | p->buf, p->buf_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 950 | if (IS_ERR(start)) { |
| 951 | ret = PTR_ERR(start); |
| 952 | goto out; |
| 953 | } |
| 954 | if (start < p->buf) { |
| 955 | /* overflow , try again with larger buffer */ |
| 956 | ret = fs_path_ensure_buf(p, |
| 957 | p->buf_len + p->buf - start); |
| 958 | if (ret < 0) |
| 959 | goto out; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 960 | start = btrfs_ref_to_path(root, tmp_path, |
| 961 | name_len, name_off, |
| 962 | eb, dir, |
| 963 | p->buf, p->buf_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 964 | if (IS_ERR(start)) { |
| 965 | ret = PTR_ERR(start); |
| 966 | goto out; |
| 967 | } |
| 968 | BUG_ON(start < p->buf); |
| 969 | } |
| 970 | p->start = start; |
| 971 | } else { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 972 | ret = fs_path_add_from_extent_buffer(p, eb, name_off, |
| 973 | name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 974 | if (ret < 0) |
| 975 | goto out; |
| 976 | } |
| 977 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 978 | cur += elem_size + name_len; |
| 979 | ret = iterate(num, dir, index, p, ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 980 | if (ret) |
| 981 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 982 | num++; |
| 983 | } |
| 984 | |
| 985 | out: |
| 986 | btrfs_free_path(tmp_path); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 987 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key, |
| 992 | const char *name, int name_len, |
| 993 | const char *data, int data_len, |
| 994 | u8 type, void *ctx); |
| 995 | |
| 996 | /* |
| 997 | * Helper function to iterate the entries in ONE btrfs_dir_item. |
| 998 | * The iterate callback may return a non zero value to stop iteration. This can |
| 999 | * be a negative value for error codes or 1 to simply stop it. |
| 1000 | * |
| 1001 | * path must point to the dir item when called. |
| 1002 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1003 | static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1004 | iterate_dir_item_t iterate, void *ctx) |
| 1005 | { |
| 1006 | int ret = 0; |
| 1007 | struct extent_buffer *eb; |
| 1008 | struct btrfs_item *item; |
| 1009 | struct btrfs_dir_item *di; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1010 | struct btrfs_key di_key; |
| 1011 | char *buf = NULL; |
Filipe Manana | 7e3ae33 | 2014-05-23 20:15:16 +0100 | [diff] [blame] | 1012 | int buf_len; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1013 | u32 name_len; |
| 1014 | u32 data_len; |
| 1015 | u32 cur; |
| 1016 | u32 len; |
| 1017 | u32 total; |
| 1018 | int slot; |
| 1019 | int num; |
| 1020 | u8 type; |
| 1021 | |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1022 | /* |
| 1023 | * Start with a small buffer (1 page). If later we end up needing more |
| 1024 | * space, which can happen for xattrs on a fs with a leaf size greater |
| 1025 | * then the page size, attempt to increase the buffer. Typically xattr |
| 1026 | * values are small. |
| 1027 | */ |
| 1028 | buf_len = PATH_MAX; |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 1029 | buf = kmalloc(buf_len, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1030 | if (!buf) { |
| 1031 | ret = -ENOMEM; |
| 1032 | goto out; |
| 1033 | } |
| 1034 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1035 | eb = path->nodes[0]; |
| 1036 | slot = path->slots[0]; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 1037 | item = btrfs_item_nr(slot); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1038 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| 1039 | cur = 0; |
| 1040 | len = 0; |
| 1041 | total = btrfs_item_size(eb, item); |
| 1042 | |
| 1043 | num = 0; |
| 1044 | while (cur < total) { |
| 1045 | name_len = btrfs_dir_name_len(eb, di); |
| 1046 | data_len = btrfs_dir_data_len(eb, di); |
| 1047 | type = btrfs_dir_type(eb, di); |
| 1048 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 1049 | |
Filipe Manana | 7e3ae33 | 2014-05-23 20:15:16 +0100 | [diff] [blame] | 1050 | if (type == BTRFS_FT_XATTR) { |
| 1051 | if (name_len > XATTR_NAME_MAX) { |
| 1052 | ret = -ENAMETOOLONG; |
| 1053 | goto out; |
| 1054 | } |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1055 | if (name_len + data_len > |
| 1056 | BTRFS_MAX_XATTR_SIZE(root->fs_info)) { |
Filipe Manana | 7e3ae33 | 2014-05-23 20:15:16 +0100 | [diff] [blame] | 1057 | ret = -E2BIG; |
| 1058 | goto out; |
| 1059 | } |
| 1060 | } else { |
| 1061 | /* |
| 1062 | * Path too long |
| 1063 | */ |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1064 | if (name_len + data_len > PATH_MAX) { |
Filipe Manana | 7e3ae33 | 2014-05-23 20:15:16 +0100 | [diff] [blame] | 1065 | ret = -ENAMETOOLONG; |
| 1066 | goto out; |
| 1067 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1070 | if (name_len + data_len > buf_len) { |
| 1071 | buf_len = name_len + data_len; |
| 1072 | if (is_vmalloc_addr(buf)) { |
| 1073 | vfree(buf); |
| 1074 | buf = NULL; |
| 1075 | } else { |
| 1076 | char *tmp = krealloc(buf, buf_len, |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 1077 | GFP_KERNEL | __GFP_NOWARN); |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1078 | |
| 1079 | if (!tmp) |
| 1080 | kfree(buf); |
| 1081 | buf = tmp; |
| 1082 | } |
| 1083 | if (!buf) { |
David Sterba | f11f744 | 2017-05-31 18:40:02 +0200 | [diff] [blame] | 1084 | buf = kvmalloc(buf_len, GFP_KERNEL); |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1085 | if (!buf) { |
| 1086 | ret = -ENOMEM; |
| 1087 | goto out; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1092 | read_extent_buffer(eb, buf, (unsigned long)(di + 1), |
| 1093 | name_len + data_len); |
| 1094 | |
| 1095 | len = sizeof(*di) + name_len + data_len; |
| 1096 | di = (struct btrfs_dir_item *)((char *)di + len); |
| 1097 | cur += len; |
| 1098 | |
| 1099 | ret = iterate(num, &di_key, buf, name_len, buf + name_len, |
| 1100 | data_len, type, ctx); |
| 1101 | if (ret < 0) |
| 1102 | goto out; |
| 1103 | if (ret) { |
| 1104 | ret = 0; |
| 1105 | goto out; |
| 1106 | } |
| 1107 | |
| 1108 | num++; |
| 1109 | } |
| 1110 | |
| 1111 | out: |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1112 | kvfree(buf); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1113 | return ret; |
| 1114 | } |
| 1115 | |
| 1116 | static int __copy_first_ref(int num, u64 dir, int index, |
| 1117 | struct fs_path *p, void *ctx) |
| 1118 | { |
| 1119 | int ret; |
| 1120 | struct fs_path *pt = ctx; |
| 1121 | |
| 1122 | ret = fs_path_copy(pt, p); |
| 1123 | if (ret < 0) |
| 1124 | return ret; |
| 1125 | |
| 1126 | /* we want the first only */ |
| 1127 | return 1; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Retrieve the first path of an inode. If an inode has more then one |
| 1132 | * ref/hardlink, this is ignored. |
| 1133 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1134 | static int get_inode_path(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1135 | u64 ino, struct fs_path *path) |
| 1136 | { |
| 1137 | int ret; |
| 1138 | struct btrfs_key key, found_key; |
| 1139 | struct btrfs_path *p; |
| 1140 | |
| 1141 | p = alloc_path_for_send(); |
| 1142 | if (!p) |
| 1143 | return -ENOMEM; |
| 1144 | |
| 1145 | fs_path_reset(path); |
| 1146 | |
| 1147 | key.objectid = ino; |
| 1148 | key.type = BTRFS_INODE_REF_KEY; |
| 1149 | key.offset = 0; |
| 1150 | |
| 1151 | ret = btrfs_search_slot_for_read(root, &key, p, 1, 0); |
| 1152 | if (ret < 0) |
| 1153 | goto out; |
| 1154 | if (ret) { |
| 1155 | ret = 1; |
| 1156 | goto out; |
| 1157 | } |
| 1158 | btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]); |
| 1159 | if (found_key.objectid != ino || |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1160 | (found_key.type != BTRFS_INODE_REF_KEY && |
| 1161 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1162 | ret = -ENOENT; |
| 1163 | goto out; |
| 1164 | } |
| 1165 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1166 | ret = iterate_inode_ref(root, p, &found_key, 1, |
| 1167 | __copy_first_ref, path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1168 | if (ret < 0) |
| 1169 | goto out; |
| 1170 | ret = 0; |
| 1171 | |
| 1172 | out: |
| 1173 | btrfs_free_path(p); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | struct backref_ctx { |
| 1178 | struct send_ctx *sctx; |
| 1179 | |
| 1180 | /* number of total found references */ |
| 1181 | u64 found; |
| 1182 | |
| 1183 | /* |
| 1184 | * used for clones found in send_root. clones found behind cur_objectid |
| 1185 | * and cur_offset are not considered as allowed clones. |
| 1186 | */ |
| 1187 | u64 cur_objectid; |
| 1188 | u64 cur_offset; |
| 1189 | |
| 1190 | /* may be truncated in case it's the last extent in a file */ |
| 1191 | u64 extent_len; |
| 1192 | |
Filipe Manana | 619d8c4 | 2015-05-03 01:56:00 +0100 | [diff] [blame] | 1193 | /* data offset in the file extent item */ |
| 1194 | u64 data_offset; |
| 1195 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1196 | /* Just to check for bugs in backref resolving */ |
Alexander Block | ee849c0 | 2012-07-28 12:42:05 +0200 | [diff] [blame] | 1197 | int found_itself; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1198 | }; |
| 1199 | |
| 1200 | static int __clone_root_cmp_bsearch(const void *key, const void *elt) |
| 1201 | { |
Jan Schmidt | 995e01b | 2012-08-13 02:52:38 -0600 | [diff] [blame] | 1202 | u64 root = (u64)(uintptr_t)key; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1203 | struct clone_root *cr = (struct clone_root *)elt; |
| 1204 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 1205 | if (root < cr->root->root_key.objectid) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1206 | return -1; |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 1207 | if (root > cr->root->root_key.objectid) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1208 | return 1; |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | static int __clone_root_cmp_sort(const void *e1, const void *e2) |
| 1213 | { |
| 1214 | struct clone_root *cr1 = (struct clone_root *)e1; |
| 1215 | struct clone_root *cr2 = (struct clone_root *)e2; |
| 1216 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 1217 | if (cr1->root->root_key.objectid < cr2->root->root_key.objectid) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1218 | return -1; |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 1219 | if (cr1->root->root_key.objectid > cr2->root->root_key.objectid) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1220 | return 1; |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * Called for every backref that is found for the current extent. |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1226 | * Results are collected in sctx->clone_roots->ino/offset/found_refs |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1227 | */ |
| 1228 | static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_) |
| 1229 | { |
| 1230 | struct backref_ctx *bctx = ctx_; |
| 1231 | struct clone_root *found; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1232 | |
| 1233 | /* First check if the root is in the list of accepted clone sources */ |
Jan Schmidt | 995e01b | 2012-08-13 02:52:38 -0600 | [diff] [blame] | 1234 | found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1235 | bctx->sctx->clone_roots_cnt, |
| 1236 | sizeof(struct clone_root), |
| 1237 | __clone_root_cmp_bsearch); |
| 1238 | if (!found) |
| 1239 | return 0; |
| 1240 | |
| 1241 | if (found->root == bctx->sctx->send_root && |
| 1242 | ino == bctx->cur_objectid && |
| 1243 | offset == bctx->cur_offset) { |
Alexander Block | ee849c0 | 2012-07-28 12:42:05 +0200 | [diff] [blame] | 1244 | bctx->found_itself = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1248 | * Make sure we don't consider clones from send_root that are |
| 1249 | * behind the current inode/offset. |
| 1250 | */ |
| 1251 | if (found->root == bctx->sctx->send_root) { |
| 1252 | /* |
Filipe Manana | 11f2069 | 2019-10-30 12:23:11 +0000 | [diff] [blame] | 1253 | * If the source inode was not yet processed we can't issue a |
| 1254 | * clone operation, as the source extent does not exist yet at |
| 1255 | * the destination of the stream. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1256 | */ |
Filipe Manana | 11f2069 | 2019-10-30 12:23:11 +0000 | [diff] [blame] | 1257 | if (ino > bctx->cur_objectid) |
| 1258 | return 0; |
| 1259 | /* |
| 1260 | * We clone from the inode currently being sent as long as the |
| 1261 | * source extent is already processed, otherwise we could try |
| 1262 | * to clone from an extent that does not exist yet at the |
| 1263 | * destination of the stream. |
| 1264 | */ |
| 1265 | if (ino == bctx->cur_objectid && |
Filipe Manana | 9722b10 | 2020-01-29 17:09:53 +0000 | [diff] [blame] | 1266 | offset + bctx->extent_len > |
| 1267 | bctx->sctx->cur_inode_next_write_offset) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1268 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | bctx->found++; |
| 1272 | found->found_refs++; |
| 1273 | if (ino < found->ino) { |
| 1274 | found->ino = ino; |
| 1275 | found->offset = offset; |
| 1276 | } else if (found->ino == ino) { |
| 1277 | /* |
| 1278 | * same extent found more then once in the same file. |
| 1279 | */ |
| 1280 | if (found->offset > offset + bctx->extent_len) |
| 1281 | found->offset = offset; |
| 1282 | } |
| 1283 | |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1288 | * Given an inode, offset and extent item, it finds a good clone for a clone |
| 1289 | * instruction. Returns -ENOENT when none could be found. The function makes |
| 1290 | * sure that the returned clone is usable at the point where sending is at the |
| 1291 | * moment. This means, that no clones are accepted which lie behind the current |
| 1292 | * inode+offset. |
| 1293 | * |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1294 | * path must point to the extent item when called. |
| 1295 | */ |
| 1296 | static int find_extent_clone(struct send_ctx *sctx, |
| 1297 | struct btrfs_path *path, |
| 1298 | u64 ino, u64 data_offset, |
| 1299 | u64 ino_size, |
| 1300 | struct clone_root **found) |
| 1301 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1302 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1303 | int ret; |
| 1304 | int extent_type; |
| 1305 | u64 logical; |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1306 | u64 disk_byte; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1307 | u64 num_bytes; |
| 1308 | u64 extent_item_pos; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1309 | u64 flags = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1310 | struct btrfs_file_extent_item *fi; |
| 1311 | struct extent_buffer *eb = path->nodes[0]; |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1312 | struct backref_ctx *backref_ctx = NULL; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1313 | struct clone_root *cur_clone_root; |
| 1314 | struct btrfs_key found_key; |
| 1315 | struct btrfs_path *tmp_path; |
Filipe Manana | fd0ddbe | 2019-10-30 12:23:01 +0000 | [diff] [blame] | 1316 | struct btrfs_extent_item *ei; |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1317 | int compressed; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1318 | u32 i; |
| 1319 | |
| 1320 | tmp_path = alloc_path_for_send(); |
| 1321 | if (!tmp_path) |
| 1322 | return -ENOMEM; |
| 1323 | |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 1324 | /* We only use this path under the commit sem */ |
| 1325 | tmp_path->need_commit_sem = 0; |
| 1326 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 1327 | backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL); |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1328 | if (!backref_ctx) { |
| 1329 | ret = -ENOMEM; |
| 1330 | goto out; |
| 1331 | } |
| 1332 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1333 | if (data_offset >= ino_size) { |
| 1334 | /* |
| 1335 | * There may be extents that lie behind the file's size. |
| 1336 | * I at least had this in combination with snapshotting while |
| 1337 | * writing large files. |
| 1338 | */ |
| 1339 | ret = 0; |
| 1340 | goto out; |
| 1341 | } |
| 1342 | |
| 1343 | fi = btrfs_item_ptr(eb, path->slots[0], |
| 1344 | struct btrfs_file_extent_item); |
| 1345 | extent_type = btrfs_file_extent_type(eb, fi); |
| 1346 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1347 | ret = -ENOENT; |
| 1348 | goto out; |
| 1349 | } |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1350 | compressed = btrfs_file_extent_compression(eb, fi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1351 | |
| 1352 | num_bytes = btrfs_file_extent_num_bytes(eb, fi); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1353 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
| 1354 | if (disk_byte == 0) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1355 | ret = -ENOENT; |
| 1356 | goto out; |
| 1357 | } |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1358 | logical = disk_byte + btrfs_file_extent_offset(eb, fi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1359 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1360 | down_read(&fs_info->commit_root_sem); |
| 1361 | ret = extent_from_logical(fs_info, disk_byte, tmp_path, |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1362 | &found_key, &flags); |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1363 | up_read(&fs_info->commit_root_sem); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1364 | |
| 1365 | if (ret < 0) |
| 1366 | goto out; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1367 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1368 | ret = -EIO; |
| 1369 | goto out; |
| 1370 | } |
| 1371 | |
Filipe Manana | fd0ddbe | 2019-10-30 12:23:01 +0000 | [diff] [blame] | 1372 | ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0], |
| 1373 | struct btrfs_extent_item); |
| 1374 | /* |
| 1375 | * Backreference walking (iterate_extent_inodes() below) is currently |
| 1376 | * too expensive when an extent has a large number of references, both |
| 1377 | * in time spent and used memory. So for now just fallback to write |
| 1378 | * operations instead of clone operations when an extent has more than |
| 1379 | * a certain amount of references. |
| 1380 | */ |
| 1381 | if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) { |
| 1382 | ret = -ENOENT; |
| 1383 | goto out; |
| 1384 | } |
| 1385 | btrfs_release_path(tmp_path); |
| 1386 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1387 | /* |
| 1388 | * Setup the clone roots. |
| 1389 | */ |
| 1390 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 1391 | cur_clone_root = sctx->clone_roots + i; |
| 1392 | cur_clone_root->ino = (u64)-1; |
| 1393 | cur_clone_root->offset = 0; |
| 1394 | cur_clone_root->found_refs = 0; |
| 1395 | } |
| 1396 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1397 | backref_ctx->sctx = sctx; |
| 1398 | backref_ctx->found = 0; |
| 1399 | backref_ctx->cur_objectid = ino; |
| 1400 | backref_ctx->cur_offset = data_offset; |
| 1401 | backref_ctx->found_itself = 0; |
| 1402 | backref_ctx->extent_len = num_bytes; |
Filipe Manana | 619d8c4 | 2015-05-03 01:56:00 +0100 | [diff] [blame] | 1403 | /* |
| 1404 | * For non-compressed extents iterate_extent_inodes() gives us extent |
| 1405 | * offsets that already take into account the data offset, but not for |
| 1406 | * compressed extents, since the offset is logical and not relative to |
| 1407 | * the physical extent locations. We must take this into account to |
| 1408 | * avoid sending clone offsets that go beyond the source file's size, |
| 1409 | * which would result in the clone ioctl failing with -EINVAL on the |
| 1410 | * receiving end. |
| 1411 | */ |
| 1412 | if (compressed == BTRFS_COMPRESS_NONE) |
| 1413 | backref_ctx->data_offset = 0; |
| 1414 | else |
| 1415 | backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1416 | |
| 1417 | /* |
| 1418 | * The last extent of a file may be too large due to page alignment. |
| 1419 | * We need to adjust extent_len in this case so that the checks in |
| 1420 | * __iterate_backrefs work. |
| 1421 | */ |
| 1422 | if (data_offset + num_bytes >= ino_size) |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1423 | backref_ctx->extent_len = ino_size - data_offset; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1424 | |
| 1425 | /* |
| 1426 | * Now collect all backrefs. |
| 1427 | */ |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1428 | if (compressed == BTRFS_COMPRESS_NONE) |
| 1429 | extent_item_pos = logical - found_key.objectid; |
| 1430 | else |
| 1431 | extent_item_pos = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1432 | ret = iterate_extent_inodes(fs_info, found_key.objectid, |
| 1433 | extent_item_pos, 1, __iterate_backrefs, |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 1434 | backref_ctx, false); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1435 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1436 | if (ret < 0) |
| 1437 | goto out; |
| 1438 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1439 | if (!backref_ctx->found_itself) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1440 | /* found a bug in backref code? */ |
| 1441 | ret = -EIO; |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1442 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 1443 | "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu", |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1444 | ino, data_offset, disk_byte, found_key.objectid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1445 | goto out; |
| 1446 | } |
| 1447 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1448 | btrfs_debug(fs_info, |
| 1449 | "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu", |
| 1450 | data_offset, ino, num_bytes, logical); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1451 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1452 | if (!backref_ctx->found) |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1453 | btrfs_debug(fs_info, "no clones found"); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1454 | |
| 1455 | cur_clone_root = NULL; |
| 1456 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 1457 | if (sctx->clone_roots[i].found_refs) { |
| 1458 | if (!cur_clone_root) |
| 1459 | cur_clone_root = sctx->clone_roots + i; |
| 1460 | else if (sctx->clone_roots[i].root == sctx->send_root) |
| 1461 | /* prefer clones from send_root over others */ |
| 1462 | cur_clone_root = sctx->clone_roots + i; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | } |
| 1466 | |
| 1467 | if (cur_clone_root) { |
| 1468 | *found = cur_clone_root; |
| 1469 | ret = 0; |
| 1470 | } else { |
| 1471 | ret = -ENOENT; |
| 1472 | } |
| 1473 | |
| 1474 | out: |
| 1475 | btrfs_free_path(tmp_path); |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1476 | kfree(backref_ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1477 | return ret; |
| 1478 | } |
| 1479 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1480 | static int read_symlink(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1481 | u64 ino, |
| 1482 | struct fs_path *dest) |
| 1483 | { |
| 1484 | int ret; |
| 1485 | struct btrfs_path *path; |
| 1486 | struct btrfs_key key; |
| 1487 | struct btrfs_file_extent_item *ei; |
| 1488 | u8 type; |
| 1489 | u8 compression; |
| 1490 | unsigned long off; |
| 1491 | int len; |
| 1492 | |
| 1493 | path = alloc_path_for_send(); |
| 1494 | if (!path) |
| 1495 | return -ENOMEM; |
| 1496 | |
| 1497 | key.objectid = ino; |
| 1498 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1499 | key.offset = 0; |
| 1500 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1501 | if (ret < 0) |
| 1502 | goto out; |
Filipe Manana | a879719 | 2015-12-31 18:07:59 +0000 | [diff] [blame] | 1503 | if (ret) { |
| 1504 | /* |
| 1505 | * An empty symlink inode. Can happen in rare error paths when |
| 1506 | * creating a symlink (transaction committed before the inode |
| 1507 | * eviction handler removed the symlink inode items and a crash |
| 1508 | * happened in between or the subvol was snapshoted in between). |
| 1509 | * Print an informative message to dmesg/syslog so that the user |
| 1510 | * can delete the symlink. |
| 1511 | */ |
| 1512 | btrfs_err(root->fs_info, |
| 1513 | "Found empty symlink inode %llu at root %llu", |
| 1514 | ino, root->root_key.objectid); |
| 1515 | ret = -EIO; |
| 1516 | goto out; |
| 1517 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1518 | |
| 1519 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1520 | struct btrfs_file_extent_item); |
| 1521 | type = btrfs_file_extent_type(path->nodes[0], ei); |
| 1522 | compression = btrfs_file_extent_compression(path->nodes[0], ei); |
| 1523 | BUG_ON(type != BTRFS_FILE_EXTENT_INLINE); |
| 1524 | BUG_ON(compression); |
| 1525 | |
| 1526 | off = btrfs_file_extent_inline_start(ei); |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 1527 | len = btrfs_file_extent_ram_bytes(path->nodes[0], ei); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1528 | |
| 1529 | ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1530 | |
| 1531 | out: |
| 1532 | btrfs_free_path(path); |
| 1533 | return ret; |
| 1534 | } |
| 1535 | |
| 1536 | /* |
| 1537 | * Helper function to generate a file name that is unique in the root of |
| 1538 | * send_root and parent_root. This is used to generate names for orphan inodes. |
| 1539 | */ |
| 1540 | static int gen_unique_name(struct send_ctx *sctx, |
| 1541 | u64 ino, u64 gen, |
| 1542 | struct fs_path *dest) |
| 1543 | { |
| 1544 | int ret = 0; |
| 1545 | struct btrfs_path *path; |
| 1546 | struct btrfs_dir_item *di; |
| 1547 | char tmp[64]; |
| 1548 | int len; |
| 1549 | u64 idx = 0; |
| 1550 | |
| 1551 | path = alloc_path_for_send(); |
| 1552 | if (!path) |
| 1553 | return -ENOMEM; |
| 1554 | |
| 1555 | while (1) { |
Filipe David Borba Manana | f74b86d | 2014-01-21 23:36:38 +0000 | [diff] [blame] | 1556 | len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu", |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1557 | ino, gen, idx); |
David Sterba | 64792f2 | 2014-02-03 18:24:09 +0100 | [diff] [blame] | 1558 | ASSERT(len < sizeof(tmp)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1559 | |
| 1560 | di = btrfs_lookup_dir_item(NULL, sctx->send_root, |
| 1561 | path, BTRFS_FIRST_FREE_OBJECTID, |
| 1562 | tmp, strlen(tmp), 0); |
| 1563 | btrfs_release_path(path); |
| 1564 | if (IS_ERR(di)) { |
| 1565 | ret = PTR_ERR(di); |
| 1566 | goto out; |
| 1567 | } |
| 1568 | if (di) { |
| 1569 | /* not unique, try again */ |
| 1570 | idx++; |
| 1571 | continue; |
| 1572 | } |
| 1573 | |
| 1574 | if (!sctx->parent_root) { |
| 1575 | /* unique */ |
| 1576 | ret = 0; |
| 1577 | break; |
| 1578 | } |
| 1579 | |
| 1580 | di = btrfs_lookup_dir_item(NULL, sctx->parent_root, |
| 1581 | path, BTRFS_FIRST_FREE_OBJECTID, |
| 1582 | tmp, strlen(tmp), 0); |
| 1583 | btrfs_release_path(path); |
| 1584 | if (IS_ERR(di)) { |
| 1585 | ret = PTR_ERR(di); |
| 1586 | goto out; |
| 1587 | } |
| 1588 | if (di) { |
| 1589 | /* not unique, try again */ |
| 1590 | idx++; |
| 1591 | continue; |
| 1592 | } |
| 1593 | /* unique */ |
| 1594 | break; |
| 1595 | } |
| 1596 | |
| 1597 | ret = fs_path_add(dest, tmp, strlen(tmp)); |
| 1598 | |
| 1599 | out: |
| 1600 | btrfs_free_path(path); |
| 1601 | return ret; |
| 1602 | } |
| 1603 | |
| 1604 | enum inode_state { |
| 1605 | inode_state_no_change, |
| 1606 | inode_state_will_create, |
| 1607 | inode_state_did_create, |
| 1608 | inode_state_will_delete, |
| 1609 | inode_state_did_delete, |
| 1610 | }; |
| 1611 | |
| 1612 | static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1613 | { |
| 1614 | int ret; |
| 1615 | int left_ret; |
| 1616 | int right_ret; |
| 1617 | u64 left_gen; |
| 1618 | u64 right_gen; |
| 1619 | |
| 1620 | ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1621 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1622 | if (ret < 0 && ret != -ENOENT) |
| 1623 | goto out; |
| 1624 | left_ret = ret; |
| 1625 | |
| 1626 | if (!sctx->parent_root) { |
| 1627 | right_ret = -ENOENT; |
| 1628 | } else { |
| 1629 | ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1630 | NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1631 | if (ret < 0 && ret != -ENOENT) |
| 1632 | goto out; |
| 1633 | right_ret = ret; |
| 1634 | } |
| 1635 | |
| 1636 | if (!left_ret && !right_ret) { |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1637 | if (left_gen == gen && right_gen == gen) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1638 | ret = inode_state_no_change; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1639 | } else if (left_gen == gen) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1640 | if (ino < sctx->send_progress) |
| 1641 | ret = inode_state_did_create; |
| 1642 | else |
| 1643 | ret = inode_state_will_create; |
| 1644 | } else if (right_gen == gen) { |
| 1645 | if (ino < sctx->send_progress) |
| 1646 | ret = inode_state_did_delete; |
| 1647 | else |
| 1648 | ret = inode_state_will_delete; |
| 1649 | } else { |
| 1650 | ret = -ENOENT; |
| 1651 | } |
| 1652 | } else if (!left_ret) { |
| 1653 | if (left_gen == gen) { |
| 1654 | if (ino < sctx->send_progress) |
| 1655 | ret = inode_state_did_create; |
| 1656 | else |
| 1657 | ret = inode_state_will_create; |
| 1658 | } else { |
| 1659 | ret = -ENOENT; |
| 1660 | } |
| 1661 | } else if (!right_ret) { |
| 1662 | if (right_gen == gen) { |
| 1663 | if (ino < sctx->send_progress) |
| 1664 | ret = inode_state_did_delete; |
| 1665 | else |
| 1666 | ret = inode_state_will_delete; |
| 1667 | } else { |
| 1668 | ret = -ENOENT; |
| 1669 | } |
| 1670 | } else { |
| 1671 | ret = -ENOENT; |
| 1672 | } |
| 1673 | |
| 1674 | out: |
| 1675 | return ret; |
| 1676 | } |
| 1677 | |
| 1678 | static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1679 | { |
| 1680 | int ret; |
| 1681 | |
Robbie Ko | 4dd9920 | 2017-01-05 16:24:55 +0800 | [diff] [blame] | 1682 | if (ino == BTRFS_FIRST_FREE_OBJECTID) |
| 1683 | return 1; |
| 1684 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1685 | ret = get_cur_inode_state(sctx, ino, gen); |
| 1686 | if (ret < 0) |
| 1687 | goto out; |
| 1688 | |
| 1689 | if (ret == inode_state_no_change || |
| 1690 | ret == inode_state_did_create || |
| 1691 | ret == inode_state_will_delete) |
| 1692 | ret = 1; |
| 1693 | else |
| 1694 | ret = 0; |
| 1695 | |
| 1696 | out: |
| 1697 | return ret; |
| 1698 | } |
| 1699 | |
| 1700 | /* |
| 1701 | * Helper function to lookup a dir item in a dir. |
| 1702 | */ |
| 1703 | static int lookup_dir_item_inode(struct btrfs_root *root, |
| 1704 | u64 dir, const char *name, int name_len, |
| 1705 | u64 *found_inode, |
| 1706 | u8 *found_type) |
| 1707 | { |
| 1708 | int ret = 0; |
| 1709 | struct btrfs_dir_item *di; |
| 1710 | struct btrfs_key key; |
| 1711 | struct btrfs_path *path; |
| 1712 | |
| 1713 | path = alloc_path_for_send(); |
| 1714 | if (!path) |
| 1715 | return -ENOMEM; |
| 1716 | |
| 1717 | di = btrfs_lookup_dir_item(NULL, root, path, |
| 1718 | dir, name, name_len, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 1719 | if (IS_ERR_OR_NULL(di)) { |
| 1720 | ret = di ? PTR_ERR(di) : -ENOENT; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1721 | goto out; |
| 1722 | } |
| 1723 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
Filipe Manana | 1af5607 | 2014-05-25 04:49:24 +0100 | [diff] [blame] | 1724 | if (key.type == BTRFS_ROOT_ITEM_KEY) { |
| 1725 | ret = -ENOENT; |
| 1726 | goto out; |
| 1727 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1728 | *found_inode = key.objectid; |
| 1729 | *found_type = btrfs_dir_type(path->nodes[0], di); |
| 1730 | |
| 1731 | out: |
| 1732 | btrfs_free_path(path); |
| 1733 | return ret; |
| 1734 | } |
| 1735 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1736 | /* |
| 1737 | * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir, |
| 1738 | * generation of the parent dir and the name of the dir entry. |
| 1739 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1740 | static int get_first_ref(struct btrfs_root *root, u64 ino, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1741 | u64 *dir, u64 *dir_gen, struct fs_path *name) |
| 1742 | { |
| 1743 | int ret; |
| 1744 | struct btrfs_key key; |
| 1745 | struct btrfs_key found_key; |
| 1746 | struct btrfs_path *path; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1747 | int len; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1748 | u64 parent_dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1749 | |
| 1750 | path = alloc_path_for_send(); |
| 1751 | if (!path) |
| 1752 | return -ENOMEM; |
| 1753 | |
| 1754 | key.objectid = ino; |
| 1755 | key.type = BTRFS_INODE_REF_KEY; |
| 1756 | key.offset = 0; |
| 1757 | |
| 1758 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 1759 | if (ret < 0) |
| 1760 | goto out; |
| 1761 | if (!ret) |
| 1762 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1763 | path->slots[0]); |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1764 | if (ret || found_key.objectid != ino || |
| 1765 | (found_key.type != BTRFS_INODE_REF_KEY && |
| 1766 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1767 | ret = -ENOENT; |
| 1768 | goto out; |
| 1769 | } |
| 1770 | |
Filipe Manana | 51a6025 | 2014-05-13 22:01:02 +0100 | [diff] [blame] | 1771 | if (found_key.type == BTRFS_INODE_REF_KEY) { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1772 | struct btrfs_inode_ref *iref; |
| 1773 | iref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1774 | struct btrfs_inode_ref); |
| 1775 | len = btrfs_inode_ref_name_len(path->nodes[0], iref); |
| 1776 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], |
| 1777 | (unsigned long)(iref + 1), |
| 1778 | len); |
| 1779 | parent_dir = found_key.offset; |
| 1780 | } else { |
| 1781 | struct btrfs_inode_extref *extref; |
| 1782 | extref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1783 | struct btrfs_inode_extref); |
| 1784 | len = btrfs_inode_extref_name_len(path->nodes[0], extref); |
| 1785 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], |
| 1786 | (unsigned long)&extref->name, len); |
| 1787 | parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref); |
| 1788 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1789 | if (ret < 0) |
| 1790 | goto out; |
| 1791 | btrfs_release_path(path); |
| 1792 | |
Filipe Manana | b46ab97 | 2014-03-21 12:46:54 +0000 | [diff] [blame] | 1793 | if (dir_gen) { |
| 1794 | ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, |
| 1795 | NULL, NULL, NULL); |
| 1796 | if (ret < 0) |
| 1797 | goto out; |
| 1798 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1799 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1800 | *dir = parent_dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1801 | |
| 1802 | out: |
| 1803 | btrfs_free_path(path); |
| 1804 | return ret; |
| 1805 | } |
| 1806 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1807 | static int is_first_ref(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1808 | u64 ino, u64 dir, |
| 1809 | const char *name, int name_len) |
| 1810 | { |
| 1811 | int ret; |
| 1812 | struct fs_path *tmp_name; |
| 1813 | u64 tmp_dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1814 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1815 | tmp_name = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1816 | if (!tmp_name) |
| 1817 | return -ENOMEM; |
| 1818 | |
Filipe Manana | b46ab97 | 2014-03-21 12:46:54 +0000 | [diff] [blame] | 1819 | ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1820 | if (ret < 0) |
| 1821 | goto out; |
| 1822 | |
Alexander Block | b9291af | 2012-07-28 11:07:18 +0200 | [diff] [blame] | 1823 | if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1824 | ret = 0; |
| 1825 | goto out; |
| 1826 | } |
| 1827 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1828 | ret = !memcmp(tmp_name->start, name, name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1829 | |
| 1830 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1831 | fs_path_free(tmp_name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1832 | return ret; |
| 1833 | } |
| 1834 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1835 | /* |
| 1836 | * Used by process_recorded_refs to determine if a new ref would overwrite an |
| 1837 | * already existing ref. In case it detects an overwrite, it returns the |
| 1838 | * inode/gen in who_ino/who_gen. |
| 1839 | * When an overwrite is detected, process_recorded_refs does proper orphanizing |
| 1840 | * to make sure later references to the overwritten inode are possible. |
| 1841 | * Orphanizing is however only required for the first ref of an inode. |
| 1842 | * process_recorded_refs does an additional is_first_ref check to see if |
| 1843 | * orphanizing is really required. |
| 1844 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1845 | static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
| 1846 | const char *name, int name_len, |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 1847 | u64 *who_ino, u64 *who_gen, u64 *who_mode) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1848 | { |
| 1849 | int ret = 0; |
Josef Bacik | ebdad91 | 2013-08-06 16:47:48 -0400 | [diff] [blame] | 1850 | u64 gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1851 | u64 other_inode = 0; |
| 1852 | u8 other_type = 0; |
| 1853 | |
| 1854 | if (!sctx->parent_root) |
| 1855 | goto out; |
| 1856 | |
| 1857 | ret = is_inode_existent(sctx, dir, dir_gen); |
| 1858 | if (ret <= 0) |
| 1859 | goto out; |
| 1860 | |
Josef Bacik | ebdad91 | 2013-08-06 16:47:48 -0400 | [diff] [blame] | 1861 | /* |
| 1862 | * If we have a parent root we need to verify that the parent dir was |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1863 | * not deleted and then re-created, if it was then we have no overwrite |
Josef Bacik | ebdad91 | 2013-08-06 16:47:48 -0400 | [diff] [blame] | 1864 | * and we can just unlink this entry. |
| 1865 | */ |
Robbie Ko | 4dd9920 | 2017-01-05 16:24:55 +0800 | [diff] [blame] | 1866 | if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | ebdad91 | 2013-08-06 16:47:48 -0400 | [diff] [blame] | 1867 | ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, |
| 1868 | NULL, NULL, NULL); |
| 1869 | if (ret < 0 && ret != -ENOENT) |
| 1870 | goto out; |
| 1871 | if (ret) { |
| 1872 | ret = 0; |
| 1873 | goto out; |
| 1874 | } |
| 1875 | if (gen != dir_gen) |
| 1876 | goto out; |
| 1877 | } |
| 1878 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1879 | ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, |
| 1880 | &other_inode, &other_type); |
| 1881 | if (ret < 0 && ret != -ENOENT) |
| 1882 | goto out; |
| 1883 | if (ret) { |
| 1884 | ret = 0; |
| 1885 | goto out; |
| 1886 | } |
| 1887 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1888 | /* |
| 1889 | * Check if the overwritten ref was already processed. If yes, the ref |
| 1890 | * was already unlinked/moved, so we can safely assume that we will not |
| 1891 | * overwrite anything at this point in time. |
| 1892 | */ |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 1893 | if (other_inode > sctx->send_progress || |
| 1894 | is_waiting_for_move(sctx, other_inode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1895 | ret = get_inode_info(sctx->parent_root, other_inode, NULL, |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 1896 | who_gen, who_mode, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1897 | if (ret < 0) |
| 1898 | goto out; |
| 1899 | |
| 1900 | ret = 1; |
| 1901 | *who_ino = other_inode; |
| 1902 | } else { |
| 1903 | ret = 0; |
| 1904 | } |
| 1905 | |
| 1906 | out: |
| 1907 | return ret; |
| 1908 | } |
| 1909 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1910 | /* |
| 1911 | * Checks if the ref was overwritten by an already processed inode. This is |
| 1912 | * used by __get_cur_name_and_parent to find out if the ref was orphanized and |
| 1913 | * thus the orphan name needs be used. |
| 1914 | * process_recorded_refs also uses it to avoid unlinking of refs that were |
| 1915 | * overwritten. |
| 1916 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1917 | static int did_overwrite_ref(struct send_ctx *sctx, |
| 1918 | u64 dir, u64 dir_gen, |
| 1919 | u64 ino, u64 ino_gen, |
| 1920 | const char *name, int name_len) |
| 1921 | { |
| 1922 | int ret = 0; |
| 1923 | u64 gen; |
| 1924 | u64 ow_inode; |
| 1925 | u8 other_type; |
| 1926 | |
| 1927 | if (!sctx->parent_root) |
| 1928 | goto out; |
| 1929 | |
| 1930 | ret = is_inode_existent(sctx, dir, dir_gen); |
| 1931 | if (ret <= 0) |
| 1932 | goto out; |
| 1933 | |
Robbie Ko | 0191410 | 2017-01-05 16:24:58 +0800 | [diff] [blame] | 1934 | if (dir != BTRFS_FIRST_FREE_OBJECTID) { |
| 1935 | ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, |
| 1936 | NULL, NULL, NULL); |
| 1937 | if (ret < 0 && ret != -ENOENT) |
| 1938 | goto out; |
| 1939 | if (ret) { |
| 1940 | ret = 0; |
| 1941 | goto out; |
| 1942 | } |
| 1943 | if (gen != dir_gen) |
| 1944 | goto out; |
| 1945 | } |
| 1946 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1947 | /* check if the ref was overwritten by another ref */ |
| 1948 | ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, |
| 1949 | &ow_inode, &other_type); |
| 1950 | if (ret < 0 && ret != -ENOENT) |
| 1951 | goto out; |
| 1952 | if (ret) { |
| 1953 | /* was never and will never be overwritten */ |
| 1954 | ret = 0; |
| 1955 | goto out; |
| 1956 | } |
| 1957 | |
| 1958 | ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1959 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1960 | if (ret < 0) |
| 1961 | goto out; |
| 1962 | |
| 1963 | if (ow_inode == ino && gen == ino_gen) { |
| 1964 | ret = 0; |
| 1965 | goto out; |
| 1966 | } |
| 1967 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 1968 | /* |
| 1969 | * We know that it is or will be overwritten. Check this now. |
| 1970 | * The current inode being processed might have been the one that caused |
Filipe Manana | b786f16 | 2015-09-26 15:30:23 +0100 | [diff] [blame] | 1971 | * inode 'ino' to be orphanized, therefore check if ow_inode matches |
| 1972 | * the current inode being processed. |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 1973 | */ |
Filipe Manana | b786f16 | 2015-09-26 15:30:23 +0100 | [diff] [blame] | 1974 | if ((ow_inode < sctx->send_progress) || |
| 1975 | (ino != sctx->cur_ino && ow_inode == sctx->cur_ino && |
| 1976 | gen == sctx->cur_inode_gen)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1977 | ret = 1; |
| 1978 | else |
| 1979 | ret = 0; |
| 1980 | |
| 1981 | out: |
| 1982 | return ret; |
| 1983 | } |
| 1984 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1985 | /* |
| 1986 | * Same as did_overwrite_ref, but also checks if it is the first ref of an inode |
| 1987 | * that got overwritten. This is used by process_recorded_refs to determine |
| 1988 | * if it has to use the path as returned by get_cur_path or the orphan name. |
| 1989 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1990 | static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1991 | { |
| 1992 | int ret = 0; |
| 1993 | struct fs_path *name = NULL; |
| 1994 | u64 dir; |
| 1995 | u64 dir_gen; |
| 1996 | |
| 1997 | if (!sctx->parent_root) |
| 1998 | goto out; |
| 1999 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2000 | name = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2001 | if (!name) |
| 2002 | return -ENOMEM; |
| 2003 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2004 | ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2005 | if (ret < 0) |
| 2006 | goto out; |
| 2007 | |
| 2008 | ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, |
| 2009 | name->start, fs_path_len(name)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2010 | |
| 2011 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2012 | fs_path_free(name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2013 | return ret; |
| 2014 | } |
| 2015 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2016 | /* |
| 2017 | * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit, |
| 2018 | * so we need to do some special handling in case we have clashes. This function |
| 2019 | * takes care of this with the help of name_cache_entry::radix_list. |
Alexander Block | 5dc67d0 | 2012-08-01 12:07:43 +0200 | [diff] [blame] | 2020 | * In case of error, nce is kfreed. |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2021 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2022 | static int name_cache_insert(struct send_ctx *sctx, |
| 2023 | struct name_cache_entry *nce) |
| 2024 | { |
| 2025 | int ret = 0; |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2026 | struct list_head *nce_head; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2027 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2028 | nce_head = radix_tree_lookup(&sctx->name_cache, |
| 2029 | (unsigned long)nce->ino); |
| 2030 | if (!nce_head) { |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2031 | nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL); |
Tsutomu Itoh | cfa7a9c | 2012-12-17 06:38:51 +0000 | [diff] [blame] | 2032 | if (!nce_head) { |
| 2033 | kfree(nce); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2034 | return -ENOMEM; |
Tsutomu Itoh | cfa7a9c | 2012-12-17 06:38:51 +0000 | [diff] [blame] | 2035 | } |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2036 | INIT_LIST_HEAD(nce_head); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2037 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2038 | ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); |
Alexander Block | 5dc67d0 | 2012-08-01 12:07:43 +0200 | [diff] [blame] | 2039 | if (ret < 0) { |
| 2040 | kfree(nce_head); |
| 2041 | kfree(nce); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2042 | return ret; |
Alexander Block | 5dc67d0 | 2012-08-01 12:07:43 +0200 | [diff] [blame] | 2043 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2044 | } |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2045 | list_add_tail(&nce->radix_list, nce_head); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2046 | list_add_tail(&nce->list, &sctx->name_cache_list); |
| 2047 | sctx->name_cache_size++; |
| 2048 | |
| 2049 | return ret; |
| 2050 | } |
| 2051 | |
| 2052 | static void name_cache_delete(struct send_ctx *sctx, |
| 2053 | struct name_cache_entry *nce) |
| 2054 | { |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2055 | struct list_head *nce_head; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2056 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2057 | nce_head = radix_tree_lookup(&sctx->name_cache, |
| 2058 | (unsigned long)nce->ino); |
David Sterba | 57fb891 | 2014-02-03 19:24:40 +0100 | [diff] [blame] | 2059 | if (!nce_head) { |
| 2060 | btrfs_err(sctx->send_root->fs_info, |
| 2061 | "name_cache_delete lookup failed ino %llu cache size %d, leaking memory", |
| 2062 | nce->ino, sctx->name_cache_size); |
| 2063 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2064 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2065 | list_del(&nce->radix_list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2066 | list_del(&nce->list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2067 | sctx->name_cache_size--; |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2068 | |
David Sterba | 57fb891 | 2014-02-03 19:24:40 +0100 | [diff] [blame] | 2069 | /* |
| 2070 | * We may not get to the final release of nce_head if the lookup fails |
| 2071 | */ |
| 2072 | if (nce_head && list_empty(nce_head)) { |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2073 | radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); |
| 2074 | kfree(nce_head); |
| 2075 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, |
| 2079 | u64 ino, u64 gen) |
| 2080 | { |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2081 | struct list_head *nce_head; |
| 2082 | struct name_cache_entry *cur; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2083 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2084 | nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); |
| 2085 | if (!nce_head) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2086 | return NULL; |
| 2087 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2088 | list_for_each_entry(cur, nce_head, radix_list) { |
| 2089 | if (cur->ino == ino && cur->gen == gen) |
| 2090 | return cur; |
| 2091 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2092 | return NULL; |
| 2093 | } |
| 2094 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2095 | /* |
| 2096 | * Removes the entry from the list and adds it back to the end. This marks the |
| 2097 | * entry as recently used so that name_cache_clean_unused does not remove it. |
| 2098 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2099 | static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce) |
| 2100 | { |
| 2101 | list_del(&nce->list); |
| 2102 | list_add_tail(&nce->list, &sctx->name_cache_list); |
| 2103 | } |
| 2104 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2105 | /* |
| 2106 | * Remove some entries from the beginning of name_cache_list. |
| 2107 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2108 | static void name_cache_clean_unused(struct send_ctx *sctx) |
| 2109 | { |
| 2110 | struct name_cache_entry *nce; |
| 2111 | |
| 2112 | if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) |
| 2113 | return; |
| 2114 | |
| 2115 | while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { |
| 2116 | nce = list_entry(sctx->name_cache_list.next, |
| 2117 | struct name_cache_entry, list); |
| 2118 | name_cache_delete(sctx, nce); |
| 2119 | kfree(nce); |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | static void name_cache_free(struct send_ctx *sctx) |
| 2124 | { |
| 2125 | struct name_cache_entry *nce; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2126 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2127 | while (!list_empty(&sctx->name_cache_list)) { |
| 2128 | nce = list_entry(sctx->name_cache_list.next, |
| 2129 | struct name_cache_entry, list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2130 | name_cache_delete(sctx, nce); |
Alexander Block | 17589bd | 2012-07-28 14:13:35 +0200 | [diff] [blame] | 2131 | kfree(nce); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2132 | } |
| 2133 | } |
| 2134 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2135 | /* |
| 2136 | * Used by get_cur_path for each ref up to the root. |
| 2137 | * Returns 0 if it succeeded. |
| 2138 | * Returns 1 if the inode is not existent or got overwritten. In that case, the |
| 2139 | * name is an orphan name. This instructs get_cur_path to stop iterating. If 1 |
| 2140 | * is returned, parent_ino/parent_gen are not guaranteed to be valid. |
| 2141 | * Returns <0 in case of error. |
| 2142 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2143 | static int __get_cur_name_and_parent(struct send_ctx *sctx, |
| 2144 | u64 ino, u64 gen, |
| 2145 | u64 *parent_ino, |
| 2146 | u64 *parent_gen, |
| 2147 | struct fs_path *dest) |
| 2148 | { |
| 2149 | int ret; |
| 2150 | int nce_ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2151 | struct name_cache_entry *nce = NULL; |
| 2152 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2153 | /* |
| 2154 | * First check if we already did a call to this function with the same |
| 2155 | * ino/gen. If yes, check if the cache entry is still up-to-date. If yes |
| 2156 | * return the cached result. |
| 2157 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2158 | nce = name_cache_search(sctx, ino, gen); |
| 2159 | if (nce) { |
| 2160 | if (ino < sctx->send_progress && nce->need_later_update) { |
| 2161 | name_cache_delete(sctx, nce); |
| 2162 | kfree(nce); |
| 2163 | nce = NULL; |
| 2164 | } else { |
| 2165 | name_cache_used(sctx, nce); |
| 2166 | *parent_ino = nce->parent_ino; |
| 2167 | *parent_gen = nce->parent_gen; |
| 2168 | ret = fs_path_add(dest, nce->name, nce->name_len); |
| 2169 | if (ret < 0) |
| 2170 | goto out; |
| 2171 | ret = nce->ret; |
| 2172 | goto out; |
| 2173 | } |
| 2174 | } |
| 2175 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2176 | /* |
| 2177 | * If the inode is not existent yet, add the orphan name and return 1. |
| 2178 | * This should only happen for the parent dir that we determine in |
| 2179 | * __record_new_ref |
| 2180 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2181 | ret = is_inode_existent(sctx, ino, gen); |
| 2182 | if (ret < 0) |
| 2183 | goto out; |
| 2184 | |
| 2185 | if (!ret) { |
| 2186 | ret = gen_unique_name(sctx, ino, gen, dest); |
| 2187 | if (ret < 0) |
| 2188 | goto out; |
| 2189 | ret = 1; |
| 2190 | goto out_cache; |
| 2191 | } |
| 2192 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2193 | /* |
| 2194 | * Depending on whether the inode was already processed or not, use |
| 2195 | * send_root or parent_root for ref lookup. |
| 2196 | */ |
Filipe Manana | bf0d1f4 | 2014-02-21 00:01:32 +0000 | [diff] [blame] | 2197 | if (ino < sctx->send_progress) |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2198 | ret = get_first_ref(sctx->send_root, ino, |
| 2199 | parent_ino, parent_gen, dest); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2200 | else |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2201 | ret = get_first_ref(sctx->parent_root, ino, |
| 2202 | parent_ino, parent_gen, dest); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2203 | if (ret < 0) |
| 2204 | goto out; |
| 2205 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2206 | /* |
| 2207 | * Check if the ref was overwritten by an inode's ref that was processed |
| 2208 | * earlier. If yes, treat as orphan and return 1. |
| 2209 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2210 | ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, |
| 2211 | dest->start, dest->end - dest->start); |
| 2212 | if (ret < 0) |
| 2213 | goto out; |
| 2214 | if (ret) { |
| 2215 | fs_path_reset(dest); |
| 2216 | ret = gen_unique_name(sctx, ino, gen, dest); |
| 2217 | if (ret < 0) |
| 2218 | goto out; |
| 2219 | ret = 1; |
| 2220 | } |
| 2221 | |
| 2222 | out_cache: |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2223 | /* |
| 2224 | * Store the result of the lookup in the name cache. |
| 2225 | */ |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2226 | nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2227 | if (!nce) { |
| 2228 | ret = -ENOMEM; |
| 2229 | goto out; |
| 2230 | } |
| 2231 | |
| 2232 | nce->ino = ino; |
| 2233 | nce->gen = gen; |
| 2234 | nce->parent_ino = *parent_ino; |
| 2235 | nce->parent_gen = *parent_gen; |
| 2236 | nce->name_len = fs_path_len(dest); |
| 2237 | nce->ret = ret; |
| 2238 | strcpy(nce->name, dest->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2239 | |
| 2240 | if (ino < sctx->send_progress) |
| 2241 | nce->need_later_update = 0; |
| 2242 | else |
| 2243 | nce->need_later_update = 1; |
| 2244 | |
| 2245 | nce_ret = name_cache_insert(sctx, nce); |
| 2246 | if (nce_ret < 0) |
| 2247 | ret = nce_ret; |
| 2248 | name_cache_clean_unused(sctx); |
| 2249 | |
| 2250 | out: |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2251 | return ret; |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * Magic happens here. This function returns the first ref to an inode as it |
| 2256 | * would look like while receiving the stream at this point in time. |
| 2257 | * We walk the path up to the root. For every inode in between, we check if it |
| 2258 | * was already processed/sent. If yes, we continue with the parent as found |
| 2259 | * in send_root. If not, we continue with the parent as found in parent_root. |
| 2260 | * If we encounter an inode that was deleted at this point in time, we use the |
| 2261 | * inodes "orphan" name instead of the real name and stop. Same with new inodes |
| 2262 | * that were not created yet and overwritten inodes/refs. |
| 2263 | * |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2264 | * When do we have orphan inodes: |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2265 | * 1. When an inode is freshly created and thus no valid refs are available yet |
| 2266 | * 2. When a directory lost all it's refs (deleted) but still has dir items |
| 2267 | * inside which were not processed yet (pending for move/delete). If anyone |
| 2268 | * tried to get the path to the dir items, it would get a path inside that |
| 2269 | * orphan directory. |
| 2270 | * 3. When an inode is moved around or gets new links, it may overwrite the ref |
| 2271 | * of an unprocessed inode. If in that case the first ref would be |
| 2272 | * overwritten, the overwritten inode gets "orphanized". Later when we |
| 2273 | * process this overwritten inode, it is restored at a new place by moving |
| 2274 | * the orphan inode. |
| 2275 | * |
| 2276 | * sctx->send_progress tells this function at which point in time receiving |
| 2277 | * would be. |
| 2278 | */ |
| 2279 | static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, |
| 2280 | struct fs_path *dest) |
| 2281 | { |
| 2282 | int ret = 0; |
| 2283 | struct fs_path *name = NULL; |
| 2284 | u64 parent_inode = 0; |
| 2285 | u64 parent_gen = 0; |
| 2286 | int stop = 0; |
| 2287 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2288 | name = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2289 | if (!name) { |
| 2290 | ret = -ENOMEM; |
| 2291 | goto out; |
| 2292 | } |
| 2293 | |
| 2294 | dest->reversed = 1; |
| 2295 | fs_path_reset(dest); |
| 2296 | |
| 2297 | while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) { |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 2298 | struct waiting_dir_move *wdm; |
| 2299 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2300 | fs_path_reset(name); |
| 2301 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2302 | if (is_waiting_for_rm(sctx, ino)) { |
| 2303 | ret = gen_unique_name(sctx, ino, gen, name); |
| 2304 | if (ret < 0) |
| 2305 | goto out; |
| 2306 | ret = fs_path_add_path(dest, name); |
| 2307 | break; |
| 2308 | } |
| 2309 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 2310 | wdm = get_waiting_dir_move(sctx, ino); |
| 2311 | if (wdm && wdm->orphanized) { |
| 2312 | ret = gen_unique_name(sctx, ino, gen, name); |
| 2313 | stop = 1; |
| 2314 | } else if (wdm) { |
Filipe Manana | bf0d1f4 | 2014-02-21 00:01:32 +0000 | [diff] [blame] | 2315 | ret = get_first_ref(sctx->parent_root, ino, |
| 2316 | &parent_inode, &parent_gen, name); |
| 2317 | } else { |
| 2318 | ret = __get_cur_name_and_parent(sctx, ino, gen, |
| 2319 | &parent_inode, |
| 2320 | &parent_gen, name); |
| 2321 | if (ret) |
| 2322 | stop = 1; |
| 2323 | } |
| 2324 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2325 | if (ret < 0) |
| 2326 | goto out; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2327 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2328 | ret = fs_path_add_path(dest, name); |
| 2329 | if (ret < 0) |
| 2330 | goto out; |
| 2331 | |
| 2332 | ino = parent_inode; |
| 2333 | gen = parent_gen; |
| 2334 | } |
| 2335 | |
| 2336 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2337 | fs_path_free(name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2338 | if (!ret) |
| 2339 | fs_path_unreverse(dest); |
| 2340 | return ret; |
| 2341 | } |
| 2342 | |
| 2343 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2344 | * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace |
| 2345 | */ |
| 2346 | static int send_subvol_begin(struct send_ctx *sctx) |
| 2347 | { |
| 2348 | int ret; |
| 2349 | struct btrfs_root *send_root = sctx->send_root; |
| 2350 | struct btrfs_root *parent_root = sctx->parent_root; |
| 2351 | struct btrfs_path *path; |
| 2352 | struct btrfs_key key; |
| 2353 | struct btrfs_root_ref *ref; |
| 2354 | struct extent_buffer *leaf; |
| 2355 | char *name = NULL; |
| 2356 | int namelen; |
| 2357 | |
Wang Shilong | ffcfaf8 | 2014-01-15 00:26:43 +0800 | [diff] [blame] | 2358 | path = btrfs_alloc_path(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2359 | if (!path) |
| 2360 | return -ENOMEM; |
| 2361 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2362 | name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2363 | if (!name) { |
| 2364 | btrfs_free_path(path); |
| 2365 | return -ENOMEM; |
| 2366 | } |
| 2367 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 2368 | key.objectid = send_root->root_key.objectid; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2369 | key.type = BTRFS_ROOT_BACKREF_KEY; |
| 2370 | key.offset = 0; |
| 2371 | |
| 2372 | ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root, |
| 2373 | &key, path, 1, 0); |
| 2374 | if (ret < 0) |
| 2375 | goto out; |
| 2376 | if (ret) { |
| 2377 | ret = -ENOENT; |
| 2378 | goto out; |
| 2379 | } |
| 2380 | |
| 2381 | leaf = path->nodes[0]; |
| 2382 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2383 | if (key.type != BTRFS_ROOT_BACKREF_KEY || |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 2384 | key.objectid != send_root->root_key.objectid) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2385 | ret = -ENOENT; |
| 2386 | goto out; |
| 2387 | } |
| 2388 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 2389 | namelen = btrfs_root_ref_name_len(leaf, ref); |
| 2390 | read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen); |
| 2391 | btrfs_release_path(path); |
| 2392 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2393 | if (parent_root) { |
| 2394 | ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); |
| 2395 | if (ret < 0) |
| 2396 | goto out; |
| 2397 | } else { |
| 2398 | ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); |
| 2399 | if (ret < 0) |
| 2400 | goto out; |
| 2401 | } |
| 2402 | |
| 2403 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); |
Robin Ruede | b96b1db | 2015-09-30 21:23:33 +0200 | [diff] [blame] | 2404 | |
| 2405 | if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid)) |
| 2406 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, |
| 2407 | sctx->send_root->root_item.received_uuid); |
| 2408 | else |
| 2409 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, |
| 2410 | sctx->send_root->root_item.uuid); |
| 2411 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2412 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, |
Filipe David Borba Manana | 5a0f4e2 | 2013-12-03 15:55:48 +0000 | [diff] [blame] | 2413 | le64_to_cpu(sctx->send_root->root_item.ctransid)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2414 | if (parent_root) { |
Josef Bacik | 37b8d27 | 2015-06-04 17:17:25 -0400 | [diff] [blame] | 2415 | if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid)) |
| 2416 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 2417 | parent_root->root_item.received_uuid); |
| 2418 | else |
| 2419 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 2420 | parent_root->root_item.uuid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2421 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
Filipe David Borba Manana | 5a0f4e2 | 2013-12-03 15:55:48 +0000 | [diff] [blame] | 2422 | le64_to_cpu(sctx->parent_root->root_item.ctransid)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | ret = send_cmd(sctx); |
| 2426 | |
| 2427 | tlv_put_failure: |
| 2428 | out: |
| 2429 | btrfs_free_path(path); |
| 2430 | kfree(name); |
| 2431 | return ret; |
| 2432 | } |
| 2433 | |
| 2434 | static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) |
| 2435 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2436 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2437 | int ret = 0; |
| 2438 | struct fs_path *p; |
| 2439 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2440 | btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2441 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2442 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2443 | if (!p) |
| 2444 | return -ENOMEM; |
| 2445 | |
| 2446 | ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); |
| 2447 | if (ret < 0) |
| 2448 | goto out; |
| 2449 | |
| 2450 | ret = get_cur_path(sctx, ino, gen, p); |
| 2451 | if (ret < 0) |
| 2452 | goto out; |
| 2453 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2454 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); |
| 2455 | |
| 2456 | ret = send_cmd(sctx); |
| 2457 | |
| 2458 | tlv_put_failure: |
| 2459 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2460 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2461 | return ret; |
| 2462 | } |
| 2463 | |
| 2464 | static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) |
| 2465 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2466 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2467 | int ret = 0; |
| 2468 | struct fs_path *p; |
| 2469 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2470 | btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2471 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2472 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2473 | if (!p) |
| 2474 | return -ENOMEM; |
| 2475 | |
| 2476 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); |
| 2477 | if (ret < 0) |
| 2478 | goto out; |
| 2479 | |
| 2480 | ret = get_cur_path(sctx, ino, gen, p); |
| 2481 | if (ret < 0) |
| 2482 | goto out; |
| 2483 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2484 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); |
| 2485 | |
| 2486 | ret = send_cmd(sctx); |
| 2487 | |
| 2488 | tlv_put_failure: |
| 2489 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2490 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2491 | return ret; |
| 2492 | } |
| 2493 | |
| 2494 | static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) |
| 2495 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2496 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2497 | int ret = 0; |
| 2498 | struct fs_path *p; |
| 2499 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2500 | btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu", |
| 2501 | ino, uid, gid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2502 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2503 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2504 | if (!p) |
| 2505 | return -ENOMEM; |
| 2506 | |
| 2507 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); |
| 2508 | if (ret < 0) |
| 2509 | goto out; |
| 2510 | |
| 2511 | ret = get_cur_path(sctx, ino, gen, p); |
| 2512 | if (ret < 0) |
| 2513 | goto out; |
| 2514 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2515 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); |
| 2516 | TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); |
| 2517 | |
| 2518 | ret = send_cmd(sctx); |
| 2519 | |
| 2520 | tlv_put_failure: |
| 2521 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2522 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2523 | return ret; |
| 2524 | } |
| 2525 | |
| 2526 | static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) |
| 2527 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2528 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2529 | int ret = 0; |
| 2530 | struct fs_path *p = NULL; |
| 2531 | struct btrfs_inode_item *ii; |
| 2532 | struct btrfs_path *path = NULL; |
| 2533 | struct extent_buffer *eb; |
| 2534 | struct btrfs_key key; |
| 2535 | int slot; |
| 2536 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2537 | btrfs_debug(fs_info, "send_utimes %llu", ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2538 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2539 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2540 | if (!p) |
| 2541 | return -ENOMEM; |
| 2542 | |
| 2543 | path = alloc_path_for_send(); |
| 2544 | if (!path) { |
| 2545 | ret = -ENOMEM; |
| 2546 | goto out; |
| 2547 | } |
| 2548 | |
| 2549 | key.objectid = ino; |
| 2550 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2551 | key.offset = 0; |
| 2552 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); |
Filipe Manana | 15b253e | 2016-07-02 05:43:46 +0100 | [diff] [blame] | 2553 | if (ret > 0) |
| 2554 | ret = -ENOENT; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2555 | if (ret < 0) |
| 2556 | goto out; |
| 2557 | |
| 2558 | eb = path->nodes[0]; |
| 2559 | slot = path->slots[0]; |
| 2560 | ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); |
| 2561 | |
| 2562 | ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); |
| 2563 | if (ret < 0) |
| 2564 | goto out; |
| 2565 | |
| 2566 | ret = get_cur_path(sctx, ino, gen, p); |
| 2567 | if (ret < 0) |
| 2568 | goto out; |
| 2569 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 2570 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime); |
| 2571 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime); |
| 2572 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime); |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2573 | /* TODO Add otime support when the otime patches get into upstream */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2574 | |
| 2575 | ret = send_cmd(sctx); |
| 2576 | |
| 2577 | tlv_put_failure: |
| 2578 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2579 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2580 | btrfs_free_path(path); |
| 2581 | return ret; |
| 2582 | } |
| 2583 | |
| 2584 | /* |
| 2585 | * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have |
| 2586 | * a valid path yet because we did not process the refs yet. So, the inode |
| 2587 | * is created as orphan. |
| 2588 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2589 | static int send_create_inode(struct send_ctx *sctx, u64 ino) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2590 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2591 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2592 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2593 | struct fs_path *p; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2594 | int cmd; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2595 | u64 gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2596 | u64 mode; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2597 | u64 rdev; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2598 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2599 | btrfs_debug(fs_info, "send_create_inode %llu", ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2600 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2601 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2602 | if (!p) |
| 2603 | return -ENOMEM; |
| 2604 | |
Liu Bo | 644d194 | 2014-02-27 17:29:01 +0800 | [diff] [blame] | 2605 | if (ino != sctx->cur_ino) { |
| 2606 | ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, |
| 2607 | NULL, NULL, &rdev); |
| 2608 | if (ret < 0) |
| 2609 | goto out; |
| 2610 | } else { |
| 2611 | gen = sctx->cur_inode_gen; |
| 2612 | mode = sctx->cur_inode_mode; |
| 2613 | rdev = sctx->cur_inode_rdev; |
| 2614 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2615 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2616 | if (S_ISREG(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2617 | cmd = BTRFS_SEND_C_MKFILE; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2618 | } else if (S_ISDIR(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2619 | cmd = BTRFS_SEND_C_MKDIR; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2620 | } else if (S_ISLNK(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2621 | cmd = BTRFS_SEND_C_SYMLINK; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2622 | } else if (S_ISCHR(mode) || S_ISBLK(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2623 | cmd = BTRFS_SEND_C_MKNOD; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2624 | } else if (S_ISFIFO(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2625 | cmd = BTRFS_SEND_C_MKFIFO; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2626 | } else if (S_ISSOCK(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2627 | cmd = BTRFS_SEND_C_MKSOCK; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2628 | } else { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 2629 | btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o", |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2630 | (int)(mode & S_IFMT)); |
Tsutomu Itoh | ca6842b | 2016-01-22 09:13:25 +0900 | [diff] [blame] | 2631 | ret = -EOPNOTSUPP; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2632 | goto out; |
| 2633 | } |
| 2634 | |
| 2635 | ret = begin_cmd(sctx, cmd); |
| 2636 | if (ret < 0) |
| 2637 | goto out; |
| 2638 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2639 | ret = gen_unique_name(sctx, ino, gen, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2640 | if (ret < 0) |
| 2641 | goto out; |
| 2642 | |
| 2643 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2644 | TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2645 | |
| 2646 | if (S_ISLNK(mode)) { |
| 2647 | fs_path_reset(p); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2648 | ret = read_symlink(sctx->send_root, ino, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2649 | if (ret < 0) |
| 2650 | goto out; |
| 2651 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); |
| 2652 | } else if (S_ISCHR(mode) || S_ISBLK(mode) || |
| 2653 | S_ISFIFO(mode) || S_ISSOCK(mode)) { |
Arne Jansen | d79e504 | 2012-10-15 18:28:46 +0000 | [diff] [blame] | 2654 | TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev)); |
| 2655 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | ret = send_cmd(sctx); |
| 2659 | if (ret < 0) |
| 2660 | goto out; |
| 2661 | |
| 2662 | |
| 2663 | tlv_put_failure: |
| 2664 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2665 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2666 | return ret; |
| 2667 | } |
| 2668 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2669 | /* |
| 2670 | * We need some special handling for inodes that get processed before the parent |
| 2671 | * directory got created. See process_recorded_refs for details. |
| 2672 | * This function does the check if we already created the dir out of order. |
| 2673 | */ |
| 2674 | static int did_create_dir(struct send_ctx *sctx, u64 dir) |
| 2675 | { |
| 2676 | int ret = 0; |
| 2677 | struct btrfs_path *path = NULL; |
| 2678 | struct btrfs_key key; |
| 2679 | struct btrfs_key found_key; |
| 2680 | struct btrfs_key di_key; |
| 2681 | struct extent_buffer *eb; |
| 2682 | struct btrfs_dir_item *di; |
| 2683 | int slot; |
| 2684 | |
| 2685 | path = alloc_path_for_send(); |
| 2686 | if (!path) { |
| 2687 | ret = -ENOMEM; |
| 2688 | goto out; |
| 2689 | } |
| 2690 | |
| 2691 | key.objectid = dir; |
| 2692 | key.type = BTRFS_DIR_INDEX_KEY; |
| 2693 | key.offset = 0; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2694 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); |
| 2695 | if (ret < 0) |
| 2696 | goto out; |
| 2697 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2698 | while (1) { |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2699 | eb = path->nodes[0]; |
| 2700 | slot = path->slots[0]; |
| 2701 | if (slot >= btrfs_header_nritems(eb)) { |
| 2702 | ret = btrfs_next_leaf(sctx->send_root, path); |
| 2703 | if (ret < 0) { |
| 2704 | goto out; |
| 2705 | } else if (ret > 0) { |
| 2706 | ret = 0; |
| 2707 | break; |
| 2708 | } |
| 2709 | continue; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2710 | } |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2711 | |
| 2712 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 2713 | if (found_key.objectid != key.objectid || |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2714 | found_key.type != key.type) { |
| 2715 | ret = 0; |
| 2716 | goto out; |
| 2717 | } |
| 2718 | |
| 2719 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| 2720 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 2721 | |
Josef Bacik | a052541 | 2013-08-12 10:56:14 -0400 | [diff] [blame] | 2722 | if (di_key.type != BTRFS_ROOT_ITEM_KEY && |
| 2723 | di_key.objectid < sctx->send_progress) { |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2724 | ret = 1; |
| 2725 | goto out; |
| 2726 | } |
| 2727 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2728 | path->slots[0]++; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | out: |
| 2732 | btrfs_free_path(path); |
| 2733 | return ret; |
| 2734 | } |
| 2735 | |
| 2736 | /* |
| 2737 | * Only creates the inode if it is: |
| 2738 | * 1. Not a directory |
| 2739 | * 2. Or a directory which was not created already due to out of order |
| 2740 | * directories. See did_create_dir and process_recorded_refs for details. |
| 2741 | */ |
| 2742 | static int send_create_inode_if_needed(struct send_ctx *sctx) |
| 2743 | { |
| 2744 | int ret; |
| 2745 | |
| 2746 | if (S_ISDIR(sctx->cur_inode_mode)) { |
| 2747 | ret = did_create_dir(sctx, sctx->cur_ino); |
| 2748 | if (ret < 0) |
| 2749 | goto out; |
| 2750 | if (ret) { |
| 2751 | ret = 0; |
| 2752 | goto out; |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | ret = send_create_inode(sctx, sctx->cur_ino); |
| 2757 | if (ret < 0) |
| 2758 | goto out; |
| 2759 | |
| 2760 | out: |
| 2761 | return ret; |
| 2762 | } |
| 2763 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2764 | struct recorded_ref { |
| 2765 | struct list_head list; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2766 | char *name; |
| 2767 | struct fs_path *full_path; |
| 2768 | u64 dir; |
| 2769 | u64 dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2770 | int name_len; |
| 2771 | }; |
| 2772 | |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 2773 | static void set_ref_path(struct recorded_ref *ref, struct fs_path *path) |
| 2774 | { |
| 2775 | ref->full_path = path; |
| 2776 | ref->name = (char *)kbasename(ref->full_path->start); |
| 2777 | ref->name_len = ref->full_path->end - ref->name; |
| 2778 | } |
| 2779 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2780 | /* |
| 2781 | * We need to process new refs before deleted refs, but compare_tree gives us |
| 2782 | * everything mixed. So we first record all refs and later process them. |
| 2783 | * This function is a helper to record one ref. |
| 2784 | */ |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 2785 | static int __record_ref(struct list_head *head, u64 dir, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2786 | u64 dir_gen, struct fs_path *path) |
| 2787 | { |
| 2788 | struct recorded_ref *ref; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2789 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2790 | ref = kmalloc(sizeof(*ref), GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2791 | if (!ref) |
| 2792 | return -ENOMEM; |
| 2793 | |
| 2794 | ref->dir = dir; |
| 2795 | ref->dir_gen = dir_gen; |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 2796 | set_ref_path(ref, path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2797 | list_add_tail(&ref->list, head); |
| 2798 | return 0; |
| 2799 | } |
| 2800 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 2801 | static int dup_ref(struct recorded_ref *ref, struct list_head *list) |
| 2802 | { |
| 2803 | struct recorded_ref *new; |
| 2804 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2805 | new = kmalloc(sizeof(*ref), GFP_KERNEL); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 2806 | if (!new) |
| 2807 | return -ENOMEM; |
| 2808 | |
| 2809 | new->dir = ref->dir; |
| 2810 | new->dir_gen = ref->dir_gen; |
| 2811 | new->full_path = NULL; |
| 2812 | INIT_LIST_HEAD(&new->list); |
| 2813 | list_add_tail(&new->list, list); |
| 2814 | return 0; |
| 2815 | } |
| 2816 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2817 | static void __free_recorded_refs(struct list_head *head) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2818 | { |
| 2819 | struct recorded_ref *cur; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2820 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2821 | while (!list_empty(head)) { |
| 2822 | cur = list_entry(head->next, struct recorded_ref, list); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2823 | fs_path_free(cur->full_path); |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2824 | list_del(&cur->list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2825 | kfree(cur); |
| 2826 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | static void free_recorded_refs(struct send_ctx *sctx) |
| 2830 | { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2831 | __free_recorded_refs(&sctx->new_refs); |
| 2832 | __free_recorded_refs(&sctx->deleted_refs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | /* |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2836 | * Renames/moves a file/dir to its orphan name. Used when the first |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2837 | * ref of an unprocessed inode gets overwritten and for all non empty |
| 2838 | * directories. |
| 2839 | */ |
| 2840 | static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, |
| 2841 | struct fs_path *path) |
| 2842 | { |
| 2843 | int ret; |
| 2844 | struct fs_path *orphan; |
| 2845 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2846 | orphan = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2847 | if (!orphan) |
| 2848 | return -ENOMEM; |
| 2849 | |
| 2850 | ret = gen_unique_name(sctx, ino, gen, orphan); |
| 2851 | if (ret < 0) |
| 2852 | goto out; |
| 2853 | |
| 2854 | ret = send_rename(sctx, path, orphan); |
| 2855 | |
| 2856 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2857 | fs_path_free(orphan); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2858 | return ret; |
| 2859 | } |
| 2860 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2861 | static struct orphan_dir_info * |
| 2862 | add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino) |
| 2863 | { |
| 2864 | struct rb_node **p = &sctx->orphan_dirs.rb_node; |
| 2865 | struct rb_node *parent = NULL; |
| 2866 | struct orphan_dir_info *entry, *odi; |
| 2867 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2868 | while (*p) { |
| 2869 | parent = *p; |
| 2870 | entry = rb_entry(parent, struct orphan_dir_info, node); |
| 2871 | if (dir_ino < entry->ino) { |
| 2872 | p = &(*p)->rb_left; |
| 2873 | } else if (dir_ino > entry->ino) { |
| 2874 | p = &(*p)->rb_right; |
| 2875 | } else { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2876 | return entry; |
| 2877 | } |
| 2878 | } |
| 2879 | |
Robbie Ko | 35c8eda | 2018-05-08 18:11:37 +0800 | [diff] [blame] | 2880 | odi = kmalloc(sizeof(*odi), GFP_KERNEL); |
| 2881 | if (!odi) |
| 2882 | return ERR_PTR(-ENOMEM); |
| 2883 | odi->ino = dir_ino; |
| 2884 | odi->gen = 0; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2885 | odi->last_dir_index_offset = 0; |
Robbie Ko | 35c8eda | 2018-05-08 18:11:37 +0800 | [diff] [blame] | 2886 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2887 | rb_link_node(&odi->node, parent, p); |
| 2888 | rb_insert_color(&odi->node, &sctx->orphan_dirs); |
| 2889 | return odi; |
| 2890 | } |
| 2891 | |
| 2892 | static struct orphan_dir_info * |
| 2893 | get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino) |
| 2894 | { |
| 2895 | struct rb_node *n = sctx->orphan_dirs.rb_node; |
| 2896 | struct orphan_dir_info *entry; |
| 2897 | |
| 2898 | while (n) { |
| 2899 | entry = rb_entry(n, struct orphan_dir_info, node); |
| 2900 | if (dir_ino < entry->ino) |
| 2901 | n = n->rb_left; |
| 2902 | else if (dir_ino > entry->ino) |
| 2903 | n = n->rb_right; |
| 2904 | else |
| 2905 | return entry; |
| 2906 | } |
| 2907 | return NULL; |
| 2908 | } |
| 2909 | |
| 2910 | static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino) |
| 2911 | { |
| 2912 | struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino); |
| 2913 | |
| 2914 | return odi != NULL; |
| 2915 | } |
| 2916 | |
| 2917 | static void free_orphan_dir_info(struct send_ctx *sctx, |
| 2918 | struct orphan_dir_info *odi) |
| 2919 | { |
| 2920 | if (!odi) |
| 2921 | return; |
| 2922 | rb_erase(&odi->node, &sctx->orphan_dirs); |
| 2923 | kfree(odi); |
| 2924 | } |
| 2925 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2926 | /* |
| 2927 | * Returns 1 if a directory can be removed at this point in time. |
| 2928 | * We check this by iterating all dir items and checking if the inode behind |
| 2929 | * the dir item was already processed. |
| 2930 | */ |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2931 | static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
| 2932 | u64 send_progress) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2933 | { |
| 2934 | int ret = 0; |
| 2935 | struct btrfs_root *root = sctx->parent_root; |
| 2936 | struct btrfs_path *path; |
| 2937 | struct btrfs_key key; |
| 2938 | struct btrfs_key found_key; |
| 2939 | struct btrfs_key loc; |
| 2940 | struct btrfs_dir_item *di; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2941 | struct orphan_dir_info *odi = NULL; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2942 | |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 2943 | /* |
| 2944 | * Don't try to rmdir the top/root subvolume dir. |
| 2945 | */ |
| 2946 | if (dir == BTRFS_FIRST_FREE_OBJECTID) |
| 2947 | return 0; |
| 2948 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2949 | path = alloc_path_for_send(); |
| 2950 | if (!path) |
| 2951 | return -ENOMEM; |
| 2952 | |
| 2953 | key.objectid = dir; |
| 2954 | key.type = BTRFS_DIR_INDEX_KEY; |
| 2955 | key.offset = 0; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2956 | |
| 2957 | odi = get_orphan_dir_info(sctx, dir); |
| 2958 | if (odi) |
| 2959 | key.offset = odi->last_dir_index_offset; |
| 2960 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2961 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2962 | if (ret < 0) |
| 2963 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2964 | |
| 2965 | while (1) { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2966 | struct waiting_dir_move *dm; |
| 2967 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2968 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 2969 | ret = btrfs_next_leaf(root, path); |
| 2970 | if (ret < 0) |
| 2971 | goto out; |
| 2972 | else if (ret > 0) |
| 2973 | break; |
| 2974 | continue; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2975 | } |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2976 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2977 | path->slots[0]); |
| 2978 | if (found_key.objectid != key.objectid || |
| 2979 | found_key.type != key.type) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2980 | break; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2981 | |
| 2982 | di = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2983 | struct btrfs_dir_item); |
| 2984 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc); |
| 2985 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2986 | dm = get_waiting_dir_move(sctx, loc.objectid); |
| 2987 | if (dm) { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2988 | odi = add_orphan_dir_info(sctx, dir); |
| 2989 | if (IS_ERR(odi)) { |
| 2990 | ret = PTR_ERR(odi); |
| 2991 | goto out; |
| 2992 | } |
| 2993 | odi->gen = dir_gen; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2994 | odi->last_dir_index_offset = found_key.offset; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2995 | dm->rmdir_ino = dir; |
| 2996 | ret = 0; |
| 2997 | goto out; |
| 2998 | } |
| 2999 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3000 | if (loc.objectid > send_progress) { |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 3001 | odi = add_orphan_dir_info(sctx, dir); |
| 3002 | if (IS_ERR(odi)) { |
| 3003 | ret = PTR_ERR(odi); |
| 3004 | goto out; |
| 3005 | } |
| 3006 | odi->gen = dir_gen; |
| 3007 | odi->last_dir_index_offset = found_key.offset; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3008 | ret = 0; |
| 3009 | goto out; |
| 3010 | } |
| 3011 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 3012 | path->slots[0]++; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3013 | } |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 3014 | free_orphan_dir_info(sctx, odi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3015 | |
| 3016 | ret = 1; |
| 3017 | |
| 3018 | out: |
| 3019 | btrfs_free_path(path); |
| 3020 | return ret; |
| 3021 | } |
| 3022 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3023 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino) |
| 3024 | { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3025 | struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3026 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3027 | return entry != NULL; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3030 | static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3031 | { |
| 3032 | struct rb_node **p = &sctx->waiting_dir_moves.rb_node; |
| 3033 | struct rb_node *parent = NULL; |
| 3034 | struct waiting_dir_move *entry, *dm; |
| 3035 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 3036 | dm = kmalloc(sizeof(*dm), GFP_KERNEL); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3037 | if (!dm) |
| 3038 | return -ENOMEM; |
| 3039 | dm->ino = ino; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3040 | dm->rmdir_ino = 0; |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3041 | dm->orphanized = orphanized; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3042 | |
| 3043 | while (*p) { |
| 3044 | parent = *p; |
| 3045 | entry = rb_entry(parent, struct waiting_dir_move, node); |
| 3046 | if (ino < entry->ino) { |
| 3047 | p = &(*p)->rb_left; |
| 3048 | } else if (ino > entry->ino) { |
| 3049 | p = &(*p)->rb_right; |
| 3050 | } else { |
| 3051 | kfree(dm); |
| 3052 | return -EEXIST; |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | rb_link_node(&dm->node, parent, p); |
| 3057 | rb_insert_color(&dm->node, &sctx->waiting_dir_moves); |
| 3058 | return 0; |
| 3059 | } |
| 3060 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3061 | static struct waiting_dir_move * |
| 3062 | get_waiting_dir_move(struct send_ctx *sctx, u64 ino) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3063 | { |
| 3064 | struct rb_node *n = sctx->waiting_dir_moves.rb_node; |
| 3065 | struct waiting_dir_move *entry; |
| 3066 | |
| 3067 | while (n) { |
| 3068 | entry = rb_entry(n, struct waiting_dir_move, node); |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3069 | if (ino < entry->ino) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3070 | n = n->rb_left; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3071 | else if (ino > entry->ino) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3072 | n = n->rb_right; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3073 | else |
| 3074 | return entry; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3075 | } |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3076 | return NULL; |
| 3077 | } |
| 3078 | |
| 3079 | static void free_waiting_dir_move(struct send_ctx *sctx, |
| 3080 | struct waiting_dir_move *dm) |
| 3081 | { |
| 3082 | if (!dm) |
| 3083 | return; |
| 3084 | rb_erase(&dm->node, &sctx->waiting_dir_moves); |
| 3085 | kfree(dm); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3088 | static int add_pending_dir_move(struct send_ctx *sctx, |
| 3089 | u64 ino, |
| 3090 | u64 ino_gen, |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3091 | u64 parent_ino, |
| 3092 | struct list_head *new_refs, |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3093 | struct list_head *deleted_refs, |
| 3094 | const bool is_orphan) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3095 | { |
| 3096 | struct rb_node **p = &sctx->pending_dir_moves.rb_node; |
| 3097 | struct rb_node *parent = NULL; |
Chris Mason | 73b802f | 2014-03-21 15:30:44 -0700 | [diff] [blame] | 3098 | struct pending_dir_move *entry = NULL, *pm; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3099 | struct recorded_ref *cur; |
| 3100 | int exists = 0; |
| 3101 | int ret; |
| 3102 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 3103 | pm = kmalloc(sizeof(*pm), GFP_KERNEL); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3104 | if (!pm) |
| 3105 | return -ENOMEM; |
| 3106 | pm->parent_ino = parent_ino; |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3107 | pm->ino = ino; |
| 3108 | pm->gen = ino_gen; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3109 | INIT_LIST_HEAD(&pm->list); |
| 3110 | INIT_LIST_HEAD(&pm->update_refs); |
| 3111 | RB_CLEAR_NODE(&pm->node); |
| 3112 | |
| 3113 | while (*p) { |
| 3114 | parent = *p; |
| 3115 | entry = rb_entry(parent, struct pending_dir_move, node); |
| 3116 | if (parent_ino < entry->parent_ino) { |
| 3117 | p = &(*p)->rb_left; |
| 3118 | } else if (parent_ino > entry->parent_ino) { |
| 3119 | p = &(*p)->rb_right; |
| 3120 | } else { |
| 3121 | exists = 1; |
| 3122 | break; |
| 3123 | } |
| 3124 | } |
| 3125 | |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3126 | list_for_each_entry(cur, deleted_refs, list) { |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3127 | ret = dup_ref(cur, &pm->update_refs); |
| 3128 | if (ret < 0) |
| 3129 | goto out; |
| 3130 | } |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3131 | list_for_each_entry(cur, new_refs, list) { |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3132 | ret = dup_ref(cur, &pm->update_refs); |
| 3133 | if (ret < 0) |
| 3134 | goto out; |
| 3135 | } |
| 3136 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3137 | ret = add_waiting_dir_move(sctx, pm->ino, is_orphan); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3138 | if (ret) |
| 3139 | goto out; |
| 3140 | |
| 3141 | if (exists) { |
| 3142 | list_add_tail(&pm->list, &entry->list); |
| 3143 | } else { |
| 3144 | rb_link_node(&pm->node, parent, p); |
| 3145 | rb_insert_color(&pm->node, &sctx->pending_dir_moves); |
| 3146 | } |
| 3147 | ret = 0; |
| 3148 | out: |
| 3149 | if (ret) { |
| 3150 | __free_recorded_refs(&pm->update_refs); |
| 3151 | kfree(pm); |
| 3152 | } |
| 3153 | return ret; |
| 3154 | } |
| 3155 | |
| 3156 | static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx, |
| 3157 | u64 parent_ino) |
| 3158 | { |
| 3159 | struct rb_node *n = sctx->pending_dir_moves.rb_node; |
| 3160 | struct pending_dir_move *entry; |
| 3161 | |
| 3162 | while (n) { |
| 3163 | entry = rb_entry(n, struct pending_dir_move, node); |
| 3164 | if (parent_ino < entry->parent_ino) |
| 3165 | n = n->rb_left; |
| 3166 | else if (parent_ino > entry->parent_ino) |
| 3167 | n = n->rb_right; |
| 3168 | else |
| 3169 | return entry; |
| 3170 | } |
| 3171 | return NULL; |
| 3172 | } |
| 3173 | |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3174 | static int path_loop(struct send_ctx *sctx, struct fs_path *name, |
| 3175 | u64 ino, u64 gen, u64 *ancestor_ino) |
| 3176 | { |
| 3177 | int ret = 0; |
| 3178 | u64 parent_inode = 0; |
| 3179 | u64 parent_gen = 0; |
| 3180 | u64 start_ino = ino; |
| 3181 | |
| 3182 | *ancestor_ino = 0; |
| 3183 | while (ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 3184 | fs_path_reset(name); |
| 3185 | |
| 3186 | if (is_waiting_for_rm(sctx, ino)) |
| 3187 | break; |
| 3188 | if (is_waiting_for_move(sctx, ino)) { |
| 3189 | if (*ancestor_ino == 0) |
| 3190 | *ancestor_ino = ino; |
| 3191 | ret = get_first_ref(sctx->parent_root, ino, |
| 3192 | &parent_inode, &parent_gen, name); |
| 3193 | } else { |
| 3194 | ret = __get_cur_name_and_parent(sctx, ino, gen, |
| 3195 | &parent_inode, |
| 3196 | &parent_gen, name); |
| 3197 | if (ret > 0) { |
| 3198 | ret = 0; |
| 3199 | break; |
| 3200 | } |
| 3201 | } |
| 3202 | if (ret < 0) |
| 3203 | break; |
| 3204 | if (parent_inode == start_ino) { |
| 3205 | ret = 1; |
| 3206 | if (*ancestor_ino == 0) |
| 3207 | *ancestor_ino = ino; |
| 3208 | break; |
| 3209 | } |
| 3210 | ino = parent_inode; |
| 3211 | gen = parent_gen; |
| 3212 | } |
| 3213 | return ret; |
| 3214 | } |
| 3215 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3216 | static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm) |
| 3217 | { |
| 3218 | struct fs_path *from_path = NULL; |
| 3219 | struct fs_path *to_path = NULL; |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3220 | struct fs_path *name = NULL; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3221 | u64 orig_progress = sctx->send_progress; |
| 3222 | struct recorded_ref *cur; |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3223 | u64 parent_ino, parent_gen; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3224 | struct waiting_dir_move *dm = NULL; |
| 3225 | u64 rmdir_ino = 0; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3226 | u64 ancestor; |
| 3227 | bool is_orphan; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3228 | int ret; |
| 3229 | |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3230 | name = fs_path_alloc(); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3231 | from_path = fs_path_alloc(); |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3232 | if (!name || !from_path) { |
| 3233 | ret = -ENOMEM; |
| 3234 | goto out; |
| 3235 | } |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3236 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3237 | dm = get_waiting_dir_move(sctx, pm->ino); |
| 3238 | ASSERT(dm); |
| 3239 | rmdir_ino = dm->rmdir_ino; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3240 | is_orphan = dm->orphanized; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3241 | free_waiting_dir_move(sctx, dm); |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3242 | |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3243 | if (is_orphan) { |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3244 | ret = gen_unique_name(sctx, pm->ino, |
| 3245 | pm->gen, from_path); |
| 3246 | } else { |
| 3247 | ret = get_first_ref(sctx->parent_root, pm->ino, |
| 3248 | &parent_ino, &parent_gen, name); |
| 3249 | if (ret < 0) |
| 3250 | goto out; |
| 3251 | ret = get_cur_path(sctx, parent_ino, parent_gen, |
| 3252 | from_path); |
| 3253 | if (ret < 0) |
| 3254 | goto out; |
| 3255 | ret = fs_path_add_path(from_path, name); |
| 3256 | } |
Filipe Manana | c992ec9 | 2014-03-22 17:15:24 +0000 | [diff] [blame] | 3257 | if (ret < 0) |
| 3258 | goto out; |
| 3259 | |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3260 | sctx->send_progress = sctx->cur_ino + 1; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3261 | ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor); |
Filipe Manana | 7969e77 | 2016-06-17 17:13:36 +0100 | [diff] [blame] | 3262 | if (ret < 0) |
| 3263 | goto out; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3264 | if (ret) { |
| 3265 | LIST_HEAD(deleted_refs); |
| 3266 | ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID); |
| 3267 | ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor, |
| 3268 | &pm->update_refs, &deleted_refs, |
| 3269 | is_orphan); |
| 3270 | if (ret < 0) |
| 3271 | goto out; |
| 3272 | if (rmdir_ino) { |
| 3273 | dm = get_waiting_dir_move(sctx, pm->ino); |
| 3274 | ASSERT(dm); |
| 3275 | dm->rmdir_ino = rmdir_ino; |
| 3276 | } |
| 3277 | goto out; |
| 3278 | } |
Filipe Manana | c992ec9 | 2014-03-22 17:15:24 +0000 | [diff] [blame] | 3279 | fs_path_reset(name); |
| 3280 | to_path = name; |
| 3281 | name = NULL; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3282 | ret = get_cur_path(sctx, pm->ino, pm->gen, to_path); |
| 3283 | if (ret < 0) |
| 3284 | goto out; |
| 3285 | |
| 3286 | ret = send_rename(sctx, from_path, to_path); |
| 3287 | if (ret < 0) |
| 3288 | goto out; |
| 3289 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3290 | if (rmdir_ino) { |
| 3291 | struct orphan_dir_info *odi; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 3292 | u64 gen; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3293 | |
| 3294 | odi = get_orphan_dir_info(sctx, rmdir_ino); |
| 3295 | if (!odi) { |
| 3296 | /* already deleted */ |
| 3297 | goto finish; |
| 3298 | } |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 3299 | gen = odi->gen; |
| 3300 | |
| 3301 | ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino); |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3302 | if (ret < 0) |
| 3303 | goto out; |
| 3304 | if (!ret) |
| 3305 | goto finish; |
| 3306 | |
| 3307 | name = fs_path_alloc(); |
| 3308 | if (!name) { |
| 3309 | ret = -ENOMEM; |
| 3310 | goto out; |
| 3311 | } |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 3312 | ret = get_cur_path(sctx, rmdir_ino, gen, name); |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3313 | if (ret < 0) |
| 3314 | goto out; |
| 3315 | ret = send_rmdir(sctx, name); |
| 3316 | if (ret < 0) |
| 3317 | goto out; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | finish: |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3321 | ret = send_utimes(sctx, pm->ino, pm->gen); |
| 3322 | if (ret < 0) |
| 3323 | goto out; |
| 3324 | |
| 3325 | /* |
| 3326 | * After rename/move, need to update the utimes of both new parent(s) |
| 3327 | * and old parent(s). |
| 3328 | */ |
| 3329 | list_for_each_entry(cur, &pm->update_refs, list) { |
Robbie Ko | 764433a | 2015-06-23 18:39:50 +0800 | [diff] [blame] | 3330 | /* |
| 3331 | * The parent inode might have been deleted in the send snapshot |
| 3332 | */ |
| 3333 | ret = get_inode_info(sctx->send_root, cur->dir, NULL, |
| 3334 | NULL, NULL, NULL, NULL, NULL); |
| 3335 | if (ret == -ENOENT) { |
| 3336 | ret = 0; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3337 | continue; |
Robbie Ko | 764433a | 2015-06-23 18:39:50 +0800 | [diff] [blame] | 3338 | } |
| 3339 | if (ret < 0) |
| 3340 | goto out; |
| 3341 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3342 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); |
| 3343 | if (ret < 0) |
| 3344 | goto out; |
| 3345 | } |
| 3346 | |
| 3347 | out: |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3348 | fs_path_free(name); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3349 | fs_path_free(from_path); |
| 3350 | fs_path_free(to_path); |
| 3351 | sctx->send_progress = orig_progress; |
| 3352 | |
| 3353 | return ret; |
| 3354 | } |
| 3355 | |
| 3356 | static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m) |
| 3357 | { |
| 3358 | if (!list_empty(&m->list)) |
| 3359 | list_del(&m->list); |
| 3360 | if (!RB_EMPTY_NODE(&m->node)) |
| 3361 | rb_erase(&m->node, &sctx->pending_dir_moves); |
| 3362 | __free_recorded_refs(&m->update_refs); |
| 3363 | kfree(m); |
| 3364 | } |
| 3365 | |
Robbie Ko | a4390ae | 2018-11-14 18:32:37 +0000 | [diff] [blame] | 3366 | static void tail_append_pending_moves(struct send_ctx *sctx, |
| 3367 | struct pending_dir_move *moves, |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3368 | struct list_head *stack) |
| 3369 | { |
| 3370 | if (list_empty(&moves->list)) { |
| 3371 | list_add_tail(&moves->list, stack); |
| 3372 | } else { |
| 3373 | LIST_HEAD(list); |
| 3374 | list_splice_init(&moves->list, &list); |
| 3375 | list_add_tail(&moves->list, stack); |
| 3376 | list_splice_tail(&list, stack); |
| 3377 | } |
Robbie Ko | a4390ae | 2018-11-14 18:32:37 +0000 | [diff] [blame] | 3378 | if (!RB_EMPTY_NODE(&moves->node)) { |
| 3379 | rb_erase(&moves->node, &sctx->pending_dir_moves); |
| 3380 | RB_CLEAR_NODE(&moves->node); |
| 3381 | } |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | static int apply_children_dir_moves(struct send_ctx *sctx) |
| 3385 | { |
| 3386 | struct pending_dir_move *pm; |
| 3387 | struct list_head stack; |
| 3388 | u64 parent_ino = sctx->cur_ino; |
| 3389 | int ret = 0; |
| 3390 | |
| 3391 | pm = get_pending_dir_moves(sctx, parent_ino); |
| 3392 | if (!pm) |
| 3393 | return 0; |
| 3394 | |
| 3395 | INIT_LIST_HEAD(&stack); |
Robbie Ko | a4390ae | 2018-11-14 18:32:37 +0000 | [diff] [blame] | 3396 | tail_append_pending_moves(sctx, pm, &stack); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3397 | |
| 3398 | while (!list_empty(&stack)) { |
| 3399 | pm = list_first_entry(&stack, struct pending_dir_move, list); |
| 3400 | parent_ino = pm->ino; |
| 3401 | ret = apply_dir_move(sctx, pm); |
| 3402 | free_pending_move(sctx, pm); |
| 3403 | if (ret) |
| 3404 | goto out; |
| 3405 | pm = get_pending_dir_moves(sctx, parent_ino); |
| 3406 | if (pm) |
Robbie Ko | a4390ae | 2018-11-14 18:32:37 +0000 | [diff] [blame] | 3407 | tail_append_pending_moves(sctx, pm, &stack); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3408 | } |
| 3409 | return 0; |
| 3410 | |
| 3411 | out: |
| 3412 | while (!list_empty(&stack)) { |
| 3413 | pm = list_first_entry(&stack, struct pending_dir_move, list); |
| 3414 | free_pending_move(sctx, pm); |
| 3415 | } |
| 3416 | return ret; |
| 3417 | } |
| 3418 | |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3419 | /* |
| 3420 | * We might need to delay a directory rename even when no ancestor directory |
| 3421 | * (in the send root) with a higher inode number than ours (sctx->cur_ino) was |
| 3422 | * renamed. This happens when we rename a directory to the old name (the name |
| 3423 | * in the parent root) of some other unrelated directory that got its rename |
| 3424 | * delayed due to some ancestor with higher number that got renamed. |
| 3425 | * |
| 3426 | * Example: |
| 3427 | * |
| 3428 | * Parent snapshot: |
| 3429 | * . (ino 256) |
| 3430 | * |---- a/ (ino 257) |
| 3431 | * | |---- file (ino 260) |
| 3432 | * | |
| 3433 | * |---- b/ (ino 258) |
| 3434 | * |---- c/ (ino 259) |
| 3435 | * |
| 3436 | * Send snapshot: |
| 3437 | * . (ino 256) |
| 3438 | * |---- a/ (ino 258) |
| 3439 | * |---- x/ (ino 259) |
| 3440 | * |---- y/ (ino 257) |
| 3441 | * |----- file (ino 260) |
| 3442 | * |
| 3443 | * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257 |
| 3444 | * from 'a' to 'x/y' happening first, which in turn depends on the rename of |
| 3445 | * inode 259 from 'c' to 'x'. So the order of rename commands the send stream |
| 3446 | * must issue is: |
| 3447 | * |
| 3448 | * 1 - rename 259 from 'c' to 'x' |
| 3449 | * 2 - rename 257 from 'a' to 'x/y' |
| 3450 | * 3 - rename 258 from 'b' to 'a' |
| 3451 | * |
| 3452 | * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can |
| 3453 | * be done right away and < 0 on error. |
| 3454 | */ |
| 3455 | static int wait_for_dest_dir_move(struct send_ctx *sctx, |
| 3456 | struct recorded_ref *parent_ref, |
| 3457 | const bool is_orphan) |
| 3458 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3459 | struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info; |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3460 | struct btrfs_path *path; |
| 3461 | struct btrfs_key key; |
| 3462 | struct btrfs_key di_key; |
| 3463 | struct btrfs_dir_item *di; |
| 3464 | u64 left_gen; |
| 3465 | u64 right_gen; |
| 3466 | int ret = 0; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3467 | struct waiting_dir_move *wdm; |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3468 | |
| 3469 | if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) |
| 3470 | return 0; |
| 3471 | |
| 3472 | path = alloc_path_for_send(); |
| 3473 | if (!path) |
| 3474 | return -ENOMEM; |
| 3475 | |
| 3476 | key.objectid = parent_ref->dir; |
| 3477 | key.type = BTRFS_DIR_ITEM_KEY; |
| 3478 | key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len); |
| 3479 | |
| 3480 | ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); |
| 3481 | if (ret < 0) { |
| 3482 | goto out; |
| 3483 | } else if (ret > 0) { |
| 3484 | ret = 0; |
| 3485 | goto out; |
| 3486 | } |
| 3487 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3488 | di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name, |
| 3489 | parent_ref->name_len); |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3490 | if (!di) { |
| 3491 | ret = 0; |
| 3492 | goto out; |
| 3493 | } |
| 3494 | /* |
| 3495 | * di_key.objectid has the number of the inode that has a dentry in the |
| 3496 | * parent directory with the same name that sctx->cur_ino is being |
| 3497 | * renamed to. We need to check if that inode is in the send root as |
| 3498 | * well and if it is currently marked as an inode with a pending rename, |
| 3499 | * if it is, we need to delay the rename of sctx->cur_ino as well, so |
| 3500 | * that it happens after that other inode is renamed. |
| 3501 | */ |
| 3502 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key); |
| 3503 | if (di_key.type != BTRFS_INODE_ITEM_KEY) { |
| 3504 | ret = 0; |
| 3505 | goto out; |
| 3506 | } |
| 3507 | |
| 3508 | ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL, |
| 3509 | &left_gen, NULL, NULL, NULL, NULL); |
| 3510 | if (ret < 0) |
| 3511 | goto out; |
| 3512 | ret = get_inode_info(sctx->send_root, di_key.objectid, NULL, |
| 3513 | &right_gen, NULL, NULL, NULL, NULL); |
| 3514 | if (ret < 0) { |
| 3515 | if (ret == -ENOENT) |
| 3516 | ret = 0; |
| 3517 | goto out; |
| 3518 | } |
| 3519 | |
| 3520 | /* Different inode, no need to delay the rename of sctx->cur_ino */ |
| 3521 | if (right_gen != left_gen) { |
| 3522 | ret = 0; |
| 3523 | goto out; |
| 3524 | } |
| 3525 | |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3526 | wdm = get_waiting_dir_move(sctx, di_key.objectid); |
| 3527 | if (wdm && !wdm->orphanized) { |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3528 | ret = add_pending_dir_move(sctx, |
| 3529 | sctx->cur_ino, |
| 3530 | sctx->cur_inode_gen, |
| 3531 | di_key.objectid, |
| 3532 | &sctx->new_refs, |
| 3533 | &sctx->deleted_refs, |
| 3534 | is_orphan); |
| 3535 | if (!ret) |
| 3536 | ret = 1; |
| 3537 | } |
| 3538 | out: |
| 3539 | btrfs_free_path(path); |
| 3540 | return ret; |
| 3541 | } |
| 3542 | |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3543 | /* |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3544 | * Check if inode ino2, or any of its ancestors, is inode ino1. |
| 3545 | * Return 1 if true, 0 if false and < 0 on error. |
| 3546 | */ |
| 3547 | static int check_ino_in_path(struct btrfs_root *root, |
| 3548 | const u64 ino1, |
| 3549 | const u64 ino1_gen, |
| 3550 | const u64 ino2, |
| 3551 | const u64 ino2_gen, |
| 3552 | struct fs_path *fs_path) |
| 3553 | { |
| 3554 | u64 ino = ino2; |
| 3555 | |
| 3556 | if (ino1 == ino2) |
| 3557 | return ino1_gen == ino2_gen; |
| 3558 | |
| 3559 | while (ino > BTRFS_FIRST_FREE_OBJECTID) { |
| 3560 | u64 parent; |
| 3561 | u64 parent_gen; |
| 3562 | int ret; |
| 3563 | |
| 3564 | fs_path_reset(fs_path); |
| 3565 | ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path); |
| 3566 | if (ret < 0) |
| 3567 | return ret; |
| 3568 | if (parent == ino1) |
| 3569 | return parent_gen == ino1_gen; |
| 3570 | ino = parent; |
| 3571 | } |
| 3572 | return 0; |
| 3573 | } |
| 3574 | |
| 3575 | /* |
| 3576 | * Check if ino ino1 is an ancestor of inode ino2 in the given root for any |
| 3577 | * possible path (in case ino2 is not a directory and has multiple hard links). |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3578 | * Return 1 if true, 0 if false and < 0 on error. |
| 3579 | */ |
| 3580 | static int is_ancestor(struct btrfs_root *root, |
| 3581 | const u64 ino1, |
| 3582 | const u64 ino1_gen, |
| 3583 | const u64 ino2, |
| 3584 | struct fs_path *fs_path) |
| 3585 | { |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3586 | bool free_fs_path = false; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3587 | int ret = 0; |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3588 | struct btrfs_path *path = NULL; |
| 3589 | struct btrfs_key key; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3590 | |
| 3591 | if (!fs_path) { |
| 3592 | fs_path = fs_path_alloc(); |
| 3593 | if (!fs_path) |
| 3594 | return -ENOMEM; |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3595 | free_fs_path = true; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3596 | } |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3597 | |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3598 | path = alloc_path_for_send(); |
| 3599 | if (!path) { |
| 3600 | ret = -ENOMEM; |
| 3601 | goto out; |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3602 | } |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3603 | |
| 3604 | key.objectid = ino2; |
| 3605 | key.type = BTRFS_INODE_REF_KEY; |
| 3606 | key.offset = 0; |
| 3607 | |
| 3608 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3609 | if (ret < 0) |
| 3610 | goto out; |
| 3611 | |
| 3612 | while (true) { |
| 3613 | struct extent_buffer *leaf = path->nodes[0]; |
| 3614 | int slot = path->slots[0]; |
| 3615 | u32 cur_offset = 0; |
| 3616 | u32 item_size; |
| 3617 | |
| 3618 | if (slot >= btrfs_header_nritems(leaf)) { |
| 3619 | ret = btrfs_next_leaf(root, path); |
| 3620 | if (ret < 0) |
| 3621 | goto out; |
| 3622 | if (ret > 0) |
| 3623 | break; |
| 3624 | continue; |
| 3625 | } |
| 3626 | |
| 3627 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 3628 | if (key.objectid != ino2) |
| 3629 | break; |
| 3630 | if (key.type != BTRFS_INODE_REF_KEY && |
| 3631 | key.type != BTRFS_INODE_EXTREF_KEY) |
| 3632 | break; |
| 3633 | |
| 3634 | item_size = btrfs_item_size_nr(leaf, slot); |
| 3635 | while (cur_offset < item_size) { |
| 3636 | u64 parent; |
| 3637 | u64 parent_gen; |
| 3638 | |
| 3639 | if (key.type == BTRFS_INODE_EXTREF_KEY) { |
| 3640 | unsigned long ptr; |
| 3641 | struct btrfs_inode_extref *extref; |
| 3642 | |
| 3643 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 3644 | extref = (struct btrfs_inode_extref *) |
| 3645 | (ptr + cur_offset); |
| 3646 | parent = btrfs_inode_extref_parent(leaf, |
| 3647 | extref); |
| 3648 | cur_offset += sizeof(*extref); |
| 3649 | cur_offset += btrfs_inode_extref_name_len(leaf, |
| 3650 | extref); |
| 3651 | } else { |
| 3652 | parent = key.offset; |
| 3653 | cur_offset = item_size; |
| 3654 | } |
| 3655 | |
| 3656 | ret = get_inode_info(root, parent, NULL, &parent_gen, |
| 3657 | NULL, NULL, NULL, NULL); |
| 3658 | if (ret < 0) |
| 3659 | goto out; |
| 3660 | ret = check_ino_in_path(root, ino1, ino1_gen, |
| 3661 | parent, parent_gen, fs_path); |
| 3662 | if (ret) |
| 3663 | goto out; |
| 3664 | } |
| 3665 | path->slots[0]++; |
| 3666 | } |
| 3667 | ret = 0; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3668 | out: |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3669 | btrfs_free_path(path); |
| 3670 | if (free_fs_path) |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3671 | fs_path_free(fs_path); |
| 3672 | return ret; |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3675 | static int wait_for_parent_move(struct send_ctx *sctx, |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3676 | struct recorded_ref *parent_ref, |
| 3677 | const bool is_orphan) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3678 | { |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3679 | int ret = 0; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3680 | u64 ino = parent_ref->dir; |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3681 | u64 ino_gen = parent_ref->dir_gen; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3682 | u64 parent_ino_before, parent_ino_after; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3683 | struct fs_path *path_before = NULL; |
| 3684 | struct fs_path *path_after = NULL; |
| 3685 | int len1, len2; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3686 | |
| 3687 | path_after = fs_path_alloc(); |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3688 | path_before = fs_path_alloc(); |
| 3689 | if (!path_after || !path_before) { |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3690 | ret = -ENOMEM; |
| 3691 | goto out; |
| 3692 | } |
| 3693 | |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3694 | /* |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3695 | * Our current directory inode may not yet be renamed/moved because some |
| 3696 | * ancestor (immediate or not) has to be renamed/moved first. So find if |
| 3697 | * such ancestor exists and make sure our own rename/move happens after |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3698 | * that ancestor is processed to avoid path build infinite loops (done |
| 3699 | * at get_cur_path()). |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3700 | */ |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3701 | while (ino > BTRFS_FIRST_FREE_OBJECTID) { |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3702 | u64 parent_ino_after_gen; |
| 3703 | |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3704 | if (is_waiting_for_move(sctx, ino)) { |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3705 | /* |
| 3706 | * If the current inode is an ancestor of ino in the |
| 3707 | * parent root, we need to delay the rename of the |
| 3708 | * current inode, otherwise don't delayed the rename |
| 3709 | * because we can end up with a circular dependency |
| 3710 | * of renames, resulting in some directories never |
| 3711 | * getting the respective rename operations issued in |
| 3712 | * the send stream or getting into infinite path build |
| 3713 | * loops. |
| 3714 | */ |
| 3715 | ret = is_ancestor(sctx->parent_root, |
| 3716 | sctx->cur_ino, sctx->cur_inode_gen, |
| 3717 | ino, path_before); |
Filipe Manana | 4122ea6 | 2016-06-27 16:54:44 +0100 | [diff] [blame] | 3718 | if (ret) |
| 3719 | break; |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3720 | } |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3721 | |
| 3722 | fs_path_reset(path_before); |
| 3723 | fs_path_reset(path_after); |
| 3724 | |
| 3725 | ret = get_first_ref(sctx->send_root, ino, &parent_ino_after, |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3726 | &parent_ino_after_gen, path_after); |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3727 | if (ret < 0) |
| 3728 | goto out; |
| 3729 | ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before, |
| 3730 | NULL, path_before); |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3731 | if (ret < 0 && ret != -ENOENT) { |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3732 | goto out; |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3733 | } else if (ret == -ENOENT) { |
Filipe Manana | bf8e8ca | 2014-10-02 19:17:32 +0100 | [diff] [blame] | 3734 | ret = 0; |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3735 | break; |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | len1 = fs_path_len(path_before); |
| 3739 | len2 = fs_path_len(path_after); |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3740 | if (ino > sctx->cur_ino && |
| 3741 | (parent_ino_before != parent_ino_after || len1 != len2 || |
| 3742 | memcmp(path_before->start, path_after->start, len1))) { |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3743 | u64 parent_ino_gen; |
| 3744 | |
| 3745 | ret = get_inode_info(sctx->parent_root, ino, NULL, |
| 3746 | &parent_ino_gen, NULL, NULL, NULL, |
| 3747 | NULL); |
| 3748 | if (ret < 0) |
| 3749 | goto out; |
| 3750 | if (ino_gen == parent_ino_gen) { |
| 3751 | ret = 1; |
| 3752 | break; |
| 3753 | } |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3754 | } |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3755 | ino = parent_ino_after; |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3756 | ino_gen = parent_ino_after_gen; |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3759 | out: |
| 3760 | fs_path_free(path_before); |
| 3761 | fs_path_free(path_after); |
| 3762 | |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3763 | if (ret == 1) { |
| 3764 | ret = add_pending_dir_move(sctx, |
| 3765 | sctx->cur_ino, |
| 3766 | sctx->cur_inode_gen, |
| 3767 | ino, |
| 3768 | &sctx->new_refs, |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3769 | &sctx->deleted_refs, |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3770 | is_orphan); |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3771 | if (!ret) |
| 3772 | ret = 1; |
| 3773 | } |
| 3774 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3775 | return ret; |
| 3776 | } |
| 3777 | |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3778 | static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) |
| 3779 | { |
| 3780 | int ret; |
| 3781 | struct fs_path *new_path; |
| 3782 | |
| 3783 | /* |
| 3784 | * Our reference's name member points to its full_path member string, so |
| 3785 | * we use here a new path. |
| 3786 | */ |
| 3787 | new_path = fs_path_alloc(); |
| 3788 | if (!new_path) |
| 3789 | return -ENOMEM; |
| 3790 | |
| 3791 | ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path); |
| 3792 | if (ret < 0) { |
| 3793 | fs_path_free(new_path); |
| 3794 | return ret; |
| 3795 | } |
| 3796 | ret = fs_path_add(new_path, ref->name, ref->name_len); |
| 3797 | if (ret < 0) { |
| 3798 | fs_path_free(new_path); |
| 3799 | return ret; |
| 3800 | } |
| 3801 | |
| 3802 | fs_path_free(ref->full_path); |
| 3803 | set_ref_path(ref, new_path); |
| 3804 | |
| 3805 | return 0; |
| 3806 | } |
| 3807 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3808 | /* |
| 3809 | * This does all the move/link/unlink/rmdir magic. |
| 3810 | */ |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3811 | static int process_recorded_refs(struct send_ctx *sctx, int *pending_move) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3812 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 3813 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3814 | int ret = 0; |
| 3815 | struct recorded_ref *cur; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3816 | struct recorded_ref *cur2; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3817 | struct list_head check_dirs; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3818 | struct fs_path *valid_path = NULL; |
Chris Mason | b24baf6 | 2012-07-25 19:21:10 -0400 | [diff] [blame] | 3819 | u64 ow_inode = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3820 | u64 ow_gen; |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3821 | u64 ow_mode; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3822 | int did_overwrite = 0; |
| 3823 | int is_orphan = 0; |
Filipe Manana | 29d6d30 | 2014-02-16 21:01:39 +0000 | [diff] [blame] | 3824 | u64 last_dir_ino_rm = 0; |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3825 | bool can_rename = true; |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3826 | bool orphanized_dir = false; |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 3827 | bool orphanized_ancestor = false; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3828 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 3829 | btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3830 | |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 3831 | /* |
| 3832 | * This should never happen as the root dir always has the same ref |
| 3833 | * which is always '..' |
| 3834 | */ |
| 3835 | BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3836 | INIT_LIST_HEAD(&check_dirs); |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 3837 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3838 | valid_path = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3839 | if (!valid_path) { |
| 3840 | ret = -ENOMEM; |
| 3841 | goto out; |
| 3842 | } |
| 3843 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3844 | /* |
| 3845 | * First, check if the first ref of the current inode was overwritten |
| 3846 | * before. If yes, we know that the current inode was already orphanized |
| 3847 | * and thus use the orphan name. If not, we can use get_cur_path to |
| 3848 | * get the path of the first ref as it would like while receiving at |
| 3849 | * this point in time. |
| 3850 | * New inodes are always orphan at the beginning, so force to use the |
| 3851 | * orphan name in this case. |
| 3852 | * The first ref is stored in valid_path and will be updated if it |
| 3853 | * gets moved around. |
| 3854 | */ |
| 3855 | if (!sctx->cur_inode_new) { |
| 3856 | ret = did_overwrite_first_ref(sctx, sctx->cur_ino, |
| 3857 | sctx->cur_inode_gen); |
| 3858 | if (ret < 0) |
| 3859 | goto out; |
| 3860 | if (ret) |
| 3861 | did_overwrite = 1; |
| 3862 | } |
| 3863 | if (sctx->cur_inode_new || did_overwrite) { |
| 3864 | ret = gen_unique_name(sctx, sctx->cur_ino, |
| 3865 | sctx->cur_inode_gen, valid_path); |
| 3866 | if (ret < 0) |
| 3867 | goto out; |
| 3868 | is_orphan = 1; |
| 3869 | } else { |
| 3870 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 3871 | valid_path); |
| 3872 | if (ret < 0) |
| 3873 | goto out; |
| 3874 | } |
| 3875 | |
| 3876 | list_for_each_entry(cur, &sctx->new_refs, list) { |
| 3877 | /* |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3878 | * We may have refs where the parent directory does not exist |
| 3879 | * yet. This happens if the parent directories inum is higher |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 3880 | * than the current inum. To handle this case, we create the |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3881 | * parent directory out of order. But we need to check if this |
| 3882 | * did already happen before due to other refs in the same dir. |
| 3883 | */ |
| 3884 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
| 3885 | if (ret < 0) |
| 3886 | goto out; |
| 3887 | if (ret == inode_state_will_create) { |
| 3888 | ret = 0; |
| 3889 | /* |
| 3890 | * First check if any of the current inodes refs did |
| 3891 | * already create the dir. |
| 3892 | */ |
| 3893 | list_for_each_entry(cur2, &sctx->new_refs, list) { |
| 3894 | if (cur == cur2) |
| 3895 | break; |
| 3896 | if (cur2->dir == cur->dir) { |
| 3897 | ret = 1; |
| 3898 | break; |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | /* |
| 3903 | * If that did not happen, check if a previous inode |
| 3904 | * did already create the dir. |
| 3905 | */ |
| 3906 | if (!ret) |
| 3907 | ret = did_create_dir(sctx, cur->dir); |
| 3908 | if (ret < 0) |
| 3909 | goto out; |
| 3910 | if (!ret) { |
| 3911 | ret = send_create_inode(sctx, cur->dir); |
| 3912 | if (ret < 0) |
| 3913 | goto out; |
| 3914 | } |
| 3915 | } |
| 3916 | |
| 3917 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3918 | * Check if this new ref would overwrite the first ref of |
| 3919 | * another unprocessed inode. If yes, orphanize the |
| 3920 | * overwritten inode. If we find an overwritten ref that is |
| 3921 | * not the first ref, simply unlink it. |
| 3922 | */ |
| 3923 | ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, |
| 3924 | cur->name, cur->name_len, |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3925 | &ow_inode, &ow_gen, &ow_mode); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3926 | if (ret < 0) |
| 3927 | goto out; |
| 3928 | if (ret) { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3929 | ret = is_first_ref(sctx->parent_root, |
| 3930 | ow_inode, cur->dir, cur->name, |
| 3931 | cur->name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3932 | if (ret < 0) |
| 3933 | goto out; |
| 3934 | if (ret) { |
Filipe Manana | 8996a48 | 2015-03-12 15:16:20 +0000 | [diff] [blame] | 3935 | struct name_cache_entry *nce; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3936 | struct waiting_dir_move *wdm; |
Filipe Manana | 8996a48 | 2015-03-12 15:16:20 +0000 | [diff] [blame] | 3937 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3938 | ret = orphanize_inode(sctx, ow_inode, ow_gen, |
| 3939 | cur->full_path); |
| 3940 | if (ret < 0) |
| 3941 | goto out; |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3942 | if (S_ISDIR(ow_mode)) |
| 3943 | orphanized_dir = true; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3944 | |
| 3945 | /* |
| 3946 | * If ow_inode has its rename operation delayed |
| 3947 | * make sure that its orphanized name is used in |
| 3948 | * the source path when performing its rename |
| 3949 | * operation. |
| 3950 | */ |
| 3951 | if (is_waiting_for_move(sctx, ow_inode)) { |
| 3952 | wdm = get_waiting_dir_move(sctx, |
| 3953 | ow_inode); |
| 3954 | ASSERT(wdm); |
| 3955 | wdm->orphanized = true; |
| 3956 | } |
| 3957 | |
Filipe Manana | 8996a48 | 2015-03-12 15:16:20 +0000 | [diff] [blame] | 3958 | /* |
| 3959 | * Make sure we clear our orphanized inode's |
| 3960 | * name from the name cache. This is because the |
| 3961 | * inode ow_inode might be an ancestor of some |
| 3962 | * other inode that will be orphanized as well |
| 3963 | * later and has an inode number greater than |
| 3964 | * sctx->send_progress. We need to prevent |
| 3965 | * future name lookups from using the old name |
| 3966 | * and get instead the orphan name. |
| 3967 | */ |
| 3968 | nce = name_cache_search(sctx, ow_inode, ow_gen); |
| 3969 | if (nce) { |
| 3970 | name_cache_delete(sctx, nce); |
| 3971 | kfree(nce); |
| 3972 | } |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3973 | |
| 3974 | /* |
| 3975 | * ow_inode might currently be an ancestor of |
| 3976 | * cur_ino, therefore compute valid_path (the |
| 3977 | * current path of cur_ino) again because it |
| 3978 | * might contain the pre-orphanization name of |
| 3979 | * ow_inode, which is no longer valid. |
| 3980 | */ |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3981 | ret = is_ancestor(sctx->parent_root, |
| 3982 | ow_inode, ow_gen, |
| 3983 | sctx->cur_ino, NULL); |
| 3984 | if (ret > 0) { |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 3985 | orphanized_ancestor = true; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3986 | fs_path_reset(valid_path); |
| 3987 | ret = get_cur_path(sctx, sctx->cur_ino, |
| 3988 | sctx->cur_inode_gen, |
| 3989 | valid_path); |
| 3990 | } |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3991 | if (ret < 0) |
| 3992 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3993 | } else { |
| 3994 | ret = send_unlink(sctx, cur->full_path); |
| 3995 | if (ret < 0) |
| 3996 | goto out; |
| 3997 | } |
| 3998 | } |
| 3999 | |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 4000 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) { |
| 4001 | ret = wait_for_dest_dir_move(sctx, cur, is_orphan); |
| 4002 | if (ret < 0) |
| 4003 | goto out; |
| 4004 | if (ret == 1) { |
| 4005 | can_rename = false; |
| 4006 | *pending_move = 1; |
| 4007 | } |
| 4008 | } |
| 4009 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 4010 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root && |
| 4011 | can_rename) { |
| 4012 | ret = wait_for_parent_move(sctx, cur, is_orphan); |
| 4013 | if (ret < 0) |
| 4014 | goto out; |
| 4015 | if (ret == 1) { |
| 4016 | can_rename = false; |
| 4017 | *pending_move = 1; |
| 4018 | } |
| 4019 | } |
| 4020 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4021 | /* |
| 4022 | * link/move the ref to the new place. If we have an orphan |
| 4023 | * inode, move it and update valid_path. If not, link or move |
| 4024 | * it depending on the inode mode. |
| 4025 | */ |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 4026 | if (is_orphan && can_rename) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4027 | ret = send_rename(sctx, valid_path, cur->full_path); |
| 4028 | if (ret < 0) |
| 4029 | goto out; |
| 4030 | is_orphan = 0; |
| 4031 | ret = fs_path_copy(valid_path, cur->full_path); |
| 4032 | if (ret < 0) |
| 4033 | goto out; |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 4034 | } else if (can_rename) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4035 | if (S_ISDIR(sctx->cur_inode_mode)) { |
| 4036 | /* |
| 4037 | * Dirs can't be linked, so move it. For moved |
| 4038 | * dirs, we always have one new and one deleted |
| 4039 | * ref. The deleted ref is ignored later. |
| 4040 | */ |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 4041 | ret = send_rename(sctx, valid_path, |
| 4042 | cur->full_path); |
| 4043 | if (!ret) |
| 4044 | ret = fs_path_copy(valid_path, |
| 4045 | cur->full_path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4046 | if (ret < 0) |
| 4047 | goto out; |
| 4048 | } else { |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 4049 | /* |
| 4050 | * We might have previously orphanized an inode |
| 4051 | * which is an ancestor of our current inode, |
| 4052 | * so our reference's full path, which was |
| 4053 | * computed before any such orphanizations, must |
| 4054 | * be updated. |
| 4055 | */ |
| 4056 | if (orphanized_dir) { |
| 4057 | ret = update_ref_path(sctx, cur); |
| 4058 | if (ret < 0) |
| 4059 | goto out; |
| 4060 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4061 | ret = send_link(sctx, cur->full_path, |
| 4062 | valid_path); |
| 4063 | if (ret < 0) |
| 4064 | goto out; |
| 4065 | } |
| 4066 | } |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4067 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4068 | if (ret < 0) |
| 4069 | goto out; |
| 4070 | } |
| 4071 | |
| 4072 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { |
| 4073 | /* |
| 4074 | * Check if we can already rmdir the directory. If not, |
| 4075 | * orphanize it. For every dir item inside that gets deleted |
| 4076 | * later, we do this check again and rmdir it then if possible. |
| 4077 | * See the use of check_dirs for more details. |
| 4078 | */ |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 4079 | ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 4080 | sctx->cur_ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4081 | if (ret < 0) |
| 4082 | goto out; |
| 4083 | if (ret) { |
| 4084 | ret = send_rmdir(sctx, valid_path); |
| 4085 | if (ret < 0) |
| 4086 | goto out; |
| 4087 | } else if (!is_orphan) { |
| 4088 | ret = orphanize_inode(sctx, sctx->cur_ino, |
| 4089 | sctx->cur_inode_gen, valid_path); |
| 4090 | if (ret < 0) |
| 4091 | goto out; |
| 4092 | is_orphan = 1; |
| 4093 | } |
| 4094 | |
| 4095 | list_for_each_entry(cur, &sctx->deleted_refs, list) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4096 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4097 | if (ret < 0) |
| 4098 | goto out; |
| 4099 | } |
Alexander Block | ccf1626 | 2012-07-28 11:46:29 +0200 | [diff] [blame] | 4100 | } else if (S_ISDIR(sctx->cur_inode_mode) && |
| 4101 | !list_empty(&sctx->deleted_refs)) { |
| 4102 | /* |
| 4103 | * We have a moved dir. Add the old parent to check_dirs |
| 4104 | */ |
| 4105 | cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, |
| 4106 | list); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4107 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | ccf1626 | 2012-07-28 11:46:29 +0200 | [diff] [blame] | 4108 | if (ret < 0) |
| 4109 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4110 | } else if (!S_ISDIR(sctx->cur_inode_mode)) { |
| 4111 | /* |
| 4112 | * We have a non dir inode. Go through all deleted refs and |
| 4113 | * unlink them if they were not already overwritten by other |
| 4114 | * inodes. |
| 4115 | */ |
| 4116 | list_for_each_entry(cur, &sctx->deleted_refs, list) { |
| 4117 | ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, |
| 4118 | sctx->cur_ino, sctx->cur_inode_gen, |
| 4119 | cur->name, cur->name_len); |
| 4120 | if (ret < 0) |
| 4121 | goto out; |
| 4122 | if (!ret) { |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 4123 | /* |
| 4124 | * If we orphanized any ancestor before, we need |
| 4125 | * to recompute the full path for deleted names, |
| 4126 | * since any such path was computed before we |
| 4127 | * processed any references and orphanized any |
| 4128 | * ancestor inode. |
| 4129 | */ |
| 4130 | if (orphanized_ancestor) { |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 4131 | ret = update_ref_path(sctx, cur); |
| 4132 | if (ret < 0) |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 4133 | goto out; |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 4134 | } |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 4135 | ret = send_unlink(sctx, cur->full_path); |
| 4136 | if (ret < 0) |
| 4137 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4138 | } |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4139 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4140 | if (ret < 0) |
| 4141 | goto out; |
| 4142 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4143 | /* |
| 4144 | * If the inode is still orphan, unlink the orphan. This may |
| 4145 | * happen when a previous inode did overwrite the first ref |
| 4146 | * of this inode and no new refs were added for the current |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4147 | * inode. Unlinking does not mean that the inode is deleted in |
| 4148 | * all cases. There may still be links to this inode in other |
| 4149 | * places. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4150 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 4151 | if (is_orphan) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4152 | ret = send_unlink(sctx, valid_path); |
| 4153 | if (ret < 0) |
| 4154 | goto out; |
| 4155 | } |
| 4156 | } |
| 4157 | |
| 4158 | /* |
| 4159 | * We did collect all parent dirs where cur_inode was once located. We |
| 4160 | * now go through all these dirs and check if they are pending for |
| 4161 | * deletion and if it's finally possible to perform the rmdir now. |
| 4162 | * We also update the inode stats of the parent dirs here. |
| 4163 | */ |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4164 | list_for_each_entry(cur, &check_dirs, list) { |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4165 | /* |
| 4166 | * In case we had refs into dirs that were not processed yet, |
| 4167 | * we don't need to do the utime and rmdir logic for these dirs. |
| 4168 | * The dir will be processed later. |
| 4169 | */ |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4170 | if (cur->dir > sctx->cur_ino) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4171 | continue; |
| 4172 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4173 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4174 | if (ret < 0) |
| 4175 | goto out; |
| 4176 | |
| 4177 | if (ret == inode_state_did_create || |
| 4178 | ret == inode_state_no_change) { |
| 4179 | /* TODO delayed utimes */ |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4180 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4181 | if (ret < 0) |
| 4182 | goto out; |
Filipe Manana | 29d6d30 | 2014-02-16 21:01:39 +0000 | [diff] [blame] | 4183 | } else if (ret == inode_state_did_delete && |
| 4184 | cur->dir != last_dir_ino_rm) { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 4185 | ret = can_rmdir(sctx, cur->dir, cur->dir_gen, |
| 4186 | sctx->cur_ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4187 | if (ret < 0) |
| 4188 | goto out; |
| 4189 | if (ret) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4190 | ret = get_cur_path(sctx, cur->dir, |
| 4191 | cur->dir_gen, valid_path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4192 | if (ret < 0) |
| 4193 | goto out; |
| 4194 | ret = send_rmdir(sctx, valid_path); |
| 4195 | if (ret < 0) |
| 4196 | goto out; |
Filipe Manana | 29d6d30 | 2014-02-16 21:01:39 +0000 | [diff] [blame] | 4197 | last_dir_ino_rm = cur->dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4198 | } |
| 4199 | } |
| 4200 | } |
| 4201 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4202 | ret = 0; |
| 4203 | |
| 4204 | out: |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4205 | __free_recorded_refs(&check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4206 | free_recorded_refs(sctx); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4207 | fs_path_free(valid_path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4208 | return ret; |
| 4209 | } |
| 4210 | |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4211 | static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name, |
| 4212 | void *ctx, struct list_head *refs) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4213 | { |
| 4214 | int ret = 0; |
| 4215 | struct send_ctx *sctx = ctx; |
| 4216 | struct fs_path *p; |
| 4217 | u64 gen; |
| 4218 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4219 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4220 | if (!p) |
| 4221 | return -ENOMEM; |
| 4222 | |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 4223 | ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 4224 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4225 | if (ret < 0) |
| 4226 | goto out; |
| 4227 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4228 | ret = get_cur_path(sctx, dir, gen, p); |
| 4229 | if (ret < 0) |
| 4230 | goto out; |
| 4231 | ret = fs_path_add_path(p, name); |
| 4232 | if (ret < 0) |
| 4233 | goto out; |
| 4234 | |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 4235 | ret = __record_ref(refs, dir, gen, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4236 | |
| 4237 | out: |
| 4238 | if (ret) |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4239 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4240 | return ret; |
| 4241 | } |
| 4242 | |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 4243 | static int __record_new_ref(int num, u64 dir, int index, |
| 4244 | struct fs_path *name, |
| 4245 | void *ctx) |
| 4246 | { |
| 4247 | struct send_ctx *sctx = ctx; |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4248 | return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs); |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4252 | static int __record_deleted_ref(int num, u64 dir, int index, |
| 4253 | struct fs_path *name, |
| 4254 | void *ctx) |
| 4255 | { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4256 | struct send_ctx *sctx = ctx; |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4257 | return record_ref(sctx->parent_root, dir, name, ctx, |
| 4258 | &sctx->deleted_refs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | static int record_new_ref(struct send_ctx *sctx) |
| 4262 | { |
| 4263 | int ret; |
| 4264 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4265 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
| 4266 | sctx->cmp_key, 0, __record_new_ref, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4267 | if (ret < 0) |
| 4268 | goto out; |
| 4269 | ret = 0; |
| 4270 | |
| 4271 | out: |
| 4272 | return ret; |
| 4273 | } |
| 4274 | |
| 4275 | static int record_deleted_ref(struct send_ctx *sctx) |
| 4276 | { |
| 4277 | int ret; |
| 4278 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4279 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
| 4280 | sctx->cmp_key, 0, __record_deleted_ref, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4281 | if (ret < 0) |
| 4282 | goto out; |
| 4283 | ret = 0; |
| 4284 | |
| 4285 | out: |
| 4286 | return ret; |
| 4287 | } |
| 4288 | |
| 4289 | struct find_ref_ctx { |
| 4290 | u64 dir; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4291 | u64 dir_gen; |
| 4292 | struct btrfs_root *root; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4293 | struct fs_path *name; |
| 4294 | int found_idx; |
| 4295 | }; |
| 4296 | |
| 4297 | static int __find_iref(int num, u64 dir, int index, |
| 4298 | struct fs_path *name, |
| 4299 | void *ctx_) |
| 4300 | { |
| 4301 | struct find_ref_ctx *ctx = ctx_; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4302 | u64 dir_gen; |
| 4303 | int ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4304 | |
| 4305 | if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) && |
| 4306 | strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4307 | /* |
| 4308 | * To avoid doing extra lookups we'll only do this if everything |
| 4309 | * else matches. |
| 4310 | */ |
| 4311 | ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL, |
| 4312 | NULL, NULL, NULL); |
| 4313 | if (ret) |
| 4314 | return ret; |
| 4315 | if (dir_gen != ctx->dir_gen) |
| 4316 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4317 | ctx->found_idx = num; |
| 4318 | return 1; |
| 4319 | } |
| 4320 | return 0; |
| 4321 | } |
| 4322 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4323 | static int find_iref(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4324 | struct btrfs_path *path, |
| 4325 | struct btrfs_key *key, |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4326 | u64 dir, u64 dir_gen, struct fs_path *name) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4327 | { |
| 4328 | int ret; |
| 4329 | struct find_ref_ctx ctx; |
| 4330 | |
| 4331 | ctx.dir = dir; |
| 4332 | ctx.name = name; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4333 | ctx.dir_gen = dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4334 | ctx.found_idx = -1; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4335 | ctx.root = root; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4336 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4337 | ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4338 | if (ret < 0) |
| 4339 | return ret; |
| 4340 | |
| 4341 | if (ctx.found_idx == -1) |
| 4342 | return -ENOENT; |
| 4343 | |
| 4344 | return ctx.found_idx; |
| 4345 | } |
| 4346 | |
| 4347 | static int __record_changed_new_ref(int num, u64 dir, int index, |
| 4348 | struct fs_path *name, |
| 4349 | void *ctx) |
| 4350 | { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4351 | u64 dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4352 | int ret; |
| 4353 | struct send_ctx *sctx = ctx; |
| 4354 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4355 | ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL, |
| 4356 | NULL, NULL, NULL); |
| 4357 | if (ret) |
| 4358 | return ret; |
| 4359 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4360 | ret = find_iref(sctx->parent_root, sctx->right_path, |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4361 | sctx->cmp_key, dir, dir_gen, name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4362 | if (ret == -ENOENT) |
| 4363 | ret = __record_new_ref(num, dir, index, name, sctx); |
| 4364 | else if (ret > 0) |
| 4365 | ret = 0; |
| 4366 | |
| 4367 | return ret; |
| 4368 | } |
| 4369 | |
| 4370 | static int __record_changed_deleted_ref(int num, u64 dir, int index, |
| 4371 | struct fs_path *name, |
| 4372 | void *ctx) |
| 4373 | { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4374 | u64 dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4375 | int ret; |
| 4376 | struct send_ctx *sctx = ctx; |
| 4377 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4378 | ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL, |
| 4379 | NULL, NULL, NULL); |
| 4380 | if (ret) |
| 4381 | return ret; |
| 4382 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4383 | ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key, |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4384 | dir, dir_gen, name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4385 | if (ret == -ENOENT) |
| 4386 | ret = __record_deleted_ref(num, dir, index, name, sctx); |
| 4387 | else if (ret > 0) |
| 4388 | ret = 0; |
| 4389 | |
| 4390 | return ret; |
| 4391 | } |
| 4392 | |
| 4393 | static int record_changed_ref(struct send_ctx *sctx) |
| 4394 | { |
| 4395 | int ret = 0; |
| 4396 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4397 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4398 | sctx->cmp_key, 0, __record_changed_new_ref, sctx); |
| 4399 | if (ret < 0) |
| 4400 | goto out; |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4401 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4402 | sctx->cmp_key, 0, __record_changed_deleted_ref, sctx); |
| 4403 | if (ret < 0) |
| 4404 | goto out; |
| 4405 | ret = 0; |
| 4406 | |
| 4407 | out: |
| 4408 | return ret; |
| 4409 | } |
| 4410 | |
| 4411 | /* |
| 4412 | * Record and process all refs at once. Needed when an inode changes the |
| 4413 | * generation number, which means that it was deleted and recreated. |
| 4414 | */ |
| 4415 | static int process_all_refs(struct send_ctx *sctx, |
| 4416 | enum btrfs_compare_tree_result cmd) |
| 4417 | { |
| 4418 | int ret; |
| 4419 | struct btrfs_root *root; |
| 4420 | struct btrfs_path *path; |
| 4421 | struct btrfs_key key; |
| 4422 | struct btrfs_key found_key; |
| 4423 | struct extent_buffer *eb; |
| 4424 | int slot; |
| 4425 | iterate_inode_ref_t cb; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4426 | int pending_move = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4427 | |
| 4428 | path = alloc_path_for_send(); |
| 4429 | if (!path) |
| 4430 | return -ENOMEM; |
| 4431 | |
| 4432 | if (cmd == BTRFS_COMPARE_TREE_NEW) { |
| 4433 | root = sctx->send_root; |
| 4434 | cb = __record_new_ref; |
| 4435 | } else if (cmd == BTRFS_COMPARE_TREE_DELETED) { |
| 4436 | root = sctx->parent_root; |
| 4437 | cb = __record_deleted_ref; |
| 4438 | } else { |
David Sterba | 4d1a63b | 2014-02-03 19:24:19 +0100 | [diff] [blame] | 4439 | btrfs_err(sctx->send_root->fs_info, |
| 4440 | "Wrong command %d in process_all_refs", cmd); |
| 4441 | ret = -EINVAL; |
| 4442 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | key.objectid = sctx->cmp_key->objectid; |
| 4446 | key.type = BTRFS_INODE_REF_KEY; |
| 4447 | key.offset = 0; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4448 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4449 | if (ret < 0) |
| 4450 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4451 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4452 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4453 | eb = path->nodes[0]; |
| 4454 | slot = path->slots[0]; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4455 | if (slot >= btrfs_header_nritems(eb)) { |
| 4456 | ret = btrfs_next_leaf(root, path); |
| 4457 | if (ret < 0) |
| 4458 | goto out; |
| 4459 | else if (ret > 0) |
| 4460 | break; |
| 4461 | continue; |
| 4462 | } |
| 4463 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4464 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 4465 | |
| 4466 | if (found_key.objectid != key.objectid || |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 4467 | (found_key.type != BTRFS_INODE_REF_KEY && |
| 4468 | found_key.type != BTRFS_INODE_EXTREF_KEY)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4469 | break; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4470 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4471 | ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4472 | if (ret < 0) |
| 4473 | goto out; |
| 4474 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4475 | path->slots[0]++; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4476 | } |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4477 | btrfs_release_path(path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4478 | |
Josef Bacik | 3dc09ec | 2016-08-24 11:57:52 -0400 | [diff] [blame] | 4479 | /* |
| 4480 | * We don't actually care about pending_move as we are simply |
| 4481 | * re-creating this inode and will be rename'ing it into place once we |
| 4482 | * rename the parent directory. |
| 4483 | */ |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4484 | ret = process_recorded_refs(sctx, &pending_move); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4485 | out: |
| 4486 | btrfs_free_path(path); |
| 4487 | return ret; |
| 4488 | } |
| 4489 | |
| 4490 | static int send_set_xattr(struct send_ctx *sctx, |
| 4491 | struct fs_path *path, |
| 4492 | const char *name, int name_len, |
| 4493 | const char *data, int data_len) |
| 4494 | { |
| 4495 | int ret = 0; |
| 4496 | |
| 4497 | ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); |
| 4498 | if (ret < 0) |
| 4499 | goto out; |
| 4500 | |
| 4501 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 4502 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); |
| 4503 | TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); |
| 4504 | |
| 4505 | ret = send_cmd(sctx); |
| 4506 | |
| 4507 | tlv_put_failure: |
| 4508 | out: |
| 4509 | return ret; |
| 4510 | } |
| 4511 | |
| 4512 | static int send_remove_xattr(struct send_ctx *sctx, |
| 4513 | struct fs_path *path, |
| 4514 | const char *name, int name_len) |
| 4515 | { |
| 4516 | int ret = 0; |
| 4517 | |
| 4518 | ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); |
| 4519 | if (ret < 0) |
| 4520 | goto out; |
| 4521 | |
| 4522 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 4523 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); |
| 4524 | |
| 4525 | ret = send_cmd(sctx); |
| 4526 | |
| 4527 | tlv_put_failure: |
| 4528 | out: |
| 4529 | return ret; |
| 4530 | } |
| 4531 | |
| 4532 | static int __process_new_xattr(int num, struct btrfs_key *di_key, |
| 4533 | const char *name, int name_len, |
| 4534 | const char *data, int data_len, |
| 4535 | u8 type, void *ctx) |
| 4536 | { |
| 4537 | int ret; |
| 4538 | struct send_ctx *sctx = ctx; |
| 4539 | struct fs_path *p; |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 4540 | struct posix_acl_xattr_header dummy_acl; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4541 | |
Marcos Paulo de Souza | 89efda5 | 2020-05-10 23:15:07 -0300 | [diff] [blame] | 4542 | /* Capabilities are emitted by finish_inode_if_needed */ |
| 4543 | if (!strncmp(name, XATTR_NAME_CAPS, name_len)) |
| 4544 | return 0; |
| 4545 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4546 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4547 | if (!p) |
| 4548 | return -ENOMEM; |
| 4549 | |
| 4550 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4551 | * This hack is needed because empty acls are stored as zero byte |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4552 | * data in xattrs. Problem with that is, that receiving these zero byte |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4553 | * acls will fail later. To fix this, we send a dummy acl list that |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4554 | * only contains the version number and no entries. |
| 4555 | */ |
| 4556 | if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) || |
| 4557 | !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) { |
| 4558 | if (data_len == 0) { |
| 4559 | dummy_acl.a_version = |
| 4560 | cpu_to_le32(POSIX_ACL_XATTR_VERSION); |
| 4561 | data = (char *)&dummy_acl; |
| 4562 | data_len = sizeof(dummy_acl); |
| 4563 | } |
| 4564 | } |
| 4565 | |
| 4566 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4567 | if (ret < 0) |
| 4568 | goto out; |
| 4569 | |
| 4570 | ret = send_set_xattr(sctx, p, name, name_len, data, data_len); |
| 4571 | |
| 4572 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4573 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4574 | return ret; |
| 4575 | } |
| 4576 | |
| 4577 | static int __process_deleted_xattr(int num, struct btrfs_key *di_key, |
| 4578 | const char *name, int name_len, |
| 4579 | const char *data, int data_len, |
| 4580 | u8 type, void *ctx) |
| 4581 | { |
| 4582 | int ret; |
| 4583 | struct send_ctx *sctx = ctx; |
| 4584 | struct fs_path *p; |
| 4585 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4586 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4587 | if (!p) |
| 4588 | return -ENOMEM; |
| 4589 | |
| 4590 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4591 | if (ret < 0) |
| 4592 | goto out; |
| 4593 | |
| 4594 | ret = send_remove_xattr(sctx, p, name, name_len); |
| 4595 | |
| 4596 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4597 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4598 | return ret; |
| 4599 | } |
| 4600 | |
| 4601 | static int process_new_xattr(struct send_ctx *sctx) |
| 4602 | { |
| 4603 | int ret = 0; |
| 4604 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4605 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4606 | __process_new_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4607 | |
| 4608 | return ret; |
| 4609 | } |
| 4610 | |
| 4611 | static int process_deleted_xattr(struct send_ctx *sctx) |
| 4612 | { |
Masahiro Yamada | e2c8990 | 2016-09-13 04:35:52 +0900 | [diff] [blame] | 4613 | return iterate_dir_item(sctx->parent_root, sctx->right_path, |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4614 | __process_deleted_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | struct find_xattr_ctx { |
| 4618 | const char *name; |
| 4619 | int name_len; |
| 4620 | int found_idx; |
| 4621 | char *found_data; |
| 4622 | int found_data_len; |
| 4623 | }; |
| 4624 | |
| 4625 | static int __find_xattr(int num, struct btrfs_key *di_key, |
| 4626 | const char *name, int name_len, |
| 4627 | const char *data, int data_len, |
| 4628 | u8 type, void *vctx) |
| 4629 | { |
| 4630 | struct find_xattr_ctx *ctx = vctx; |
| 4631 | |
| 4632 | if (name_len == ctx->name_len && |
| 4633 | strncmp(name, ctx->name, name_len) == 0) { |
| 4634 | ctx->found_idx = num; |
| 4635 | ctx->found_data_len = data_len; |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 4636 | ctx->found_data = kmemdup(data, data_len, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4637 | if (!ctx->found_data) |
| 4638 | return -ENOMEM; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4639 | return 1; |
| 4640 | } |
| 4641 | return 0; |
| 4642 | } |
| 4643 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4644 | static int find_xattr(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4645 | struct btrfs_path *path, |
| 4646 | struct btrfs_key *key, |
| 4647 | const char *name, int name_len, |
| 4648 | char **data, int *data_len) |
| 4649 | { |
| 4650 | int ret; |
| 4651 | struct find_xattr_ctx ctx; |
| 4652 | |
| 4653 | ctx.name = name; |
| 4654 | ctx.name_len = name_len; |
| 4655 | ctx.found_idx = -1; |
| 4656 | ctx.found_data = NULL; |
| 4657 | ctx.found_data_len = 0; |
| 4658 | |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4659 | ret = iterate_dir_item(root, path, __find_xattr, &ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4660 | if (ret < 0) |
| 4661 | return ret; |
| 4662 | |
| 4663 | if (ctx.found_idx == -1) |
| 4664 | return -ENOENT; |
| 4665 | if (data) { |
| 4666 | *data = ctx.found_data; |
| 4667 | *data_len = ctx.found_data_len; |
| 4668 | } else { |
| 4669 | kfree(ctx.found_data); |
| 4670 | } |
| 4671 | return ctx.found_idx; |
| 4672 | } |
| 4673 | |
| 4674 | |
| 4675 | static int __process_changed_new_xattr(int num, struct btrfs_key *di_key, |
| 4676 | const char *name, int name_len, |
| 4677 | const char *data, int data_len, |
| 4678 | u8 type, void *ctx) |
| 4679 | { |
| 4680 | int ret; |
| 4681 | struct send_ctx *sctx = ctx; |
| 4682 | char *found_data = NULL; |
| 4683 | int found_data_len = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4684 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4685 | ret = find_xattr(sctx->parent_root, sctx->right_path, |
| 4686 | sctx->cmp_key, name, name_len, &found_data, |
| 4687 | &found_data_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4688 | if (ret == -ENOENT) { |
| 4689 | ret = __process_new_xattr(num, di_key, name, name_len, data, |
| 4690 | data_len, type, ctx); |
| 4691 | } else if (ret >= 0) { |
| 4692 | if (data_len != found_data_len || |
| 4693 | memcmp(data, found_data, data_len)) { |
| 4694 | ret = __process_new_xattr(num, di_key, name, name_len, |
| 4695 | data, data_len, type, ctx); |
| 4696 | } else { |
| 4697 | ret = 0; |
| 4698 | } |
| 4699 | } |
| 4700 | |
| 4701 | kfree(found_data); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4702 | return ret; |
| 4703 | } |
| 4704 | |
| 4705 | static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key, |
| 4706 | const char *name, int name_len, |
| 4707 | const char *data, int data_len, |
| 4708 | u8 type, void *ctx) |
| 4709 | { |
| 4710 | int ret; |
| 4711 | struct send_ctx *sctx = ctx; |
| 4712 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4713 | ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key, |
| 4714 | name, name_len, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4715 | if (ret == -ENOENT) |
| 4716 | ret = __process_deleted_xattr(num, di_key, name, name_len, data, |
| 4717 | data_len, type, ctx); |
| 4718 | else if (ret >= 0) |
| 4719 | ret = 0; |
| 4720 | |
| 4721 | return ret; |
| 4722 | } |
| 4723 | |
| 4724 | static int process_changed_xattr(struct send_ctx *sctx) |
| 4725 | { |
| 4726 | int ret = 0; |
| 4727 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4728 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4729 | __process_changed_new_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4730 | if (ret < 0) |
| 4731 | goto out; |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4732 | ret = iterate_dir_item(sctx->parent_root, sctx->right_path, |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4733 | __process_changed_deleted_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4734 | |
| 4735 | out: |
| 4736 | return ret; |
| 4737 | } |
| 4738 | |
| 4739 | static int process_all_new_xattrs(struct send_ctx *sctx) |
| 4740 | { |
| 4741 | int ret; |
| 4742 | struct btrfs_root *root; |
| 4743 | struct btrfs_path *path; |
| 4744 | struct btrfs_key key; |
| 4745 | struct btrfs_key found_key; |
| 4746 | struct extent_buffer *eb; |
| 4747 | int slot; |
| 4748 | |
| 4749 | path = alloc_path_for_send(); |
| 4750 | if (!path) |
| 4751 | return -ENOMEM; |
| 4752 | |
| 4753 | root = sctx->send_root; |
| 4754 | |
| 4755 | key.objectid = sctx->cmp_key->objectid; |
| 4756 | key.type = BTRFS_XATTR_ITEM_KEY; |
| 4757 | key.offset = 0; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4758 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4759 | if (ret < 0) |
| 4760 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4761 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4762 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4763 | eb = path->nodes[0]; |
| 4764 | slot = path->slots[0]; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4765 | if (slot >= btrfs_header_nritems(eb)) { |
| 4766 | ret = btrfs_next_leaf(root, path); |
| 4767 | if (ret < 0) { |
| 4768 | goto out; |
| 4769 | } else if (ret > 0) { |
| 4770 | ret = 0; |
| 4771 | break; |
| 4772 | } |
| 4773 | continue; |
| 4774 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4775 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4776 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4777 | if (found_key.objectid != key.objectid || |
| 4778 | found_key.type != key.type) { |
| 4779 | ret = 0; |
| 4780 | goto out; |
| 4781 | } |
| 4782 | |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4783 | ret = iterate_dir_item(root, path, __process_new_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4784 | if (ret < 0) |
| 4785 | goto out; |
| 4786 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4787 | path->slots[0]++; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | out: |
| 4791 | btrfs_free_path(path); |
| 4792 | return ret; |
| 4793 | } |
| 4794 | |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 4795 | static inline u64 max_send_read_size(const struct send_ctx *sctx) |
| 4796 | { |
| 4797 | return sctx->send_max_size - SZ_16K; |
| 4798 | } |
| 4799 | |
| 4800 | static int put_data_header(struct send_ctx *sctx, u32 len) |
| 4801 | { |
| 4802 | struct btrfs_tlv_header *hdr; |
| 4803 | |
| 4804 | if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len) |
| 4805 | return -EOVERFLOW; |
| 4806 | hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size); |
| 4807 | put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type); |
| 4808 | put_unaligned_le16(len, &hdr->tlv_len); |
| 4809 | sctx->send_size += sizeof(*hdr); |
| 4810 | return 0; |
| 4811 | } |
| 4812 | |
| 4813 | static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len) |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4814 | { |
| 4815 | struct btrfs_root *root = sctx->send_root; |
| 4816 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4817 | struct inode *inode; |
| 4818 | struct page *page; |
| 4819 | char *addr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4820 | pgoff_t index = offset >> PAGE_SHIFT; |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4821 | pgoff_t last_index; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 4822 | unsigned pg_offset = offset_in_page(offset); |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 4823 | int ret; |
| 4824 | |
| 4825 | ret = put_data_header(sctx, len); |
| 4826 | if (ret) |
| 4827 | return ret; |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4828 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 4829 | inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4830 | if (IS_ERR(inode)) |
| 4831 | return PTR_ERR(inode); |
| 4832 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4833 | last_index = (offset + len - 1) >> PAGE_SHIFT; |
Liu Bo | 2131bcd | 2014-03-05 10:07:35 +0800 | [diff] [blame] | 4834 | |
| 4835 | /* initial readahead */ |
| 4836 | memset(&sctx->ra, 0, sizeof(struct file_ra_state)); |
| 4837 | file_ra_state_init(&sctx->ra, inode->i_mapping); |
Liu Bo | 2131bcd | 2014-03-05 10:07:35 +0800 | [diff] [blame] | 4838 | |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4839 | while (index <= last_index) { |
| 4840 | unsigned cur_len = min_t(unsigned, len, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4841 | PAGE_SIZE - pg_offset); |
Kuanling Huang | eef16ba | 2017-09-15 16:47:45 +0800 | [diff] [blame] | 4842 | |
| 4843 | page = find_lock_page(inode->i_mapping, index); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4844 | if (!page) { |
Kuanling Huang | eef16ba | 2017-09-15 16:47:45 +0800 | [diff] [blame] | 4845 | page_cache_sync_readahead(inode->i_mapping, &sctx->ra, |
| 4846 | NULL, index, last_index + 1 - index); |
| 4847 | |
| 4848 | page = find_or_create_page(inode->i_mapping, index, |
| 4849 | GFP_KERNEL); |
| 4850 | if (!page) { |
| 4851 | ret = -ENOMEM; |
| 4852 | break; |
| 4853 | } |
| 4854 | } |
| 4855 | |
| 4856 | if (PageReadahead(page)) { |
| 4857 | page_cache_async_readahead(inode->i_mapping, &sctx->ra, |
| 4858 | NULL, page, index, last_index + 1 - index); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | if (!PageUptodate(page)) { |
| 4862 | btrfs_readpage(NULL, page); |
| 4863 | lock_page(page); |
| 4864 | if (!PageUptodate(page)) { |
| 4865 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4866 | put_page(page); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4867 | ret = -EIO; |
| 4868 | break; |
| 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | addr = kmap(page); |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 4873 | memcpy(sctx->send_buf + sctx->send_size, addr + pg_offset, |
| 4874 | cur_len); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4875 | kunmap(page); |
| 4876 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4877 | put_page(page); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4878 | index++; |
| 4879 | pg_offset = 0; |
| 4880 | len -= cur_len; |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 4881 | sctx->send_size += cur_len; |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4882 | } |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4883 | iput(inode); |
| 4884 | return ret; |
| 4885 | } |
| 4886 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4887 | /* |
| 4888 | * Read some bytes from the current inode/file and send a write command to |
| 4889 | * user space. |
| 4890 | */ |
| 4891 | static int send_write(struct send_ctx *sctx, u64 offset, u32 len) |
| 4892 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 4893 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4894 | int ret = 0; |
| 4895 | struct fs_path *p; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4896 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4897 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4898 | if (!p) |
| 4899 | return -ENOMEM; |
| 4900 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 4901 | btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4902 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4903 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); |
| 4904 | if (ret < 0) |
| 4905 | goto out; |
| 4906 | |
| 4907 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4908 | if (ret < 0) |
| 4909 | goto out; |
| 4910 | |
| 4911 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 4912 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 4913 | ret = put_file_data(sctx, offset, len); |
| 4914 | if (ret < 0) |
| 4915 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4916 | |
| 4917 | ret = send_cmd(sctx); |
| 4918 | |
| 4919 | tlv_put_failure: |
| 4920 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4921 | fs_path_free(p); |
Omar Sandoval | a9b2e0d | 2020-08-21 00:39:51 -0700 | [diff] [blame] | 4922 | return ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | /* |
| 4926 | * Send a clone command to user space. |
| 4927 | */ |
| 4928 | static int send_clone(struct send_ctx *sctx, |
| 4929 | u64 offset, u32 len, |
| 4930 | struct clone_root *clone_root) |
| 4931 | { |
| 4932 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4933 | struct fs_path *p; |
| 4934 | u64 gen; |
| 4935 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 4936 | btrfs_debug(sctx->send_root->fs_info, |
| 4937 | "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu", |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 4938 | offset, len, clone_root->root->root_key.objectid, |
| 4939 | clone_root->ino, clone_root->offset); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4940 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4941 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4942 | if (!p) |
| 4943 | return -ENOMEM; |
| 4944 | |
| 4945 | ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); |
| 4946 | if (ret < 0) |
| 4947 | goto out; |
| 4948 | |
| 4949 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4950 | if (ret < 0) |
| 4951 | goto out; |
| 4952 | |
| 4953 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 4954 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); |
| 4955 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 4956 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4957 | if (clone_root->root == sctx->send_root) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4958 | ret = get_inode_info(sctx->send_root, clone_root->ino, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 4959 | &gen, NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4960 | if (ret < 0) |
| 4961 | goto out; |
| 4962 | ret = get_cur_path(sctx, clone_root->ino, gen, p); |
| 4963 | } else { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4964 | ret = get_inode_path(clone_root->root, clone_root->ino, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4965 | } |
| 4966 | if (ret < 0) |
| 4967 | goto out; |
| 4968 | |
Josef Bacik | 37b8d27 | 2015-06-04 17:17:25 -0400 | [diff] [blame] | 4969 | /* |
| 4970 | * If the parent we're using has a received_uuid set then use that as |
| 4971 | * our clone source as that is what we will look for when doing a |
| 4972 | * receive. |
| 4973 | * |
| 4974 | * This covers the case that we create a snapshot off of a received |
| 4975 | * subvolume and then use that as the parent and try to receive on a |
| 4976 | * different host. |
| 4977 | */ |
| 4978 | if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid)) |
| 4979 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 4980 | clone_root->root->root_item.received_uuid); |
| 4981 | else |
| 4982 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 4983 | clone_root->root->root_item.uuid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4984 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
Filipe David Borba Manana | 5a0f4e2 | 2013-12-03 15:55:48 +0000 | [diff] [blame] | 4985 | le64_to_cpu(clone_root->root->root_item.ctransid)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4986 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); |
| 4987 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, |
| 4988 | clone_root->offset); |
| 4989 | |
| 4990 | ret = send_cmd(sctx); |
| 4991 | |
| 4992 | tlv_put_failure: |
| 4993 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4994 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4995 | return ret; |
| 4996 | } |
| 4997 | |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 4998 | /* |
| 4999 | * Send an update extent command to user space. |
| 5000 | */ |
| 5001 | static int send_update_extent(struct send_ctx *sctx, |
| 5002 | u64 offset, u32 len) |
| 5003 | { |
| 5004 | int ret = 0; |
| 5005 | struct fs_path *p; |
| 5006 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 5007 | p = fs_path_alloc(); |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 5008 | if (!p) |
| 5009 | return -ENOMEM; |
| 5010 | |
| 5011 | ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT); |
| 5012 | if (ret < 0) |
| 5013 | goto out; |
| 5014 | |
| 5015 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 5016 | if (ret < 0) |
| 5017 | goto out; |
| 5018 | |
| 5019 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 5020 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 5021 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len); |
| 5022 | |
| 5023 | ret = send_cmd(sctx); |
| 5024 | |
| 5025 | tlv_put_failure: |
| 5026 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 5027 | fs_path_free(p); |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 5028 | return ret; |
| 5029 | } |
| 5030 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5031 | static int send_hole(struct send_ctx *sctx, u64 end) |
| 5032 | { |
| 5033 | struct fs_path *p = NULL; |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 5034 | u64 read_size = max_send_read_size(sctx); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5035 | u64 offset = sctx->cur_inode_last_extent; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5036 | int ret = 0; |
| 5037 | |
Filipe Manana | 22d3151 | 2018-07-30 12:39:58 +0100 | [diff] [blame] | 5038 | /* |
| 5039 | * A hole that starts at EOF or beyond it. Since we do not yet support |
| 5040 | * fallocate (for extent preallocation and hole punching), sending a |
| 5041 | * write of zeroes starting at EOF or beyond would later require issuing |
| 5042 | * a truncate operation which would undo the write and achieve nothing. |
| 5043 | */ |
| 5044 | if (offset >= sctx->cur_inode_size) |
| 5045 | return 0; |
| 5046 | |
Filipe Manana | 6b1f72e5 | 2019-05-20 09:55:42 +0100 | [diff] [blame] | 5047 | /* |
| 5048 | * Don't go beyond the inode's i_size due to prealloc extents that start |
| 5049 | * after the i_size. |
| 5050 | */ |
| 5051 | end = min_t(u64, end, sctx->cur_inode_size); |
| 5052 | |
Filipe Manana | d4dfc0f | 2018-02-06 20:39:20 +0000 | [diff] [blame] | 5053 | if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) |
| 5054 | return send_update_extent(sctx, offset, end - offset); |
| 5055 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5056 | p = fs_path_alloc(); |
| 5057 | if (!p) |
| 5058 | return -ENOMEM; |
Filipe Manana | c715e15 | 2014-03-31 14:52:14 +0100 | [diff] [blame] | 5059 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 5060 | if (ret < 0) |
| 5061 | goto tlv_put_failure; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5062 | while (offset < end) { |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 5063 | u64 len = min(end - offset, read_size); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5064 | |
| 5065 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); |
| 5066 | if (ret < 0) |
| 5067 | break; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5068 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 5069 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 5070 | ret = put_data_header(sctx, len); |
| 5071 | if (ret < 0) |
| 5072 | break; |
| 5073 | memset(sctx->send_buf + sctx->send_size, 0, len); |
| 5074 | sctx->send_size += len; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5075 | ret = send_cmd(sctx); |
| 5076 | if (ret < 0) |
| 5077 | break; |
| 5078 | offset += len; |
| 5079 | } |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5080 | sctx->cur_inode_next_write_offset = offset; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5081 | tlv_put_failure: |
| 5082 | fs_path_free(p); |
| 5083 | return ret; |
| 5084 | } |
| 5085 | |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5086 | static int send_extent_data(struct send_ctx *sctx, |
| 5087 | const u64 offset, |
| 5088 | const u64 len) |
| 5089 | { |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 5090 | u64 read_size = max_send_read_size(sctx); |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5091 | u64 sent = 0; |
| 5092 | |
| 5093 | if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) |
| 5094 | return send_update_extent(sctx, offset, len); |
| 5095 | |
| 5096 | while (sent < len) { |
Omar Sandoval | 8c7d9fe | 2020-08-21 00:39:52 -0700 | [diff] [blame] | 5097 | u64 size = min(len - sent, read_size); |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5098 | int ret; |
| 5099 | |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5100 | ret = send_write(sctx, offset + sent, size); |
| 5101 | if (ret < 0) |
| 5102 | return ret; |
Omar Sandoval | a9b2e0d | 2020-08-21 00:39:51 -0700 | [diff] [blame] | 5103 | sent += size; |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5104 | } |
| 5105 | return 0; |
| 5106 | } |
| 5107 | |
Marcos Paulo de Souza | 89efda5 | 2020-05-10 23:15:07 -0300 | [diff] [blame] | 5108 | /* |
| 5109 | * Search for a capability xattr related to sctx->cur_ino. If the capability is |
| 5110 | * found, call send_set_xattr function to emit it. |
| 5111 | * |
| 5112 | * Return 0 if there isn't a capability, or when the capability was emitted |
| 5113 | * successfully, or < 0 if an error occurred. |
| 5114 | */ |
| 5115 | static int send_capabilities(struct send_ctx *sctx) |
| 5116 | { |
| 5117 | struct fs_path *fspath = NULL; |
| 5118 | struct btrfs_path *path; |
| 5119 | struct btrfs_dir_item *di; |
| 5120 | struct extent_buffer *leaf; |
| 5121 | unsigned long data_ptr; |
| 5122 | char *buf = NULL; |
| 5123 | int buf_len; |
| 5124 | int ret = 0; |
| 5125 | |
| 5126 | path = alloc_path_for_send(); |
| 5127 | if (!path) |
| 5128 | return -ENOMEM; |
| 5129 | |
| 5130 | di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino, |
| 5131 | XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0); |
| 5132 | if (!di) { |
| 5133 | /* There is no xattr for this inode */ |
| 5134 | goto out; |
| 5135 | } else if (IS_ERR(di)) { |
| 5136 | ret = PTR_ERR(di); |
| 5137 | goto out; |
| 5138 | } |
| 5139 | |
| 5140 | leaf = path->nodes[0]; |
| 5141 | buf_len = btrfs_dir_data_len(leaf, di); |
| 5142 | |
| 5143 | fspath = fs_path_alloc(); |
| 5144 | buf = kmalloc(buf_len, GFP_KERNEL); |
| 5145 | if (!fspath || !buf) { |
| 5146 | ret = -ENOMEM; |
| 5147 | goto out; |
| 5148 | } |
| 5149 | |
| 5150 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath); |
| 5151 | if (ret < 0) |
| 5152 | goto out; |
| 5153 | |
| 5154 | data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di); |
| 5155 | read_extent_buffer(leaf, buf, data_ptr, buf_len); |
| 5156 | |
| 5157 | ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS, |
| 5158 | strlen(XATTR_NAME_CAPS), buf, buf_len); |
| 5159 | out: |
| 5160 | kfree(buf); |
| 5161 | fs_path_free(fspath); |
| 5162 | btrfs_free_path(path); |
| 5163 | return ret; |
| 5164 | } |
| 5165 | |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5166 | static int clone_range(struct send_ctx *sctx, |
| 5167 | struct clone_root *clone_root, |
| 5168 | const u64 disk_byte, |
| 5169 | u64 data_offset, |
| 5170 | u64 offset, |
| 5171 | u64 len) |
| 5172 | { |
| 5173 | struct btrfs_path *path; |
| 5174 | struct btrfs_key key; |
| 5175 | int ret; |
Austin Kim | 431d398 | 2019-09-03 12:30:19 +0900 | [diff] [blame] | 5176 | u64 clone_src_i_size = 0; |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5177 | |
Filipe Manana | 72610b1 | 2017-08-10 22:54:51 +0100 | [diff] [blame] | 5178 | /* |
| 5179 | * Prevent cloning from a zero offset with a length matching the sector |
| 5180 | * size because in some scenarios this will make the receiver fail. |
| 5181 | * |
| 5182 | * For example, if in the source filesystem the extent at offset 0 |
| 5183 | * has a length of sectorsize and it was written using direct IO, then |
| 5184 | * it can never be an inline extent (even if compression is enabled). |
| 5185 | * Then this extent can be cloned in the original filesystem to a non |
| 5186 | * zero file offset, but it may not be possible to clone in the |
| 5187 | * destination filesystem because it can be inlined due to compression |
| 5188 | * on the destination filesystem (as the receiver's write operations are |
| 5189 | * always done using buffered IO). The same happens when the original |
| 5190 | * filesystem does not have compression enabled but the destination |
| 5191 | * filesystem has. |
| 5192 | */ |
| 5193 | if (clone_root->offset == 0 && |
| 5194 | len == sctx->send_root->fs_info->sectorsize) |
| 5195 | return send_extent_data(sctx, offset, len); |
| 5196 | |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5197 | path = alloc_path_for_send(); |
| 5198 | if (!path) |
| 5199 | return -ENOMEM; |
| 5200 | |
| 5201 | /* |
Robbie Ko | 040ee612 | 2019-03-29 18:03:27 +0800 | [diff] [blame] | 5202 | * There are inodes that have extents that lie behind its i_size. Don't |
| 5203 | * accept clones from these extents. |
| 5204 | */ |
| 5205 | ret = __get_inode_info(clone_root->root, path, clone_root->ino, |
| 5206 | &clone_src_i_size, NULL, NULL, NULL, NULL, NULL); |
| 5207 | btrfs_release_path(path); |
| 5208 | if (ret < 0) |
| 5209 | goto out; |
| 5210 | |
| 5211 | /* |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5212 | * We can't send a clone operation for the entire range if we find |
| 5213 | * extent items in the respective range in the source file that |
| 5214 | * refer to different extents or if we find holes. |
| 5215 | * So check for that and do a mix of clone and regular write/copy |
| 5216 | * operations if needed. |
| 5217 | * |
| 5218 | * Example: |
| 5219 | * |
| 5220 | * mkfs.btrfs -f /dev/sda |
| 5221 | * mount /dev/sda /mnt |
| 5222 | * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo |
| 5223 | * cp --reflink=always /mnt/foo /mnt/bar |
| 5224 | * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo |
| 5225 | * btrfs subvolume snapshot -r /mnt /mnt/snap |
| 5226 | * |
| 5227 | * If when we send the snapshot and we are processing file bar (which |
| 5228 | * has a higher inode number than foo) we blindly send a clone operation |
| 5229 | * for the [0, 100K[ range from foo to bar, the receiver ends up getting |
| 5230 | * a file bar that matches the content of file foo - iow, doesn't match |
| 5231 | * the content from bar in the original filesystem. |
| 5232 | */ |
| 5233 | key.objectid = clone_root->ino; |
| 5234 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5235 | key.offset = clone_root->offset; |
| 5236 | ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0); |
| 5237 | if (ret < 0) |
| 5238 | goto out; |
| 5239 | if (ret > 0 && path->slots[0] > 0) { |
| 5240 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); |
| 5241 | if (key.objectid == clone_root->ino && |
| 5242 | key.type == BTRFS_EXTENT_DATA_KEY) |
| 5243 | path->slots[0]--; |
| 5244 | } |
| 5245 | |
| 5246 | while (true) { |
| 5247 | struct extent_buffer *leaf = path->nodes[0]; |
| 5248 | int slot = path->slots[0]; |
| 5249 | struct btrfs_file_extent_item *ei; |
| 5250 | u8 type; |
| 5251 | u64 ext_len; |
| 5252 | u64 clone_len; |
Robbie Ko | 040ee612 | 2019-03-29 18:03:27 +0800 | [diff] [blame] | 5253 | u64 clone_data_offset; |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5254 | |
| 5255 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5256 | ret = btrfs_next_leaf(clone_root->root, path); |
| 5257 | if (ret < 0) |
| 5258 | goto out; |
| 5259 | else if (ret > 0) |
| 5260 | break; |
| 5261 | continue; |
| 5262 | } |
| 5263 | |
| 5264 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5265 | |
| 5266 | /* |
| 5267 | * We might have an implicit trailing hole (NO_HOLES feature |
| 5268 | * enabled). We deal with it after leaving this loop. |
| 5269 | */ |
| 5270 | if (key.objectid != clone_root->ino || |
| 5271 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 5272 | break; |
| 5273 | |
| 5274 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5275 | type = btrfs_file_extent_type(leaf, ei); |
| 5276 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 5277 | ext_len = btrfs_file_extent_ram_bytes(leaf, ei); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5278 | ext_len = PAGE_ALIGN(ext_len); |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5279 | } else { |
| 5280 | ext_len = btrfs_file_extent_num_bytes(leaf, ei); |
| 5281 | } |
| 5282 | |
| 5283 | if (key.offset + ext_len <= clone_root->offset) |
| 5284 | goto next; |
| 5285 | |
| 5286 | if (key.offset > clone_root->offset) { |
| 5287 | /* Implicit hole, NO_HOLES feature enabled. */ |
| 5288 | u64 hole_len = key.offset - clone_root->offset; |
| 5289 | |
| 5290 | if (hole_len > len) |
| 5291 | hole_len = len; |
| 5292 | ret = send_extent_data(sctx, offset, hole_len); |
| 5293 | if (ret < 0) |
| 5294 | goto out; |
| 5295 | |
| 5296 | len -= hole_len; |
| 5297 | if (len == 0) |
| 5298 | break; |
| 5299 | offset += hole_len; |
| 5300 | clone_root->offset += hole_len; |
| 5301 | data_offset += hole_len; |
| 5302 | } |
| 5303 | |
| 5304 | if (key.offset >= clone_root->offset + len) |
| 5305 | break; |
| 5306 | |
Robbie Ko | 040ee612 | 2019-03-29 18:03:27 +0800 | [diff] [blame] | 5307 | if (key.offset >= clone_src_i_size) |
| 5308 | break; |
| 5309 | |
| 5310 | if (key.offset + ext_len > clone_src_i_size) |
| 5311 | ext_len = clone_src_i_size - key.offset; |
| 5312 | |
| 5313 | clone_data_offset = btrfs_file_extent_offset(leaf, ei); |
| 5314 | if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) { |
| 5315 | clone_root->offset = key.offset; |
| 5316 | if (clone_data_offset < data_offset && |
| 5317 | clone_data_offset + ext_len > data_offset) { |
| 5318 | u64 extent_offset; |
| 5319 | |
| 5320 | extent_offset = data_offset - clone_data_offset; |
| 5321 | ext_len -= extent_offset; |
| 5322 | clone_data_offset += extent_offset; |
| 5323 | clone_root->offset += extent_offset; |
| 5324 | } |
| 5325 | } |
| 5326 | |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5327 | clone_len = min_t(u64, ext_len, len); |
| 5328 | |
| 5329 | if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte && |
Filipe Manana | 3c850b4 | 2019-05-20 09:57:00 +0100 | [diff] [blame] | 5330 | clone_data_offset == data_offset) { |
| 5331 | const u64 src_end = clone_root->offset + clone_len; |
| 5332 | const u64 sectorsize = SZ_64K; |
| 5333 | |
| 5334 | /* |
| 5335 | * We can't clone the last block, when its size is not |
| 5336 | * sector size aligned, into the middle of a file. If we |
| 5337 | * do so, the receiver will get a failure (-EINVAL) when |
| 5338 | * trying to clone or will silently corrupt the data in |
| 5339 | * the destination file if it's on a kernel without the |
| 5340 | * fix introduced by commit ac765f83f1397646 |
| 5341 | * ("Btrfs: fix data corruption due to cloning of eof |
| 5342 | * block). |
| 5343 | * |
| 5344 | * So issue a clone of the aligned down range plus a |
| 5345 | * regular write for the eof block, if we hit that case. |
| 5346 | * |
| 5347 | * Also, we use the maximum possible sector size, 64K, |
| 5348 | * because we don't know what's the sector size of the |
| 5349 | * filesystem that receives the stream, so we have to |
| 5350 | * assume the largest possible sector size. |
| 5351 | */ |
| 5352 | if (src_end == clone_src_i_size && |
| 5353 | !IS_ALIGNED(src_end, sectorsize) && |
| 5354 | offset + clone_len < sctx->cur_inode_size) { |
| 5355 | u64 slen; |
| 5356 | |
| 5357 | slen = ALIGN_DOWN(src_end - clone_root->offset, |
| 5358 | sectorsize); |
| 5359 | if (slen > 0) { |
| 5360 | ret = send_clone(sctx, offset, slen, |
| 5361 | clone_root); |
| 5362 | if (ret < 0) |
| 5363 | goto out; |
| 5364 | } |
| 5365 | ret = send_extent_data(sctx, offset + slen, |
| 5366 | clone_len - slen); |
| 5367 | } else { |
| 5368 | ret = send_clone(sctx, offset, clone_len, |
| 5369 | clone_root); |
| 5370 | } |
| 5371 | } else { |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5372 | ret = send_extent_data(sctx, offset, clone_len); |
Filipe Manana | 3c850b4 | 2019-05-20 09:57:00 +0100 | [diff] [blame] | 5373 | } |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5374 | |
| 5375 | if (ret < 0) |
| 5376 | goto out; |
| 5377 | |
| 5378 | len -= clone_len; |
| 5379 | if (len == 0) |
| 5380 | break; |
| 5381 | offset += clone_len; |
| 5382 | clone_root->offset += clone_len; |
| 5383 | data_offset += clone_len; |
| 5384 | next: |
| 5385 | path->slots[0]++; |
| 5386 | } |
| 5387 | |
| 5388 | if (len > 0) |
| 5389 | ret = send_extent_data(sctx, offset, len); |
| 5390 | else |
| 5391 | ret = 0; |
| 5392 | out: |
| 5393 | btrfs_free_path(path); |
| 5394 | return ret; |
| 5395 | } |
| 5396 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5397 | static int send_write_or_clone(struct send_ctx *sctx, |
| 5398 | struct btrfs_path *path, |
| 5399 | struct btrfs_key *key, |
| 5400 | struct clone_root *clone_root) |
| 5401 | { |
| 5402 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5403 | u64 offset = key->offset; |
Omar Sandoval | c9a949a | 2020-08-21 00:39:53 -0700 | [diff] [blame] | 5404 | u64 end; |
Filipe David Borba Manana | 28e5dd8 | 2014-01-12 02:26:28 +0000 | [diff] [blame] | 5405 | u64 bs = sctx->send_root->fs_info->sb->s_blocksize; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5406 | |
Omar Sandoval | c9a949a | 2020-08-21 00:39:53 -0700 | [diff] [blame] | 5407 | end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size); |
| 5408 | if (offset >= end) |
| 5409 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5410 | |
Omar Sandoval | c9a949a | 2020-08-21 00:39:53 -0700 | [diff] [blame] | 5411 | if (clone_root && IS_ALIGNED(end, bs)) { |
| 5412 | struct btrfs_file_extent_item *ei; |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5413 | u64 disk_byte; |
| 5414 | u64 data_offset; |
| 5415 | |
Omar Sandoval | c9a949a | 2020-08-21 00:39:53 -0700 | [diff] [blame] | 5416 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5417 | struct btrfs_file_extent_item); |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5418 | disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei); |
| 5419 | data_offset = btrfs_file_extent_offset(path->nodes[0], ei); |
| 5420 | ret = clone_range(sctx, clone_root, disk_byte, data_offset, |
Omar Sandoval | c9a949a | 2020-08-21 00:39:53 -0700 | [diff] [blame] | 5421 | offset, end - offset); |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 5422 | } else { |
Omar Sandoval | c9a949a | 2020-08-21 00:39:53 -0700 | [diff] [blame] | 5423 | ret = send_extent_data(sctx, offset, end - offset); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5424 | } |
Omar Sandoval | c9a949a | 2020-08-21 00:39:53 -0700 | [diff] [blame] | 5425 | sctx->cur_inode_next_write_offset = end; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5426 | return ret; |
| 5427 | } |
| 5428 | |
| 5429 | static int is_extent_unchanged(struct send_ctx *sctx, |
| 5430 | struct btrfs_path *left_path, |
| 5431 | struct btrfs_key *ekey) |
| 5432 | { |
| 5433 | int ret = 0; |
| 5434 | struct btrfs_key key; |
| 5435 | struct btrfs_path *path = NULL; |
| 5436 | struct extent_buffer *eb; |
| 5437 | int slot; |
| 5438 | struct btrfs_key found_key; |
| 5439 | struct btrfs_file_extent_item *ei; |
| 5440 | u64 left_disknr; |
| 5441 | u64 right_disknr; |
| 5442 | u64 left_offset; |
| 5443 | u64 right_offset; |
| 5444 | u64 left_offset_fixed; |
| 5445 | u64 left_len; |
| 5446 | u64 right_len; |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5447 | u64 left_gen; |
| 5448 | u64 right_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5449 | u8 left_type; |
| 5450 | u8 right_type; |
| 5451 | |
| 5452 | path = alloc_path_for_send(); |
| 5453 | if (!path) |
| 5454 | return -ENOMEM; |
| 5455 | |
| 5456 | eb = left_path->nodes[0]; |
| 5457 | slot = left_path->slots[0]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5458 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 5459 | left_type = btrfs_file_extent_type(eb, ei); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5460 | |
| 5461 | if (left_type != BTRFS_FILE_EXTENT_REG) { |
| 5462 | ret = 0; |
| 5463 | goto out; |
| 5464 | } |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5465 | left_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
| 5466 | left_len = btrfs_file_extent_num_bytes(eb, ei); |
| 5467 | left_offset = btrfs_file_extent_offset(eb, ei); |
| 5468 | left_gen = btrfs_file_extent_generation(eb, ei); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5469 | |
| 5470 | /* |
| 5471 | * Following comments will refer to these graphics. L is the left |
| 5472 | * extents which we are checking at the moment. 1-8 are the right |
| 5473 | * extents that we iterate. |
| 5474 | * |
| 5475 | * |-----L-----| |
| 5476 | * |-1-|-2a-|-3-|-4-|-5-|-6-| |
| 5477 | * |
| 5478 | * |-----L-----| |
| 5479 | * |--1--|-2b-|...(same as above) |
| 5480 | * |
| 5481 | * Alternative situation. Happens on files where extents got split. |
| 5482 | * |-----L-----| |
| 5483 | * |-----------7-----------|-6-| |
| 5484 | * |
| 5485 | * Alternative situation. Happens on files which got larger. |
| 5486 | * |-----L-----| |
| 5487 | * |-8-| |
| 5488 | * Nothing follows after 8. |
| 5489 | */ |
| 5490 | |
| 5491 | key.objectid = ekey->objectid; |
| 5492 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5493 | key.offset = ekey->offset; |
| 5494 | ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); |
| 5495 | if (ret < 0) |
| 5496 | goto out; |
| 5497 | if (ret) { |
| 5498 | ret = 0; |
| 5499 | goto out; |
| 5500 | } |
| 5501 | |
| 5502 | /* |
| 5503 | * Handle special case where the right side has no extents at all. |
| 5504 | */ |
| 5505 | eb = path->nodes[0]; |
| 5506 | slot = path->slots[0]; |
| 5507 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 5508 | if (found_key.objectid != key.objectid || |
| 5509 | found_key.type != key.type) { |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 5510 | /* If we're a hole then just pretend nothing changed */ |
| 5511 | ret = (left_disknr) ? 0 : 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5512 | goto out; |
| 5513 | } |
| 5514 | |
| 5515 | /* |
| 5516 | * We're now on 2a, 2b or 7. |
| 5517 | */ |
| 5518 | key = found_key; |
| 5519 | while (key.offset < ekey->offset + left_len) { |
| 5520 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 5521 | right_type = btrfs_file_extent_type(eb, ei); |
Filipe Manana | e1cbfd7 | 2017-04-04 20:31:00 +0100 | [diff] [blame] | 5522 | if (right_type != BTRFS_FILE_EXTENT_REG && |
| 5523 | right_type != BTRFS_FILE_EXTENT_INLINE) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5524 | ret = 0; |
| 5525 | goto out; |
| 5526 | } |
| 5527 | |
Filipe Manana | e1cbfd7 | 2017-04-04 20:31:00 +0100 | [diff] [blame] | 5528 | if (right_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 5529 | right_len = btrfs_file_extent_ram_bytes(eb, ei); |
Filipe Manana | e1cbfd7 | 2017-04-04 20:31:00 +0100 | [diff] [blame] | 5530 | right_len = PAGE_ALIGN(right_len); |
| 5531 | } else { |
| 5532 | right_len = btrfs_file_extent_num_bytes(eb, ei); |
| 5533 | } |
Josef Bacik | 007d31f | 2013-10-31 16:49:02 -0400 | [diff] [blame] | 5534 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5535 | /* |
| 5536 | * Are we at extent 8? If yes, we know the extent is changed. |
| 5537 | * This may only happen on the first iteration. |
| 5538 | */ |
Alexander Block | d8347fa | 2012-08-01 12:49:15 +0200 | [diff] [blame] | 5539 | if (found_key.offset + right_len <= ekey->offset) { |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 5540 | /* If we're a hole just pretend nothing changed */ |
| 5541 | ret = (left_disknr) ? 0 : 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5542 | goto out; |
| 5543 | } |
| 5544 | |
Filipe Manana | e1cbfd7 | 2017-04-04 20:31:00 +0100 | [diff] [blame] | 5545 | /* |
| 5546 | * We just wanted to see if when we have an inline extent, what |
| 5547 | * follows it is a regular extent (wanted to check the above |
| 5548 | * condition for inline extents too). This should normally not |
| 5549 | * happen but it's possible for example when we have an inline |
| 5550 | * compressed extent representing data with a size matching |
| 5551 | * the page size (currently the same as sector size). |
| 5552 | */ |
| 5553 | if (right_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5554 | ret = 0; |
| 5555 | goto out; |
| 5556 | } |
| 5557 | |
Filipe Manana | 24e52b1 | 2017-07-06 15:31:46 +0100 | [diff] [blame] | 5558 | right_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
| 5559 | right_offset = btrfs_file_extent_offset(eb, ei); |
| 5560 | right_gen = btrfs_file_extent_generation(eb, ei); |
| 5561 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5562 | left_offset_fixed = left_offset; |
| 5563 | if (key.offset < ekey->offset) { |
| 5564 | /* Fix the right offset for 2a and 7. */ |
| 5565 | right_offset += ekey->offset - key.offset; |
| 5566 | } else { |
| 5567 | /* Fix the left offset for all behind 2a and 2b */ |
| 5568 | left_offset_fixed += key.offset - ekey->offset; |
| 5569 | } |
| 5570 | |
| 5571 | /* |
| 5572 | * Check if we have the same extent. |
| 5573 | */ |
Alexander Block | 3954096 | 2012-08-01 12:46:05 +0200 | [diff] [blame] | 5574 | if (left_disknr != right_disknr || |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5575 | left_offset_fixed != right_offset || |
| 5576 | left_gen != right_gen) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5577 | ret = 0; |
| 5578 | goto out; |
| 5579 | } |
| 5580 | |
| 5581 | /* |
| 5582 | * Go to the next extent. |
| 5583 | */ |
| 5584 | ret = btrfs_next_item(sctx->parent_root, path); |
| 5585 | if (ret < 0) |
| 5586 | goto out; |
| 5587 | if (!ret) { |
| 5588 | eb = path->nodes[0]; |
| 5589 | slot = path->slots[0]; |
| 5590 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 5591 | } |
| 5592 | if (ret || found_key.objectid != key.objectid || |
| 5593 | found_key.type != key.type) { |
| 5594 | key.offset += right_len; |
| 5595 | break; |
Jan Schmidt | adaa4b8 | 2013-03-21 14:30:23 +0000 | [diff] [blame] | 5596 | } |
| 5597 | if (found_key.offset != key.offset + right_len) { |
| 5598 | ret = 0; |
| 5599 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5600 | } |
| 5601 | key = found_key; |
| 5602 | } |
| 5603 | |
| 5604 | /* |
| 5605 | * We're now behind the left extent (treat as unchanged) or at the end |
| 5606 | * of the right side (treat as changed). |
| 5607 | */ |
| 5608 | if (key.offset >= ekey->offset + left_len) |
| 5609 | ret = 1; |
| 5610 | else |
| 5611 | ret = 0; |
| 5612 | |
| 5613 | |
| 5614 | out: |
| 5615 | btrfs_free_path(path); |
| 5616 | return ret; |
| 5617 | } |
| 5618 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5619 | static int get_last_extent(struct send_ctx *sctx, u64 offset) |
| 5620 | { |
| 5621 | struct btrfs_path *path; |
| 5622 | struct btrfs_root *root = sctx->send_root; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5623 | struct btrfs_key key; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5624 | int ret; |
| 5625 | |
| 5626 | path = alloc_path_for_send(); |
| 5627 | if (!path) |
| 5628 | return -ENOMEM; |
| 5629 | |
| 5630 | sctx->cur_inode_last_extent = 0; |
| 5631 | |
| 5632 | key.objectid = sctx->cur_ino; |
| 5633 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5634 | key.offset = offset; |
| 5635 | ret = btrfs_search_slot_for_read(root, &key, path, 0, 1); |
| 5636 | if (ret < 0) |
| 5637 | goto out; |
| 5638 | ret = 0; |
| 5639 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 5640 | if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY) |
| 5641 | goto out; |
| 5642 | |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 5643 | sctx->cur_inode_last_extent = btrfs_file_extent_end(path); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5644 | out: |
| 5645 | btrfs_free_path(path); |
| 5646 | return ret; |
| 5647 | } |
| 5648 | |
Filipe Manana | 82bfb2e | 2017-02-14 17:56:32 +0000 | [diff] [blame] | 5649 | static int range_is_hole_in_parent(struct send_ctx *sctx, |
| 5650 | const u64 start, |
| 5651 | const u64 end) |
| 5652 | { |
| 5653 | struct btrfs_path *path; |
| 5654 | struct btrfs_key key; |
| 5655 | struct btrfs_root *root = sctx->parent_root; |
| 5656 | u64 search_start = start; |
| 5657 | int ret; |
| 5658 | |
| 5659 | path = alloc_path_for_send(); |
| 5660 | if (!path) |
| 5661 | return -ENOMEM; |
| 5662 | |
| 5663 | key.objectid = sctx->cur_ino; |
| 5664 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5665 | key.offset = search_start; |
| 5666 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5667 | if (ret < 0) |
| 5668 | goto out; |
| 5669 | if (ret > 0 && path->slots[0] > 0) |
| 5670 | path->slots[0]--; |
| 5671 | |
| 5672 | while (search_start < end) { |
| 5673 | struct extent_buffer *leaf = path->nodes[0]; |
| 5674 | int slot = path->slots[0]; |
| 5675 | struct btrfs_file_extent_item *fi; |
| 5676 | u64 extent_end; |
| 5677 | |
| 5678 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5679 | ret = btrfs_next_leaf(root, path); |
| 5680 | if (ret < 0) |
| 5681 | goto out; |
| 5682 | else if (ret > 0) |
| 5683 | break; |
| 5684 | continue; |
| 5685 | } |
| 5686 | |
| 5687 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5688 | if (key.objectid < sctx->cur_ino || |
| 5689 | key.type < BTRFS_EXTENT_DATA_KEY) |
| 5690 | goto next; |
| 5691 | if (key.objectid > sctx->cur_ino || |
| 5692 | key.type > BTRFS_EXTENT_DATA_KEY || |
| 5693 | key.offset >= end) |
| 5694 | break; |
| 5695 | |
| 5696 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 5697 | extent_end = btrfs_file_extent_end(path); |
Filipe Manana | 82bfb2e | 2017-02-14 17:56:32 +0000 | [diff] [blame] | 5698 | if (extent_end <= start) |
| 5699 | goto next; |
| 5700 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) { |
| 5701 | search_start = extent_end; |
| 5702 | goto next; |
| 5703 | } |
| 5704 | ret = 0; |
| 5705 | goto out; |
| 5706 | next: |
| 5707 | path->slots[0]++; |
| 5708 | } |
| 5709 | ret = 1; |
| 5710 | out: |
| 5711 | btrfs_free_path(path); |
| 5712 | return ret; |
| 5713 | } |
| 5714 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5715 | static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path, |
| 5716 | struct btrfs_key *key) |
| 5717 | { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5718 | int ret = 0; |
| 5719 | |
| 5720 | if (sctx->cur_ino != key->objectid || !need_send_hole(sctx)) |
| 5721 | return 0; |
| 5722 | |
| 5723 | if (sctx->cur_inode_last_extent == (u64)-1) { |
| 5724 | ret = get_last_extent(sctx, key->offset - 1); |
| 5725 | if (ret) |
| 5726 | return ret; |
| 5727 | } |
| 5728 | |
Filipe David Borba Manana | bf54f41 | 2014-01-28 01:38:06 +0000 | [diff] [blame] | 5729 | if (path->slots[0] == 0 && |
| 5730 | sctx->cur_inode_last_extent < key->offset) { |
| 5731 | /* |
| 5732 | * We might have skipped entire leafs that contained only |
| 5733 | * file extent items for our current inode. These leafs have |
| 5734 | * a generation number smaller (older) than the one in the |
| 5735 | * current leaf and the leaf our last extent came from, and |
| 5736 | * are located between these 2 leafs. |
| 5737 | */ |
| 5738 | ret = get_last_extent(sctx, key->offset - 1); |
| 5739 | if (ret) |
| 5740 | return ret; |
| 5741 | } |
| 5742 | |
Filipe Manana | 82bfb2e | 2017-02-14 17:56:32 +0000 | [diff] [blame] | 5743 | if (sctx->cur_inode_last_extent < key->offset) { |
| 5744 | ret = range_is_hole_in_parent(sctx, |
| 5745 | sctx->cur_inode_last_extent, |
| 5746 | key->offset); |
| 5747 | if (ret < 0) |
| 5748 | return ret; |
| 5749 | else if (ret == 0) |
| 5750 | ret = send_hole(sctx, key->offset); |
| 5751 | else |
| 5752 | ret = 0; |
| 5753 | } |
Filipe Manana | a5eeb3d | 2020-03-09 12:41:06 +0000 | [diff] [blame] | 5754 | sctx->cur_inode_last_extent = btrfs_file_extent_end(path); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5755 | return ret; |
| 5756 | } |
| 5757 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5758 | static int process_extent(struct send_ctx *sctx, |
| 5759 | struct btrfs_path *path, |
| 5760 | struct btrfs_key *key) |
| 5761 | { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5762 | struct clone_root *found_clone = NULL; |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 5763 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5764 | |
| 5765 | if (S_ISLNK(sctx->cur_inode_mode)) |
| 5766 | return 0; |
| 5767 | |
| 5768 | if (sctx->parent_root && !sctx->cur_inode_new) { |
| 5769 | ret = is_extent_unchanged(sctx, path, key); |
| 5770 | if (ret < 0) |
| 5771 | goto out; |
| 5772 | if (ret) { |
| 5773 | ret = 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5774 | goto out_hole; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5775 | } |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 5776 | } else { |
| 5777 | struct btrfs_file_extent_item *ei; |
| 5778 | u8 type; |
| 5779 | |
| 5780 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5781 | struct btrfs_file_extent_item); |
| 5782 | type = btrfs_file_extent_type(path->nodes[0], ei); |
| 5783 | if (type == BTRFS_FILE_EXTENT_PREALLOC || |
| 5784 | type == BTRFS_FILE_EXTENT_REG) { |
| 5785 | /* |
| 5786 | * The send spec does not have a prealloc command yet, |
| 5787 | * so just leave a hole for prealloc'ed extents until |
| 5788 | * we have enough commands queued up to justify rev'ing |
| 5789 | * the send spec. |
| 5790 | */ |
| 5791 | if (type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 5792 | ret = 0; |
| 5793 | goto out; |
| 5794 | } |
| 5795 | |
| 5796 | /* Have a hole, just skip it. */ |
| 5797 | if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) { |
| 5798 | ret = 0; |
| 5799 | goto out; |
| 5800 | } |
| 5801 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5802 | } |
| 5803 | |
| 5804 | ret = find_extent_clone(sctx, path, key->objectid, key->offset, |
| 5805 | sctx->cur_inode_size, &found_clone); |
| 5806 | if (ret != -ENOENT && ret < 0) |
| 5807 | goto out; |
| 5808 | |
| 5809 | ret = send_write_or_clone(sctx, path, key, found_clone); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5810 | if (ret) |
| 5811 | goto out; |
| 5812 | out_hole: |
| 5813 | ret = maybe_send_hole(sctx, path, key); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5814 | out: |
| 5815 | return ret; |
| 5816 | } |
| 5817 | |
| 5818 | static int process_all_extents(struct send_ctx *sctx) |
| 5819 | { |
| 5820 | int ret; |
| 5821 | struct btrfs_root *root; |
| 5822 | struct btrfs_path *path; |
| 5823 | struct btrfs_key key; |
| 5824 | struct btrfs_key found_key; |
| 5825 | struct extent_buffer *eb; |
| 5826 | int slot; |
| 5827 | |
| 5828 | root = sctx->send_root; |
| 5829 | path = alloc_path_for_send(); |
| 5830 | if (!path) |
| 5831 | return -ENOMEM; |
| 5832 | |
| 5833 | key.objectid = sctx->cmp_key->objectid; |
| 5834 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5835 | key.offset = 0; |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 5836 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5837 | if (ret < 0) |
| 5838 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5839 | |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 5840 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5841 | eb = path->nodes[0]; |
| 5842 | slot = path->slots[0]; |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 5843 | |
| 5844 | if (slot >= btrfs_header_nritems(eb)) { |
| 5845 | ret = btrfs_next_leaf(root, path); |
| 5846 | if (ret < 0) { |
| 5847 | goto out; |
| 5848 | } else if (ret > 0) { |
| 5849 | ret = 0; |
| 5850 | break; |
| 5851 | } |
| 5852 | continue; |
| 5853 | } |
| 5854 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5855 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 5856 | |
| 5857 | if (found_key.objectid != key.objectid || |
| 5858 | found_key.type != key.type) { |
| 5859 | ret = 0; |
| 5860 | goto out; |
| 5861 | } |
| 5862 | |
| 5863 | ret = process_extent(sctx, path, &found_key); |
| 5864 | if (ret < 0) |
| 5865 | goto out; |
| 5866 | |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 5867 | path->slots[0]++; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | out: |
| 5871 | btrfs_free_path(path); |
| 5872 | return ret; |
| 5873 | } |
| 5874 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5875 | static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end, |
| 5876 | int *pending_move, |
| 5877 | int *refs_processed) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5878 | { |
| 5879 | int ret = 0; |
| 5880 | |
| 5881 | if (sctx->cur_ino == 0) |
| 5882 | goto out; |
| 5883 | if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 5884 | sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5885 | goto out; |
| 5886 | if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) |
| 5887 | goto out; |
| 5888 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5889 | ret = process_recorded_refs(sctx, pending_move); |
Alexander Block | e479d9b | 2012-07-28 16:09:35 +0200 | [diff] [blame] | 5890 | if (ret < 0) |
| 5891 | goto out; |
| 5892 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5893 | *refs_processed = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5894 | out: |
| 5895 | return ret; |
| 5896 | } |
| 5897 | |
| 5898 | static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) |
| 5899 | { |
| 5900 | int ret = 0; |
| 5901 | u64 left_mode; |
| 5902 | u64 left_uid; |
| 5903 | u64 left_gid; |
| 5904 | u64 right_mode; |
| 5905 | u64 right_uid; |
| 5906 | u64 right_gid; |
| 5907 | int need_chmod = 0; |
| 5908 | int need_chown = 0; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5909 | int need_truncate = 1; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5910 | int pending_move = 0; |
| 5911 | int refs_processed = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5912 | |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 5913 | if (sctx->ignore_cur_inode) |
| 5914 | return 0; |
| 5915 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5916 | ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move, |
| 5917 | &refs_processed); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5918 | if (ret < 0) |
| 5919 | goto out; |
| 5920 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5921 | /* |
| 5922 | * We have processed the refs and thus need to advance send_progress. |
| 5923 | * Now, calls to get_cur_xxx will take the updated refs of the current |
| 5924 | * inode into account. |
| 5925 | * |
| 5926 | * On the other hand, if our current inode is a directory and couldn't |
| 5927 | * be moved/renamed because its parent was renamed/moved too and it has |
| 5928 | * a higher inode number, we can only move/rename our current inode |
| 5929 | * after we moved/renamed its parent. Therefore in this case operate on |
| 5930 | * the old path (pre move/rename) of our current inode, and the |
| 5931 | * move/rename will be performed later. |
| 5932 | */ |
| 5933 | if (refs_processed && !pending_move) |
| 5934 | sctx->send_progress = sctx->cur_ino + 1; |
| 5935 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5936 | if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) |
| 5937 | goto out; |
| 5938 | if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) |
| 5939 | goto out; |
| 5940 | |
| 5941 | ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 5942 | &left_mode, &left_uid, &left_gid, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5943 | if (ret < 0) |
| 5944 | goto out; |
| 5945 | |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5946 | if (!sctx->parent_root || sctx->cur_inode_new) { |
| 5947 | need_chown = 1; |
| 5948 | if (!S_ISLNK(sctx->cur_inode_mode)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5949 | need_chmod = 1; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5950 | if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size) |
| 5951 | need_truncate = 0; |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5952 | } else { |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5953 | u64 old_size; |
| 5954 | |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5955 | ret = get_inode_info(sctx->parent_root, sctx->cur_ino, |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5956 | &old_size, NULL, &right_mode, &right_uid, |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5957 | &right_gid, NULL); |
| 5958 | if (ret < 0) |
| 5959 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5960 | |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5961 | if (left_uid != right_uid || left_gid != right_gid) |
| 5962 | need_chown = 1; |
| 5963 | if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode) |
| 5964 | need_chmod = 1; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5965 | if ((old_size == sctx->cur_inode_size) || |
| 5966 | (sctx->cur_inode_size > old_size && |
| 5967 | sctx->cur_inode_next_write_offset == sctx->cur_inode_size)) |
| 5968 | need_truncate = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5969 | } |
| 5970 | |
| 5971 | if (S_ISREG(sctx->cur_inode_mode)) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5972 | if (need_send_hole(sctx)) { |
Filipe Manana | 766b5e5 | 2014-03-30 23:02:53 +0100 | [diff] [blame] | 5973 | if (sctx->cur_inode_last_extent == (u64)-1 || |
| 5974 | sctx->cur_inode_last_extent < |
| 5975 | sctx->cur_inode_size) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5976 | ret = get_last_extent(sctx, (u64)-1); |
| 5977 | if (ret) |
| 5978 | goto out; |
| 5979 | } |
| 5980 | if (sctx->cur_inode_last_extent < |
| 5981 | sctx->cur_inode_size) { |
| 5982 | ret = send_hole(sctx, sctx->cur_inode_size); |
| 5983 | if (ret) |
| 5984 | goto out; |
| 5985 | } |
| 5986 | } |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5987 | if (need_truncate) { |
| 5988 | ret = send_truncate(sctx, sctx->cur_ino, |
| 5989 | sctx->cur_inode_gen, |
| 5990 | sctx->cur_inode_size); |
| 5991 | if (ret < 0) |
| 5992 | goto out; |
| 5993 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5994 | } |
| 5995 | |
| 5996 | if (need_chown) { |
| 5997 | ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 5998 | left_uid, left_gid); |
| 5999 | if (ret < 0) |
| 6000 | goto out; |
| 6001 | } |
| 6002 | if (need_chmod) { |
| 6003 | ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 6004 | left_mode); |
| 6005 | if (ret < 0) |
| 6006 | goto out; |
| 6007 | } |
| 6008 | |
Marcos Paulo de Souza | 89efda5 | 2020-05-10 23:15:07 -0300 | [diff] [blame] | 6009 | ret = send_capabilities(sctx); |
| 6010 | if (ret < 0) |
| 6011 | goto out; |
| 6012 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6013 | /* |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 6014 | * If other directory inodes depended on our current directory |
| 6015 | * inode's move/rename, now do their move/rename operations. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6016 | */ |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 6017 | if (!is_waiting_for_move(sctx, sctx->cur_ino)) { |
| 6018 | ret = apply_children_dir_moves(sctx); |
| 6019 | if (ret) |
| 6020 | goto out; |
Filipe Manana | fcbd215 | 2014-03-03 12:28:40 +0000 | [diff] [blame] | 6021 | /* |
| 6022 | * Need to send that every time, no matter if it actually |
| 6023 | * changed between the two trees as we have done changes to |
| 6024 | * the inode before. If our inode is a directory and it's |
| 6025 | * waiting to be moved/renamed, we will send its utimes when |
| 6026 | * it's moved/renamed, therefore we don't need to do it here. |
| 6027 | */ |
| 6028 | sctx->send_progress = sctx->cur_ino + 1; |
| 6029 | ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); |
| 6030 | if (ret < 0) |
| 6031 | goto out; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 6032 | } |
| 6033 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6034 | out: |
| 6035 | return ret; |
| 6036 | } |
| 6037 | |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6038 | struct parent_paths_ctx { |
| 6039 | struct list_head *refs; |
| 6040 | struct send_ctx *sctx; |
| 6041 | }; |
| 6042 | |
| 6043 | static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name, |
| 6044 | void *ctx) |
| 6045 | { |
| 6046 | struct parent_paths_ctx *ppctx = ctx; |
| 6047 | |
| 6048 | return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx, |
| 6049 | ppctx->refs); |
| 6050 | } |
| 6051 | |
| 6052 | /* |
| 6053 | * Issue unlink operations for all paths of the current inode found in the |
| 6054 | * parent snapshot. |
| 6055 | */ |
| 6056 | static int btrfs_unlink_all_paths(struct send_ctx *sctx) |
| 6057 | { |
| 6058 | LIST_HEAD(deleted_refs); |
| 6059 | struct btrfs_path *path; |
| 6060 | struct btrfs_key key; |
| 6061 | struct parent_paths_ctx ctx; |
| 6062 | int ret; |
| 6063 | |
| 6064 | path = alloc_path_for_send(); |
| 6065 | if (!path) |
| 6066 | return -ENOMEM; |
| 6067 | |
| 6068 | key.objectid = sctx->cur_ino; |
| 6069 | key.type = BTRFS_INODE_REF_KEY; |
| 6070 | key.offset = 0; |
| 6071 | ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); |
| 6072 | if (ret < 0) |
| 6073 | goto out; |
| 6074 | |
| 6075 | ctx.refs = &deleted_refs; |
| 6076 | ctx.sctx = sctx; |
| 6077 | |
| 6078 | while (true) { |
| 6079 | struct extent_buffer *eb = path->nodes[0]; |
| 6080 | int slot = path->slots[0]; |
| 6081 | |
| 6082 | if (slot >= btrfs_header_nritems(eb)) { |
| 6083 | ret = btrfs_next_leaf(sctx->parent_root, path); |
| 6084 | if (ret < 0) |
| 6085 | goto out; |
| 6086 | else if (ret > 0) |
| 6087 | break; |
| 6088 | continue; |
| 6089 | } |
| 6090 | |
| 6091 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 6092 | if (key.objectid != sctx->cur_ino) |
| 6093 | break; |
| 6094 | if (key.type != BTRFS_INODE_REF_KEY && |
| 6095 | key.type != BTRFS_INODE_EXTREF_KEY) |
| 6096 | break; |
| 6097 | |
| 6098 | ret = iterate_inode_ref(sctx->parent_root, path, &key, 1, |
| 6099 | record_parent_ref, &ctx); |
| 6100 | if (ret < 0) |
| 6101 | goto out; |
| 6102 | |
| 6103 | path->slots[0]++; |
| 6104 | } |
| 6105 | |
| 6106 | while (!list_empty(&deleted_refs)) { |
| 6107 | struct recorded_ref *ref; |
| 6108 | |
| 6109 | ref = list_first_entry(&deleted_refs, struct recorded_ref, list); |
| 6110 | ret = send_unlink(sctx, ref->full_path); |
| 6111 | if (ret < 0) |
| 6112 | goto out; |
| 6113 | fs_path_free(ref->full_path); |
| 6114 | list_del(&ref->list); |
| 6115 | kfree(ref); |
| 6116 | } |
| 6117 | ret = 0; |
| 6118 | out: |
| 6119 | btrfs_free_path(path); |
| 6120 | if (ret) |
| 6121 | __free_recorded_refs(&deleted_refs); |
| 6122 | return ret; |
| 6123 | } |
| 6124 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6125 | static int changed_inode(struct send_ctx *sctx, |
| 6126 | enum btrfs_compare_tree_result result) |
| 6127 | { |
| 6128 | int ret = 0; |
| 6129 | struct btrfs_key *key = sctx->cmp_key; |
| 6130 | struct btrfs_inode_item *left_ii = NULL; |
| 6131 | struct btrfs_inode_item *right_ii = NULL; |
| 6132 | u64 left_gen = 0; |
| 6133 | u64 right_gen = 0; |
| 6134 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6135 | sctx->cur_ino = key->objectid; |
| 6136 | sctx->cur_inode_new_gen = 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 6137 | sctx->cur_inode_last_extent = (u64)-1; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 6138 | sctx->cur_inode_next_write_offset = 0; |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6139 | sctx->ignore_cur_inode = false; |
Alexander Block | e479d9b | 2012-07-28 16:09:35 +0200 | [diff] [blame] | 6140 | |
| 6141 | /* |
| 6142 | * Set send_progress to current inode. This will tell all get_cur_xxx |
| 6143 | * functions that the current inode's refs are not updated yet. Later, |
| 6144 | * when process_recorded_refs is finished, it is set to cur_ino + 1. |
| 6145 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6146 | sctx->send_progress = sctx->cur_ino; |
| 6147 | |
| 6148 | if (result == BTRFS_COMPARE_TREE_NEW || |
| 6149 | result == BTRFS_COMPARE_TREE_CHANGED) { |
| 6150 | left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], |
| 6151 | sctx->left_path->slots[0], |
| 6152 | struct btrfs_inode_item); |
| 6153 | left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], |
| 6154 | left_ii); |
| 6155 | } else { |
| 6156 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], |
| 6157 | sctx->right_path->slots[0], |
| 6158 | struct btrfs_inode_item); |
| 6159 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], |
| 6160 | right_ii); |
| 6161 | } |
| 6162 | if (result == BTRFS_COMPARE_TREE_CHANGED) { |
| 6163 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], |
| 6164 | sctx->right_path->slots[0], |
| 6165 | struct btrfs_inode_item); |
| 6166 | |
| 6167 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], |
| 6168 | right_ii); |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 6169 | |
| 6170 | /* |
| 6171 | * The cur_ino = root dir case is special here. We can't treat |
| 6172 | * the inode as deleted+reused because it would generate a |
| 6173 | * stream that tries to delete/mkdir the root dir. |
| 6174 | */ |
| 6175 | if (left_gen != right_gen && |
| 6176 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6177 | sctx->cur_inode_new_gen = 1; |
| 6178 | } |
| 6179 | |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6180 | /* |
| 6181 | * Normally we do not find inodes with a link count of zero (orphans) |
| 6182 | * because the most common case is to create a snapshot and use it |
| 6183 | * for a send operation. However other less common use cases involve |
| 6184 | * using a subvolume and send it after turning it to RO mode just |
| 6185 | * after deleting all hard links of a file while holding an open |
| 6186 | * file descriptor against it or turning a RO snapshot into RW mode, |
| 6187 | * keep an open file descriptor against a file, delete it and then |
| 6188 | * turn the snapshot back to RO mode before using it for a send |
| 6189 | * operation. So if we find such cases, ignore the inode and all its |
| 6190 | * items completely if it's a new inode, or if it's a changed inode |
| 6191 | * make sure all its previous paths (from the parent snapshot) are all |
| 6192 | * unlinked and all other the inode items are ignored. |
| 6193 | */ |
| 6194 | if (result == BTRFS_COMPARE_TREE_NEW || |
| 6195 | result == BTRFS_COMPARE_TREE_CHANGED) { |
| 6196 | u32 nlinks; |
| 6197 | |
| 6198 | nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii); |
| 6199 | if (nlinks == 0) { |
| 6200 | sctx->ignore_cur_inode = true; |
| 6201 | if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 6202 | ret = btrfs_unlink_all_paths(sctx); |
| 6203 | goto out; |
| 6204 | } |
| 6205 | } |
| 6206 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6207 | if (result == BTRFS_COMPARE_TREE_NEW) { |
| 6208 | sctx->cur_inode_gen = left_gen; |
| 6209 | sctx->cur_inode_new = 1; |
| 6210 | sctx->cur_inode_deleted = 0; |
| 6211 | sctx->cur_inode_size = btrfs_inode_size( |
| 6212 | sctx->left_path->nodes[0], left_ii); |
| 6213 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6214 | sctx->left_path->nodes[0], left_ii); |
Liu Bo | 644d194 | 2014-02-27 17:29:01 +0800 | [diff] [blame] | 6215 | sctx->cur_inode_rdev = btrfs_inode_rdev( |
| 6216 | sctx->left_path->nodes[0], left_ii); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6217 | if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 6218 | ret = send_create_inode_if_needed(sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6219 | } else if (result == BTRFS_COMPARE_TREE_DELETED) { |
| 6220 | sctx->cur_inode_gen = right_gen; |
| 6221 | sctx->cur_inode_new = 0; |
| 6222 | sctx->cur_inode_deleted = 1; |
| 6223 | sctx->cur_inode_size = btrfs_inode_size( |
| 6224 | sctx->right_path->nodes[0], right_ii); |
| 6225 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6226 | sctx->right_path->nodes[0], right_ii); |
| 6227 | } else if (result == BTRFS_COMPARE_TREE_CHANGED) { |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6228 | /* |
| 6229 | * We need to do some special handling in case the inode was |
| 6230 | * reported as changed with a changed generation number. This |
| 6231 | * means that the original inode was deleted and new inode |
| 6232 | * reused the same inum. So we have to treat the old inode as |
| 6233 | * deleted and the new one as new. |
| 6234 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6235 | if (sctx->cur_inode_new_gen) { |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6236 | /* |
| 6237 | * First, process the inode as if it was deleted. |
| 6238 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6239 | sctx->cur_inode_gen = right_gen; |
| 6240 | sctx->cur_inode_new = 0; |
| 6241 | sctx->cur_inode_deleted = 1; |
| 6242 | sctx->cur_inode_size = btrfs_inode_size( |
| 6243 | sctx->right_path->nodes[0], right_ii); |
| 6244 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6245 | sctx->right_path->nodes[0], right_ii); |
| 6246 | ret = process_all_refs(sctx, |
| 6247 | BTRFS_COMPARE_TREE_DELETED); |
| 6248 | if (ret < 0) |
| 6249 | goto out; |
| 6250 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6251 | /* |
| 6252 | * Now process the inode as if it was new. |
| 6253 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6254 | sctx->cur_inode_gen = left_gen; |
| 6255 | sctx->cur_inode_new = 1; |
| 6256 | sctx->cur_inode_deleted = 0; |
| 6257 | sctx->cur_inode_size = btrfs_inode_size( |
| 6258 | sctx->left_path->nodes[0], left_ii); |
| 6259 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6260 | sctx->left_path->nodes[0], left_ii); |
Liu Bo | 644d194 | 2014-02-27 17:29:01 +0800 | [diff] [blame] | 6261 | sctx->cur_inode_rdev = btrfs_inode_rdev( |
| 6262 | sctx->left_path->nodes[0], left_ii); |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 6263 | ret = send_create_inode_if_needed(sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6264 | if (ret < 0) |
| 6265 | goto out; |
| 6266 | |
| 6267 | ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); |
| 6268 | if (ret < 0) |
| 6269 | goto out; |
Alexander Block | e479d9b | 2012-07-28 16:09:35 +0200 | [diff] [blame] | 6270 | /* |
| 6271 | * Advance send_progress now as we did not get into |
| 6272 | * process_recorded_refs_if_needed in the new_gen case. |
| 6273 | */ |
| 6274 | sctx->send_progress = sctx->cur_ino + 1; |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6275 | |
| 6276 | /* |
| 6277 | * Now process all extents and xattrs of the inode as if |
| 6278 | * they were all new. |
| 6279 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6280 | ret = process_all_extents(sctx); |
| 6281 | if (ret < 0) |
| 6282 | goto out; |
| 6283 | ret = process_all_new_xattrs(sctx); |
| 6284 | if (ret < 0) |
| 6285 | goto out; |
| 6286 | } else { |
| 6287 | sctx->cur_inode_gen = left_gen; |
| 6288 | sctx->cur_inode_new = 0; |
| 6289 | sctx->cur_inode_new_gen = 0; |
| 6290 | sctx->cur_inode_deleted = 0; |
| 6291 | sctx->cur_inode_size = btrfs_inode_size( |
| 6292 | sctx->left_path->nodes[0], left_ii); |
| 6293 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6294 | sctx->left_path->nodes[0], left_ii); |
| 6295 | } |
| 6296 | } |
| 6297 | |
| 6298 | out: |
| 6299 | return ret; |
| 6300 | } |
| 6301 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6302 | /* |
| 6303 | * We have to process new refs before deleted refs, but compare_trees gives us |
| 6304 | * the new and deleted refs mixed. To fix this, we record the new/deleted refs |
| 6305 | * first and later process them in process_recorded_refs. |
| 6306 | * For the cur_inode_new_gen case, we skip recording completely because |
| 6307 | * changed_inode did already initiate processing of refs. The reason for this is |
| 6308 | * that in this case, compare_tree actually compares the refs of 2 different |
| 6309 | * inodes. To fix this, process_all_refs is used in changed_inode to handle all |
| 6310 | * refs of the right tree as deleted and all refs of the left tree as new. |
| 6311 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6312 | static int changed_ref(struct send_ctx *sctx, |
| 6313 | enum btrfs_compare_tree_result result) |
| 6314 | { |
| 6315 | int ret = 0; |
| 6316 | |
Filipe Manana | 9515558 | 2016-08-01 01:50:37 +0100 | [diff] [blame] | 6317 | if (sctx->cur_ino != sctx->cmp_key->objectid) { |
| 6318 | inconsistent_snapshot_error(sctx, result, "reference"); |
| 6319 | return -EIO; |
| 6320 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6321 | |
| 6322 | if (!sctx->cur_inode_new_gen && |
| 6323 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 6324 | if (result == BTRFS_COMPARE_TREE_NEW) |
| 6325 | ret = record_new_ref(sctx); |
| 6326 | else if (result == BTRFS_COMPARE_TREE_DELETED) |
| 6327 | ret = record_deleted_ref(sctx); |
| 6328 | else if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 6329 | ret = record_changed_ref(sctx); |
| 6330 | } |
| 6331 | |
| 6332 | return ret; |
| 6333 | } |
| 6334 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6335 | /* |
| 6336 | * Process new/deleted/changed xattrs. We skip processing in the |
| 6337 | * cur_inode_new_gen case because changed_inode did already initiate processing |
| 6338 | * of xattrs. The reason is the same as in changed_ref |
| 6339 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6340 | static int changed_xattr(struct send_ctx *sctx, |
| 6341 | enum btrfs_compare_tree_result result) |
| 6342 | { |
| 6343 | int ret = 0; |
| 6344 | |
Filipe Manana | 9515558 | 2016-08-01 01:50:37 +0100 | [diff] [blame] | 6345 | if (sctx->cur_ino != sctx->cmp_key->objectid) { |
| 6346 | inconsistent_snapshot_error(sctx, result, "xattr"); |
| 6347 | return -EIO; |
| 6348 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6349 | |
| 6350 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { |
| 6351 | if (result == BTRFS_COMPARE_TREE_NEW) |
| 6352 | ret = process_new_xattr(sctx); |
| 6353 | else if (result == BTRFS_COMPARE_TREE_DELETED) |
| 6354 | ret = process_deleted_xattr(sctx); |
| 6355 | else if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 6356 | ret = process_changed_xattr(sctx); |
| 6357 | } |
| 6358 | |
| 6359 | return ret; |
| 6360 | } |
| 6361 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6362 | /* |
| 6363 | * Process new/deleted/changed extents. We skip processing in the |
| 6364 | * cur_inode_new_gen case because changed_inode did already initiate processing |
| 6365 | * of extents. The reason is the same as in changed_ref |
| 6366 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6367 | static int changed_extent(struct send_ctx *sctx, |
| 6368 | enum btrfs_compare_tree_result result) |
| 6369 | { |
| 6370 | int ret = 0; |
| 6371 | |
Filipe Manana | b4f9a1a | 2019-07-17 13:23:39 +0100 | [diff] [blame] | 6372 | /* |
| 6373 | * We have found an extent item that changed without the inode item |
| 6374 | * having changed. This can happen either after relocation (where the |
| 6375 | * disk_bytenr of an extent item is replaced at |
| 6376 | * relocation.c:replace_file_extents()) or after deduplication into a |
| 6377 | * file in both the parent and send snapshots (where an extent item can |
| 6378 | * get modified or replaced with a new one). Note that deduplication |
| 6379 | * updates the inode item, but it only changes the iversion (sequence |
| 6380 | * field in the inode item) of the inode, so if a file is deduplicated |
| 6381 | * the same amount of times in both the parent and send snapshots, its |
| 6382 | * iversion becames the same in both snapshots, whence the inode item is |
| 6383 | * the same on both snapshots. |
| 6384 | */ |
| 6385 | if (sctx->cur_ino != sctx->cmp_key->objectid) |
| 6386 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6387 | |
| 6388 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { |
| 6389 | if (result != BTRFS_COMPARE_TREE_DELETED) |
| 6390 | ret = process_extent(sctx, sctx->left_path, |
| 6391 | sctx->cmp_key); |
| 6392 | } |
| 6393 | |
| 6394 | return ret; |
| 6395 | } |
| 6396 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 6397 | static int dir_changed(struct send_ctx *sctx, u64 dir) |
| 6398 | { |
| 6399 | u64 orig_gen, new_gen; |
| 6400 | int ret; |
| 6401 | |
| 6402 | ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL, |
| 6403 | NULL, NULL); |
| 6404 | if (ret) |
| 6405 | return ret; |
| 6406 | |
| 6407 | ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL, |
| 6408 | NULL, NULL, NULL); |
| 6409 | if (ret) |
| 6410 | return ret; |
| 6411 | |
| 6412 | return (orig_gen != new_gen) ? 1 : 0; |
| 6413 | } |
| 6414 | |
| 6415 | static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path, |
| 6416 | struct btrfs_key *key) |
| 6417 | { |
| 6418 | struct btrfs_inode_extref *extref; |
| 6419 | struct extent_buffer *leaf; |
| 6420 | u64 dirid = 0, last_dirid = 0; |
| 6421 | unsigned long ptr; |
| 6422 | u32 item_size; |
| 6423 | u32 cur_offset = 0; |
| 6424 | int ref_name_len; |
| 6425 | int ret = 0; |
| 6426 | |
| 6427 | /* Easy case, just check this one dirid */ |
| 6428 | if (key->type == BTRFS_INODE_REF_KEY) { |
| 6429 | dirid = key->offset; |
| 6430 | |
| 6431 | ret = dir_changed(sctx, dirid); |
| 6432 | goto out; |
| 6433 | } |
| 6434 | |
| 6435 | leaf = path->nodes[0]; |
| 6436 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 6437 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 6438 | while (cur_offset < item_size) { |
| 6439 | extref = (struct btrfs_inode_extref *)(ptr + |
| 6440 | cur_offset); |
| 6441 | dirid = btrfs_inode_extref_parent(leaf, extref); |
| 6442 | ref_name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 6443 | cur_offset += ref_name_len + sizeof(*extref); |
| 6444 | if (dirid == last_dirid) |
| 6445 | continue; |
| 6446 | ret = dir_changed(sctx, dirid); |
| 6447 | if (ret) |
| 6448 | break; |
| 6449 | last_dirid = dirid; |
| 6450 | } |
| 6451 | out: |
| 6452 | return ret; |
| 6453 | } |
| 6454 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6455 | /* |
| 6456 | * Updates compare related fields in sctx and simply forwards to the actual |
| 6457 | * changed_xxx functions. |
| 6458 | */ |
Nikolay Borisov | ee8c494 | 2017-08-21 12:43:45 +0300 | [diff] [blame] | 6459 | static int changed_cb(struct btrfs_path *left_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6460 | struct btrfs_path *right_path, |
| 6461 | struct btrfs_key *key, |
| 6462 | enum btrfs_compare_tree_result result, |
| 6463 | void *ctx) |
| 6464 | { |
| 6465 | int ret = 0; |
| 6466 | struct send_ctx *sctx = ctx; |
| 6467 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 6468 | if (result == BTRFS_COMPARE_TREE_SAME) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 6469 | if (key->type == BTRFS_INODE_REF_KEY || |
| 6470 | key->type == BTRFS_INODE_EXTREF_KEY) { |
| 6471 | ret = compare_refs(sctx, left_path, key); |
| 6472 | if (!ret) |
| 6473 | return 0; |
| 6474 | if (ret < 0) |
| 6475 | return ret; |
| 6476 | } else if (key->type == BTRFS_EXTENT_DATA_KEY) { |
| 6477 | return maybe_send_hole(sctx, left_path, key); |
| 6478 | } else { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 6479 | return 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 6480 | } |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 6481 | result = BTRFS_COMPARE_TREE_CHANGED; |
| 6482 | ret = 0; |
| 6483 | } |
| 6484 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6485 | sctx->left_path = left_path; |
| 6486 | sctx->right_path = right_path; |
| 6487 | sctx->cmp_key = key; |
| 6488 | |
| 6489 | ret = finish_inode_if_needed(sctx, 0); |
| 6490 | if (ret < 0) |
| 6491 | goto out; |
| 6492 | |
Alexander Block | 2981e22 | 2012-08-01 14:47:03 +0200 | [diff] [blame] | 6493 | /* Ignore non-FS objects */ |
| 6494 | if (key->objectid == BTRFS_FREE_INO_OBJECTID || |
| 6495 | key->objectid == BTRFS_FREE_SPACE_OBJECTID) |
| 6496 | goto out; |
| 6497 | |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6498 | if (key->type == BTRFS_INODE_ITEM_KEY) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6499 | ret = changed_inode(sctx, result); |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6500 | } else if (!sctx->ignore_cur_inode) { |
| 6501 | if (key->type == BTRFS_INODE_REF_KEY || |
| 6502 | key->type == BTRFS_INODE_EXTREF_KEY) |
| 6503 | ret = changed_ref(sctx, result); |
| 6504 | else if (key->type == BTRFS_XATTR_ITEM_KEY) |
| 6505 | ret = changed_xattr(sctx, result); |
| 6506 | else if (key->type == BTRFS_EXTENT_DATA_KEY) |
| 6507 | ret = changed_extent(sctx, result); |
| 6508 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6509 | |
| 6510 | out: |
| 6511 | return ret; |
| 6512 | } |
| 6513 | |
| 6514 | static int full_send_tree(struct send_ctx *sctx) |
| 6515 | { |
| 6516 | int ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6517 | struct btrfs_root *send_root = sctx->send_root; |
| 6518 | struct btrfs_key key; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6519 | struct btrfs_path *path; |
| 6520 | struct extent_buffer *eb; |
| 6521 | int slot; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6522 | |
| 6523 | path = alloc_path_for_send(); |
| 6524 | if (!path) |
| 6525 | return -ENOMEM; |
| 6526 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6527 | key.objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 6528 | key.type = BTRFS_INODE_ITEM_KEY; |
| 6529 | key.offset = 0; |
| 6530 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6531 | ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0); |
| 6532 | if (ret < 0) |
| 6533 | goto out; |
| 6534 | if (ret) |
| 6535 | goto out_finish; |
| 6536 | |
| 6537 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6538 | eb = path->nodes[0]; |
| 6539 | slot = path->slots[0]; |
Filipe Manana | ca5d2ba | 2018-07-23 09:10:09 +0100 | [diff] [blame] | 6540 | btrfs_item_key_to_cpu(eb, &key, slot); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6541 | |
Filipe Manana | ca5d2ba | 2018-07-23 09:10:09 +0100 | [diff] [blame] | 6542 | ret = changed_cb(path, NULL, &key, |
Nikolay Borisov | ee8c494 | 2017-08-21 12:43:45 +0300 | [diff] [blame] | 6543 | BTRFS_COMPARE_TREE_NEW, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6544 | if (ret < 0) |
| 6545 | goto out; |
| 6546 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6547 | ret = btrfs_next_item(send_root, path); |
| 6548 | if (ret < 0) |
| 6549 | goto out; |
| 6550 | if (ret) { |
| 6551 | ret = 0; |
| 6552 | break; |
| 6553 | } |
| 6554 | } |
| 6555 | |
| 6556 | out_finish: |
| 6557 | ret = finish_inode_if_needed(sctx, 1); |
| 6558 | |
| 6559 | out: |
| 6560 | btrfs_free_path(path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6561 | return ret; |
| 6562 | } |
| 6563 | |
David Sterba | 18d0f5c | 2019-08-21 19:12:59 +0200 | [diff] [blame] | 6564 | static int tree_move_down(struct btrfs_path *path, int *level) |
| 6565 | { |
| 6566 | struct extent_buffer *eb; |
| 6567 | |
| 6568 | BUG_ON(*level == 0); |
| 6569 | eb = btrfs_read_node_slot(path->nodes[*level], path->slots[*level]); |
| 6570 | if (IS_ERR(eb)) |
| 6571 | return PTR_ERR(eb); |
| 6572 | |
| 6573 | path->nodes[*level - 1] = eb; |
| 6574 | path->slots[*level - 1] = 0; |
| 6575 | (*level)--; |
| 6576 | return 0; |
| 6577 | } |
| 6578 | |
| 6579 | static int tree_move_next_or_upnext(struct btrfs_path *path, |
| 6580 | int *level, int root_level) |
| 6581 | { |
| 6582 | int ret = 0; |
| 6583 | int nritems; |
| 6584 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 6585 | |
| 6586 | path->slots[*level]++; |
| 6587 | |
| 6588 | while (path->slots[*level] >= nritems) { |
| 6589 | if (*level == root_level) |
| 6590 | return -1; |
| 6591 | |
| 6592 | /* move upnext */ |
| 6593 | path->slots[*level] = 0; |
| 6594 | free_extent_buffer(path->nodes[*level]); |
| 6595 | path->nodes[*level] = NULL; |
| 6596 | (*level)++; |
| 6597 | path->slots[*level]++; |
| 6598 | |
| 6599 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 6600 | ret = 1; |
| 6601 | } |
| 6602 | return ret; |
| 6603 | } |
| 6604 | |
| 6605 | /* |
| 6606 | * Returns 1 if it had to move up and next. 0 is returned if it moved only next |
| 6607 | * or down. |
| 6608 | */ |
| 6609 | static int tree_advance(struct btrfs_path *path, |
| 6610 | int *level, int root_level, |
| 6611 | int allow_down, |
| 6612 | struct btrfs_key *key) |
| 6613 | { |
| 6614 | int ret; |
| 6615 | |
| 6616 | if (*level == 0 || !allow_down) { |
| 6617 | ret = tree_move_next_or_upnext(path, level, root_level); |
| 6618 | } else { |
| 6619 | ret = tree_move_down(path, level); |
| 6620 | } |
| 6621 | if (ret >= 0) { |
| 6622 | if (*level == 0) |
| 6623 | btrfs_item_key_to_cpu(path->nodes[*level], key, |
| 6624 | path->slots[*level]); |
| 6625 | else |
| 6626 | btrfs_node_key_to_cpu(path->nodes[*level], key, |
| 6627 | path->slots[*level]); |
| 6628 | } |
| 6629 | return ret; |
| 6630 | } |
| 6631 | |
| 6632 | static int tree_compare_item(struct btrfs_path *left_path, |
| 6633 | struct btrfs_path *right_path, |
| 6634 | char *tmp_buf) |
| 6635 | { |
| 6636 | int cmp; |
| 6637 | int len1, len2; |
| 6638 | unsigned long off1, off2; |
| 6639 | |
| 6640 | len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); |
| 6641 | len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); |
| 6642 | if (len1 != len2) |
| 6643 | return 1; |
| 6644 | |
| 6645 | off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); |
| 6646 | off2 = btrfs_item_ptr_offset(right_path->nodes[0], |
| 6647 | right_path->slots[0]); |
| 6648 | |
| 6649 | read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); |
| 6650 | |
| 6651 | cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); |
| 6652 | if (cmp) |
| 6653 | return 1; |
| 6654 | return 0; |
| 6655 | } |
| 6656 | |
| 6657 | /* |
| 6658 | * This function compares two trees and calls the provided callback for |
| 6659 | * every changed/new/deleted item it finds. |
| 6660 | * If shared tree blocks are encountered, whole subtrees are skipped, making |
| 6661 | * the compare pretty fast on snapshotted subvolumes. |
| 6662 | * |
| 6663 | * This currently works on commit roots only. As commit roots are read only, |
| 6664 | * we don't do any locking. The commit roots are protected with transactions. |
| 6665 | * Transactions are ended and rejoined when a commit is tried in between. |
| 6666 | * |
| 6667 | * This function checks for modifications done to the trees while comparing. |
| 6668 | * If it detects a change, it aborts immediately. |
| 6669 | */ |
| 6670 | static int btrfs_compare_trees(struct btrfs_root *left_root, |
David Sterba | 1b51d6f | 2020-08-17 12:16:57 +0200 | [diff] [blame] | 6671 | struct btrfs_root *right_root, void *ctx) |
David Sterba | 18d0f5c | 2019-08-21 19:12:59 +0200 | [diff] [blame] | 6672 | { |
| 6673 | struct btrfs_fs_info *fs_info = left_root->fs_info; |
| 6674 | int ret; |
| 6675 | int cmp; |
| 6676 | struct btrfs_path *left_path = NULL; |
| 6677 | struct btrfs_path *right_path = NULL; |
| 6678 | struct btrfs_key left_key; |
| 6679 | struct btrfs_key right_key; |
| 6680 | char *tmp_buf = NULL; |
| 6681 | int left_root_level; |
| 6682 | int right_root_level; |
| 6683 | int left_level; |
| 6684 | int right_level; |
| 6685 | int left_end_reached; |
| 6686 | int right_end_reached; |
| 6687 | int advance_left; |
| 6688 | int advance_right; |
| 6689 | u64 left_blockptr; |
| 6690 | u64 right_blockptr; |
| 6691 | u64 left_gen; |
| 6692 | u64 right_gen; |
| 6693 | |
| 6694 | left_path = btrfs_alloc_path(); |
| 6695 | if (!left_path) { |
| 6696 | ret = -ENOMEM; |
| 6697 | goto out; |
| 6698 | } |
| 6699 | right_path = btrfs_alloc_path(); |
| 6700 | if (!right_path) { |
| 6701 | ret = -ENOMEM; |
| 6702 | goto out; |
| 6703 | } |
| 6704 | |
| 6705 | tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); |
| 6706 | if (!tmp_buf) { |
| 6707 | ret = -ENOMEM; |
| 6708 | goto out; |
| 6709 | } |
| 6710 | |
| 6711 | left_path->search_commit_root = 1; |
| 6712 | left_path->skip_locking = 1; |
| 6713 | right_path->search_commit_root = 1; |
| 6714 | right_path->skip_locking = 1; |
| 6715 | |
| 6716 | /* |
| 6717 | * Strategy: Go to the first items of both trees. Then do |
| 6718 | * |
| 6719 | * If both trees are at level 0 |
| 6720 | * Compare keys of current items |
| 6721 | * If left < right treat left item as new, advance left tree |
| 6722 | * and repeat |
| 6723 | * If left > right treat right item as deleted, advance right tree |
| 6724 | * and repeat |
| 6725 | * If left == right do deep compare of items, treat as changed if |
| 6726 | * needed, advance both trees and repeat |
| 6727 | * If both trees are at the same level but not at level 0 |
| 6728 | * Compare keys of current nodes/leafs |
| 6729 | * If left < right advance left tree and repeat |
| 6730 | * If left > right advance right tree and repeat |
| 6731 | * If left == right compare blockptrs of the next nodes/leafs |
| 6732 | * If they match advance both trees but stay at the same level |
| 6733 | * and repeat |
| 6734 | * If they don't match advance both trees while allowing to go |
| 6735 | * deeper and repeat |
| 6736 | * If tree levels are different |
| 6737 | * Advance the tree that needs it and repeat |
| 6738 | * |
| 6739 | * Advancing a tree means: |
| 6740 | * If we are at level 0, try to go to the next slot. If that's not |
| 6741 | * possible, go one level up and repeat. Stop when we found a level |
| 6742 | * where we could go to the next slot. We may at this point be on a |
| 6743 | * node or a leaf. |
| 6744 | * |
| 6745 | * If we are not at level 0 and not on shared tree blocks, go one |
| 6746 | * level deeper. |
| 6747 | * |
| 6748 | * If we are not at level 0 and on shared tree blocks, go one slot to |
| 6749 | * the right if possible or go up and right. |
| 6750 | */ |
| 6751 | |
| 6752 | down_read(&fs_info->commit_root_sem); |
| 6753 | left_level = btrfs_header_level(left_root->commit_root); |
| 6754 | left_root_level = left_level; |
| 6755 | left_path->nodes[left_level] = |
| 6756 | btrfs_clone_extent_buffer(left_root->commit_root); |
| 6757 | if (!left_path->nodes[left_level]) { |
| 6758 | up_read(&fs_info->commit_root_sem); |
| 6759 | ret = -ENOMEM; |
| 6760 | goto out; |
| 6761 | } |
| 6762 | |
| 6763 | right_level = btrfs_header_level(right_root->commit_root); |
| 6764 | right_root_level = right_level; |
| 6765 | right_path->nodes[right_level] = |
| 6766 | btrfs_clone_extent_buffer(right_root->commit_root); |
| 6767 | if (!right_path->nodes[right_level]) { |
| 6768 | up_read(&fs_info->commit_root_sem); |
| 6769 | ret = -ENOMEM; |
| 6770 | goto out; |
| 6771 | } |
| 6772 | up_read(&fs_info->commit_root_sem); |
| 6773 | |
| 6774 | if (left_level == 0) |
| 6775 | btrfs_item_key_to_cpu(left_path->nodes[left_level], |
| 6776 | &left_key, left_path->slots[left_level]); |
| 6777 | else |
| 6778 | btrfs_node_key_to_cpu(left_path->nodes[left_level], |
| 6779 | &left_key, left_path->slots[left_level]); |
| 6780 | if (right_level == 0) |
| 6781 | btrfs_item_key_to_cpu(right_path->nodes[right_level], |
| 6782 | &right_key, right_path->slots[right_level]); |
| 6783 | else |
| 6784 | btrfs_node_key_to_cpu(right_path->nodes[right_level], |
| 6785 | &right_key, right_path->slots[right_level]); |
| 6786 | |
| 6787 | left_end_reached = right_end_reached = 0; |
| 6788 | advance_left = advance_right = 0; |
| 6789 | |
| 6790 | while (1) { |
Nikolay Borisov | 6af112b | 2019-09-04 19:33:58 +0300 | [diff] [blame] | 6791 | cond_resched(); |
David Sterba | 18d0f5c | 2019-08-21 19:12:59 +0200 | [diff] [blame] | 6792 | if (advance_left && !left_end_reached) { |
| 6793 | ret = tree_advance(left_path, &left_level, |
| 6794 | left_root_level, |
| 6795 | advance_left != ADVANCE_ONLY_NEXT, |
| 6796 | &left_key); |
| 6797 | if (ret == -1) |
| 6798 | left_end_reached = ADVANCE; |
| 6799 | else if (ret < 0) |
| 6800 | goto out; |
| 6801 | advance_left = 0; |
| 6802 | } |
| 6803 | if (advance_right && !right_end_reached) { |
| 6804 | ret = tree_advance(right_path, &right_level, |
| 6805 | right_root_level, |
| 6806 | advance_right != ADVANCE_ONLY_NEXT, |
| 6807 | &right_key); |
| 6808 | if (ret == -1) |
| 6809 | right_end_reached = ADVANCE; |
| 6810 | else if (ret < 0) |
| 6811 | goto out; |
| 6812 | advance_right = 0; |
| 6813 | } |
| 6814 | |
| 6815 | if (left_end_reached && right_end_reached) { |
| 6816 | ret = 0; |
| 6817 | goto out; |
| 6818 | } else if (left_end_reached) { |
| 6819 | if (right_level == 0) { |
| 6820 | ret = changed_cb(left_path, right_path, |
| 6821 | &right_key, |
| 6822 | BTRFS_COMPARE_TREE_DELETED, |
| 6823 | ctx); |
| 6824 | if (ret < 0) |
| 6825 | goto out; |
| 6826 | } |
| 6827 | advance_right = ADVANCE; |
| 6828 | continue; |
| 6829 | } else if (right_end_reached) { |
| 6830 | if (left_level == 0) { |
| 6831 | ret = changed_cb(left_path, right_path, |
| 6832 | &left_key, |
| 6833 | BTRFS_COMPARE_TREE_NEW, |
| 6834 | ctx); |
| 6835 | if (ret < 0) |
| 6836 | goto out; |
| 6837 | } |
| 6838 | advance_left = ADVANCE; |
| 6839 | continue; |
| 6840 | } |
| 6841 | |
| 6842 | if (left_level == 0 && right_level == 0) { |
| 6843 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 6844 | if (cmp < 0) { |
| 6845 | ret = changed_cb(left_path, right_path, |
| 6846 | &left_key, |
| 6847 | BTRFS_COMPARE_TREE_NEW, |
| 6848 | ctx); |
| 6849 | if (ret < 0) |
| 6850 | goto out; |
| 6851 | advance_left = ADVANCE; |
| 6852 | } else if (cmp > 0) { |
| 6853 | ret = changed_cb(left_path, right_path, |
| 6854 | &right_key, |
| 6855 | BTRFS_COMPARE_TREE_DELETED, |
| 6856 | ctx); |
| 6857 | if (ret < 0) |
| 6858 | goto out; |
| 6859 | advance_right = ADVANCE; |
| 6860 | } else { |
| 6861 | enum btrfs_compare_tree_result result; |
| 6862 | |
| 6863 | WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); |
| 6864 | ret = tree_compare_item(left_path, right_path, |
| 6865 | tmp_buf); |
| 6866 | if (ret) |
| 6867 | result = BTRFS_COMPARE_TREE_CHANGED; |
| 6868 | else |
| 6869 | result = BTRFS_COMPARE_TREE_SAME; |
| 6870 | ret = changed_cb(left_path, right_path, |
| 6871 | &left_key, result, ctx); |
| 6872 | if (ret < 0) |
| 6873 | goto out; |
| 6874 | advance_left = ADVANCE; |
| 6875 | advance_right = ADVANCE; |
| 6876 | } |
| 6877 | } else if (left_level == right_level) { |
| 6878 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 6879 | if (cmp < 0) { |
| 6880 | advance_left = ADVANCE; |
| 6881 | } else if (cmp > 0) { |
| 6882 | advance_right = ADVANCE; |
| 6883 | } else { |
| 6884 | left_blockptr = btrfs_node_blockptr( |
| 6885 | left_path->nodes[left_level], |
| 6886 | left_path->slots[left_level]); |
| 6887 | right_blockptr = btrfs_node_blockptr( |
| 6888 | right_path->nodes[right_level], |
| 6889 | right_path->slots[right_level]); |
| 6890 | left_gen = btrfs_node_ptr_generation( |
| 6891 | left_path->nodes[left_level], |
| 6892 | left_path->slots[left_level]); |
| 6893 | right_gen = btrfs_node_ptr_generation( |
| 6894 | right_path->nodes[right_level], |
| 6895 | right_path->slots[right_level]); |
| 6896 | if (left_blockptr == right_blockptr && |
| 6897 | left_gen == right_gen) { |
| 6898 | /* |
| 6899 | * As we're on a shared block, don't |
| 6900 | * allow to go deeper. |
| 6901 | */ |
| 6902 | advance_left = ADVANCE_ONLY_NEXT; |
| 6903 | advance_right = ADVANCE_ONLY_NEXT; |
| 6904 | } else { |
| 6905 | advance_left = ADVANCE; |
| 6906 | advance_right = ADVANCE; |
| 6907 | } |
| 6908 | } |
| 6909 | } else if (left_level < right_level) { |
| 6910 | advance_right = ADVANCE; |
| 6911 | } else { |
| 6912 | advance_left = ADVANCE; |
| 6913 | } |
| 6914 | } |
| 6915 | |
| 6916 | out: |
| 6917 | btrfs_free_path(left_path); |
| 6918 | btrfs_free_path(right_path); |
| 6919 | kvfree(tmp_buf); |
| 6920 | return ret; |
| 6921 | } |
| 6922 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6923 | static int send_subvol(struct send_ctx *sctx) |
| 6924 | { |
| 6925 | int ret; |
| 6926 | |
Stefan Behrens | c2c7132 | 2013-04-10 17:10:52 +0000 | [diff] [blame] | 6927 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) { |
| 6928 | ret = send_header(sctx); |
| 6929 | if (ret < 0) |
| 6930 | goto out; |
| 6931 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6932 | |
| 6933 | ret = send_subvol_begin(sctx); |
| 6934 | if (ret < 0) |
| 6935 | goto out; |
| 6936 | |
| 6937 | if (sctx->parent_root) { |
David Sterba | 1b51d6f | 2020-08-17 12:16:57 +0200 | [diff] [blame] | 6938 | ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6939 | if (ret < 0) |
| 6940 | goto out; |
| 6941 | ret = finish_inode_if_needed(sctx, 1); |
| 6942 | if (ret < 0) |
| 6943 | goto out; |
| 6944 | } else { |
| 6945 | ret = full_send_tree(sctx); |
| 6946 | if (ret < 0) |
| 6947 | goto out; |
| 6948 | } |
| 6949 | |
| 6950 | out: |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6951 | free_recorded_refs(sctx); |
| 6952 | return ret; |
| 6953 | } |
| 6954 | |
Filipe Manana | e5fa8f8 | 2014-10-21 11:11:41 +0100 | [diff] [blame] | 6955 | /* |
| 6956 | * If orphan cleanup did remove any orphans from a root, it means the tree |
| 6957 | * was modified and therefore the commit root is not the same as the current |
| 6958 | * root anymore. This is a problem, because send uses the commit root and |
| 6959 | * therefore can see inode items that don't exist in the current root anymore, |
| 6960 | * and for example make calls to btrfs_iget, which will do tree lookups based |
| 6961 | * on the current root and not on the commit root. Those lookups will fail, |
| 6962 | * returning a -ESTALE error, and making send fail with that error. So make |
| 6963 | * sure a send does not see any orphans we have just removed, and that it will |
| 6964 | * see the same inodes regardless of whether a transaction commit happened |
| 6965 | * before it started (meaning that the commit root will be the same as the |
| 6966 | * current root) or not. |
| 6967 | */ |
| 6968 | static int ensure_commit_roots_uptodate(struct send_ctx *sctx) |
| 6969 | { |
| 6970 | int i; |
| 6971 | struct btrfs_trans_handle *trans = NULL; |
| 6972 | |
| 6973 | again: |
| 6974 | if (sctx->parent_root && |
| 6975 | sctx->parent_root->node != sctx->parent_root->commit_root) |
| 6976 | goto commit_trans; |
| 6977 | |
| 6978 | for (i = 0; i < sctx->clone_roots_cnt; i++) |
| 6979 | if (sctx->clone_roots[i].root->node != |
| 6980 | sctx->clone_roots[i].root->commit_root) |
| 6981 | goto commit_trans; |
| 6982 | |
| 6983 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6984 | return btrfs_end_transaction(trans); |
Filipe Manana | e5fa8f8 | 2014-10-21 11:11:41 +0100 | [diff] [blame] | 6985 | |
| 6986 | return 0; |
| 6987 | |
| 6988 | commit_trans: |
| 6989 | /* Use any root, all fs roots will get their commit roots updated. */ |
| 6990 | if (!trans) { |
| 6991 | trans = btrfs_join_transaction(sctx->send_root); |
| 6992 | if (IS_ERR(trans)) |
| 6993 | return PTR_ERR(trans); |
| 6994 | goto again; |
| 6995 | } |
| 6996 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6997 | return btrfs_commit_transaction(trans); |
Filipe Manana | e5fa8f8 | 2014-10-21 11:11:41 +0100 | [diff] [blame] | 6998 | } |
| 6999 | |
Filipe Manana | 9f89d5d | 2019-04-15 09:29:36 +0100 | [diff] [blame] | 7000 | /* |
| 7001 | * Make sure any existing dellaloc is flushed for any root used by a send |
| 7002 | * operation so that we do not miss any data and we do not race with writeback |
| 7003 | * finishing and changing a tree while send is using the tree. This could |
| 7004 | * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and |
| 7005 | * a send operation then uses the subvolume. |
| 7006 | * After flushing delalloc ensure_commit_roots_uptodate() must be called. |
| 7007 | */ |
| 7008 | static int flush_delalloc_roots(struct send_ctx *sctx) |
| 7009 | { |
| 7010 | struct btrfs_root *root = sctx->parent_root; |
| 7011 | int ret; |
| 7012 | int i; |
| 7013 | |
| 7014 | if (root) { |
| 7015 | ret = btrfs_start_delalloc_snapshot(root); |
| 7016 | if (ret) |
| 7017 | return ret; |
| 7018 | btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX); |
| 7019 | } |
| 7020 | |
| 7021 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 7022 | root = sctx->clone_roots[i].root; |
| 7023 | ret = btrfs_start_delalloc_snapshot(root); |
| 7024 | if (ret) |
| 7025 | return ret; |
| 7026 | btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX); |
| 7027 | } |
| 7028 | |
| 7029 | return 0; |
| 7030 | } |
| 7031 | |
David Sterba | 66ef7d6 | 2013-12-17 15:07:20 +0100 | [diff] [blame] | 7032 | static void btrfs_root_dec_send_in_progress(struct btrfs_root* root) |
| 7033 | { |
| 7034 | spin_lock(&root->root_item_lock); |
| 7035 | root->send_in_progress--; |
| 7036 | /* |
| 7037 | * Not much left to do, we don't know why it's unbalanced and |
| 7038 | * can't blindly reset it to 0. |
| 7039 | */ |
| 7040 | if (root->send_in_progress < 0) |
| 7041 | btrfs_err(root->fs_info, |
Colin Ian King | f5686e3 | 2018-05-04 12:11:12 +0100 | [diff] [blame] | 7042 | "send_in_progress unbalanced %d root %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7043 | root->send_in_progress, root->root_key.objectid); |
David Sterba | 66ef7d6 | 2013-12-17 15:07:20 +0100 | [diff] [blame] | 7044 | spin_unlock(&root->root_item_lock); |
| 7045 | } |
| 7046 | |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7047 | static void dedupe_in_progress_warn(const struct btrfs_root *root) |
| 7048 | { |
| 7049 | btrfs_warn_rl(root->fs_info, |
| 7050 | "cannot use root %llu for send while deduplications on it are in progress (%d in progress)", |
| 7051 | root->root_key.objectid, root->dedupe_in_progress); |
| 7052 | } |
| 7053 | |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 7054 | long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7055 | { |
| 7056 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7057 | struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root; |
| 7058 | struct btrfs_fs_info *fs_info = send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7059 | struct btrfs_root *clone_root; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7060 | struct send_ctx *sctx = NULL; |
| 7061 | u32 i; |
| 7062 | u64 *clone_sources_tmp = NULL; |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7063 | int clone_sources_to_rollback = 0; |
Denis Efremov | bae12df | 2020-09-21 20:03:36 +0300 | [diff] [blame^] | 7064 | size_t alloc_size; |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7065 | int sort_clone_roots = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7066 | |
| 7067 | if (!capable(CAP_SYS_ADMIN)) |
| 7068 | return -EPERM; |
| 7069 | |
Josef Bacik | 139f807 | 2013-05-20 11:26:50 -0400 | [diff] [blame] | 7070 | /* |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7071 | * The subvolume must remain read-only during send, protect against |
David Sterba | 521e054 | 2014-04-15 16:41:44 +0200 | [diff] [blame] | 7072 | * making it RW. This also protects against deletion. |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7073 | */ |
| 7074 | spin_lock(&send_root->root_item_lock); |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7075 | if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) { |
| 7076 | dedupe_in_progress_warn(send_root); |
| 7077 | spin_unlock(&send_root->root_item_lock); |
| 7078 | return -EAGAIN; |
| 7079 | } |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7080 | send_root->send_in_progress++; |
| 7081 | spin_unlock(&send_root->root_item_lock); |
| 7082 | |
| 7083 | /* |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7084 | * Userspace tools do the checks and warn the user if it's |
| 7085 | * not RO. |
| 7086 | */ |
| 7087 | if (!btrfs_root_readonly(send_root)) { |
| 7088 | ret = -EPERM; |
| 7089 | goto out; |
| 7090 | } |
| 7091 | |
Dan Carpenter | 457ae72 | 2017-03-17 23:51:20 +0300 | [diff] [blame] | 7092 | /* |
| 7093 | * Check that we don't overflow at later allocations, we request |
| 7094 | * clone_sources_count + 1 items, and compare to unsigned long inside |
| 7095 | * access_ok. |
| 7096 | */ |
Dan Carpenter | f5ecec3 | 2016-04-13 09:40:59 +0300 | [diff] [blame] | 7097 | if (arg->clone_sources_count > |
Dan Carpenter | 457ae72 | 2017-03-17 23:51:20 +0300 | [diff] [blame] | 7098 | ULONG_MAX / sizeof(struct clone_root) - 1) { |
Dan Carpenter | f5ecec3 | 2016-04-13 09:40:59 +0300 | [diff] [blame] | 7099 | ret = -EINVAL; |
| 7100 | goto out; |
| 7101 | } |
| 7102 | |
Stefan Behrens | c2c7132 | 2013-04-10 17:10:52 +0000 | [diff] [blame] | 7103 | if (arg->flags & ~BTRFS_SEND_FLAG_MASK) { |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 7104 | ret = -EINVAL; |
| 7105 | goto out; |
| 7106 | } |
| 7107 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 7108 | sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7109 | if (!sctx) { |
| 7110 | ret = -ENOMEM; |
| 7111 | goto out; |
| 7112 | } |
| 7113 | |
| 7114 | INIT_LIST_HEAD(&sctx->new_refs); |
| 7115 | INIT_LIST_HEAD(&sctx->deleted_refs); |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 7116 | INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7117 | INIT_LIST_HEAD(&sctx->name_cache_list); |
| 7118 | |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 7119 | sctx->flags = arg->flags; |
| 7120 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7121 | sctx->send_filp = fget(arg->send_fd); |
Tsutomu Itoh | ecc7ada | 2013-04-19 01:04:46 +0000 | [diff] [blame] | 7122 | if (!sctx->send_filp) { |
| 7123 | ret = -EBADF; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7124 | goto out; |
| 7125 | } |
| 7126 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7127 | sctx->send_root = send_root; |
David Sterba | 521e054 | 2014-04-15 16:41:44 +0200 | [diff] [blame] | 7128 | /* |
| 7129 | * Unlikely but possible, if the subvolume is marked for deletion but |
| 7130 | * is slow to remove the directory entry, send can still be started |
| 7131 | */ |
| 7132 | if (btrfs_root_dead(sctx->send_root)) { |
| 7133 | ret = -EPERM; |
| 7134 | goto out; |
| 7135 | } |
| 7136 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7137 | sctx->clone_roots_cnt = arg->clone_sources_count; |
| 7138 | |
| 7139 | sctx->send_max_size = BTRFS_SEND_BUF_SIZE; |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7140 | sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7141 | if (!sctx->send_buf) { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7142 | ret = -ENOMEM; |
| 7143 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7144 | } |
| 7145 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 7146 | sctx->pending_dir_moves = RB_ROOT; |
| 7147 | sctx->waiting_dir_moves = RB_ROOT; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 7148 | sctx->orphan_dirs = RB_ROOT; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 7149 | |
Denis Efremov | bae12df | 2020-09-21 20:03:36 +0300 | [diff] [blame^] | 7150 | sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots), |
| 7151 | arg->clone_sources_count + 1, |
| 7152 | GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7153 | if (!sctx->clone_roots) { |
David Sterba | 818e010 | 2017-05-31 18:40:02 +0200 | [diff] [blame] | 7154 | ret = -ENOMEM; |
| 7155 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7156 | } |
| 7157 | |
Denis Efremov | bae12df | 2020-09-21 20:03:36 +0300 | [diff] [blame^] | 7158 | alloc_size = array_size(sizeof(*arg->clone_sources), |
| 7159 | arg->clone_sources_count); |
David Sterba | e55d115 | 2016-04-11 18:52:02 +0200 | [diff] [blame] | 7160 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7161 | if (arg->clone_sources_count) { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7162 | clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7163 | if (!clone_sources_tmp) { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7164 | ret = -ENOMEM; |
| 7165 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | ret = copy_from_user(clone_sources_tmp, arg->clone_sources, |
David Sterba | e55d115 | 2016-04-11 18:52:02 +0200 | [diff] [blame] | 7169 | alloc_size); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7170 | if (ret) { |
| 7171 | ret = -EFAULT; |
| 7172 | goto out; |
| 7173 | } |
| 7174 | |
| 7175 | for (i = 0; i < arg->clone_sources_count; i++) { |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 7176 | clone_root = btrfs_get_fs_root(fs_info, |
| 7177 | clone_sources_tmp[i], true); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7178 | if (IS_ERR(clone_root)) { |
| 7179 | ret = PTR_ERR(clone_root); |
| 7180 | goto out; |
| 7181 | } |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7182 | spin_lock(&clone_root->root_item_lock); |
Filipe Manana | 5cc2b17 | 2015-03-02 20:53:52 +0000 | [diff] [blame] | 7183 | if (!btrfs_root_readonly(clone_root) || |
| 7184 | btrfs_root_dead(clone_root)) { |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7185 | spin_unlock(&clone_root->root_item_lock); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7186 | btrfs_put_root(clone_root); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7187 | ret = -EPERM; |
| 7188 | goto out; |
| 7189 | } |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7190 | if (clone_root->dedupe_in_progress) { |
| 7191 | dedupe_in_progress_warn(clone_root); |
| 7192 | spin_unlock(&clone_root->root_item_lock); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7193 | btrfs_put_root(clone_root); |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7194 | ret = -EAGAIN; |
| 7195 | goto out; |
| 7196 | } |
Filipe Manana | 2f1f465 | 2015-03-02 20:53:53 +0000 | [diff] [blame] | 7197 | clone_root->send_in_progress++; |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7198 | spin_unlock(&clone_root->root_item_lock); |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7199 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7200 | sctx->clone_roots[i].root = clone_root; |
Filipe Manana | 2f1f465 | 2015-03-02 20:53:53 +0000 | [diff] [blame] | 7201 | clone_sources_to_rollback = i + 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7202 | } |
David Sterba | 2f91306 | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 7203 | kvfree(clone_sources_tmp); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7204 | clone_sources_tmp = NULL; |
| 7205 | } |
| 7206 | |
| 7207 | if (arg->parent_root) { |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 7208 | sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root, |
| 7209 | true); |
Stefan Behrens | b1b1959 | 2013-05-13 14:42:57 +0000 | [diff] [blame] | 7210 | if (IS_ERR(sctx->parent_root)) { |
| 7211 | ret = PTR_ERR(sctx->parent_root); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7212 | goto out; |
| 7213 | } |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7214 | |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7215 | spin_lock(&sctx->parent_root->root_item_lock); |
| 7216 | sctx->parent_root->send_in_progress++; |
David Sterba | 521e054 | 2014-04-15 16:41:44 +0200 | [diff] [blame] | 7217 | if (!btrfs_root_readonly(sctx->parent_root) || |
| 7218 | btrfs_root_dead(sctx->parent_root)) { |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7219 | spin_unlock(&sctx->parent_root->root_item_lock); |
| 7220 | ret = -EPERM; |
| 7221 | goto out; |
| 7222 | } |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7223 | if (sctx->parent_root->dedupe_in_progress) { |
| 7224 | dedupe_in_progress_warn(sctx->parent_root); |
| 7225 | spin_unlock(&sctx->parent_root->root_item_lock); |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7226 | ret = -EAGAIN; |
| 7227 | goto out; |
| 7228 | } |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7229 | spin_unlock(&sctx->parent_root->root_item_lock); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7230 | } |
| 7231 | |
| 7232 | /* |
| 7233 | * Clones from send_root are allowed, but only if the clone source |
| 7234 | * is behind the current send position. This is checked while searching |
| 7235 | * for possible clone sources. |
| 7236 | */ |
Josef Bacik | 6f9a3da | 2020-01-24 09:32:48 -0500 | [diff] [blame] | 7237 | sctx->clone_roots[sctx->clone_roots_cnt++].root = |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7238 | btrfs_grab_root(sctx->send_root); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7239 | |
| 7240 | /* We do a bsearch later */ |
| 7241 | sort(sctx->clone_roots, sctx->clone_roots_cnt, |
| 7242 | sizeof(*sctx->clone_roots), __clone_root_cmp_sort, |
| 7243 | NULL); |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7244 | sort_clone_roots = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7245 | |
Filipe Manana | 9f89d5d | 2019-04-15 09:29:36 +0100 | [diff] [blame] | 7246 | ret = flush_delalloc_roots(sctx); |
| 7247 | if (ret) |
| 7248 | goto out; |
| 7249 | |
Filipe Manana | e5fa8f8 | 2014-10-21 11:11:41 +0100 | [diff] [blame] | 7250 | ret = ensure_commit_roots_uptodate(sctx); |
| 7251 | if (ret) |
| 7252 | goto out; |
| 7253 | |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 7254 | mutex_lock(&fs_info->balance_mutex); |
| 7255 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
| 7256 | mutex_unlock(&fs_info->balance_mutex); |
| 7257 | btrfs_warn_rl(fs_info, |
| 7258 | "cannot run send because a balance operation is in progress"); |
| 7259 | ret = -EAGAIN; |
| 7260 | goto out; |
| 7261 | } |
| 7262 | fs_info->send_in_progress++; |
| 7263 | mutex_unlock(&fs_info->balance_mutex); |
| 7264 | |
David Sterba | 2755a0d | 2014-07-31 00:43:18 +0200 | [diff] [blame] | 7265 | current->journal_info = BTRFS_SEND_TRANS_STUB; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7266 | ret = send_subvol(sctx); |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 7267 | current->journal_info = NULL; |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 7268 | mutex_lock(&fs_info->balance_mutex); |
| 7269 | fs_info->send_in_progress--; |
| 7270 | mutex_unlock(&fs_info->balance_mutex); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7271 | if (ret < 0) |
| 7272 | goto out; |
| 7273 | |
Stefan Behrens | c2c7132 | 2013-04-10 17:10:52 +0000 | [diff] [blame] | 7274 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) { |
| 7275 | ret = begin_cmd(sctx, BTRFS_SEND_C_END); |
| 7276 | if (ret < 0) |
| 7277 | goto out; |
| 7278 | ret = send_cmd(sctx); |
| 7279 | if (ret < 0) |
| 7280 | goto out; |
| 7281 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7282 | |
| 7283 | out: |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 7284 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)); |
| 7285 | while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) { |
| 7286 | struct rb_node *n; |
| 7287 | struct pending_dir_move *pm; |
| 7288 | |
| 7289 | n = rb_first(&sctx->pending_dir_moves); |
| 7290 | pm = rb_entry(n, struct pending_dir_move, node); |
| 7291 | while (!list_empty(&pm->list)) { |
| 7292 | struct pending_dir_move *pm2; |
| 7293 | |
| 7294 | pm2 = list_first_entry(&pm->list, |
| 7295 | struct pending_dir_move, list); |
| 7296 | free_pending_move(sctx, pm2); |
| 7297 | } |
| 7298 | free_pending_move(sctx, pm); |
| 7299 | } |
| 7300 | |
| 7301 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)); |
| 7302 | while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) { |
| 7303 | struct rb_node *n; |
| 7304 | struct waiting_dir_move *dm; |
| 7305 | |
| 7306 | n = rb_first(&sctx->waiting_dir_moves); |
| 7307 | dm = rb_entry(n, struct waiting_dir_move, node); |
| 7308 | rb_erase(&dm->node, &sctx->waiting_dir_moves); |
| 7309 | kfree(dm); |
| 7310 | } |
| 7311 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 7312 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs)); |
| 7313 | while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) { |
| 7314 | struct rb_node *n; |
| 7315 | struct orphan_dir_info *odi; |
| 7316 | |
| 7317 | n = rb_first(&sctx->orphan_dirs); |
| 7318 | odi = rb_entry(n, struct orphan_dir_info, node); |
| 7319 | free_orphan_dir_info(sctx, odi); |
| 7320 | } |
| 7321 | |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7322 | if (sort_clone_roots) { |
Josef Bacik | 6f9a3da | 2020-01-24 09:32:48 -0500 | [diff] [blame] | 7323 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7324 | btrfs_root_dec_send_in_progress( |
| 7325 | sctx->clone_roots[i].root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7326 | btrfs_put_root(sctx->clone_roots[i].root); |
Josef Bacik | 6f9a3da | 2020-01-24 09:32:48 -0500 | [diff] [blame] | 7327 | } |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7328 | } else { |
Josef Bacik | 6f9a3da | 2020-01-24 09:32:48 -0500 | [diff] [blame] | 7329 | for (i = 0; sctx && i < clone_sources_to_rollback; i++) { |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7330 | btrfs_root_dec_send_in_progress( |
| 7331 | sctx->clone_roots[i].root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7332 | btrfs_put_root(sctx->clone_roots[i].root); |
Josef Bacik | 6f9a3da | 2020-01-24 09:32:48 -0500 | [diff] [blame] | 7333 | } |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7334 | |
| 7335 | btrfs_root_dec_send_in_progress(send_root); |
| 7336 | } |
Josef Bacik | 6f9a3da | 2020-01-24 09:32:48 -0500 | [diff] [blame] | 7337 | if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) { |
David Sterba | 66ef7d6 | 2013-12-17 15:07:20 +0100 | [diff] [blame] | 7338 | btrfs_root_dec_send_in_progress(sctx->parent_root); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 7339 | btrfs_put_root(sctx->parent_root); |
Josef Bacik | 6f9a3da | 2020-01-24 09:32:48 -0500 | [diff] [blame] | 7340 | } |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7341 | |
David Sterba | 2f91306 | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 7342 | kvfree(clone_sources_tmp); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7343 | |
| 7344 | if (sctx) { |
| 7345 | if (sctx->send_filp) |
| 7346 | fput(sctx->send_filp); |
| 7347 | |
David Sterba | c03d01f | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 7348 | kvfree(sctx->clone_roots); |
David Sterba | 6ff48ce | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 7349 | kvfree(sctx->send_buf); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7350 | |
| 7351 | name_cache_free(sctx); |
| 7352 | |
| 7353 | kfree(sctx); |
| 7354 | } |
| 7355 | |
| 7356 | return ret; |
| 7357 | } |