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" |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 26 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 27 | /* |
| 28 | * A fs_path is a helper to dynamically build path names with unknown size. |
| 29 | * It reallocates the internal buffer on demand. |
| 30 | * It allows fast adding of path elements on the right side (normal path) and |
| 31 | * fast adding to the left side (reversed path). A reversed path can also be |
| 32 | * unreversed if needed. |
| 33 | */ |
| 34 | struct fs_path { |
| 35 | union { |
| 36 | struct { |
| 37 | char *start; |
| 38 | char *end; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 39 | |
| 40 | char *buf; |
David Sterba | 1f5a7ff | 2014-02-03 19:23:47 +0100 | [diff] [blame] | 41 | unsigned short buf_len:15; |
| 42 | unsigned short reversed:1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 43 | char inline_buf[]; |
| 44 | }; |
David Sterba | ace0105 | 2014-02-05 16:17:34 +0100 | [diff] [blame] | 45 | /* |
| 46 | * Average path length does not exceed 200 bytes, we'll have |
| 47 | * better packing in the slab and higher chance to satisfy |
| 48 | * a allocation later during send. |
| 49 | */ |
| 50 | char pad[256]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 51 | }; |
| 52 | }; |
| 53 | #define FS_PATH_INLINE_SIZE \ |
| 54 | (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf)) |
| 55 | |
| 56 | |
| 57 | /* reused for each extent */ |
| 58 | struct clone_root { |
| 59 | struct btrfs_root *root; |
| 60 | u64 ino; |
| 61 | u64 offset; |
| 62 | |
| 63 | u64 found_refs; |
| 64 | }; |
| 65 | |
| 66 | #define SEND_CTX_MAX_NAME_CACHE_SIZE 128 |
| 67 | #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2) |
| 68 | |
| 69 | struct send_ctx { |
| 70 | struct file *send_filp; |
| 71 | loff_t send_off; |
| 72 | char *send_buf; |
| 73 | u32 send_size; |
| 74 | u32 send_max_size; |
| 75 | u64 total_send_size; |
| 76 | u64 cmd_send_size[BTRFS_SEND_C_MAX + 1]; |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 77 | u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 78 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 79 | struct btrfs_root *send_root; |
| 80 | struct btrfs_root *parent_root; |
| 81 | struct clone_root *clone_roots; |
| 82 | int clone_roots_cnt; |
| 83 | |
| 84 | /* current state of the compare_tree call */ |
| 85 | struct btrfs_path *left_path; |
| 86 | struct btrfs_path *right_path; |
| 87 | struct btrfs_key *cmp_key; |
| 88 | |
| 89 | /* |
| 90 | * infos of the currently processed inode. In case of deleted inodes, |
| 91 | * these are the values from the deleted inode. |
| 92 | */ |
| 93 | u64 cur_ino; |
| 94 | u64 cur_inode_gen; |
| 95 | int cur_inode_new; |
| 96 | int cur_inode_new_gen; |
| 97 | int cur_inode_deleted; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 98 | u64 cur_inode_size; |
| 99 | u64 cur_inode_mode; |
Liu Bo | 644d194 | 2014-02-27 17:29:01 +0800 | [diff] [blame] | 100 | u64 cur_inode_rdev; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 101 | u64 cur_inode_last_extent; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 102 | u64 cur_inode_next_write_offset; |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 103 | bool ignore_cur_inode; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 104 | |
| 105 | u64 send_progress; |
| 106 | |
| 107 | struct list_head new_refs; |
| 108 | struct list_head deleted_refs; |
| 109 | |
| 110 | struct radix_tree_root name_cache; |
| 111 | struct list_head name_cache_list; |
| 112 | int name_cache_size; |
| 113 | |
Liu Bo | 2131bcd | 2014-03-05 10:07:35 +0800 | [diff] [blame] | 114 | struct file_ra_state ra; |
| 115 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 116 | char *read_buf; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * We process inodes by their increasing order, so if before an |
| 120 | * incremental send we reverse the parent/child relationship of |
| 121 | * directories such that a directory with a lower inode number was |
| 122 | * the parent of a directory with a higher inode number, and the one |
| 123 | * becoming the new parent got renamed too, we can't rename/move the |
| 124 | * directory with lower inode number when we finish processing it - we |
| 125 | * must process the directory with higher inode number first, then |
| 126 | * rename/move it and then rename/move the directory with lower inode |
| 127 | * number. Example follows. |
| 128 | * |
| 129 | * Tree state when the first send was performed: |
| 130 | * |
| 131 | * . |
| 132 | * |-- a (ino 257) |
| 133 | * |-- b (ino 258) |
| 134 | * | |
| 135 | * | |
| 136 | * |-- c (ino 259) |
| 137 | * | |-- d (ino 260) |
| 138 | * | |
| 139 | * |-- c2 (ino 261) |
| 140 | * |
| 141 | * Tree state when the second (incremental) send is performed: |
| 142 | * |
| 143 | * . |
| 144 | * |-- a (ino 257) |
| 145 | * |-- b (ino 258) |
| 146 | * |-- c2 (ino 261) |
| 147 | * |-- d2 (ino 260) |
| 148 | * |-- cc (ino 259) |
| 149 | * |
| 150 | * The sequence of steps that lead to the second state was: |
| 151 | * |
| 152 | * mv /a/b/c/d /a/b/c2/d2 |
| 153 | * mv /a/b/c /a/b/c2/d2/cc |
| 154 | * |
| 155 | * "c" has lower inode number, but we can't move it (2nd mv operation) |
| 156 | * before we move "d", which has higher inode number. |
| 157 | * |
| 158 | * So we just memorize which move/rename operations must be performed |
| 159 | * later when their respective parent is processed and moved/renamed. |
| 160 | */ |
| 161 | |
| 162 | /* Indexed by parent directory inode number. */ |
| 163 | struct rb_root pending_dir_moves; |
| 164 | |
| 165 | /* |
| 166 | * Reverse index, indexed by the inode number of a directory that |
| 167 | * is waiting for the move/rename of its immediate parent before its |
| 168 | * own move/rename can be performed. |
| 169 | */ |
| 170 | struct rb_root waiting_dir_moves; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * A directory that is going to be rm'ed might have a child directory |
| 174 | * which is in the pending directory moves index above. In this case, |
| 175 | * the directory can only be removed after the move/rename of its child |
| 176 | * is performed. Example: |
| 177 | * |
| 178 | * Parent snapshot: |
| 179 | * |
| 180 | * . (ino 256) |
| 181 | * |-- a/ (ino 257) |
| 182 | * |-- b/ (ino 258) |
| 183 | * |-- c/ (ino 259) |
| 184 | * | |-- x/ (ino 260) |
| 185 | * | |
| 186 | * |-- y/ (ino 261) |
| 187 | * |
| 188 | * Send snapshot: |
| 189 | * |
| 190 | * . (ino 256) |
| 191 | * |-- a/ (ino 257) |
| 192 | * |-- b/ (ino 258) |
| 193 | * |-- YY/ (ino 261) |
| 194 | * |-- x/ (ino 260) |
| 195 | * |
| 196 | * Sequence of steps that lead to the send snapshot: |
| 197 | * rm -f /a/b/c/foo.txt |
| 198 | * mv /a/b/y /a/b/YY |
| 199 | * mv /a/b/c/x /a/b/YY |
| 200 | * rmdir /a/b/c |
| 201 | * |
| 202 | * When the child is processed, its move/rename is delayed until its |
| 203 | * parent is processed (as explained above), but all other operations |
| 204 | * like update utimes, chown, chgrp, etc, are performed and the paths |
| 205 | * that it uses for those operations must use the orphanized name of |
| 206 | * its parent (the directory we're going to rm later), so we need to |
| 207 | * memorize that name. |
| 208 | * |
| 209 | * Indexed by the inode number of the directory to be deleted. |
| 210 | */ |
| 211 | struct rb_root orphan_dirs; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | struct pending_dir_move { |
| 215 | struct rb_node node; |
| 216 | struct list_head list; |
| 217 | u64 parent_ino; |
| 218 | u64 ino; |
| 219 | u64 gen; |
| 220 | struct list_head update_refs; |
| 221 | }; |
| 222 | |
| 223 | struct waiting_dir_move { |
| 224 | struct rb_node node; |
| 225 | u64 ino; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 226 | /* |
| 227 | * There might be some directory that could not be removed because it |
| 228 | * was waiting for this directory inode to be moved first. Therefore |
| 229 | * after this directory is moved, we can try to rmdir the ino rmdir_ino. |
| 230 | */ |
| 231 | u64 rmdir_ino; |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 232 | bool orphanized; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | struct orphan_dir_info { |
| 236 | struct rb_node node; |
| 237 | u64 ino; |
| 238 | u64 gen; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 239 | u64 last_dir_index_offset; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | struct name_cache_entry { |
| 243 | struct list_head list; |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 244 | /* |
| 245 | * radix_tree has only 32bit entries but we need to handle 64bit inums. |
| 246 | * We use the lower 32bit of the 64bit inum to store it in the tree. If |
| 247 | * more then one inum would fall into the same entry, we use radix_list |
| 248 | * to store the additional entries. radix_list is also used to store |
| 249 | * entries where two entries have the same inum but different |
| 250 | * generations. |
| 251 | */ |
| 252 | struct list_head radix_list; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 253 | u64 ino; |
| 254 | u64 gen; |
| 255 | u64 parent_ino; |
| 256 | u64 parent_gen; |
| 257 | int ret; |
| 258 | int need_later_update; |
| 259 | int name_len; |
| 260 | char name[]; |
| 261 | }; |
| 262 | |
David Sterba | 18d0f5c | 2019-08-21 19:12:59 +0200 | [diff] [blame] | 263 | #define ADVANCE 1 |
| 264 | #define ADVANCE_ONLY_NEXT -1 |
| 265 | |
| 266 | enum btrfs_compare_tree_result { |
| 267 | BTRFS_COMPARE_TREE_NEW, |
| 268 | BTRFS_COMPARE_TREE_DELETED, |
| 269 | BTRFS_COMPARE_TREE_CHANGED, |
| 270 | BTRFS_COMPARE_TREE_SAME, |
| 271 | }; |
| 272 | typedef int (*btrfs_changed_cb_t)(struct btrfs_path *left_path, |
| 273 | struct btrfs_path *right_path, |
| 274 | struct btrfs_key *key, |
| 275 | enum btrfs_compare_tree_result result, |
| 276 | void *ctx); |
| 277 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 278 | __cold |
Filipe Manana | 9515558 | 2016-08-01 01:50:37 +0100 | [diff] [blame] | 279 | static void inconsistent_snapshot_error(struct send_ctx *sctx, |
| 280 | enum btrfs_compare_tree_result result, |
| 281 | const char *what) |
| 282 | { |
| 283 | const char *result_string; |
| 284 | |
| 285 | switch (result) { |
| 286 | case BTRFS_COMPARE_TREE_NEW: |
| 287 | result_string = "new"; |
| 288 | break; |
| 289 | case BTRFS_COMPARE_TREE_DELETED: |
| 290 | result_string = "deleted"; |
| 291 | break; |
| 292 | case BTRFS_COMPARE_TREE_CHANGED: |
| 293 | result_string = "updated"; |
| 294 | break; |
| 295 | case BTRFS_COMPARE_TREE_SAME: |
| 296 | ASSERT(0); |
| 297 | result_string = "unchanged"; |
| 298 | break; |
| 299 | default: |
| 300 | ASSERT(0); |
| 301 | result_string = "unexpected"; |
| 302 | } |
| 303 | |
| 304 | btrfs_err(sctx->send_root->fs_info, |
| 305 | "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu", |
| 306 | result_string, what, sctx->cmp_key->objectid, |
| 307 | sctx->send_root->root_key.objectid, |
| 308 | (sctx->parent_root ? |
| 309 | sctx->parent_root->root_key.objectid : 0)); |
| 310 | } |
| 311 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 312 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino); |
| 313 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 314 | static struct waiting_dir_move * |
| 315 | get_waiting_dir_move(struct send_ctx *sctx, u64 ino); |
| 316 | |
| 317 | static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino); |
| 318 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 319 | static int need_send_hole(struct send_ctx *sctx) |
| 320 | { |
| 321 | return (sctx->parent_root && !sctx->cur_inode_new && |
| 322 | !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted && |
| 323 | S_ISREG(sctx->cur_inode_mode)); |
| 324 | } |
| 325 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 326 | static void fs_path_reset(struct fs_path *p) |
| 327 | { |
| 328 | if (p->reversed) { |
| 329 | p->start = p->buf + p->buf_len - 1; |
| 330 | p->end = p->start; |
| 331 | *p->start = 0; |
| 332 | } else { |
| 333 | p->start = p->buf; |
| 334 | p->end = p->start; |
| 335 | *p->start = 0; |
| 336 | } |
| 337 | } |
| 338 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 339 | static struct fs_path *fs_path_alloc(void) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 340 | { |
| 341 | struct fs_path *p; |
| 342 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 343 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 344 | if (!p) |
| 345 | return NULL; |
| 346 | p->reversed = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 347 | p->buf = p->inline_buf; |
| 348 | p->buf_len = FS_PATH_INLINE_SIZE; |
| 349 | fs_path_reset(p); |
| 350 | return p; |
| 351 | } |
| 352 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 353 | static struct fs_path *fs_path_alloc_reversed(void) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 354 | { |
| 355 | struct fs_path *p; |
| 356 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 357 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 358 | if (!p) |
| 359 | return NULL; |
| 360 | p->reversed = 1; |
| 361 | fs_path_reset(p); |
| 362 | return p; |
| 363 | } |
| 364 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 365 | static void fs_path_free(struct fs_path *p) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 366 | { |
| 367 | if (!p) |
| 368 | return; |
David Sterba | ace0105 | 2014-02-05 16:17:34 +0100 | [diff] [blame] | 369 | if (p->buf != p->inline_buf) |
| 370 | kfree(p->buf); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 371 | kfree(p); |
| 372 | } |
| 373 | |
| 374 | static int fs_path_len(struct fs_path *p) |
| 375 | { |
| 376 | return p->end - p->start; |
| 377 | } |
| 378 | |
| 379 | static int fs_path_ensure_buf(struct fs_path *p, int len) |
| 380 | { |
| 381 | char *tmp_buf; |
| 382 | int path_len; |
| 383 | int old_buf_len; |
| 384 | |
| 385 | len++; |
| 386 | |
| 387 | if (p->buf_len >= len) |
| 388 | return 0; |
| 389 | |
Chris Mason | cfd4a53 | 2014-04-26 05:02:03 -0700 | [diff] [blame] | 390 | if (len > PATH_MAX) { |
| 391 | WARN_ON(1); |
| 392 | return -ENOMEM; |
| 393 | } |
| 394 | |
David Sterba | 1b2782c | 2014-02-25 19:32:59 +0100 | [diff] [blame] | 395 | path_len = p->end - p->start; |
| 396 | old_buf_len = p->buf_len; |
| 397 | |
David Sterba | ace0105 | 2014-02-05 16:17:34 +0100 | [diff] [blame] | 398 | /* |
| 399 | * First time the inline_buf does not suffice |
| 400 | */ |
Filipe Manana | 01a9a8a | 2014-05-21 17:38:13 +0100 | [diff] [blame] | 401 | if (p->buf == p->inline_buf) { |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 402 | tmp_buf = kmalloc(len, GFP_KERNEL); |
Filipe Manana | 01a9a8a | 2014-05-21 17:38:13 +0100 | [diff] [blame] | 403 | if (tmp_buf) |
| 404 | memcpy(tmp_buf, p->buf, old_buf_len); |
| 405 | } else { |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 406 | tmp_buf = krealloc(p->buf, len, GFP_KERNEL); |
Filipe Manana | 01a9a8a | 2014-05-21 17:38:13 +0100 | [diff] [blame] | 407 | } |
David Sterba | 9c9ca00 | 2014-02-25 19:33:08 +0100 | [diff] [blame] | 408 | if (!tmp_buf) |
| 409 | return -ENOMEM; |
| 410 | p->buf = tmp_buf; |
| 411 | /* |
| 412 | * The real size of the buffer is bigger, this will let the fast path |
| 413 | * happen most of the time |
| 414 | */ |
| 415 | p->buf_len = ksize(p->buf); |
David Sterba | ace0105 | 2014-02-05 16:17:34 +0100 | [diff] [blame] | 416 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 417 | if (p->reversed) { |
| 418 | tmp_buf = p->buf + old_buf_len - path_len - 1; |
| 419 | p->end = p->buf + p->buf_len - 1; |
| 420 | p->start = p->end - path_len; |
| 421 | memmove(p->start, tmp_buf, path_len + 1); |
| 422 | } else { |
| 423 | p->start = p->buf; |
| 424 | p->end = p->start + path_len; |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 429 | static int fs_path_prepare_for_add(struct fs_path *p, int name_len, |
| 430 | char **prepared) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 431 | { |
| 432 | int ret; |
| 433 | int new_len; |
| 434 | |
| 435 | new_len = p->end - p->start + name_len; |
| 436 | if (p->start != p->end) |
| 437 | new_len++; |
| 438 | ret = fs_path_ensure_buf(p, new_len); |
| 439 | if (ret < 0) |
| 440 | goto out; |
| 441 | |
| 442 | if (p->reversed) { |
| 443 | if (p->start != p->end) |
| 444 | *--p->start = '/'; |
| 445 | p->start -= name_len; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 446 | *prepared = p->start; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 447 | } else { |
| 448 | if (p->start != p->end) |
| 449 | *p->end++ = '/'; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 450 | *prepared = p->end; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 451 | p->end += name_len; |
| 452 | *p->end = 0; |
| 453 | } |
| 454 | |
| 455 | out: |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | static int fs_path_add(struct fs_path *p, const char *name, int name_len) |
| 460 | { |
| 461 | int ret; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 462 | char *prepared; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 463 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 464 | ret = fs_path_prepare_for_add(p, name_len, &prepared); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 465 | if (ret < 0) |
| 466 | goto out; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 467 | memcpy(prepared, name, name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 468 | |
| 469 | out: |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | static int fs_path_add_path(struct fs_path *p, struct fs_path *p2) |
| 474 | { |
| 475 | int ret; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 476 | char *prepared; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 477 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 478 | ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 479 | if (ret < 0) |
| 480 | goto out; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 481 | memcpy(prepared, p2->start, p2->end - p2->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 482 | |
| 483 | out: |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | static int fs_path_add_from_extent_buffer(struct fs_path *p, |
| 488 | struct extent_buffer *eb, |
| 489 | unsigned long off, int len) |
| 490 | { |
| 491 | int ret; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 492 | char *prepared; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 493 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 494 | ret = fs_path_prepare_for_add(p, len, &prepared); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 495 | if (ret < 0) |
| 496 | goto out; |
| 497 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 498 | read_extent_buffer(eb, prepared, off, len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 499 | |
| 500 | out: |
| 501 | return ret; |
| 502 | } |
| 503 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 504 | static int fs_path_copy(struct fs_path *p, struct fs_path *from) |
| 505 | { |
| 506 | int ret; |
| 507 | |
| 508 | p->reversed = from->reversed; |
| 509 | fs_path_reset(p); |
| 510 | |
| 511 | ret = fs_path_add_path(p, from); |
| 512 | |
| 513 | return ret; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | static void fs_path_unreverse(struct fs_path *p) |
| 518 | { |
| 519 | char *tmp; |
| 520 | int len; |
| 521 | |
| 522 | if (!p->reversed) |
| 523 | return; |
| 524 | |
| 525 | tmp = p->start; |
| 526 | len = p->end - p->start; |
| 527 | p->start = p->buf; |
| 528 | p->end = p->start + len; |
| 529 | memmove(p->start, tmp, len + 1); |
| 530 | p->reversed = 0; |
| 531 | } |
| 532 | |
| 533 | static struct btrfs_path *alloc_path_for_send(void) |
| 534 | { |
| 535 | struct btrfs_path *path; |
| 536 | |
| 537 | path = btrfs_alloc_path(); |
| 538 | if (!path) |
| 539 | return NULL; |
| 540 | path->search_commit_root = 1; |
| 541 | path->skip_locking = 1; |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 542 | path->need_commit_sem = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 543 | return path; |
| 544 | } |
| 545 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 546 | 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] | 547 | { |
| 548 | int ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 549 | u32 pos = 0; |
| 550 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 551 | while (pos < len) { |
Christoph Hellwig | 8e93157 | 2017-09-01 17:39:19 +0200 | [diff] [blame] | 552 | ret = kernel_write(filp, buf + pos, len - pos, off); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 553 | /* TODO handle that correctly */ |
| 554 | /*if (ret == -ERESTARTSYS) { |
| 555 | continue; |
| 556 | }*/ |
| 557 | if (ret < 0) |
Christoph Hellwig | 8e93157 | 2017-09-01 17:39:19 +0200 | [diff] [blame] | 558 | return ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 559 | if (ret == 0) { |
Christoph Hellwig | 8e93157 | 2017-09-01 17:39:19 +0200 | [diff] [blame] | 560 | return -EIO; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 561 | } |
| 562 | pos += ret; |
| 563 | } |
| 564 | |
Christoph Hellwig | 8e93157 | 2017-09-01 17:39:19 +0200 | [diff] [blame] | 565 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) |
| 569 | { |
| 570 | struct btrfs_tlv_header *hdr; |
| 571 | int total_len = sizeof(*hdr) + len; |
| 572 | int left = sctx->send_max_size - sctx->send_size; |
| 573 | |
| 574 | if (unlikely(left < total_len)) |
| 575 | return -EOVERFLOW; |
| 576 | |
| 577 | hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); |
| 578 | hdr->tlv_type = cpu_to_le16(attr); |
| 579 | hdr->tlv_len = cpu_to_le16(len); |
| 580 | memcpy(hdr + 1, data, len); |
| 581 | sctx->send_size += total_len; |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
David Sterba | 95bc79d | 2013-12-16 17:34:10 +0100 | [diff] [blame] | 586 | #define TLV_PUT_DEFINE_INT(bits) \ |
| 587 | static int tlv_put_u##bits(struct send_ctx *sctx, \ |
| 588 | u##bits attr, u##bits value) \ |
| 589 | { \ |
| 590 | __le##bits __tmp = cpu_to_le##bits(value); \ |
| 591 | return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \ |
| 592 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 593 | |
David Sterba | 95bc79d | 2013-12-16 17:34:10 +0100 | [diff] [blame] | 594 | TLV_PUT_DEFINE_INT(64) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 595 | |
| 596 | static int tlv_put_string(struct send_ctx *sctx, u16 attr, |
| 597 | const char *str, int len) |
| 598 | { |
| 599 | if (len == -1) |
| 600 | len = strlen(str); |
| 601 | return tlv_put(sctx, attr, str, len); |
| 602 | } |
| 603 | |
| 604 | static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, |
| 605 | const u8 *uuid) |
| 606 | { |
| 607 | return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); |
| 608 | } |
| 609 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 610 | static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, |
| 611 | struct extent_buffer *eb, |
| 612 | struct btrfs_timespec *ts) |
| 613 | { |
| 614 | struct btrfs_timespec bts; |
| 615 | read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts)); |
| 616 | return tlv_put(sctx, attr, &bts, sizeof(bts)); |
| 617 | } |
| 618 | |
| 619 | |
Liu Bo | 895a72b | 2018-03-02 18:05:49 -0700 | [diff] [blame] | 620 | #define TLV_PUT(sctx, attrtype, data, attrlen) \ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 621 | do { \ |
Liu Bo | 895a72b | 2018-03-02 18:05:49 -0700 | [diff] [blame] | 622 | ret = tlv_put(sctx, attrtype, data, attrlen); \ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 623 | if (ret < 0) \ |
| 624 | goto tlv_put_failure; \ |
| 625 | } while (0) |
| 626 | |
| 627 | #define TLV_PUT_INT(sctx, attrtype, bits, value) \ |
| 628 | do { \ |
| 629 | ret = tlv_put_u##bits(sctx, attrtype, value); \ |
| 630 | if (ret < 0) \ |
| 631 | goto tlv_put_failure; \ |
| 632 | } while (0) |
| 633 | |
| 634 | #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) |
| 635 | #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) |
| 636 | #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) |
| 637 | #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) |
| 638 | #define TLV_PUT_STRING(sctx, attrtype, str, len) \ |
| 639 | do { \ |
| 640 | ret = tlv_put_string(sctx, attrtype, str, len); \ |
| 641 | if (ret < 0) \ |
| 642 | goto tlv_put_failure; \ |
| 643 | } while (0) |
| 644 | #define TLV_PUT_PATH(sctx, attrtype, p) \ |
| 645 | do { \ |
| 646 | ret = tlv_put_string(sctx, attrtype, p->start, \ |
| 647 | p->end - p->start); \ |
| 648 | if (ret < 0) \ |
| 649 | goto tlv_put_failure; \ |
| 650 | } while(0) |
| 651 | #define TLV_PUT_UUID(sctx, attrtype, uuid) \ |
| 652 | do { \ |
| 653 | ret = tlv_put_uuid(sctx, attrtype, uuid); \ |
| 654 | if (ret < 0) \ |
| 655 | goto tlv_put_failure; \ |
| 656 | } while (0) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 657 | #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ |
| 658 | do { \ |
| 659 | ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \ |
| 660 | if (ret < 0) \ |
| 661 | goto tlv_put_failure; \ |
| 662 | } while (0) |
| 663 | |
| 664 | static int send_header(struct send_ctx *sctx) |
| 665 | { |
| 666 | struct btrfs_stream_header hdr; |
| 667 | |
| 668 | strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC); |
| 669 | hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION); |
| 670 | |
Anand Jain | 1bcea35 | 2012-09-14 00:04:21 -0600 | [diff] [blame] | 671 | return write_buf(sctx->send_filp, &hdr, sizeof(hdr), |
| 672 | &sctx->send_off); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* |
| 676 | * For each command/item we want to send to userspace, we call this function. |
| 677 | */ |
| 678 | static int begin_cmd(struct send_ctx *sctx, int cmd) |
| 679 | { |
| 680 | struct btrfs_cmd_header *hdr; |
| 681 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 682 | if (WARN_ON(!sctx->send_buf)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 683 | return -EINVAL; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 684 | |
| 685 | BUG_ON(sctx->send_size); |
| 686 | |
| 687 | sctx->send_size += sizeof(*hdr); |
| 688 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| 689 | hdr->cmd = cpu_to_le16(cmd); |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | static int send_cmd(struct send_ctx *sctx) |
| 695 | { |
| 696 | int ret; |
| 697 | struct btrfs_cmd_header *hdr; |
| 698 | u32 crc; |
| 699 | |
| 700 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| 701 | hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr)); |
| 702 | hdr->crc = 0; |
| 703 | |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 704 | crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 705 | hdr->crc = cpu_to_le32(crc); |
| 706 | |
Anand Jain | 1bcea35 | 2012-09-14 00:04:21 -0600 | [diff] [blame] | 707 | ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, |
| 708 | &sctx->send_off); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 709 | |
| 710 | sctx->total_send_size += sctx->send_size; |
| 711 | sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size; |
| 712 | sctx->send_size = 0; |
| 713 | |
| 714 | return ret; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Sends a move instruction to user space |
| 719 | */ |
| 720 | static int send_rename(struct send_ctx *sctx, |
| 721 | struct fs_path *from, struct fs_path *to) |
| 722 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 723 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 724 | int ret; |
| 725 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 726 | btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 727 | |
| 728 | ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); |
| 729 | if (ret < 0) |
| 730 | goto out; |
| 731 | |
| 732 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); |
| 733 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); |
| 734 | |
| 735 | ret = send_cmd(sctx); |
| 736 | |
| 737 | tlv_put_failure: |
| 738 | out: |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Sends a link instruction to user space |
| 744 | */ |
| 745 | static int send_link(struct send_ctx *sctx, |
| 746 | struct fs_path *path, struct fs_path *lnk) |
| 747 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 748 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 749 | int ret; |
| 750 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 751 | btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 752 | |
| 753 | ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); |
| 754 | if (ret < 0) |
| 755 | goto out; |
| 756 | |
| 757 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 758 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); |
| 759 | |
| 760 | ret = send_cmd(sctx); |
| 761 | |
| 762 | tlv_put_failure: |
| 763 | out: |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Sends an unlink instruction to user space |
| 769 | */ |
| 770 | static int send_unlink(struct send_ctx *sctx, struct fs_path *path) |
| 771 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 772 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 773 | int ret; |
| 774 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 775 | btrfs_debug(fs_info, "send_unlink %s", path->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 776 | |
| 777 | ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); |
| 778 | if (ret < 0) |
| 779 | goto out; |
| 780 | |
| 781 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 782 | |
| 783 | ret = send_cmd(sctx); |
| 784 | |
| 785 | tlv_put_failure: |
| 786 | out: |
| 787 | return ret; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Sends a rmdir instruction to user space |
| 792 | */ |
| 793 | static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) |
| 794 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 795 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 796 | int ret; |
| 797 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 798 | btrfs_debug(fs_info, "send_rmdir %s", path->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 799 | |
| 800 | ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); |
| 801 | if (ret < 0) |
| 802 | goto out; |
| 803 | |
| 804 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 805 | |
| 806 | ret = send_cmd(sctx); |
| 807 | |
| 808 | tlv_put_failure: |
| 809 | out: |
| 810 | return ret; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Helper function to retrieve some fields from an inode item. |
| 815 | */ |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 816 | static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path, |
| 817 | u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid, |
| 818 | u64 *gid, u64 *rdev) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 819 | { |
| 820 | int ret; |
| 821 | struct btrfs_inode_item *ii; |
| 822 | struct btrfs_key key; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 823 | |
| 824 | key.objectid = ino; |
| 825 | key.type = BTRFS_INODE_ITEM_KEY; |
| 826 | key.offset = 0; |
| 827 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 828 | if (ret) { |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 829 | if (ret > 0) |
| 830 | ret = -ENOENT; |
| 831 | return ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | ii = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 835 | struct btrfs_inode_item); |
| 836 | if (size) |
| 837 | *size = btrfs_inode_size(path->nodes[0], ii); |
| 838 | if (gen) |
| 839 | *gen = btrfs_inode_generation(path->nodes[0], ii); |
| 840 | if (mode) |
| 841 | *mode = btrfs_inode_mode(path->nodes[0], ii); |
| 842 | if (uid) |
| 843 | *uid = btrfs_inode_uid(path->nodes[0], ii); |
| 844 | if (gid) |
| 845 | *gid = btrfs_inode_gid(path->nodes[0], ii); |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 846 | if (rdev) |
| 847 | *rdev = btrfs_inode_rdev(path->nodes[0], ii); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 848 | |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | static int get_inode_info(struct btrfs_root *root, |
| 853 | u64 ino, u64 *size, u64 *gen, |
| 854 | u64 *mode, u64 *uid, u64 *gid, |
| 855 | u64 *rdev) |
| 856 | { |
| 857 | struct btrfs_path *path; |
| 858 | int ret; |
| 859 | |
| 860 | path = alloc_path_for_send(); |
| 861 | if (!path) |
| 862 | return -ENOMEM; |
| 863 | ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid, |
| 864 | rdev); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 865 | btrfs_free_path(path); |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index, |
| 870 | struct fs_path *p, |
| 871 | void *ctx); |
| 872 | |
| 873 | /* |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 874 | * Helper function to iterate the entries in ONE btrfs_inode_ref or |
| 875 | * btrfs_inode_extref. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 876 | * The iterate callback may return a non zero value to stop iteration. This can |
| 877 | * be a negative value for error codes or 1 to simply stop it. |
| 878 | * |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 879 | * path must point to the INODE_REF or INODE_EXTREF when called. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 880 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 881 | 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] | 882 | struct btrfs_key *found_key, int resolve, |
| 883 | iterate_inode_ref_t iterate, void *ctx) |
| 884 | { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 885 | struct extent_buffer *eb = path->nodes[0]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 886 | struct btrfs_item *item; |
| 887 | struct btrfs_inode_ref *iref; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 888 | struct btrfs_inode_extref *extref; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 889 | struct btrfs_path *tmp_path; |
| 890 | struct fs_path *p; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 891 | u32 cur = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 892 | u32 total; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 893 | int slot = path->slots[0]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 894 | u32 name_len; |
| 895 | char *start; |
| 896 | int ret = 0; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 897 | int num = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 898 | int index; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 899 | u64 dir; |
| 900 | unsigned long name_off; |
| 901 | unsigned long elem_size; |
| 902 | unsigned long ptr; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 903 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 904 | p = fs_path_alloc_reversed(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 905 | if (!p) |
| 906 | return -ENOMEM; |
| 907 | |
| 908 | tmp_path = alloc_path_for_send(); |
| 909 | if (!tmp_path) { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 910 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 911 | return -ENOMEM; |
| 912 | } |
| 913 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 914 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 915 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
| 916 | ptr = (unsigned long)btrfs_item_ptr(eb, slot, |
| 917 | struct btrfs_inode_ref); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 918 | item = btrfs_item_nr(slot); |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 919 | total = btrfs_item_size(eb, item); |
| 920 | elem_size = sizeof(*iref); |
| 921 | } else { |
| 922 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 923 | total = btrfs_item_size_nr(eb, slot); |
| 924 | elem_size = sizeof(*extref); |
| 925 | } |
| 926 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 927 | while (cur < total) { |
| 928 | fs_path_reset(p); |
| 929 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 930 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
| 931 | iref = (struct btrfs_inode_ref *)(ptr + cur); |
| 932 | name_len = btrfs_inode_ref_name_len(eb, iref); |
| 933 | name_off = (unsigned long)(iref + 1); |
| 934 | index = btrfs_inode_ref_index(eb, iref); |
| 935 | dir = found_key->offset; |
| 936 | } else { |
| 937 | extref = (struct btrfs_inode_extref *)(ptr + cur); |
| 938 | name_len = btrfs_inode_extref_name_len(eb, extref); |
| 939 | name_off = (unsigned long)&extref->name; |
| 940 | index = btrfs_inode_extref_index(eb, extref); |
| 941 | dir = btrfs_inode_extref_parent(eb, extref); |
| 942 | } |
| 943 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 944 | if (resolve) { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 945 | start = btrfs_ref_to_path(root, tmp_path, name_len, |
| 946 | name_off, eb, dir, |
| 947 | p->buf, p->buf_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 948 | if (IS_ERR(start)) { |
| 949 | ret = PTR_ERR(start); |
| 950 | goto out; |
| 951 | } |
| 952 | if (start < p->buf) { |
| 953 | /* overflow , try again with larger buffer */ |
| 954 | ret = fs_path_ensure_buf(p, |
| 955 | p->buf_len + p->buf - start); |
| 956 | if (ret < 0) |
| 957 | goto out; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 958 | start = btrfs_ref_to_path(root, tmp_path, |
| 959 | name_len, name_off, |
| 960 | eb, dir, |
| 961 | p->buf, p->buf_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 962 | if (IS_ERR(start)) { |
| 963 | ret = PTR_ERR(start); |
| 964 | goto out; |
| 965 | } |
| 966 | BUG_ON(start < p->buf); |
| 967 | } |
| 968 | p->start = start; |
| 969 | } else { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 970 | ret = fs_path_add_from_extent_buffer(p, eb, name_off, |
| 971 | name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 972 | if (ret < 0) |
| 973 | goto out; |
| 974 | } |
| 975 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 976 | cur += elem_size + name_len; |
| 977 | ret = iterate(num, dir, index, p, ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 978 | if (ret) |
| 979 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 980 | num++; |
| 981 | } |
| 982 | |
| 983 | out: |
| 984 | btrfs_free_path(tmp_path); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 985 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 986 | return ret; |
| 987 | } |
| 988 | |
| 989 | typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key, |
| 990 | const char *name, int name_len, |
| 991 | const char *data, int data_len, |
| 992 | u8 type, void *ctx); |
| 993 | |
| 994 | /* |
| 995 | * Helper function to iterate the entries in ONE btrfs_dir_item. |
| 996 | * The iterate callback may return a non zero value to stop iteration. This can |
| 997 | * be a negative value for error codes or 1 to simply stop it. |
| 998 | * |
| 999 | * path must point to the dir item when called. |
| 1000 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1001 | 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] | 1002 | iterate_dir_item_t iterate, void *ctx) |
| 1003 | { |
| 1004 | int ret = 0; |
| 1005 | struct extent_buffer *eb; |
| 1006 | struct btrfs_item *item; |
| 1007 | struct btrfs_dir_item *di; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1008 | struct btrfs_key di_key; |
| 1009 | char *buf = NULL; |
Filipe Manana | 7e3ae33 | 2014-05-23 20:15:16 +0100 | [diff] [blame] | 1010 | int buf_len; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1011 | u32 name_len; |
| 1012 | u32 data_len; |
| 1013 | u32 cur; |
| 1014 | u32 len; |
| 1015 | u32 total; |
| 1016 | int slot; |
| 1017 | int num; |
| 1018 | u8 type; |
| 1019 | |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1020 | /* |
| 1021 | * Start with a small buffer (1 page). If later we end up needing more |
| 1022 | * space, which can happen for xattrs on a fs with a leaf size greater |
| 1023 | * then the page size, attempt to increase the buffer. Typically xattr |
| 1024 | * values are small. |
| 1025 | */ |
| 1026 | buf_len = PATH_MAX; |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 1027 | buf = kmalloc(buf_len, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1028 | if (!buf) { |
| 1029 | ret = -ENOMEM; |
| 1030 | goto out; |
| 1031 | } |
| 1032 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1033 | eb = path->nodes[0]; |
| 1034 | slot = path->slots[0]; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 1035 | item = btrfs_item_nr(slot); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1036 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| 1037 | cur = 0; |
| 1038 | len = 0; |
| 1039 | total = btrfs_item_size(eb, item); |
| 1040 | |
| 1041 | num = 0; |
| 1042 | while (cur < total) { |
| 1043 | name_len = btrfs_dir_name_len(eb, di); |
| 1044 | data_len = btrfs_dir_data_len(eb, di); |
| 1045 | type = btrfs_dir_type(eb, di); |
| 1046 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 1047 | |
Filipe Manana | 7e3ae33 | 2014-05-23 20:15:16 +0100 | [diff] [blame] | 1048 | if (type == BTRFS_FT_XATTR) { |
| 1049 | if (name_len > XATTR_NAME_MAX) { |
| 1050 | ret = -ENAMETOOLONG; |
| 1051 | goto out; |
| 1052 | } |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1053 | if (name_len + data_len > |
| 1054 | BTRFS_MAX_XATTR_SIZE(root->fs_info)) { |
Filipe Manana | 7e3ae33 | 2014-05-23 20:15:16 +0100 | [diff] [blame] | 1055 | ret = -E2BIG; |
| 1056 | goto out; |
| 1057 | } |
| 1058 | } else { |
| 1059 | /* |
| 1060 | * Path too long |
| 1061 | */ |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1062 | if (name_len + data_len > PATH_MAX) { |
Filipe Manana | 7e3ae33 | 2014-05-23 20:15:16 +0100 | [diff] [blame] | 1063 | ret = -ENAMETOOLONG; |
| 1064 | goto out; |
| 1065 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1068 | if (name_len + data_len > buf_len) { |
| 1069 | buf_len = name_len + data_len; |
| 1070 | if (is_vmalloc_addr(buf)) { |
| 1071 | vfree(buf); |
| 1072 | buf = NULL; |
| 1073 | } else { |
| 1074 | char *tmp = krealloc(buf, buf_len, |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 1075 | GFP_KERNEL | __GFP_NOWARN); |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1076 | |
| 1077 | if (!tmp) |
| 1078 | kfree(buf); |
| 1079 | buf = tmp; |
| 1080 | } |
| 1081 | if (!buf) { |
David Sterba | f11f744 | 2017-05-31 18:40:02 +0200 | [diff] [blame] | 1082 | buf = kvmalloc(buf_len, GFP_KERNEL); |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1083 | if (!buf) { |
| 1084 | ret = -ENOMEM; |
| 1085 | goto out; |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1090 | read_extent_buffer(eb, buf, (unsigned long)(di + 1), |
| 1091 | name_len + data_len); |
| 1092 | |
| 1093 | len = sizeof(*di) + name_len + data_len; |
| 1094 | di = (struct btrfs_dir_item *)((char *)di + len); |
| 1095 | cur += len; |
| 1096 | |
| 1097 | ret = iterate(num, &di_key, buf, name_len, buf + name_len, |
| 1098 | data_len, type, ctx); |
| 1099 | if (ret < 0) |
| 1100 | goto out; |
| 1101 | if (ret) { |
| 1102 | ret = 0; |
| 1103 | goto out; |
| 1104 | } |
| 1105 | |
| 1106 | num++; |
| 1107 | } |
| 1108 | |
| 1109 | out: |
Filipe Manana | 4395e0c | 2014-08-20 10:45:45 +0100 | [diff] [blame] | 1110 | kvfree(buf); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1111 | return ret; |
| 1112 | } |
| 1113 | |
| 1114 | static int __copy_first_ref(int num, u64 dir, int index, |
| 1115 | struct fs_path *p, void *ctx) |
| 1116 | { |
| 1117 | int ret; |
| 1118 | struct fs_path *pt = ctx; |
| 1119 | |
| 1120 | ret = fs_path_copy(pt, p); |
| 1121 | if (ret < 0) |
| 1122 | return ret; |
| 1123 | |
| 1124 | /* we want the first only */ |
| 1125 | return 1; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * Retrieve the first path of an inode. If an inode has more then one |
| 1130 | * ref/hardlink, this is ignored. |
| 1131 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1132 | static int get_inode_path(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1133 | u64 ino, struct fs_path *path) |
| 1134 | { |
| 1135 | int ret; |
| 1136 | struct btrfs_key key, found_key; |
| 1137 | struct btrfs_path *p; |
| 1138 | |
| 1139 | p = alloc_path_for_send(); |
| 1140 | if (!p) |
| 1141 | return -ENOMEM; |
| 1142 | |
| 1143 | fs_path_reset(path); |
| 1144 | |
| 1145 | key.objectid = ino; |
| 1146 | key.type = BTRFS_INODE_REF_KEY; |
| 1147 | key.offset = 0; |
| 1148 | |
| 1149 | ret = btrfs_search_slot_for_read(root, &key, p, 1, 0); |
| 1150 | if (ret < 0) |
| 1151 | goto out; |
| 1152 | if (ret) { |
| 1153 | ret = 1; |
| 1154 | goto out; |
| 1155 | } |
| 1156 | btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]); |
| 1157 | if (found_key.objectid != ino || |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1158 | (found_key.type != BTRFS_INODE_REF_KEY && |
| 1159 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1160 | ret = -ENOENT; |
| 1161 | goto out; |
| 1162 | } |
| 1163 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1164 | ret = iterate_inode_ref(root, p, &found_key, 1, |
| 1165 | __copy_first_ref, path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1166 | if (ret < 0) |
| 1167 | goto out; |
| 1168 | ret = 0; |
| 1169 | |
| 1170 | out: |
| 1171 | btrfs_free_path(p); |
| 1172 | return ret; |
| 1173 | } |
| 1174 | |
| 1175 | struct backref_ctx { |
| 1176 | struct send_ctx *sctx; |
| 1177 | |
| 1178 | /* number of total found references */ |
| 1179 | u64 found; |
| 1180 | |
| 1181 | /* |
| 1182 | * used for clones found in send_root. clones found behind cur_objectid |
| 1183 | * and cur_offset are not considered as allowed clones. |
| 1184 | */ |
| 1185 | u64 cur_objectid; |
| 1186 | u64 cur_offset; |
| 1187 | |
| 1188 | /* may be truncated in case it's the last extent in a file */ |
| 1189 | u64 extent_len; |
| 1190 | |
Filipe Manana | 619d8c4 | 2015-05-03 01:56:00 +0100 | [diff] [blame] | 1191 | /* data offset in the file extent item */ |
| 1192 | u64 data_offset; |
| 1193 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1194 | /* Just to check for bugs in backref resolving */ |
Alexander Block | ee849c0 | 2012-07-28 12:42:05 +0200 | [diff] [blame] | 1195 | int found_itself; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1196 | }; |
| 1197 | |
| 1198 | static int __clone_root_cmp_bsearch(const void *key, const void *elt) |
| 1199 | { |
Jan Schmidt | 995e01b | 2012-08-13 02:52:38 -0600 | [diff] [blame] | 1200 | u64 root = (u64)(uintptr_t)key; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1201 | struct clone_root *cr = (struct clone_root *)elt; |
| 1202 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 1203 | if (root < cr->root->root_key.objectid) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1204 | return -1; |
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; |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static int __clone_root_cmp_sort(const void *e1, const void *e2) |
| 1211 | { |
| 1212 | struct clone_root *cr1 = (struct clone_root *)e1; |
| 1213 | struct clone_root *cr2 = (struct clone_root *)e2; |
| 1214 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 1215 | if (cr1->root->root_key.objectid < cr2->root->root_key.objectid) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1216 | return -1; |
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; |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * Called for every backref that is found for the current extent. |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1224 | * Results are collected in sctx->clone_roots->ino/offset/found_refs |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1225 | */ |
| 1226 | static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_) |
| 1227 | { |
| 1228 | struct backref_ctx *bctx = ctx_; |
| 1229 | struct clone_root *found; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1230 | |
| 1231 | /* 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] | 1232 | found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1233 | bctx->sctx->clone_roots_cnt, |
| 1234 | sizeof(struct clone_root), |
| 1235 | __clone_root_cmp_bsearch); |
| 1236 | if (!found) |
| 1237 | return 0; |
| 1238 | |
| 1239 | if (found->root == bctx->sctx->send_root && |
| 1240 | ino == bctx->cur_objectid && |
| 1241 | offset == bctx->cur_offset) { |
Alexander Block | ee849c0 | 2012-07-28 12:42:05 +0200 | [diff] [blame] | 1242 | bctx->found_itself = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1246 | * Make sure we don't consider clones from send_root that are |
| 1247 | * behind the current inode/offset. |
| 1248 | */ |
| 1249 | if (found->root == bctx->sctx->send_root) { |
| 1250 | /* |
Filipe Manana | 11f2069 | 2019-10-30 12:23:11 +0000 | [diff] [blame^] | 1251 | * If the source inode was not yet processed we can't issue a |
| 1252 | * clone operation, as the source extent does not exist yet at |
| 1253 | * the destination of the stream. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1254 | */ |
Filipe Manana | 11f2069 | 2019-10-30 12:23:11 +0000 | [diff] [blame^] | 1255 | if (ino > bctx->cur_objectid) |
| 1256 | return 0; |
| 1257 | /* |
| 1258 | * We clone from the inode currently being sent as long as the |
| 1259 | * source extent is already processed, otherwise we could try |
| 1260 | * to clone from an extent that does not exist yet at the |
| 1261 | * destination of the stream. |
| 1262 | */ |
| 1263 | if (ino == bctx->cur_objectid && |
| 1264 | offset >= bctx->sctx->cur_inode_next_write_offset) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1265 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | bctx->found++; |
| 1269 | found->found_refs++; |
| 1270 | if (ino < found->ino) { |
| 1271 | found->ino = ino; |
| 1272 | found->offset = offset; |
| 1273 | } else if (found->ino == ino) { |
| 1274 | /* |
| 1275 | * same extent found more then once in the same file. |
| 1276 | */ |
| 1277 | if (found->offset > offset + bctx->extent_len) |
| 1278 | found->offset = offset; |
| 1279 | } |
| 1280 | |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1285 | * Given an inode, offset and extent item, it finds a good clone for a clone |
| 1286 | * instruction. Returns -ENOENT when none could be found. The function makes |
| 1287 | * sure that the returned clone is usable at the point where sending is at the |
| 1288 | * moment. This means, that no clones are accepted which lie behind the current |
| 1289 | * inode+offset. |
| 1290 | * |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1291 | * path must point to the extent item when called. |
| 1292 | */ |
| 1293 | static int find_extent_clone(struct send_ctx *sctx, |
| 1294 | struct btrfs_path *path, |
| 1295 | u64 ino, u64 data_offset, |
| 1296 | u64 ino_size, |
| 1297 | struct clone_root **found) |
| 1298 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1299 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1300 | int ret; |
| 1301 | int extent_type; |
| 1302 | u64 logical; |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1303 | u64 disk_byte; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1304 | u64 num_bytes; |
| 1305 | u64 extent_item_pos; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1306 | u64 flags = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1307 | struct btrfs_file_extent_item *fi; |
| 1308 | struct extent_buffer *eb = path->nodes[0]; |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1309 | struct backref_ctx *backref_ctx = NULL; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1310 | struct clone_root *cur_clone_root; |
| 1311 | struct btrfs_key found_key; |
| 1312 | struct btrfs_path *tmp_path; |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1313 | int compressed; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1314 | u32 i; |
| 1315 | |
| 1316 | tmp_path = alloc_path_for_send(); |
| 1317 | if (!tmp_path) |
| 1318 | return -ENOMEM; |
| 1319 | |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 1320 | /* We only use this path under the commit sem */ |
| 1321 | tmp_path->need_commit_sem = 0; |
| 1322 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 1323 | backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL); |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1324 | if (!backref_ctx) { |
| 1325 | ret = -ENOMEM; |
| 1326 | goto out; |
| 1327 | } |
| 1328 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1329 | if (data_offset >= ino_size) { |
| 1330 | /* |
| 1331 | * There may be extents that lie behind the file's size. |
| 1332 | * I at least had this in combination with snapshotting while |
| 1333 | * writing large files. |
| 1334 | */ |
| 1335 | ret = 0; |
| 1336 | goto out; |
| 1337 | } |
| 1338 | |
| 1339 | fi = btrfs_item_ptr(eb, path->slots[0], |
| 1340 | struct btrfs_file_extent_item); |
| 1341 | extent_type = btrfs_file_extent_type(eb, fi); |
| 1342 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1343 | ret = -ENOENT; |
| 1344 | goto out; |
| 1345 | } |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1346 | compressed = btrfs_file_extent_compression(eb, fi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1347 | |
| 1348 | num_bytes = btrfs_file_extent_num_bytes(eb, fi); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1349 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
| 1350 | if (disk_byte == 0) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1351 | ret = -ENOENT; |
| 1352 | goto out; |
| 1353 | } |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1354 | logical = disk_byte + btrfs_file_extent_offset(eb, fi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1355 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1356 | down_read(&fs_info->commit_root_sem); |
| 1357 | ret = extent_from_logical(fs_info, disk_byte, tmp_path, |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1358 | &found_key, &flags); |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1359 | up_read(&fs_info->commit_root_sem); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1360 | btrfs_release_path(tmp_path); |
| 1361 | |
| 1362 | if (ret < 0) |
| 1363 | goto out; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1364 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1365 | ret = -EIO; |
| 1366 | goto out; |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * Setup the clone roots. |
| 1371 | */ |
| 1372 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 1373 | cur_clone_root = sctx->clone_roots + i; |
| 1374 | cur_clone_root->ino = (u64)-1; |
| 1375 | cur_clone_root->offset = 0; |
| 1376 | cur_clone_root->found_refs = 0; |
| 1377 | } |
| 1378 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1379 | backref_ctx->sctx = sctx; |
| 1380 | backref_ctx->found = 0; |
| 1381 | backref_ctx->cur_objectid = ino; |
| 1382 | backref_ctx->cur_offset = data_offset; |
| 1383 | backref_ctx->found_itself = 0; |
| 1384 | backref_ctx->extent_len = num_bytes; |
Filipe Manana | 619d8c4 | 2015-05-03 01:56:00 +0100 | [diff] [blame] | 1385 | /* |
| 1386 | * For non-compressed extents iterate_extent_inodes() gives us extent |
| 1387 | * offsets that already take into account the data offset, but not for |
| 1388 | * compressed extents, since the offset is logical and not relative to |
| 1389 | * the physical extent locations. We must take this into account to |
| 1390 | * avoid sending clone offsets that go beyond the source file's size, |
| 1391 | * which would result in the clone ioctl failing with -EINVAL on the |
| 1392 | * receiving end. |
| 1393 | */ |
| 1394 | if (compressed == BTRFS_COMPRESS_NONE) |
| 1395 | backref_ctx->data_offset = 0; |
| 1396 | else |
| 1397 | backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1398 | |
| 1399 | /* |
| 1400 | * The last extent of a file may be too large due to page alignment. |
| 1401 | * We need to adjust extent_len in this case so that the checks in |
| 1402 | * __iterate_backrefs work. |
| 1403 | */ |
| 1404 | if (data_offset + num_bytes >= ino_size) |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1405 | backref_ctx->extent_len = ino_size - data_offset; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1406 | |
| 1407 | /* |
| 1408 | * Now collect all backrefs. |
| 1409 | */ |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1410 | if (compressed == BTRFS_COMPRESS_NONE) |
| 1411 | extent_item_pos = logical - found_key.objectid; |
| 1412 | else |
| 1413 | extent_item_pos = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1414 | ret = iterate_extent_inodes(fs_info, found_key.objectid, |
| 1415 | extent_item_pos, 1, __iterate_backrefs, |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 1416 | backref_ctx, false); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1417 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1418 | if (ret < 0) |
| 1419 | goto out; |
| 1420 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1421 | if (!backref_ctx->found_itself) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1422 | /* found a bug in backref code? */ |
| 1423 | ret = -EIO; |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1424 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 1425 | "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] | 1426 | ino, data_offset, disk_byte, found_key.objectid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1427 | goto out; |
| 1428 | } |
| 1429 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1430 | btrfs_debug(fs_info, |
| 1431 | "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu", |
| 1432 | data_offset, ino, num_bytes, logical); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1433 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1434 | if (!backref_ctx->found) |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 1435 | btrfs_debug(fs_info, "no clones found"); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1436 | |
| 1437 | cur_clone_root = NULL; |
| 1438 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 1439 | if (sctx->clone_roots[i].found_refs) { |
| 1440 | if (!cur_clone_root) |
| 1441 | cur_clone_root = sctx->clone_roots + i; |
| 1442 | else if (sctx->clone_roots[i].root == sctx->send_root) |
| 1443 | /* prefer clones from send_root over others */ |
| 1444 | cur_clone_root = sctx->clone_roots + i; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | } |
| 1448 | |
| 1449 | if (cur_clone_root) { |
| 1450 | *found = cur_clone_root; |
| 1451 | ret = 0; |
| 1452 | } else { |
| 1453 | ret = -ENOENT; |
| 1454 | } |
| 1455 | |
| 1456 | out: |
| 1457 | btrfs_free_path(tmp_path); |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1458 | kfree(backref_ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1459 | return ret; |
| 1460 | } |
| 1461 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1462 | static int read_symlink(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1463 | u64 ino, |
| 1464 | struct fs_path *dest) |
| 1465 | { |
| 1466 | int ret; |
| 1467 | struct btrfs_path *path; |
| 1468 | struct btrfs_key key; |
| 1469 | struct btrfs_file_extent_item *ei; |
| 1470 | u8 type; |
| 1471 | u8 compression; |
| 1472 | unsigned long off; |
| 1473 | int len; |
| 1474 | |
| 1475 | path = alloc_path_for_send(); |
| 1476 | if (!path) |
| 1477 | return -ENOMEM; |
| 1478 | |
| 1479 | key.objectid = ino; |
| 1480 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1481 | key.offset = 0; |
| 1482 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1483 | if (ret < 0) |
| 1484 | goto out; |
Filipe Manana | a879719 | 2015-12-31 18:07:59 +0000 | [diff] [blame] | 1485 | if (ret) { |
| 1486 | /* |
| 1487 | * An empty symlink inode. Can happen in rare error paths when |
| 1488 | * creating a symlink (transaction committed before the inode |
| 1489 | * eviction handler removed the symlink inode items and a crash |
| 1490 | * happened in between or the subvol was snapshoted in between). |
| 1491 | * Print an informative message to dmesg/syslog so that the user |
| 1492 | * can delete the symlink. |
| 1493 | */ |
| 1494 | btrfs_err(root->fs_info, |
| 1495 | "Found empty symlink inode %llu at root %llu", |
| 1496 | ino, root->root_key.objectid); |
| 1497 | ret = -EIO; |
| 1498 | goto out; |
| 1499 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1500 | |
| 1501 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1502 | struct btrfs_file_extent_item); |
| 1503 | type = btrfs_file_extent_type(path->nodes[0], ei); |
| 1504 | compression = btrfs_file_extent_compression(path->nodes[0], ei); |
| 1505 | BUG_ON(type != BTRFS_FILE_EXTENT_INLINE); |
| 1506 | BUG_ON(compression); |
| 1507 | |
| 1508 | off = btrfs_file_extent_inline_start(ei); |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 1509 | len = btrfs_file_extent_ram_bytes(path->nodes[0], ei); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1510 | |
| 1511 | 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] | 1512 | |
| 1513 | out: |
| 1514 | btrfs_free_path(path); |
| 1515 | return ret; |
| 1516 | } |
| 1517 | |
| 1518 | /* |
| 1519 | * Helper function to generate a file name that is unique in the root of |
| 1520 | * send_root and parent_root. This is used to generate names for orphan inodes. |
| 1521 | */ |
| 1522 | static int gen_unique_name(struct send_ctx *sctx, |
| 1523 | u64 ino, u64 gen, |
| 1524 | struct fs_path *dest) |
| 1525 | { |
| 1526 | int ret = 0; |
| 1527 | struct btrfs_path *path; |
| 1528 | struct btrfs_dir_item *di; |
| 1529 | char tmp[64]; |
| 1530 | int len; |
| 1531 | u64 idx = 0; |
| 1532 | |
| 1533 | path = alloc_path_for_send(); |
| 1534 | if (!path) |
| 1535 | return -ENOMEM; |
| 1536 | |
| 1537 | while (1) { |
Filipe David Borba Manana | f74b86d | 2014-01-21 23:36:38 +0000 | [diff] [blame] | 1538 | len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu", |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1539 | ino, gen, idx); |
David Sterba | 64792f2 | 2014-02-03 18:24:09 +0100 | [diff] [blame] | 1540 | ASSERT(len < sizeof(tmp)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1541 | |
| 1542 | di = btrfs_lookup_dir_item(NULL, sctx->send_root, |
| 1543 | path, BTRFS_FIRST_FREE_OBJECTID, |
| 1544 | tmp, strlen(tmp), 0); |
| 1545 | btrfs_release_path(path); |
| 1546 | if (IS_ERR(di)) { |
| 1547 | ret = PTR_ERR(di); |
| 1548 | goto out; |
| 1549 | } |
| 1550 | if (di) { |
| 1551 | /* not unique, try again */ |
| 1552 | idx++; |
| 1553 | continue; |
| 1554 | } |
| 1555 | |
| 1556 | if (!sctx->parent_root) { |
| 1557 | /* unique */ |
| 1558 | ret = 0; |
| 1559 | break; |
| 1560 | } |
| 1561 | |
| 1562 | di = btrfs_lookup_dir_item(NULL, sctx->parent_root, |
| 1563 | path, BTRFS_FIRST_FREE_OBJECTID, |
| 1564 | tmp, strlen(tmp), 0); |
| 1565 | btrfs_release_path(path); |
| 1566 | if (IS_ERR(di)) { |
| 1567 | ret = PTR_ERR(di); |
| 1568 | goto out; |
| 1569 | } |
| 1570 | if (di) { |
| 1571 | /* not unique, try again */ |
| 1572 | idx++; |
| 1573 | continue; |
| 1574 | } |
| 1575 | /* unique */ |
| 1576 | break; |
| 1577 | } |
| 1578 | |
| 1579 | ret = fs_path_add(dest, tmp, strlen(tmp)); |
| 1580 | |
| 1581 | out: |
| 1582 | btrfs_free_path(path); |
| 1583 | return ret; |
| 1584 | } |
| 1585 | |
| 1586 | enum inode_state { |
| 1587 | inode_state_no_change, |
| 1588 | inode_state_will_create, |
| 1589 | inode_state_did_create, |
| 1590 | inode_state_will_delete, |
| 1591 | inode_state_did_delete, |
| 1592 | }; |
| 1593 | |
| 1594 | static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1595 | { |
| 1596 | int ret; |
| 1597 | int left_ret; |
| 1598 | int right_ret; |
| 1599 | u64 left_gen; |
| 1600 | u64 right_gen; |
| 1601 | |
| 1602 | 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] | 1603 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1604 | if (ret < 0 && ret != -ENOENT) |
| 1605 | goto out; |
| 1606 | left_ret = ret; |
| 1607 | |
| 1608 | if (!sctx->parent_root) { |
| 1609 | right_ret = -ENOENT; |
| 1610 | } else { |
| 1611 | ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1612 | NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1613 | if (ret < 0 && ret != -ENOENT) |
| 1614 | goto out; |
| 1615 | right_ret = ret; |
| 1616 | } |
| 1617 | |
| 1618 | if (!left_ret && !right_ret) { |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1619 | if (left_gen == gen && right_gen == gen) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1620 | ret = inode_state_no_change; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1621 | } else if (left_gen == gen) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1622 | if (ino < sctx->send_progress) |
| 1623 | ret = inode_state_did_create; |
| 1624 | else |
| 1625 | ret = inode_state_will_create; |
| 1626 | } else if (right_gen == gen) { |
| 1627 | if (ino < sctx->send_progress) |
| 1628 | ret = inode_state_did_delete; |
| 1629 | else |
| 1630 | ret = inode_state_will_delete; |
| 1631 | } else { |
| 1632 | ret = -ENOENT; |
| 1633 | } |
| 1634 | } else if (!left_ret) { |
| 1635 | if (left_gen == gen) { |
| 1636 | if (ino < sctx->send_progress) |
| 1637 | ret = inode_state_did_create; |
| 1638 | else |
| 1639 | ret = inode_state_will_create; |
| 1640 | } else { |
| 1641 | ret = -ENOENT; |
| 1642 | } |
| 1643 | } else if (!right_ret) { |
| 1644 | 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 { |
| 1653 | ret = -ENOENT; |
| 1654 | } |
| 1655 | |
| 1656 | out: |
| 1657 | return ret; |
| 1658 | } |
| 1659 | |
| 1660 | static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1661 | { |
| 1662 | int ret; |
| 1663 | |
Robbie Ko | 4dd9920 | 2017-01-05 16:24:55 +0800 | [diff] [blame] | 1664 | if (ino == BTRFS_FIRST_FREE_OBJECTID) |
| 1665 | return 1; |
| 1666 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1667 | ret = get_cur_inode_state(sctx, ino, gen); |
| 1668 | if (ret < 0) |
| 1669 | goto out; |
| 1670 | |
| 1671 | if (ret == inode_state_no_change || |
| 1672 | ret == inode_state_did_create || |
| 1673 | ret == inode_state_will_delete) |
| 1674 | ret = 1; |
| 1675 | else |
| 1676 | ret = 0; |
| 1677 | |
| 1678 | out: |
| 1679 | return ret; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
| 1683 | * Helper function to lookup a dir item in a dir. |
| 1684 | */ |
| 1685 | static int lookup_dir_item_inode(struct btrfs_root *root, |
| 1686 | u64 dir, const char *name, int name_len, |
| 1687 | u64 *found_inode, |
| 1688 | u8 *found_type) |
| 1689 | { |
| 1690 | int ret = 0; |
| 1691 | struct btrfs_dir_item *di; |
| 1692 | struct btrfs_key key; |
| 1693 | struct btrfs_path *path; |
| 1694 | |
| 1695 | path = alloc_path_for_send(); |
| 1696 | if (!path) |
| 1697 | return -ENOMEM; |
| 1698 | |
| 1699 | di = btrfs_lookup_dir_item(NULL, root, path, |
| 1700 | dir, name, name_len, 0); |
Liu Bo | 3cf5068 | 2018-09-12 06:06:26 +0800 | [diff] [blame] | 1701 | if (IS_ERR_OR_NULL(di)) { |
| 1702 | ret = di ? PTR_ERR(di) : -ENOENT; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1703 | goto out; |
| 1704 | } |
| 1705 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
Filipe Manana | 1af5607 | 2014-05-25 04:49:24 +0100 | [diff] [blame] | 1706 | if (key.type == BTRFS_ROOT_ITEM_KEY) { |
| 1707 | ret = -ENOENT; |
| 1708 | goto out; |
| 1709 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1710 | *found_inode = key.objectid; |
| 1711 | *found_type = btrfs_dir_type(path->nodes[0], di); |
| 1712 | |
| 1713 | out: |
| 1714 | btrfs_free_path(path); |
| 1715 | return ret; |
| 1716 | } |
| 1717 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1718 | /* |
| 1719 | * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir, |
| 1720 | * generation of the parent dir and the name of the dir entry. |
| 1721 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1722 | static int get_first_ref(struct btrfs_root *root, u64 ino, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1723 | u64 *dir, u64 *dir_gen, struct fs_path *name) |
| 1724 | { |
| 1725 | int ret; |
| 1726 | struct btrfs_key key; |
| 1727 | struct btrfs_key found_key; |
| 1728 | struct btrfs_path *path; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1729 | int len; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1730 | u64 parent_dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1731 | |
| 1732 | path = alloc_path_for_send(); |
| 1733 | if (!path) |
| 1734 | return -ENOMEM; |
| 1735 | |
| 1736 | key.objectid = ino; |
| 1737 | key.type = BTRFS_INODE_REF_KEY; |
| 1738 | key.offset = 0; |
| 1739 | |
| 1740 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 1741 | if (ret < 0) |
| 1742 | goto out; |
| 1743 | if (!ret) |
| 1744 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1745 | path->slots[0]); |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1746 | if (ret || found_key.objectid != ino || |
| 1747 | (found_key.type != BTRFS_INODE_REF_KEY && |
| 1748 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1749 | ret = -ENOENT; |
| 1750 | goto out; |
| 1751 | } |
| 1752 | |
Filipe Manana | 51a6025 | 2014-05-13 22:01:02 +0100 | [diff] [blame] | 1753 | if (found_key.type == BTRFS_INODE_REF_KEY) { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1754 | struct btrfs_inode_ref *iref; |
| 1755 | iref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1756 | struct btrfs_inode_ref); |
| 1757 | len = btrfs_inode_ref_name_len(path->nodes[0], iref); |
| 1758 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], |
| 1759 | (unsigned long)(iref + 1), |
| 1760 | len); |
| 1761 | parent_dir = found_key.offset; |
| 1762 | } else { |
| 1763 | struct btrfs_inode_extref *extref; |
| 1764 | extref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1765 | struct btrfs_inode_extref); |
| 1766 | len = btrfs_inode_extref_name_len(path->nodes[0], extref); |
| 1767 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], |
| 1768 | (unsigned long)&extref->name, len); |
| 1769 | parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref); |
| 1770 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1771 | if (ret < 0) |
| 1772 | goto out; |
| 1773 | btrfs_release_path(path); |
| 1774 | |
Filipe Manana | b46ab97 | 2014-03-21 12:46:54 +0000 | [diff] [blame] | 1775 | if (dir_gen) { |
| 1776 | ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, |
| 1777 | NULL, NULL, NULL); |
| 1778 | if (ret < 0) |
| 1779 | goto out; |
| 1780 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1781 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1782 | *dir = parent_dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1783 | |
| 1784 | out: |
| 1785 | btrfs_free_path(path); |
| 1786 | return ret; |
| 1787 | } |
| 1788 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1789 | static int is_first_ref(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1790 | u64 ino, u64 dir, |
| 1791 | const char *name, int name_len) |
| 1792 | { |
| 1793 | int ret; |
| 1794 | struct fs_path *tmp_name; |
| 1795 | u64 tmp_dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1796 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1797 | tmp_name = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1798 | if (!tmp_name) |
| 1799 | return -ENOMEM; |
| 1800 | |
Filipe Manana | b46ab97 | 2014-03-21 12:46:54 +0000 | [diff] [blame] | 1801 | ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1802 | if (ret < 0) |
| 1803 | goto out; |
| 1804 | |
Alexander Block | b9291af | 2012-07-28 11:07:18 +0200 | [diff] [blame] | 1805 | if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1806 | ret = 0; |
| 1807 | goto out; |
| 1808 | } |
| 1809 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1810 | ret = !memcmp(tmp_name->start, name, name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1811 | |
| 1812 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1813 | fs_path_free(tmp_name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1814 | return ret; |
| 1815 | } |
| 1816 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1817 | /* |
| 1818 | * Used by process_recorded_refs to determine if a new ref would overwrite an |
| 1819 | * already existing ref. In case it detects an overwrite, it returns the |
| 1820 | * inode/gen in who_ino/who_gen. |
| 1821 | * When an overwrite is detected, process_recorded_refs does proper orphanizing |
| 1822 | * to make sure later references to the overwritten inode are possible. |
| 1823 | * Orphanizing is however only required for the first ref of an inode. |
| 1824 | * process_recorded_refs does an additional is_first_ref check to see if |
| 1825 | * orphanizing is really required. |
| 1826 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1827 | static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
| 1828 | const char *name, int name_len, |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 1829 | u64 *who_ino, u64 *who_gen, u64 *who_mode) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1830 | { |
| 1831 | int ret = 0; |
Josef Bacik | ebdad91 | 2013-08-06 16:47:48 -0400 | [diff] [blame] | 1832 | u64 gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1833 | u64 other_inode = 0; |
| 1834 | u8 other_type = 0; |
| 1835 | |
| 1836 | if (!sctx->parent_root) |
| 1837 | goto out; |
| 1838 | |
| 1839 | ret = is_inode_existent(sctx, dir, dir_gen); |
| 1840 | if (ret <= 0) |
| 1841 | goto out; |
| 1842 | |
Josef Bacik | ebdad91 | 2013-08-06 16:47:48 -0400 | [diff] [blame] | 1843 | /* |
| 1844 | * 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] | 1845 | * 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] | 1846 | * and we can just unlink this entry. |
| 1847 | */ |
Robbie Ko | 4dd9920 | 2017-01-05 16:24:55 +0800 | [diff] [blame] | 1848 | if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) { |
Josef Bacik | ebdad91 | 2013-08-06 16:47:48 -0400 | [diff] [blame] | 1849 | ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, |
| 1850 | NULL, NULL, NULL); |
| 1851 | if (ret < 0 && ret != -ENOENT) |
| 1852 | goto out; |
| 1853 | if (ret) { |
| 1854 | ret = 0; |
| 1855 | goto out; |
| 1856 | } |
| 1857 | if (gen != dir_gen) |
| 1858 | goto out; |
| 1859 | } |
| 1860 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1861 | ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, |
| 1862 | &other_inode, &other_type); |
| 1863 | if (ret < 0 && ret != -ENOENT) |
| 1864 | goto out; |
| 1865 | if (ret) { |
| 1866 | ret = 0; |
| 1867 | goto out; |
| 1868 | } |
| 1869 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1870 | /* |
| 1871 | * Check if the overwritten ref was already processed. If yes, the ref |
| 1872 | * was already unlinked/moved, so we can safely assume that we will not |
| 1873 | * overwrite anything at this point in time. |
| 1874 | */ |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 1875 | if (other_inode > sctx->send_progress || |
| 1876 | is_waiting_for_move(sctx, other_inode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1877 | ret = get_inode_info(sctx->parent_root, other_inode, NULL, |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 1878 | who_gen, who_mode, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1879 | if (ret < 0) |
| 1880 | goto out; |
| 1881 | |
| 1882 | ret = 1; |
| 1883 | *who_ino = other_inode; |
| 1884 | } else { |
| 1885 | ret = 0; |
| 1886 | } |
| 1887 | |
| 1888 | out: |
| 1889 | return ret; |
| 1890 | } |
| 1891 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1892 | /* |
| 1893 | * Checks if the ref was overwritten by an already processed inode. This is |
| 1894 | * used by __get_cur_name_and_parent to find out if the ref was orphanized and |
| 1895 | * thus the orphan name needs be used. |
| 1896 | * process_recorded_refs also uses it to avoid unlinking of refs that were |
| 1897 | * overwritten. |
| 1898 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1899 | static int did_overwrite_ref(struct send_ctx *sctx, |
| 1900 | u64 dir, u64 dir_gen, |
| 1901 | u64 ino, u64 ino_gen, |
| 1902 | const char *name, int name_len) |
| 1903 | { |
| 1904 | int ret = 0; |
| 1905 | u64 gen; |
| 1906 | u64 ow_inode; |
| 1907 | u8 other_type; |
| 1908 | |
| 1909 | if (!sctx->parent_root) |
| 1910 | goto out; |
| 1911 | |
| 1912 | ret = is_inode_existent(sctx, dir, dir_gen); |
| 1913 | if (ret <= 0) |
| 1914 | goto out; |
| 1915 | |
Robbie Ko | 0191410 | 2017-01-05 16:24:58 +0800 | [diff] [blame] | 1916 | if (dir != BTRFS_FIRST_FREE_OBJECTID) { |
| 1917 | ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, |
| 1918 | NULL, NULL, NULL); |
| 1919 | if (ret < 0 && ret != -ENOENT) |
| 1920 | goto out; |
| 1921 | if (ret) { |
| 1922 | ret = 0; |
| 1923 | goto out; |
| 1924 | } |
| 1925 | if (gen != dir_gen) |
| 1926 | goto out; |
| 1927 | } |
| 1928 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1929 | /* check if the ref was overwritten by another ref */ |
| 1930 | ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, |
| 1931 | &ow_inode, &other_type); |
| 1932 | if (ret < 0 && ret != -ENOENT) |
| 1933 | goto out; |
| 1934 | if (ret) { |
| 1935 | /* was never and will never be overwritten */ |
| 1936 | ret = 0; |
| 1937 | goto out; |
| 1938 | } |
| 1939 | |
| 1940 | 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] | 1941 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1942 | if (ret < 0) |
| 1943 | goto out; |
| 1944 | |
| 1945 | if (ow_inode == ino && gen == ino_gen) { |
| 1946 | ret = 0; |
| 1947 | goto out; |
| 1948 | } |
| 1949 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 1950 | /* |
| 1951 | * We know that it is or will be overwritten. Check this now. |
| 1952 | * The current inode being processed might have been the one that caused |
Filipe Manana | b786f16 | 2015-09-26 15:30:23 +0100 | [diff] [blame] | 1953 | * inode 'ino' to be orphanized, therefore check if ow_inode matches |
| 1954 | * the current inode being processed. |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 1955 | */ |
Filipe Manana | b786f16 | 2015-09-26 15:30:23 +0100 | [diff] [blame] | 1956 | if ((ow_inode < sctx->send_progress) || |
| 1957 | (ino != sctx->cur_ino && ow_inode == sctx->cur_ino && |
| 1958 | gen == sctx->cur_inode_gen)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1959 | ret = 1; |
| 1960 | else |
| 1961 | ret = 0; |
| 1962 | |
| 1963 | out: |
| 1964 | return ret; |
| 1965 | } |
| 1966 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1967 | /* |
| 1968 | * Same as did_overwrite_ref, but also checks if it is the first ref of an inode |
| 1969 | * that got overwritten. This is used by process_recorded_refs to determine |
| 1970 | * if it has to use the path as returned by get_cur_path or the orphan name. |
| 1971 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1972 | static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1973 | { |
| 1974 | int ret = 0; |
| 1975 | struct fs_path *name = NULL; |
| 1976 | u64 dir; |
| 1977 | u64 dir_gen; |
| 1978 | |
| 1979 | if (!sctx->parent_root) |
| 1980 | goto out; |
| 1981 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1982 | name = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1983 | if (!name) |
| 1984 | return -ENOMEM; |
| 1985 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1986 | ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1987 | if (ret < 0) |
| 1988 | goto out; |
| 1989 | |
| 1990 | ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, |
| 1991 | name->start, fs_path_len(name)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1992 | |
| 1993 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1994 | fs_path_free(name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1995 | return ret; |
| 1996 | } |
| 1997 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1998 | /* |
| 1999 | * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit, |
| 2000 | * so we need to do some special handling in case we have clashes. This function |
| 2001 | * 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] | 2002 | * In case of error, nce is kfreed. |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2003 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2004 | static int name_cache_insert(struct send_ctx *sctx, |
| 2005 | struct name_cache_entry *nce) |
| 2006 | { |
| 2007 | int ret = 0; |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2008 | struct list_head *nce_head; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2009 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2010 | nce_head = radix_tree_lookup(&sctx->name_cache, |
| 2011 | (unsigned long)nce->ino); |
| 2012 | if (!nce_head) { |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2013 | nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL); |
Tsutomu Itoh | cfa7a9c | 2012-12-17 06:38:51 +0000 | [diff] [blame] | 2014 | if (!nce_head) { |
| 2015 | kfree(nce); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2016 | return -ENOMEM; |
Tsutomu Itoh | cfa7a9c | 2012-12-17 06:38:51 +0000 | [diff] [blame] | 2017 | } |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2018 | INIT_LIST_HEAD(nce_head); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2019 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2020 | ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); |
Alexander Block | 5dc67d0 | 2012-08-01 12:07:43 +0200 | [diff] [blame] | 2021 | if (ret < 0) { |
| 2022 | kfree(nce_head); |
| 2023 | kfree(nce); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2024 | return ret; |
Alexander Block | 5dc67d0 | 2012-08-01 12:07:43 +0200 | [diff] [blame] | 2025 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2026 | } |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2027 | list_add_tail(&nce->radix_list, nce_head); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2028 | list_add_tail(&nce->list, &sctx->name_cache_list); |
| 2029 | sctx->name_cache_size++; |
| 2030 | |
| 2031 | return ret; |
| 2032 | } |
| 2033 | |
| 2034 | static void name_cache_delete(struct send_ctx *sctx, |
| 2035 | struct name_cache_entry *nce) |
| 2036 | { |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2037 | struct list_head *nce_head; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2038 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2039 | nce_head = radix_tree_lookup(&sctx->name_cache, |
| 2040 | (unsigned long)nce->ino); |
David Sterba | 57fb891 | 2014-02-03 19:24:40 +0100 | [diff] [blame] | 2041 | if (!nce_head) { |
| 2042 | btrfs_err(sctx->send_root->fs_info, |
| 2043 | "name_cache_delete lookup failed ino %llu cache size %d, leaking memory", |
| 2044 | nce->ino, sctx->name_cache_size); |
| 2045 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2046 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2047 | list_del(&nce->radix_list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2048 | list_del(&nce->list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2049 | sctx->name_cache_size--; |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2050 | |
David Sterba | 57fb891 | 2014-02-03 19:24:40 +0100 | [diff] [blame] | 2051 | /* |
| 2052 | * We may not get to the final release of nce_head if the lookup fails |
| 2053 | */ |
| 2054 | if (nce_head && list_empty(nce_head)) { |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2055 | radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); |
| 2056 | kfree(nce_head); |
| 2057 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, |
| 2061 | u64 ino, u64 gen) |
| 2062 | { |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2063 | struct list_head *nce_head; |
| 2064 | struct name_cache_entry *cur; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2065 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2066 | nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); |
| 2067 | if (!nce_head) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2068 | return NULL; |
| 2069 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 2070 | list_for_each_entry(cur, nce_head, radix_list) { |
| 2071 | if (cur->ino == ino && cur->gen == gen) |
| 2072 | return cur; |
| 2073 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2074 | return NULL; |
| 2075 | } |
| 2076 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2077 | /* |
| 2078 | * Removes the entry from the list and adds it back to the end. This marks the |
| 2079 | * entry as recently used so that name_cache_clean_unused does not remove it. |
| 2080 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2081 | static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce) |
| 2082 | { |
| 2083 | list_del(&nce->list); |
| 2084 | list_add_tail(&nce->list, &sctx->name_cache_list); |
| 2085 | } |
| 2086 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2087 | /* |
| 2088 | * Remove some entries from the beginning of name_cache_list. |
| 2089 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2090 | static void name_cache_clean_unused(struct send_ctx *sctx) |
| 2091 | { |
| 2092 | struct name_cache_entry *nce; |
| 2093 | |
| 2094 | if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) |
| 2095 | return; |
| 2096 | |
| 2097 | while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { |
| 2098 | nce = list_entry(sctx->name_cache_list.next, |
| 2099 | struct name_cache_entry, list); |
| 2100 | name_cache_delete(sctx, nce); |
| 2101 | kfree(nce); |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | static void name_cache_free(struct send_ctx *sctx) |
| 2106 | { |
| 2107 | struct name_cache_entry *nce; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2108 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2109 | while (!list_empty(&sctx->name_cache_list)) { |
| 2110 | nce = list_entry(sctx->name_cache_list.next, |
| 2111 | struct name_cache_entry, list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2112 | name_cache_delete(sctx, nce); |
Alexander Block | 17589bd | 2012-07-28 14:13:35 +0200 | [diff] [blame] | 2113 | kfree(nce); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2117 | /* |
| 2118 | * Used by get_cur_path for each ref up to the root. |
| 2119 | * Returns 0 if it succeeded. |
| 2120 | * Returns 1 if the inode is not existent or got overwritten. In that case, the |
| 2121 | * name is an orphan name. This instructs get_cur_path to stop iterating. If 1 |
| 2122 | * is returned, parent_ino/parent_gen are not guaranteed to be valid. |
| 2123 | * Returns <0 in case of error. |
| 2124 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2125 | static int __get_cur_name_and_parent(struct send_ctx *sctx, |
| 2126 | u64 ino, u64 gen, |
| 2127 | u64 *parent_ino, |
| 2128 | u64 *parent_gen, |
| 2129 | struct fs_path *dest) |
| 2130 | { |
| 2131 | int ret; |
| 2132 | int nce_ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2133 | struct name_cache_entry *nce = NULL; |
| 2134 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2135 | /* |
| 2136 | * First check if we already did a call to this function with the same |
| 2137 | * ino/gen. If yes, check if the cache entry is still up-to-date. If yes |
| 2138 | * return the cached result. |
| 2139 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2140 | nce = name_cache_search(sctx, ino, gen); |
| 2141 | if (nce) { |
| 2142 | if (ino < sctx->send_progress && nce->need_later_update) { |
| 2143 | name_cache_delete(sctx, nce); |
| 2144 | kfree(nce); |
| 2145 | nce = NULL; |
| 2146 | } else { |
| 2147 | name_cache_used(sctx, nce); |
| 2148 | *parent_ino = nce->parent_ino; |
| 2149 | *parent_gen = nce->parent_gen; |
| 2150 | ret = fs_path_add(dest, nce->name, nce->name_len); |
| 2151 | if (ret < 0) |
| 2152 | goto out; |
| 2153 | ret = nce->ret; |
| 2154 | goto out; |
| 2155 | } |
| 2156 | } |
| 2157 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2158 | /* |
| 2159 | * If the inode is not existent yet, add the orphan name and return 1. |
| 2160 | * This should only happen for the parent dir that we determine in |
| 2161 | * __record_new_ref |
| 2162 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2163 | ret = is_inode_existent(sctx, ino, gen); |
| 2164 | if (ret < 0) |
| 2165 | goto out; |
| 2166 | |
| 2167 | if (!ret) { |
| 2168 | ret = gen_unique_name(sctx, ino, gen, dest); |
| 2169 | if (ret < 0) |
| 2170 | goto out; |
| 2171 | ret = 1; |
| 2172 | goto out_cache; |
| 2173 | } |
| 2174 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2175 | /* |
| 2176 | * Depending on whether the inode was already processed or not, use |
| 2177 | * send_root or parent_root for ref lookup. |
| 2178 | */ |
Filipe Manana | bf0d1f4 | 2014-02-21 00:01:32 +0000 | [diff] [blame] | 2179 | if (ino < sctx->send_progress) |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2180 | ret = get_first_ref(sctx->send_root, ino, |
| 2181 | parent_ino, parent_gen, dest); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2182 | else |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2183 | ret = get_first_ref(sctx->parent_root, ino, |
| 2184 | parent_ino, parent_gen, dest); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2185 | if (ret < 0) |
| 2186 | goto out; |
| 2187 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2188 | /* |
| 2189 | * Check if the ref was overwritten by an inode's ref that was processed |
| 2190 | * earlier. If yes, treat as orphan and return 1. |
| 2191 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2192 | ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, |
| 2193 | dest->start, dest->end - dest->start); |
| 2194 | if (ret < 0) |
| 2195 | goto out; |
| 2196 | if (ret) { |
| 2197 | fs_path_reset(dest); |
| 2198 | ret = gen_unique_name(sctx, ino, gen, dest); |
| 2199 | if (ret < 0) |
| 2200 | goto out; |
| 2201 | ret = 1; |
| 2202 | } |
| 2203 | |
| 2204 | out_cache: |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2205 | /* |
| 2206 | * Store the result of the lookup in the name cache. |
| 2207 | */ |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2208 | nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2209 | if (!nce) { |
| 2210 | ret = -ENOMEM; |
| 2211 | goto out; |
| 2212 | } |
| 2213 | |
| 2214 | nce->ino = ino; |
| 2215 | nce->gen = gen; |
| 2216 | nce->parent_ino = *parent_ino; |
| 2217 | nce->parent_gen = *parent_gen; |
| 2218 | nce->name_len = fs_path_len(dest); |
| 2219 | nce->ret = ret; |
| 2220 | strcpy(nce->name, dest->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2221 | |
| 2222 | if (ino < sctx->send_progress) |
| 2223 | nce->need_later_update = 0; |
| 2224 | else |
| 2225 | nce->need_later_update = 1; |
| 2226 | |
| 2227 | nce_ret = name_cache_insert(sctx, nce); |
| 2228 | if (nce_ret < 0) |
| 2229 | ret = nce_ret; |
| 2230 | name_cache_clean_unused(sctx); |
| 2231 | |
| 2232 | out: |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2233 | return ret; |
| 2234 | } |
| 2235 | |
| 2236 | /* |
| 2237 | * Magic happens here. This function returns the first ref to an inode as it |
| 2238 | * would look like while receiving the stream at this point in time. |
| 2239 | * We walk the path up to the root. For every inode in between, we check if it |
| 2240 | * was already processed/sent. If yes, we continue with the parent as found |
| 2241 | * in send_root. If not, we continue with the parent as found in parent_root. |
| 2242 | * If we encounter an inode that was deleted at this point in time, we use the |
| 2243 | * inodes "orphan" name instead of the real name and stop. Same with new inodes |
| 2244 | * that were not created yet and overwritten inodes/refs. |
| 2245 | * |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2246 | * When do we have orphan inodes: |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2247 | * 1. When an inode is freshly created and thus no valid refs are available yet |
| 2248 | * 2. When a directory lost all it's refs (deleted) but still has dir items |
| 2249 | * inside which were not processed yet (pending for move/delete). If anyone |
| 2250 | * tried to get the path to the dir items, it would get a path inside that |
| 2251 | * orphan directory. |
| 2252 | * 3. When an inode is moved around or gets new links, it may overwrite the ref |
| 2253 | * of an unprocessed inode. If in that case the first ref would be |
| 2254 | * overwritten, the overwritten inode gets "orphanized". Later when we |
| 2255 | * process this overwritten inode, it is restored at a new place by moving |
| 2256 | * the orphan inode. |
| 2257 | * |
| 2258 | * sctx->send_progress tells this function at which point in time receiving |
| 2259 | * would be. |
| 2260 | */ |
| 2261 | static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, |
| 2262 | struct fs_path *dest) |
| 2263 | { |
| 2264 | int ret = 0; |
| 2265 | struct fs_path *name = NULL; |
| 2266 | u64 parent_inode = 0; |
| 2267 | u64 parent_gen = 0; |
| 2268 | int stop = 0; |
| 2269 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2270 | name = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2271 | if (!name) { |
| 2272 | ret = -ENOMEM; |
| 2273 | goto out; |
| 2274 | } |
| 2275 | |
| 2276 | dest->reversed = 1; |
| 2277 | fs_path_reset(dest); |
| 2278 | |
| 2279 | while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) { |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 2280 | struct waiting_dir_move *wdm; |
| 2281 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2282 | fs_path_reset(name); |
| 2283 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2284 | if (is_waiting_for_rm(sctx, ino)) { |
| 2285 | ret = gen_unique_name(sctx, ino, gen, name); |
| 2286 | if (ret < 0) |
| 2287 | goto out; |
| 2288 | ret = fs_path_add_path(dest, name); |
| 2289 | break; |
| 2290 | } |
| 2291 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 2292 | wdm = get_waiting_dir_move(sctx, ino); |
| 2293 | if (wdm && wdm->orphanized) { |
| 2294 | ret = gen_unique_name(sctx, ino, gen, name); |
| 2295 | stop = 1; |
| 2296 | } else if (wdm) { |
Filipe Manana | bf0d1f4 | 2014-02-21 00:01:32 +0000 | [diff] [blame] | 2297 | ret = get_first_ref(sctx->parent_root, ino, |
| 2298 | &parent_inode, &parent_gen, name); |
| 2299 | } else { |
| 2300 | ret = __get_cur_name_and_parent(sctx, ino, gen, |
| 2301 | &parent_inode, |
| 2302 | &parent_gen, name); |
| 2303 | if (ret) |
| 2304 | stop = 1; |
| 2305 | } |
| 2306 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2307 | if (ret < 0) |
| 2308 | goto out; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2309 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2310 | ret = fs_path_add_path(dest, name); |
| 2311 | if (ret < 0) |
| 2312 | goto out; |
| 2313 | |
| 2314 | ino = parent_inode; |
| 2315 | gen = parent_gen; |
| 2316 | } |
| 2317 | |
| 2318 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2319 | fs_path_free(name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2320 | if (!ret) |
| 2321 | fs_path_unreverse(dest); |
| 2322 | return ret; |
| 2323 | } |
| 2324 | |
| 2325 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2326 | * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace |
| 2327 | */ |
| 2328 | static int send_subvol_begin(struct send_ctx *sctx) |
| 2329 | { |
| 2330 | int ret; |
| 2331 | struct btrfs_root *send_root = sctx->send_root; |
| 2332 | struct btrfs_root *parent_root = sctx->parent_root; |
| 2333 | struct btrfs_path *path; |
| 2334 | struct btrfs_key key; |
| 2335 | struct btrfs_root_ref *ref; |
| 2336 | struct extent_buffer *leaf; |
| 2337 | char *name = NULL; |
| 2338 | int namelen; |
| 2339 | |
Wang Shilong | ffcfaf8 | 2014-01-15 00:26:43 +0800 | [diff] [blame] | 2340 | path = btrfs_alloc_path(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2341 | if (!path) |
| 2342 | return -ENOMEM; |
| 2343 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2344 | name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2345 | if (!name) { |
| 2346 | btrfs_free_path(path); |
| 2347 | return -ENOMEM; |
| 2348 | } |
| 2349 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 2350 | key.objectid = send_root->root_key.objectid; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2351 | key.type = BTRFS_ROOT_BACKREF_KEY; |
| 2352 | key.offset = 0; |
| 2353 | |
| 2354 | ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root, |
| 2355 | &key, path, 1, 0); |
| 2356 | if (ret < 0) |
| 2357 | goto out; |
| 2358 | if (ret) { |
| 2359 | ret = -ENOENT; |
| 2360 | goto out; |
| 2361 | } |
| 2362 | |
| 2363 | leaf = path->nodes[0]; |
| 2364 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2365 | if (key.type != BTRFS_ROOT_BACKREF_KEY || |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 2366 | key.objectid != send_root->root_key.objectid) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2367 | ret = -ENOENT; |
| 2368 | goto out; |
| 2369 | } |
| 2370 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 2371 | namelen = btrfs_root_ref_name_len(leaf, ref); |
| 2372 | read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen); |
| 2373 | btrfs_release_path(path); |
| 2374 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2375 | if (parent_root) { |
| 2376 | ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); |
| 2377 | if (ret < 0) |
| 2378 | goto out; |
| 2379 | } else { |
| 2380 | ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); |
| 2381 | if (ret < 0) |
| 2382 | goto out; |
| 2383 | } |
| 2384 | |
| 2385 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); |
Robin Ruede | b96b1db | 2015-09-30 21:23:33 +0200 | [diff] [blame] | 2386 | |
| 2387 | if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid)) |
| 2388 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, |
| 2389 | sctx->send_root->root_item.received_uuid); |
| 2390 | else |
| 2391 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, |
| 2392 | sctx->send_root->root_item.uuid); |
| 2393 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2394 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, |
Filipe David Borba Manana | 5a0f4e2 | 2013-12-03 15:55:48 +0000 | [diff] [blame] | 2395 | le64_to_cpu(sctx->send_root->root_item.ctransid)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2396 | if (parent_root) { |
Josef Bacik | 37b8d27 | 2015-06-04 17:17:25 -0400 | [diff] [blame] | 2397 | if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid)) |
| 2398 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 2399 | parent_root->root_item.received_uuid); |
| 2400 | else |
| 2401 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 2402 | parent_root->root_item.uuid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2403 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
Filipe David Borba Manana | 5a0f4e2 | 2013-12-03 15:55:48 +0000 | [diff] [blame] | 2404 | le64_to_cpu(sctx->parent_root->root_item.ctransid)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | ret = send_cmd(sctx); |
| 2408 | |
| 2409 | tlv_put_failure: |
| 2410 | out: |
| 2411 | btrfs_free_path(path); |
| 2412 | kfree(name); |
| 2413 | return ret; |
| 2414 | } |
| 2415 | |
| 2416 | static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) |
| 2417 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2418 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2419 | int ret = 0; |
| 2420 | struct fs_path *p; |
| 2421 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2422 | btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2423 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2424 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2425 | if (!p) |
| 2426 | return -ENOMEM; |
| 2427 | |
| 2428 | ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); |
| 2429 | if (ret < 0) |
| 2430 | goto out; |
| 2431 | |
| 2432 | ret = get_cur_path(sctx, ino, gen, p); |
| 2433 | if (ret < 0) |
| 2434 | goto out; |
| 2435 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2436 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); |
| 2437 | |
| 2438 | ret = send_cmd(sctx); |
| 2439 | |
| 2440 | tlv_put_failure: |
| 2441 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2442 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2443 | return ret; |
| 2444 | } |
| 2445 | |
| 2446 | static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) |
| 2447 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2448 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2449 | int ret = 0; |
| 2450 | struct fs_path *p; |
| 2451 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2452 | btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2453 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2454 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2455 | if (!p) |
| 2456 | return -ENOMEM; |
| 2457 | |
| 2458 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); |
| 2459 | if (ret < 0) |
| 2460 | goto out; |
| 2461 | |
| 2462 | ret = get_cur_path(sctx, ino, gen, p); |
| 2463 | if (ret < 0) |
| 2464 | goto out; |
| 2465 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2466 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); |
| 2467 | |
| 2468 | ret = send_cmd(sctx); |
| 2469 | |
| 2470 | tlv_put_failure: |
| 2471 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2472 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2473 | return ret; |
| 2474 | } |
| 2475 | |
| 2476 | static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) |
| 2477 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2478 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2479 | int ret = 0; |
| 2480 | struct fs_path *p; |
| 2481 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2482 | btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu", |
| 2483 | ino, uid, gid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2484 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2485 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2486 | if (!p) |
| 2487 | return -ENOMEM; |
| 2488 | |
| 2489 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); |
| 2490 | if (ret < 0) |
| 2491 | goto out; |
| 2492 | |
| 2493 | ret = get_cur_path(sctx, ino, gen, p); |
| 2494 | if (ret < 0) |
| 2495 | goto out; |
| 2496 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2497 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); |
| 2498 | TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); |
| 2499 | |
| 2500 | ret = send_cmd(sctx); |
| 2501 | |
| 2502 | tlv_put_failure: |
| 2503 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2504 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2505 | return ret; |
| 2506 | } |
| 2507 | |
| 2508 | static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) |
| 2509 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2510 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2511 | int ret = 0; |
| 2512 | struct fs_path *p = NULL; |
| 2513 | struct btrfs_inode_item *ii; |
| 2514 | struct btrfs_path *path = NULL; |
| 2515 | struct extent_buffer *eb; |
| 2516 | struct btrfs_key key; |
| 2517 | int slot; |
| 2518 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2519 | btrfs_debug(fs_info, "send_utimes %llu", ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2520 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2521 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2522 | if (!p) |
| 2523 | return -ENOMEM; |
| 2524 | |
| 2525 | path = alloc_path_for_send(); |
| 2526 | if (!path) { |
| 2527 | ret = -ENOMEM; |
| 2528 | goto out; |
| 2529 | } |
| 2530 | |
| 2531 | key.objectid = ino; |
| 2532 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2533 | key.offset = 0; |
| 2534 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); |
Filipe Manana | 15b253e | 2016-07-02 05:43:46 +0100 | [diff] [blame] | 2535 | if (ret > 0) |
| 2536 | ret = -ENOENT; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2537 | if (ret < 0) |
| 2538 | goto out; |
| 2539 | |
| 2540 | eb = path->nodes[0]; |
| 2541 | slot = path->slots[0]; |
| 2542 | ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); |
| 2543 | |
| 2544 | ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); |
| 2545 | if (ret < 0) |
| 2546 | goto out; |
| 2547 | |
| 2548 | ret = get_cur_path(sctx, ino, gen, p); |
| 2549 | if (ret < 0) |
| 2550 | goto out; |
| 2551 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
David Sterba | a937b97 | 2014-12-12 17:39:12 +0100 | [diff] [blame] | 2552 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime); |
| 2553 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime); |
| 2554 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime); |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2555 | /* TODO Add otime support when the otime patches get into upstream */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2556 | |
| 2557 | ret = send_cmd(sctx); |
| 2558 | |
| 2559 | tlv_put_failure: |
| 2560 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2561 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2562 | btrfs_free_path(path); |
| 2563 | return ret; |
| 2564 | } |
| 2565 | |
| 2566 | /* |
| 2567 | * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have |
| 2568 | * a valid path yet because we did not process the refs yet. So, the inode |
| 2569 | * is created as orphan. |
| 2570 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2571 | static int send_create_inode(struct send_ctx *sctx, u64 ino) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2572 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2573 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2574 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2575 | struct fs_path *p; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2576 | int cmd; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2577 | u64 gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2578 | u64 mode; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2579 | u64 rdev; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2580 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 2581 | btrfs_debug(fs_info, "send_create_inode %llu", ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2582 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2583 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2584 | if (!p) |
| 2585 | return -ENOMEM; |
| 2586 | |
Liu Bo | 644d194 | 2014-02-27 17:29:01 +0800 | [diff] [blame] | 2587 | if (ino != sctx->cur_ino) { |
| 2588 | ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, |
| 2589 | NULL, NULL, &rdev); |
| 2590 | if (ret < 0) |
| 2591 | goto out; |
| 2592 | } else { |
| 2593 | gen = sctx->cur_inode_gen; |
| 2594 | mode = sctx->cur_inode_mode; |
| 2595 | rdev = sctx->cur_inode_rdev; |
| 2596 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2597 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2598 | if (S_ISREG(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2599 | cmd = BTRFS_SEND_C_MKFILE; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2600 | } else if (S_ISDIR(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2601 | cmd = BTRFS_SEND_C_MKDIR; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2602 | } else if (S_ISLNK(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2603 | cmd = BTRFS_SEND_C_SYMLINK; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2604 | } else if (S_ISCHR(mode) || S_ISBLK(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2605 | cmd = BTRFS_SEND_C_MKNOD; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2606 | } else if (S_ISFIFO(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2607 | cmd = BTRFS_SEND_C_MKFIFO; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2608 | } else if (S_ISSOCK(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2609 | cmd = BTRFS_SEND_C_MKSOCK; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2610 | } else { |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 2611 | btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o", |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2612 | (int)(mode & S_IFMT)); |
Tsutomu Itoh | ca6842b | 2016-01-22 09:13:25 +0900 | [diff] [blame] | 2613 | ret = -EOPNOTSUPP; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2614 | goto out; |
| 2615 | } |
| 2616 | |
| 2617 | ret = begin_cmd(sctx, cmd); |
| 2618 | if (ret < 0) |
| 2619 | goto out; |
| 2620 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2621 | ret = gen_unique_name(sctx, ino, gen, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2622 | if (ret < 0) |
| 2623 | goto out; |
| 2624 | |
| 2625 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2626 | TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2627 | |
| 2628 | if (S_ISLNK(mode)) { |
| 2629 | fs_path_reset(p); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2630 | ret = read_symlink(sctx->send_root, ino, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2631 | if (ret < 0) |
| 2632 | goto out; |
| 2633 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); |
| 2634 | } else if (S_ISCHR(mode) || S_ISBLK(mode) || |
| 2635 | S_ISFIFO(mode) || S_ISSOCK(mode)) { |
Arne Jansen | d79e504 | 2012-10-15 18:28:46 +0000 | [diff] [blame] | 2636 | TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev)); |
| 2637 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | ret = send_cmd(sctx); |
| 2641 | if (ret < 0) |
| 2642 | goto out; |
| 2643 | |
| 2644 | |
| 2645 | tlv_put_failure: |
| 2646 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2647 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2648 | return ret; |
| 2649 | } |
| 2650 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2651 | /* |
| 2652 | * We need some special handling for inodes that get processed before the parent |
| 2653 | * directory got created. See process_recorded_refs for details. |
| 2654 | * This function does the check if we already created the dir out of order. |
| 2655 | */ |
| 2656 | static int did_create_dir(struct send_ctx *sctx, u64 dir) |
| 2657 | { |
| 2658 | int ret = 0; |
| 2659 | struct btrfs_path *path = NULL; |
| 2660 | struct btrfs_key key; |
| 2661 | struct btrfs_key found_key; |
| 2662 | struct btrfs_key di_key; |
| 2663 | struct extent_buffer *eb; |
| 2664 | struct btrfs_dir_item *di; |
| 2665 | int slot; |
| 2666 | |
| 2667 | path = alloc_path_for_send(); |
| 2668 | if (!path) { |
| 2669 | ret = -ENOMEM; |
| 2670 | goto out; |
| 2671 | } |
| 2672 | |
| 2673 | key.objectid = dir; |
| 2674 | key.type = BTRFS_DIR_INDEX_KEY; |
| 2675 | key.offset = 0; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2676 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); |
| 2677 | if (ret < 0) |
| 2678 | goto out; |
| 2679 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2680 | while (1) { |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2681 | eb = path->nodes[0]; |
| 2682 | slot = path->slots[0]; |
| 2683 | if (slot >= btrfs_header_nritems(eb)) { |
| 2684 | ret = btrfs_next_leaf(sctx->send_root, path); |
| 2685 | if (ret < 0) { |
| 2686 | goto out; |
| 2687 | } else if (ret > 0) { |
| 2688 | ret = 0; |
| 2689 | break; |
| 2690 | } |
| 2691 | continue; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2692 | } |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2693 | |
| 2694 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 2695 | if (found_key.objectid != key.objectid || |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2696 | found_key.type != key.type) { |
| 2697 | ret = 0; |
| 2698 | goto out; |
| 2699 | } |
| 2700 | |
| 2701 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| 2702 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 2703 | |
Josef Bacik | a052541 | 2013-08-12 10:56:14 -0400 | [diff] [blame] | 2704 | if (di_key.type != BTRFS_ROOT_ITEM_KEY && |
| 2705 | di_key.objectid < sctx->send_progress) { |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2706 | ret = 1; |
| 2707 | goto out; |
| 2708 | } |
| 2709 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2710 | path->slots[0]++; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | out: |
| 2714 | btrfs_free_path(path); |
| 2715 | return ret; |
| 2716 | } |
| 2717 | |
| 2718 | /* |
| 2719 | * Only creates the inode if it is: |
| 2720 | * 1. Not a directory |
| 2721 | * 2. Or a directory which was not created already due to out of order |
| 2722 | * directories. See did_create_dir and process_recorded_refs for details. |
| 2723 | */ |
| 2724 | static int send_create_inode_if_needed(struct send_ctx *sctx) |
| 2725 | { |
| 2726 | int ret; |
| 2727 | |
| 2728 | if (S_ISDIR(sctx->cur_inode_mode)) { |
| 2729 | ret = did_create_dir(sctx, sctx->cur_ino); |
| 2730 | if (ret < 0) |
| 2731 | goto out; |
| 2732 | if (ret) { |
| 2733 | ret = 0; |
| 2734 | goto out; |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | ret = send_create_inode(sctx, sctx->cur_ino); |
| 2739 | if (ret < 0) |
| 2740 | goto out; |
| 2741 | |
| 2742 | out: |
| 2743 | return ret; |
| 2744 | } |
| 2745 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2746 | struct recorded_ref { |
| 2747 | struct list_head list; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2748 | char *name; |
| 2749 | struct fs_path *full_path; |
| 2750 | u64 dir; |
| 2751 | u64 dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2752 | int name_len; |
| 2753 | }; |
| 2754 | |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 2755 | static void set_ref_path(struct recorded_ref *ref, struct fs_path *path) |
| 2756 | { |
| 2757 | ref->full_path = path; |
| 2758 | ref->name = (char *)kbasename(ref->full_path->start); |
| 2759 | ref->name_len = ref->full_path->end - ref->name; |
| 2760 | } |
| 2761 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2762 | /* |
| 2763 | * We need to process new refs before deleted refs, but compare_tree gives us |
| 2764 | * everything mixed. So we first record all refs and later process them. |
| 2765 | * This function is a helper to record one ref. |
| 2766 | */ |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 2767 | static int __record_ref(struct list_head *head, u64 dir, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2768 | u64 dir_gen, struct fs_path *path) |
| 2769 | { |
| 2770 | struct recorded_ref *ref; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2771 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2772 | ref = kmalloc(sizeof(*ref), GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2773 | if (!ref) |
| 2774 | return -ENOMEM; |
| 2775 | |
| 2776 | ref->dir = dir; |
| 2777 | ref->dir_gen = dir_gen; |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 2778 | set_ref_path(ref, path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2779 | list_add_tail(&ref->list, head); |
| 2780 | return 0; |
| 2781 | } |
| 2782 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 2783 | static int dup_ref(struct recorded_ref *ref, struct list_head *list) |
| 2784 | { |
| 2785 | struct recorded_ref *new; |
| 2786 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 2787 | new = kmalloc(sizeof(*ref), GFP_KERNEL); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 2788 | if (!new) |
| 2789 | return -ENOMEM; |
| 2790 | |
| 2791 | new->dir = ref->dir; |
| 2792 | new->dir_gen = ref->dir_gen; |
| 2793 | new->full_path = NULL; |
| 2794 | INIT_LIST_HEAD(&new->list); |
| 2795 | list_add_tail(&new->list, list); |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2799 | static void __free_recorded_refs(struct list_head *head) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2800 | { |
| 2801 | struct recorded_ref *cur; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2802 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2803 | while (!list_empty(head)) { |
| 2804 | cur = list_entry(head->next, struct recorded_ref, list); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2805 | fs_path_free(cur->full_path); |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2806 | list_del(&cur->list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2807 | kfree(cur); |
| 2808 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | static void free_recorded_refs(struct send_ctx *sctx) |
| 2812 | { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2813 | __free_recorded_refs(&sctx->new_refs); |
| 2814 | __free_recorded_refs(&sctx->deleted_refs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | /* |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2818 | * 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] | 2819 | * ref of an unprocessed inode gets overwritten and for all non empty |
| 2820 | * directories. |
| 2821 | */ |
| 2822 | static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, |
| 2823 | struct fs_path *path) |
| 2824 | { |
| 2825 | int ret; |
| 2826 | struct fs_path *orphan; |
| 2827 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2828 | orphan = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2829 | if (!orphan) |
| 2830 | return -ENOMEM; |
| 2831 | |
| 2832 | ret = gen_unique_name(sctx, ino, gen, orphan); |
| 2833 | if (ret < 0) |
| 2834 | goto out; |
| 2835 | |
| 2836 | ret = send_rename(sctx, path, orphan); |
| 2837 | |
| 2838 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2839 | fs_path_free(orphan); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2840 | return ret; |
| 2841 | } |
| 2842 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2843 | static struct orphan_dir_info * |
| 2844 | add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino) |
| 2845 | { |
| 2846 | struct rb_node **p = &sctx->orphan_dirs.rb_node; |
| 2847 | struct rb_node *parent = NULL; |
| 2848 | struct orphan_dir_info *entry, *odi; |
| 2849 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2850 | while (*p) { |
| 2851 | parent = *p; |
| 2852 | entry = rb_entry(parent, struct orphan_dir_info, node); |
| 2853 | if (dir_ino < entry->ino) { |
| 2854 | p = &(*p)->rb_left; |
| 2855 | } else if (dir_ino > entry->ino) { |
| 2856 | p = &(*p)->rb_right; |
| 2857 | } else { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2858 | return entry; |
| 2859 | } |
| 2860 | } |
| 2861 | |
Robbie Ko | 35c8eda | 2018-05-08 18:11:37 +0800 | [diff] [blame] | 2862 | odi = kmalloc(sizeof(*odi), GFP_KERNEL); |
| 2863 | if (!odi) |
| 2864 | return ERR_PTR(-ENOMEM); |
| 2865 | odi->ino = dir_ino; |
| 2866 | odi->gen = 0; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2867 | odi->last_dir_index_offset = 0; |
Robbie Ko | 35c8eda | 2018-05-08 18:11:37 +0800 | [diff] [blame] | 2868 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2869 | rb_link_node(&odi->node, parent, p); |
| 2870 | rb_insert_color(&odi->node, &sctx->orphan_dirs); |
| 2871 | return odi; |
| 2872 | } |
| 2873 | |
| 2874 | static struct orphan_dir_info * |
| 2875 | get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino) |
| 2876 | { |
| 2877 | struct rb_node *n = sctx->orphan_dirs.rb_node; |
| 2878 | struct orphan_dir_info *entry; |
| 2879 | |
| 2880 | while (n) { |
| 2881 | entry = rb_entry(n, struct orphan_dir_info, node); |
| 2882 | if (dir_ino < entry->ino) |
| 2883 | n = n->rb_left; |
| 2884 | else if (dir_ino > entry->ino) |
| 2885 | n = n->rb_right; |
| 2886 | else |
| 2887 | return entry; |
| 2888 | } |
| 2889 | return NULL; |
| 2890 | } |
| 2891 | |
| 2892 | static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino) |
| 2893 | { |
| 2894 | struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino); |
| 2895 | |
| 2896 | return odi != NULL; |
| 2897 | } |
| 2898 | |
| 2899 | static void free_orphan_dir_info(struct send_ctx *sctx, |
| 2900 | struct orphan_dir_info *odi) |
| 2901 | { |
| 2902 | if (!odi) |
| 2903 | return; |
| 2904 | rb_erase(&odi->node, &sctx->orphan_dirs); |
| 2905 | kfree(odi); |
| 2906 | } |
| 2907 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2908 | /* |
| 2909 | * Returns 1 if a directory can be removed at this point in time. |
| 2910 | * We check this by iterating all dir items and checking if the inode behind |
| 2911 | * the dir item was already processed. |
| 2912 | */ |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2913 | static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
| 2914 | u64 send_progress) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2915 | { |
| 2916 | int ret = 0; |
| 2917 | struct btrfs_root *root = sctx->parent_root; |
| 2918 | struct btrfs_path *path; |
| 2919 | struct btrfs_key key; |
| 2920 | struct btrfs_key found_key; |
| 2921 | struct btrfs_key loc; |
| 2922 | struct btrfs_dir_item *di; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2923 | struct orphan_dir_info *odi = NULL; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2924 | |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 2925 | /* |
| 2926 | * Don't try to rmdir the top/root subvolume dir. |
| 2927 | */ |
| 2928 | if (dir == BTRFS_FIRST_FREE_OBJECTID) |
| 2929 | return 0; |
| 2930 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2931 | path = alloc_path_for_send(); |
| 2932 | if (!path) |
| 2933 | return -ENOMEM; |
| 2934 | |
| 2935 | key.objectid = dir; |
| 2936 | key.type = BTRFS_DIR_INDEX_KEY; |
| 2937 | key.offset = 0; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2938 | |
| 2939 | odi = get_orphan_dir_info(sctx, dir); |
| 2940 | if (odi) |
| 2941 | key.offset = odi->last_dir_index_offset; |
| 2942 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2943 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2944 | if (ret < 0) |
| 2945 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2946 | |
| 2947 | while (1) { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2948 | struct waiting_dir_move *dm; |
| 2949 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2950 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 2951 | ret = btrfs_next_leaf(root, path); |
| 2952 | if (ret < 0) |
| 2953 | goto out; |
| 2954 | else if (ret > 0) |
| 2955 | break; |
| 2956 | continue; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2957 | } |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2958 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2959 | path->slots[0]); |
| 2960 | if (found_key.objectid != key.objectid || |
| 2961 | found_key.type != key.type) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2962 | break; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2963 | |
| 2964 | di = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2965 | struct btrfs_dir_item); |
| 2966 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc); |
| 2967 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2968 | dm = get_waiting_dir_move(sctx, loc.objectid); |
| 2969 | if (dm) { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2970 | odi = add_orphan_dir_info(sctx, dir); |
| 2971 | if (IS_ERR(odi)) { |
| 2972 | ret = PTR_ERR(odi); |
| 2973 | goto out; |
| 2974 | } |
| 2975 | odi->gen = dir_gen; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2976 | odi->last_dir_index_offset = found_key.offset; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 2977 | dm->rmdir_ino = dir; |
| 2978 | ret = 0; |
| 2979 | goto out; |
| 2980 | } |
| 2981 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2982 | if (loc.objectid > send_progress) { |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2983 | odi = add_orphan_dir_info(sctx, dir); |
| 2984 | if (IS_ERR(odi)) { |
| 2985 | ret = PTR_ERR(odi); |
| 2986 | goto out; |
| 2987 | } |
| 2988 | odi->gen = dir_gen; |
| 2989 | odi->last_dir_index_offset = found_key.offset; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2990 | ret = 0; |
| 2991 | goto out; |
| 2992 | } |
| 2993 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 2994 | path->slots[0]++; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2995 | } |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 2996 | free_orphan_dir_info(sctx, odi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2997 | |
| 2998 | ret = 1; |
| 2999 | |
| 3000 | out: |
| 3001 | btrfs_free_path(path); |
| 3002 | return ret; |
| 3003 | } |
| 3004 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3005 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino) |
| 3006 | { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3007 | 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] | 3008 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3009 | return entry != NULL; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3012 | 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] | 3013 | { |
| 3014 | struct rb_node **p = &sctx->waiting_dir_moves.rb_node; |
| 3015 | struct rb_node *parent = NULL; |
| 3016 | struct waiting_dir_move *entry, *dm; |
| 3017 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 3018 | dm = kmalloc(sizeof(*dm), GFP_KERNEL); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3019 | if (!dm) |
| 3020 | return -ENOMEM; |
| 3021 | dm->ino = ino; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3022 | dm->rmdir_ino = 0; |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3023 | dm->orphanized = orphanized; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3024 | |
| 3025 | while (*p) { |
| 3026 | parent = *p; |
| 3027 | entry = rb_entry(parent, struct waiting_dir_move, node); |
| 3028 | if (ino < entry->ino) { |
| 3029 | p = &(*p)->rb_left; |
| 3030 | } else if (ino > entry->ino) { |
| 3031 | p = &(*p)->rb_right; |
| 3032 | } else { |
| 3033 | kfree(dm); |
| 3034 | return -EEXIST; |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | rb_link_node(&dm->node, parent, p); |
| 3039 | rb_insert_color(&dm->node, &sctx->waiting_dir_moves); |
| 3040 | return 0; |
| 3041 | } |
| 3042 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3043 | static struct waiting_dir_move * |
| 3044 | get_waiting_dir_move(struct send_ctx *sctx, u64 ino) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3045 | { |
| 3046 | struct rb_node *n = sctx->waiting_dir_moves.rb_node; |
| 3047 | struct waiting_dir_move *entry; |
| 3048 | |
| 3049 | while (n) { |
| 3050 | entry = rb_entry(n, struct waiting_dir_move, node); |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3051 | if (ino < entry->ino) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3052 | n = n->rb_left; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3053 | else if (ino > entry->ino) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3054 | n = n->rb_right; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3055 | else |
| 3056 | return entry; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3057 | } |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3058 | return NULL; |
| 3059 | } |
| 3060 | |
| 3061 | static void free_waiting_dir_move(struct send_ctx *sctx, |
| 3062 | struct waiting_dir_move *dm) |
| 3063 | { |
| 3064 | if (!dm) |
| 3065 | return; |
| 3066 | rb_erase(&dm->node, &sctx->waiting_dir_moves); |
| 3067 | kfree(dm); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3070 | static int add_pending_dir_move(struct send_ctx *sctx, |
| 3071 | u64 ino, |
| 3072 | u64 ino_gen, |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3073 | u64 parent_ino, |
| 3074 | struct list_head *new_refs, |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3075 | struct list_head *deleted_refs, |
| 3076 | const bool is_orphan) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3077 | { |
| 3078 | struct rb_node **p = &sctx->pending_dir_moves.rb_node; |
| 3079 | struct rb_node *parent = NULL; |
Chris Mason | 73b802f | 2014-03-21 15:30:44 -0700 | [diff] [blame] | 3080 | struct pending_dir_move *entry = NULL, *pm; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3081 | struct recorded_ref *cur; |
| 3082 | int exists = 0; |
| 3083 | int ret; |
| 3084 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 3085 | pm = kmalloc(sizeof(*pm), GFP_KERNEL); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3086 | if (!pm) |
| 3087 | return -ENOMEM; |
| 3088 | pm->parent_ino = parent_ino; |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3089 | pm->ino = ino; |
| 3090 | pm->gen = ino_gen; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3091 | INIT_LIST_HEAD(&pm->list); |
| 3092 | INIT_LIST_HEAD(&pm->update_refs); |
| 3093 | RB_CLEAR_NODE(&pm->node); |
| 3094 | |
| 3095 | while (*p) { |
| 3096 | parent = *p; |
| 3097 | entry = rb_entry(parent, struct pending_dir_move, node); |
| 3098 | if (parent_ino < entry->parent_ino) { |
| 3099 | p = &(*p)->rb_left; |
| 3100 | } else if (parent_ino > entry->parent_ino) { |
| 3101 | p = &(*p)->rb_right; |
| 3102 | } else { |
| 3103 | exists = 1; |
| 3104 | break; |
| 3105 | } |
| 3106 | } |
| 3107 | |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3108 | list_for_each_entry(cur, deleted_refs, list) { |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3109 | ret = dup_ref(cur, &pm->update_refs); |
| 3110 | if (ret < 0) |
| 3111 | goto out; |
| 3112 | } |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3113 | list_for_each_entry(cur, new_refs, list) { |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3114 | ret = dup_ref(cur, &pm->update_refs); |
| 3115 | if (ret < 0) |
| 3116 | goto out; |
| 3117 | } |
| 3118 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3119 | ret = add_waiting_dir_move(sctx, pm->ino, is_orphan); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3120 | if (ret) |
| 3121 | goto out; |
| 3122 | |
| 3123 | if (exists) { |
| 3124 | list_add_tail(&pm->list, &entry->list); |
| 3125 | } else { |
| 3126 | rb_link_node(&pm->node, parent, p); |
| 3127 | rb_insert_color(&pm->node, &sctx->pending_dir_moves); |
| 3128 | } |
| 3129 | ret = 0; |
| 3130 | out: |
| 3131 | if (ret) { |
| 3132 | __free_recorded_refs(&pm->update_refs); |
| 3133 | kfree(pm); |
| 3134 | } |
| 3135 | return ret; |
| 3136 | } |
| 3137 | |
| 3138 | static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx, |
| 3139 | u64 parent_ino) |
| 3140 | { |
| 3141 | struct rb_node *n = sctx->pending_dir_moves.rb_node; |
| 3142 | struct pending_dir_move *entry; |
| 3143 | |
| 3144 | while (n) { |
| 3145 | entry = rb_entry(n, struct pending_dir_move, node); |
| 3146 | if (parent_ino < entry->parent_ino) |
| 3147 | n = n->rb_left; |
| 3148 | else if (parent_ino > entry->parent_ino) |
| 3149 | n = n->rb_right; |
| 3150 | else |
| 3151 | return entry; |
| 3152 | } |
| 3153 | return NULL; |
| 3154 | } |
| 3155 | |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3156 | static int path_loop(struct send_ctx *sctx, struct fs_path *name, |
| 3157 | u64 ino, u64 gen, u64 *ancestor_ino) |
| 3158 | { |
| 3159 | int ret = 0; |
| 3160 | u64 parent_inode = 0; |
| 3161 | u64 parent_gen = 0; |
| 3162 | u64 start_ino = ino; |
| 3163 | |
| 3164 | *ancestor_ino = 0; |
| 3165 | while (ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 3166 | fs_path_reset(name); |
| 3167 | |
| 3168 | if (is_waiting_for_rm(sctx, ino)) |
| 3169 | break; |
| 3170 | if (is_waiting_for_move(sctx, ino)) { |
| 3171 | if (*ancestor_ino == 0) |
| 3172 | *ancestor_ino = ino; |
| 3173 | ret = get_first_ref(sctx->parent_root, ino, |
| 3174 | &parent_inode, &parent_gen, name); |
| 3175 | } else { |
| 3176 | ret = __get_cur_name_and_parent(sctx, ino, gen, |
| 3177 | &parent_inode, |
| 3178 | &parent_gen, name); |
| 3179 | if (ret > 0) { |
| 3180 | ret = 0; |
| 3181 | break; |
| 3182 | } |
| 3183 | } |
| 3184 | if (ret < 0) |
| 3185 | break; |
| 3186 | if (parent_inode == start_ino) { |
| 3187 | ret = 1; |
| 3188 | if (*ancestor_ino == 0) |
| 3189 | *ancestor_ino = ino; |
| 3190 | break; |
| 3191 | } |
| 3192 | ino = parent_inode; |
| 3193 | gen = parent_gen; |
| 3194 | } |
| 3195 | return ret; |
| 3196 | } |
| 3197 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3198 | static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm) |
| 3199 | { |
| 3200 | struct fs_path *from_path = NULL; |
| 3201 | struct fs_path *to_path = NULL; |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3202 | struct fs_path *name = NULL; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3203 | u64 orig_progress = sctx->send_progress; |
| 3204 | struct recorded_ref *cur; |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3205 | u64 parent_ino, parent_gen; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3206 | struct waiting_dir_move *dm = NULL; |
| 3207 | u64 rmdir_ino = 0; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3208 | u64 ancestor; |
| 3209 | bool is_orphan; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3210 | int ret; |
| 3211 | |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3212 | name = fs_path_alloc(); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3213 | from_path = fs_path_alloc(); |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3214 | if (!name || !from_path) { |
| 3215 | ret = -ENOMEM; |
| 3216 | goto out; |
| 3217 | } |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3218 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3219 | dm = get_waiting_dir_move(sctx, pm->ino); |
| 3220 | ASSERT(dm); |
| 3221 | rmdir_ino = dm->rmdir_ino; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3222 | is_orphan = dm->orphanized; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3223 | free_waiting_dir_move(sctx, dm); |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3224 | |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3225 | if (is_orphan) { |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3226 | ret = gen_unique_name(sctx, pm->ino, |
| 3227 | pm->gen, from_path); |
| 3228 | } else { |
| 3229 | ret = get_first_ref(sctx->parent_root, pm->ino, |
| 3230 | &parent_ino, &parent_gen, name); |
| 3231 | if (ret < 0) |
| 3232 | goto out; |
| 3233 | ret = get_cur_path(sctx, parent_ino, parent_gen, |
| 3234 | from_path); |
| 3235 | if (ret < 0) |
| 3236 | goto out; |
| 3237 | ret = fs_path_add_path(from_path, name); |
| 3238 | } |
Filipe Manana | c992ec9 | 2014-03-22 17:15:24 +0000 | [diff] [blame] | 3239 | if (ret < 0) |
| 3240 | goto out; |
| 3241 | |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3242 | sctx->send_progress = sctx->cur_ino + 1; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3243 | ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor); |
Filipe Manana | 7969e77 | 2016-06-17 17:13:36 +0100 | [diff] [blame] | 3244 | if (ret < 0) |
| 3245 | goto out; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3246 | if (ret) { |
| 3247 | LIST_HEAD(deleted_refs); |
| 3248 | ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID); |
| 3249 | ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor, |
| 3250 | &pm->update_refs, &deleted_refs, |
| 3251 | is_orphan); |
| 3252 | if (ret < 0) |
| 3253 | goto out; |
| 3254 | if (rmdir_ino) { |
| 3255 | dm = get_waiting_dir_move(sctx, pm->ino); |
| 3256 | ASSERT(dm); |
| 3257 | dm->rmdir_ino = rmdir_ino; |
| 3258 | } |
| 3259 | goto out; |
| 3260 | } |
Filipe Manana | c992ec9 | 2014-03-22 17:15:24 +0000 | [diff] [blame] | 3261 | fs_path_reset(name); |
| 3262 | to_path = name; |
| 3263 | name = NULL; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3264 | ret = get_cur_path(sctx, pm->ino, pm->gen, to_path); |
| 3265 | if (ret < 0) |
| 3266 | goto out; |
| 3267 | |
| 3268 | ret = send_rename(sctx, from_path, to_path); |
| 3269 | if (ret < 0) |
| 3270 | goto out; |
| 3271 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3272 | if (rmdir_ino) { |
| 3273 | struct orphan_dir_info *odi; |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 3274 | u64 gen; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3275 | |
| 3276 | odi = get_orphan_dir_info(sctx, rmdir_ino); |
| 3277 | if (!odi) { |
| 3278 | /* already deleted */ |
| 3279 | goto finish; |
| 3280 | } |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 3281 | gen = odi->gen; |
| 3282 | |
| 3283 | ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino); |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3284 | if (ret < 0) |
| 3285 | goto out; |
| 3286 | if (!ret) |
| 3287 | goto finish; |
| 3288 | |
| 3289 | name = fs_path_alloc(); |
| 3290 | if (!name) { |
| 3291 | ret = -ENOMEM; |
| 3292 | goto out; |
| 3293 | } |
Robbie Ko | 0f96f51 | 2018-05-08 18:11:38 +0800 | [diff] [blame] | 3294 | ret = get_cur_path(sctx, rmdir_ino, gen, name); |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3295 | if (ret < 0) |
| 3296 | goto out; |
| 3297 | ret = send_rmdir(sctx, name); |
| 3298 | if (ret < 0) |
| 3299 | goto out; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | finish: |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3303 | ret = send_utimes(sctx, pm->ino, pm->gen); |
| 3304 | if (ret < 0) |
| 3305 | goto out; |
| 3306 | |
| 3307 | /* |
| 3308 | * After rename/move, need to update the utimes of both new parent(s) |
| 3309 | * and old parent(s). |
| 3310 | */ |
| 3311 | list_for_each_entry(cur, &pm->update_refs, list) { |
Robbie Ko | 764433a | 2015-06-23 18:39:50 +0800 | [diff] [blame] | 3312 | /* |
| 3313 | * The parent inode might have been deleted in the send snapshot |
| 3314 | */ |
| 3315 | ret = get_inode_info(sctx->send_root, cur->dir, NULL, |
| 3316 | NULL, NULL, NULL, NULL, NULL); |
| 3317 | if (ret == -ENOENT) { |
| 3318 | ret = 0; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 3319 | continue; |
Robbie Ko | 764433a | 2015-06-23 18:39:50 +0800 | [diff] [blame] | 3320 | } |
| 3321 | if (ret < 0) |
| 3322 | goto out; |
| 3323 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3324 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); |
| 3325 | if (ret < 0) |
| 3326 | goto out; |
| 3327 | } |
| 3328 | |
| 3329 | out: |
Filipe Manana | 2b863a1 | 2014-02-16 13:43:11 +0000 | [diff] [blame] | 3330 | fs_path_free(name); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3331 | fs_path_free(from_path); |
| 3332 | fs_path_free(to_path); |
| 3333 | sctx->send_progress = orig_progress; |
| 3334 | |
| 3335 | return ret; |
| 3336 | } |
| 3337 | |
| 3338 | static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m) |
| 3339 | { |
| 3340 | if (!list_empty(&m->list)) |
| 3341 | list_del(&m->list); |
| 3342 | if (!RB_EMPTY_NODE(&m->node)) |
| 3343 | rb_erase(&m->node, &sctx->pending_dir_moves); |
| 3344 | __free_recorded_refs(&m->update_refs); |
| 3345 | kfree(m); |
| 3346 | } |
| 3347 | |
Robbie Ko | a4390ae | 2018-11-14 18:32:37 +0000 | [diff] [blame] | 3348 | static void tail_append_pending_moves(struct send_ctx *sctx, |
| 3349 | struct pending_dir_move *moves, |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3350 | struct list_head *stack) |
| 3351 | { |
| 3352 | if (list_empty(&moves->list)) { |
| 3353 | list_add_tail(&moves->list, stack); |
| 3354 | } else { |
| 3355 | LIST_HEAD(list); |
| 3356 | list_splice_init(&moves->list, &list); |
| 3357 | list_add_tail(&moves->list, stack); |
| 3358 | list_splice_tail(&list, stack); |
| 3359 | } |
Robbie Ko | a4390ae | 2018-11-14 18:32:37 +0000 | [diff] [blame] | 3360 | if (!RB_EMPTY_NODE(&moves->node)) { |
| 3361 | rb_erase(&moves->node, &sctx->pending_dir_moves); |
| 3362 | RB_CLEAR_NODE(&moves->node); |
| 3363 | } |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | static int apply_children_dir_moves(struct send_ctx *sctx) |
| 3367 | { |
| 3368 | struct pending_dir_move *pm; |
| 3369 | struct list_head stack; |
| 3370 | u64 parent_ino = sctx->cur_ino; |
| 3371 | int ret = 0; |
| 3372 | |
| 3373 | pm = get_pending_dir_moves(sctx, parent_ino); |
| 3374 | if (!pm) |
| 3375 | return 0; |
| 3376 | |
| 3377 | INIT_LIST_HEAD(&stack); |
Robbie Ko | a4390ae | 2018-11-14 18:32:37 +0000 | [diff] [blame] | 3378 | tail_append_pending_moves(sctx, pm, &stack); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3379 | |
| 3380 | while (!list_empty(&stack)) { |
| 3381 | pm = list_first_entry(&stack, struct pending_dir_move, list); |
| 3382 | parent_ino = pm->ino; |
| 3383 | ret = apply_dir_move(sctx, pm); |
| 3384 | free_pending_move(sctx, pm); |
| 3385 | if (ret) |
| 3386 | goto out; |
| 3387 | pm = get_pending_dir_moves(sctx, parent_ino); |
| 3388 | if (pm) |
Robbie Ko | a4390ae | 2018-11-14 18:32:37 +0000 | [diff] [blame] | 3389 | tail_append_pending_moves(sctx, pm, &stack); |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3390 | } |
| 3391 | return 0; |
| 3392 | |
| 3393 | out: |
| 3394 | while (!list_empty(&stack)) { |
| 3395 | pm = list_first_entry(&stack, struct pending_dir_move, list); |
| 3396 | free_pending_move(sctx, pm); |
| 3397 | } |
| 3398 | return ret; |
| 3399 | } |
| 3400 | |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3401 | /* |
| 3402 | * We might need to delay a directory rename even when no ancestor directory |
| 3403 | * (in the send root) with a higher inode number than ours (sctx->cur_ino) was |
| 3404 | * renamed. This happens when we rename a directory to the old name (the name |
| 3405 | * in the parent root) of some other unrelated directory that got its rename |
| 3406 | * delayed due to some ancestor with higher number that got renamed. |
| 3407 | * |
| 3408 | * Example: |
| 3409 | * |
| 3410 | * Parent snapshot: |
| 3411 | * . (ino 256) |
| 3412 | * |---- a/ (ino 257) |
| 3413 | * | |---- file (ino 260) |
| 3414 | * | |
| 3415 | * |---- b/ (ino 258) |
| 3416 | * |---- c/ (ino 259) |
| 3417 | * |
| 3418 | * Send snapshot: |
| 3419 | * . (ino 256) |
| 3420 | * |---- a/ (ino 258) |
| 3421 | * |---- x/ (ino 259) |
| 3422 | * |---- y/ (ino 257) |
| 3423 | * |----- file (ino 260) |
| 3424 | * |
| 3425 | * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257 |
| 3426 | * from 'a' to 'x/y' happening first, which in turn depends on the rename of |
| 3427 | * inode 259 from 'c' to 'x'. So the order of rename commands the send stream |
| 3428 | * must issue is: |
| 3429 | * |
| 3430 | * 1 - rename 259 from 'c' to 'x' |
| 3431 | * 2 - rename 257 from 'a' to 'x/y' |
| 3432 | * 3 - rename 258 from 'b' to 'a' |
| 3433 | * |
| 3434 | * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can |
| 3435 | * be done right away and < 0 on error. |
| 3436 | */ |
| 3437 | static int wait_for_dest_dir_move(struct send_ctx *sctx, |
| 3438 | struct recorded_ref *parent_ref, |
| 3439 | const bool is_orphan) |
| 3440 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3441 | struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info; |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3442 | struct btrfs_path *path; |
| 3443 | struct btrfs_key key; |
| 3444 | struct btrfs_key di_key; |
| 3445 | struct btrfs_dir_item *di; |
| 3446 | u64 left_gen; |
| 3447 | u64 right_gen; |
| 3448 | int ret = 0; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3449 | struct waiting_dir_move *wdm; |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3450 | |
| 3451 | if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) |
| 3452 | return 0; |
| 3453 | |
| 3454 | path = alloc_path_for_send(); |
| 3455 | if (!path) |
| 3456 | return -ENOMEM; |
| 3457 | |
| 3458 | key.objectid = parent_ref->dir; |
| 3459 | key.type = BTRFS_DIR_ITEM_KEY; |
| 3460 | key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len); |
| 3461 | |
| 3462 | ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); |
| 3463 | if (ret < 0) { |
| 3464 | goto out; |
| 3465 | } else if (ret > 0) { |
| 3466 | ret = 0; |
| 3467 | goto out; |
| 3468 | } |
| 3469 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3470 | di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name, |
| 3471 | parent_ref->name_len); |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3472 | if (!di) { |
| 3473 | ret = 0; |
| 3474 | goto out; |
| 3475 | } |
| 3476 | /* |
| 3477 | * di_key.objectid has the number of the inode that has a dentry in the |
| 3478 | * parent directory with the same name that sctx->cur_ino is being |
| 3479 | * renamed to. We need to check if that inode is in the send root as |
| 3480 | * well and if it is currently marked as an inode with a pending rename, |
| 3481 | * if it is, we need to delay the rename of sctx->cur_ino as well, so |
| 3482 | * that it happens after that other inode is renamed. |
| 3483 | */ |
| 3484 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key); |
| 3485 | if (di_key.type != BTRFS_INODE_ITEM_KEY) { |
| 3486 | ret = 0; |
| 3487 | goto out; |
| 3488 | } |
| 3489 | |
| 3490 | ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL, |
| 3491 | &left_gen, NULL, NULL, NULL, NULL); |
| 3492 | if (ret < 0) |
| 3493 | goto out; |
| 3494 | ret = get_inode_info(sctx->send_root, di_key.objectid, NULL, |
| 3495 | &right_gen, NULL, NULL, NULL, NULL); |
| 3496 | if (ret < 0) { |
| 3497 | if (ret == -ENOENT) |
| 3498 | ret = 0; |
| 3499 | goto out; |
| 3500 | } |
| 3501 | |
| 3502 | /* Different inode, no need to delay the rename of sctx->cur_ino */ |
| 3503 | if (right_gen != left_gen) { |
| 3504 | ret = 0; |
| 3505 | goto out; |
| 3506 | } |
| 3507 | |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3508 | wdm = get_waiting_dir_move(sctx, di_key.objectid); |
| 3509 | if (wdm && !wdm->orphanized) { |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3510 | ret = add_pending_dir_move(sctx, |
| 3511 | sctx->cur_ino, |
| 3512 | sctx->cur_inode_gen, |
| 3513 | di_key.objectid, |
| 3514 | &sctx->new_refs, |
| 3515 | &sctx->deleted_refs, |
| 3516 | is_orphan); |
| 3517 | if (!ret) |
| 3518 | ret = 1; |
| 3519 | } |
| 3520 | out: |
| 3521 | btrfs_free_path(path); |
| 3522 | return ret; |
| 3523 | } |
| 3524 | |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3525 | /* |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3526 | * Check if inode ino2, or any of its ancestors, is inode ino1. |
| 3527 | * Return 1 if true, 0 if false and < 0 on error. |
| 3528 | */ |
| 3529 | static int check_ino_in_path(struct btrfs_root *root, |
| 3530 | const u64 ino1, |
| 3531 | const u64 ino1_gen, |
| 3532 | const u64 ino2, |
| 3533 | const u64 ino2_gen, |
| 3534 | struct fs_path *fs_path) |
| 3535 | { |
| 3536 | u64 ino = ino2; |
| 3537 | |
| 3538 | if (ino1 == ino2) |
| 3539 | return ino1_gen == ino2_gen; |
| 3540 | |
| 3541 | while (ino > BTRFS_FIRST_FREE_OBJECTID) { |
| 3542 | u64 parent; |
| 3543 | u64 parent_gen; |
| 3544 | int ret; |
| 3545 | |
| 3546 | fs_path_reset(fs_path); |
| 3547 | ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path); |
| 3548 | if (ret < 0) |
| 3549 | return ret; |
| 3550 | if (parent == ino1) |
| 3551 | return parent_gen == ino1_gen; |
| 3552 | ino = parent; |
| 3553 | } |
| 3554 | return 0; |
| 3555 | } |
| 3556 | |
| 3557 | /* |
| 3558 | * Check if ino ino1 is an ancestor of inode ino2 in the given root for any |
| 3559 | * 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] | 3560 | * Return 1 if true, 0 if false and < 0 on error. |
| 3561 | */ |
| 3562 | static int is_ancestor(struct btrfs_root *root, |
| 3563 | const u64 ino1, |
| 3564 | const u64 ino1_gen, |
| 3565 | const u64 ino2, |
| 3566 | struct fs_path *fs_path) |
| 3567 | { |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3568 | bool free_fs_path = false; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3569 | int ret = 0; |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3570 | struct btrfs_path *path = NULL; |
| 3571 | struct btrfs_key key; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3572 | |
| 3573 | if (!fs_path) { |
| 3574 | fs_path = fs_path_alloc(); |
| 3575 | if (!fs_path) |
| 3576 | return -ENOMEM; |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3577 | free_fs_path = true; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3578 | } |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3579 | |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3580 | path = alloc_path_for_send(); |
| 3581 | if (!path) { |
| 3582 | ret = -ENOMEM; |
| 3583 | goto out; |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3584 | } |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3585 | |
| 3586 | key.objectid = ino2; |
| 3587 | key.type = BTRFS_INODE_REF_KEY; |
| 3588 | key.offset = 0; |
| 3589 | |
| 3590 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3591 | if (ret < 0) |
| 3592 | goto out; |
| 3593 | |
| 3594 | while (true) { |
| 3595 | struct extent_buffer *leaf = path->nodes[0]; |
| 3596 | int slot = path->slots[0]; |
| 3597 | u32 cur_offset = 0; |
| 3598 | u32 item_size; |
| 3599 | |
| 3600 | if (slot >= btrfs_header_nritems(leaf)) { |
| 3601 | ret = btrfs_next_leaf(root, path); |
| 3602 | if (ret < 0) |
| 3603 | goto out; |
| 3604 | if (ret > 0) |
| 3605 | break; |
| 3606 | continue; |
| 3607 | } |
| 3608 | |
| 3609 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 3610 | if (key.objectid != ino2) |
| 3611 | break; |
| 3612 | if (key.type != BTRFS_INODE_REF_KEY && |
| 3613 | key.type != BTRFS_INODE_EXTREF_KEY) |
| 3614 | break; |
| 3615 | |
| 3616 | item_size = btrfs_item_size_nr(leaf, slot); |
| 3617 | while (cur_offset < item_size) { |
| 3618 | u64 parent; |
| 3619 | u64 parent_gen; |
| 3620 | |
| 3621 | if (key.type == BTRFS_INODE_EXTREF_KEY) { |
| 3622 | unsigned long ptr; |
| 3623 | struct btrfs_inode_extref *extref; |
| 3624 | |
| 3625 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 3626 | extref = (struct btrfs_inode_extref *) |
| 3627 | (ptr + cur_offset); |
| 3628 | parent = btrfs_inode_extref_parent(leaf, |
| 3629 | extref); |
| 3630 | cur_offset += sizeof(*extref); |
| 3631 | cur_offset += btrfs_inode_extref_name_len(leaf, |
| 3632 | extref); |
| 3633 | } else { |
| 3634 | parent = key.offset; |
| 3635 | cur_offset = item_size; |
| 3636 | } |
| 3637 | |
| 3638 | ret = get_inode_info(root, parent, NULL, &parent_gen, |
| 3639 | NULL, NULL, NULL, NULL); |
| 3640 | if (ret < 0) |
| 3641 | goto out; |
| 3642 | ret = check_ino_in_path(root, ino1, ino1_gen, |
| 3643 | parent, parent_gen, fs_path); |
| 3644 | if (ret) |
| 3645 | goto out; |
| 3646 | } |
| 3647 | path->slots[0]++; |
| 3648 | } |
| 3649 | ret = 0; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3650 | out: |
Filipe Manana | ea37d59 | 2017-11-17 01:54:00 +0000 | [diff] [blame] | 3651 | btrfs_free_path(path); |
| 3652 | if (free_fs_path) |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3653 | fs_path_free(fs_path); |
| 3654 | return ret; |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3657 | static int wait_for_parent_move(struct send_ctx *sctx, |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3658 | struct recorded_ref *parent_ref, |
| 3659 | const bool is_orphan) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3660 | { |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3661 | int ret = 0; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3662 | u64 ino = parent_ref->dir; |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3663 | u64 ino_gen = parent_ref->dir_gen; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3664 | u64 parent_ino_before, parent_ino_after; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3665 | struct fs_path *path_before = NULL; |
| 3666 | struct fs_path *path_after = NULL; |
| 3667 | int len1, len2; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3668 | |
| 3669 | path_after = fs_path_alloc(); |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3670 | path_before = fs_path_alloc(); |
| 3671 | if (!path_after || !path_before) { |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3672 | ret = -ENOMEM; |
| 3673 | goto out; |
| 3674 | } |
| 3675 | |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3676 | /* |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3677 | * Our current directory inode may not yet be renamed/moved because some |
| 3678 | * ancestor (immediate or not) has to be renamed/moved first. So find if |
| 3679 | * such ancestor exists and make sure our own rename/move happens after |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3680 | * that ancestor is processed to avoid path build infinite loops (done |
| 3681 | * at get_cur_path()). |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3682 | */ |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3683 | while (ino > BTRFS_FIRST_FREE_OBJECTID) { |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3684 | u64 parent_ino_after_gen; |
| 3685 | |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3686 | if (is_waiting_for_move(sctx, ino)) { |
Filipe Manana | 80aa602 | 2015-03-27 17:50:45 +0000 | [diff] [blame] | 3687 | /* |
| 3688 | * If the current inode is an ancestor of ino in the |
| 3689 | * parent root, we need to delay the rename of the |
| 3690 | * current inode, otherwise don't delayed the rename |
| 3691 | * because we can end up with a circular dependency |
| 3692 | * of renames, resulting in some directories never |
| 3693 | * getting the respective rename operations issued in |
| 3694 | * the send stream or getting into infinite path build |
| 3695 | * loops. |
| 3696 | */ |
| 3697 | ret = is_ancestor(sctx->parent_root, |
| 3698 | sctx->cur_ino, sctx->cur_inode_gen, |
| 3699 | ino, path_before); |
Filipe Manana | 4122ea6 | 2016-06-27 16:54:44 +0100 | [diff] [blame] | 3700 | if (ret) |
| 3701 | break; |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3702 | } |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3703 | |
| 3704 | fs_path_reset(path_before); |
| 3705 | fs_path_reset(path_after); |
| 3706 | |
| 3707 | ret = get_first_ref(sctx->send_root, ino, &parent_ino_after, |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3708 | &parent_ino_after_gen, path_after); |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3709 | if (ret < 0) |
| 3710 | goto out; |
| 3711 | ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before, |
| 3712 | NULL, path_before); |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3713 | if (ret < 0 && ret != -ENOENT) { |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3714 | goto out; |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3715 | } else if (ret == -ENOENT) { |
Filipe Manana | bf8e8ca | 2014-10-02 19:17:32 +0100 | [diff] [blame] | 3716 | ret = 0; |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3717 | break; |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | len1 = fs_path_len(path_before); |
| 3721 | len2 = fs_path_len(path_after); |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3722 | if (ino > sctx->cur_ino && |
| 3723 | (parent_ino_before != parent_ino_after || len1 != len2 || |
| 3724 | memcmp(path_before->start, path_after->start, len1))) { |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3725 | u64 parent_ino_gen; |
| 3726 | |
| 3727 | ret = get_inode_info(sctx->parent_root, ino, NULL, |
| 3728 | &parent_ino_gen, NULL, NULL, NULL, |
| 3729 | NULL); |
| 3730 | if (ret < 0) |
| 3731 | goto out; |
| 3732 | if (ino_gen == parent_ino_gen) { |
| 3733 | ret = 1; |
| 3734 | break; |
| 3735 | } |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3736 | } |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3737 | ino = parent_ino_after; |
Filipe Manana | fe9c798 | 2017-01-11 02:15:39 +0000 | [diff] [blame] | 3738 | ino_gen = parent_ino_after_gen; |
Filipe Manana | bfa7e1f | 2014-03-19 14:20:54 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3741 | out: |
| 3742 | fs_path_free(path_before); |
| 3743 | fs_path_free(path_after); |
| 3744 | |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3745 | if (ret == 1) { |
| 3746 | ret = add_pending_dir_move(sctx, |
| 3747 | sctx->cur_ino, |
| 3748 | sctx->cur_inode_gen, |
| 3749 | ino, |
| 3750 | &sctx->new_refs, |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3751 | &sctx->deleted_refs, |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3752 | is_orphan); |
Filipe Manana | f959492 | 2014-03-27 20:14:01 +0000 | [diff] [blame] | 3753 | if (!ret) |
| 3754 | ret = 1; |
| 3755 | } |
| 3756 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3757 | return ret; |
| 3758 | } |
| 3759 | |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3760 | static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) |
| 3761 | { |
| 3762 | int ret; |
| 3763 | struct fs_path *new_path; |
| 3764 | |
| 3765 | /* |
| 3766 | * Our reference's name member points to its full_path member string, so |
| 3767 | * we use here a new path. |
| 3768 | */ |
| 3769 | new_path = fs_path_alloc(); |
| 3770 | if (!new_path) |
| 3771 | return -ENOMEM; |
| 3772 | |
| 3773 | ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path); |
| 3774 | if (ret < 0) { |
| 3775 | fs_path_free(new_path); |
| 3776 | return ret; |
| 3777 | } |
| 3778 | ret = fs_path_add(new_path, ref->name, ref->name_len); |
| 3779 | if (ret < 0) { |
| 3780 | fs_path_free(new_path); |
| 3781 | return ret; |
| 3782 | } |
| 3783 | |
| 3784 | fs_path_free(ref->full_path); |
| 3785 | set_ref_path(ref, new_path); |
| 3786 | |
| 3787 | return 0; |
| 3788 | } |
| 3789 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3790 | /* |
| 3791 | * This does all the move/link/unlink/rmdir magic. |
| 3792 | */ |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3793 | static int process_recorded_refs(struct send_ctx *sctx, int *pending_move) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3794 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 3795 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3796 | int ret = 0; |
| 3797 | struct recorded_ref *cur; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3798 | struct recorded_ref *cur2; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3799 | struct list_head check_dirs; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3800 | struct fs_path *valid_path = NULL; |
Chris Mason | b24baf6 | 2012-07-25 19:21:10 -0400 | [diff] [blame] | 3801 | u64 ow_inode = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3802 | u64 ow_gen; |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3803 | u64 ow_mode; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3804 | int did_overwrite = 0; |
| 3805 | int is_orphan = 0; |
Filipe Manana | 29d6d30 | 2014-02-16 21:01:39 +0000 | [diff] [blame] | 3806 | u64 last_dir_ino_rm = 0; |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3807 | bool can_rename = true; |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3808 | bool orphanized_dir = false; |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 3809 | bool orphanized_ancestor = false; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3810 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 3811 | btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3812 | |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 3813 | /* |
| 3814 | * This should never happen as the root dir always has the same ref |
| 3815 | * which is always '..' |
| 3816 | */ |
| 3817 | BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3818 | INIT_LIST_HEAD(&check_dirs); |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 3819 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3820 | valid_path = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3821 | if (!valid_path) { |
| 3822 | ret = -ENOMEM; |
| 3823 | goto out; |
| 3824 | } |
| 3825 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3826 | /* |
| 3827 | * First, check if the first ref of the current inode was overwritten |
| 3828 | * before. If yes, we know that the current inode was already orphanized |
| 3829 | * and thus use the orphan name. If not, we can use get_cur_path to |
| 3830 | * get the path of the first ref as it would like while receiving at |
| 3831 | * this point in time. |
| 3832 | * New inodes are always orphan at the beginning, so force to use the |
| 3833 | * orphan name in this case. |
| 3834 | * The first ref is stored in valid_path and will be updated if it |
| 3835 | * gets moved around. |
| 3836 | */ |
| 3837 | if (!sctx->cur_inode_new) { |
| 3838 | ret = did_overwrite_first_ref(sctx, sctx->cur_ino, |
| 3839 | sctx->cur_inode_gen); |
| 3840 | if (ret < 0) |
| 3841 | goto out; |
| 3842 | if (ret) |
| 3843 | did_overwrite = 1; |
| 3844 | } |
| 3845 | if (sctx->cur_inode_new || did_overwrite) { |
| 3846 | ret = gen_unique_name(sctx, sctx->cur_ino, |
| 3847 | sctx->cur_inode_gen, valid_path); |
| 3848 | if (ret < 0) |
| 3849 | goto out; |
| 3850 | is_orphan = 1; |
| 3851 | } else { |
| 3852 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 3853 | valid_path); |
| 3854 | if (ret < 0) |
| 3855 | goto out; |
| 3856 | } |
| 3857 | |
| 3858 | list_for_each_entry(cur, &sctx->new_refs, list) { |
| 3859 | /* |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3860 | * We may have refs where the parent directory does not exist |
| 3861 | * yet. This happens if the parent directories inum is higher |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 3862 | * than the current inum. To handle this case, we create the |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3863 | * parent directory out of order. But we need to check if this |
| 3864 | * did already happen before due to other refs in the same dir. |
| 3865 | */ |
| 3866 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
| 3867 | if (ret < 0) |
| 3868 | goto out; |
| 3869 | if (ret == inode_state_will_create) { |
| 3870 | ret = 0; |
| 3871 | /* |
| 3872 | * First check if any of the current inodes refs did |
| 3873 | * already create the dir. |
| 3874 | */ |
| 3875 | list_for_each_entry(cur2, &sctx->new_refs, list) { |
| 3876 | if (cur == cur2) |
| 3877 | break; |
| 3878 | if (cur2->dir == cur->dir) { |
| 3879 | ret = 1; |
| 3880 | break; |
| 3881 | } |
| 3882 | } |
| 3883 | |
| 3884 | /* |
| 3885 | * If that did not happen, check if a previous inode |
| 3886 | * did already create the dir. |
| 3887 | */ |
| 3888 | if (!ret) |
| 3889 | ret = did_create_dir(sctx, cur->dir); |
| 3890 | if (ret < 0) |
| 3891 | goto out; |
| 3892 | if (!ret) { |
| 3893 | ret = send_create_inode(sctx, cur->dir); |
| 3894 | if (ret < 0) |
| 3895 | goto out; |
| 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3900 | * Check if this new ref would overwrite the first ref of |
| 3901 | * another unprocessed inode. If yes, orphanize the |
| 3902 | * overwritten inode. If we find an overwritten ref that is |
| 3903 | * not the first ref, simply unlink it. |
| 3904 | */ |
| 3905 | ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, |
| 3906 | cur->name, cur->name_len, |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3907 | &ow_inode, &ow_gen, &ow_mode); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3908 | if (ret < 0) |
| 3909 | goto out; |
| 3910 | if (ret) { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3911 | ret = is_first_ref(sctx->parent_root, |
| 3912 | ow_inode, cur->dir, cur->name, |
| 3913 | cur->name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3914 | if (ret < 0) |
| 3915 | goto out; |
| 3916 | if (ret) { |
Filipe Manana | 8996a48 | 2015-03-12 15:16:20 +0000 | [diff] [blame] | 3917 | struct name_cache_entry *nce; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3918 | struct waiting_dir_move *wdm; |
Filipe Manana | 8996a48 | 2015-03-12 15:16:20 +0000 | [diff] [blame] | 3919 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3920 | ret = orphanize_inode(sctx, ow_inode, ow_gen, |
| 3921 | cur->full_path); |
| 3922 | if (ret < 0) |
| 3923 | goto out; |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 3924 | if (S_ISDIR(ow_mode)) |
| 3925 | orphanized_dir = true; |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3926 | |
| 3927 | /* |
| 3928 | * If ow_inode has its rename operation delayed |
| 3929 | * make sure that its orphanized name is used in |
| 3930 | * the source path when performing its rename |
| 3931 | * operation. |
| 3932 | */ |
| 3933 | if (is_waiting_for_move(sctx, ow_inode)) { |
| 3934 | wdm = get_waiting_dir_move(sctx, |
| 3935 | ow_inode); |
| 3936 | ASSERT(wdm); |
| 3937 | wdm->orphanized = true; |
| 3938 | } |
| 3939 | |
Filipe Manana | 8996a48 | 2015-03-12 15:16:20 +0000 | [diff] [blame] | 3940 | /* |
| 3941 | * Make sure we clear our orphanized inode's |
| 3942 | * name from the name cache. This is because the |
| 3943 | * inode ow_inode might be an ancestor of some |
| 3944 | * other inode that will be orphanized as well |
| 3945 | * later and has an inode number greater than |
| 3946 | * sctx->send_progress. We need to prevent |
| 3947 | * future name lookups from using the old name |
| 3948 | * and get instead the orphan name. |
| 3949 | */ |
| 3950 | nce = name_cache_search(sctx, ow_inode, ow_gen); |
| 3951 | if (nce) { |
| 3952 | name_cache_delete(sctx, nce); |
| 3953 | kfree(nce); |
| 3954 | } |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3955 | |
| 3956 | /* |
| 3957 | * ow_inode might currently be an ancestor of |
| 3958 | * cur_ino, therefore compute valid_path (the |
| 3959 | * current path of cur_ino) again because it |
| 3960 | * might contain the pre-orphanization name of |
| 3961 | * ow_inode, which is no longer valid. |
| 3962 | */ |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3963 | ret = is_ancestor(sctx->parent_root, |
| 3964 | ow_inode, ow_gen, |
| 3965 | sctx->cur_ino, NULL); |
| 3966 | if (ret > 0) { |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 3967 | orphanized_ancestor = true; |
Filipe Manana | 72c3668 | 2017-06-07 11:41:29 +0100 | [diff] [blame] | 3968 | fs_path_reset(valid_path); |
| 3969 | ret = get_cur_path(sctx, sctx->cur_ino, |
| 3970 | sctx->cur_inode_gen, |
| 3971 | valid_path); |
| 3972 | } |
Robbie Ko | 801bec3 | 2015-06-23 18:39:46 +0800 | [diff] [blame] | 3973 | if (ret < 0) |
| 3974 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3975 | } else { |
| 3976 | ret = send_unlink(sctx, cur->full_path); |
| 3977 | if (ret < 0) |
| 3978 | goto out; |
| 3979 | } |
| 3980 | } |
| 3981 | |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 3982 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) { |
| 3983 | ret = wait_for_dest_dir_move(sctx, cur, is_orphan); |
| 3984 | if (ret < 0) |
| 3985 | goto out; |
| 3986 | if (ret == 1) { |
| 3987 | can_rename = false; |
| 3988 | *pending_move = 1; |
| 3989 | } |
| 3990 | } |
| 3991 | |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 3992 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root && |
| 3993 | can_rename) { |
| 3994 | ret = wait_for_parent_move(sctx, cur, is_orphan); |
| 3995 | if (ret < 0) |
| 3996 | goto out; |
| 3997 | if (ret == 1) { |
| 3998 | can_rename = false; |
| 3999 | *pending_move = 1; |
| 4000 | } |
| 4001 | } |
| 4002 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4003 | /* |
| 4004 | * link/move the ref to the new place. If we have an orphan |
| 4005 | * inode, move it and update valid_path. If not, link or move |
| 4006 | * it depending on the inode mode. |
| 4007 | */ |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 4008 | if (is_orphan && can_rename) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4009 | ret = send_rename(sctx, valid_path, cur->full_path); |
| 4010 | if (ret < 0) |
| 4011 | goto out; |
| 4012 | is_orphan = 0; |
| 4013 | ret = fs_path_copy(valid_path, cur->full_path); |
| 4014 | if (ret < 0) |
| 4015 | goto out; |
Filipe Manana | 84471e2 | 2015-02-28 22:29:22 +0000 | [diff] [blame] | 4016 | } else if (can_rename) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4017 | if (S_ISDIR(sctx->cur_inode_mode)) { |
| 4018 | /* |
| 4019 | * Dirs can't be linked, so move it. For moved |
| 4020 | * dirs, we always have one new and one deleted |
| 4021 | * ref. The deleted ref is ignored later. |
| 4022 | */ |
Filipe Manana | 8b191a6 | 2015-04-09 14:09:14 +0100 | [diff] [blame] | 4023 | ret = send_rename(sctx, valid_path, |
| 4024 | cur->full_path); |
| 4025 | if (!ret) |
| 4026 | ret = fs_path_copy(valid_path, |
| 4027 | cur->full_path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4028 | if (ret < 0) |
| 4029 | goto out; |
| 4030 | } else { |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 4031 | /* |
| 4032 | * We might have previously orphanized an inode |
| 4033 | * which is an ancestor of our current inode, |
| 4034 | * so our reference's full path, which was |
| 4035 | * computed before any such orphanizations, must |
| 4036 | * be updated. |
| 4037 | */ |
| 4038 | if (orphanized_dir) { |
| 4039 | ret = update_ref_path(sctx, cur); |
| 4040 | if (ret < 0) |
| 4041 | goto out; |
| 4042 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4043 | ret = send_link(sctx, cur->full_path, |
| 4044 | valid_path); |
| 4045 | if (ret < 0) |
| 4046 | goto out; |
| 4047 | } |
| 4048 | } |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4049 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4050 | if (ret < 0) |
| 4051 | goto out; |
| 4052 | } |
| 4053 | |
| 4054 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { |
| 4055 | /* |
| 4056 | * Check if we can already rmdir the directory. If not, |
| 4057 | * orphanize it. For every dir item inside that gets deleted |
| 4058 | * later, we do this check again and rmdir it then if possible. |
| 4059 | * See the use of check_dirs for more details. |
| 4060 | */ |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 4061 | ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 4062 | sctx->cur_ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4063 | if (ret < 0) |
| 4064 | goto out; |
| 4065 | if (ret) { |
| 4066 | ret = send_rmdir(sctx, valid_path); |
| 4067 | if (ret < 0) |
| 4068 | goto out; |
| 4069 | } else if (!is_orphan) { |
| 4070 | ret = orphanize_inode(sctx, sctx->cur_ino, |
| 4071 | sctx->cur_inode_gen, valid_path); |
| 4072 | if (ret < 0) |
| 4073 | goto out; |
| 4074 | is_orphan = 1; |
| 4075 | } |
| 4076 | |
| 4077 | list_for_each_entry(cur, &sctx->deleted_refs, list) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4078 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4079 | if (ret < 0) |
| 4080 | goto out; |
| 4081 | } |
Alexander Block | ccf1626 | 2012-07-28 11:46:29 +0200 | [diff] [blame] | 4082 | } else if (S_ISDIR(sctx->cur_inode_mode) && |
| 4083 | !list_empty(&sctx->deleted_refs)) { |
| 4084 | /* |
| 4085 | * We have a moved dir. Add the old parent to check_dirs |
| 4086 | */ |
| 4087 | cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, |
| 4088 | list); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4089 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | ccf1626 | 2012-07-28 11:46:29 +0200 | [diff] [blame] | 4090 | if (ret < 0) |
| 4091 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4092 | } else if (!S_ISDIR(sctx->cur_inode_mode)) { |
| 4093 | /* |
| 4094 | * We have a non dir inode. Go through all deleted refs and |
| 4095 | * unlink them if they were not already overwritten by other |
| 4096 | * inodes. |
| 4097 | */ |
| 4098 | list_for_each_entry(cur, &sctx->deleted_refs, list) { |
| 4099 | ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, |
| 4100 | sctx->cur_ino, sctx->cur_inode_gen, |
| 4101 | cur->name, cur->name_len); |
| 4102 | if (ret < 0) |
| 4103 | goto out; |
| 4104 | if (!ret) { |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 4105 | /* |
| 4106 | * If we orphanized any ancestor before, we need |
| 4107 | * to recompute the full path for deleted names, |
| 4108 | * since any such path was computed before we |
| 4109 | * processed any references and orphanized any |
| 4110 | * ancestor inode. |
| 4111 | */ |
| 4112 | if (orphanized_ancestor) { |
Filipe Manana | f596278 | 2017-06-22 20:03:51 +0100 | [diff] [blame] | 4113 | ret = update_ref_path(sctx, cur); |
| 4114 | if (ret < 0) |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 4115 | goto out; |
Filipe Manana | fdb1388 | 2017-06-13 14:13:11 +0100 | [diff] [blame] | 4116 | } |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 4117 | ret = send_unlink(sctx, cur->full_path); |
| 4118 | if (ret < 0) |
| 4119 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4120 | } |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4121 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4122 | if (ret < 0) |
| 4123 | goto out; |
| 4124 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4125 | /* |
| 4126 | * If the inode is still orphan, unlink the orphan. This may |
| 4127 | * happen when a previous inode did overwrite the first ref |
| 4128 | * of this inode and no new refs were added for the current |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4129 | * inode. Unlinking does not mean that the inode is deleted in |
| 4130 | * all cases. There may still be links to this inode in other |
| 4131 | * places. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4132 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 4133 | if (is_orphan) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4134 | ret = send_unlink(sctx, valid_path); |
| 4135 | if (ret < 0) |
| 4136 | goto out; |
| 4137 | } |
| 4138 | } |
| 4139 | |
| 4140 | /* |
| 4141 | * We did collect all parent dirs where cur_inode was once located. We |
| 4142 | * now go through all these dirs and check if they are pending for |
| 4143 | * deletion and if it's finally possible to perform the rmdir now. |
| 4144 | * We also update the inode stats of the parent dirs here. |
| 4145 | */ |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4146 | list_for_each_entry(cur, &check_dirs, list) { |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4147 | /* |
| 4148 | * In case we had refs into dirs that were not processed yet, |
| 4149 | * we don't need to do the utime and rmdir logic for these dirs. |
| 4150 | * The dir will be processed later. |
| 4151 | */ |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4152 | if (cur->dir > sctx->cur_ino) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4153 | continue; |
| 4154 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4155 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4156 | if (ret < 0) |
| 4157 | goto out; |
| 4158 | |
| 4159 | if (ret == inode_state_did_create || |
| 4160 | ret == inode_state_no_change) { |
| 4161 | /* TODO delayed utimes */ |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4162 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4163 | if (ret < 0) |
| 4164 | goto out; |
Filipe Manana | 29d6d30 | 2014-02-16 21:01:39 +0000 | [diff] [blame] | 4165 | } else if (ret == inode_state_did_delete && |
| 4166 | cur->dir != last_dir_ino_rm) { |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 4167 | ret = can_rmdir(sctx, cur->dir, cur->dir_gen, |
| 4168 | sctx->cur_ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4169 | if (ret < 0) |
| 4170 | goto out; |
| 4171 | if (ret) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4172 | ret = get_cur_path(sctx, cur->dir, |
| 4173 | cur->dir_gen, valid_path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4174 | if (ret < 0) |
| 4175 | goto out; |
| 4176 | ret = send_rmdir(sctx, valid_path); |
| 4177 | if (ret < 0) |
| 4178 | goto out; |
Filipe Manana | 29d6d30 | 2014-02-16 21:01:39 +0000 | [diff] [blame] | 4179 | last_dir_ino_rm = cur->dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4180 | } |
| 4181 | } |
| 4182 | } |
| 4183 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4184 | ret = 0; |
| 4185 | |
| 4186 | out: |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4187 | __free_recorded_refs(&check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4188 | free_recorded_refs(sctx); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4189 | fs_path_free(valid_path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4190 | return ret; |
| 4191 | } |
| 4192 | |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4193 | static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name, |
| 4194 | void *ctx, struct list_head *refs) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4195 | { |
| 4196 | int ret = 0; |
| 4197 | struct send_ctx *sctx = ctx; |
| 4198 | struct fs_path *p; |
| 4199 | u64 gen; |
| 4200 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4201 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4202 | if (!p) |
| 4203 | return -ENOMEM; |
| 4204 | |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 4205 | ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 4206 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4207 | if (ret < 0) |
| 4208 | goto out; |
| 4209 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4210 | ret = get_cur_path(sctx, dir, gen, p); |
| 4211 | if (ret < 0) |
| 4212 | goto out; |
| 4213 | ret = fs_path_add_path(p, name); |
| 4214 | if (ret < 0) |
| 4215 | goto out; |
| 4216 | |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 4217 | ret = __record_ref(refs, dir, gen, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4218 | |
| 4219 | out: |
| 4220 | if (ret) |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4221 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4222 | return ret; |
| 4223 | } |
| 4224 | |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 4225 | static int __record_new_ref(int num, u64 dir, int index, |
| 4226 | struct fs_path *name, |
| 4227 | void *ctx) |
| 4228 | { |
| 4229 | struct send_ctx *sctx = ctx; |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4230 | return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs); |
Liu Bo | a4d96d6 | 2014-03-03 21:31:03 +0800 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4234 | static int __record_deleted_ref(int num, u64 dir, int index, |
| 4235 | struct fs_path *name, |
| 4236 | void *ctx) |
| 4237 | { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4238 | struct send_ctx *sctx = ctx; |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4239 | return record_ref(sctx->parent_root, dir, name, ctx, |
| 4240 | &sctx->deleted_refs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | static int record_new_ref(struct send_ctx *sctx) |
| 4244 | { |
| 4245 | int ret; |
| 4246 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4247 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
| 4248 | sctx->cmp_key, 0, __record_new_ref, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4249 | if (ret < 0) |
| 4250 | goto out; |
| 4251 | ret = 0; |
| 4252 | |
| 4253 | out: |
| 4254 | return ret; |
| 4255 | } |
| 4256 | |
| 4257 | static int record_deleted_ref(struct send_ctx *sctx) |
| 4258 | { |
| 4259 | int ret; |
| 4260 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4261 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
| 4262 | sctx->cmp_key, 0, __record_deleted_ref, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4263 | if (ret < 0) |
| 4264 | goto out; |
| 4265 | ret = 0; |
| 4266 | |
| 4267 | out: |
| 4268 | return ret; |
| 4269 | } |
| 4270 | |
| 4271 | struct find_ref_ctx { |
| 4272 | u64 dir; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4273 | u64 dir_gen; |
| 4274 | struct btrfs_root *root; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4275 | struct fs_path *name; |
| 4276 | int found_idx; |
| 4277 | }; |
| 4278 | |
| 4279 | static int __find_iref(int num, u64 dir, int index, |
| 4280 | struct fs_path *name, |
| 4281 | void *ctx_) |
| 4282 | { |
| 4283 | struct find_ref_ctx *ctx = ctx_; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4284 | u64 dir_gen; |
| 4285 | int ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4286 | |
| 4287 | if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) && |
| 4288 | strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4289 | /* |
| 4290 | * To avoid doing extra lookups we'll only do this if everything |
| 4291 | * else matches. |
| 4292 | */ |
| 4293 | ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL, |
| 4294 | NULL, NULL, NULL); |
| 4295 | if (ret) |
| 4296 | return ret; |
| 4297 | if (dir_gen != ctx->dir_gen) |
| 4298 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4299 | ctx->found_idx = num; |
| 4300 | return 1; |
| 4301 | } |
| 4302 | return 0; |
| 4303 | } |
| 4304 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4305 | static int find_iref(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4306 | struct btrfs_path *path, |
| 4307 | struct btrfs_key *key, |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4308 | u64 dir, u64 dir_gen, struct fs_path *name) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4309 | { |
| 4310 | int ret; |
| 4311 | struct find_ref_ctx ctx; |
| 4312 | |
| 4313 | ctx.dir = dir; |
| 4314 | ctx.name = name; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4315 | ctx.dir_gen = dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4316 | ctx.found_idx = -1; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4317 | ctx.root = root; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4318 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4319 | ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4320 | if (ret < 0) |
| 4321 | return ret; |
| 4322 | |
| 4323 | if (ctx.found_idx == -1) |
| 4324 | return -ENOENT; |
| 4325 | |
| 4326 | return ctx.found_idx; |
| 4327 | } |
| 4328 | |
| 4329 | static int __record_changed_new_ref(int num, u64 dir, int index, |
| 4330 | struct fs_path *name, |
| 4331 | void *ctx) |
| 4332 | { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4333 | u64 dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4334 | int ret; |
| 4335 | struct send_ctx *sctx = ctx; |
| 4336 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4337 | ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL, |
| 4338 | NULL, NULL, NULL); |
| 4339 | if (ret) |
| 4340 | return ret; |
| 4341 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4342 | ret = find_iref(sctx->parent_root, sctx->right_path, |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4343 | sctx->cmp_key, dir, dir_gen, name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4344 | if (ret == -ENOENT) |
| 4345 | ret = __record_new_ref(num, dir, index, name, sctx); |
| 4346 | else if (ret > 0) |
| 4347 | ret = 0; |
| 4348 | |
| 4349 | return ret; |
| 4350 | } |
| 4351 | |
| 4352 | static int __record_changed_deleted_ref(int num, u64 dir, int index, |
| 4353 | struct fs_path *name, |
| 4354 | void *ctx) |
| 4355 | { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4356 | u64 dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4357 | int ret; |
| 4358 | struct send_ctx *sctx = ctx; |
| 4359 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4360 | ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL, |
| 4361 | NULL, NULL, NULL); |
| 4362 | if (ret) |
| 4363 | return ret; |
| 4364 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4365 | ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key, |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4366 | dir, dir_gen, name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4367 | if (ret == -ENOENT) |
| 4368 | ret = __record_deleted_ref(num, dir, index, name, sctx); |
| 4369 | else if (ret > 0) |
| 4370 | ret = 0; |
| 4371 | |
| 4372 | return ret; |
| 4373 | } |
| 4374 | |
| 4375 | static int record_changed_ref(struct send_ctx *sctx) |
| 4376 | { |
| 4377 | int ret = 0; |
| 4378 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4379 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4380 | sctx->cmp_key, 0, __record_changed_new_ref, sctx); |
| 4381 | if (ret < 0) |
| 4382 | goto out; |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4383 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4384 | sctx->cmp_key, 0, __record_changed_deleted_ref, sctx); |
| 4385 | if (ret < 0) |
| 4386 | goto out; |
| 4387 | ret = 0; |
| 4388 | |
| 4389 | out: |
| 4390 | return ret; |
| 4391 | } |
| 4392 | |
| 4393 | /* |
| 4394 | * Record and process all refs at once. Needed when an inode changes the |
| 4395 | * generation number, which means that it was deleted and recreated. |
| 4396 | */ |
| 4397 | static int process_all_refs(struct send_ctx *sctx, |
| 4398 | enum btrfs_compare_tree_result cmd) |
| 4399 | { |
| 4400 | int ret; |
| 4401 | struct btrfs_root *root; |
| 4402 | struct btrfs_path *path; |
| 4403 | struct btrfs_key key; |
| 4404 | struct btrfs_key found_key; |
| 4405 | struct extent_buffer *eb; |
| 4406 | int slot; |
| 4407 | iterate_inode_ref_t cb; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4408 | int pending_move = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4409 | |
| 4410 | path = alloc_path_for_send(); |
| 4411 | if (!path) |
| 4412 | return -ENOMEM; |
| 4413 | |
| 4414 | if (cmd == BTRFS_COMPARE_TREE_NEW) { |
| 4415 | root = sctx->send_root; |
| 4416 | cb = __record_new_ref; |
| 4417 | } else if (cmd == BTRFS_COMPARE_TREE_DELETED) { |
| 4418 | root = sctx->parent_root; |
| 4419 | cb = __record_deleted_ref; |
| 4420 | } else { |
David Sterba | 4d1a63b | 2014-02-03 19:24:19 +0100 | [diff] [blame] | 4421 | btrfs_err(sctx->send_root->fs_info, |
| 4422 | "Wrong command %d in process_all_refs", cmd); |
| 4423 | ret = -EINVAL; |
| 4424 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | key.objectid = sctx->cmp_key->objectid; |
| 4428 | key.type = BTRFS_INODE_REF_KEY; |
| 4429 | key.offset = 0; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4430 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4431 | if (ret < 0) |
| 4432 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4433 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4434 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4435 | eb = path->nodes[0]; |
| 4436 | slot = path->slots[0]; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4437 | if (slot >= btrfs_header_nritems(eb)) { |
| 4438 | ret = btrfs_next_leaf(root, path); |
| 4439 | if (ret < 0) |
| 4440 | goto out; |
| 4441 | else if (ret > 0) |
| 4442 | break; |
| 4443 | continue; |
| 4444 | } |
| 4445 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4446 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 4447 | |
| 4448 | if (found_key.objectid != key.objectid || |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 4449 | (found_key.type != BTRFS_INODE_REF_KEY && |
| 4450 | found_key.type != BTRFS_INODE_EXTREF_KEY)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4451 | break; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4452 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4453 | ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4454 | if (ret < 0) |
| 4455 | goto out; |
| 4456 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4457 | path->slots[0]++; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4458 | } |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4459 | btrfs_release_path(path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4460 | |
Josef Bacik | 3dc09ec | 2016-08-24 11:57:52 -0400 | [diff] [blame] | 4461 | /* |
| 4462 | * We don't actually care about pending_move as we are simply |
| 4463 | * re-creating this inode and will be rename'ing it into place once we |
| 4464 | * rename the parent directory. |
| 4465 | */ |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4466 | ret = process_recorded_refs(sctx, &pending_move); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4467 | out: |
| 4468 | btrfs_free_path(path); |
| 4469 | return ret; |
| 4470 | } |
| 4471 | |
| 4472 | static int send_set_xattr(struct send_ctx *sctx, |
| 4473 | struct fs_path *path, |
| 4474 | const char *name, int name_len, |
| 4475 | const char *data, int data_len) |
| 4476 | { |
| 4477 | int ret = 0; |
| 4478 | |
| 4479 | ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); |
| 4480 | if (ret < 0) |
| 4481 | goto out; |
| 4482 | |
| 4483 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 4484 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); |
| 4485 | TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); |
| 4486 | |
| 4487 | ret = send_cmd(sctx); |
| 4488 | |
| 4489 | tlv_put_failure: |
| 4490 | out: |
| 4491 | return ret; |
| 4492 | } |
| 4493 | |
| 4494 | static int send_remove_xattr(struct send_ctx *sctx, |
| 4495 | struct fs_path *path, |
| 4496 | const char *name, int name_len) |
| 4497 | { |
| 4498 | int ret = 0; |
| 4499 | |
| 4500 | ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); |
| 4501 | if (ret < 0) |
| 4502 | goto out; |
| 4503 | |
| 4504 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 4505 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); |
| 4506 | |
| 4507 | ret = send_cmd(sctx); |
| 4508 | |
| 4509 | tlv_put_failure: |
| 4510 | out: |
| 4511 | return ret; |
| 4512 | } |
| 4513 | |
| 4514 | static int __process_new_xattr(int num, struct btrfs_key *di_key, |
| 4515 | const char *name, int name_len, |
| 4516 | const char *data, int data_len, |
| 4517 | u8 type, void *ctx) |
| 4518 | { |
| 4519 | int ret; |
| 4520 | struct send_ctx *sctx = ctx; |
| 4521 | struct fs_path *p; |
Andreas Gruenbacher | 2211d5b | 2016-09-27 13:03:22 +0200 | [diff] [blame] | 4522 | struct posix_acl_xattr_header dummy_acl; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4523 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4524 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4525 | if (!p) |
| 4526 | return -ENOMEM; |
| 4527 | |
| 4528 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4529 | * This hack is needed because empty acls are stored as zero byte |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4530 | * 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] | 4531 | * 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] | 4532 | * only contains the version number and no entries. |
| 4533 | */ |
| 4534 | if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) || |
| 4535 | !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) { |
| 4536 | if (data_len == 0) { |
| 4537 | dummy_acl.a_version = |
| 4538 | cpu_to_le32(POSIX_ACL_XATTR_VERSION); |
| 4539 | data = (char *)&dummy_acl; |
| 4540 | data_len = sizeof(dummy_acl); |
| 4541 | } |
| 4542 | } |
| 4543 | |
| 4544 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4545 | if (ret < 0) |
| 4546 | goto out; |
| 4547 | |
| 4548 | ret = send_set_xattr(sctx, p, name, name_len, data, data_len); |
| 4549 | |
| 4550 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4551 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4552 | return ret; |
| 4553 | } |
| 4554 | |
| 4555 | static int __process_deleted_xattr(int num, struct btrfs_key *di_key, |
| 4556 | const char *name, int name_len, |
| 4557 | const char *data, int data_len, |
| 4558 | u8 type, void *ctx) |
| 4559 | { |
| 4560 | int ret; |
| 4561 | struct send_ctx *sctx = ctx; |
| 4562 | struct fs_path *p; |
| 4563 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4564 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4565 | if (!p) |
| 4566 | return -ENOMEM; |
| 4567 | |
| 4568 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4569 | if (ret < 0) |
| 4570 | goto out; |
| 4571 | |
| 4572 | ret = send_remove_xattr(sctx, p, name, name_len); |
| 4573 | |
| 4574 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4575 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4576 | return ret; |
| 4577 | } |
| 4578 | |
| 4579 | static int process_new_xattr(struct send_ctx *sctx) |
| 4580 | { |
| 4581 | int ret = 0; |
| 4582 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4583 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4584 | __process_new_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4585 | |
| 4586 | return ret; |
| 4587 | } |
| 4588 | |
| 4589 | static int process_deleted_xattr(struct send_ctx *sctx) |
| 4590 | { |
Masahiro Yamada | e2c8990 | 2016-09-13 04:35:52 +0900 | [diff] [blame] | 4591 | return iterate_dir_item(sctx->parent_root, sctx->right_path, |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4592 | __process_deleted_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | struct find_xattr_ctx { |
| 4596 | const char *name; |
| 4597 | int name_len; |
| 4598 | int found_idx; |
| 4599 | char *found_data; |
| 4600 | int found_data_len; |
| 4601 | }; |
| 4602 | |
| 4603 | static int __find_xattr(int num, struct btrfs_key *di_key, |
| 4604 | const char *name, int name_len, |
| 4605 | const char *data, int data_len, |
| 4606 | u8 type, void *vctx) |
| 4607 | { |
| 4608 | struct find_xattr_ctx *ctx = vctx; |
| 4609 | |
| 4610 | if (name_len == ctx->name_len && |
| 4611 | strncmp(name, ctx->name, name_len) == 0) { |
| 4612 | ctx->found_idx = num; |
| 4613 | ctx->found_data_len = data_len; |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 4614 | ctx->found_data = kmemdup(data, data_len, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4615 | if (!ctx->found_data) |
| 4616 | return -ENOMEM; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4617 | return 1; |
| 4618 | } |
| 4619 | return 0; |
| 4620 | } |
| 4621 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4622 | static int find_xattr(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4623 | struct btrfs_path *path, |
| 4624 | struct btrfs_key *key, |
| 4625 | const char *name, int name_len, |
| 4626 | char **data, int *data_len) |
| 4627 | { |
| 4628 | int ret; |
| 4629 | struct find_xattr_ctx ctx; |
| 4630 | |
| 4631 | ctx.name = name; |
| 4632 | ctx.name_len = name_len; |
| 4633 | ctx.found_idx = -1; |
| 4634 | ctx.found_data = NULL; |
| 4635 | ctx.found_data_len = 0; |
| 4636 | |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4637 | ret = iterate_dir_item(root, path, __find_xattr, &ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4638 | if (ret < 0) |
| 4639 | return ret; |
| 4640 | |
| 4641 | if (ctx.found_idx == -1) |
| 4642 | return -ENOENT; |
| 4643 | if (data) { |
| 4644 | *data = ctx.found_data; |
| 4645 | *data_len = ctx.found_data_len; |
| 4646 | } else { |
| 4647 | kfree(ctx.found_data); |
| 4648 | } |
| 4649 | return ctx.found_idx; |
| 4650 | } |
| 4651 | |
| 4652 | |
| 4653 | static int __process_changed_new_xattr(int num, struct btrfs_key *di_key, |
| 4654 | const char *name, int name_len, |
| 4655 | const char *data, int data_len, |
| 4656 | u8 type, void *ctx) |
| 4657 | { |
| 4658 | int ret; |
| 4659 | struct send_ctx *sctx = ctx; |
| 4660 | char *found_data = NULL; |
| 4661 | int found_data_len = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4662 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4663 | ret = find_xattr(sctx->parent_root, sctx->right_path, |
| 4664 | sctx->cmp_key, name, name_len, &found_data, |
| 4665 | &found_data_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4666 | if (ret == -ENOENT) { |
| 4667 | ret = __process_new_xattr(num, di_key, name, name_len, data, |
| 4668 | data_len, type, ctx); |
| 4669 | } else if (ret >= 0) { |
| 4670 | if (data_len != found_data_len || |
| 4671 | memcmp(data, found_data, data_len)) { |
| 4672 | ret = __process_new_xattr(num, di_key, name, name_len, |
| 4673 | data, data_len, type, ctx); |
| 4674 | } else { |
| 4675 | ret = 0; |
| 4676 | } |
| 4677 | } |
| 4678 | |
| 4679 | kfree(found_data); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4680 | return ret; |
| 4681 | } |
| 4682 | |
| 4683 | static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key, |
| 4684 | const char *name, int name_len, |
| 4685 | const char *data, int data_len, |
| 4686 | u8 type, void *ctx) |
| 4687 | { |
| 4688 | int ret; |
| 4689 | struct send_ctx *sctx = ctx; |
| 4690 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4691 | ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key, |
| 4692 | name, name_len, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4693 | if (ret == -ENOENT) |
| 4694 | ret = __process_deleted_xattr(num, di_key, name, name_len, data, |
| 4695 | data_len, type, ctx); |
| 4696 | else if (ret >= 0) |
| 4697 | ret = 0; |
| 4698 | |
| 4699 | return ret; |
| 4700 | } |
| 4701 | |
| 4702 | static int process_changed_xattr(struct send_ctx *sctx) |
| 4703 | { |
| 4704 | int ret = 0; |
| 4705 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4706 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4707 | __process_changed_new_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4708 | if (ret < 0) |
| 4709 | goto out; |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4710 | ret = iterate_dir_item(sctx->parent_root, sctx->right_path, |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4711 | __process_changed_deleted_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4712 | |
| 4713 | out: |
| 4714 | return ret; |
| 4715 | } |
| 4716 | |
| 4717 | static int process_all_new_xattrs(struct send_ctx *sctx) |
| 4718 | { |
| 4719 | int ret; |
| 4720 | struct btrfs_root *root; |
| 4721 | struct btrfs_path *path; |
| 4722 | struct btrfs_key key; |
| 4723 | struct btrfs_key found_key; |
| 4724 | struct extent_buffer *eb; |
| 4725 | int slot; |
| 4726 | |
| 4727 | path = alloc_path_for_send(); |
| 4728 | if (!path) |
| 4729 | return -ENOMEM; |
| 4730 | |
| 4731 | root = sctx->send_root; |
| 4732 | |
| 4733 | key.objectid = sctx->cmp_key->objectid; |
| 4734 | key.type = BTRFS_XATTR_ITEM_KEY; |
| 4735 | key.offset = 0; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4736 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4737 | if (ret < 0) |
| 4738 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4739 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4740 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4741 | eb = path->nodes[0]; |
| 4742 | slot = path->slots[0]; |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4743 | if (slot >= btrfs_header_nritems(eb)) { |
| 4744 | ret = btrfs_next_leaf(root, path); |
| 4745 | if (ret < 0) { |
| 4746 | goto out; |
| 4747 | } else if (ret > 0) { |
| 4748 | ret = 0; |
| 4749 | break; |
| 4750 | } |
| 4751 | continue; |
| 4752 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4753 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4754 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4755 | if (found_key.objectid != key.objectid || |
| 4756 | found_key.type != key.type) { |
| 4757 | ret = 0; |
| 4758 | goto out; |
| 4759 | } |
| 4760 | |
Nikolay Borisov | a035751 | 2017-08-21 12:43:44 +0300 | [diff] [blame] | 4761 | ret = iterate_dir_item(root, path, __process_new_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4762 | if (ret < 0) |
| 4763 | goto out; |
| 4764 | |
Filipe David Borba Manana | dff6d0a | 2014-02-05 16:48:56 +0000 | [diff] [blame] | 4765 | path->slots[0]++; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | out: |
| 4769 | btrfs_free_path(path); |
| 4770 | return ret; |
| 4771 | } |
| 4772 | |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4773 | static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len) |
| 4774 | { |
| 4775 | struct btrfs_root *root = sctx->send_root; |
| 4776 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4777 | struct inode *inode; |
| 4778 | struct page *page; |
| 4779 | char *addr; |
| 4780 | struct btrfs_key key; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4781 | pgoff_t index = offset >> PAGE_SHIFT; |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4782 | pgoff_t last_index; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 4783 | unsigned pg_offset = offset_in_page(offset); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4784 | ssize_t ret = 0; |
| 4785 | |
| 4786 | key.objectid = sctx->cur_ino; |
| 4787 | key.type = BTRFS_INODE_ITEM_KEY; |
| 4788 | key.offset = 0; |
| 4789 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 4790 | inode = btrfs_iget(fs_info->sb, &key, root); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4791 | if (IS_ERR(inode)) |
| 4792 | return PTR_ERR(inode); |
| 4793 | |
| 4794 | if (offset + len > i_size_read(inode)) { |
| 4795 | if (offset > i_size_read(inode)) |
| 4796 | len = 0; |
| 4797 | else |
| 4798 | len = offset - i_size_read(inode); |
| 4799 | } |
| 4800 | if (len == 0) |
| 4801 | goto out; |
| 4802 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4803 | last_index = (offset + len - 1) >> PAGE_SHIFT; |
Liu Bo | 2131bcd | 2014-03-05 10:07:35 +0800 | [diff] [blame] | 4804 | |
| 4805 | /* initial readahead */ |
| 4806 | memset(&sctx->ra, 0, sizeof(struct file_ra_state)); |
| 4807 | file_ra_state_init(&sctx->ra, inode->i_mapping); |
Liu Bo | 2131bcd | 2014-03-05 10:07:35 +0800 | [diff] [blame] | 4808 | |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4809 | while (index <= last_index) { |
| 4810 | unsigned cur_len = min_t(unsigned, len, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4811 | PAGE_SIZE - pg_offset); |
Kuanling Huang | eef16ba | 2017-09-15 16:47:45 +0800 | [diff] [blame] | 4812 | |
| 4813 | page = find_lock_page(inode->i_mapping, index); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4814 | if (!page) { |
Kuanling Huang | eef16ba | 2017-09-15 16:47:45 +0800 | [diff] [blame] | 4815 | page_cache_sync_readahead(inode->i_mapping, &sctx->ra, |
| 4816 | NULL, index, last_index + 1 - index); |
| 4817 | |
| 4818 | page = find_or_create_page(inode->i_mapping, index, |
| 4819 | GFP_KERNEL); |
| 4820 | if (!page) { |
| 4821 | ret = -ENOMEM; |
| 4822 | break; |
| 4823 | } |
| 4824 | } |
| 4825 | |
| 4826 | if (PageReadahead(page)) { |
| 4827 | page_cache_async_readahead(inode->i_mapping, &sctx->ra, |
| 4828 | NULL, page, index, last_index + 1 - index); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | if (!PageUptodate(page)) { |
| 4832 | btrfs_readpage(NULL, page); |
| 4833 | lock_page(page); |
| 4834 | if (!PageUptodate(page)) { |
| 4835 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4836 | put_page(page); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4837 | ret = -EIO; |
| 4838 | break; |
| 4839 | } |
| 4840 | } |
| 4841 | |
| 4842 | addr = kmap(page); |
| 4843 | memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len); |
| 4844 | kunmap(page); |
| 4845 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4846 | put_page(page); |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4847 | index++; |
| 4848 | pg_offset = 0; |
| 4849 | len -= cur_len; |
| 4850 | ret += cur_len; |
| 4851 | } |
| 4852 | out: |
| 4853 | iput(inode); |
| 4854 | return ret; |
| 4855 | } |
| 4856 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4857 | /* |
| 4858 | * Read some bytes from the current inode/file and send a write command to |
| 4859 | * user space. |
| 4860 | */ |
| 4861 | static int send_write(struct send_ctx *sctx, u64 offset, u32 len) |
| 4862 | { |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 4863 | struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4864 | int ret = 0; |
| 4865 | struct fs_path *p; |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4866 | ssize_t num_read = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4867 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4868 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4869 | if (!p) |
| 4870 | return -ENOMEM; |
| 4871 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 4872 | btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4873 | |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4874 | num_read = fill_read_buf(sctx, offset, len); |
| 4875 | if (num_read <= 0) { |
| 4876 | if (num_read < 0) |
| 4877 | ret = num_read; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4878 | goto out; |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4879 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4880 | |
| 4881 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); |
| 4882 | if (ret < 0) |
| 4883 | goto out; |
| 4884 | |
| 4885 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4886 | if (ret < 0) |
| 4887 | goto out; |
| 4888 | |
| 4889 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 4890 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4891 | TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4892 | |
| 4893 | ret = send_cmd(sctx); |
| 4894 | |
| 4895 | tlv_put_failure: |
| 4896 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4897 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4898 | if (ret < 0) |
| 4899 | return ret; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4900 | return num_read; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | /* |
| 4904 | * Send a clone command to user space. |
| 4905 | */ |
| 4906 | static int send_clone(struct send_ctx *sctx, |
| 4907 | u64 offset, u32 len, |
| 4908 | struct clone_root *clone_root) |
| 4909 | { |
| 4910 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4911 | struct fs_path *p; |
| 4912 | u64 gen; |
| 4913 | |
Jeff Mahoney | 04ab956 | 2016-09-20 10:05:03 -0400 | [diff] [blame] | 4914 | btrfs_debug(sctx->send_root->fs_info, |
| 4915 | "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] | 4916 | offset, len, clone_root->root->root_key.objectid, |
| 4917 | clone_root->ino, clone_root->offset); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4918 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4919 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4920 | if (!p) |
| 4921 | return -ENOMEM; |
| 4922 | |
| 4923 | ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); |
| 4924 | if (ret < 0) |
| 4925 | goto out; |
| 4926 | |
| 4927 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4928 | if (ret < 0) |
| 4929 | goto out; |
| 4930 | |
| 4931 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 4932 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); |
| 4933 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 4934 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4935 | if (clone_root->root == sctx->send_root) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4936 | ret = get_inode_info(sctx->send_root, clone_root->ino, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 4937 | &gen, NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4938 | if (ret < 0) |
| 4939 | goto out; |
| 4940 | ret = get_cur_path(sctx, clone_root->ino, gen, p); |
| 4941 | } else { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4942 | ret = get_inode_path(clone_root->root, clone_root->ino, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4943 | } |
| 4944 | if (ret < 0) |
| 4945 | goto out; |
| 4946 | |
Josef Bacik | 37b8d27 | 2015-06-04 17:17:25 -0400 | [diff] [blame] | 4947 | /* |
| 4948 | * If the parent we're using has a received_uuid set then use that as |
| 4949 | * our clone source as that is what we will look for when doing a |
| 4950 | * receive. |
| 4951 | * |
| 4952 | * This covers the case that we create a snapshot off of a received |
| 4953 | * subvolume and then use that as the parent and try to receive on a |
| 4954 | * different host. |
| 4955 | */ |
| 4956 | if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid)) |
| 4957 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 4958 | clone_root->root->root_item.received_uuid); |
| 4959 | else |
| 4960 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 4961 | clone_root->root->root_item.uuid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4962 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
Filipe David Borba Manana | 5a0f4e2 | 2013-12-03 15:55:48 +0000 | [diff] [blame] | 4963 | le64_to_cpu(clone_root->root->root_item.ctransid)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4964 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); |
| 4965 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, |
| 4966 | clone_root->offset); |
| 4967 | |
| 4968 | ret = send_cmd(sctx); |
| 4969 | |
| 4970 | tlv_put_failure: |
| 4971 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4972 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4973 | return ret; |
| 4974 | } |
| 4975 | |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 4976 | /* |
| 4977 | * Send an update extent command to user space. |
| 4978 | */ |
| 4979 | static int send_update_extent(struct send_ctx *sctx, |
| 4980 | u64 offset, u32 len) |
| 4981 | { |
| 4982 | int ret = 0; |
| 4983 | struct fs_path *p; |
| 4984 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4985 | p = fs_path_alloc(); |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 4986 | if (!p) |
| 4987 | return -ENOMEM; |
| 4988 | |
| 4989 | ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT); |
| 4990 | if (ret < 0) |
| 4991 | goto out; |
| 4992 | |
| 4993 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4994 | if (ret < 0) |
| 4995 | goto out; |
| 4996 | |
| 4997 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 4998 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 4999 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len); |
| 5000 | |
| 5001 | ret = send_cmd(sctx); |
| 5002 | |
| 5003 | tlv_put_failure: |
| 5004 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 5005 | fs_path_free(p); |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 5006 | return ret; |
| 5007 | } |
| 5008 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5009 | static int send_hole(struct send_ctx *sctx, u64 end) |
| 5010 | { |
| 5011 | struct fs_path *p = NULL; |
| 5012 | u64 offset = sctx->cur_inode_last_extent; |
| 5013 | u64 len; |
| 5014 | int ret = 0; |
| 5015 | |
Filipe Manana | 22d3151 | 2018-07-30 12:39:58 +0100 | [diff] [blame] | 5016 | /* |
| 5017 | * A hole that starts at EOF or beyond it. Since we do not yet support |
| 5018 | * fallocate (for extent preallocation and hole punching), sending a |
| 5019 | * write of zeroes starting at EOF or beyond would later require issuing |
| 5020 | * a truncate operation which would undo the write and achieve nothing. |
| 5021 | */ |
| 5022 | if (offset >= sctx->cur_inode_size) |
| 5023 | return 0; |
| 5024 | |
Filipe Manana | 6b1f72e5 | 2019-05-20 09:55:42 +0100 | [diff] [blame] | 5025 | /* |
| 5026 | * Don't go beyond the inode's i_size due to prealloc extents that start |
| 5027 | * after the i_size. |
| 5028 | */ |
| 5029 | end = min_t(u64, end, sctx->cur_inode_size); |
| 5030 | |
Filipe Manana | d4dfc0f | 2018-02-06 20:39:20 +0000 | [diff] [blame] | 5031 | if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) |
| 5032 | return send_update_extent(sctx, offset, end - offset); |
| 5033 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5034 | p = fs_path_alloc(); |
| 5035 | if (!p) |
| 5036 | return -ENOMEM; |
Filipe Manana | c715e15 | 2014-03-31 14:52:14 +0100 | [diff] [blame] | 5037 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 5038 | if (ret < 0) |
| 5039 | goto tlv_put_failure; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5040 | memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE); |
| 5041 | while (offset < end) { |
| 5042 | len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE); |
| 5043 | |
| 5044 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); |
| 5045 | if (ret < 0) |
| 5046 | break; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5047 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 5048 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 5049 | TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len); |
| 5050 | ret = send_cmd(sctx); |
| 5051 | if (ret < 0) |
| 5052 | break; |
| 5053 | offset += len; |
| 5054 | } |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5055 | sctx->cur_inode_next_write_offset = offset; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5056 | tlv_put_failure: |
| 5057 | fs_path_free(p); |
| 5058 | return ret; |
| 5059 | } |
| 5060 | |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5061 | static int send_extent_data(struct send_ctx *sctx, |
| 5062 | const u64 offset, |
| 5063 | const u64 len) |
| 5064 | { |
| 5065 | u64 sent = 0; |
| 5066 | |
| 5067 | if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) |
| 5068 | return send_update_extent(sctx, offset, len); |
| 5069 | |
| 5070 | while (sent < len) { |
| 5071 | u64 size = len - sent; |
| 5072 | int ret; |
| 5073 | |
| 5074 | if (size > BTRFS_SEND_READ_SIZE) |
| 5075 | size = BTRFS_SEND_READ_SIZE; |
| 5076 | ret = send_write(sctx, offset + sent, size); |
| 5077 | if (ret < 0) |
| 5078 | return ret; |
| 5079 | if (!ret) |
| 5080 | break; |
| 5081 | sent += ret; |
| 5082 | } |
| 5083 | return 0; |
| 5084 | } |
| 5085 | |
| 5086 | static int clone_range(struct send_ctx *sctx, |
| 5087 | struct clone_root *clone_root, |
| 5088 | const u64 disk_byte, |
| 5089 | u64 data_offset, |
| 5090 | u64 offset, |
| 5091 | u64 len) |
| 5092 | { |
| 5093 | struct btrfs_path *path; |
| 5094 | struct btrfs_key key; |
| 5095 | int ret; |
Austin Kim | 431d398 | 2019-09-03 12:30:19 +0900 | [diff] [blame] | 5096 | u64 clone_src_i_size = 0; |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5097 | |
Filipe Manana | 72610b1 | 2017-08-10 22:54:51 +0100 | [diff] [blame] | 5098 | /* |
| 5099 | * Prevent cloning from a zero offset with a length matching the sector |
| 5100 | * size because in some scenarios this will make the receiver fail. |
| 5101 | * |
| 5102 | * For example, if in the source filesystem the extent at offset 0 |
| 5103 | * has a length of sectorsize and it was written using direct IO, then |
| 5104 | * it can never be an inline extent (even if compression is enabled). |
| 5105 | * Then this extent can be cloned in the original filesystem to a non |
| 5106 | * zero file offset, but it may not be possible to clone in the |
| 5107 | * destination filesystem because it can be inlined due to compression |
| 5108 | * on the destination filesystem (as the receiver's write operations are |
| 5109 | * always done using buffered IO). The same happens when the original |
| 5110 | * filesystem does not have compression enabled but the destination |
| 5111 | * filesystem has. |
| 5112 | */ |
| 5113 | if (clone_root->offset == 0 && |
| 5114 | len == sctx->send_root->fs_info->sectorsize) |
| 5115 | return send_extent_data(sctx, offset, len); |
| 5116 | |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5117 | path = alloc_path_for_send(); |
| 5118 | if (!path) |
| 5119 | return -ENOMEM; |
| 5120 | |
| 5121 | /* |
Robbie Ko | 040ee612 | 2019-03-29 18:03:27 +0800 | [diff] [blame] | 5122 | * There are inodes that have extents that lie behind its i_size. Don't |
| 5123 | * accept clones from these extents. |
| 5124 | */ |
| 5125 | ret = __get_inode_info(clone_root->root, path, clone_root->ino, |
| 5126 | &clone_src_i_size, NULL, NULL, NULL, NULL, NULL); |
| 5127 | btrfs_release_path(path); |
| 5128 | if (ret < 0) |
| 5129 | goto out; |
| 5130 | |
| 5131 | /* |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5132 | * We can't send a clone operation for the entire range if we find |
| 5133 | * extent items in the respective range in the source file that |
| 5134 | * refer to different extents or if we find holes. |
| 5135 | * So check for that and do a mix of clone and regular write/copy |
| 5136 | * operations if needed. |
| 5137 | * |
| 5138 | * Example: |
| 5139 | * |
| 5140 | * mkfs.btrfs -f /dev/sda |
| 5141 | * mount /dev/sda /mnt |
| 5142 | * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo |
| 5143 | * cp --reflink=always /mnt/foo /mnt/bar |
| 5144 | * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo |
| 5145 | * btrfs subvolume snapshot -r /mnt /mnt/snap |
| 5146 | * |
| 5147 | * If when we send the snapshot and we are processing file bar (which |
| 5148 | * has a higher inode number than foo) we blindly send a clone operation |
| 5149 | * for the [0, 100K[ range from foo to bar, the receiver ends up getting |
| 5150 | * a file bar that matches the content of file foo - iow, doesn't match |
| 5151 | * the content from bar in the original filesystem. |
| 5152 | */ |
| 5153 | key.objectid = clone_root->ino; |
| 5154 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5155 | key.offset = clone_root->offset; |
| 5156 | ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0); |
| 5157 | if (ret < 0) |
| 5158 | goto out; |
| 5159 | if (ret > 0 && path->slots[0] > 0) { |
| 5160 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1); |
| 5161 | if (key.objectid == clone_root->ino && |
| 5162 | key.type == BTRFS_EXTENT_DATA_KEY) |
| 5163 | path->slots[0]--; |
| 5164 | } |
| 5165 | |
| 5166 | while (true) { |
| 5167 | struct extent_buffer *leaf = path->nodes[0]; |
| 5168 | int slot = path->slots[0]; |
| 5169 | struct btrfs_file_extent_item *ei; |
| 5170 | u8 type; |
| 5171 | u64 ext_len; |
| 5172 | u64 clone_len; |
Robbie Ko | 040ee612 | 2019-03-29 18:03:27 +0800 | [diff] [blame] | 5173 | u64 clone_data_offset; |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5174 | |
| 5175 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5176 | ret = btrfs_next_leaf(clone_root->root, path); |
| 5177 | if (ret < 0) |
| 5178 | goto out; |
| 5179 | else if (ret > 0) |
| 5180 | break; |
| 5181 | continue; |
| 5182 | } |
| 5183 | |
| 5184 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5185 | |
| 5186 | /* |
| 5187 | * We might have an implicit trailing hole (NO_HOLES feature |
| 5188 | * enabled). We deal with it after leaving this loop. |
| 5189 | */ |
| 5190 | if (key.objectid != clone_root->ino || |
| 5191 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 5192 | break; |
| 5193 | |
| 5194 | ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5195 | type = btrfs_file_extent_type(leaf, ei); |
| 5196 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 5197 | ext_len = btrfs_file_extent_ram_bytes(leaf, ei); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5198 | ext_len = PAGE_ALIGN(ext_len); |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5199 | } else { |
| 5200 | ext_len = btrfs_file_extent_num_bytes(leaf, ei); |
| 5201 | } |
| 5202 | |
| 5203 | if (key.offset + ext_len <= clone_root->offset) |
| 5204 | goto next; |
| 5205 | |
| 5206 | if (key.offset > clone_root->offset) { |
| 5207 | /* Implicit hole, NO_HOLES feature enabled. */ |
| 5208 | u64 hole_len = key.offset - clone_root->offset; |
| 5209 | |
| 5210 | if (hole_len > len) |
| 5211 | hole_len = len; |
| 5212 | ret = send_extent_data(sctx, offset, hole_len); |
| 5213 | if (ret < 0) |
| 5214 | goto out; |
| 5215 | |
| 5216 | len -= hole_len; |
| 5217 | if (len == 0) |
| 5218 | break; |
| 5219 | offset += hole_len; |
| 5220 | clone_root->offset += hole_len; |
| 5221 | data_offset += hole_len; |
| 5222 | } |
| 5223 | |
| 5224 | if (key.offset >= clone_root->offset + len) |
| 5225 | break; |
| 5226 | |
Robbie Ko | 040ee612 | 2019-03-29 18:03:27 +0800 | [diff] [blame] | 5227 | if (key.offset >= clone_src_i_size) |
| 5228 | break; |
| 5229 | |
| 5230 | if (key.offset + ext_len > clone_src_i_size) |
| 5231 | ext_len = clone_src_i_size - key.offset; |
| 5232 | |
| 5233 | clone_data_offset = btrfs_file_extent_offset(leaf, ei); |
| 5234 | if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) { |
| 5235 | clone_root->offset = key.offset; |
| 5236 | if (clone_data_offset < data_offset && |
| 5237 | clone_data_offset + ext_len > data_offset) { |
| 5238 | u64 extent_offset; |
| 5239 | |
| 5240 | extent_offset = data_offset - clone_data_offset; |
| 5241 | ext_len -= extent_offset; |
| 5242 | clone_data_offset += extent_offset; |
| 5243 | clone_root->offset += extent_offset; |
| 5244 | } |
| 5245 | } |
| 5246 | |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5247 | clone_len = min_t(u64, ext_len, len); |
| 5248 | |
| 5249 | if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte && |
Filipe Manana | 3c850b4 | 2019-05-20 09:57:00 +0100 | [diff] [blame] | 5250 | clone_data_offset == data_offset) { |
| 5251 | const u64 src_end = clone_root->offset + clone_len; |
| 5252 | const u64 sectorsize = SZ_64K; |
| 5253 | |
| 5254 | /* |
| 5255 | * We can't clone the last block, when its size is not |
| 5256 | * sector size aligned, into the middle of a file. If we |
| 5257 | * do so, the receiver will get a failure (-EINVAL) when |
| 5258 | * trying to clone or will silently corrupt the data in |
| 5259 | * the destination file if it's on a kernel without the |
| 5260 | * fix introduced by commit ac765f83f1397646 |
| 5261 | * ("Btrfs: fix data corruption due to cloning of eof |
| 5262 | * block). |
| 5263 | * |
| 5264 | * So issue a clone of the aligned down range plus a |
| 5265 | * regular write for the eof block, if we hit that case. |
| 5266 | * |
| 5267 | * Also, we use the maximum possible sector size, 64K, |
| 5268 | * because we don't know what's the sector size of the |
| 5269 | * filesystem that receives the stream, so we have to |
| 5270 | * assume the largest possible sector size. |
| 5271 | */ |
| 5272 | if (src_end == clone_src_i_size && |
| 5273 | !IS_ALIGNED(src_end, sectorsize) && |
| 5274 | offset + clone_len < sctx->cur_inode_size) { |
| 5275 | u64 slen; |
| 5276 | |
| 5277 | slen = ALIGN_DOWN(src_end - clone_root->offset, |
| 5278 | sectorsize); |
| 5279 | if (slen > 0) { |
| 5280 | ret = send_clone(sctx, offset, slen, |
| 5281 | clone_root); |
| 5282 | if (ret < 0) |
| 5283 | goto out; |
| 5284 | } |
| 5285 | ret = send_extent_data(sctx, offset + slen, |
| 5286 | clone_len - slen); |
| 5287 | } else { |
| 5288 | ret = send_clone(sctx, offset, clone_len, |
| 5289 | clone_root); |
| 5290 | } |
| 5291 | } else { |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5292 | ret = send_extent_data(sctx, offset, clone_len); |
Filipe Manana | 3c850b4 | 2019-05-20 09:57:00 +0100 | [diff] [blame] | 5293 | } |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5294 | |
| 5295 | if (ret < 0) |
| 5296 | goto out; |
| 5297 | |
| 5298 | len -= clone_len; |
| 5299 | if (len == 0) |
| 5300 | break; |
| 5301 | offset += clone_len; |
| 5302 | clone_root->offset += clone_len; |
| 5303 | data_offset += clone_len; |
| 5304 | next: |
| 5305 | path->slots[0]++; |
| 5306 | } |
| 5307 | |
| 5308 | if (len > 0) |
| 5309 | ret = send_extent_data(sctx, offset, len); |
| 5310 | else |
| 5311 | ret = 0; |
| 5312 | out: |
| 5313 | btrfs_free_path(path); |
| 5314 | return ret; |
| 5315 | } |
| 5316 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5317 | static int send_write_or_clone(struct send_ctx *sctx, |
| 5318 | struct btrfs_path *path, |
| 5319 | struct btrfs_key *key, |
| 5320 | struct clone_root *clone_root) |
| 5321 | { |
| 5322 | int ret = 0; |
| 5323 | struct btrfs_file_extent_item *ei; |
| 5324 | u64 offset = key->offset; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5325 | u64 len; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5326 | u8 type; |
Filipe David Borba Manana | 28e5dd8 | 2014-01-12 02:26:28 +0000 | [diff] [blame] | 5327 | u64 bs = sctx->send_root->fs_info->sb->s_blocksize; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5328 | |
| 5329 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5330 | struct btrfs_file_extent_item); |
| 5331 | type = btrfs_file_extent_type(path->nodes[0], ei); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5332 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 5333 | len = btrfs_file_extent_ram_bytes(path->nodes[0], ei); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5334 | /* |
| 5335 | * it is possible the inline item won't cover the whole page, |
| 5336 | * but there may be items after this page. Make |
| 5337 | * sure to send the whole thing |
| 5338 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5339 | len = PAGE_ALIGN(len); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5340 | } else { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5341 | len = btrfs_file_extent_num_bytes(path->nodes[0], ei); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5342 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5343 | |
Filipe Manana | a6aa10c7 | 2018-04-30 19:05:07 +0100 | [diff] [blame] | 5344 | if (offset >= sctx->cur_inode_size) { |
| 5345 | ret = 0; |
| 5346 | goto out; |
| 5347 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5348 | if (offset + len > sctx->cur_inode_size) |
| 5349 | len = sctx->cur_inode_size - offset; |
| 5350 | if (len == 0) { |
| 5351 | ret = 0; |
| 5352 | goto out; |
| 5353 | } |
| 5354 | |
Filipe David Borba Manana | 28e5dd8 | 2014-01-12 02:26:28 +0000 | [diff] [blame] | 5355 | if (clone_root && IS_ALIGNED(offset + len, bs)) { |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5356 | u64 disk_byte; |
| 5357 | u64 data_offset; |
| 5358 | |
| 5359 | disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei); |
| 5360 | data_offset = btrfs_file_extent_offset(path->nodes[0], ei); |
| 5361 | ret = clone_range(sctx, clone_root, disk_byte, data_offset, |
| 5362 | offset, len); |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 5363 | } else { |
Filipe Manana | d906d49 | 2015-10-02 10:47:34 +0100 | [diff] [blame] | 5364 | ret = send_extent_data(sctx, offset, len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5365 | } |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5366 | sctx->cur_inode_next_write_offset = offset + len; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5367 | out: |
| 5368 | return ret; |
| 5369 | } |
| 5370 | |
| 5371 | static int is_extent_unchanged(struct send_ctx *sctx, |
| 5372 | struct btrfs_path *left_path, |
| 5373 | struct btrfs_key *ekey) |
| 5374 | { |
| 5375 | int ret = 0; |
| 5376 | struct btrfs_key key; |
| 5377 | struct btrfs_path *path = NULL; |
| 5378 | struct extent_buffer *eb; |
| 5379 | int slot; |
| 5380 | struct btrfs_key found_key; |
| 5381 | struct btrfs_file_extent_item *ei; |
| 5382 | u64 left_disknr; |
| 5383 | u64 right_disknr; |
| 5384 | u64 left_offset; |
| 5385 | u64 right_offset; |
| 5386 | u64 left_offset_fixed; |
| 5387 | u64 left_len; |
| 5388 | u64 right_len; |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5389 | u64 left_gen; |
| 5390 | u64 right_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5391 | u8 left_type; |
| 5392 | u8 right_type; |
| 5393 | |
| 5394 | path = alloc_path_for_send(); |
| 5395 | if (!path) |
| 5396 | return -ENOMEM; |
| 5397 | |
| 5398 | eb = left_path->nodes[0]; |
| 5399 | slot = left_path->slots[0]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5400 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 5401 | left_type = btrfs_file_extent_type(eb, ei); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5402 | |
| 5403 | if (left_type != BTRFS_FILE_EXTENT_REG) { |
| 5404 | ret = 0; |
| 5405 | goto out; |
| 5406 | } |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5407 | left_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
| 5408 | left_len = btrfs_file_extent_num_bytes(eb, ei); |
| 5409 | left_offset = btrfs_file_extent_offset(eb, ei); |
| 5410 | left_gen = btrfs_file_extent_generation(eb, ei); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5411 | |
| 5412 | /* |
| 5413 | * Following comments will refer to these graphics. L is the left |
| 5414 | * extents which we are checking at the moment. 1-8 are the right |
| 5415 | * extents that we iterate. |
| 5416 | * |
| 5417 | * |-----L-----| |
| 5418 | * |-1-|-2a-|-3-|-4-|-5-|-6-| |
| 5419 | * |
| 5420 | * |-----L-----| |
| 5421 | * |--1--|-2b-|...(same as above) |
| 5422 | * |
| 5423 | * Alternative situation. Happens on files where extents got split. |
| 5424 | * |-----L-----| |
| 5425 | * |-----------7-----------|-6-| |
| 5426 | * |
| 5427 | * Alternative situation. Happens on files which got larger. |
| 5428 | * |-----L-----| |
| 5429 | * |-8-| |
| 5430 | * Nothing follows after 8. |
| 5431 | */ |
| 5432 | |
| 5433 | key.objectid = ekey->objectid; |
| 5434 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5435 | key.offset = ekey->offset; |
| 5436 | ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); |
| 5437 | if (ret < 0) |
| 5438 | goto out; |
| 5439 | if (ret) { |
| 5440 | ret = 0; |
| 5441 | goto out; |
| 5442 | } |
| 5443 | |
| 5444 | /* |
| 5445 | * Handle special case where the right side has no extents at all. |
| 5446 | */ |
| 5447 | eb = path->nodes[0]; |
| 5448 | slot = path->slots[0]; |
| 5449 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 5450 | if (found_key.objectid != key.objectid || |
| 5451 | found_key.type != key.type) { |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 5452 | /* If we're a hole then just pretend nothing changed */ |
| 5453 | ret = (left_disknr) ? 0 : 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5454 | goto out; |
| 5455 | } |
| 5456 | |
| 5457 | /* |
| 5458 | * We're now on 2a, 2b or 7. |
| 5459 | */ |
| 5460 | key = found_key; |
| 5461 | while (key.offset < ekey->offset + left_len) { |
| 5462 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 5463 | right_type = btrfs_file_extent_type(eb, ei); |
Filipe Manana | e1cbfd7 | 2017-04-04 20:31:00 +0100 | [diff] [blame] | 5464 | if (right_type != BTRFS_FILE_EXTENT_REG && |
| 5465 | right_type != BTRFS_FILE_EXTENT_INLINE) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5466 | ret = 0; |
| 5467 | goto out; |
| 5468 | } |
| 5469 | |
Filipe Manana | e1cbfd7 | 2017-04-04 20:31:00 +0100 | [diff] [blame] | 5470 | if (right_type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 5471 | right_len = btrfs_file_extent_ram_bytes(eb, ei); |
Filipe Manana | e1cbfd7 | 2017-04-04 20:31:00 +0100 | [diff] [blame] | 5472 | right_len = PAGE_ALIGN(right_len); |
| 5473 | } else { |
| 5474 | right_len = btrfs_file_extent_num_bytes(eb, ei); |
| 5475 | } |
Josef Bacik | 007d31f | 2013-10-31 16:49:02 -0400 | [diff] [blame] | 5476 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5477 | /* |
| 5478 | * Are we at extent 8? If yes, we know the extent is changed. |
| 5479 | * This may only happen on the first iteration. |
| 5480 | */ |
Alexander Block | d8347fa | 2012-08-01 12:49:15 +0200 | [diff] [blame] | 5481 | if (found_key.offset + right_len <= ekey->offset) { |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 5482 | /* If we're a hole just pretend nothing changed */ |
| 5483 | ret = (left_disknr) ? 0 : 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5484 | goto out; |
| 5485 | } |
| 5486 | |
Filipe Manana | e1cbfd7 | 2017-04-04 20:31:00 +0100 | [diff] [blame] | 5487 | /* |
| 5488 | * We just wanted to see if when we have an inline extent, what |
| 5489 | * follows it is a regular extent (wanted to check the above |
| 5490 | * condition for inline extents too). This should normally not |
| 5491 | * happen but it's possible for example when we have an inline |
| 5492 | * compressed extent representing data with a size matching |
| 5493 | * the page size (currently the same as sector size). |
| 5494 | */ |
| 5495 | if (right_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5496 | ret = 0; |
| 5497 | goto out; |
| 5498 | } |
| 5499 | |
Filipe Manana | 24e52b1 | 2017-07-06 15:31:46 +0100 | [diff] [blame] | 5500 | right_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
| 5501 | right_offset = btrfs_file_extent_offset(eb, ei); |
| 5502 | right_gen = btrfs_file_extent_generation(eb, ei); |
| 5503 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5504 | left_offset_fixed = left_offset; |
| 5505 | if (key.offset < ekey->offset) { |
| 5506 | /* Fix the right offset for 2a and 7. */ |
| 5507 | right_offset += ekey->offset - key.offset; |
| 5508 | } else { |
| 5509 | /* Fix the left offset for all behind 2a and 2b */ |
| 5510 | left_offset_fixed += key.offset - ekey->offset; |
| 5511 | } |
| 5512 | |
| 5513 | /* |
| 5514 | * Check if we have the same extent. |
| 5515 | */ |
Alexander Block | 3954096 | 2012-08-01 12:46:05 +0200 | [diff] [blame] | 5516 | if (left_disknr != right_disknr || |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5517 | left_offset_fixed != right_offset || |
| 5518 | left_gen != right_gen) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5519 | ret = 0; |
| 5520 | goto out; |
| 5521 | } |
| 5522 | |
| 5523 | /* |
| 5524 | * Go to the next extent. |
| 5525 | */ |
| 5526 | ret = btrfs_next_item(sctx->parent_root, path); |
| 5527 | if (ret < 0) |
| 5528 | goto out; |
| 5529 | if (!ret) { |
| 5530 | eb = path->nodes[0]; |
| 5531 | slot = path->slots[0]; |
| 5532 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 5533 | } |
| 5534 | if (ret || found_key.objectid != key.objectid || |
| 5535 | found_key.type != key.type) { |
| 5536 | key.offset += right_len; |
| 5537 | break; |
Jan Schmidt | adaa4b8 | 2013-03-21 14:30:23 +0000 | [diff] [blame] | 5538 | } |
| 5539 | if (found_key.offset != key.offset + right_len) { |
| 5540 | ret = 0; |
| 5541 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5542 | } |
| 5543 | key = found_key; |
| 5544 | } |
| 5545 | |
| 5546 | /* |
| 5547 | * We're now behind the left extent (treat as unchanged) or at the end |
| 5548 | * of the right side (treat as changed). |
| 5549 | */ |
| 5550 | if (key.offset >= ekey->offset + left_len) |
| 5551 | ret = 1; |
| 5552 | else |
| 5553 | ret = 0; |
| 5554 | |
| 5555 | |
| 5556 | out: |
| 5557 | btrfs_free_path(path); |
| 5558 | return ret; |
| 5559 | } |
| 5560 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5561 | static int get_last_extent(struct send_ctx *sctx, u64 offset) |
| 5562 | { |
| 5563 | struct btrfs_path *path; |
| 5564 | struct btrfs_root *root = sctx->send_root; |
| 5565 | struct btrfs_file_extent_item *fi; |
| 5566 | struct btrfs_key key; |
| 5567 | u64 extent_end; |
| 5568 | u8 type; |
| 5569 | int ret; |
| 5570 | |
| 5571 | path = alloc_path_for_send(); |
| 5572 | if (!path) |
| 5573 | return -ENOMEM; |
| 5574 | |
| 5575 | sctx->cur_inode_last_extent = 0; |
| 5576 | |
| 5577 | key.objectid = sctx->cur_ino; |
| 5578 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5579 | key.offset = offset; |
| 5580 | ret = btrfs_search_slot_for_read(root, &key, path, 0, 1); |
| 5581 | if (ret < 0) |
| 5582 | goto out; |
| 5583 | ret = 0; |
| 5584 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 5585 | if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY) |
| 5586 | goto out; |
| 5587 | |
| 5588 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5589 | struct btrfs_file_extent_item); |
| 5590 | type = btrfs_file_extent_type(path->nodes[0], fi); |
| 5591 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 5592 | u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5593 | extent_end = ALIGN(key.offset + size, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5594 | sctx->send_root->fs_info->sectorsize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5595 | } else { |
| 5596 | extent_end = key.offset + |
| 5597 | btrfs_file_extent_num_bytes(path->nodes[0], fi); |
| 5598 | } |
| 5599 | sctx->cur_inode_last_extent = extent_end; |
| 5600 | out: |
| 5601 | btrfs_free_path(path); |
| 5602 | return ret; |
| 5603 | } |
| 5604 | |
Filipe Manana | 82bfb2e | 2017-02-14 17:56:32 +0000 | [diff] [blame] | 5605 | static int range_is_hole_in_parent(struct send_ctx *sctx, |
| 5606 | const u64 start, |
| 5607 | const u64 end) |
| 5608 | { |
| 5609 | struct btrfs_path *path; |
| 5610 | struct btrfs_key key; |
| 5611 | struct btrfs_root *root = sctx->parent_root; |
| 5612 | u64 search_start = start; |
| 5613 | int ret; |
| 5614 | |
| 5615 | path = alloc_path_for_send(); |
| 5616 | if (!path) |
| 5617 | return -ENOMEM; |
| 5618 | |
| 5619 | key.objectid = sctx->cur_ino; |
| 5620 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5621 | key.offset = search_start; |
| 5622 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5623 | if (ret < 0) |
| 5624 | goto out; |
| 5625 | if (ret > 0 && path->slots[0] > 0) |
| 5626 | path->slots[0]--; |
| 5627 | |
| 5628 | while (search_start < end) { |
| 5629 | struct extent_buffer *leaf = path->nodes[0]; |
| 5630 | int slot = path->slots[0]; |
| 5631 | struct btrfs_file_extent_item *fi; |
| 5632 | u64 extent_end; |
| 5633 | |
| 5634 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5635 | ret = btrfs_next_leaf(root, path); |
| 5636 | if (ret < 0) |
| 5637 | goto out; |
| 5638 | else if (ret > 0) |
| 5639 | break; |
| 5640 | continue; |
| 5641 | } |
| 5642 | |
| 5643 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 5644 | if (key.objectid < sctx->cur_ino || |
| 5645 | key.type < BTRFS_EXTENT_DATA_KEY) |
| 5646 | goto next; |
| 5647 | if (key.objectid > sctx->cur_ino || |
| 5648 | key.type > BTRFS_EXTENT_DATA_KEY || |
| 5649 | key.offset >= end) |
| 5650 | break; |
| 5651 | |
| 5652 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5653 | if (btrfs_file_extent_type(leaf, fi) == |
| 5654 | BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 5655 | u64 size = btrfs_file_extent_ram_bytes(leaf, fi); |
Filipe Manana | 82bfb2e | 2017-02-14 17:56:32 +0000 | [diff] [blame] | 5656 | |
| 5657 | extent_end = ALIGN(key.offset + size, |
| 5658 | root->fs_info->sectorsize); |
| 5659 | } else { |
| 5660 | extent_end = key.offset + |
| 5661 | btrfs_file_extent_num_bytes(leaf, fi); |
| 5662 | } |
| 5663 | if (extent_end <= start) |
| 5664 | goto next; |
| 5665 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) { |
| 5666 | search_start = extent_end; |
| 5667 | goto next; |
| 5668 | } |
| 5669 | ret = 0; |
| 5670 | goto out; |
| 5671 | next: |
| 5672 | path->slots[0]++; |
| 5673 | } |
| 5674 | ret = 1; |
| 5675 | out: |
| 5676 | btrfs_free_path(path); |
| 5677 | return ret; |
| 5678 | } |
| 5679 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5680 | static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path, |
| 5681 | struct btrfs_key *key) |
| 5682 | { |
| 5683 | struct btrfs_file_extent_item *fi; |
| 5684 | u64 extent_end; |
| 5685 | u8 type; |
| 5686 | int ret = 0; |
| 5687 | |
| 5688 | if (sctx->cur_ino != key->objectid || !need_send_hole(sctx)) |
| 5689 | return 0; |
| 5690 | |
| 5691 | if (sctx->cur_inode_last_extent == (u64)-1) { |
| 5692 | ret = get_last_extent(sctx, key->offset - 1); |
| 5693 | if (ret) |
| 5694 | return ret; |
| 5695 | } |
| 5696 | |
| 5697 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5698 | struct btrfs_file_extent_item); |
| 5699 | type = btrfs_file_extent_type(path->nodes[0], fi); |
| 5700 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 5701 | u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5702 | extent_end = ALIGN(key->offset + size, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5703 | sctx->send_root->fs_info->sectorsize); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5704 | } else { |
| 5705 | extent_end = key->offset + |
| 5706 | btrfs_file_extent_num_bytes(path->nodes[0], fi); |
| 5707 | } |
Filipe David Borba Manana | bf54f41 | 2014-01-28 01:38:06 +0000 | [diff] [blame] | 5708 | |
| 5709 | if (path->slots[0] == 0 && |
| 5710 | sctx->cur_inode_last_extent < key->offset) { |
| 5711 | /* |
| 5712 | * We might have skipped entire leafs that contained only |
| 5713 | * file extent items for our current inode. These leafs have |
| 5714 | * a generation number smaller (older) than the one in the |
| 5715 | * current leaf and the leaf our last extent came from, and |
| 5716 | * are located between these 2 leafs. |
| 5717 | */ |
| 5718 | ret = get_last_extent(sctx, key->offset - 1); |
| 5719 | if (ret) |
| 5720 | return ret; |
| 5721 | } |
| 5722 | |
Filipe Manana | 82bfb2e | 2017-02-14 17:56:32 +0000 | [diff] [blame] | 5723 | if (sctx->cur_inode_last_extent < key->offset) { |
| 5724 | ret = range_is_hole_in_parent(sctx, |
| 5725 | sctx->cur_inode_last_extent, |
| 5726 | key->offset); |
| 5727 | if (ret < 0) |
| 5728 | return ret; |
| 5729 | else if (ret == 0) |
| 5730 | ret = send_hole(sctx, key->offset); |
| 5731 | else |
| 5732 | ret = 0; |
| 5733 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5734 | sctx->cur_inode_last_extent = extent_end; |
| 5735 | return ret; |
| 5736 | } |
| 5737 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5738 | static int process_extent(struct send_ctx *sctx, |
| 5739 | struct btrfs_path *path, |
| 5740 | struct btrfs_key *key) |
| 5741 | { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5742 | struct clone_root *found_clone = NULL; |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 5743 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5744 | |
| 5745 | if (S_ISLNK(sctx->cur_inode_mode)) |
| 5746 | return 0; |
| 5747 | |
| 5748 | if (sctx->parent_root && !sctx->cur_inode_new) { |
| 5749 | ret = is_extent_unchanged(sctx, path, key); |
| 5750 | if (ret < 0) |
| 5751 | goto out; |
| 5752 | if (ret) { |
| 5753 | ret = 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5754 | goto out_hole; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5755 | } |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 5756 | } else { |
| 5757 | struct btrfs_file_extent_item *ei; |
| 5758 | u8 type; |
| 5759 | |
| 5760 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5761 | struct btrfs_file_extent_item); |
| 5762 | type = btrfs_file_extent_type(path->nodes[0], ei); |
| 5763 | if (type == BTRFS_FILE_EXTENT_PREALLOC || |
| 5764 | type == BTRFS_FILE_EXTENT_REG) { |
| 5765 | /* |
| 5766 | * The send spec does not have a prealloc command yet, |
| 5767 | * so just leave a hole for prealloc'ed extents until |
| 5768 | * we have enough commands queued up to justify rev'ing |
| 5769 | * the send spec. |
| 5770 | */ |
| 5771 | if (type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 5772 | ret = 0; |
| 5773 | goto out; |
| 5774 | } |
| 5775 | |
| 5776 | /* Have a hole, just skip it. */ |
| 5777 | if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) { |
| 5778 | ret = 0; |
| 5779 | goto out; |
| 5780 | } |
| 5781 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5782 | } |
| 5783 | |
| 5784 | ret = find_extent_clone(sctx, path, key->objectid, key->offset, |
| 5785 | sctx->cur_inode_size, &found_clone); |
| 5786 | if (ret != -ENOENT && ret < 0) |
| 5787 | goto out; |
| 5788 | |
| 5789 | ret = send_write_or_clone(sctx, path, key, found_clone); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5790 | if (ret) |
| 5791 | goto out; |
| 5792 | out_hole: |
| 5793 | ret = maybe_send_hole(sctx, path, key); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5794 | out: |
| 5795 | return ret; |
| 5796 | } |
| 5797 | |
| 5798 | static int process_all_extents(struct send_ctx *sctx) |
| 5799 | { |
| 5800 | int ret; |
| 5801 | struct btrfs_root *root; |
| 5802 | struct btrfs_path *path; |
| 5803 | struct btrfs_key key; |
| 5804 | struct btrfs_key found_key; |
| 5805 | struct extent_buffer *eb; |
| 5806 | int slot; |
| 5807 | |
| 5808 | root = sctx->send_root; |
| 5809 | path = alloc_path_for_send(); |
| 5810 | if (!path) |
| 5811 | return -ENOMEM; |
| 5812 | |
| 5813 | key.objectid = sctx->cmp_key->objectid; |
| 5814 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5815 | key.offset = 0; |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 5816 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5817 | if (ret < 0) |
| 5818 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5819 | |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 5820 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5821 | eb = path->nodes[0]; |
| 5822 | slot = path->slots[0]; |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 5823 | |
| 5824 | if (slot >= btrfs_header_nritems(eb)) { |
| 5825 | ret = btrfs_next_leaf(root, path); |
| 5826 | if (ret < 0) { |
| 5827 | goto out; |
| 5828 | } else if (ret > 0) { |
| 5829 | ret = 0; |
| 5830 | break; |
| 5831 | } |
| 5832 | continue; |
| 5833 | } |
| 5834 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5835 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 5836 | |
| 5837 | if (found_key.objectid != key.objectid || |
| 5838 | found_key.type != key.type) { |
| 5839 | ret = 0; |
| 5840 | goto out; |
| 5841 | } |
| 5842 | |
| 5843 | ret = process_extent(sctx, path, &found_key); |
| 5844 | if (ret < 0) |
| 5845 | goto out; |
| 5846 | |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 5847 | path->slots[0]++; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5848 | } |
| 5849 | |
| 5850 | out: |
| 5851 | btrfs_free_path(path); |
| 5852 | return ret; |
| 5853 | } |
| 5854 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5855 | static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end, |
| 5856 | int *pending_move, |
| 5857 | int *refs_processed) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5858 | { |
| 5859 | int ret = 0; |
| 5860 | |
| 5861 | if (sctx->cur_ino == 0) |
| 5862 | goto out; |
| 5863 | if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 5864 | sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5865 | goto out; |
| 5866 | if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) |
| 5867 | goto out; |
| 5868 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5869 | ret = process_recorded_refs(sctx, pending_move); |
Alexander Block | e479d9b | 2012-07-28 16:09:35 +0200 | [diff] [blame] | 5870 | if (ret < 0) |
| 5871 | goto out; |
| 5872 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5873 | *refs_processed = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5874 | out: |
| 5875 | return ret; |
| 5876 | } |
| 5877 | |
| 5878 | static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) |
| 5879 | { |
| 5880 | int ret = 0; |
| 5881 | u64 left_mode; |
| 5882 | u64 left_uid; |
| 5883 | u64 left_gid; |
| 5884 | u64 right_mode; |
| 5885 | u64 right_uid; |
| 5886 | u64 right_gid; |
| 5887 | int need_chmod = 0; |
| 5888 | int need_chown = 0; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5889 | int need_truncate = 1; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5890 | int pending_move = 0; |
| 5891 | int refs_processed = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5892 | |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 5893 | if (sctx->ignore_cur_inode) |
| 5894 | return 0; |
| 5895 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5896 | ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move, |
| 5897 | &refs_processed); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5898 | if (ret < 0) |
| 5899 | goto out; |
| 5900 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5901 | /* |
| 5902 | * We have processed the refs and thus need to advance send_progress. |
| 5903 | * Now, calls to get_cur_xxx will take the updated refs of the current |
| 5904 | * inode into account. |
| 5905 | * |
| 5906 | * On the other hand, if our current inode is a directory and couldn't |
| 5907 | * be moved/renamed because its parent was renamed/moved too and it has |
| 5908 | * a higher inode number, we can only move/rename our current inode |
| 5909 | * after we moved/renamed its parent. Therefore in this case operate on |
| 5910 | * the old path (pre move/rename) of our current inode, and the |
| 5911 | * move/rename will be performed later. |
| 5912 | */ |
| 5913 | if (refs_processed && !pending_move) |
| 5914 | sctx->send_progress = sctx->cur_ino + 1; |
| 5915 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5916 | if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) |
| 5917 | goto out; |
| 5918 | if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) |
| 5919 | goto out; |
| 5920 | |
| 5921 | ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 5922 | &left_mode, &left_uid, &left_gid, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5923 | if (ret < 0) |
| 5924 | goto out; |
| 5925 | |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5926 | if (!sctx->parent_root || sctx->cur_inode_new) { |
| 5927 | need_chown = 1; |
| 5928 | if (!S_ISLNK(sctx->cur_inode_mode)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5929 | need_chmod = 1; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5930 | if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size) |
| 5931 | need_truncate = 0; |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5932 | } else { |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5933 | u64 old_size; |
| 5934 | |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5935 | ret = get_inode_info(sctx->parent_root, sctx->cur_ino, |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5936 | &old_size, NULL, &right_mode, &right_uid, |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5937 | &right_gid, NULL); |
| 5938 | if (ret < 0) |
| 5939 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5940 | |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 5941 | if (left_uid != right_uid || left_gid != right_gid) |
| 5942 | need_chown = 1; |
| 5943 | if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode) |
| 5944 | need_chmod = 1; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5945 | if ((old_size == sctx->cur_inode_size) || |
| 5946 | (sctx->cur_inode_size > old_size && |
| 5947 | sctx->cur_inode_next_write_offset == sctx->cur_inode_size)) |
| 5948 | need_truncate = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5949 | } |
| 5950 | |
| 5951 | if (S_ISREG(sctx->cur_inode_mode)) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5952 | if (need_send_hole(sctx)) { |
Filipe Manana | 766b5e5 | 2014-03-30 23:02:53 +0100 | [diff] [blame] | 5953 | if (sctx->cur_inode_last_extent == (u64)-1 || |
| 5954 | sctx->cur_inode_last_extent < |
| 5955 | sctx->cur_inode_size) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5956 | ret = get_last_extent(sctx, (u64)-1); |
| 5957 | if (ret) |
| 5958 | goto out; |
| 5959 | } |
| 5960 | if (sctx->cur_inode_last_extent < |
| 5961 | sctx->cur_inode_size) { |
| 5962 | ret = send_hole(sctx, sctx->cur_inode_size); |
| 5963 | if (ret) |
| 5964 | goto out; |
| 5965 | } |
| 5966 | } |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 5967 | if (need_truncate) { |
| 5968 | ret = send_truncate(sctx, sctx->cur_ino, |
| 5969 | sctx->cur_inode_gen, |
| 5970 | sctx->cur_inode_size); |
| 5971 | if (ret < 0) |
| 5972 | goto out; |
| 5973 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5974 | } |
| 5975 | |
| 5976 | if (need_chown) { |
| 5977 | ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 5978 | left_uid, left_gid); |
| 5979 | if (ret < 0) |
| 5980 | goto out; |
| 5981 | } |
| 5982 | if (need_chmod) { |
| 5983 | ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 5984 | left_mode); |
| 5985 | if (ret < 0) |
| 5986 | goto out; |
| 5987 | } |
| 5988 | |
| 5989 | /* |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5990 | * If other directory inodes depended on our current directory |
| 5991 | * inode's move/rename, now do their move/rename operations. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5992 | */ |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5993 | if (!is_waiting_for_move(sctx, sctx->cur_ino)) { |
| 5994 | ret = apply_children_dir_moves(sctx); |
| 5995 | if (ret) |
| 5996 | goto out; |
Filipe Manana | fcbd215 | 2014-03-03 12:28:40 +0000 | [diff] [blame] | 5997 | /* |
| 5998 | * Need to send that every time, no matter if it actually |
| 5999 | * changed between the two trees as we have done changes to |
| 6000 | * the inode before. If our inode is a directory and it's |
| 6001 | * waiting to be moved/renamed, we will send its utimes when |
| 6002 | * it's moved/renamed, therefore we don't need to do it here. |
| 6003 | */ |
| 6004 | sctx->send_progress = sctx->cur_ino + 1; |
| 6005 | ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); |
| 6006 | if (ret < 0) |
| 6007 | goto out; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 6008 | } |
| 6009 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6010 | out: |
| 6011 | return ret; |
| 6012 | } |
| 6013 | |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6014 | struct parent_paths_ctx { |
| 6015 | struct list_head *refs; |
| 6016 | struct send_ctx *sctx; |
| 6017 | }; |
| 6018 | |
| 6019 | static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name, |
| 6020 | void *ctx) |
| 6021 | { |
| 6022 | struct parent_paths_ctx *ppctx = ctx; |
| 6023 | |
| 6024 | return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx, |
| 6025 | ppctx->refs); |
| 6026 | } |
| 6027 | |
| 6028 | /* |
| 6029 | * Issue unlink operations for all paths of the current inode found in the |
| 6030 | * parent snapshot. |
| 6031 | */ |
| 6032 | static int btrfs_unlink_all_paths(struct send_ctx *sctx) |
| 6033 | { |
| 6034 | LIST_HEAD(deleted_refs); |
| 6035 | struct btrfs_path *path; |
| 6036 | struct btrfs_key key; |
| 6037 | struct parent_paths_ctx ctx; |
| 6038 | int ret; |
| 6039 | |
| 6040 | path = alloc_path_for_send(); |
| 6041 | if (!path) |
| 6042 | return -ENOMEM; |
| 6043 | |
| 6044 | key.objectid = sctx->cur_ino; |
| 6045 | key.type = BTRFS_INODE_REF_KEY; |
| 6046 | key.offset = 0; |
| 6047 | ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); |
| 6048 | if (ret < 0) |
| 6049 | goto out; |
| 6050 | |
| 6051 | ctx.refs = &deleted_refs; |
| 6052 | ctx.sctx = sctx; |
| 6053 | |
| 6054 | while (true) { |
| 6055 | struct extent_buffer *eb = path->nodes[0]; |
| 6056 | int slot = path->slots[0]; |
| 6057 | |
| 6058 | if (slot >= btrfs_header_nritems(eb)) { |
| 6059 | ret = btrfs_next_leaf(sctx->parent_root, path); |
| 6060 | if (ret < 0) |
| 6061 | goto out; |
| 6062 | else if (ret > 0) |
| 6063 | break; |
| 6064 | continue; |
| 6065 | } |
| 6066 | |
| 6067 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 6068 | if (key.objectid != sctx->cur_ino) |
| 6069 | break; |
| 6070 | if (key.type != BTRFS_INODE_REF_KEY && |
| 6071 | key.type != BTRFS_INODE_EXTREF_KEY) |
| 6072 | break; |
| 6073 | |
| 6074 | ret = iterate_inode_ref(sctx->parent_root, path, &key, 1, |
| 6075 | record_parent_ref, &ctx); |
| 6076 | if (ret < 0) |
| 6077 | goto out; |
| 6078 | |
| 6079 | path->slots[0]++; |
| 6080 | } |
| 6081 | |
| 6082 | while (!list_empty(&deleted_refs)) { |
| 6083 | struct recorded_ref *ref; |
| 6084 | |
| 6085 | ref = list_first_entry(&deleted_refs, struct recorded_ref, list); |
| 6086 | ret = send_unlink(sctx, ref->full_path); |
| 6087 | if (ret < 0) |
| 6088 | goto out; |
| 6089 | fs_path_free(ref->full_path); |
| 6090 | list_del(&ref->list); |
| 6091 | kfree(ref); |
| 6092 | } |
| 6093 | ret = 0; |
| 6094 | out: |
| 6095 | btrfs_free_path(path); |
| 6096 | if (ret) |
| 6097 | __free_recorded_refs(&deleted_refs); |
| 6098 | return ret; |
| 6099 | } |
| 6100 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6101 | static int changed_inode(struct send_ctx *sctx, |
| 6102 | enum btrfs_compare_tree_result result) |
| 6103 | { |
| 6104 | int ret = 0; |
| 6105 | struct btrfs_key *key = sctx->cmp_key; |
| 6106 | struct btrfs_inode_item *left_ii = NULL; |
| 6107 | struct btrfs_inode_item *right_ii = NULL; |
| 6108 | u64 left_gen = 0; |
| 6109 | u64 right_gen = 0; |
| 6110 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6111 | sctx->cur_ino = key->objectid; |
| 6112 | sctx->cur_inode_new_gen = 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 6113 | sctx->cur_inode_last_extent = (u64)-1; |
Filipe Manana | ffa7c42 | 2018-02-06 20:40:40 +0000 | [diff] [blame] | 6114 | sctx->cur_inode_next_write_offset = 0; |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6115 | sctx->ignore_cur_inode = false; |
Alexander Block | e479d9b | 2012-07-28 16:09:35 +0200 | [diff] [blame] | 6116 | |
| 6117 | /* |
| 6118 | * Set send_progress to current inode. This will tell all get_cur_xxx |
| 6119 | * functions that the current inode's refs are not updated yet. Later, |
| 6120 | * when process_recorded_refs is finished, it is set to cur_ino + 1. |
| 6121 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6122 | sctx->send_progress = sctx->cur_ino; |
| 6123 | |
| 6124 | if (result == BTRFS_COMPARE_TREE_NEW || |
| 6125 | result == BTRFS_COMPARE_TREE_CHANGED) { |
| 6126 | left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], |
| 6127 | sctx->left_path->slots[0], |
| 6128 | struct btrfs_inode_item); |
| 6129 | left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], |
| 6130 | left_ii); |
| 6131 | } else { |
| 6132 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], |
| 6133 | sctx->right_path->slots[0], |
| 6134 | struct btrfs_inode_item); |
| 6135 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], |
| 6136 | right_ii); |
| 6137 | } |
| 6138 | if (result == BTRFS_COMPARE_TREE_CHANGED) { |
| 6139 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], |
| 6140 | sctx->right_path->slots[0], |
| 6141 | struct btrfs_inode_item); |
| 6142 | |
| 6143 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], |
| 6144 | right_ii); |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 6145 | |
| 6146 | /* |
| 6147 | * The cur_ino = root dir case is special here. We can't treat |
| 6148 | * the inode as deleted+reused because it would generate a |
| 6149 | * stream that tries to delete/mkdir the root dir. |
| 6150 | */ |
| 6151 | if (left_gen != right_gen && |
| 6152 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6153 | sctx->cur_inode_new_gen = 1; |
| 6154 | } |
| 6155 | |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6156 | /* |
| 6157 | * Normally we do not find inodes with a link count of zero (orphans) |
| 6158 | * because the most common case is to create a snapshot and use it |
| 6159 | * for a send operation. However other less common use cases involve |
| 6160 | * using a subvolume and send it after turning it to RO mode just |
| 6161 | * after deleting all hard links of a file while holding an open |
| 6162 | * file descriptor against it or turning a RO snapshot into RW mode, |
| 6163 | * keep an open file descriptor against a file, delete it and then |
| 6164 | * turn the snapshot back to RO mode before using it for a send |
| 6165 | * operation. So if we find such cases, ignore the inode and all its |
| 6166 | * items completely if it's a new inode, or if it's a changed inode |
| 6167 | * make sure all its previous paths (from the parent snapshot) are all |
| 6168 | * unlinked and all other the inode items are ignored. |
| 6169 | */ |
| 6170 | if (result == BTRFS_COMPARE_TREE_NEW || |
| 6171 | result == BTRFS_COMPARE_TREE_CHANGED) { |
| 6172 | u32 nlinks; |
| 6173 | |
| 6174 | nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii); |
| 6175 | if (nlinks == 0) { |
| 6176 | sctx->ignore_cur_inode = true; |
| 6177 | if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 6178 | ret = btrfs_unlink_all_paths(sctx); |
| 6179 | goto out; |
| 6180 | } |
| 6181 | } |
| 6182 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6183 | if (result == BTRFS_COMPARE_TREE_NEW) { |
| 6184 | sctx->cur_inode_gen = left_gen; |
| 6185 | sctx->cur_inode_new = 1; |
| 6186 | sctx->cur_inode_deleted = 0; |
| 6187 | sctx->cur_inode_size = btrfs_inode_size( |
| 6188 | sctx->left_path->nodes[0], left_ii); |
| 6189 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6190 | sctx->left_path->nodes[0], left_ii); |
Liu Bo | 644d194 | 2014-02-27 17:29:01 +0800 | [diff] [blame] | 6191 | sctx->cur_inode_rdev = btrfs_inode_rdev( |
| 6192 | sctx->left_path->nodes[0], left_ii); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6193 | if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 6194 | ret = send_create_inode_if_needed(sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6195 | } else if (result == BTRFS_COMPARE_TREE_DELETED) { |
| 6196 | sctx->cur_inode_gen = right_gen; |
| 6197 | sctx->cur_inode_new = 0; |
| 6198 | sctx->cur_inode_deleted = 1; |
| 6199 | sctx->cur_inode_size = btrfs_inode_size( |
| 6200 | sctx->right_path->nodes[0], right_ii); |
| 6201 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6202 | sctx->right_path->nodes[0], right_ii); |
| 6203 | } else if (result == BTRFS_COMPARE_TREE_CHANGED) { |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6204 | /* |
| 6205 | * We need to do some special handling in case the inode was |
| 6206 | * reported as changed with a changed generation number. This |
| 6207 | * means that the original inode was deleted and new inode |
| 6208 | * reused the same inum. So we have to treat the old inode as |
| 6209 | * deleted and the new one as new. |
| 6210 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6211 | if (sctx->cur_inode_new_gen) { |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6212 | /* |
| 6213 | * First, process the inode as if it was deleted. |
| 6214 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6215 | sctx->cur_inode_gen = right_gen; |
| 6216 | sctx->cur_inode_new = 0; |
| 6217 | sctx->cur_inode_deleted = 1; |
| 6218 | sctx->cur_inode_size = btrfs_inode_size( |
| 6219 | sctx->right_path->nodes[0], right_ii); |
| 6220 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6221 | sctx->right_path->nodes[0], right_ii); |
| 6222 | ret = process_all_refs(sctx, |
| 6223 | BTRFS_COMPARE_TREE_DELETED); |
| 6224 | if (ret < 0) |
| 6225 | goto out; |
| 6226 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6227 | /* |
| 6228 | * Now process the inode as if it was new. |
| 6229 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6230 | sctx->cur_inode_gen = left_gen; |
| 6231 | sctx->cur_inode_new = 1; |
| 6232 | sctx->cur_inode_deleted = 0; |
| 6233 | sctx->cur_inode_size = btrfs_inode_size( |
| 6234 | sctx->left_path->nodes[0], left_ii); |
| 6235 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6236 | sctx->left_path->nodes[0], left_ii); |
Liu Bo | 644d194 | 2014-02-27 17:29:01 +0800 | [diff] [blame] | 6237 | sctx->cur_inode_rdev = btrfs_inode_rdev( |
| 6238 | sctx->left_path->nodes[0], left_ii); |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 6239 | ret = send_create_inode_if_needed(sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6240 | if (ret < 0) |
| 6241 | goto out; |
| 6242 | |
| 6243 | ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); |
| 6244 | if (ret < 0) |
| 6245 | goto out; |
Alexander Block | e479d9b | 2012-07-28 16:09:35 +0200 | [diff] [blame] | 6246 | /* |
| 6247 | * Advance send_progress now as we did not get into |
| 6248 | * process_recorded_refs_if_needed in the new_gen case. |
| 6249 | */ |
| 6250 | sctx->send_progress = sctx->cur_ino + 1; |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6251 | |
| 6252 | /* |
| 6253 | * Now process all extents and xattrs of the inode as if |
| 6254 | * they were all new. |
| 6255 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6256 | ret = process_all_extents(sctx); |
| 6257 | if (ret < 0) |
| 6258 | goto out; |
| 6259 | ret = process_all_new_xattrs(sctx); |
| 6260 | if (ret < 0) |
| 6261 | goto out; |
| 6262 | } else { |
| 6263 | sctx->cur_inode_gen = left_gen; |
| 6264 | sctx->cur_inode_new = 0; |
| 6265 | sctx->cur_inode_new_gen = 0; |
| 6266 | sctx->cur_inode_deleted = 0; |
| 6267 | sctx->cur_inode_size = btrfs_inode_size( |
| 6268 | sctx->left_path->nodes[0], left_ii); |
| 6269 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 6270 | sctx->left_path->nodes[0], left_ii); |
| 6271 | } |
| 6272 | } |
| 6273 | |
| 6274 | out: |
| 6275 | return ret; |
| 6276 | } |
| 6277 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6278 | /* |
| 6279 | * We have to process new refs before deleted refs, but compare_trees gives us |
| 6280 | * the new and deleted refs mixed. To fix this, we record the new/deleted refs |
| 6281 | * first and later process them in process_recorded_refs. |
| 6282 | * For the cur_inode_new_gen case, we skip recording completely because |
| 6283 | * changed_inode did already initiate processing of refs. The reason for this is |
| 6284 | * that in this case, compare_tree actually compares the refs of 2 different |
| 6285 | * inodes. To fix this, process_all_refs is used in changed_inode to handle all |
| 6286 | * refs of the right tree as deleted and all refs of the left tree as new. |
| 6287 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6288 | static int changed_ref(struct send_ctx *sctx, |
| 6289 | enum btrfs_compare_tree_result result) |
| 6290 | { |
| 6291 | int ret = 0; |
| 6292 | |
Filipe Manana | 9515558 | 2016-08-01 01:50:37 +0100 | [diff] [blame] | 6293 | if (sctx->cur_ino != sctx->cmp_key->objectid) { |
| 6294 | inconsistent_snapshot_error(sctx, result, "reference"); |
| 6295 | return -EIO; |
| 6296 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6297 | |
| 6298 | if (!sctx->cur_inode_new_gen && |
| 6299 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 6300 | if (result == BTRFS_COMPARE_TREE_NEW) |
| 6301 | ret = record_new_ref(sctx); |
| 6302 | else if (result == BTRFS_COMPARE_TREE_DELETED) |
| 6303 | ret = record_deleted_ref(sctx); |
| 6304 | else if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 6305 | ret = record_changed_ref(sctx); |
| 6306 | } |
| 6307 | |
| 6308 | return ret; |
| 6309 | } |
| 6310 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6311 | /* |
| 6312 | * Process new/deleted/changed xattrs. We skip processing in the |
| 6313 | * cur_inode_new_gen case because changed_inode did already initiate processing |
| 6314 | * of xattrs. The reason is the same as in changed_ref |
| 6315 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6316 | static int changed_xattr(struct send_ctx *sctx, |
| 6317 | enum btrfs_compare_tree_result result) |
| 6318 | { |
| 6319 | int ret = 0; |
| 6320 | |
Filipe Manana | 9515558 | 2016-08-01 01:50:37 +0100 | [diff] [blame] | 6321 | if (sctx->cur_ino != sctx->cmp_key->objectid) { |
| 6322 | inconsistent_snapshot_error(sctx, result, "xattr"); |
| 6323 | return -EIO; |
| 6324 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6325 | |
| 6326 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { |
| 6327 | if (result == BTRFS_COMPARE_TREE_NEW) |
| 6328 | ret = process_new_xattr(sctx); |
| 6329 | else if (result == BTRFS_COMPARE_TREE_DELETED) |
| 6330 | ret = process_deleted_xattr(sctx); |
| 6331 | else if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 6332 | ret = process_changed_xattr(sctx); |
| 6333 | } |
| 6334 | |
| 6335 | return ret; |
| 6336 | } |
| 6337 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6338 | /* |
| 6339 | * Process new/deleted/changed extents. We skip processing in the |
| 6340 | * cur_inode_new_gen case because changed_inode did already initiate processing |
| 6341 | * of extents. The reason is the same as in changed_ref |
| 6342 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6343 | static int changed_extent(struct send_ctx *sctx, |
| 6344 | enum btrfs_compare_tree_result result) |
| 6345 | { |
| 6346 | int ret = 0; |
| 6347 | |
Filipe Manana | b4f9a1a | 2019-07-17 13:23:39 +0100 | [diff] [blame] | 6348 | /* |
| 6349 | * We have found an extent item that changed without the inode item |
| 6350 | * having changed. This can happen either after relocation (where the |
| 6351 | * disk_bytenr of an extent item is replaced at |
| 6352 | * relocation.c:replace_file_extents()) or after deduplication into a |
| 6353 | * file in both the parent and send snapshots (where an extent item can |
| 6354 | * get modified or replaced with a new one). Note that deduplication |
| 6355 | * updates the inode item, but it only changes the iversion (sequence |
| 6356 | * field in the inode item) of the inode, so if a file is deduplicated |
| 6357 | * the same amount of times in both the parent and send snapshots, its |
| 6358 | * iversion becames the same in both snapshots, whence the inode item is |
| 6359 | * the same on both snapshots. |
| 6360 | */ |
| 6361 | if (sctx->cur_ino != sctx->cmp_key->objectid) |
| 6362 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6363 | |
| 6364 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { |
| 6365 | if (result != BTRFS_COMPARE_TREE_DELETED) |
| 6366 | ret = process_extent(sctx, sctx->left_path, |
| 6367 | sctx->cmp_key); |
| 6368 | } |
| 6369 | |
| 6370 | return ret; |
| 6371 | } |
| 6372 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 6373 | static int dir_changed(struct send_ctx *sctx, u64 dir) |
| 6374 | { |
| 6375 | u64 orig_gen, new_gen; |
| 6376 | int ret; |
| 6377 | |
| 6378 | ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL, |
| 6379 | NULL, NULL); |
| 6380 | if (ret) |
| 6381 | return ret; |
| 6382 | |
| 6383 | ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL, |
| 6384 | NULL, NULL, NULL); |
| 6385 | if (ret) |
| 6386 | return ret; |
| 6387 | |
| 6388 | return (orig_gen != new_gen) ? 1 : 0; |
| 6389 | } |
| 6390 | |
| 6391 | static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path, |
| 6392 | struct btrfs_key *key) |
| 6393 | { |
| 6394 | struct btrfs_inode_extref *extref; |
| 6395 | struct extent_buffer *leaf; |
| 6396 | u64 dirid = 0, last_dirid = 0; |
| 6397 | unsigned long ptr; |
| 6398 | u32 item_size; |
| 6399 | u32 cur_offset = 0; |
| 6400 | int ref_name_len; |
| 6401 | int ret = 0; |
| 6402 | |
| 6403 | /* Easy case, just check this one dirid */ |
| 6404 | if (key->type == BTRFS_INODE_REF_KEY) { |
| 6405 | dirid = key->offset; |
| 6406 | |
| 6407 | ret = dir_changed(sctx, dirid); |
| 6408 | goto out; |
| 6409 | } |
| 6410 | |
| 6411 | leaf = path->nodes[0]; |
| 6412 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 6413 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 6414 | while (cur_offset < item_size) { |
| 6415 | extref = (struct btrfs_inode_extref *)(ptr + |
| 6416 | cur_offset); |
| 6417 | dirid = btrfs_inode_extref_parent(leaf, extref); |
| 6418 | ref_name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 6419 | cur_offset += ref_name_len + sizeof(*extref); |
| 6420 | if (dirid == last_dirid) |
| 6421 | continue; |
| 6422 | ret = dir_changed(sctx, dirid); |
| 6423 | if (ret) |
| 6424 | break; |
| 6425 | last_dirid = dirid; |
| 6426 | } |
| 6427 | out: |
| 6428 | return ret; |
| 6429 | } |
| 6430 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 6431 | /* |
| 6432 | * Updates compare related fields in sctx and simply forwards to the actual |
| 6433 | * changed_xxx functions. |
| 6434 | */ |
Nikolay Borisov | ee8c494 | 2017-08-21 12:43:45 +0300 | [diff] [blame] | 6435 | static int changed_cb(struct btrfs_path *left_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6436 | struct btrfs_path *right_path, |
| 6437 | struct btrfs_key *key, |
| 6438 | enum btrfs_compare_tree_result result, |
| 6439 | void *ctx) |
| 6440 | { |
| 6441 | int ret = 0; |
| 6442 | struct send_ctx *sctx = ctx; |
| 6443 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 6444 | if (result == BTRFS_COMPARE_TREE_SAME) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 6445 | if (key->type == BTRFS_INODE_REF_KEY || |
| 6446 | key->type == BTRFS_INODE_EXTREF_KEY) { |
| 6447 | ret = compare_refs(sctx, left_path, key); |
| 6448 | if (!ret) |
| 6449 | return 0; |
| 6450 | if (ret < 0) |
| 6451 | return ret; |
| 6452 | } else if (key->type == BTRFS_EXTENT_DATA_KEY) { |
| 6453 | return maybe_send_hole(sctx, left_path, key); |
| 6454 | } else { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 6455 | return 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 6456 | } |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 6457 | result = BTRFS_COMPARE_TREE_CHANGED; |
| 6458 | ret = 0; |
| 6459 | } |
| 6460 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6461 | sctx->left_path = left_path; |
| 6462 | sctx->right_path = right_path; |
| 6463 | sctx->cmp_key = key; |
| 6464 | |
| 6465 | ret = finish_inode_if_needed(sctx, 0); |
| 6466 | if (ret < 0) |
| 6467 | goto out; |
| 6468 | |
Alexander Block | 2981e22 | 2012-08-01 14:47:03 +0200 | [diff] [blame] | 6469 | /* Ignore non-FS objects */ |
| 6470 | if (key->objectid == BTRFS_FREE_INO_OBJECTID || |
| 6471 | key->objectid == BTRFS_FREE_SPACE_OBJECTID) |
| 6472 | goto out; |
| 6473 | |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6474 | if (key->type == BTRFS_INODE_ITEM_KEY) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6475 | ret = changed_inode(sctx, result); |
Filipe Manana | 46b2f45 | 2018-07-24 11:54:04 +0100 | [diff] [blame] | 6476 | } else if (!sctx->ignore_cur_inode) { |
| 6477 | if (key->type == BTRFS_INODE_REF_KEY || |
| 6478 | key->type == BTRFS_INODE_EXTREF_KEY) |
| 6479 | ret = changed_ref(sctx, result); |
| 6480 | else if (key->type == BTRFS_XATTR_ITEM_KEY) |
| 6481 | ret = changed_xattr(sctx, result); |
| 6482 | else if (key->type == BTRFS_EXTENT_DATA_KEY) |
| 6483 | ret = changed_extent(sctx, result); |
| 6484 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6485 | |
| 6486 | out: |
| 6487 | return ret; |
| 6488 | } |
| 6489 | |
| 6490 | static int full_send_tree(struct send_ctx *sctx) |
| 6491 | { |
| 6492 | int ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6493 | struct btrfs_root *send_root = sctx->send_root; |
| 6494 | struct btrfs_key key; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6495 | struct btrfs_path *path; |
| 6496 | struct extent_buffer *eb; |
| 6497 | int slot; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6498 | |
| 6499 | path = alloc_path_for_send(); |
| 6500 | if (!path) |
| 6501 | return -ENOMEM; |
| 6502 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6503 | key.objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 6504 | key.type = BTRFS_INODE_ITEM_KEY; |
| 6505 | key.offset = 0; |
| 6506 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6507 | ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0); |
| 6508 | if (ret < 0) |
| 6509 | goto out; |
| 6510 | if (ret) |
| 6511 | goto out_finish; |
| 6512 | |
| 6513 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6514 | eb = path->nodes[0]; |
| 6515 | slot = path->slots[0]; |
Filipe Manana | ca5d2ba | 2018-07-23 09:10:09 +0100 | [diff] [blame] | 6516 | btrfs_item_key_to_cpu(eb, &key, slot); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6517 | |
Filipe Manana | ca5d2ba | 2018-07-23 09:10:09 +0100 | [diff] [blame] | 6518 | ret = changed_cb(path, NULL, &key, |
Nikolay Borisov | ee8c494 | 2017-08-21 12:43:45 +0300 | [diff] [blame] | 6519 | BTRFS_COMPARE_TREE_NEW, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6520 | if (ret < 0) |
| 6521 | goto out; |
| 6522 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6523 | ret = btrfs_next_item(send_root, path); |
| 6524 | if (ret < 0) |
| 6525 | goto out; |
| 6526 | if (ret) { |
| 6527 | ret = 0; |
| 6528 | break; |
| 6529 | } |
| 6530 | } |
| 6531 | |
| 6532 | out_finish: |
| 6533 | ret = finish_inode_if_needed(sctx, 1); |
| 6534 | |
| 6535 | out: |
| 6536 | btrfs_free_path(path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6537 | return ret; |
| 6538 | } |
| 6539 | |
David Sterba | 18d0f5c | 2019-08-21 19:12:59 +0200 | [diff] [blame] | 6540 | static int tree_move_down(struct btrfs_path *path, int *level) |
| 6541 | { |
| 6542 | struct extent_buffer *eb; |
| 6543 | |
| 6544 | BUG_ON(*level == 0); |
| 6545 | eb = btrfs_read_node_slot(path->nodes[*level], path->slots[*level]); |
| 6546 | if (IS_ERR(eb)) |
| 6547 | return PTR_ERR(eb); |
| 6548 | |
| 6549 | path->nodes[*level - 1] = eb; |
| 6550 | path->slots[*level - 1] = 0; |
| 6551 | (*level)--; |
| 6552 | return 0; |
| 6553 | } |
| 6554 | |
| 6555 | static int tree_move_next_or_upnext(struct btrfs_path *path, |
| 6556 | int *level, int root_level) |
| 6557 | { |
| 6558 | int ret = 0; |
| 6559 | int nritems; |
| 6560 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 6561 | |
| 6562 | path->slots[*level]++; |
| 6563 | |
| 6564 | while (path->slots[*level] >= nritems) { |
| 6565 | if (*level == root_level) |
| 6566 | return -1; |
| 6567 | |
| 6568 | /* move upnext */ |
| 6569 | path->slots[*level] = 0; |
| 6570 | free_extent_buffer(path->nodes[*level]); |
| 6571 | path->nodes[*level] = NULL; |
| 6572 | (*level)++; |
| 6573 | path->slots[*level]++; |
| 6574 | |
| 6575 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 6576 | ret = 1; |
| 6577 | } |
| 6578 | return ret; |
| 6579 | } |
| 6580 | |
| 6581 | /* |
| 6582 | * Returns 1 if it had to move up and next. 0 is returned if it moved only next |
| 6583 | * or down. |
| 6584 | */ |
| 6585 | static int tree_advance(struct btrfs_path *path, |
| 6586 | int *level, int root_level, |
| 6587 | int allow_down, |
| 6588 | struct btrfs_key *key) |
| 6589 | { |
| 6590 | int ret; |
| 6591 | |
| 6592 | if (*level == 0 || !allow_down) { |
| 6593 | ret = tree_move_next_or_upnext(path, level, root_level); |
| 6594 | } else { |
| 6595 | ret = tree_move_down(path, level); |
| 6596 | } |
| 6597 | if (ret >= 0) { |
| 6598 | if (*level == 0) |
| 6599 | btrfs_item_key_to_cpu(path->nodes[*level], key, |
| 6600 | path->slots[*level]); |
| 6601 | else |
| 6602 | btrfs_node_key_to_cpu(path->nodes[*level], key, |
| 6603 | path->slots[*level]); |
| 6604 | } |
| 6605 | return ret; |
| 6606 | } |
| 6607 | |
| 6608 | static int tree_compare_item(struct btrfs_path *left_path, |
| 6609 | struct btrfs_path *right_path, |
| 6610 | char *tmp_buf) |
| 6611 | { |
| 6612 | int cmp; |
| 6613 | int len1, len2; |
| 6614 | unsigned long off1, off2; |
| 6615 | |
| 6616 | len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); |
| 6617 | len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); |
| 6618 | if (len1 != len2) |
| 6619 | return 1; |
| 6620 | |
| 6621 | off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); |
| 6622 | off2 = btrfs_item_ptr_offset(right_path->nodes[0], |
| 6623 | right_path->slots[0]); |
| 6624 | |
| 6625 | read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); |
| 6626 | |
| 6627 | cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); |
| 6628 | if (cmp) |
| 6629 | return 1; |
| 6630 | return 0; |
| 6631 | } |
| 6632 | |
| 6633 | /* |
| 6634 | * This function compares two trees and calls the provided callback for |
| 6635 | * every changed/new/deleted item it finds. |
| 6636 | * If shared tree blocks are encountered, whole subtrees are skipped, making |
| 6637 | * the compare pretty fast on snapshotted subvolumes. |
| 6638 | * |
| 6639 | * This currently works on commit roots only. As commit roots are read only, |
| 6640 | * we don't do any locking. The commit roots are protected with transactions. |
| 6641 | * Transactions are ended and rejoined when a commit is tried in between. |
| 6642 | * |
| 6643 | * This function checks for modifications done to the trees while comparing. |
| 6644 | * If it detects a change, it aborts immediately. |
| 6645 | */ |
| 6646 | static int btrfs_compare_trees(struct btrfs_root *left_root, |
| 6647 | struct btrfs_root *right_root, |
| 6648 | btrfs_changed_cb_t changed_cb, void *ctx) |
| 6649 | { |
| 6650 | struct btrfs_fs_info *fs_info = left_root->fs_info; |
| 6651 | int ret; |
| 6652 | int cmp; |
| 6653 | struct btrfs_path *left_path = NULL; |
| 6654 | struct btrfs_path *right_path = NULL; |
| 6655 | struct btrfs_key left_key; |
| 6656 | struct btrfs_key right_key; |
| 6657 | char *tmp_buf = NULL; |
| 6658 | int left_root_level; |
| 6659 | int right_root_level; |
| 6660 | int left_level; |
| 6661 | int right_level; |
| 6662 | int left_end_reached; |
| 6663 | int right_end_reached; |
| 6664 | int advance_left; |
| 6665 | int advance_right; |
| 6666 | u64 left_blockptr; |
| 6667 | u64 right_blockptr; |
| 6668 | u64 left_gen; |
| 6669 | u64 right_gen; |
| 6670 | |
| 6671 | left_path = btrfs_alloc_path(); |
| 6672 | if (!left_path) { |
| 6673 | ret = -ENOMEM; |
| 6674 | goto out; |
| 6675 | } |
| 6676 | right_path = btrfs_alloc_path(); |
| 6677 | if (!right_path) { |
| 6678 | ret = -ENOMEM; |
| 6679 | goto out; |
| 6680 | } |
| 6681 | |
| 6682 | tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); |
| 6683 | if (!tmp_buf) { |
| 6684 | ret = -ENOMEM; |
| 6685 | goto out; |
| 6686 | } |
| 6687 | |
| 6688 | left_path->search_commit_root = 1; |
| 6689 | left_path->skip_locking = 1; |
| 6690 | right_path->search_commit_root = 1; |
| 6691 | right_path->skip_locking = 1; |
| 6692 | |
| 6693 | /* |
| 6694 | * Strategy: Go to the first items of both trees. Then do |
| 6695 | * |
| 6696 | * If both trees are at level 0 |
| 6697 | * Compare keys of current items |
| 6698 | * If left < right treat left item as new, advance left tree |
| 6699 | * and repeat |
| 6700 | * If left > right treat right item as deleted, advance right tree |
| 6701 | * and repeat |
| 6702 | * If left == right do deep compare of items, treat as changed if |
| 6703 | * needed, advance both trees and repeat |
| 6704 | * If both trees are at the same level but not at level 0 |
| 6705 | * Compare keys of current nodes/leafs |
| 6706 | * If left < right advance left tree and repeat |
| 6707 | * If left > right advance right tree and repeat |
| 6708 | * If left == right compare blockptrs of the next nodes/leafs |
| 6709 | * If they match advance both trees but stay at the same level |
| 6710 | * and repeat |
| 6711 | * If they don't match advance both trees while allowing to go |
| 6712 | * deeper and repeat |
| 6713 | * If tree levels are different |
| 6714 | * Advance the tree that needs it and repeat |
| 6715 | * |
| 6716 | * Advancing a tree means: |
| 6717 | * If we are at level 0, try to go to the next slot. If that's not |
| 6718 | * possible, go one level up and repeat. Stop when we found a level |
| 6719 | * where we could go to the next slot. We may at this point be on a |
| 6720 | * node or a leaf. |
| 6721 | * |
| 6722 | * If we are not at level 0 and not on shared tree blocks, go one |
| 6723 | * level deeper. |
| 6724 | * |
| 6725 | * If we are not at level 0 and on shared tree blocks, go one slot to |
| 6726 | * the right if possible or go up and right. |
| 6727 | */ |
| 6728 | |
| 6729 | down_read(&fs_info->commit_root_sem); |
| 6730 | left_level = btrfs_header_level(left_root->commit_root); |
| 6731 | left_root_level = left_level; |
| 6732 | left_path->nodes[left_level] = |
| 6733 | btrfs_clone_extent_buffer(left_root->commit_root); |
| 6734 | if (!left_path->nodes[left_level]) { |
| 6735 | up_read(&fs_info->commit_root_sem); |
| 6736 | ret = -ENOMEM; |
| 6737 | goto out; |
| 6738 | } |
| 6739 | |
| 6740 | right_level = btrfs_header_level(right_root->commit_root); |
| 6741 | right_root_level = right_level; |
| 6742 | right_path->nodes[right_level] = |
| 6743 | btrfs_clone_extent_buffer(right_root->commit_root); |
| 6744 | if (!right_path->nodes[right_level]) { |
| 6745 | up_read(&fs_info->commit_root_sem); |
| 6746 | ret = -ENOMEM; |
| 6747 | goto out; |
| 6748 | } |
| 6749 | up_read(&fs_info->commit_root_sem); |
| 6750 | |
| 6751 | if (left_level == 0) |
| 6752 | btrfs_item_key_to_cpu(left_path->nodes[left_level], |
| 6753 | &left_key, left_path->slots[left_level]); |
| 6754 | else |
| 6755 | btrfs_node_key_to_cpu(left_path->nodes[left_level], |
| 6756 | &left_key, left_path->slots[left_level]); |
| 6757 | if (right_level == 0) |
| 6758 | btrfs_item_key_to_cpu(right_path->nodes[right_level], |
| 6759 | &right_key, right_path->slots[right_level]); |
| 6760 | else |
| 6761 | btrfs_node_key_to_cpu(right_path->nodes[right_level], |
| 6762 | &right_key, right_path->slots[right_level]); |
| 6763 | |
| 6764 | left_end_reached = right_end_reached = 0; |
| 6765 | advance_left = advance_right = 0; |
| 6766 | |
| 6767 | while (1) { |
Nikolay Borisov | 6af112b | 2019-09-04 19:33:58 +0300 | [diff] [blame] | 6768 | cond_resched(); |
David Sterba | 18d0f5c | 2019-08-21 19:12:59 +0200 | [diff] [blame] | 6769 | if (advance_left && !left_end_reached) { |
| 6770 | ret = tree_advance(left_path, &left_level, |
| 6771 | left_root_level, |
| 6772 | advance_left != ADVANCE_ONLY_NEXT, |
| 6773 | &left_key); |
| 6774 | if (ret == -1) |
| 6775 | left_end_reached = ADVANCE; |
| 6776 | else if (ret < 0) |
| 6777 | goto out; |
| 6778 | advance_left = 0; |
| 6779 | } |
| 6780 | if (advance_right && !right_end_reached) { |
| 6781 | ret = tree_advance(right_path, &right_level, |
| 6782 | right_root_level, |
| 6783 | advance_right != ADVANCE_ONLY_NEXT, |
| 6784 | &right_key); |
| 6785 | if (ret == -1) |
| 6786 | right_end_reached = ADVANCE; |
| 6787 | else if (ret < 0) |
| 6788 | goto out; |
| 6789 | advance_right = 0; |
| 6790 | } |
| 6791 | |
| 6792 | if (left_end_reached && right_end_reached) { |
| 6793 | ret = 0; |
| 6794 | goto out; |
| 6795 | } else if (left_end_reached) { |
| 6796 | if (right_level == 0) { |
| 6797 | ret = changed_cb(left_path, right_path, |
| 6798 | &right_key, |
| 6799 | BTRFS_COMPARE_TREE_DELETED, |
| 6800 | ctx); |
| 6801 | if (ret < 0) |
| 6802 | goto out; |
| 6803 | } |
| 6804 | advance_right = ADVANCE; |
| 6805 | continue; |
| 6806 | } else if (right_end_reached) { |
| 6807 | if (left_level == 0) { |
| 6808 | ret = changed_cb(left_path, right_path, |
| 6809 | &left_key, |
| 6810 | BTRFS_COMPARE_TREE_NEW, |
| 6811 | ctx); |
| 6812 | if (ret < 0) |
| 6813 | goto out; |
| 6814 | } |
| 6815 | advance_left = ADVANCE; |
| 6816 | continue; |
| 6817 | } |
| 6818 | |
| 6819 | if (left_level == 0 && right_level == 0) { |
| 6820 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 6821 | if (cmp < 0) { |
| 6822 | ret = changed_cb(left_path, right_path, |
| 6823 | &left_key, |
| 6824 | BTRFS_COMPARE_TREE_NEW, |
| 6825 | ctx); |
| 6826 | if (ret < 0) |
| 6827 | goto out; |
| 6828 | advance_left = ADVANCE; |
| 6829 | } else if (cmp > 0) { |
| 6830 | ret = changed_cb(left_path, right_path, |
| 6831 | &right_key, |
| 6832 | BTRFS_COMPARE_TREE_DELETED, |
| 6833 | ctx); |
| 6834 | if (ret < 0) |
| 6835 | goto out; |
| 6836 | advance_right = ADVANCE; |
| 6837 | } else { |
| 6838 | enum btrfs_compare_tree_result result; |
| 6839 | |
| 6840 | WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); |
| 6841 | ret = tree_compare_item(left_path, right_path, |
| 6842 | tmp_buf); |
| 6843 | if (ret) |
| 6844 | result = BTRFS_COMPARE_TREE_CHANGED; |
| 6845 | else |
| 6846 | result = BTRFS_COMPARE_TREE_SAME; |
| 6847 | ret = changed_cb(left_path, right_path, |
| 6848 | &left_key, result, ctx); |
| 6849 | if (ret < 0) |
| 6850 | goto out; |
| 6851 | advance_left = ADVANCE; |
| 6852 | advance_right = ADVANCE; |
| 6853 | } |
| 6854 | } else if (left_level == right_level) { |
| 6855 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 6856 | if (cmp < 0) { |
| 6857 | advance_left = ADVANCE; |
| 6858 | } else if (cmp > 0) { |
| 6859 | advance_right = ADVANCE; |
| 6860 | } else { |
| 6861 | left_blockptr = btrfs_node_blockptr( |
| 6862 | left_path->nodes[left_level], |
| 6863 | left_path->slots[left_level]); |
| 6864 | right_blockptr = btrfs_node_blockptr( |
| 6865 | right_path->nodes[right_level], |
| 6866 | right_path->slots[right_level]); |
| 6867 | left_gen = btrfs_node_ptr_generation( |
| 6868 | left_path->nodes[left_level], |
| 6869 | left_path->slots[left_level]); |
| 6870 | right_gen = btrfs_node_ptr_generation( |
| 6871 | right_path->nodes[right_level], |
| 6872 | right_path->slots[right_level]); |
| 6873 | if (left_blockptr == right_blockptr && |
| 6874 | left_gen == right_gen) { |
| 6875 | /* |
| 6876 | * As we're on a shared block, don't |
| 6877 | * allow to go deeper. |
| 6878 | */ |
| 6879 | advance_left = ADVANCE_ONLY_NEXT; |
| 6880 | advance_right = ADVANCE_ONLY_NEXT; |
| 6881 | } else { |
| 6882 | advance_left = ADVANCE; |
| 6883 | advance_right = ADVANCE; |
| 6884 | } |
| 6885 | } |
| 6886 | } else if (left_level < right_level) { |
| 6887 | advance_right = ADVANCE; |
| 6888 | } else { |
| 6889 | advance_left = ADVANCE; |
| 6890 | } |
| 6891 | } |
| 6892 | |
| 6893 | out: |
| 6894 | btrfs_free_path(left_path); |
| 6895 | btrfs_free_path(right_path); |
| 6896 | kvfree(tmp_buf); |
| 6897 | return ret; |
| 6898 | } |
| 6899 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6900 | static int send_subvol(struct send_ctx *sctx) |
| 6901 | { |
| 6902 | int ret; |
| 6903 | |
Stefan Behrens | c2c7132 | 2013-04-10 17:10:52 +0000 | [diff] [blame] | 6904 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) { |
| 6905 | ret = send_header(sctx); |
| 6906 | if (ret < 0) |
| 6907 | goto out; |
| 6908 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6909 | |
| 6910 | ret = send_subvol_begin(sctx); |
| 6911 | if (ret < 0) |
| 6912 | goto out; |
| 6913 | |
| 6914 | if (sctx->parent_root) { |
| 6915 | ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, |
| 6916 | changed_cb, sctx); |
| 6917 | if (ret < 0) |
| 6918 | goto out; |
| 6919 | ret = finish_inode_if_needed(sctx, 1); |
| 6920 | if (ret < 0) |
| 6921 | goto out; |
| 6922 | } else { |
| 6923 | ret = full_send_tree(sctx); |
| 6924 | if (ret < 0) |
| 6925 | goto out; |
| 6926 | } |
| 6927 | |
| 6928 | out: |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 6929 | free_recorded_refs(sctx); |
| 6930 | return ret; |
| 6931 | } |
| 6932 | |
Filipe Manana | e5fa8f8 | 2014-10-21 11:11:41 +0100 | [diff] [blame] | 6933 | /* |
| 6934 | * If orphan cleanup did remove any orphans from a root, it means the tree |
| 6935 | * was modified and therefore the commit root is not the same as the current |
| 6936 | * root anymore. This is a problem, because send uses the commit root and |
| 6937 | * therefore can see inode items that don't exist in the current root anymore, |
| 6938 | * and for example make calls to btrfs_iget, which will do tree lookups based |
| 6939 | * on the current root and not on the commit root. Those lookups will fail, |
| 6940 | * returning a -ESTALE error, and making send fail with that error. So make |
| 6941 | * sure a send does not see any orphans we have just removed, and that it will |
| 6942 | * see the same inodes regardless of whether a transaction commit happened |
| 6943 | * before it started (meaning that the commit root will be the same as the |
| 6944 | * current root) or not. |
| 6945 | */ |
| 6946 | static int ensure_commit_roots_uptodate(struct send_ctx *sctx) |
| 6947 | { |
| 6948 | int i; |
| 6949 | struct btrfs_trans_handle *trans = NULL; |
| 6950 | |
| 6951 | again: |
| 6952 | if (sctx->parent_root && |
| 6953 | sctx->parent_root->node != sctx->parent_root->commit_root) |
| 6954 | goto commit_trans; |
| 6955 | |
| 6956 | for (i = 0; i < sctx->clone_roots_cnt; i++) |
| 6957 | if (sctx->clone_roots[i].root->node != |
| 6958 | sctx->clone_roots[i].root->commit_root) |
| 6959 | goto commit_trans; |
| 6960 | |
| 6961 | if (trans) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6962 | return btrfs_end_transaction(trans); |
Filipe Manana | e5fa8f8 | 2014-10-21 11:11:41 +0100 | [diff] [blame] | 6963 | |
| 6964 | return 0; |
| 6965 | |
| 6966 | commit_trans: |
| 6967 | /* Use any root, all fs roots will get their commit roots updated. */ |
| 6968 | if (!trans) { |
| 6969 | trans = btrfs_join_transaction(sctx->send_root); |
| 6970 | if (IS_ERR(trans)) |
| 6971 | return PTR_ERR(trans); |
| 6972 | goto again; |
| 6973 | } |
| 6974 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6975 | return btrfs_commit_transaction(trans); |
Filipe Manana | e5fa8f8 | 2014-10-21 11:11:41 +0100 | [diff] [blame] | 6976 | } |
| 6977 | |
Filipe Manana | 9f89d5d | 2019-04-15 09:29:36 +0100 | [diff] [blame] | 6978 | /* |
| 6979 | * Make sure any existing dellaloc is flushed for any root used by a send |
| 6980 | * operation so that we do not miss any data and we do not race with writeback |
| 6981 | * finishing and changing a tree while send is using the tree. This could |
| 6982 | * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and |
| 6983 | * a send operation then uses the subvolume. |
| 6984 | * After flushing delalloc ensure_commit_roots_uptodate() must be called. |
| 6985 | */ |
| 6986 | static int flush_delalloc_roots(struct send_ctx *sctx) |
| 6987 | { |
| 6988 | struct btrfs_root *root = sctx->parent_root; |
| 6989 | int ret; |
| 6990 | int i; |
| 6991 | |
| 6992 | if (root) { |
| 6993 | ret = btrfs_start_delalloc_snapshot(root); |
| 6994 | if (ret) |
| 6995 | return ret; |
| 6996 | btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX); |
| 6997 | } |
| 6998 | |
| 6999 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 7000 | root = sctx->clone_roots[i].root; |
| 7001 | ret = btrfs_start_delalloc_snapshot(root); |
| 7002 | if (ret) |
| 7003 | return ret; |
| 7004 | btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX); |
| 7005 | } |
| 7006 | |
| 7007 | return 0; |
| 7008 | } |
| 7009 | |
David Sterba | 66ef7d6 | 2013-12-17 15:07:20 +0100 | [diff] [blame] | 7010 | static void btrfs_root_dec_send_in_progress(struct btrfs_root* root) |
| 7011 | { |
| 7012 | spin_lock(&root->root_item_lock); |
| 7013 | root->send_in_progress--; |
| 7014 | /* |
| 7015 | * Not much left to do, we don't know why it's unbalanced and |
| 7016 | * can't blindly reset it to 0. |
| 7017 | */ |
| 7018 | if (root->send_in_progress < 0) |
| 7019 | btrfs_err(root->fs_info, |
Colin Ian King | f5686e3 | 2018-05-04 12:11:12 +0100 | [diff] [blame] | 7020 | "send_in_progress unbalanced %d root %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7021 | root->send_in_progress, root->root_key.objectid); |
David Sterba | 66ef7d6 | 2013-12-17 15:07:20 +0100 | [diff] [blame] | 7022 | spin_unlock(&root->root_item_lock); |
| 7023 | } |
| 7024 | |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7025 | static void dedupe_in_progress_warn(const struct btrfs_root *root) |
| 7026 | { |
| 7027 | btrfs_warn_rl(root->fs_info, |
| 7028 | "cannot use root %llu for send while deduplications on it are in progress (%d in progress)", |
| 7029 | root->root_key.objectid, root->dedupe_in_progress); |
| 7030 | } |
| 7031 | |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 7032 | 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] | 7033 | { |
| 7034 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7035 | struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root; |
| 7036 | struct btrfs_fs_info *fs_info = send_root->fs_info; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7037 | struct btrfs_root *clone_root; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7038 | struct btrfs_key key; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7039 | struct send_ctx *sctx = NULL; |
| 7040 | u32 i; |
| 7041 | u64 *clone_sources_tmp = NULL; |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7042 | int clone_sources_to_rollback = 0; |
David Sterba | e55d115 | 2016-04-11 18:52:02 +0200 | [diff] [blame] | 7043 | unsigned alloc_size; |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7044 | int sort_clone_roots = 0; |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7045 | int index; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7046 | |
| 7047 | if (!capable(CAP_SYS_ADMIN)) |
| 7048 | return -EPERM; |
| 7049 | |
Josef Bacik | 139f807 | 2013-05-20 11:26:50 -0400 | [diff] [blame] | 7050 | /* |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7051 | * The subvolume must remain read-only during send, protect against |
David Sterba | 521e054 | 2014-04-15 16:41:44 +0200 | [diff] [blame] | 7052 | * making it RW. This also protects against deletion. |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7053 | */ |
| 7054 | spin_lock(&send_root->root_item_lock); |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7055 | if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) { |
| 7056 | dedupe_in_progress_warn(send_root); |
| 7057 | spin_unlock(&send_root->root_item_lock); |
| 7058 | return -EAGAIN; |
| 7059 | } |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7060 | send_root->send_in_progress++; |
| 7061 | spin_unlock(&send_root->root_item_lock); |
| 7062 | |
| 7063 | /* |
Josef Bacik | 139f807 | 2013-05-20 11:26:50 -0400 | [diff] [blame] | 7064 | * This is done when we lookup the root, it should already be complete |
| 7065 | * by the time we get here. |
| 7066 | */ |
| 7067 | WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE); |
| 7068 | |
| 7069 | /* |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7070 | * Userspace tools do the checks and warn the user if it's |
| 7071 | * not RO. |
| 7072 | */ |
| 7073 | if (!btrfs_root_readonly(send_root)) { |
| 7074 | ret = -EPERM; |
| 7075 | goto out; |
| 7076 | } |
| 7077 | |
Dan Carpenter | 457ae72 | 2017-03-17 23:51:20 +0300 | [diff] [blame] | 7078 | /* |
| 7079 | * Check that we don't overflow at later allocations, we request |
| 7080 | * clone_sources_count + 1 items, and compare to unsigned long inside |
| 7081 | * access_ok. |
| 7082 | */ |
Dan Carpenter | f5ecec3 | 2016-04-13 09:40:59 +0300 | [diff] [blame] | 7083 | if (arg->clone_sources_count > |
Dan Carpenter | 457ae72 | 2017-03-17 23:51:20 +0300 | [diff] [blame] | 7084 | ULONG_MAX / sizeof(struct clone_root) - 1) { |
Dan Carpenter | f5ecec3 | 2016-04-13 09:40:59 +0300 | [diff] [blame] | 7085 | ret = -EINVAL; |
| 7086 | goto out; |
| 7087 | } |
| 7088 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 7089 | if (!access_ok(arg->clone_sources, |
Dan Carpenter | 700ff4f | 2013-01-10 03:57:25 -0500 | [diff] [blame] | 7090 | sizeof(*arg->clone_sources) * |
| 7091 | arg->clone_sources_count)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7092 | ret = -EFAULT; |
| 7093 | goto out; |
| 7094 | } |
| 7095 | |
Stefan Behrens | c2c7132 | 2013-04-10 17:10:52 +0000 | [diff] [blame] | 7096 | if (arg->flags & ~BTRFS_SEND_FLAG_MASK) { |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 7097 | ret = -EINVAL; |
| 7098 | goto out; |
| 7099 | } |
| 7100 | |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 7101 | sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7102 | if (!sctx) { |
| 7103 | ret = -ENOMEM; |
| 7104 | goto out; |
| 7105 | } |
| 7106 | |
| 7107 | INIT_LIST_HEAD(&sctx->new_refs); |
| 7108 | INIT_LIST_HEAD(&sctx->deleted_refs); |
David Sterba | e780b0d | 2016-01-18 18:42:13 +0100 | [diff] [blame] | 7109 | INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7110 | INIT_LIST_HEAD(&sctx->name_cache_list); |
| 7111 | |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 7112 | sctx->flags = arg->flags; |
| 7113 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7114 | sctx->send_filp = fget(arg->send_fd); |
Tsutomu Itoh | ecc7ada | 2013-04-19 01:04:46 +0000 | [diff] [blame] | 7115 | if (!sctx->send_filp) { |
| 7116 | ret = -EBADF; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7117 | goto out; |
| 7118 | } |
| 7119 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7120 | sctx->send_root = send_root; |
David Sterba | 521e054 | 2014-04-15 16:41:44 +0200 | [diff] [blame] | 7121 | /* |
| 7122 | * Unlikely but possible, if the subvolume is marked for deletion but |
| 7123 | * is slow to remove the directory entry, send can still be started |
| 7124 | */ |
| 7125 | if (btrfs_root_dead(sctx->send_root)) { |
| 7126 | ret = -EPERM; |
| 7127 | goto out; |
| 7128 | } |
| 7129 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7130 | sctx->clone_roots_cnt = arg->clone_sources_count; |
| 7131 | |
| 7132 | sctx->send_max_size = BTRFS_SEND_BUF_SIZE; |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7133 | sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7134 | if (!sctx->send_buf) { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7135 | ret = -ENOMEM; |
| 7136 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7137 | } |
| 7138 | |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7139 | sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7140 | if (!sctx->read_buf) { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7141 | ret = -ENOMEM; |
| 7142 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7143 | } |
| 7144 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 7145 | sctx->pending_dir_moves = RB_ROOT; |
| 7146 | sctx->waiting_dir_moves = RB_ROOT; |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 7147 | sctx->orphan_dirs = RB_ROOT; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 7148 | |
David Sterba | e55d115 | 2016-04-11 18:52:02 +0200 | [diff] [blame] | 7149 | alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1); |
| 7150 | |
David Sterba | 818e010 | 2017-05-31 18:40:02 +0200 | [diff] [blame] | 7151 | sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7152 | if (!sctx->clone_roots) { |
David Sterba | 818e010 | 2017-05-31 18:40:02 +0200 | [diff] [blame] | 7153 | ret = -ENOMEM; |
| 7154 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7155 | } |
| 7156 | |
David Sterba | e55d115 | 2016-04-11 18:52:02 +0200 | [diff] [blame] | 7157 | alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources); |
| 7158 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7159 | if (arg->clone_sources_count) { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7160 | clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7161 | if (!clone_sources_tmp) { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 7162 | ret = -ENOMEM; |
| 7163 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7164 | } |
| 7165 | |
| 7166 | ret = copy_from_user(clone_sources_tmp, arg->clone_sources, |
David Sterba | e55d115 | 2016-04-11 18:52:02 +0200 | [diff] [blame] | 7167 | alloc_size); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7168 | if (ret) { |
| 7169 | ret = -EFAULT; |
| 7170 | goto out; |
| 7171 | } |
| 7172 | |
| 7173 | for (i = 0; i < arg->clone_sources_count; i++) { |
| 7174 | key.objectid = clone_sources_tmp[i]; |
| 7175 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 7176 | key.offset = (u64)-1; |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7177 | |
| 7178 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 7179 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7180 | clone_root = btrfs_read_fs_root_no_name(fs_info, &key); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7181 | if (IS_ERR(clone_root)) { |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7182 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7183 | ret = PTR_ERR(clone_root); |
| 7184 | goto out; |
| 7185 | } |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7186 | spin_lock(&clone_root->root_item_lock); |
Filipe Manana | 5cc2b17 | 2015-03-02 20:53:52 +0000 | [diff] [blame] | 7187 | if (!btrfs_root_readonly(clone_root) || |
| 7188 | btrfs_root_dead(clone_root)) { |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7189 | spin_unlock(&clone_root->root_item_lock); |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7190 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7191 | ret = -EPERM; |
| 7192 | goto out; |
| 7193 | } |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7194 | if (clone_root->dedupe_in_progress) { |
| 7195 | dedupe_in_progress_warn(clone_root); |
| 7196 | spin_unlock(&clone_root->root_item_lock); |
| 7197 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 7198 | ret = -EAGAIN; |
| 7199 | goto out; |
| 7200 | } |
Filipe Manana | 2f1f465 | 2015-03-02 20:53:53 +0000 | [diff] [blame] | 7201 | clone_root->send_in_progress++; |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7202 | spin_unlock(&clone_root->root_item_lock); |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7203 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 7204 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7205 | sctx->clone_roots[i].root = clone_root; |
Filipe Manana | 2f1f465 | 2015-03-02 20:53:53 +0000 | [diff] [blame] | 7206 | clone_sources_to_rollback = i + 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7207 | } |
David Sterba | 2f91306 | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 7208 | kvfree(clone_sources_tmp); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7209 | clone_sources_tmp = NULL; |
| 7210 | } |
| 7211 | |
| 7212 | if (arg->parent_root) { |
| 7213 | key.objectid = arg->parent_root; |
| 7214 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 7215 | key.offset = (u64)-1; |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7216 | |
| 7217 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 7218 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7219 | sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key); |
Stefan Behrens | b1b1959 | 2013-05-13 14:42:57 +0000 | [diff] [blame] | 7220 | if (IS_ERR(sctx->parent_root)) { |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7221 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Stefan Behrens | b1b1959 | 2013-05-13 14:42:57 +0000 | [diff] [blame] | 7222 | ret = PTR_ERR(sctx->parent_root); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7223 | goto out; |
| 7224 | } |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7225 | |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7226 | spin_lock(&sctx->parent_root->root_item_lock); |
| 7227 | sctx->parent_root->send_in_progress++; |
David Sterba | 521e054 | 2014-04-15 16:41:44 +0200 | [diff] [blame] | 7228 | if (!btrfs_root_readonly(sctx->parent_root) || |
| 7229 | btrfs_root_dead(sctx->parent_root)) { |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7230 | spin_unlock(&sctx->parent_root->root_item_lock); |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7231 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7232 | ret = -EPERM; |
| 7233 | goto out; |
| 7234 | } |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 7235 | if (sctx->parent_root->dedupe_in_progress) { |
| 7236 | dedupe_in_progress_warn(sctx->parent_root); |
| 7237 | spin_unlock(&sctx->parent_root->root_item_lock); |
| 7238 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 7239 | ret = -EAGAIN; |
| 7240 | goto out; |
| 7241 | } |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7242 | spin_unlock(&sctx->parent_root->root_item_lock); |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 7243 | |
| 7244 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7245 | } |
| 7246 | |
| 7247 | /* |
| 7248 | * Clones from send_root are allowed, but only if the clone source |
| 7249 | * is behind the current send position. This is checked while searching |
| 7250 | * for possible clone sources. |
| 7251 | */ |
| 7252 | sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root; |
| 7253 | |
| 7254 | /* We do a bsearch later */ |
| 7255 | sort(sctx->clone_roots, sctx->clone_roots_cnt, |
| 7256 | sizeof(*sctx->clone_roots), __clone_root_cmp_sort, |
| 7257 | NULL); |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7258 | sort_clone_roots = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7259 | |
Filipe Manana | 9f89d5d | 2019-04-15 09:29:36 +0100 | [diff] [blame] | 7260 | ret = flush_delalloc_roots(sctx); |
| 7261 | if (ret) |
| 7262 | goto out; |
| 7263 | |
Filipe Manana | e5fa8f8 | 2014-10-21 11:11:41 +0100 | [diff] [blame] | 7264 | ret = ensure_commit_roots_uptodate(sctx); |
| 7265 | if (ret) |
| 7266 | goto out; |
| 7267 | |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 7268 | mutex_lock(&fs_info->balance_mutex); |
| 7269 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
| 7270 | mutex_unlock(&fs_info->balance_mutex); |
| 7271 | btrfs_warn_rl(fs_info, |
| 7272 | "cannot run send because a balance operation is in progress"); |
| 7273 | ret = -EAGAIN; |
| 7274 | goto out; |
| 7275 | } |
| 7276 | fs_info->send_in_progress++; |
| 7277 | mutex_unlock(&fs_info->balance_mutex); |
| 7278 | |
David Sterba | 2755a0d | 2014-07-31 00:43:18 +0200 | [diff] [blame] | 7279 | current->journal_info = BTRFS_SEND_TRANS_STUB; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7280 | ret = send_subvol(sctx); |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 7281 | current->journal_info = NULL; |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 7282 | mutex_lock(&fs_info->balance_mutex); |
| 7283 | fs_info->send_in_progress--; |
| 7284 | mutex_unlock(&fs_info->balance_mutex); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7285 | if (ret < 0) |
| 7286 | goto out; |
| 7287 | |
Stefan Behrens | c2c7132 | 2013-04-10 17:10:52 +0000 | [diff] [blame] | 7288 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) { |
| 7289 | ret = begin_cmd(sctx, BTRFS_SEND_C_END); |
| 7290 | if (ret < 0) |
| 7291 | goto out; |
| 7292 | ret = send_cmd(sctx); |
| 7293 | if (ret < 0) |
| 7294 | goto out; |
| 7295 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7296 | |
| 7297 | out: |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 7298 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)); |
| 7299 | while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) { |
| 7300 | struct rb_node *n; |
| 7301 | struct pending_dir_move *pm; |
| 7302 | |
| 7303 | n = rb_first(&sctx->pending_dir_moves); |
| 7304 | pm = rb_entry(n, struct pending_dir_move, node); |
| 7305 | while (!list_empty(&pm->list)) { |
| 7306 | struct pending_dir_move *pm2; |
| 7307 | |
| 7308 | pm2 = list_first_entry(&pm->list, |
| 7309 | struct pending_dir_move, list); |
| 7310 | free_pending_move(sctx, pm2); |
| 7311 | } |
| 7312 | free_pending_move(sctx, pm); |
| 7313 | } |
| 7314 | |
| 7315 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)); |
| 7316 | while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) { |
| 7317 | struct rb_node *n; |
| 7318 | struct waiting_dir_move *dm; |
| 7319 | |
| 7320 | n = rb_first(&sctx->waiting_dir_moves); |
| 7321 | dm = rb_entry(n, struct waiting_dir_move, node); |
| 7322 | rb_erase(&dm->node, &sctx->waiting_dir_moves); |
| 7323 | kfree(dm); |
| 7324 | } |
| 7325 | |
Filipe Manana | 9dc4421 | 2014-02-19 14:31:44 +0000 | [diff] [blame] | 7326 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs)); |
| 7327 | while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) { |
| 7328 | struct rb_node *n; |
| 7329 | struct orphan_dir_info *odi; |
| 7330 | |
| 7331 | n = rb_first(&sctx->orphan_dirs); |
| 7332 | odi = rb_entry(n, struct orphan_dir_info, node); |
| 7333 | free_orphan_dir_info(sctx, odi); |
| 7334 | } |
| 7335 | |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 7336 | if (sort_clone_roots) { |
| 7337 | for (i = 0; i < sctx->clone_roots_cnt; i++) |
| 7338 | btrfs_root_dec_send_in_progress( |
| 7339 | sctx->clone_roots[i].root); |
| 7340 | } else { |
| 7341 | for (i = 0; sctx && i < clone_sources_to_rollback; i++) |
| 7342 | btrfs_root_dec_send_in_progress( |
| 7343 | sctx->clone_roots[i].root); |
| 7344 | |
| 7345 | btrfs_root_dec_send_in_progress(send_root); |
| 7346 | } |
David Sterba | 66ef7d6 | 2013-12-17 15:07:20 +0100 | [diff] [blame] | 7347 | if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) |
| 7348 | btrfs_root_dec_send_in_progress(sctx->parent_root); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 7349 | |
David Sterba | 2f91306 | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 7350 | kvfree(clone_sources_tmp); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7351 | |
| 7352 | if (sctx) { |
| 7353 | if (sctx->send_filp) |
| 7354 | fput(sctx->send_filp); |
| 7355 | |
David Sterba | c03d01f | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 7356 | kvfree(sctx->clone_roots); |
David Sterba | 6ff48ce | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 7357 | kvfree(sctx->send_buf); |
David Sterba | eb5b75f | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 7358 | kvfree(sctx->read_buf); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 7359 | |
| 7360 | name_cache_free(sctx); |
| 7361 | |
| 7362 | kfree(sctx); |
| 7363 | } |
| 7364 | |
| 7365 | return ret; |
| 7366 | } |