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