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