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